Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /base /location.h
blob: f324c409cf2386b0471b718397a6f1b6c5773d4a [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:06[diff] [blame]1// Copyright 2012 The Chromium Authors
ajwong@chromium.orgc62dd9d2011-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
kylechard917b982023-11-30 22:52:44[diff] [blame]8#include<compare>
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]9#include<string>
10
11#include"base/base_export.h"
Keishi Hattori488b7602022-05-02 13:09:31[diff] [blame]12#include"base/memory/raw_ptr_exclusion.h"
Alexander Timin26ac38f2021-10-05 00:50:36[diff] [blame]13#include"base/trace_event/base_tracing_forward.h"
Jeremy Romana5999de2019-07-04 20:13:01[diff] [blame]14#include"build/build_config.h"
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]15
Brett Wilsonabbb9602017-09-11 23:26:39[diff] [blame]16namespacebase{
ajwong@chromium.orgc62dd9d2011-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.
20class BASE_EXPORTLocation{
21public:
Brett Wilsonc794cf62017-09-01 20:14:33[diff] [blame]22Location();
23Location(constLocation& other);
Stephan Hartmann66f224a2021-12-07 23:52:23[diff] [blame]24Location(Location&& other) noexcept;
Peter Kastinge08ca13342021-07-08 02:46:57[diff] [blame]25Location&operator=(constLocation& other);
Brett Wilsonc794cf62017-09-01 20:14:33[diff] [blame]26
Daniel Chengcd6d6fb2022-10-22 03:38:32[diff] [blame]27staticLocationCreateForTesting(constchar* function_name,
28constchar* file_name,
29int line_number,
30constvoid* program_counter){
31returnLocation(function_name, file_name, line_number, program_counter);
32}
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]33
Hans Wennborge66cdda2021-04-21 11:02:31[diff] [blame]34// Comparator for testing. The program counter should uniquely
Brett Wilsonc794cf62017-09-01 20:14:33[diff] [blame]35// identify a location.
kylechard917b982023-11-30 22:52:44[diff] [blame]36friendbooloperator==(constLocation& lhs,constLocation& rhs){
37return lhs.program_counter_== rhs.program_counter_;
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]38}
39
kylechard917b982023-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.
44friend std::weak_orderingoperator<=>(constLocation& lhs,
45constLocation& rhs){
46return lhs.program_counter_<=> rhs.program_counter_;
Aditya Kushwah5a286b72022-02-10 04:54:43[diff] [blame]47}
48
Phillis Tang9f9c0fd2024-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).
52bool has_source_info()const{return function_name_&& file_name_;}
53
Brett Wilsonc794cf62017-09-01 20:14:33[diff] [blame]54// Will be nullptr for default initialized Location objects and when source
55// names are disabled.
56constchar* function_name()const{return function_name_;}
57
58// Will be nullptr for default initialized Location objects and when source
59// names are disabled.
60constchar* file_name()const{return file_name_;}
61
62// Will be -1 for default initialized Location objects and when source names
63// are disabled.
64int 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.
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]69constvoid* program_counter()const{return program_counter_;}
70
Brett Wilson07ec3b52017-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>".
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]73 std::stringToString()const;
74
Alexander Timin26ac38f2021-10-05 00:50:36[diff] [blame]75// Write a representation of this object into a trace.
76voidWriteIntoTrace(perfetto::TracedValue context)const;
77
Jeremy Romana5999de2019-07-04 20:13:01[diff] [blame]78staticLocationCurrent(constchar* function_name= __builtin_FUNCTION(),
79constchar* file_name= __builtin_FILE(),
80int line_number= __builtin_LINE());
Jeremy Romana5999de2019-07-04 20:13:01[diff] [blame]81
Peter Boström5f34e2f2024-12-19 01:07:54[diff] [blame]82staticLocationCurrentWithoutFunctionName(
83constchar* file_name= __builtin_FILE(),
84int line_number= __builtin_LINE());
85
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]86private:
Daniel Chengcd6d6fb2022-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.
90Location(constchar* file_name,constvoid* 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.
95Location(constchar* function_name,
96constchar* file_name,
97int line_number,
98constvoid* program_counter);
99
Brett Wilsonc794cf62017-09-01 20:14:33[diff] [blame]100constchar* function_name_=nullptr;
101constchar* file_name_=nullptr;
102int line_number_=-1;
Lukasz Anforowicz877e6222021-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 Hattori488b7602022-05-02 13:09:31[diff] [blame]106 RAW_PTR_EXCLUSIONconstvoid* program_counter_=nullptr;
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]107};
108
109BASE_EXPORTconstvoid*GetProgramCounter();
110
Daniel Chenge0f23c8e2020-02-25 21:03:27[diff] [blame]111#define FROM_HERE::base::Location::Current()
112
Brett Wilsonabbb9602017-09-11 23:26:39[diff] [blame]113}// namespace base
114
ajwong@chromium.orgc62dd9d2011-09-21 18:05:41[diff] [blame]115#endif// BASE_LOCATION_H_

[8]ページ先頭

©2009-2025 Movatter.jp