OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "build/build_config.h" | |
6 | |
7 #if defined(COMPILER_MSVC) | |
8 // MSDN says to #include <intrin.h>, but that breaks the VS2005 build. | |
9 extern "C" { | |
10 void* _ReturnAddress(); | |
11 } | |
12 #endif | |
13 | |
14 #include "base/tracked.h" | |
15 | |
16 #include "base/string_number_conversions.h" | |
17 #include "base/stringprintf.h" | |
18 #include "base/tracked_objects.h" | |
19 | |
20 using base::TimeTicks; | |
21 | |
22 namespace tracked_objects { | |
23 | |
24 //------------------------------------------------------------------------------ | |
25 | |
26 Location::Location(const char* function_name, | |
27 const char* file_name, | |
28 int line_number, | |
29 const void* program_counter) | |
30 : function_name_(function_name), | |
31 file_name_(file_name), | |
32 line_number_(line_number), | |
33 program_counter_(program_counter) { | |
34 } | |
35 | |
36 Location::Location() | |
37 : function_name_("Unknown"), | |
38 file_name_("Unknown"), | |
39 line_number_(-1), | |
40 program_counter_(NULL) { | |
41 } | |
42 | |
43 std::string Location::ToString() const { | |
44 return std::string(function_name_) + "@" + file_name_ + ":" + | |
45 base::IntToString(line_number_); | |
46 } | |
47 | |
48 void Location::Write(bool display_filename, bool display_function_name, | |
49 std::string* output) const { | |
50 base::StringAppendF(output, "%s[%d] ", | |
51 display_filename ? file_name_ : "line", | |
52 line_number_); | |
53 | |
54 if (display_function_name) { | |
55 WriteFunctionName(output); | |
56 output->push_back(' '); | |
57 } | |
58 } | |
59 | |
60 void Location::WriteFunctionName(std::string* output) const { | |
61 // Translate "<" to "<" for HTML safety. | |
62 // TODO(jar): Support ASCII or html for logging in ASCII. | |
63 for (const char *p = function_name_; *p; p++) { | |
64 switch (*p) { | |
65 case '<': | |
66 output->append("<"); | |
67 break; | |
68 | |
69 case '>': | |
70 output->append(">"); | |
71 break; | |
72 | |
73 default: | |
74 output->push_back(*p); | |
75 break; | |
76 } | |
77 } | |
78 } | |
79 | |
80 #if defined(COMPILER_MSVC) | |
81 __declspec(noinline) | |
82 #endif | |
83 BASE_EXPORT const void* GetProgramCounter() { | |
84 #if defined(COMPILER_MSVC) | |
85 return _ReturnAddress(); | |
86 #elif defined(COMPILER_GCC) | |
87 return __builtin_extract_return_addr(__builtin_return_address(0)); | |
88 #endif // COMPILER_GCC | |
89 | |
90 return NULL; | |
91 } | |
92 | |
93 //------------------------------------------------------------------------------ | |
94 | |
95 #if !defined(TRACK_ALL_TASK_OBJECTS) | |
96 | |
97 Tracked::Tracked() : birth_program_counter_(NULL) {} | |
98 Tracked::~Tracked() {} | |
99 | |
100 void Tracked::SetBirthPlace(const Location& from_here) { | |
101 birth_program_counter_ = from_here.program_counter(); | |
102 } | |
103 | |
104 const Location Tracked::GetBirthPlace() const { | |
105 static Location kNone("NoFunctionName", "NeedToSetBirthPlace", -1, NULL); | |
106 return kNone; | |
107 } | |
108 bool Tracked::MissingBirthPlace() const { return false; } | |
109 void Tracked::ResetBirthTime() {} | |
110 | |
111 #else | |
112 | |
113 Tracked::Tracked() | |
114 : tracked_births_(NULL), | |
115 tracked_birth_time_(TimeTicks::Now()) { | |
116 } | |
117 | |
118 Tracked::~Tracked() { | |
119 if (!ThreadData::IsActive() || !tracked_births_) | |
120 return; | |
121 ThreadData::current()->TallyADeath(*tracked_births_, | |
122 TimeTicks::Now() - tracked_birth_time_); | |
123 } | |
124 | |
125 void Tracked::SetBirthPlace(const Location& from_here) { | |
126 if (!ThreadData::IsActive()) | |
127 return; | |
128 if (tracked_births_) | |
129 tracked_births_->ForgetBirth(); | |
130 ThreadData* current_thread_data = ThreadData::current(); | |
131 if (!current_thread_data) | |
132 return; // Shutdown started, and this thread wasn't registered. | |
133 tracked_births_ = current_thread_data->TallyABirth(from_here); | |
134 | |
135 birth_program_counter_ = from_here.program_counter(); | |
136 } | |
137 | |
138 const Location Tracked::GetBirthPlace() const { | |
139 static Location kNone("UnknownFunctionName", "UnknownFile", -1, NULL); | |
140 return tracked_births_ ? tracked_births_->location() : kNone; | |
141 } | |
142 | |
143 void Tracked::ResetBirthTime() { | |
144 tracked_birth_time_ = TimeTicks::Now(); | |
145 } | |
146 | |
147 bool Tracked::MissingBirthPlace() const { | |
148 return !tracked_births_ || tracked_births_->location().line_number() == -1; | |
149 } | |
150 | |
151 #endif // !defined(TRACK_ALL_TASK_OBJECTS) | |
152 | |
153 } // namespace tracked_objects | |
OLD | NEW |