# This is gdbinit for source level debugging with -fdebug-compilation-dir
# compile option or when building with libc++.

python

import os
import subprocess
import sys

def get_current_debug_file_directories():
  dir = gdb.execute("show debug-file-directory", to_string=True)
  dir = dir[len('The directory where separate debug symbols are searched for is "'):-len('".')-1]
  return set(dir.split(":"))


def add_debug_file_directory(dir):
  # gdb has no function to add debug-file-directory, simulates that by using
  # `show debug-file-directory` and `set debug-file-directory <directories>`.
  current_dirs = get_current_debug_file_directories()
  current_dirs.add(dir)
  gdb.execute("set debug-file-directory %s" % ":".join(current_dirs),
              to_string=True)


libcxx_pretty_printers_loaded = False
def load_libcxx_pretty_printers(compile_dir):
  global libcxx_pretty_printers_loaded
  if libcxx_pretty_printers_loaded:
    return
  git = subprocess.Popen(
      ['git', '-C', compile_dir, 'rev-parse', '--show-toplevel'],
      stdout=subprocess.PIPE,
      stderr=subprocess.PIPE)
  src_dir, _ = git.communicate()
  if git.returncode:
    return
  libcxx_pretty_printers = os.path.join(str(src_dir).rstrip(), 'third_party',
                                        'libcxx-pretty-printers', 'src')
  if not os.path.isdir(libcxx_pretty_printers):
    return
  sys.path.insert(1, libcxx_pretty_printers)
  from libcxx.v1.printers import register_libcxx_printers
  register_libcxx_printers(None)
  libcxx_pretty_printers_loaded = True


def newobj_handler(event):
  compile_dir = os.path.dirname(event.new_objfile.filename)
  if not compile_dir:
    return

  # Add source path
  gdb.execute("dir %s" % compile_dir)

  # Need to tell the location of .dwo files.
  # https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
  # https://crbug.com/603286#c35
  add_debug_file_directory(compile_dir)

  load_libcxx_pretty_printers(compile_dir)


# Event hook for newly loaded objfiles.
# https://sourceware.org/gdb/onlinedocs/gdb/Events-In-Python.html
gdb.events.new_objfile.connect(newobj_handler)

end
