gdb.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import logging
  4. import yatest.common
  5. logger = logging.getLogger(__name__)
  6. SHORT_DUMP_COMMAND = "{gdb} {executable} {core_file} --eval-command='backtrace full' --batch -q"
  7. LONG_DUMP_COMMAND = "{gdb} {executable} {core_file} --eval-command='thread apply all bt full' --batch -q"
  8. def get_gdb():
  9. return yatest.common.gdb_path()
  10. def run_gdb_command(command, stdout_file, stderr_file):
  11. logger.debug("Running gdb command %s" % command)
  12. with open(stdout_file, "w") as out, open(stderr_file, "w") as err:
  13. yatest.common.process.execute(
  14. command,
  15. check_exit_code=True,
  16. wait=True,
  17. shell=True,
  18. stdout=out,
  19. stderr=err
  20. )
  21. def dump_traceback(executable, core_file, output_file):
  22. """
  23. Dumps traceback if its possible
  24. :param executable: binary for gdb
  25. :param core_file: core file for gdb
  26. :param output_file: file to dump traceback to, also dump full traceback to <output_file + ".full">
  27. :return: string tuple (short_backtrace, full_backtrace)
  28. """
  29. try:
  30. gdb = get_gdb()
  31. short_dump_command = SHORT_DUMP_COMMAND.format(gdb=gdb, executable=executable, core_file=core_file)
  32. long_dump_command = LONG_DUMP_COMMAND.format(gdb=gdb, executable=executable, core_file=core_file)
  33. run_gdb_command(short_dump_command, output_file, output_file + '.err')
  34. output_file_full = output_file + ".full"
  35. output_file_full_err = output_file_full + '.err'
  36. run_gdb_command(long_dump_command, output_file_full, output_file_full_err)
  37. except Exception:
  38. logger.exception("Failed to print trace")
  39. return '', ''
  40. short_backtrace = ''
  41. full_backtrace = ''
  42. with open(output_file) as o, open(output_file_full) as e:
  43. short_backtrace = o.read()
  44. full_backtrace = e.read()
  45. return short_backtrace, full_backtrace