blob: f324c409cf2386b0471b718397a6f1b6c5773d4a [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]c62dd9d2011-09-21 18:05:412// 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
kylechard917b982023-11-30 22:52:448#include <compare>
[email protected]c62dd9d2011-09-21 18:05:419#include <string>
10
11#include "base/base_export.h"
Keishi Hattori488b7602022-05-02 13:09:3112#include "base/memory/raw_ptr_exclusion.h"
Alexander Timin26ac38f2021-10-05 00:50:3613#include "base/trace_event/base_tracing_forward.h"
Jeremy Romana5999de2019-07-04 20:13:0114#include "build/build_config.h"
[email protected]c62dd9d2011-09-21 18:05:4115
Brett Wilsonabbb9602017-09-11 23:26:3916namespace base {
[email protected]c62dd9d2011-09-21 18:05:4117
18// Location provides basic info where of an object was constructed, or was
19// significantly brought to life.
20class BASE_EXPORT Location {
21 public:
Brett Wilsonc794cf62017-09-01 20:14:3322 Location();
23 Location(const Location& other);
Stephan Hartmann66f224a2021-12-07 23:52:2324 Location(Location&& other) noexcept;
Peter Kastinge08ca13342021-07-08 02:46:5725 Location& operator=(const Location& other);
Brett Wilsonc794cf62017-09-01 20:14:3326
Daniel Chengcd6d6fb2022-10-22 03:38:3227 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]c62dd9d2011-09-21 18:05:4133
Hans Wennborge66cdda2021-04-21 11:02:3134 // Comparator for testing. The program counter should uniquely
Brett Wilsonc794cf62017-09-01 20:14:3335 // identify a location.
kylechard917b982023-11-30 22:52:4436 friend bool operator==(const Location& lhs, const Location& rhs) {
37 return lhs.program_counter_ == rhs.program_counter_;
[email protected]c62dd9d2011-09-21 18:05:4138 }
39
kylechard917b982023-11-30 22:52:4440 // 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 Kushwah5a286b72022-02-10 04:54:4347 }
48
Phillis Tang9f9c0fd2024-12-23 18:01:5749 // 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 Wilsonc794cf62017-09-01 20:14:3354 // 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]c62dd9d2011-09-21 18:05:4169 const void* program_counter() const { return program_counter_; }
70
Brett Wilson07ec3b52017-10-09 21:42:4571 // Converts to the most user-readable form possible. If function and filename
72 // are not available, this will return "pc:<hex address>".
[email protected]c62dd9d2011-09-21 18:05:4173 std::string ToString() const;
74
Alexander Timin26ac38f2021-10-05 00:50:3675 // Write a representation of this object into a trace.
76 void WriteIntoTrace(perfetto::TracedValue context) const;
77
Jeremy Romana5999de2019-07-04 20:13:0178 static Location Current(const char* function_name = __builtin_FUNCTION(),
79 const char* file_name = __builtin_FILE(),
80 int line_number = __builtin_LINE());
Jeremy Romana5999de2019-07-04 20:13:0181
Peter Boström5f34e2f2024-12-19 01:07:5482 static Location CurrentWithoutFunctionName(
83 const char* file_name = __builtin_FILE(),
84 int line_number = __builtin_LINE());
85
[email protected]c62dd9d2011-09-21 18:05:4186 private:
Daniel Chengcd6d6fb2022-10-22 03:38:3287 // 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 Wilsonc794cf62017-09-01 20:14:33100 const char* function_name_ = nullptr;
101 const char* file_name_ = nullptr;
102 int line_number_ = -1;
Lukasz Anforowicz877e6222021-11-26 00:03:23103
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 Hattori488b7602022-05-02 13:09:31106 RAW_PTR_EXCLUSION const void* program_counter_ = nullptr;
[email protected]c62dd9d2011-09-21 18:05:41107};
108
109BASE_EXPORT const void* GetProgramCounter();
110
Daniel Chenge0f23c8e2020-02-25 21:03:27111#define FROM_HERE ::base::Location::Current()
112
Brett Wilsonabbb9602017-09-11 23:26:39113} // namespace base
114
[email protected]c62dd9d2011-09-21 18:05:41115#endif // BASE_LOCATION_H_