gdbinit 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright 2018 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. python
  5. import os
  6. import subprocess
  7. import sys
  8. compile_dirs = set()
  9. src_dir = None
  10. def get_current_debug_file_directories():
  11. dir = gdb.execute("show debug-file-directory", to_string=True)
  12. dir = dir[
  13. len('The directory where separate debug symbols are searched for is "'
  14. ):-len('".') - 1]
  15. return set(dir.split(":"))
  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(
  22. "set debug-file-directory %s" % ":".join(current_dirs), to_string=True)
  23. def load_libcxx_pretty_printers(src_dir):
  24. libcxx_pretty_printers = os.path.join(src_dir, 'buildtools', 'third_party',
  25. 'libc++', 'trunk', 'utils', 'gdb',
  26. 'libcxx')
  27. if not os.path.isdir(libcxx_pretty_printers):
  28. return
  29. sys.path.insert(1, libcxx_pretty_printers)
  30. from printers import register_libcxx_printer_loader
  31. register_libcxx_printer_loader()
  32. def load_gdb_chrome(src_dir):
  33. tools_gdb = os.path.join(src_dir, 'tools', 'gdb')
  34. sys.path.insert(1, tools_gdb)
  35. import gdb_chrome
  36. gdb.execute('source %s' % os.path.join(tools_gdb, 'viewg.gdb'))
  37. def set_src_dir(compile_dir):
  38. global src_dir
  39. git = subprocess.Popen(
  40. ['git', '-C', compile_dir, 'rev-parse', '--show-toplevel'],
  41. stdout=subprocess.PIPE,
  42. stderr=subprocess.PIPE)
  43. src_dir, _ = git.communicate()
  44. if git.returncode:
  45. return
  46. if isinstance(src_dir, str):
  47. src_dir = src_dir.rstrip()
  48. else:
  49. src_dir = src_dir.decode('utf-8').rstrip()
  50. load_libcxx_pretty_printers(src_dir)
  51. load_gdb_chrome(src_dir)
  52. def newobj_handler(event):
  53. global compile_dirs
  54. compile_dir = os.path.dirname(event.new_objfile.filename)
  55. if not compile_dir:
  56. return
  57. if compile_dir in compile_dirs:
  58. return
  59. compile_dirs.add(compile_dir)
  60. # Add source path
  61. gdb.execute("dir %s" % compile_dir)
  62. # Need to tell the location of .dwo files.
  63. # https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
  64. # https://crbug.com/603286#c35
  65. add_debug_file_directory(compile_dir)
  66. global src_dir
  67. if not src_dir:
  68. set_src_dir(compile_dir)
  69. # Event hook for newly loaded objfiles.
  70. # https://sourceware.org/gdb/onlinedocs/gdb/Events-In-Python.html
  71. gdb.events.new_objfile.connect(newobj_handler)
  72. gdb.execute("set environment CHROMIUM_GDBINIT_SOURCED=1")
  73. # GDB 9.1 adds a setting, disabled by default, to do multithreaded symbol
  74. # loading. This is a major speed up for Chrome, so turn it on here. We
  75. # use try..except to avoid displaying errors in earlier GDB versions.
  76. try:
  77. # unlimited means "number of cores"
  78. gdb.execute("maint set worker-threads unlimited")
  79. except:
  80. pass
  81. end