summaryrefslogtreecommitdiff
path: root/chromium/components/browser_watcher/stability_report_user_stream_data_source.cc
blob: bec019b55e56b1085388e576927447d250fc3e6f (plain)
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
// Copyright 2017 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/stability_report_user_stream_data_source.h"

#include <string>
#include <utility>

#include <windows.h>

// Must be included after windows.h.
#include <psapi.h>

#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/process/memory.h"
#include "base/process/process.h"
#include "base/strings/string16.h"
#include "base/time/time.h"
#include "components/browser_watcher/minidump_user_streams.h"
#include "components/browser_watcher/stability_metrics.h"
#include "components/browser_watcher/stability_paths.h"
#include "components/browser_watcher/stability_report_extractor.h"
#include "third_party/crashpad/crashpad/minidump/minidump_user_extension_stream_data_source.h"
#include "third_party/crashpad/crashpad/snapshot/exception_snapshot.h"
#include "third_party/crashpad/crashpad/snapshot/process_snapshot.h"

namespace browser_watcher {

namespace {

base::FilePath GetStabilityFileName(
    const base::FilePath& user_data_dir,
    crashpad::ProcessSnapshot* process_snapshot) {
  DCHECK(process_snapshot);

  timeval creation_time = {};
  process_snapshot->ProcessStartTime(&creation_time);

  return GetStabilityFileForProcess(process_snapshot->ProcessID(),
                                    creation_time, user_data_dir);
}

class BufferExtensionStreamDataSource final
    : public crashpad::MinidumpUserExtensionStreamDataSource {
 public:
  explicit BufferExtensionStreamDataSource(uint32_t stream_type);

  bool Init(const StabilityReport& report);

  size_t StreamDataSize() override;
  bool ReadStreamData(Delegate* delegate) override;

 private:
  std::string data_;

  DISALLOW_COPY_AND_ASSIGN(BufferExtensionStreamDataSource);
};

BufferExtensionStreamDataSource::BufferExtensionStreamDataSource(
    uint32_t stream_type)
    : crashpad::MinidumpUserExtensionStreamDataSource(stream_type) {}

bool BufferExtensionStreamDataSource::Init(const StabilityReport& report) {
  if (report.SerializeToString(&data_))
    return true;
  data_.clear();
  return false;
}

size_t BufferExtensionStreamDataSource::StreamDataSize() {
  DCHECK(!data_.empty());
  return data_.size();
}

bool BufferExtensionStreamDataSource::ReadStreamData(Delegate* delegate) {
  DCHECK(!data_.empty());
  return delegate->ExtensionStreamDataSourceRead(
      data_.size() ? data_.data() : nullptr, data_.size());
}

// TODO(manzagop): Collection should factor in whether this is a true crash or
// dump without crashing.
bool CollectStabilityReport(const base::FilePath& path,
                            StabilityReport* report) {
  CollectionStatus status = Extract(path, report);
  UMA_HISTOGRAM_ENUMERATION("ActivityTracker.CollectCrash.Status", status,
                            COLLECTION_STATUS_MAX);
  if (status != SUCCESS)
    return false;

  LogCollectOnCrashEvent(CollectOnCrashEvent::kReportExtractionSuccess);
  MarkStabilityFileDeletedOnCrash(path);

  return true;
}

void CollectSystemPerformanceMetrics(StabilityReport* report) {
  // Grab system commit memory. Also best effort.
  PERFORMANCE_INFORMATION perf_info = {sizeof(perf_info)};
  if (GetPerformanceInfo(&perf_info, sizeof(perf_info))) {
    auto* memory_state =
        report->mutable_system_memory_state()->mutable_windows_memory();

    memory_state->set_system_commit_limit(perf_info.CommitLimit);
    memory_state->set_system_commit_remaining(perf_info.CommitLimit -
                                              perf_info.CommitTotal);
    memory_state->set_system_handle_count(perf_info.HandleCount);
  }
}

void CollectProcessPerformanceMetrics(
    crashpad::ProcessSnapshot* process_snapshot,
    StabilityReport* report) {
  const crashpad::ExceptionSnapshot* exception = process_snapshot->Exception();

  if (!exception)
    return;

  // Find or create the ProcessState for the process in question.
  base::ProcessId pid = process_snapshot->ProcessID();
  ProcessState* process_state = nullptr;
  for (int i = 0; i < report->process_states_size(); ++i) {
    ProcessState* temp = report->mutable_process_states(i);

    if (temp->has_process_id() && temp->process_id() == pid) {
      process_state = temp;
      break;
    }
  }

  if (!process_state) {
    process_state = report->add_process_states();
    process_state->set_process_id(pid);
  }

  auto* memory_state =
      process_state->mutable_memory_state()->mutable_windows_memory();

  // Grab the requested allocation size in case of OOM exception.
  if (exception->Exception() == base::win::kOomExceptionCode) {
    const auto& codes = exception->Codes();
    if (codes.size()) {
      // The first parameter, if present, is the size of the allocation attempt.
      memory_state->set_process_allocation_attempt(codes[0]);
    }
  }

  base::Process process(base::Process::OpenWithAccess(
      pid, PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ));

  if (process.IsValid()) {
    PROCESS_MEMORY_COUNTERS_EX process_memory = {sizeof(process_memory)};
    if (::GetProcessMemoryInfo(
            process.Handle(),
            reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&process_memory),
            sizeof(process_memory))) {
      // This is in units of bytes, re-scale to pages for consistency with
      // system metrics.
      const uint64_t kPageSize = 4096;
      memory_state->set_process_private_usage(process_memory.PrivateUsage /
                                              kPageSize);
      memory_state->set_process_peak_workingset_size(
          process_memory.PeakWorkingSetSize / kPageSize);
      memory_state->set_process_peak_pagefile_usage(
          process_memory.PeakPagefileUsage / kPageSize);
    }

    DWORD process_handle_count = 0;
    if (::GetProcessHandleCount(process.Handle(), &process_handle_count)) {
      memory_state->set_process_handle_count(process_handle_count);
    }
  }
}

}  // namespace

StabilityReportUserStreamDataSource::StabilityReportUserStreamDataSource(
    const base::FilePath& user_data_dir)
    : user_data_dir_(user_data_dir) {}

std::unique_ptr<crashpad::MinidumpUserExtensionStreamDataSource>
StabilityReportUserStreamDataSource::ProduceStreamData(
    crashpad::ProcessSnapshot* process_snapshot) {
  DCHECK(process_snapshot);
  LogCollectOnCrashEvent(CollectOnCrashEvent::kCollectAttempt);

  StabilityReport report;
  bool collected_report = false;
  if (!user_data_dir_.empty()) {
    LogCollectOnCrashEvent(CollectOnCrashEvent::kUserDataDirNotEmpty);

    base::FilePath stability_file =
        GetStabilityFileName(user_data_dir_, process_snapshot);
    if (PathExists(stability_file)) {
      LogCollectOnCrashEvent(CollectOnCrashEvent::kPathExists);

      collected_report = CollectStabilityReport(stability_file, &report);
    }
  }

  CollectSystemPerformanceMetrics(&report);
  CollectProcessPerformanceMetrics(process_snapshot, &report);

  std::unique_ptr<BufferExtensionStreamDataSource> source(
      new BufferExtensionStreamDataSource(kStabilityReportStreamType));
  if (!source->Init(report))
    return nullptr;

  if (collected_report)
    LogCollectOnCrashEvent(CollectOnCrashEvent::kSuccess);

  return source;
}

}  // namespace browser_watcher