activity_report_extractor.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright 2016 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. #include "components/browser_watcher/activity_report_extractor.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/check_op.h"
  10. #include "base/debug/activity_analyzer.h"
  11. #include "base/notreached.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/strings/string_util.h"
  14. #include "base/strings/stringprintf.h"
  15. #include "base/strings/utf_string_conversions.h"
  16. #include "components/browser_watcher/activity_data_names.h"
  17. #include "third_party/crashpad/crashpad/util/misc/uuid.h"
  18. namespace browser_watcher {
  19. using ActivitySnapshot = base::debug::ThreadActivityAnalyzer::Snapshot;
  20. using base::PersistentMemoryAllocator;
  21. using base::debug::ActivityUserData;
  22. using base::debug::GlobalActivityAnalyzer;
  23. using base::debug::GlobalActivityTracker;
  24. using base::debug::ThreadActivityAnalyzer;
  25. namespace {
  26. // Collects stability user data from the recorded format to the collected
  27. // format.
  28. void CollectUserData(
  29. const ActivityUserData::Snapshot& recorded_map,
  30. google::protobuf::Map<std::string, TypedValue>* collected_map,
  31. StabilityReport* report) {
  32. DCHECK(collected_map);
  33. for (const auto& name_and_value : recorded_map) {
  34. const std::string& key = name_and_value.first;
  35. const ActivityUserData::TypedValue& recorded_value = name_and_value.second;
  36. TypedValue collected_value;
  37. switch (recorded_value.type()) {
  38. case ActivityUserData::END_OF_VALUES:
  39. NOTREACHED();
  40. break;
  41. case ActivityUserData::RAW_VALUE: {
  42. base::StringPiece raw = recorded_value.Get();
  43. collected_value.set_bytes_value(raw.data(), raw.size());
  44. break;
  45. }
  46. case ActivityUserData::RAW_VALUE_REFERENCE: {
  47. base::StringPiece recorded_ref = recorded_value.GetReference();
  48. TypedValue::Reference* collected_ref =
  49. collected_value.mutable_bytes_reference();
  50. collected_ref->set_address(
  51. reinterpret_cast<uintptr_t>(recorded_ref.data()));
  52. collected_ref->set_size(recorded_ref.size());
  53. break;
  54. }
  55. case ActivityUserData::STRING_VALUE: {
  56. base::StringPiece value = recorded_value.GetString();
  57. collected_value.set_string_value(value.data(), value.size());
  58. // Promote version information to the global key value store.
  59. if (report) {
  60. bool should_promote =
  61. key == kActivityProduct || key == kActivityChannel ||
  62. key == kActivityPlatform || key == kActivityVersion;
  63. if (should_promote) {
  64. // TODO(siggi): Copy the promoted data or perhaps remove this
  65. // promotion once the backend is able to display the per-process
  66. // data.
  67. (*report->mutable_global_data())[key].Swap(&collected_value);
  68. continue;
  69. }
  70. }
  71. break;
  72. }
  73. case ActivityUserData::STRING_VALUE_REFERENCE: {
  74. base::StringPiece recorded_ref = recorded_value.GetStringReference();
  75. TypedValue::Reference* collected_ref =
  76. collected_value.mutable_string_reference();
  77. collected_ref->set_address(
  78. reinterpret_cast<uintptr_t>(recorded_ref.data()));
  79. collected_ref->set_size(recorded_ref.size());
  80. break;
  81. }
  82. case ActivityUserData::CHAR_VALUE: {
  83. char char_value = recorded_value.GetChar();
  84. collected_value.set_char_value(&char_value, 1);
  85. break;
  86. }
  87. case ActivityUserData::BOOL_VALUE:
  88. collected_value.set_bool_value(recorded_value.GetBool());
  89. break;
  90. case ActivityUserData::SIGNED_VALUE:
  91. collected_value.set_signed_value(recorded_value.GetInt());
  92. // Promote the execution timestamp to the global key value store.
  93. if (report && key == kActivityStartTimestamp) {
  94. (*report->mutable_global_data())[key].Swap(&collected_value);
  95. continue;
  96. }
  97. break;
  98. case ActivityUserData::UNSIGNED_VALUE:
  99. collected_value.set_unsigned_value(recorded_value.GetUint());
  100. break;
  101. }
  102. (*collected_map)[key].Swap(&collected_value);
  103. }
  104. }
  105. void CollectModuleInformation(
  106. const std::vector<GlobalActivityTracker::ModuleInfo>& modules,
  107. ProcessState* process_state) {
  108. DCHECK(process_state);
  109. for (const GlobalActivityTracker::ModuleInfo& recorded : modules) {
  110. CodeModule* collected = process_state->add_modules();
  111. collected->set_base_address(recorded.address);
  112. collected->set_size(recorded.size);
  113. collected->set_code_file(recorded.file);
  114. // Compute the code identifier using the required format.
  115. std::string code_identifier =
  116. base::StringPrintf("%08X%zx", recorded.timestamp, recorded.size);
  117. collected->set_code_identifier(code_identifier);
  118. collected->set_debug_file(recorded.debug_file);
  119. // Compute the debug identifier using the required format.
  120. const crashpad::UUID* uuid =
  121. reinterpret_cast<const crashpad::UUID*>(recorded.identifier);
  122. std::string debug_identifier = base::StringPrintf(
  123. "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%x", uuid->data_1,
  124. uuid->data_2, uuid->data_3, uuid->data_4[0], uuid->data_4[1],
  125. uuid->data_5[0], uuid->data_5[1], uuid->data_5[2], uuid->data_5[3],
  126. uuid->data_5[4], uuid->data_5[5], recorded.age);
  127. collected->set_debug_identifier(debug_identifier);
  128. collected->set_is_unloaded(!recorded.is_loaded);
  129. }
  130. }
  131. void CollectActivity(const base::debug::Activity& recorded,
  132. Activity* collected) {
  133. DCHECK(collected);
  134. collected->set_time(recorded.time_internal);
  135. collected->set_address(recorded.calling_address);
  136. collected->set_origin_address(recorded.origin_address);
  137. // TODO(manzagop): the current collection deals with specific activity types;
  138. // consider modifying it to handle the notion of activity categories.
  139. switch (recorded.activity_type) {
  140. case base::debug::Activity::ACT_TASK_RUN:
  141. collected->set_type(Activity::ACT_TASK_RUN);
  142. collected->set_task_sequence_id(recorded.data.task.sequence_id);
  143. break;
  144. case base::debug::Activity::ACT_LOCK_ACQUIRE:
  145. collected->set_type(Activity::ACT_LOCK_ACQUIRE);
  146. collected->set_lock_address(recorded.data.lock.lock_address);
  147. break;
  148. case base::debug::Activity::ACT_EVENT_WAIT:
  149. collected->set_type(Activity::ACT_EVENT_WAIT);
  150. collected->set_event_address(recorded.data.event.event_address);
  151. break;
  152. case base::debug::Activity::ACT_THREAD_JOIN:
  153. collected->set_type(Activity::ACT_THREAD_JOIN);
  154. collected->set_thread_id(recorded.data.thread.thread_id);
  155. break;
  156. case base::debug::Activity::ACT_PROCESS_WAIT:
  157. collected->set_type(Activity::ACT_PROCESS_WAIT);
  158. collected->set_process_id(recorded.data.process.process_id);
  159. break;
  160. case base::debug::Activity::ACT_GENERIC:
  161. collected->set_type(Activity::ACT_GENERIC);
  162. collected->set_generic_id(recorded.data.generic.id);
  163. collected->set_generic_data(recorded.data.generic.info);
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. void CollectException(const base::debug::Activity& recorded,
  170. Exception* collected) {
  171. DCHECK(collected);
  172. collected->set_code(recorded.data.exception.code);
  173. collected->set_program_counter(recorded.calling_address);
  174. collected->set_exception_address(recorded.origin_address);
  175. collected->set_time(recorded.time_internal);
  176. }
  177. void CollectThread(
  178. const base::debug::ThreadActivityAnalyzer::Snapshot& snapshot,
  179. ThreadState* thread_state) {
  180. DCHECK(thread_state);
  181. thread_state->set_thread_name(snapshot.thread_name);
  182. thread_state->set_thread_id(snapshot.thread_id);
  183. thread_state->set_activity_count(snapshot.activity_stack_depth);
  184. if (snapshot.last_exception.activity_type ==
  185. base::debug::Activity::ACT_EXCEPTION) {
  186. CollectException(snapshot.last_exception,
  187. thread_state->mutable_exception());
  188. }
  189. for (size_t i = 0; i < snapshot.activity_stack.size(); ++i) {
  190. Activity* collected = thread_state->add_activities();
  191. CollectActivity(snapshot.activity_stack[i], collected);
  192. if (i < snapshot.user_data_stack.size()) {
  193. CollectUserData(snapshot.user_data_stack[i],
  194. collected->mutable_user_data(), nullptr);
  195. }
  196. }
  197. }
  198. bool SetProcessType(ProcessState* process_state) {
  199. DCHECK(process_state);
  200. google::protobuf::Map<std::string, TypedValue>* process_data =
  201. process_state->mutable_data();
  202. const auto it = process_data->find(kActivityProcessType);
  203. if (it == process_data->end())
  204. return false;
  205. const TypedValue& value = it->second;
  206. if (value.value_case() != TypedValue::kSignedValue)
  207. return false;
  208. process_state->set_process_type(
  209. static_cast<browser_watcher::ProcessState_Type>(value.signed_value()));
  210. process_data->erase(it);
  211. return true;
  212. }
  213. void CollectProcess(int64_t pid,
  214. GlobalActivityAnalyzer* analyzer,
  215. StabilityReport* report) {
  216. DCHECK(analyzer);
  217. DCHECK(report);
  218. ProcessState* process_state = report->add_process_states();
  219. // Collect thread activity data.
  220. ThreadActivityAnalyzer* thread_analyzer = analyzer->GetFirstAnalyzer(pid);
  221. for (; thread_analyzer != nullptr;
  222. thread_analyzer = analyzer->GetNextAnalyzer()) {
  223. // Only valid analyzers are expected per contract of GetFirstAnalyzer /
  224. // GetNextAnalyzer.
  225. DCHECK(thread_analyzer->IsValid());
  226. if (!process_state->has_process_id()) {
  227. process_state->set_process_id(
  228. thread_analyzer->activity_snapshot().process_id);
  229. }
  230. DCHECK_EQ(thread_analyzer->activity_snapshot().process_id,
  231. process_state->process_id());
  232. ThreadState* thread_state = process_state->add_threads();
  233. CollectThread(thread_analyzer->activity_snapshot(), thread_state);
  234. }
  235. // Collect global user data.
  236. ActivityUserData::Snapshot process_data_snapshot =
  237. analyzer->GetProcessDataSnapshot(pid);
  238. CollectUserData(process_data_snapshot, process_state->mutable_data(), report);
  239. SetProcessType(process_state);
  240. // Collect module information.
  241. CollectModuleInformation(analyzer->GetModules(pid), process_state);
  242. }
  243. } // namespace
  244. CollectionStatus Extract(
  245. std::unique_ptr<GlobalActivityAnalyzer> global_analyzer,
  246. StabilityReport* report) {
  247. DCHECK(global_analyzer);
  248. DCHECK(report);
  249. report->set_is_complete(global_analyzer->IsDataComplete());
  250. // Collect process data.
  251. for (int64_t pid = global_analyzer->GetFirstProcess(); pid != 0;
  252. pid = global_analyzer->GetNextProcess()) {
  253. CollectProcess(pid, global_analyzer.get(), report);
  254. }
  255. // Collect log messages.
  256. std::vector<std::string> log_messages = global_analyzer->GetLogMessages();
  257. for (const std::string& message : log_messages) {
  258. report->add_log_messages(message);
  259. }
  260. return SUCCESS;
  261. }
  262. } // namespace browser_watcher