Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: components/browser_watcher/postmortem_report_extractor.cc

Issue 2722223002: Separate collection logic from the extraction of the report (Closed)
Patch Set: Add a TODO Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "components/browser_watcher/postmortem_report_extractor.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/debug/activity_analyzer.h"
13 #include "base/logging.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "components/browser_watcher/stability_data_names.h"
18 #include "components/variations/active_field_trials.h"
19 #include "third_party/crashpad/crashpad/util/misc/uuid.h"
20
21 namespace browser_watcher {
22
23 using ActivitySnapshot = base::debug::ThreadActivityAnalyzer::Snapshot;
24 using base::debug::ActivityUserData;
25 using base::debug::GlobalActivityAnalyzer;
26 using base::debug::GlobalActivityTracker;
27 using base::debug::ThreadActivityAnalyzer;
28
29 namespace {
30
31 const char kFieldTrialKeyPrefix[] = "FieldTrial.";
32
33 // Collects stability user data from the recorded format to the collected
34 // format.
35 void CollectUserData(
36 const ActivityUserData::Snapshot& recorded_map,
37 google::protobuf::Map<std::string, TypedValue>* collected_map,
38 StabilityReport* report) {
39 DCHECK(collected_map);
40
41 for (const auto& name_and_value : recorded_map) {
42 const std::string& key = name_and_value.first;
43 const ActivityUserData::TypedValue& recorded_value = name_and_value.second;
44 TypedValue collected_value;
45
46 switch (recorded_value.type()) {
47 case ActivityUserData::END_OF_VALUES:
48 NOTREACHED();
49 break;
50 case ActivityUserData::RAW_VALUE: {
51 base::StringPiece raw = recorded_value.Get();
52 collected_value.set_bytes_value(raw.data(), raw.size());
53 break;
54 }
55 case ActivityUserData::RAW_VALUE_REFERENCE: {
56 base::StringPiece recorded_ref = recorded_value.GetReference();
57 TypedValue::Reference* collected_ref =
58 collected_value.mutable_bytes_reference();
59 collected_ref->set_address(
60 reinterpret_cast<uintptr_t>(recorded_ref.data()));
61 collected_ref->set_size(recorded_ref.size());
62 break;
63 }
64 case ActivityUserData::STRING_VALUE: {
65 base::StringPiece value = recorded_value.GetString();
66
67 if (report && base::StartsWith(key, kFieldTrialKeyPrefix,
68 base::CompareCase::SENSITIVE)) {
69 // This entry represents an active Field Trial.
70 std::string trial_name =
71 key.substr(std::strlen(kFieldTrialKeyPrefix));
72 variations::ActiveGroupId group_id =
73 variations::MakeActiveGroupId(trial_name, value.as_string());
74 FieldTrial* field_trial = report->add_field_trials();
75 field_trial->set_name_id(group_id.name);
76 field_trial->set_group_id(group_id.group);
77 continue;
78 }
79
80 collected_value.set_string_value(value.data(), value.size());
81 break;
82 }
83 case ActivityUserData::STRING_VALUE_REFERENCE: {
84 base::StringPiece recorded_ref = recorded_value.GetStringReference();
85 TypedValue::Reference* collected_ref =
86 collected_value.mutable_string_reference();
87 collected_ref->set_address(
88 reinterpret_cast<uintptr_t>(recorded_ref.data()));
89 collected_ref->set_size(recorded_ref.size());
90 break;
91 }
92 case ActivityUserData::CHAR_VALUE: {
93 char char_value = recorded_value.GetChar();
94 collected_value.set_char_value(&char_value, 1);
95 break;
96 }
97 case ActivityUserData::BOOL_VALUE:
98 collected_value.set_bool_value(recorded_value.GetBool());
99 break;
100 case ActivityUserData::SIGNED_VALUE:
101 collected_value.set_signed_value(recorded_value.GetInt());
102 break;
103 case ActivityUserData::UNSIGNED_VALUE:
104 collected_value.set_unsigned_value(recorded_value.GetUint());
105 break;
106 }
107
108 (*collected_map)[key].Swap(&collected_value);
109 }
110 }
111
112 void CollectModuleInformation(
113 const std::vector<GlobalActivityTracker::ModuleInfo>& modules,
114 ProcessState* process_state) {
115 DCHECK(process_state);
116
117 char code_identifier[17];
Sigurður Ásgeirsson 2017/03/06 19:17:10 Ugh :/
manzagop (departed) 2017/03/06 21:29:39 Acknowledged.
118 char debug_identifier[41];
119
120 for (const GlobalActivityTracker::ModuleInfo& recorded : modules) {
121 CodeModule* collected = process_state->add_modules();
122 collected->set_base_address(recorded.address);
123 collected->set_size(recorded.size);
124 collected->set_code_file(recorded.file);
125
126 // Compute the code identifier using the required format.
127 snprintf(code_identifier, sizeof(code_identifier), "%08X%zx",
Sigurður Ásgeirsson 2017/03/06 19:17:10 I'd sprint for a StringPrintf here and below. Fixe
manzagop (departed) 2017/03/06 21:29:39 Done. Can you say more?
Sigurður Ásgeirsson 2017/03/07 14:46:01 https://www.google.ca/webhp?sourceid=chrome-instan
128 recorded.timestamp, recorded.size);
129 collected->set_code_identifier(code_identifier);
130 collected->set_debug_file(recorded.debug_file);
131
132 // Compute the debug identifier using the required format.
133 const crashpad::UUID* uuid =
134 reinterpret_cast<const crashpad::UUID*>(recorded.identifier);
135 snprintf(debug_identifier, sizeof(debug_identifier),
136 "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%x", uuid->data_1,
137 uuid->data_2, uuid->data_3, uuid->data_4[0], uuid->data_4[1],
138 uuid->data_5[0], uuid->data_5[1], uuid->data_5[2], uuid->data_5[3],
139 uuid->data_5[4], uuid->data_5[5], recorded.age);
140 collected->set_debug_identifier(debug_identifier);
141 collected->set_is_unloaded(!recorded.is_loaded);
142 }
143 }
144
145 } // namespace
146
147 // DO NOT SUBMIT: review diff stepping stone. Move the function after 1st round.
148 void CollectThread(
149 const base::debug::ThreadActivityAnalyzer::Snapshot& snapshot,
150 ThreadState* thread_state);
151
152 CollectionStatus Extract(const base::FilePath& stability_file,
153 StabilityReport* report) {
154 DCHECK(report);
155
156 // Create a global analyzer.
157 std::unique_ptr<GlobalActivityAnalyzer> global_analyzer =
158 GlobalActivityAnalyzer::CreateWithFile(stability_file);
159 if (!global_analyzer)
160 return ANALYZER_CREATION_FAILED;
161
162 // Early exit if there is no data.
163 std::vector<std::string> log_messages = global_analyzer->GetLogMessages();
164 ActivityUserData::Snapshot global_data_snapshot =
165 global_analyzer->GetGlobalUserDataSnapshot();
166 ThreadActivityAnalyzer* thread_analyzer = global_analyzer->GetFirstAnalyzer();
167 if (log_messages.empty() && global_data_snapshot.empty() &&
168 !thread_analyzer) {
169 return DEBUG_FILE_NO_DATA;
170 }
171
172 // Collect log messages.
173 for (const std::string& message : log_messages) {
174 report->add_log_messages(message);
175 }
176
177 // Collect global user data.
178 google::protobuf::Map<std::string, TypedValue>& global_data =
179 *(report->mutable_global_data());
180 CollectUserData(global_data_snapshot, &global_data, report);
181
182 // Collect thread activity data.
183 // Note: a single process is instrumented.
184 ProcessState* process_state = report->add_process_states();
185 for (; thread_analyzer != nullptr;
186 thread_analyzer = global_analyzer->GetNextAnalyzer()) {
187 // Only valid analyzers are expected per contract of GetFirstAnalyzer /
188 // GetNextAnalyzer.
189 DCHECK(thread_analyzer->IsValid());
190
191 if (!process_state->has_process_id()) {
192 process_state->set_process_id(
193 thread_analyzer->activity_snapshot().process_id);
194 }
195 DCHECK_EQ(thread_analyzer->activity_snapshot().process_id,
196 process_state->process_id());
197
198 ThreadState* thread_state = process_state->add_threads();
199 CollectThread(thread_analyzer->activity_snapshot(), thread_state);
200 }
201
202 // Collect module information.
203 CollectModuleInformation(global_analyzer->GetModules(), process_state);
204
205 return SUCCESS;
206 }
207
208 // DO NOT SUBMIT: review diff stepping stone. Move the function after 1st round.
209 void CollectThread(
210 const base::debug::ThreadActivityAnalyzer::Snapshot& snapshot,
211 ThreadState* thread_state) {
212 DCHECK(thread_state);
213
214 thread_state->set_thread_name(snapshot.thread_name);
215 thread_state->set_thread_id(snapshot.thread_id);
216 thread_state->set_activity_count(snapshot.activity_stack_depth);
217
218 for (size_t i = 0; i < snapshot.activity_stack.size(); ++i) {
219 const base::debug::Activity& recorded = snapshot.activity_stack[i];
220 Activity* collected = thread_state->add_activities();
221
222 // Collect activity
223 switch (recorded.activity_type) {
224 case base::debug::Activity::ACT_TASK_RUN:
225 collected->set_type(Activity::ACT_TASK_RUN);
226 collected->set_origin_address(recorded.origin_address);
227 collected->set_task_sequence_id(recorded.data.task.sequence_id);
228 break;
229 case base::debug::Activity::ACT_LOCK_ACQUIRE:
230 collected->set_type(Activity::ACT_LOCK_ACQUIRE);
231 collected->set_lock_address(recorded.data.lock.lock_address);
232 break;
233 case base::debug::Activity::ACT_EVENT_WAIT:
234 collected->set_type(Activity::ACT_EVENT_WAIT);
235 collected->set_event_address(recorded.data.event.event_address);
236 break;
237 case base::debug::Activity::ACT_THREAD_JOIN:
238 collected->set_type(Activity::ACT_THREAD_JOIN);
239 collected->set_thread_id(recorded.data.thread.thread_id);
240 break;
241 case base::debug::Activity::ACT_PROCESS_WAIT:
242 collected->set_type(Activity::ACT_PROCESS_WAIT);
243 collected->set_process_id(recorded.data.process.process_id);
244 break;
245 default:
246 break;
247 }
248
249 // Collect user data
250 if (i < snapshot.user_data_stack.size()) {
251 CollectUserData(snapshot.user_data_stack[i],
252 collected->mutable_user_data(), nullptr);
253 }
254 }
255 }
256
257 } // namespace browser_watcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698