proc.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # gdb helper commands and functions for Linux kernel debugging
  3. #
  4. # Kernel proc information reader
  5. #
  6. # Copyright (c) 2016 Linaro Ltd
  7. #
  8. # Authors:
  9. # Kieran Bingham <kieran.bingham@linaro.org>
  10. #
  11. # This work is licensed under the terms of the GNU GPL version 2.
  12. #
  13. import gdb
  14. class LxCmdLine(gdb.Command):
  15. """ Report the Linux Commandline used in the current kernel.
  16. Equivalent to cat /proc/cmdline on a running target"""
  17. def __init__(self):
  18. super(LxCmdLine, self).__init__("lx-cmdline", gdb.COMMAND_DATA)
  19. def invoke(self, arg, from_tty):
  20. gdb.write(gdb.parse_and_eval("saved_command_line").string() + "\n")
  21. LxCmdLine()
  22. class LxVersion(gdb.Command):
  23. """ Report the Linux Version of the current kernel.
  24. Equivalent to cat /proc/version on a running target"""
  25. def __init__(self):
  26. super(LxVersion, self).__init__("lx-version", gdb.COMMAND_DATA)
  27. def invoke(self, arg, from_tty):
  28. # linux_banner should contain a newline
  29. gdb.write(gdb.parse_and_eval("linux_banner").string())
  30. LxVersion()
  31. # Resource Structure Printers
  32. # /proc/iomem
  33. # /proc/ioports
  34. def get_resources(resource, depth):
  35. while resource:
  36. yield resource, depth
  37. child = resource['child']
  38. if child:
  39. for res, deep in get_resources(child, depth + 1):
  40. yield res, deep
  41. resource = resource['sibling']
  42. def show_lx_resources(resource_str):
  43. resource = gdb.parse_and_eval(resource_str)
  44. width = 4 if resource['end'] < 0x10000 else 8
  45. # Iterate straight to the first child
  46. for res, depth in get_resources(resource['child'], 0):
  47. start = int(res['start'])
  48. end = int(res['end'])
  49. gdb.write(" " * depth * 2 +
  50. "{0:0{1}x}-".format(start, width) +
  51. "{0:0{1}x} : ".format(end, width) +
  52. res['name'].string() + "\n")
  53. class LxIOMem(gdb.Command):
  54. """Identify the IO memory resource locations defined by the kernel
  55. Equivalent to cat /proc/iomem on a running target"""
  56. def __init__(self):
  57. super(LxIOMem, self).__init__("lx-iomem", gdb.COMMAND_DATA)
  58. def invoke(self, arg, from_tty):
  59. return show_lx_resources("iomem_resource")
  60. LxIOMem()
  61. class LxIOPorts(gdb.Command):
  62. """Identify the IO port resource locations defined by the kernel
  63. Equivalent to cat /proc/ioports on a running target"""
  64. def __init__(self):
  65. super(LxIOPorts, self).__init__("lx-ioports", gdb.COMMAND_DATA)
  66. def invoke(self, arg, from_tty):
  67. return show_lx_resources("ioport_resource")
  68. LxIOPorts()