security/sandbox/chromium/base/location.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/location.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef BASE_LOCATION_H_
     1.9 +#define BASE_LOCATION_H_
    1.10 +
    1.11 +#include <string>
    1.12 +
    1.13 +#include "base/base_export.h"
    1.14 +#include "base/basictypes.h"
    1.15 +
    1.16 +namespace tracked_objects {
    1.17 +
    1.18 +// Location provides basic info where of an object was constructed, or was
    1.19 +// significantly brought to life.
    1.20 +class BASE_EXPORT Location {
    1.21 + public:
    1.22 +  // Constructor should be called with a long-lived char*, such as __FILE__.
    1.23 +  // It assumes the provided value will persist as a global constant, and it
    1.24 +  // will not make a copy of it.
    1.25 +  Location(const char* function_name,
    1.26 +           const char* file_name,
    1.27 +           int line_number,
    1.28 +           const void* program_counter);
    1.29 +
    1.30 +  // Provide a default constructor for easy of debugging.
    1.31 +  Location();
    1.32 +
    1.33 +  // Comparison operator for insertion into a std::map<> hash tables.
    1.34 +  // All we need is *some* (any) hashing distinction.  Strings should already
    1.35 +  // be unique, so we don't bother with strcmp or such.
    1.36 +  // Use line number as the primary key (because it is fast, and usually gets us
    1.37 +  // a difference), and then pointers as secondary keys (just to get some
    1.38 +  // distinctions).
    1.39 +  bool operator < (const Location& other) const {
    1.40 +    if (line_number_ != other.line_number_)
    1.41 +      return line_number_ < other.line_number_;
    1.42 +    if (file_name_ != other.file_name_)
    1.43 +      return file_name_ < other.file_name_;
    1.44 +    return function_name_ < other.function_name_;
    1.45 +  }
    1.46 +
    1.47 +  const char* function_name()   const { return function_name_; }
    1.48 +  const char* file_name()       const { return file_name_; }
    1.49 +  int line_number()             const { return line_number_; }
    1.50 +  const void* program_counter() const { return program_counter_; }
    1.51 +
    1.52 +  std::string ToString() const;
    1.53 +
    1.54 +  // Translate the some of the state in this instance into a human readable
    1.55 +  // string with HTML characters in the function names escaped, and append that
    1.56 +  // string to |output|.  Inclusion of the file_name_ and function_name_ are
    1.57 +  // optional, and controlled by the boolean arguments.
    1.58 +  void Write(bool display_filename, bool display_function_name,
    1.59 +             std::string* output) const;
    1.60 +
    1.61 +  // Write function_name_ in HTML with '<' and '>' properly encoded.
    1.62 +  void WriteFunctionName(std::string* output) const;
    1.63 +
    1.64 + private:
    1.65 +  const char* function_name_;
    1.66 +  const char* file_name_;
    1.67 +  int line_number_;
    1.68 +  const void* program_counter_;
    1.69 +};
    1.70 +
    1.71 +// A "snapshotted" representation of the Location class that can safely be
    1.72 +// passed across process boundaries.
    1.73 +struct BASE_EXPORT LocationSnapshot {
    1.74 +  // The default constructor is exposed to support the IPC serialization macros.
    1.75 +  LocationSnapshot();
    1.76 +  explicit LocationSnapshot(const tracked_objects::Location& location);
    1.77 +  ~LocationSnapshot();
    1.78 +
    1.79 +  std::string file_name;
    1.80 +  std::string function_name;
    1.81 +  int line_number;
    1.82 +};
    1.83 +
    1.84 +BASE_EXPORT const void* GetProgramCounter();
    1.85 +
    1.86 +// Define a macro to record the current source location.
    1.87 +#define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__FUNCTION__)
    1.88 +
    1.89 +#define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name)                        \
    1.90 +    ::tracked_objects::Location(function_name,                                 \
    1.91 +                                __FILE__,                                      \
    1.92 +                                __LINE__,                                      \
    1.93 +                                ::tracked_objects::GetProgramCounter())
    1.94 +
    1.95 +}  // namespace tracked_objects
    1.96 +
    1.97 +#endif  // BASE_LOCATION_H_

mercurial