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 | |
35 | 16 |
36 Location::Location(const char* function_name, | 17 Location::Location(const char* function_name, |
37 const char* file_name, | 18 const char* file_name, |
38 int line_number, | 19 int line_number, |
39 const void* program_counter) | 20 const void* program_counter) |
40 : function_name_(function_name), | 21 : function_name_(function_name), |
41 file_name_(file_name), | 22 file_name_(file_name), |
42 line_number_(line_number), | 23 line_number_(line_number), |
43 program_counter_(program_counter) { | 24 program_counter_(program_counter) { |
44 } | 25 } |
45 | 26 |
46 Location::Location() | 27 Location::Location() |
47 : function_name_("Unknown"), | 28 : function_name_("Unknown"), |
48 file_name_("Unknown"), | 29 file_name_("Unknown"), |
49 line_number_(-1), | 30 line_number_(-1), |
50 program_counter_(NULL) { | 31 program_counter_(NULL) { |
51 } | 32 } |
52 | 33 |
53 Location::Location(const Location& other) | 34 Location::Location(const Location& other) |
54 : function_name_(other.function_name_), | 35 : function_name_(other.function_name_), |
55 file_name_(other.file_name_), | 36 file_name_(other.file_name_), |
56 line_number_(other.line_number_), | 37 line_number_(other.line_number_), |
57 program_counter_(other.program_counter_) { | 38 program_counter_(other.program_counter_) { |
58 } | 39 } |
59 | 40 |
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 | |
72 std::string Location::ToString() const { | 41 std::string Location::ToString() const { |
73 return std::string(function_name_) + "@" + file_name_ + ":" + | 42 return std::string(function_name_) + "@" + file_name_ + ":" + |
74 base::IntToString(line_number_); | 43 base::IntToString(line_number_); |
75 } | 44 } |
76 | 45 |
77 void Location::Write(bool display_filename, bool display_function_name, | 46 void Location::Write(bool display_filename, bool display_function_name, |
78 std::string* output) const { | 47 std::string* output) const { |
79 base::StringAppendF(output, "%s[%d] ", | 48 base::StringAppendF(output, "%s[%d] ", |
80 display_filename ? file_name_ : "line", | 49 display_filename ? file_name_ : "line", |
81 line_number_); | 50 line_number_); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 87 } |
119 | 88 |
120 LocationSnapshot::~LocationSnapshot() { | 89 LocationSnapshot::~LocationSnapshot() { |
121 } | 90 } |
122 | 91 |
123 //------------------------------------------------------------------------------ | 92 //------------------------------------------------------------------------------ |
124 #if defined(COMPILER_MSVC) | 93 #if defined(COMPILER_MSVC) |
125 __declspec(noinline) | 94 __declspec(noinline) |
126 #endif | 95 #endif |
127 BASE_EXPORT const void* GetProgramCounter() { | 96 BASE_EXPORT const void* GetProgramCounter() { |
128 return GetReturnAddress(); | 97 #if defined(COMPILER_MSVC) |
| 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 |
129 } | 104 } |
130 | 105 |
131 } // namespace tracked_objects | 106 } // namespace tracked_objects |
OLD | NEW |