| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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" |
| 6 |
| 7 #if defined(COMPILER_MSVC) |
| 8 #include <intrin.h> |
| 9 #endif |
| 10 |
| 5 #include "base/tracked.h" | 11 #include "base/tracked.h" |
| 6 | 12 |
| 7 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 8 #include "base/tracked_objects.h" | 14 #include "base/tracked_objects.h" |
| 9 | 15 |
| 10 using base::TimeTicks; | 16 using base::TimeTicks; |
| 11 | 17 |
| 12 namespace tracked_objects { | 18 namespace tracked_objects { |
| 13 | 19 |
| 14 //------------------------------------------------------------------------------ | 20 //------------------------------------------------------------------------------ |
| 15 | 21 |
| 16 Location::Location(const char* function_name, const char* file_name, | 22 Location::Location(const char* function_name, |
| 17 int line_number) | 23 const char* file_name, |
| 24 int line_number, |
| 25 const void* program_counter) |
| 18 : function_name_(function_name), | 26 : function_name_(function_name), |
| 19 file_name_(file_name), | 27 file_name_(file_name), |
| 20 line_number_(line_number) { | 28 line_number_(line_number), |
| 29 program_counter_(program_counter) { |
| 21 } | 30 } |
| 22 | 31 |
| 23 Location::Location() | 32 Location::Location() |
| 24 : function_name_("Unknown"), | 33 : function_name_("Unknown"), |
| 25 file_name_("Unknown"), | 34 file_name_("Unknown"), |
| 26 line_number_(-1) { | 35 line_number_(-1), |
| 36 program_counter_(NULL) { |
| 27 } | 37 } |
| 28 | 38 |
| 29 void Location::Write(bool display_filename, bool display_function_name, | 39 void Location::Write(bool display_filename, bool display_function_name, |
| 30 std::string* output) const { | 40 std::string* output) const { |
| 31 base::StringAppendF(output, "%s[%d] ", | 41 base::StringAppendF(output, "%s[%d] ", |
| 32 display_filename ? file_name_ : "line", | 42 display_filename ? file_name_ : "line", |
| 33 line_number_); | 43 line_number_); |
| 34 | 44 |
| 35 if (display_function_name) { | 45 if (display_function_name) { |
| 36 WriteFunctionName(output); | 46 WriteFunctionName(output); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 51 output->append(">"); | 61 output->append(">"); |
| 52 break; | 62 break; |
| 53 | 63 |
| 54 default: | 64 default: |
| 55 output->push_back(*p); | 65 output->push_back(*p); |
| 56 break; | 66 break; |
| 57 } | 67 } |
| 58 } | 68 } |
| 59 } | 69 } |
| 60 | 70 |
| 71 BASE_API const void* GetProgramCounterRegister() { |
| 72 #if defined(COMPILER_MSVC) |
| 73 return _ReturnAddress(); |
| 74 #elif defined(COMPILER_GCC) |
| 75 return __builtin_extract_return_address(__builtin_return_address(0)); |
| 76 #endif // COMPILER_GCC |
| 77 |
| 78 return NULL; |
| 79 } |
| 80 |
| 61 //------------------------------------------------------------------------------ | 81 //------------------------------------------------------------------------------ |
| 62 | 82 |
| 63 #ifndef TRACK_ALL_TASK_OBJECTS | 83 #ifndef TRACK_ALL_TASK_OBJECTS |
| 64 | 84 |
| 65 Tracked::Tracked() {} | 85 Tracked::Tracked() : birth_program_counter_(NULL) {} |
| 66 Tracked::~Tracked() {} | 86 Tracked::~Tracked() {} |
| 67 void Tracked::SetBirthPlace(const Location& from_here) {} | 87 |
| 88 void Tracked::SetBirthPlace(const Location& from_here) { |
| 89 birth_program_counter_ = from_here.program_counter(); |
| 90 } |
| 91 |
| 68 const Location Tracked::GetBirthPlace() const { | 92 const Location Tracked::GetBirthPlace() const { |
| 69 static Location kNone("NoFunctionName", "NeedToSetBirthPlace", -1); | 93 static Location kNone("NoFunctionName", "NeedToSetBirthPlace", -1, NULL); |
| 70 return kNone; | 94 return kNone; |
| 71 } | 95 } |
| 72 bool Tracked::MissingBirthplace() const { return false; } | 96 bool Tracked::MissingBirthplace() const { return false; } |
| 73 void Tracked::ResetBirthTime() {} | 97 void Tracked::ResetBirthTime() {} |
| 74 | 98 |
| 75 #else | 99 #else |
| 76 | 100 |
| 77 Tracked::Tracked() | 101 Tracked::Tracked() |
| 78 : tracked_births_(NULL), | 102 : tracked_births_(NULL), |
| 79 tracked_birth_time_(TimeTicks::Now()) { | 103 tracked_birth_time_(TimeTicks::Now()) { |
| 80 if (!ThreadData::IsActive()) | 104 if (!ThreadData::IsActive()) |
| 81 return; | 105 return; |
| 82 SetBirthPlace(Location("NoFunctionName", "NeedToSetBirthPlace", -1)); | 106 SetBirthPlace(Location("NoFunctionName", "NeedToSetBirthPlace", -1, NULL)); |
| 83 } | 107 } |
| 84 | 108 |
| 85 Tracked::~Tracked() { | 109 Tracked::~Tracked() { |
| 86 if (!ThreadData::IsActive() || !tracked_births_) | 110 if (!ThreadData::IsActive() || !tracked_births_) |
| 87 return; | 111 return; |
| 88 ThreadData::current()->TallyADeath(*tracked_births_, | 112 ThreadData::current()->TallyADeath(*tracked_births_, |
| 89 TimeTicks::Now() - tracked_birth_time_); | 113 TimeTicks::Now() - tracked_birth_time_); |
| 90 } | 114 } |
| 91 | 115 |
| 92 void Tracked::SetBirthPlace(const Location& from_here) { | 116 void Tracked::SetBirthPlace(const Location& from_here) { |
| 93 if (!ThreadData::IsActive()) | 117 if (!ThreadData::IsActive()) |
| 94 return; | 118 return; |
| 95 if (tracked_births_) | 119 if (tracked_births_) |
| 96 tracked_births_->ForgetBirth(); | 120 tracked_births_->ForgetBirth(); |
| 97 ThreadData* current_thread_data = ThreadData::current(); | 121 ThreadData* current_thread_data = ThreadData::current(); |
| 98 if (!current_thread_data) | 122 if (!current_thread_data) |
| 99 return; // Shutdown started, and this thread wasn't registered. | 123 return; // Shutdown started, and this thread wasn't registered. |
| 100 tracked_births_ = current_thread_data->TallyABirth(from_here); | 124 tracked_births_ = current_thread_data->TallyABirth(from_here); |
| 125 |
| 126 birth_program_counter_ = from_here.program_counter(); |
| 101 } | 127 } |
| 102 | 128 |
| 103 const Location Tracked::GetBirthPlace() const { | 129 const Location Tracked::GetBirthPlace() const { |
| 104 return tracked_births_->location(); | 130 return tracked_births_->location(); |
| 105 } | 131 } |
| 106 | 132 |
| 107 void Tracked::ResetBirthTime() { | 133 void Tracked::ResetBirthTime() { |
| 108 tracked_birth_time_ = TimeTicks::Now(); | 134 tracked_birth_time_ = TimeTicks::Now(); |
| 109 } | 135 } |
| 110 | 136 |
| 111 bool Tracked::MissingBirthplace() const { | 137 bool Tracked::MissingBirthplace() const { |
| 112 return -1 == tracked_births_->location().line_number(); | 138 return -1 == tracked_births_->location().line_number(); |
| 113 } | 139 } |
| 114 | 140 |
| 115 #endif // NDEBUG | 141 #endif // NDEBUG |
| 116 | 142 |
| 117 } // namespace tracked_objects | 143 } // namespace tracked_objects |
| OLD | NEW |