File: breakpad_file_extractor.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (266 lines) | stat: -rw-r--r-- 9,951 bytes parent folder | download | duplicates (6)
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
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Uses dump_syms to extract breakpad symbol files."""

import logging
import os
import subprocess
import sys

import flag_utils
import rename_breakpad

sys.path.insert(
    0,
    os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'third_party',
                 'catapult', 'common', 'py_utils'))
from py_utils import tempfile_ext


def ExtractBreakpadFiles(dump_syms_path,
                         build_dir,
                         breakpad_output_dir,
                         search_unstripped=True,
                         module_ids=None):
  """Uses dump_syms to extract breakpad files.

  Args:
    dump_syms_path: The path to the dump_syms binary that should be run.
    build_dir: The path to the input directory containing the binaries that
      dump_syms will use.
    breakpad_output_dir: The output directory for the breakpad symbol files
      produced.
    search_unstripped: Boolean flag for whether to search for 'lib.unstripped'
      subdirectory or not. If specified and '|build_dir|/lib.unstripped' exists,
      dump_syms is run on this directory instead. If not specified, dump_syms is
      run on |build_dir|.
    module_ids: A set of module IDs needed to symbolize the trace. Only extracts
      breakpad on symbol binaries with a module ID in this set. Extracts all
      symbols if |module_ids| is None.

  Returns:
    True if at least one breakpad file could be extracted from |build_dir|;
    False, otherwise

  Raises:
    Exception: If the dump_syms binary or the input and output
      directories cannot be found.
  """
  # Check to see if |dump_syms_path| is a file.
  dump_syms_path = EnsureDumpSymsBinary(dump_syms_path, build_dir)

  # Check if |build_dir| and |breakpad_base_dir| are directories.
  if not os.path.isdir(build_dir):
    raise Exception('Invalid build directory.')
  if not os.path.isdir(breakpad_output_dir):
    raise Exception('Invalid breakpad output directory.')

  # If on Android, lib.unstripped will hold symbols for binaries.
  symbol_dir = build_dir
  if search_unstripped and os.path.isdir(
      os.path.join(build_dir, 'lib.unstripped')):
    symbol_dir = os.path.join(build_dir, 'lib.unstripped')

  breakpad_file_count = 0
  for file_iter in os.listdir(symbol_dir):
    input_file_path = os.path.join(symbol_dir, file_iter)
    if os.path.isfile(input_file_path) and IsValidBinaryPath(input_file_path):
      if not IsModuleNeededForSymbolization(dump_syms_path, module_ids,
                                            input_file_path):
        continue
      # Construct absolute file paths for input and output files.
      output_file_path = os.path.join(breakpad_output_dir,
                                      file_iter + '.breakpad')

      flag_utils.GetTracingLogger().debug('Extracting breakpad file from: %s',
                                          input_file_path)
      if _RunDumpSyms(dump_syms_path, input_file_path, output_file_path):
        flag_utils.GetTracingLogger().debug('Extracted breakpad to: %s',
                                            output_file_path)
        breakpad_file_count += 1

  # Extracting breakpad symbols should be successful with at least one file.
  return breakpad_file_count > 0


def ExtractBreakpadOnSubtree(symbols_root, metadata, dump_syms_path):
  """Converts symbol files in the given subtree into breakpad files.

  Args:
    symbols_root: root of subtree containing symbol files to convert to breakpad
      format.
    metadata: trace metadata to extract module ids from.
    dump_syms_path: local path to dump_syms binary.

  Raises:
    Exception: if path to dump_syms binary not passed or no breakpad files
      could be extracted from subtree.
  """
  flag_utils.GetTracingLogger().debug('Converting symbols to breakpad format.')
  if dump_syms_path is None:
    raise Exception('Path to dump_syms binary is required for symbolizing '
                    'official Android traces. You can build dump_syms from '
                    'your local build directory with the right architecture '
                    'with: autoninja -C out_<arch>/Release dump_syms.')

  # Set of module IDs we need to symbolize.
  module_ids = GetModuleIdsToSymbolize(metadata)

  did_extract = False
  for root_dir, _, _ in os.walk(symbols_root, topdown=True):
    root_path = os.path.abspath(root_dir)
    did_extract |= ExtractBreakpadFiles(dump_syms_path,
                                        root_path,
                                        root_path,
                                        search_unstripped=False,
                                        module_ids=module_ids)

  if not did_extract:
    raise Exception(
        'No breakpad symbols could be extracted from files in the subtree: ' +
        symbols_root)


def GetModuleIdsToSymbolize(metadata):
  """Returns module IDs needed for symbolization and logs breakpad message.

  We log the message before calling |ExtractBreakpadFiles| because otherwise
  we will repeatedly log when |ExtractBreakpadOnSubtree| recursively runs
  |ExtractBreakpadFiles|.

  Args:
    metadata: metadata extracted from the trace.
  """
  module_ids = metadata.GetModuleIds()

  if module_ids is None:
    flag_utils.GetTracingLogger().info(
        'No specified modules to extract. Converting all symbol '
        'binaries to breakpad.')
  else:
    flag_utils.GetTracingLogger().debug('Module IDs to symbolize: %s',
                                        (module_ids))

  return module_ids


def IsModuleNeededForSymbolization(dump_syms_path, module_ids, symbol_binary):
  """Determines if we should extract breakpad from symbol binary.

  If module_ids is None, then we extract breakpad on all symbol binaries.
  Otherwise, we only extract breakpad on binaries with a module ID needed to
  symbolize the trace.

  Args:
    dump_syms_path: The path to the dump_syms binary that should be run.
    module_ids: A set of module IDs needed to symbolize the trace. Only extracts
      breakpad on symbol binaries with a module ID in this set. Extracts all
      symbols if |module_ids| is None.
    symbol_binary: Symbol binary file to symbolize trace.

  Returns:
    True if symbols should be extracted to breakpad; false, otherwise.
  """
  if module_ids is None:
    return True

  # Only convert breakpad if binary has module ID we need to symbolize.
  module_id = _GetModuleIDFromBinary(dump_syms_path, symbol_binary)
  if module_id is None or module_id not in module_ids:
    flag_utils.GetTracingLogger().debug(
        'Skipping breakpad extraction for module (%s, %s) '
        'since trace has no frames with this ID.', module_id, symbol_binary)
    return False
  return True


def _GetModuleIDFromBinary(dump_syms_path, symbol_binary):
  """Gets module ID of symbol binary.

  Args:
    dump_syms_path: The path to the dump_syms binary that should be run.
    symbol_binary: path to symbol binary.

  Returns:
    Module ID from symbol binary, or None if fails to extract.
  """
  # Creates temp file because |_RunDumpSyms| pipes result into a file.
  # After extracting the module ID, we do not need this output file.
  with tempfile_ext.NamedTemporaryFile(mode='w+') as output_file:
    output_file.close()  # RunDumpsyms opens the file again.
    if not _RunDumpSyms(dump_syms_path,
                        symbol_binary,
                        output_file.name,
                        only_module_header=True):
      return None
    return rename_breakpad.ExtractModuleIdIfValidBreakpad(output_file.name)


def _RunDumpSyms(dump_syms_binary,
                 input_file_path,
                 output_file_path,
                 only_module_header=False):
  """Runs the dump_syms binary on a file and outputs the resulting breakpad.

     symbols to the specified file.
  Args:
    dump_syms_binary: The path to the dump_syms binary that should be run.
    input_file_path: Input file path to run dump_syms on.
    output_file_path: Output file path to store result.
    only_module_header: Only extracts the module header, if specified.

  Returns:
    True if the command succeeded and false otherwise.
  """
  cmd = [dump_syms_binary]
  if only_module_header:
    cmd.append('-i')
  cmd.append(input_file_path)

  with open(output_file_path, 'w') as f:
    proc = subprocess.Popen(cmd, stdout=f, stderr=subprocess.PIPE)
  stderr = proc.communicate()[1]
  if proc.returncode != 0:
    flag_utils.GetTracingLogger().info(
        'Dump_syms failed to extract information from symbol binary: %s. '
        'Error: %s', input_file_path, str(stderr))
    return False
  return True


def EnsureDumpSymsBinary(dump_syms_path, build_dir):
  """Checks to see if dump_syms can be found.

  Args:
    dump_syms_path: The given path to dump_syms.
    build_dir: The path to a directory.

  Returns:
    The path to the dump_syms binary or raises exception.
  """
  if dump_syms_path is not None and os.path.isfile(
      dump_syms_path) and 'dump_syms' in dump_syms_path:
    return dump_syms_path

  if build_dir is not None:
    path_to_dump_syms = os.path.join(build_dir, 'dump_syms')
    if os.path.isfile(path_to_dump_syms):
      return path_to_dump_syms

  if not build_dir:
    build_dir = 'out/android'  # For error message.
  raise Exception(
      'dump_syms binary not found. Build a binary with '
      'autoninja -C {build_dir} dump_syms and try again with '
      '--dump_syms={build_dir}/dump_syms'.format(build_dir=build_dir))


def IsValidBinaryPath(path):
  # Get the file name from the full file path.
  file_name = os.path.basename(path)
  if file_name.endswith('partition.so') or file_name.endswith(
      '.dwp') or file_name.endswith('.dwo') or '_combined' in file_name:
    return False
  return file_name == 'chrome' or file_name.endswith(
      '.so') or file_name.endswith('.exe')