blob: f85c233ea061c6a4b4e5230757b38ecf994460f7 [file] [log] [blame]
Tom Anderson71df8872018-06-21 19:02:251# This is gdbinit for source level debugging with -fdebug-compilation-dir
Tom Anderson838a2b62018-07-30 17:58:272# compile option or when building with libc++.
Takuto Ikuta52fcb202018-05-24 21:19:223
4python
Takuto Ikuta3ae0e03b2018-05-18 06:10:405
6import os
Tom Anderson838a2b62018-07-30 17:58:277import subprocess
8import sys
Takuto Ikuta3ae0e03b2018-05-18 06:10:409
Takuto Ikuta52fcb202018-05-24 21:19:2210def get_current_debug_file_directories():
11 dir = gdb.execute("show debug-file-directory", to_string=True)
12 dir = dir[len('The directory where separate debug symbols are searched for is "'):-len('".')-1]
13 return set(dir.split(":"))
14
15
16def add_debug_file_directory(dir):
17 # gdb has no function to add debug-file-directory, simulates that by using
18 # `show debug-file-directory` and `set debug-file-directory <directories>`.
19 current_dirs = get_current_debug_file_directories()
20 current_dirs.add(dir)
21 gdb.execute("set debug-file-directory %s" % ":".join(current_dirs),
22 to_string=True)
23
24
Tom Anderson838a2b62018-07-30 17:58:2725libcxx_pretty_printers_loaded = False
26def load_libcxx_pretty_printers(compile_dir):
27 global libcxx_pretty_printers_loaded
28 if libcxx_pretty_printers_loaded:
29 return
30 git = subprocess.Popen(
31 ['git', '-C', compile_dir, 'rev-parse', '--show-toplevel'],
32 stdout=subprocess.PIPE,
33 stderr=subprocess.PIPE)
34 src_dir, _ = git.communicate()
35 if git.returncode:
36 return
Hayato Ito4eac52c72018-11-16 06:46:5337 libcxx_pretty_printers = os.path.join(str(src_dir).rstrip(), 'third_party',
Tom Anderson838a2b62018-07-30 17:58:2738 'libcxx-pretty-printers', 'src')
39 if not os.path.isdir(libcxx_pretty_printers):
40 return
41 sys.path.insert(1, libcxx_pretty_printers)
42 from libcxx.v1.printers import register_libcxx_printers
43 register_libcxx_printers(None)
44 libcxx_pretty_printers_loaded = True
45
46
Takuto Ikuta52fcb202018-05-24 21:19:2247def newobj_handler(event):
48 compile_dir = os.path.dirname(event.new_objfile.filename)
49 if not compile_dir:
50 return
51
52 # Add source path
Takuto Ikuta3ae0e03b2018-05-18 06:10:4053 gdb.execute("dir %s" % compile_dir)
54
Takuto Ikuta52fcb202018-05-24 21:19:2255 # Need to tell the location of .dwo files.
56 # https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
57 # https://crbug.com/603286#c35
58 add_debug_file_directory(compile_dir)
59
Tom Anderson838a2b62018-07-30 17:58:2760 load_libcxx_pretty_printers(compile_dir)
61
Takuto Ikuta52fcb202018-05-24 21:19:2262
63# Event hook for newly loaded objfiles.
64# https://sourceware.org/gdb/onlinedocs/gdb/Events-In-Python.html
65gdb.events.new_objfile.connect(newobj_handler)
66
Tom Anderson5a1ce542019-03-08 20:19:2767gdb.execute("set environment CHROMIUM_GDBINIT_SOURCED=1")
68
Takuto Ikuta3ae0e03b2018-05-18 06:10:4069end