1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "base/location.h"
11
12 #include "base/compiler_specific.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/trace_event/base_tracing.h"
16
17 #if defined(COMPILER_MSVC)
18 #include <intrin.h>
19 #endif
20
21 namespace base {
22
23 namespace {
24
25 // Returns the length of the given null terminated c-string.
StrLen(const char * str)26 constexpr size_t StrLen(const char* str) {
27 size_t str_len = 0;
28 for (str_len = 0; str[str_len] != '\0'; ++str_len)
29 ;
30 return str_len;
31 }
32
33 // Finds the length of the build folder prefix from the file path.
34 // TODO(ssid): Strip prefixes from stored strings in the binary. This code only
35 // skips the prefix while reading the file name strings at runtime.
StrippedFilePathPrefixLength()36 constexpr size_t StrippedFilePathPrefixLength() {
37 constexpr char path[] = __FILE__;
38 // Only keep the file path starting from the src directory.
39 #if defined(__clang__) && defined(_MSC_VER)
40 constexpr char stripped[] = "base\\location.cc";
41 #else
42 constexpr char stripped[] = "base/location.cc";
43 #endif
44 constexpr size_t path_len = StrLen(path);
45 constexpr size_t stripped_len = StrLen(stripped);
46 static_assert(path_len >= stripped_len,
47 "Invalid file path for base/location.cc.");
48 return path_len - stripped_len;
49 }
50
51 constexpr size_t kStrippedPrefixLength = StrippedFilePathPrefixLength();
52
53 // Returns true if the |name| string has |prefix_len| characters in the prefix
54 // and the suffix matches the |expected| string.
55 // TODO(ssid): With C++20 we can make base::EndsWith() constexpr and use it
56 // instead.
StrEndsWith(const char * name,size_t prefix_len,const char * expected)57 constexpr bool StrEndsWith(const char* name,
58 size_t prefix_len,
59 const char* expected) {
60 const size_t name_len = StrLen(name);
61 const size_t expected_len = StrLen(expected);
62 if (name_len != prefix_len + expected_len)
63 return false;
64 for (size_t i = 0; i < expected_len; ++i) {
65 if (name[i + prefix_len] != expected[i])
66 return false;
67 }
68 return true;
69 }
70
71 #if defined(__clang__) && defined(_MSC_VER)
72 static_assert(StrEndsWith(__FILE__, kStrippedPrefixLength, "base\\location.cc"),
73 "The file name does not match the expected prefix format.");
74 #else
75 static_assert(StrEndsWith(__FILE__, kStrippedPrefixLength, "base/location.cc"),
76 "The file name does not match the expected prefix format.");
77 #endif
78
79 } // namespace
80
81 Location::Location() = default;
82 Location::Location(const Location& other) = default;
83 Location::Location(Location&& other) noexcept = default;
84 Location& Location::operator=(const Location& other) = default;
85
Location(const char * file_name,const void * program_counter)86 Location::Location(const char* file_name, const void* program_counter)
87 : file_name_(file_name), program_counter_(program_counter) {}
88
Location(const char * function_name,const char * file_name,int line_number,const void * program_counter)89 Location::Location(const char* function_name,
90 const char* file_name,
91 int line_number,
92 const void* program_counter)
93 : function_name_(function_name),
94 file_name_(file_name),
95 line_number_(line_number),
96 program_counter_(program_counter) {
97 #if !BUILDFLAG(IS_NACL)
98 // The program counter should not be null except in a default constructed
99 // (empty) Location object. This value is used for identity, so if it doesn't
100 // uniquely identify a location, things will break.
101 //
102 // The program counter isn't supported in NaCl so location objects won't work
103 // properly in that context.
104 DCHECK(program_counter);
105 #endif
106 }
107
ToString() const108 std::string Location::ToString() const {
109 if (has_source_info()) {
110 return std::string(function_name_) + "@" + file_name_ + ":" +
111 NumberToString(line_number_);
112 }
113 return StringPrintf("pc:%p", program_counter_);
114 }
115
WriteIntoTrace(perfetto::TracedValue context) const116 void Location::WriteIntoTrace(perfetto::TracedValue context) const {
117 auto dict = std::move(context).WriteDictionary();
118 dict.Add("function_name", function_name_);
119 dict.Add("file_name", file_name_);
120 dict.Add("line_number", line_number_);
121 }
122
123 #if defined(COMPILER_MSVC)
124 #define RETURN_ADDRESS() _ReturnAddress()
125 #elif defined(COMPILER_GCC) && !BUILDFLAG(IS_NACL)
126 #define RETURN_ADDRESS() \
127 __builtin_extract_return_addr(__builtin_return_address(0))
128 #else
129 #define RETURN_ADDRESS() nullptr
130 #endif
131
132 // static
Current(const char * function_name,const char * file_name,int line_number)133 NOINLINE Location Location::Current(const char* function_name,
134 const char* file_name,
135 int line_number) {
136 return Location(function_name, file_name + kStrippedPrefixLength, line_number,
137 RETURN_ADDRESS());
138 }
139
140 //------------------------------------------------------------------------------
GetProgramCounter()141 NOINLINE const void* GetProgramCounter() {
142 return RETURN_ADDRESS();
143 }
144
145 } // namespace base
146