blob: 50138fed7adfd58a19bc3b3300ec8fe1562df98e [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
8#include <string>
9
10#include "base/base_export.h"
Scott Violet44165792018-02-22 02:08:0811#include "base/debug/debugging_buildflags.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
Wanchang Ryu6ca0bda5b2020-07-21 12:08:4818#if defined(__clang__)
Jeremy Romana5999de2019-07-04 20:13:0119// Clang allows detection of these builtins.
20#define SUPPORTS_LOCATION_BUILTINS \
21 (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \
22 __has_builtin(__builtin_LINE))
23#elif defined(COMPILER_GCC) && __GNUC__ >= 7
24// GCC has supported these for a long time, but they point at the function
25// declaration in the case of default arguments, rather than at the call site.
26#define SUPPORTS_LOCATION_BUILTINS 1
27#else
28#define SUPPORTS_LOCATION_BUILTINS 0
29#endif
30
[email protected]c62dd9d2011-09-21 18:05:4131// Location provides basic info where of an object was constructed, or was
32// significantly brought to life.
33class BASE_EXPORT Location {
34 public:
Brett Wilsonc794cf62017-09-01 20:14:3335 Location();
36 Location(const Location& other);
Stephan Hartmann66f224a2021-12-07 23:52:2337 Location(Location&& other) noexcept;
Peter Kastinge08ca13342021-07-08 02:46:5738 Location& operator=(const Location& other);
Brett Wilsonc794cf62017-09-01 20:14:3339
40 // Only initializes the file name and program counter, the source information
41 // will be null for the strings, and -1 for the line number.
42 // TODO(http://crbug.com/760702) remove file name from this constructor.
43 Location(const char* file_name, const void* program_counter);
44
[email protected]c62dd9d2011-09-21 18:05:4145 // Constructor should be called with a long-lived char*, such as __FILE__.
46 // It assumes the provided value will persist as a global constant, and it
47 // will not make a copy of it.
48 Location(const char* function_name,
49 const char* file_name,
50 int line_number,
51 const void* program_counter);
52
Hans Wennborge66cdda2021-04-21 11:02:3153 // Comparator for testing. The program counter should uniquely
Brett Wilsonc794cf62017-09-01 20:14:3354 // identify a location.
squee3b16f12015-03-14 02:39:0355 bool operator==(const Location& other) const {
Brett Wilsonc794cf62017-09-01 20:14:3356 return program_counter_ == other.program_counter_;
[email protected]c62dd9d2011-09-21 18:05:4157 }
58
Aditya Kushwah5a286b72022-02-10 04:54:4359 // Comparator is necessary to use location object within an ordered container
60 // type (eg. std::map).
61 bool operator<(const Location& other) const {
62 return program_counter_ < other.program_counter_;
63 }
64
Brett Wilsonc794cf62017-09-01 20:14:3365 // Returns true if there is source code location info. If this is false,
66 // the Location object only contains a program counter or is
67 // default-initialized (the program counter is also null).
68 bool has_source_info() const { return function_name_ && file_name_; }
69
70 // Will be nullptr for default initialized Location objects and when source
71 // names are disabled.
72 const char* function_name() const { return function_name_; }
73
74 // Will be nullptr for default initialized Location objects and when source
75 // names are disabled.
76 const char* file_name() const { return file_name_; }
77
78 // Will be -1 for default initialized Location objects and when source names
79 // are disabled.
80 int line_number() const { return line_number_; }
81
82 // The address of the code generating this Location object. Should always be
83 // valid except for default initialized Location objects, which will be
84 // nullptr.
[email protected]c62dd9d2011-09-21 18:05:4185 const void* program_counter() const { return program_counter_; }
86
Brett Wilson07ec3b52017-10-09 21:42:4587 // Converts to the most user-readable form possible. If function and filename
88 // are not available, this will return "pc:<hex address>".
[email protected]c62dd9d2011-09-21 18:05:4189 std::string ToString() const;
90
Alexander Timin26ac38f2021-10-05 00:50:3691 // Write a representation of this object into a trace.
92 void WriteIntoTrace(perfetto::TracedValue context) const;
93
Daniel Chenge0f23c8e2020-02-25 21:03:2794#if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
95#if !BUILDFLAG(ENABLE_LOCATION_SOURCE)
tzik4f05a25a2017-09-13 00:23:1696 static Location CreateFromHere(const char* file_name);
Daniel Chenge0f23c8e2020-02-25 21:03:2797#else
tzik4f05a25a2017-09-13 00:23:1698 static Location CreateFromHere(const char* function_name,
99 const char* file_name,
100 int line_number);
Daniel Chenge0f23c8e2020-02-25 21:03:27101#endif
102#endif
tzik4f05a25a2017-09-13 00:23:16103
Jeremy Romana5999de2019-07-04 20:13:01104#if SUPPORTS_LOCATION_BUILTINS && BUILDFLAG(ENABLE_LOCATION_SOURCE)
105 static Location Current(const char* function_name = __builtin_FUNCTION(),
106 const char* file_name = __builtin_FILE(),
107 int line_number = __builtin_LINE());
108#elif SUPPORTS_LOCATION_BUILTINS
109 static Location Current(const char* file_name = __builtin_FILE());
110#else
111 static Location Current();
112#endif
113
[email protected]c62dd9d2011-09-21 18:05:41114 private:
Brett Wilsonc794cf62017-09-01 20:14:33115 const char* function_name_ = nullptr;
116 const char* file_name_ = nullptr;
117 int line_number_ = -1;
Lukasz Anforowicz877e6222021-11-26 00:03:23118
119 // `program_counter_` is not a raw_ptr<...> for performance reasons (based on
120 // analysis of sampling profiler data and tab_search:top100:2020).
Keishi Hattori488b7602022-05-02 13:09:31121 RAW_PTR_EXCLUSION const void* program_counter_ = nullptr;
[email protected]c62dd9d2011-09-21 18:05:41122};
123
124BASE_EXPORT const void* GetProgramCounter();
125
Daniel Chenge0f23c8e2020-02-25 21:03:27126#if BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
127
128#define FROM_HERE ::base::Location::Current()
129
Brett Wilsonc794cf62017-09-01 20:14:33130// The macros defined here will expand to the current function.
Daniel Chenge0f23c8e2020-02-25 21:03:27131#elif BUILDFLAG(ENABLE_LOCATION_SOURCE)
Brett Wilson5b5b7cb2017-09-01 04:40:02132
Brett Wilsonc794cf62017-09-01 20:14:33133// Full source information should be included.
Daniel Chenge0f23c8e2020-02-25 21:03:27134#define FROM_HERE ::base::Location::CreateFromHere(__func__, __FILE__, __LINE__)
Brett Wilsonc794cf62017-09-01 20:14:33135
136#else
137
138// TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls.
tzik4f05a25a2017-09-13 00:23:16139#define FROM_HERE ::base::Location::CreateFromHere(__FILE__)
Brett Wilsonc794cf62017-09-01 20:14:33140
141#endif
[email protected]c62dd9d2011-09-21 18:05:41142
Brett Wilsonabbb9602017-09-11 23:26:39143} // namespace base
144
[email protected]c62dd9d2011-09-21 18:05:41145#endif // BASE_LOCATION_H_