File: dump_allocation_recorder.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (332 lines) | stat: -rw-r--r-- 11,056 bytes parent folder | download | duplicates (8)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This implements a small tool to dump the recorded data of the allocation
// recorder from a Crashpad dump to cout.
//
// Invocation: dump_allocation_recorder_data <dump_file>
// When calling the tool without a dump or the given dump contains invalid data,
// an error message will be printed.
//
// Calling the tool with a valid dump file which contains data included from the
// allocation recorder will result in all recorded data being printed to the
// screen.
//
// Example output:
// ===================================================================
// Operation-Id: 0
// Type: FREE
// Address: 0x78802fd2980
// Size: not set
// Frames:
// # 0: libfoo.so + 0x2e404 (0x795e4ef2e404)
// [ ... ]
// #15: libbar.so + 0xdaa00 (0x795de0fdaa00)
// ===================================================================
// Operation-Id: 1
// Type: FREE
// Address: 0x78802fd2b00
// Size: not set
// Frames:
// # 0: libfoo.so + 0x2e404 (0x795e4ef2e404)
// [ ... ]
// #14: libbaz.so + 0x55eb9 (0x795e0f155eb9)
// ===================================================================
// [ many more entries here ]
// ===================================================================
//
// The call stack can than be symbolized using tools like stack.py or
// llvm-symbolize.

#include <iomanip>
#include <iostream>

#include "base/check.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/notreached.h"
#include "base/types/expected.h"
#include "components/allocation_recorder/crash_handler/memory_operation_report.pb.h"
#include "components/allocation_recorder/internal/internal.h"
#include "third_party/crashpad/crashpad/snapshot/minidump/process_snapshot_minidump.h"
#include "third_party/crashpad/crashpad/util/file/file_reader.h"

namespace allocation_recorder {
// Implement operator << for various types of the allocation recorder payload.
// The functions must live in the same namespace as the type to print.
// Therefore, they can't be moved into anonymous namespace.

std::ostream& operator<<(std::ostream& out_stream,
                         const OperationType& operation_type) {
  switch (operation_type) {
    case OperationType::ALLOCATION:
      out_stream << "ALLOCATION";
      break;
    case OperationType::FREE:
      out_stream << "FREE";
      break;
    case OperationType::NONE:
      out_stream << "NONE";
      break;
    default:
      // Some internal values are added by protobuf to the enum. Those values
      // shouldn't play a role, but the compiler complains if we do not handle
      // them.
      NOTREACHED();
  }
  return out_stream;
}

struct StackFrameAndMinidump {
  const StackFrame& frame;
  const crashpad::ProcessSnapshotMinidump& minidump;
};

std::ostream& operator<<(std::ostream& out_stream,
                         const StackFrameAndMinidump& frame_and_minidump) {
  const StackFrame& frame = frame_and_minidump.frame;
  const crashpad::ProcessSnapshotMinidump& minidump =
      frame_and_minidump.minidump;

  uint64_t relative_addr = 0;
  std::string module_name = "<unknown-module>";
  for (const crashpad::ModuleSnapshot* module : minidump.Modules()) {
    if (frame.address() >= module->Address() &&
        frame.address() < module->Address() + module->Size()) {
      relative_addr = frame.address() - module->Address();
      module_name = module->Name();
      break;
    }
  }

  out_stream << module_name << " + 0x" << std::hex << relative_addr << " (0x"
             << frame.address() << ")";
  return out_stream;
}

struct StackTraceAndMinidump {
  const StackTrace& stack_trace;
  const crashpad::ProcessSnapshotMinidump& minidump;
};

std::ostream& operator<<(
    std::ostream& out_stream,
    const StackTraceAndMinidump& stack_trace_and_minidump) {
  const StackTrace& stack_trace = stack_trace_and_minidump.stack_trace;
  const crashpad::ProcessSnapshotMinidump& minidump =
      stack_trace_and_minidump.minidump;

  if (stack_trace.frames_size() > 0) {
    const auto print_frame = [&](int frame_index, bool print_separator) {
      out_stream << '#' << std::setfill('0') << std::setw(2) << std::dec
                 << frame_index << ": "
                 << StackFrameAndMinidump{stack_trace.frames(frame_index),
                                          minidump};
      if (print_separator) {
        out_stream << '\n';
      }
    };

    const int last_frame_index = stack_trace.frames_size() - 1;

    for (int frame_index = 0; frame_index < last_frame_index; ++frame_index) {
      print_frame(frame_index, true);
    }

    print_frame(last_frame_index, false);
  }

  return out_stream;
}

struct MemoryOperationAndProcessMinidump {
  const MemoryOperation& operation;
  const crashpad::ProcessSnapshotMinidump& minidump;
};

std::ostream& operator<<(
    std::ostream& out_stream,
    const MemoryOperationAndProcessMinidump& operation_and_minidump) {
  const MemoryOperation& operation = operation_and_minidump.operation;
  const crashpad::ProcessSnapshotMinidump& minidump =
      operation_and_minidump.minidump;

  out_stream << "Type: " << operation.operation_type() << '\n';
  out_stream << "Address: " << std::hex << "0x" << operation.address() << '\n';

  out_stream << "Size: ";
  if (operation.has_size()) {
    out_stream << std::dec << operation.size();
  } else {
    out_stream << "not set";
  }
  out_stream << '\n';

  out_stream << "Frames:\n";
  if (operation.has_stack_trace()) {
    out_stream << StackTraceAndMinidump{operation.stack_trace(), minidump};
  } else {
    out_stream << "not set";
  }
  out_stream << '\n';

  return out_stream;
}

namespace {

const char* const entry_separator =
    "===================================================================\n";

base::expected<allocation_recorder::Payload, std::string>
FindAndDeserializeAllocationRecorderStream(
    const crashpad::ProcessSnapshotMinidump& minidump) {
  const std::vector<const crashpad::MinidumpStream*>& custom_streams =
      minidump.CustomMinidumpStreams();

  const auto it_user_stream = std::find_if(
      custom_streams.begin(), custom_streams.end(),
      [](const crashpad::MinidumpStream* user_stream) {
        return user_stream &&
               user_stream->stream_type() ==
                   static_cast<crashpad::MinidumpStreamType>(
                       allocation_recorder::internal::kStreamDataType);
      });

  if (it_user_stream == custom_streams.end()) {
    return base::unexpected("No allocation recorder stream found.");
  }

  allocation_recorder::Payload report;
  if (!report.ParseFromArray((*it_user_stream)->data().data(),
                             (*it_user_stream)->data().size())) {
    return base::unexpected(
        "The stream doesn't contain a valid allocation recorder payload.");
  }

  return base::ok(report);
}

void Print(const allocation_recorder::MemoryOperationReport& report,
           std::ostream& out_stream,
           const crashpad::ProcessSnapshotMinidump& minidump_process_snapshot) {
  if (report.memory_operations_size() == 0) {
    out_stream << "NO RECORDS\n";
  } else {
    const auto print_memory_operation = [&](int operation_index,
                                            bool print_entry_separator) {
      out_stream << "Operation-Id: " << std::dec << operation_index << '\n'
                 << MemoryOperationAndProcessMinidump{
                        report.memory_operations(operation_index),
                        minidump_process_snapshot};
      if (print_entry_separator) {
        out_stream << entry_separator;
      }
    };

    const int last_memory_operation_index = report.memory_operations_size() - 1;

    for (int memory_operation_index = 0;
         memory_operation_index < last_memory_operation_index;
         ++memory_operation_index) {
      print_memory_operation(memory_operation_index, true);
    }

    print_memory_operation(last_memory_operation_index, false);
  }
}

void Print(const allocation_recorder::ProcessingFailures& failures,
           std::ostream& out_stream) {
  if (failures.messages_size() > 0) {
    const auto& print_failure_message = [&](int message_index,
                                            bool print_entry_separator) {
      out_stream << failures.messages(message_index);
      if (print_entry_separator) {
        out_stream << entry_separator;
      }
    };

    const int last_message_index = failures.messages_size() - 1;

    for (int message_index = 0; message_index < failures.messages_size();
         ++message_index) {
      print_failure_message(message_index, true);
    }
    print_failure_message(last_message_index, false);
  }
}

void PrintPayload(
    const allocation_recorder::Payload& payload,
    const crashpad::ProcessSnapshotMinidump& minidump_process_snapshot) {
  std::ostream& out_stream = std::cout;

  out_stream << entry_separator;

  switch (payload.payload_case()) {
    case allocation_recorder::Payload::PayloadCase::kOperationReport:
      CHECK(payload.has_operation_report());
      Print(payload.operation_report(), out_stream, minidump_process_snapshot);
      break;
    case allocation_recorder::Payload::PayloadCase::kProcessingFailures:
      CHECK(payload.has_processing_failures());
      Print(payload.processing_failures(), out_stream);
      break;
    default:
      // Some internal values are added by protobuf to the enum. Those values
      // shouldn't play a role, but the compiler complains if we do not handle
      // them.
      NOTREACHED();
  }

  out_stream << entry_separator;
}

template <typename... T>
void PrintError(T&... message_parts) {
  ((std::cerr << std::forward<T>(message_parts)), ...) << "\n";
}

}  // namespace
}  // namespace allocation_recorder

int main(int argc, const char* argv[]) {
  base::CommandLine::Init(argc, argv);
  const base::CommandLine& command_line =
      *base::CommandLine::ForCurrentProcess();

  base::CommandLine::StringVector args = command_line.GetArgs();
  if (args.size() != 1) {
    allocation_recorder::PrintError("Usage: ", argv[0], " <minidump_path>");
    return 1;
  }

  base::FilePath path(args[0]);

  crashpad::FileReader minidump_file_reader;
  if (!minidump_file_reader.Open(path)) {
    allocation_recorder::PrintError("Can't open the minidump.");
    return 1;
  }

  crashpad::ProcessSnapshotMinidump minidump_process_snapshot;
  if (!minidump_process_snapshot.Initialize(&minidump_file_reader)) {
    allocation_recorder::PrintError("Can't initialize the process snapshot.");
    return 1;
  }

  auto payload_deserialization =
      allocation_recorder::FindAndDeserializeAllocationRecorderStream(
          minidump_process_snapshot);

  if (payload_deserialization.has_value()) {
    allocation_recorder::PrintPayload(payload_deserialization.value(),
                                      minidump_process_snapshot);
  } else {
    allocation_recorder::PrintError(payload_deserialization.error());
  }

  return 0;
}