| Tom Anderson | 71df887 | 2018-06-21 19:02:25 | [diff] [blame] | 1 | # This is gdbinit for source level debugging with -fdebug-compilation-dir |
| Tom Anderson | 838a2b6 | 2018-07-30 17:58:27 | [diff] [blame] | 2 | # compile option or when building with libc++. |
| Takuto Ikuta | 52fcb20 | 2018-05-24 21:19:22 | [diff] [blame] | 3 | |
| 4 | python |
| Takuto Ikuta | 3ae0e03b | 2018-05-18 06:10:40 | [diff] [blame] | 5 | |
| 6 | import os |
| Tom Anderson | 838a2b6 | 2018-07-30 17:58:27 | [diff] [blame] | 7 | import subprocess |
| 8 | import sys |
| Takuto Ikuta | 3ae0e03b | 2018-05-18 06:10:40 | [diff] [blame] | 9 | |
| Takuto Ikuta | 52fcb20 | 2018-05-24 21:19:22 | [diff] [blame] | 10 | def 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 | |
| 16 | def 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 Anderson | 838a2b6 | 2018-07-30 17:58:27 | [diff] [blame] | 25 | libcxx_pretty_printers_loaded = False |
| 26 | def 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 Ito | 4eac52c7 | 2018-11-16 06:46:53 | [diff] [blame] | 37 | libcxx_pretty_printers = os.path.join(str(src_dir).rstrip(), 'third_party', |
| Tom Anderson | 838a2b6 | 2018-07-30 17:58:27 | [diff] [blame] | 38 | '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 Ikuta | 52fcb20 | 2018-05-24 21:19:22 | [diff] [blame] | 47 | def 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 Ikuta | 3ae0e03b | 2018-05-18 06:10:40 | [diff] [blame] | 53 | gdb.execute("dir %s" % compile_dir) |
| 54 | |
| Takuto Ikuta | 52fcb20 | 2018-05-24 21:19:22 | [diff] [blame] | 55 | # 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 Anderson | 838a2b6 | 2018-07-30 17:58:27 | [diff] [blame] | 60 | load_libcxx_pretty_printers(compile_dir) |
| 61 | |
| Takuto Ikuta | 52fcb20 | 2018-05-24 21:19:22 | [diff] [blame] | 62 | |
| 63 | # Event hook for newly loaded objfiles. |
| 64 | # https://sourceware.org/gdb/onlinedocs/gdb/Events-In-Python.html |
| 65 | gdb.events.new_objfile.connect(newobj_handler) |
| 66 | |
| Tom Anderson | 5a1ce54 | 2019-03-08 20:19:27 | [diff] [blame^] | 67 | gdb.execute("set environment CHROMIUM_GDBINIT_SOURCED=1") |
| 68 | |
| Takuto Ikuta | 3ae0e03b | 2018-05-18 06:10:40 | [diff] [blame] | 69 | end |