Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[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 | |
kylechar | d917b98 | 2023-11-30 22:52:44 | [diff] [blame] | 8 | #include <compare> |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include "base/base_export.h" |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 12 | #include "base/memory/raw_ptr_exclusion.h" |
Alexander Timin | 26ac38f | 2021-10-05 00:50:36 | [diff] [blame] | 13 | #include "base/trace_event/base_tracing_forward.h" |
Jeremy Roman | a5999de | 2019-07-04 20:13:01 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 15 | |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 16 | namespace base { |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 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: |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 22 | Location(); |
| 23 | Location(const Location& other); |
Stephan Hartmann | 66f224a | 2021-12-07 23:52:23 | [diff] [blame] | 24 | Location(Location&& other) noexcept; |
Peter Kasting | e08ca1334 | 2021-07-08 02:46:57 | [diff] [blame] | 25 | Location& operator=(const Location& other); |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 26 | |
Daniel Cheng | cd6d6fb | 2022-10-22 03:38:32 | [diff] [blame] | 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 | } |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 33 | |
Hans Wennborg | e66cdda | 2021-04-21 11:02:31 | [diff] [blame] | 34 | // Comparator for testing. The program counter should uniquely |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 35 | // identify a location. |
kylechar | d917b98 | 2023-11-30 22:52:44 | [diff] [blame] | 36 | friend bool operator==(const Location& lhs, const Location& rhs) { |
| 37 | return lhs.program_counter_ == rhs.program_counter_; |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 38 | } |
| 39 | |
kylechar | d917b98 | 2023-11-30 22:52:44 | [diff] [blame] | 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_; |
Aditya Kushwah | 5a286b7 | 2022-02-10 04:54:43 | [diff] [blame] | 47 | } |
| 48 | |
Phillis Tang | 9f9c0fd | 2024-12-23 18:01:57 | [diff] [blame] | 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). |
| 52 | bool has_source_info() const { return function_name_ && file_name_; } |
| 53 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 54 | // Will be nullptr for default initialized Location objects and when source |
| 55 | // names are disabled. |
| 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. |
| 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. |
| 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. |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 69 | const void* program_counter() const { return program_counter_; } |
| 70 | |
Brett Wilson | 07ec3b5 | 2017-10-09 21:42:45 | [diff] [blame] | 71 | // Converts to the most user-readable form possible. If function and filename |
| 72 | // are not available, this will return "pc:<hex address>". |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 73 | std::string ToString() const; |
| 74 | |
Alexander Timin | 26ac38f | 2021-10-05 00:50:36 | [diff] [blame] | 75 | // Write a representation of this object into a trace. |
| 76 | void WriteIntoTrace(perfetto::TracedValue context) const; |
| 77 | |
Jeremy Roman | a5999de | 2019-07-04 20:13:01 | [diff] [blame] | 78 | static Location Current(const char* function_name = __builtin_FUNCTION(), |
| 79 | const char* file_name = __builtin_FILE(), |
| 80 | int line_number = __builtin_LINE()); |
Jeremy Roman | a5999de | 2019-07-04 20:13:01 | [diff] [blame] | 81 | |
Peter Boström | 5f34e2f | 2024-12-19 01:07:54 | [diff] [blame] | 82 | static Location CurrentWithoutFunctionName( |
| 83 | const char* file_name = __builtin_FILE(), |
| 84 | int line_number = __builtin_LINE()); |
| 85 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 86 | private: |
Daniel Cheng | cd6d6fb | 2022-10-22 03:38:32 | [diff] [blame] | 87 | // Only initializes the file name and program counter, the source information |
| 88 | // will be null for the strings, and -1 for the line number. |
| 89 | // TODO(http://crbug.com/760702) remove file name from this constructor. |
| 90 | Location(const char* file_name, const void* program_counter); |
| 91 | |
| 92 | // Constructor should be called with a long-lived char*, such as __FILE__. |
| 93 | // It assumes the provided value will persist as a global constant, and it |
| 94 | // will not make a copy of it. |
| 95 | Location(const char* function_name, |
| 96 | const char* file_name, |
| 97 | int line_number, |
| 98 | const void* program_counter); |
| 99 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 100 | const char* function_name_ = nullptr; |
| 101 | const char* file_name_ = nullptr; |
| 102 | int line_number_ = -1; |
Lukasz Anforowicz | 877e622 | 2021-11-26 00:03:23 | [diff] [blame] | 103 | |
| 104 | // `program_counter_` is not a raw_ptr<...> for performance reasons (based on |
| 105 | // analysis of sampling profiler data and tab_search:top100:2020). |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 106 | RAW_PTR_EXCLUSION const void* program_counter_ = nullptr; |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | BASE_EXPORT const void* GetProgramCounter(); |
| 110 | |
Daniel Cheng | e0f23c8e | 2020-02-25 21:03:27 | [diff] [blame] | 111 | #define FROM_HERE ::base::Location::Current() |
| 112 | |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 113 | } // namespace base |
| 114 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 115 | #endif // BASE_LOCATION_H_ |