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