| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(COMPILER_MSVC) | 7 #if defined(COMPILER_MSVC) |
| 8 #include <intrin.h> | 8 #include <intrin.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 | 14 |
| 15 namespace tracked_objects { | 15 namespace tracked_objects { |
| 16 namespace { |
| 17 |
| 18 #if defined(COMPILER_MSVC) |
| 19 __forceinline |
| 20 #elif defined(COMPILER_GCC) && !defined(OS_NACL) |
| 21 __attribute__((always_inline)) |
| 22 inline |
| 23 #endif |
| 24 const void* GetReturnAddress() { |
| 25 #if defined(COMPILER_MSVC) |
| 26 return _ReturnAddress(); |
| 27 #elif defined(COMPILER_GCC) && !defined(OS_NACL) |
| 28 return __builtin_extract_return_addr(__builtin_return_address(0)); |
| 29 #else |
| 30 return nullptr; |
| 31 #endif |
| 32 } |
| 33 |
| 34 } // namespace |
| 16 | 35 |
| 17 Location::Location(const char* function_name, | 36 Location::Location(const char* function_name, |
| 18 const char* file_name, | 37 const char* file_name, |
| 19 int line_number, | 38 int line_number, |
| 20 const void* program_counter) | 39 const void* program_counter) |
| 21 : function_name_(function_name), | 40 : function_name_(function_name), |
| 22 file_name_(file_name), | 41 file_name_(file_name), |
| 23 line_number_(line_number), | 42 line_number_(line_number), |
| 24 program_counter_(program_counter) { | 43 program_counter_(program_counter) { |
| 25 } | 44 } |
| 26 | 45 |
| 27 Location::Location() | 46 Location::Location() |
| 28 : function_name_("Unknown"), | 47 : function_name_("Unknown"), |
| 29 file_name_("Unknown"), | 48 file_name_("Unknown"), |
| 30 line_number_(-1), | 49 line_number_(-1), |
| 31 program_counter_(NULL) { | 50 program_counter_(NULL) { |
| 32 } | 51 } |
| 33 | 52 |
| 34 Location::Location(const Location& other) | 53 Location::Location(const Location& other) |
| 35 : function_name_(other.function_name_), | 54 : function_name_(other.function_name_), |
| 36 file_name_(other.file_name_), | 55 file_name_(other.file_name_), |
| 37 line_number_(other.line_number_), | 56 line_number_(other.line_number_), |
| 38 program_counter_(other.program_counter_) { | 57 program_counter_(other.program_counter_) { |
| 39 } | 58 } |
| 40 | 59 |
| 60 // static |
| 61 #if defined(COMPILER_MSVC) |
| 62 __declspec(noinline) |
| 63 #endif |
| 64 Location Location::CreateForCurrentProgramCounter( |
| 65 const char* function_name, |
| 66 const char* file_name, |
| 67 int line_number) { |
| 68 const void* program_counter = GetReturnAddress(); |
| 69 return Location(function_name, file_name, line_number, program_counter); |
| 70 } |
| 71 |
| 41 std::string Location::ToString() const { | 72 std::string Location::ToString() const { |
| 42 return std::string(function_name_) + "@" + file_name_ + ":" + | 73 return std::string(function_name_) + "@" + file_name_ + ":" + |
| 43 base::IntToString(line_number_); | 74 base::IntToString(line_number_); |
| 44 } | 75 } |
| 45 | 76 |
| 46 void Location::Write(bool display_filename, bool display_function_name, | 77 void Location::Write(bool display_filename, bool display_function_name, |
| 47 std::string* output) const { | 78 std::string* output) const { |
| 48 base::StringAppendF(output, "%s[%d] ", | 79 base::StringAppendF(output, "%s[%d] ", |
| 49 display_filename ? file_name_ : "line", | 80 display_filename ? file_name_ : "line", |
| 50 line_number_); | 81 line_number_); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 118 } |
| 88 | 119 |
| 89 LocationSnapshot::~LocationSnapshot() { | 120 LocationSnapshot::~LocationSnapshot() { |
| 90 } | 121 } |
| 91 | 122 |
| 92 //------------------------------------------------------------------------------ | 123 //------------------------------------------------------------------------------ |
| 93 #if defined(COMPILER_MSVC) | 124 #if defined(COMPILER_MSVC) |
| 94 __declspec(noinline) | 125 __declspec(noinline) |
| 95 #endif | 126 #endif |
| 96 BASE_EXPORT const void* GetProgramCounter() { | 127 BASE_EXPORT const void* GetProgramCounter() { |
| 97 #if defined(COMPILER_MSVC) | 128 return GetReturnAddress(); |
| 98 return _ReturnAddress(); | |
| 99 #elif defined(COMPILER_GCC) && !defined(OS_NACL) | |
| 100 return __builtin_extract_return_addr(__builtin_return_address(0)); | |
| 101 #else | |
| 102 return NULL; | |
| 103 #endif | |
| 104 } | 129 } |
| 105 | 130 |
| 106 } // namespace tracked_objects | 131 } // namespace tracked_objects |
| OLD | NEW |