rungdb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. import os
  3. import shlex
  4. import sys
  5. import signal
  6. import subprocess
  7. import common
  8. defaults = {
  9. 'after': '',
  10. 'before': '',
  11. 'no_continue': False,
  12. 'kgdb': False,
  13. 'no_lxsymbols': False,
  14. 'break_at': None,
  15. }
  16. def main(args, extra_args=None):
  17. '''
  18. :param args: argparse parse_argument() output. Must contain all the common options,
  19. but does not need GDB specific ones.
  20. :type args: argparse.Namespace
  21. :param extra_args: extra arguments to be added to args
  22. :type extra_args: Dict[str,Any]
  23. :return: GDB exit status
  24. :rtype: int
  25. '''
  26. global defaults
  27. args = common.resolve_args(defaults, args, extra_args)
  28. after = shlex.split(args.after)
  29. before = shlex.split(args.before)
  30. if args.no_lxsymbols:
  31. lx_symbols = []
  32. else:
  33. lx_symbols = ['-ex', 'lx-symbols ../kernel_modules-1.0/']
  34. if args.break_at is not None:
  35. break_at = ['-ex', 'break {}'.format(args.break_at)]
  36. else:
  37. break_at = []
  38. cmd = (
  39. [
  40. os.path.join(common.host_bin_dir,
  41. '{}-linux-gdb'.format(args.arch))
  42. ] +
  43. before +
  44. [
  45. '-q',
  46. '-ex', 'add-auto-load-safe-path {}'.format(common.linux_variant_dir),
  47. '-ex', 'file {}'.format(common.vmlinux),
  48. '-ex', 'target remote localhost:{}'.format(common.gdb_port),
  49. ]
  50. )
  51. if not args.kgdb:
  52. cmd.extend(break_at)
  53. if not args.no_continue:
  54. # ## lx-symbols
  55. #
  56. # ### lx-symbols after continue
  57. #
  58. # lx symbols must be run after continue.
  59. #
  60. # running it immediately after the connect on the bootloader leads to failure,
  61. # likely because kernel structure on which it depends are not yet available.
  62. #
  63. # With this setup, continue runs, and lx-symbols only runs when a break happens,
  64. # either by hitting the breakpoint, or by entering Ctrl + C.
  65. #
  66. # Sure, if the user sets a break on a raw address of the bootloader,
  67. # problems will still arise, but let's think about that some other time.
  68. #
  69. # ### lx-symbols autoload
  70. #
  71. # The lx-symbols commands gets loaded through the file vmlinux-gdb.py
  72. # which gets put on the kernel build root when python debugging scripts are enabled.
  73. cmd.extend(['-ex', 'continue'] + lx_symbols)
  74. cmd.extend(after)
  75. return common.run_cmd(cmd, cmd_file=os.path.join(common.run_dir, 'rungdb.sh'), cwd=common.linux_variant_dir)
  76. if __name__ == '__main__':
  77. parser = common.get_argparse(argparse_args={'description': 'Connect with GDB to an emulator to debug Linux itself'})
  78. parser.add_argument(
  79. '-A', '--after', default=defaults['after'],
  80. help='Pass extra arguments to GDB, to be appended after all other arguments'
  81. )
  82. parser.add_argument(
  83. '-b', '--before', default=defaults['before'],
  84. help='Pass extra arguments to GDB to be prepended before any of the arguments passed by this script'
  85. )
  86. parser.add_argument(
  87. '-C', '--no-continue', default=defaults['no_continue'], action='store_true',
  88. help="Don't run continue after connecting"
  89. )
  90. parser.add_argument(
  91. '-k', '--kgdb', default=defaults['kgdb'], action='store_true'
  92. )
  93. parser.add_argument(
  94. '-X', '--no-lxsymbols', default=defaults['no_lxsymbols'], action='store_true'
  95. )
  96. parser.add_argument(
  97. 'break_at', nargs='?',
  98. help='Extra options to append at the end of the emulator command line'
  99. )
  100. args = common.setup(parser)
  101. sys.exit(main(args))