blob: 397acfcd08cbc589ad74297a28bc9865b0b7bf40 [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
7import os
Tom Anderson838a2b62018-07-30 17:58:278import subprocess
9import sys
Takuto Ikuta3ae0e03b2018-05-18 06:10:4010
Tom Andersonf06ac382019-04-10 03:49:3811compile_dirs = set()
Christian Biesinger3ab7c3532019-10-15 15:03:4912src_dir = None
Tom Andersonf06ac382019-04-10 03:49:3813
Takuto Ikuta52fcb202018-05-24 21:19:2214def get_current_debug_file_directories():
15 dir = gdb.execute("show debug-file-directory", to_string=True)
Tom Andersonf06ac382019-04-10 03:49:3816 dir = dir[
17 len('The directory where separate debug symbols are searched for is "'
18 ):-len('".') - 1]
Takuto Ikuta52fcb202018-05-24 21:19:2219 return set(dir.split(":"))
20
21
22def add_debug_file_directory(dir):
23 # gdb has no function to add debug-file-directory, simulates that by using
24 # `show debug-file-directory` and `set debug-file-directory <directories>`.
25 current_dirs = get_current_debug_file_directories()
26 current_dirs.add(dir)
Tom Andersonf06ac382019-04-10 03:49:3827 gdb.execute(
28 "set debug-file-directory %s" % ":".join(current_dirs), to_string=True)
Takuto Ikuta52fcb202018-05-24 21:19:2229
30
Tom Andersonf06ac382019-04-10 03:49:3831def load_libcxx_pretty_printers(src_dir):
sbingler7f3a5792023-11-06 05:19:2132 libcxx_pretty_printers = os.path.join(src_dir, 'third_party', 'libc++', 'src',
33 'utils', 'gdb', 'libcxx')
Tom Anderson838a2b62018-07-30 17:58:2734 if not os.path.isdir(libcxx_pretty_printers):
35 return
36 sys.path.insert(1, libcxx_pretty_printers)
Matt Fysh34415f72020-05-01 02:29:4437 from printers import register_libcxx_printer_loader
38 register_libcxx_printer_loader()
Tom Andersonf06ac382019-04-10 03:49:3839
40
41def load_gdb_chrome(src_dir):
42 tools_gdb = os.path.join(src_dir, 'tools', 'gdb')
43
44 sys.path.insert(1, tools_gdb)
45 import gdb_chrome
46
47 gdb.execute('source %s' % os.path.join(tools_gdb, 'viewg.gdb'))
Tom Anderson838a2b62018-07-30 17:58:2748
49
Christian Biesinger3ab7c3532019-10-15 15:03:4950def set_src_dir(compile_dir):
51 global src_dir
52 git = subprocess.Popen(
53 ['git', '-C', compile_dir, 'rev-parse', '--show-toplevel'],
54 stdout=subprocess.PIPE,
55 stderr=subprocess.PIPE)
56 src_dir, _ = git.communicate()
57 if git.returncode:
58 return
59 if isinstance(src_dir, str):
60 src_dir = src_dir.rstrip()
61 else:
62 src_dir = src_dir.decode('utf-8').rstrip()
63
Elly Fong-Jonesd93746d2023-02-22 17:34:1264 # If there's no chrome directory in the repo root, we got the wrong git repo.
65 # The most common way to have this happen is to be in a git checkout of
66 # another project that vendors chromium in its own third_party directory. In
67 # that case, try falling back to the current working directory.
68 if not os.path.isdir(os.path.join(src_dir, 'chrome')):
69 src_dir = os.path.abspath(os.getcwd())
70
Christian Biesinger3ab7c3532019-10-15 15:03:4971 load_libcxx_pretty_printers(src_dir)
72
73 load_gdb_chrome(src_dir)
74
75
Takuto Ikuta52fcb202018-05-24 21:19:2276def newobj_handler(event):
Tom Andersonf06ac382019-04-10 03:49:3877 global compile_dirs
Takuto Ikuta52fcb202018-05-24 21:19:2278 compile_dir = os.path.dirname(event.new_objfile.filename)
79 if not compile_dir:
80 return
Tom Andersonf06ac382019-04-10 03:49:3881 if compile_dir in compile_dirs:
82 return
83 compile_dirs.add(compile_dir)
Takuto Ikuta52fcb202018-05-24 21:19:2284
85 # Add source path
Takuto Ikuta3ae0e03b2018-05-18 06:10:4086 gdb.execute("dir %s" % compile_dir)
87
Takuto Ikuta52fcb202018-05-24 21:19:2288 # Need to tell the location of .dwo files.
89 # https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
90 # https://crbug.com/603286#c35
91 add_debug_file_directory(compile_dir)
92
Christian Biesinger3ab7c3532019-10-15 15:03:4993 global src_dir
94 if not src_dir:
95 set_src_dir(compile_dir)
Tom Anderson838a2b62018-07-30 17:58:2796
Takuto Ikuta52fcb202018-05-24 21:19:2297
98# Event hook for newly loaded objfiles.
99# https://sourceware.org/gdb/onlinedocs/gdb/Events-In-Python.html
100gdb.events.new_objfile.connect(newobj_handler)
101
Tom Anderson5a1ce542019-03-08 20:19:27102gdb.execute("set environment CHROMIUM_GDBINIT_SOURCED=1")
103
Christian Biesingerf1facae2019-12-09 18:42:38104# GDB 9.1 adds a setting, disabled by default, to do multithreaded symbol
105# loading. This is a major speed up for Chrome, so turn it on here. We
106# use try..except to avoid displaying errors in earlier GDB versions.
107try:
108 # unlimited means "number of cores"
David Grogan5b25ac782020-01-06 23:12:07109 gdb.execute("maint set worker-threads unlimited")
Christian Biesingerf1facae2019-12-09 18:42:38110except:
111 pass
112
Takuto Ikuta3ae0e03b2018-05-18 06:10:40113end