[email protected] | ec6bf33 | 2012-03-16 21:43:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 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" |
Scott Violet | 4416579 | 2018-02-22 02:08:08 | [diff] [blame] | 11 | #include "base/debug/debugging_buildflags.h" |
Alexander Timin | 26ac38f | 2021-10-05 00:50:36 | [diff] [blame] | 12 | #include "base/trace_event/base_tracing_forward.h" |
Jeremy Roman | a5999de | 2019-07-04 20:13:01 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 14 | |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 15 | namespace base { |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 16 | |
Wanchang Ryu | 6ca0bda5b | 2020-07-21 12:08:48 | [diff] [blame] | 17 | #if defined(__clang__) |
Jeremy Roman | a5999de | 2019-07-04 20:13:01 | [diff] [blame] | 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 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 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: |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 34 | Location(); |
| 35 | Location(const Location& other); |
Stephan Hartmann | 66f224a | 2021-12-07 23:52:23 | [diff] [blame] | 36 | Location(Location&& other) noexcept; |
Peter Kasting | e08ca1334 | 2021-07-08 02:46:57 | [diff] [blame] | 37 | Location& operator=(const Location& other); |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 38 | |
| 39 | // Only initializes the file name and program counter, the source information |
| 40 | // will be null for the strings, and -1 for the line number. |
| 41 | // TODO(http://crbug.com/760702) remove file name from this constructor. |
| 42 | Location(const char* file_name, const void* program_counter); |
| 43 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 44 | // Constructor should be called with a long-lived char*, such as __FILE__. |
| 45 | // It assumes the provided value will persist as a global constant, and it |
| 46 | // will not make a copy of it. |
| 47 | Location(const char* function_name, |
| 48 | const char* file_name, |
| 49 | int line_number, |
| 50 | const void* program_counter); |
| 51 | |
Hans Wennborg | e66cdda | 2021-04-21 11:02:31 | [diff] [blame] | 52 | // Comparator for testing. The program counter should uniquely |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 53 | // identify a location. |
sque | e3b16f1 | 2015-03-14 02:39:03 | [diff] [blame] | 54 | bool operator==(const Location& other) const { |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 55 | return program_counter_ == other.program_counter_; |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 56 | } |
| 57 | |
Aditya Kushwah | 5a286b7 | 2022-02-10 04:54:43 | [diff] [blame^] | 58 | // Comparator is necessary to use location object within an ordered container |
| 59 | // type (eg. std::map). |
| 60 | bool operator<(const Location& other) const { |
| 61 | return program_counter_ < other.program_counter_; |
| 62 | } |
| 63 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 64 | // Returns true if there is source code location info. If this is false, |
| 65 | // the Location object only contains a program counter or is |
| 66 | // default-initialized (the program counter is also null). |
| 67 | bool has_source_info() const { return function_name_ && file_name_; } |
| 68 | |
| 69 | // Will be nullptr for default initialized Location objects and when source |
| 70 | // names are disabled. |
| 71 | const char* function_name() const { return function_name_; } |
| 72 | |
| 73 | // Will be nullptr for default initialized Location objects and when source |
| 74 | // names are disabled. |
| 75 | const char* file_name() const { return file_name_; } |
| 76 | |
| 77 | // Will be -1 for default initialized Location objects and when source names |
| 78 | // are disabled. |
| 79 | int line_number() const { return line_number_; } |
| 80 | |
| 81 | // The address of the code generating this Location object. Should always be |
| 82 | // valid except for default initialized Location objects, which will be |
| 83 | // nullptr. |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 84 | const void* program_counter() const { return program_counter_; } |
| 85 | |
Brett Wilson | 07ec3b5 | 2017-10-09 21:42:45 | [diff] [blame] | 86 | // Converts to the most user-readable form possible. If function and filename |
| 87 | // are not available, this will return "pc:<hex address>". |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 88 | std::string ToString() const; |
| 89 | |
Alexander Timin | 26ac38f | 2021-10-05 00:50:36 | [diff] [blame] | 90 | // Write a representation of this object into a trace. |
| 91 | void WriteIntoTrace(perfetto::TracedValue context) const; |
| 92 | |
Daniel Cheng | e0f23c8e | 2020-02-25 21:03:27 | [diff] [blame] | 93 | #if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS) |
| 94 | #if !BUILDFLAG(ENABLE_LOCATION_SOURCE) |
tzik | 4f05a25a | 2017-09-13 00:23:16 | [diff] [blame] | 95 | static Location CreateFromHere(const char* file_name); |
Daniel Cheng | e0f23c8e | 2020-02-25 21:03:27 | [diff] [blame] | 96 | #else |
tzik | 4f05a25a | 2017-09-13 00:23:16 | [diff] [blame] | 97 | static Location CreateFromHere(const char* function_name, |
| 98 | const char* file_name, |
| 99 | int line_number); |
Daniel Cheng | e0f23c8e | 2020-02-25 21:03:27 | [diff] [blame] | 100 | #endif |
| 101 | #endif |
tzik | 4f05a25a | 2017-09-13 00:23:16 | [diff] [blame] | 102 | |
Jeremy Roman | a5999de | 2019-07-04 20:13:01 | [diff] [blame] | 103 | #if SUPPORTS_LOCATION_BUILTINS && BUILDFLAG(ENABLE_LOCATION_SOURCE) |
| 104 | static Location Current(const char* function_name = __builtin_FUNCTION(), |
| 105 | const char* file_name = __builtin_FILE(), |
| 106 | int line_number = __builtin_LINE()); |
| 107 | #elif SUPPORTS_LOCATION_BUILTINS |
| 108 | static Location Current(const char* file_name = __builtin_FILE()); |
| 109 | #else |
| 110 | static Location Current(); |
| 111 | #endif |
| 112 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 113 | private: |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 114 | const char* function_name_ = nullptr; |
| 115 | const char* file_name_ = nullptr; |
| 116 | int line_number_ = -1; |
Lukasz Anforowicz | 877e622 | 2021-11-26 00:03:23 | [diff] [blame] | 117 | |
| 118 | // `program_counter_` is not a raw_ptr<...> for performance reasons (based on |
| 119 | // analysis of sampling profiler data and tab_search:top100:2020). |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 120 | const void* program_counter_ = nullptr; |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | BASE_EXPORT const void* GetProgramCounter(); |
| 124 | |
Daniel Cheng | e0f23c8e | 2020-02-25 21:03:27 | [diff] [blame] | 125 | #if BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS) |
| 126 | |
| 127 | #define FROM_HERE ::base::Location::Current() |
| 128 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 129 | // The macros defined here will expand to the current function. |
Daniel Cheng | e0f23c8e | 2020-02-25 21:03:27 | [diff] [blame] | 130 | #elif BUILDFLAG(ENABLE_LOCATION_SOURCE) |
Brett Wilson | 5b5b7cb | 2017-09-01 04:40:02 | [diff] [blame] | 131 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 132 | // Full source information should be included. |
Daniel Cheng | e0f23c8e | 2020-02-25 21:03:27 | [diff] [blame] | 133 | #define FROM_HERE ::base::Location::CreateFromHere(__func__, __FILE__, __LINE__) |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 134 | |
| 135 | #else |
| 136 | |
| 137 | // TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls. |
tzik | 4f05a25a | 2017-09-13 00:23:16 | [diff] [blame] | 138 | #define FROM_HERE ::base::Location::CreateFromHere(__FILE__) |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 139 | |
| 140 | #endif |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 141 | |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 142 | } // namespace base |
| 143 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 144 | #endif // BASE_LOCATION_H_ |