File: postmortem_report_collector.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (351 lines) | stat: -rw-r--r-- 13,204 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/browser_watcher/postmortem_report_collector.h"

#include <utility>

#include "base/debug/activity_analyzer.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/path_service.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"
#include "components/browser_watcher/postmortem_minidump_writer.h"
#include "third_party/crashpad/crashpad/client/settings.h"
#include "third_party/crashpad/crashpad/util/misc/uuid.h"

using base::FilePath;

namespace browser_watcher {

using ActivitySnapshot = base::debug::ThreadActivityAnalyzer::Snapshot;
using base::debug::ActivityUserData;
using base::debug::GlobalActivityAnalyzer;
using base::debug::ThreadActivityAnalyzer;
using crashpad::CrashReportDatabase;

namespace {

// Collects stability user data from the recorded format to the collected
// format.
void CollectUserData(
    const ActivityUserData::Snapshot& recorded_map,
    google::protobuf::Map<std::string, TypedValue>* collected_map) {
  DCHECK(collected_map);

  for (const auto& name_and_value : recorded_map) {
    const ActivityUserData::TypedValue& recorded_value = name_and_value.second;
    TypedValue collected_value;

    switch (recorded_value.type()) {
      case ActivityUserData::END_OF_VALUES:
        NOTREACHED();
        break;
      case ActivityUserData::RAW_VALUE:
        collected_value.set_bytes_value(recorded_value.Get().as_string());
        break;
      case ActivityUserData::RAW_VALUE_REFERENCE: {
        base::StringPiece recorded_ref = recorded_value.GetReference();
        TypedValue::Reference* collected_ref =
            collected_value.mutable_bytes_reference();
        collected_ref->set_address(
            reinterpret_cast<uintptr_t>(recorded_ref.data()));
        collected_ref->set_size(recorded_ref.size());
        break;
      }
      case ActivityUserData::STRING_VALUE:
        collected_value.set_string_value(
            recorded_value.GetString().as_string());
        break;
      case ActivityUserData::STRING_VALUE_REFERENCE: {
        base::StringPiece recorded_ref = recorded_value.GetStringReference();
        TypedValue::Reference* collected_ref =
            collected_value.mutable_string_reference();
        collected_ref->set_address(
            reinterpret_cast<uintptr_t>(recorded_ref.data()));
        collected_ref->set_size(recorded_ref.size());
        break;
      }
      case ActivityUserData::CHAR_VALUE:
        collected_value.set_char_value(
            std::string(1, recorded_value.GetChar()));
        break;
      case ActivityUserData::BOOL_VALUE:
        collected_value.set_bool_value(recorded_value.GetBool());
        break;
      case ActivityUserData::SIGNED_VALUE:
        collected_value.set_signed_value(recorded_value.GetInt());
        break;
      case ActivityUserData::UNSIGNED_VALUE:
        collected_value.set_unsigned_value(recorded_value.GetUint());
        break;
    }

    (*collected_map)[name_and_value.first].Swap(&collected_value);
  }
}

}  // namespace

PostmortemReportCollector::PostmortemReportCollector(
    const std::string& product_name,
    const std::string& version_number,
    const std::string& channel_name)
    : product_name_(product_name),
      version_number_(version_number),
      channel_name_(channel_name) {}

int PostmortemReportCollector::CollectAndSubmitForUpload(
    const base::FilePath& debug_info_dir,
    const base::FilePath::StringType& debug_file_pattern,
    const std::set<base::FilePath>& excluded_debug_files,
    crashpad::CrashReportDatabase* report_database) {
  DCHECK_NE(true, debug_info_dir.empty());
  DCHECK_NE(true, debug_file_pattern.empty());
  DCHECK_NE(nullptr, report_database);

  // Collect the list of files to harvest.
  std::vector<FilePath> debug_files = GetDebugStateFilePaths(
      debug_info_dir, debug_file_pattern, excluded_debug_files);

  // Determine the crashpad client id.
  crashpad::UUID client_id;
  crashpad::Settings* settings = report_database->GetSettings();
  if (settings) {
    // If GetSettings() or GetClientID() fails client_id will be left at its
    // default value, all zeroes, which is appropriate.
    settings->GetClientID(&client_id);
  }

  // Process each stability file.
  int success_cnt = 0;
  for (const FilePath& file : debug_files) {
    CollectionStatus status =
        CollectAndSubmit(client_id, file, report_database);
    // TODO(manzagop): consider making this a stability metric.
    UMA_HISTOGRAM_ENUMERATION("ActivityTracker.Collect.Status", status,
                              COLLECTION_STATUS_MAX);
    if (status == SUCCESS)
      ++success_cnt;
  }

  return success_cnt;
}

std::vector<FilePath> PostmortemReportCollector::GetDebugStateFilePaths(
    const base::FilePath& debug_info_dir,
    const base::FilePath::StringType& debug_file_pattern,
    const std::set<FilePath>& excluded_debug_files) {
  DCHECK_NE(true, debug_info_dir.empty());
  DCHECK_NE(true, debug_file_pattern.empty());

  std::vector<FilePath> paths;
  base::FileEnumerator enumerator(debug_info_dir, false /* recursive */,
                                  base::FileEnumerator::FILES,
                                  debug_file_pattern);
  FilePath path;
  for (path = enumerator.Next(); !path.empty(); path = enumerator.Next()) {
    if (excluded_debug_files.find(path) == excluded_debug_files.end())
      paths.push_back(path);
  }
  return paths;
}

PostmortemReportCollector::CollectionStatus
PostmortemReportCollector::CollectAndSubmit(
    const crashpad::UUID& client_id,
    const FilePath& file,
    crashpad::CrashReportDatabase* report_database) {
  DCHECK_NE(nullptr, report_database);

  // Note: the code below involves two notions of report: chrome internal state
  // reports and the crashpad reports they get wrapped into.

  // Collect the data from the debug file to a proto. Note: a non-empty report
  // is interpreted here as an unclean exit.
  std::unique_ptr<StabilityReport> report_proto;
  CollectionStatus status = Collect(file, &report_proto);
  if (status != SUCCESS) {
    // The file was empty, or there was an error collecting the data. Detailed
    // logging happens within the Collect function.
    if (!base::DeleteFile(file, false))
      LOG(ERROR) << "Failed to delete " << file.value();
    return status;
  }
  DCHECK_NE(nullptr, report_proto.get());

  // Prepare a crashpad report.
  CrashReportDatabase::NewReport* new_report = nullptr;
  CrashReportDatabase::OperationStatus database_status =
      report_database->PrepareNewCrashReport(&new_report);
  if (database_status != CrashReportDatabase::kNoError) {
    LOG(ERROR) << "PrepareNewCrashReport failed";
    return PREPARE_NEW_CRASH_REPORT_FAILED;
  }
  CrashReportDatabase::CallErrorWritingCrashReport
      call_error_writing_crash_report(report_database, new_report);

  // Write the report to a minidump.
  if (!WriteReportToMinidump(*report_proto, client_id, new_report->uuid,
                             reinterpret_cast<FILE*>(new_report->handle))) {
    return WRITE_TO_MINIDUMP_FAILED;
  }

  // If the file cannot be deleted, do not report its contents. Note this can
  // lead to under reporting and retries. However, under reporting is
  // preferable to the over reporting that would happen with a file that
  // cannot be deleted.
  // TODO(manzagop): metrics for the number of non-deletable files.
  if (!base::DeleteFile(file, false)) {
    LOG(ERROR) << "Failed to delete " << file.value();
    return DEBUG_FILE_DELETION_FAILED;
  }

  // Finalize the report wrt the report database. Note that this doesn't trigger
  // an immediate upload, but Crashpad will eventually upload the report (as of
  // writing, the delay is on the order of up to 15 minutes).
  call_error_writing_crash_report.Disarm();
  crashpad::UUID unused_report_id;
  database_status = report_database->FinishedWritingCrashReport(
      new_report, &unused_report_id);
  if (database_status != CrashReportDatabase::kNoError) {
    LOG(ERROR) << "FinishedWritingCrashReport failed";
    return FINISHED_WRITING_CRASH_REPORT_FAILED;
  }

  return SUCCESS;
}

PostmortemReportCollector::CollectionStatus PostmortemReportCollector::Collect(
    const base::FilePath& debug_state_file,
    std::unique_ptr<StabilityReport>* report) {
  DCHECK_NE(nullptr, report);
  report->reset();

  // Create a global analyzer.
  std::unique_ptr<GlobalActivityAnalyzer> global_analyzer =
      GlobalActivityAnalyzer::CreateWithFile(debug_state_file);
  if (!global_analyzer)
    return ANALYZER_CREATION_FAILED;

  // Early exit if there is no data.
  std::vector<std::string> log_messages = global_analyzer->GetLogMessages();
  ActivityUserData::Snapshot global_data_snapshot =
      global_analyzer->GetGlobalUserDataSnapshot();
  ThreadActivityAnalyzer* thread_analyzer = global_analyzer->GetFirstAnalyzer();
  if (log_messages.empty() && global_data_snapshot.empty() &&
      !thread_analyzer) {
    return DEBUG_FILE_NO_DATA;
  }

  // Create the report, then flesh it out.
  report->reset(new StabilityReport());

  // Collect log messages.
  for (const std::string& message : log_messages) {
    (*report)->add_log_messages(message);
  }

  // Collect global user data.
  CollectUserData(global_data_snapshot, (*report)->mutable_global_data());

  // Collect thread activity data.
  // Note: a single process is instrumented.
  ProcessState* process_state = (*report)->add_process_states();
  for (; thread_analyzer != nullptr;
       thread_analyzer = global_analyzer->GetNextAnalyzer()) {
    // Only valid analyzers are expected per contract of GetFirstAnalyzer /
    // GetNextAnalyzer.
    DCHECK(thread_analyzer->IsValid());

    if (!process_state->has_process_id()) {
      process_state->set_process_id(
          thread_analyzer->activity_snapshot().process_id);
    }
    DCHECK_EQ(thread_analyzer->activity_snapshot().process_id,
              process_state->process_id());

    ThreadState* thread_state = process_state->add_threads();
    CollectThread(thread_analyzer->activity_snapshot(), thread_state);
  }

  return SUCCESS;
}

void PostmortemReportCollector::CollectThread(
    const base::debug::ThreadActivityAnalyzer::Snapshot& snapshot,
    ThreadState* thread_state) {
  DCHECK(thread_state);

  thread_state->set_thread_name(snapshot.thread_name);
  thread_state->set_thread_id(snapshot.thread_id);
  thread_state->set_activity_count(snapshot.activity_stack_depth);

  for (size_t i = 0; i < snapshot.activity_stack.size(); ++i) {
    const base::debug::Activity& recorded = snapshot.activity_stack[i];
    Activity* collected = thread_state->add_activities();

    // Collect activity
    switch (recorded.activity_type) {
      case base::debug::Activity::ACT_TASK_RUN:
        collected->set_type(Activity::ACT_TASK_RUN);
        collected->set_origin_address(recorded.origin_address);
        collected->set_task_sequence_id(recorded.data.task.sequence_id);
        break;
      case base::debug::Activity::ACT_LOCK_ACQUIRE:
        collected->set_type(Activity::ACT_LOCK_ACQUIRE);
        collected->set_lock_address(recorded.data.lock.lock_address);
        break;
      case base::debug::Activity::ACT_EVENT_WAIT:
        collected->set_type(Activity::ACT_EVENT_WAIT);
        collected->set_event_address(recorded.data.event.event_address);
        break;
      case base::debug::Activity::ACT_THREAD_JOIN:
        collected->set_type(Activity::ACT_THREAD_JOIN);
        collected->set_thread_id(recorded.data.thread.thread_id);
        break;
      case base::debug::Activity::ACT_PROCESS_WAIT:
        collected->set_type(Activity::ACT_PROCESS_WAIT);
        collected->set_process_id(recorded.data.process.process_id);
        break;
      default:
        break;
    }

    // Collect user data
    if (i < snapshot.user_data_stack.size()) {
      CollectUserData(snapshot.user_data_stack[i],
                      collected->mutable_user_data());
    }
  }
}

bool PostmortemReportCollector::WriteReportToMinidump(
    const StabilityReport& report,
    const crashpad::UUID& client_id,
    const crashpad::UUID& report_id,
    base::PlatformFile minidump_file) {
  MinidumpInfo minidump_info;
  minidump_info.client_id = client_id;
  minidump_info.report_id = report_id;
  // TODO(manzagop): replace this information, i.e. the reporter's attributes,
  // by that of the reportee. Doing so requires adding this information to the
  // stability report. In the meantime, there is a tolerable information
  // mismatch after upgrades.
  minidump_info.product_name = product_name();
  minidump_info.version_number = version_number();
  minidump_info.channel_name = channel_name();
#if defined(ARCH_CPU_X86)
  minidump_info.platform = std::string("Win32");
#elif defined(ARCH_CPU_X86_64)
  minidump_info.platform = std::string("Win64");
#endif

  return WritePostmortemDump(minidump_file, report, minidump_info);
}

}  // namespace browser_watcher