1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_LOCATION_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_LOCATION_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <stddef.h> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include <cassert> 11*635a8641SAndroid Build Coastguard Worker #include <string> 12*635a8641SAndroid Build Coastguard Worker 13*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 14*635a8641SAndroid Build Coastguard Worker #include "base/debug/debugging_buildflags.h" 15*635a8641SAndroid Build Coastguard Worker #include "base/hash.h" 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker namespace base { 18*635a8641SAndroid Build Coastguard Worker 19*635a8641SAndroid Build Coastguard Worker // Location provides basic info where of an object was constructed, or was 20*635a8641SAndroid Build Coastguard Worker // significantly brought to life. 21*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT Location { 22*635a8641SAndroid Build Coastguard Worker public: 23*635a8641SAndroid Build Coastguard Worker Location(); 24*635a8641SAndroid Build Coastguard Worker Location(const Location& other); 25*635a8641SAndroid Build Coastguard Worker 26*635a8641SAndroid Build Coastguard Worker // Only initializes the file name and program counter, the source information 27*635a8641SAndroid Build Coastguard Worker // will be null for the strings, and -1 for the line number. 28*635a8641SAndroid Build Coastguard Worker // TODO(http://crbug.com/760702) remove file name from this constructor. 29*635a8641SAndroid Build Coastguard Worker Location(const char* file_name, const void* program_counter); 30*635a8641SAndroid Build Coastguard Worker 31*635a8641SAndroid Build Coastguard Worker // Constructor should be called with a long-lived char*, such as __FILE__. 32*635a8641SAndroid Build Coastguard Worker // It assumes the provided value will persist as a global constant, and it 33*635a8641SAndroid Build Coastguard Worker // will not make a copy of it. 34*635a8641SAndroid Build Coastguard Worker Location(const char* function_name, 35*635a8641SAndroid Build Coastguard Worker const char* file_name, 36*635a8641SAndroid Build Coastguard Worker int line_number, 37*635a8641SAndroid Build Coastguard Worker const void* program_counter); 38*635a8641SAndroid Build Coastguard Worker 39*635a8641SAndroid Build Coastguard Worker // Comparator for hash map insertion. The program counter should uniquely 40*635a8641SAndroid Build Coastguard Worker // identify a location. 41*635a8641SAndroid Build Coastguard Worker bool operator==(const Location& other) const { 42*635a8641SAndroid Build Coastguard Worker return program_counter_ == other.program_counter_; 43*635a8641SAndroid Build Coastguard Worker } 44*635a8641SAndroid Build Coastguard Worker 45*635a8641SAndroid Build Coastguard Worker // Returns true if there is source code location info. If this is false, 46*635a8641SAndroid Build Coastguard Worker // the Location object only contains a program counter or is 47*635a8641SAndroid Build Coastguard Worker // default-initialized (the program counter is also null). has_source_info()48*635a8641SAndroid Build Coastguard Worker bool has_source_info() const { return function_name_ && file_name_; } 49*635a8641SAndroid Build Coastguard Worker 50*635a8641SAndroid Build Coastguard Worker // Will be nullptr for default initialized Location objects and when source 51*635a8641SAndroid Build Coastguard Worker // names are disabled. function_name()52*635a8641SAndroid Build Coastguard Worker const char* function_name() const { return function_name_; } 53*635a8641SAndroid Build Coastguard Worker 54*635a8641SAndroid Build Coastguard Worker // Will be nullptr for default initialized Location objects and when source 55*635a8641SAndroid Build Coastguard Worker // names are disabled. file_name()56*635a8641SAndroid Build Coastguard Worker const char* file_name() const { return file_name_; } 57*635a8641SAndroid Build Coastguard Worker 58*635a8641SAndroid Build Coastguard Worker // Will be -1 for default initialized Location objects and when source names 59*635a8641SAndroid Build Coastguard Worker // are disabled. line_number()60*635a8641SAndroid Build Coastguard Worker int line_number() const { return line_number_; } 61*635a8641SAndroid Build Coastguard Worker 62*635a8641SAndroid Build Coastguard Worker // The address of the code generating this Location object. Should always be 63*635a8641SAndroid Build Coastguard Worker // valid except for default initialized Location objects, which will be 64*635a8641SAndroid Build Coastguard Worker // nullptr. program_counter()65*635a8641SAndroid Build Coastguard Worker const void* program_counter() const { return program_counter_; } 66*635a8641SAndroid Build Coastguard Worker 67*635a8641SAndroid Build Coastguard Worker // Converts to the most user-readable form possible. If function and filename 68*635a8641SAndroid Build Coastguard Worker // are not available, this will return "pc:<hex address>". 69*635a8641SAndroid Build Coastguard Worker std::string ToString() const; 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard Worker static Location CreateFromHere(const char* file_name); 72*635a8641SAndroid Build Coastguard Worker static Location CreateFromHere(const char* function_name, 73*635a8641SAndroid Build Coastguard Worker const char* file_name, 74*635a8641SAndroid Build Coastguard Worker int line_number); 75*635a8641SAndroid Build Coastguard Worker 76*635a8641SAndroid Build Coastguard Worker private: 77*635a8641SAndroid Build Coastguard Worker const char* function_name_ = nullptr; 78*635a8641SAndroid Build Coastguard Worker const char* file_name_ = nullptr; 79*635a8641SAndroid Build Coastguard Worker int line_number_ = -1; 80*635a8641SAndroid Build Coastguard Worker const void* program_counter_ = nullptr; 81*635a8641SAndroid Build Coastguard Worker }; 82*635a8641SAndroid Build Coastguard Worker 83*635a8641SAndroid Build Coastguard Worker BASE_EXPORT const void* GetProgramCounter(); 84*635a8641SAndroid Build Coastguard Worker 85*635a8641SAndroid Build Coastguard Worker // The macros defined here will expand to the current function. 86*635a8641SAndroid Build Coastguard Worker #if BUILDFLAG(ENABLE_LOCATION_SOURCE) 87*635a8641SAndroid Build Coastguard Worker 88*635a8641SAndroid Build Coastguard Worker // Full source information should be included. 89*635a8641SAndroid Build Coastguard Worker #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__func__) 90*635a8641SAndroid Build Coastguard Worker #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ 91*635a8641SAndroid Build Coastguard Worker ::base::Location::CreateFromHere(function_name, __FILE__, __LINE__) 92*635a8641SAndroid Build Coastguard Worker 93*635a8641SAndroid Build Coastguard Worker #else 94*635a8641SAndroid Build Coastguard Worker 95*635a8641SAndroid Build Coastguard Worker // TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls. 96*635a8641SAndroid Build Coastguard Worker #define FROM_HERE ::base::Location::CreateFromHere(__FILE__) 97*635a8641SAndroid Build Coastguard Worker #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ 98*635a8641SAndroid Build Coastguard Worker ::base::Location::CreateFromHere(function_name, __FILE__, -1) 99*635a8641SAndroid Build Coastguard Worker 100*635a8641SAndroid Build Coastguard Worker #endif 101*635a8641SAndroid Build Coastguard Worker 102*635a8641SAndroid Build Coastguard Worker } // namespace base 103*635a8641SAndroid Build Coastguard Worker 104*635a8641SAndroid Build Coastguard Worker namespace std { 105*635a8641SAndroid Build Coastguard Worker 106*635a8641SAndroid Build Coastguard Worker // Specialization for using Location in hash tables. 107*635a8641SAndroid Build Coastguard Worker template <> 108*635a8641SAndroid Build Coastguard Worker struct hash<::base::Location> { 109*635a8641SAndroid Build Coastguard Worker std::size_t operator()(const ::base::Location& loc) const { 110*635a8641SAndroid Build Coastguard Worker const void* program_counter = loc.program_counter(); 111*635a8641SAndroid Build Coastguard Worker return base::Hash(&program_counter, sizeof(void*)); 112*635a8641SAndroid Build Coastguard Worker } 113*635a8641SAndroid Build Coastguard Worker }; 114*635a8641SAndroid Build Coastguard Worker 115*635a8641SAndroid Build Coastguard Worker } // namespace std 116*635a8641SAndroid Build Coastguard Worker 117*635a8641SAndroid Build Coastguard Worker #endif // BASE_LOCATION_H_ 118