| 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 |
| 21 | Location::Location(const char* file_name, const void* program_counter) |
| 22 | : file_name_(file_name), program_counter_(program_counter) {} |
| 23 |
| 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 |
| 43 | std::string Location::ToString() const { |
| 44 | if (has_source_info()) { |
| 45 | return std::string(function_name_) + "@" + file_name_ + ":" + |
| 46 | NumberToString(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 | #if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS) |
| 61 | #if !BUILDFLAG(ENABLE_LOCATION_SOURCE) |
| 62 |
| 63 | // static |
| 64 | NOINLINE Location Location::CreateFromHere(const char* file_name) { |
| 65 | return Location(file_name, RETURN_ADDRESS()); |
| 66 | } |
| 67 |
| 68 | #else |
| 69 |
| 70 | // static |
| 71 | NOINLINE Location Location::CreateFromHere(const char* function_name, |
| 72 | const char* file_name, |
| 73 | int line_number) { |
| 74 | return Location(function_name, file_name, line_number, RETURN_ADDRESS()); |
| 75 | } |
| 76 |
| 77 | #endif |
| 78 | #endif |
| 79 |
| 80 | #if SUPPORTS_LOCATION_BUILTINS && BUILDFLAG(ENABLE_LOCATION_SOURCE) |
| 81 | // static |
| 82 | NOINLINE Location Location::Current(const char* function_name, |
| 83 | const char* file_name, |
| 84 | int line_number) { |
| 85 | return Location(function_name, file_name, line_number, RETURN_ADDRESS()); |
| 86 | } |
| 87 | #elif SUPPORTS_LOCATION_BUILTINS |
| 88 | // static |
| 89 | NOINLINE Location Location::Current(const char* file_name) { |
| 90 | return Location(file_name, RETURN_ADDRESS()); |
| 91 | } |
| 92 | #else |
| 93 | // static |
| 94 | NOINLINE Location Location::Current() { |
| 95 | return Location(nullptr, RETURN_ADDRESS()); |
| 96 | } |
| 97 | #endif |
| 98 |
| 99 | //------------------------------------------------------------------------------ |
| 100 | NOINLINE const void* GetProgramCounter() { |
| 101 | return RETURN_ADDRESS(); |
| 102 | } |
| 103 |
| 104 | } // namespace base |
| 105 |