File: payload.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 (127 lines) | stat: -rw-r--r-- 4,297 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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/allocation_recorder/crash_handler/payload.h"

#include <iterator>
#include <string>

#include "base/debug/allocation_trace.h"
#include "base/debug/debugging_buildflags.h"
#include "base/strings/stringprintf.h"
#include "components/allocation_recorder/crash_handler/memory_operation_report.pb.h"

namespace allocation_recorder::crash_handler {
namespace {

::allocation_recorder::Statistics ConvertRecorderStatistics(
    const base::debug::tracer::AllocationTraceRecorderStatistics&
        recorder_statistics) {
  ::allocation_recorder::Statistics data;

  data.set_total_number_of_operations(
      recorder_statistics.total_number_of_allocations);

#if BUILDFLAG(ENABLE_ALLOCATION_TRACE_RECORDER_FULL_REPORTING)
  data.set_total_number_of_collisions(
      recorder_statistics.total_number_of_collisions);
#endif

  return data;
}

::allocation_recorder::StackFrame ConvertStackFrame(const void* const frame) {
  ::allocation_recorder::StackFrame converted_frame;

  converted_frame.set_address(reinterpret_cast<uint64_t>(frame));

  return converted_frame;
}

::allocation_recorder::OperationType ConvertOperationType(
    base::debug::tracer::OperationType operation_type) {
  switch (operation_type) {
    case base::debug::tracer::OperationType::kNone:
      return allocation_recorder::OperationType::NONE;
    case base::debug::tracer::OperationType::kAllocation:
      return allocation_recorder::OperationType::ALLOCATION;
    case base::debug::tracer::OperationType::kFree:
      return allocation_recorder::OperationType::FREE;
  }
}

::allocation_recorder::StackTrace ConvertCallStack(
    const base::debug::tracer::StackTraceContainer& source_trace) {
  // We try to copy an _interesting_ section of the original stack trace. For
  // this we find the first null entry (aka the top of the stack) and copy the
  // n frames before it.
  const auto first_null_entry =
      std::find(std::begin(source_trace), std::end(source_trace), nullptr);

  ::allocation_recorder::StackTrace data;

  for (auto current_item = std::begin(source_trace);
       current_item != first_null_entry; ++current_item) {
    data.mutable_frames()->Add(ConvertStackFrame(*current_item));
  }

  return data;
}

allocation_recorder::MemoryOperation ConvertSingleRecord(
    const base::debug::tracer::OperationRecord& src_record) {
  allocation_recorder::MemoryOperation data;

  data.set_operation_type(ConvertOperationType(src_record.GetOperationType()));
  data.set_address(reinterpret_cast<uint64_t>(src_record.GetAddress()));
  if (src_record.GetOperationType() ==
      base::debug::tracer::OperationType::kAllocation) {
    data.set_size(src_record.GetSize());
  }
  *(data.mutable_stack_trace()) = ConvertCallStack(src_record.GetStackTrace());

  return data;
}

google::protobuf::RepeatedPtrField<allocation_recorder::MemoryOperation>
ConvertMemoryOperations(
    const base::debug::tracer::AllocationTraceRecorder& recorder) {
  google::protobuf::RepeatedPtrField<allocation_recorder::MemoryOperation> data;

  for (size_t operation_index = 0; operation_index < recorder.size();
       ++operation_index) {
    data.Add(ConvertSingleRecord(recorder[operation_index]));
  }

  return data;
}
}  // namespace

allocation_recorder::Payload CreatePayloadWithMemoryOperationReport(
    const base::debug::tracer::AllocationTraceRecorder& recorder) {
  allocation_recorder::Payload full_report;

  *(full_report.mutable_operation_report()->mutable_statistics()) =
      ConvertRecorderStatistics(recorder.GetRecorderStatistics());

  (*full_report.mutable_operation_report()->mutable_memory_operations()) =
      ConvertMemoryOperations(recorder);

  return full_report;
}

allocation_recorder::Payload CreatePayloadWithProcessingFailures(
    base::span<const std::string_view> error_messages) {
  allocation_recorder::Payload full_report;
  auto& destination_messages =
      *(full_report.mutable_processing_failures()->mutable_messages());

  for (const auto error_message : error_messages) {
    destination_messages.Add(std::string(error_message));
  }

  return full_report;
}

}  // namespace allocation_recorder::crash_handler