blob: 63eb4799bb4274ae6828824b2199d1b5883ce14b [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:091# Copyright 2018 The Chromium Authors
Tom Andersonf06ac382019-04-10 03:49:382# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Takuto Ikuta52fcb202018-05-24 21:19:224
5python
Takuto Ikuta3ae0e03b2018-05-18 06:10:406
Hans Wennborg75399ae2025-04-29 16:52:517import gdb.printing
Takuto Ikuta3ae0e03b2018-05-18 06:10:408import os
Tom Anderson838a2b62018-07-30 17:58:279import subprocess
10import sys
Takuto Ikuta3ae0e03b2018-05-18 06:10:4011
Tom Andersonf06ac382019-04-10 03:49:3812compile_dirs = set()
Christian Biesinger3ab7c3532019-10-15 15:03:4913src_dir = None
Tom Andersonf06ac382019-04-10 03:49:3814
Takuto Ikuta52fcb202018-05-24 21:19:2215def get_current_debug_file_directories():
16 dir = gdb.execute("show debug-file-directory", to_string=True)
Tom Andersonf06ac382019-04-10 03:49:3817 dir = dir[
18 len('The directory where separate debug symbols are searched for is "'
19 ):-len('".') - 1]
Takuto Ikuta52fcb202018-05-24 21:19:2220 return set(dir.split(":"))
21
22
23def add_debug_file_directory(dir):
24 # gdb has no function to add debug-file-directory, simulates that by using
25 # `show debug-file-directory` and `set debug-file-directory <directories>`.
26 current_dirs = get_current_debug_file_directories()
27 current_dirs.add(dir)
Tom Andersonf06ac382019-04-10 03:49:3828 gdb.execute(
29 "set debug-file-directory %s" % ":".join(current_dirs), to_string=True)
Takuto Ikuta52fcb202018-05-24 21:19:2230
31
Tom Andersonf06ac382019-04-10 03:49:3832def load_libcxx_pretty_printers(src_dir):
sbingler7f3a5792023-11-06 05:19:2133 libcxx_pretty_printers = os.path.join(src_dir, 'third_party', 'libc++', 'src',
34 'utils', 'gdb', 'libcxx')
Tom Anderson838a2b62018-07-30 17:58:2735 if not os.path.isdir(libcxx_pretty_printers):
36 return
37 sys.path.insert(1, libcxx_pretty_printers)
Matt Fysh34415f72020-05-01 02:29:4438 from printers import register_libcxx_printer_loader
39 register_libcxx_printer_loader()
Tom Andersonf06ac382019-04-10 03:49:3840
41
42def load_gdb_chrome(src_dir):
43 tools_gdb = os.path.join(src_dir, 'tools', 'gdb')
44
45 sys.path.insert(1, tools_gdb)
46 import gdb_chrome
47
48 gdb.execute('source %s' % os.path.join(tools_gdb, 'viewg.gdb'))
Tom Anderson838a2b62018-07-30 17:58:2749
50
Christian Biesinger3ab7c3532019-10-15 15:03:4951def set_src_dir(compile_dir):
52 global src_dir
53 git = subprocess.Popen(
54 ['git', '-C', compile_dir, 'rev-parse', '--show-toplevel'],
55 stdout=subprocess.PIPE,
56 stderr=subprocess.PIPE)
57 src_dir, _ = git.communicate()
58 if git.returncode:
59 return
60 if isinstance(src_dir, str):
61 src_dir = src_dir.rstrip()
62 else:
63 src_dir = src_dir.decode('utf-8').rstrip()
64
Elly Fong-Jonesd93746d2023-02-22 17:34:1265 # If there's no chrome directory in the repo root, we got the wrong git repo.
66 # The most common way to have this happen is to be in a git checkout of
67 # another project that vendors chromium in its own third_party directory. In
68 # that case, try falling back to the current working directory.
69 if not os.path.isdir(os.path.join(src_dir, 'chrome')):
70 src_dir = os.path.abspath(os.getcwd())
71
Christian Biesinger3ab7c3532019-10-15 15:03:4972 load_libcxx_pretty_printers(src_dir)
73
74 load_gdb_chrome(src_dir)
75
76
Takuto Ikuta52fcb202018-05-24 21:19:2277def newobj_handler(event):
Tom Andersonf06ac382019-04-10 03:49:3878 global compile_dirs
Takuto Ikuta52fcb202018-05-24 21:19:2279 compile_dir = os.path.dirname(event.new_objfile.filename)
80 if not compile_dir:
81 return
Tom Andersonf06ac382019-04-10 03:49:3882 if compile_dir in compile_dirs:
83 return
84 compile_dirs.add(compile_dir)
Takuto Ikuta52fcb202018-05-24 21:19:2285
86 # Add source path
Takuto Ikuta3ae0e03b2018-05-18 06:10:4087 gdb.execute("dir %s" % compile_dir)
88
Takuto Ikuta52fcb202018-05-24 21:19:2289 # Need to tell the location of .dwo files.
90 # https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
91 # https://crbug.com/603286#c35
92 add_debug_file_directory(compile_dir)
93
Christian Biesinger3ab7c3532019-10-15 15:03:4994 global src_dir
95 if not src_dir:
96 set_src_dir(compile_dir)
Tom Anderson838a2b62018-07-30 17:58:2797
Takuto Ikuta52fcb202018-05-24 21:19:2298
99# Event hook for newly loaded objfiles.
100# https://sourceware.org/gdb/onlinedocs/gdb/Events-In-Python.html
101gdb.events.new_objfile.connect(newobj_handler)
102
Tom Anderson5a1ce542019-03-08 20:19:27103gdb.execute("set environment CHROMIUM_GDBINIT_SOURCED=1")
104
Christian Biesingerf1facae2019-12-09 18:42:38105# GDB 9.1 adds a setting, disabled by default, to do multithreaded symbol
106# loading. This is a major speed up for Chrome, so turn it on here. We
107# use try..except to avoid displaying errors in earlier GDB versions.
108try:
109 # unlimited means "number of cores"
David Grogan5b25ac782020-01-06 23:12:07110 gdb.execute("maint set worker-threads unlimited")
Christian Biesingerf1facae2019-12-09 18:42:38111except:
112 pass
113
Takuto Ikuta3ae0e03b2018-05-18 06:10:40114end