1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/location.h"
6
7 #if defined(COMPILER_MSVC)
8 #include <intrin.h>
9 #endif
10
11 #include "base/compiler_specific.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "build/build_config.h"
15
16 namespace base {
17
18 Location::Location() = default;
19 Location::Location(const Location& other) = default;
20
Location(const char * file_name,const void * program_counter)21 Location::Location(const char* file_name, const void* program_counter)
22 : file_name_(file_name), program_counter_(program_counter) {}
23
Location(const char * function_name,const char * file_name,int line_number,const void * program_counter)24 Location::Location(const char* function_name,
25 const char* file_name,
26 int line_number,
27 const void* program_counter)
28 : function_name_(function_name),
29 file_name_(file_name),
30 line_number_(line_number),
31 program_counter_(program_counter) {
32 #if !defined(OS_NACL)
33 // The program counter should not be null except in a default constructed
34 // (empty) Location object. This value is used for identity, so if it doesn't
35 // uniquely identify a location, things will break.
36 //
37 // The program counter isn't supported in NaCl so location objects won't work
38 // properly in that context.
39 DCHECK(program_counter);
40 #endif
41 }
42
ToString() const43 std::string Location::ToString() const {
44 if (has_source_info()) {
45 return std::string(function_name_) + "@" + file_name_ + ":" +
46 IntToString(line_number_);
47 }
48 return StringPrintf("pc:%p", program_counter_);
49 }
50
51 #if defined(COMPILER_MSVC)
52 #define RETURN_ADDRESS() _ReturnAddress()
53 #elif defined(COMPILER_GCC) && !defined(OS_NACL)
54 #define RETURN_ADDRESS() \
55 __builtin_extract_return_addr(__builtin_return_address(0))
56 #else
57 #define RETURN_ADDRESS() nullptr
58 #endif
59
60 // static
CreateFromHere(const char * file_name)61 NOINLINE Location Location::CreateFromHere(const char* file_name) {
62 return Location(file_name, RETURN_ADDRESS());
63 }
64
65 // static
CreateFromHere(const char * function_name,const char * file_name,int line_number)66 NOINLINE Location Location::CreateFromHere(const char* function_name,
67 const char* file_name,
68 int line_number) {
69 return Location(function_name, file_name, line_number, RETURN_ADDRESS());
70 }
71
72 //------------------------------------------------------------------------------
GetProgramCounter()73 NOINLINE const void* GetProgramCounter() {
74 return RETURN_ADDRESS();
75 }
76
77 } // namespace base
78