location.h (4593B)
1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_LOCATION_H_ 6 #define BASE_LOCATION_H_ 7 8 #include <string> 9 10 #include "base/base_export.h" 11 #include "base/memory/raw_ptr_exclusion.h" 12 #include "base/trace_event/base_tracing_forward.h" 13 #include "build/build_config.h" 14 15 namespace base { 16 17 #if defined(__has_builtin) 18 // Clang allows detection of these builtins. 19 # define SUPPORTS_LOCATION_BUILTINS \ 20 (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \ 21 __has_builtin(__builtin_LINE)) 22 #elif defined(COMPILER_GCC) && __GNUC__ >= 7 23 // GCC has supported these for a long time, but they point at the function 24 // declaration in the case of default arguments, rather than at the call site. 25 # define SUPPORTS_LOCATION_BUILTINS 1 26 #else 27 # define SUPPORTS_LOCATION_BUILTINS 0 28 #endif 29 30 // Location provides basic info where of an object was constructed, or was 31 // significantly brought to life. 32 class BASE_EXPORT Location { 33 public: 34 Location(); 35 Location(const Location& other); 36 Location(Location&& other) noexcept; 37 Location& operator=(const Location& other); 38 39 static Location CreateForTesting(const char* function_name, 40 const char* file_name, 41 int line_number, 42 const void* program_counter) { 43 return Location(function_name, file_name, line_number, program_counter); 44 } 45 46 // Comparator for testing. The program counter should uniquely 47 // identify a location. 48 bool operator==(const Location& other) const { 49 return program_counter_ == other.program_counter_; 50 } 51 52 // Comparator is necessary to use location object within an ordered container 53 // type (eg. std::map). 54 bool operator<(const Location& other) const { 55 return program_counter_ < other.program_counter_; 56 } 57 58 // Returns true if there is source code location info. If this is false, 59 // the Location object only contains a program counter or is 60 // default-initialized (the program counter is also null). 61 bool has_source_info() const { return function_name_ && file_name_; } 62 63 // Will be nullptr for default initialized Location objects and when source 64 // names are disabled. 65 const char* function_name() const { return function_name_; } 66 67 // Will be nullptr for default initialized Location objects and when source 68 // names are disabled. 69 const char* file_name() const { return file_name_; } 70 71 // Will be -1 for default initialized Location objects and when source names 72 // are disabled. 73 int line_number() const { return line_number_; } 74 75 // The address of the code generating this Location object. Should always be 76 // valid except for default initialized Location objects, which will be 77 // nullptr. 78 const void* program_counter() const { return program_counter_; } 79 80 // Converts to the most user-readable form possible. If function and filename 81 // are not available, this will return "pc:<hex address>". 82 std::string ToString() const; 83 84 // Write a representation of this object into a trace. 85 void WriteIntoTrace(perfetto::TracedValue context) const; 86 87 #if SUPPORTS_LOCATION_BUILTINS 88 static Location Current(const char* function_name = __builtin_FUNCTION(), 89 const char* file_name = __builtin_FILE(), 90 int line_number = __builtin_LINE()); 91 #else 92 static Location Current(); 93 #endif 94 95 private: 96 // Only initializes the file name and program counter, the source information 97 // will be null for the strings, and -1 for the line number. 98 // TODO(http://crbug.com/760702) remove file name from this constructor. 99 Location(const char* file_name, const void* program_counter); 100 101 // Constructor should be called with a long-lived char*, such as __FILE__. 102 // It assumes the provided value will persist as a global constant, and it 103 // will not make a copy of it. 104 Location(const char* function_name, 105 const char* file_name, 106 int line_number, 107 const void* program_counter); 108 109 const char* function_name_ = nullptr; 110 const char* file_name_ = nullptr; 111 int line_number_ = -1; 112 113 // `program_counter_` is not a raw_ptr<...> for performance reasons (based on 114 // analysis of sampling profiler data and tab_search:top100:2020). 115 RAW_PTR_EXCLUSION const void* program_counter_ = nullptr; 116 }; 117 118 BASE_EXPORT const void* GetProgramCounter(); 119 120 #define FROM_HERE ::base::Location::Current() 121 122 } // namespace base 123 124 #endif // BASE_LOCATION_H_