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 <compare> 9 #include <string> 10 11 #include "base/base_export.h" 12 #include "base/memory/raw_ptr_exclusion.h" 13 #include "base/trace_event/base_tracing_forward.h" 14 #include "build/build_config.h" 15 16 namespace base { 17 18 // Location provides basic info where of an object was constructed, or was 19 // significantly brought to life. 20 class BASE_EXPORT Location { 21 public: 22 Location(); 23 Location(const Location& other); 24 Location(Location&& other) noexcept; 25 Location& operator=(const Location& other); 26 CreateForTesting(const char * function_name,const char * file_name,int line_number,const void * program_counter)27 static Location CreateForTesting(const char* function_name, 28 const char* file_name, 29 int line_number, 30 const void* program_counter) { 31 return Location(function_name, file_name, line_number, program_counter); 32 } 33 34 // Comparator for testing. The program counter should uniquely 35 // identify a location. 36 friend bool operator==(const Location& lhs, const Location& rhs) { 37 return lhs.program_counter_ == rhs.program_counter_; 38 } 39 40 // The program counter should uniquely identify a location. There is no 41 // guarantee that a program counter corresponds to unique function/file/line 42 // values, based on how it's constructed, and therefore equivalent locations 43 // could be distinguishable. 44 friend std::weak_ordering operator<=>(const Location& lhs, 45 const Location& rhs) { 46 return lhs.program_counter_ <=> rhs.program_counter_; 47 } 48 49 // Returns true if there is source code location info. If this is false, 50 // the Location object only contains a program counter or is 51 // default-initialized (the program counter is also null). has_source_info()52 bool has_source_info() const { return function_name_ && file_name_; } 53 54 // Will be nullptr for default initialized Location objects and when source 55 // names are disabled. function_name()56 const char* function_name() const { return function_name_; } 57 58 // Will be nullptr for default initialized Location objects and when source 59 // names are disabled. file_name()60 const char* file_name() const { return file_name_; } 61 62 // Will be -1 for default initialized Location objects and when source names 63 // are disabled. line_number()64 int line_number() const { return line_number_; } 65 66 // The address of the code generating this Location object. Should always be 67 // valid except for default initialized Location objects, which will be 68 // nullptr. program_counter()69 const void* program_counter() const { return program_counter_; } 70 71 // Converts to the most user-readable form possible. If function and filename 72 // are not available, this will return "pc:<hex address>". 73 std::string ToString() const; 74 75 // Write a representation of this object into a trace. 76 void WriteIntoTrace(perfetto::TracedValue context) const; 77 78 static Location Current(const char* function_name = __builtin_FUNCTION(), 79 const char* file_name = __builtin_FILE(), 80 int line_number = __builtin_LINE()); 81 82 private: 83 // Only initializes the file name and program counter, the source information 84 // will be null for the strings, and -1 for the line number. 85 // TODO(http://crbug.com/760702) remove file name from this constructor. 86 Location(const char* file_name, const void* program_counter); 87 88 // Constructor should be called with a long-lived char*, such as __FILE__. 89 // It assumes the provided value will persist as a global constant, and it 90 // will not make a copy of it. 91 Location(const char* function_name, 92 const char* file_name, 93 int line_number, 94 const void* program_counter); 95 96 const char* function_name_ = nullptr; 97 const char* file_name_ = nullptr; 98 int line_number_ = -1; 99 100 // `program_counter_` is not a raw_ptr<...> for performance reasons (based on 101 // analysis of sampling profiler data and tab_search:top100:2020). 102 RAW_PTR_EXCLUSION const void* program_counter_ = nullptr; 103 }; 104 105 BASE_EXPORT const void* GetProgramCounter(); 106 107 #define FROM_HERE ::base::Location::Current() 108 109 } // namespace base 110 111 #endif // BASE_LOCATION_H_ 112