proc.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. from linux import constants
  15. from linux import utils
  16. from linux import tasks
  17. from linux import lists
  18. class LxCmdLine(gdb.Command):
  19. """ Report the Linux Commandline used in the current kernel.
  20. Equivalent to cat /proc/cmdline on a running target"""
  21. def __init__(self):
  22. super(LxCmdLine, self).__init__("lx-cmdline", gdb.COMMAND_DATA)
  23. def invoke(self, arg, from_tty):
  24. gdb.write(gdb.parse_and_eval("saved_command_line").string() + "\n")
  25. LxCmdLine()
  26. class LxVersion(gdb.Command):
  27. """ Report the Linux Version of the current kernel.
  28. Equivalent to cat /proc/version on a running target"""
  29. def __init__(self):
  30. super(LxVersion, self).__init__("lx-version", gdb.COMMAND_DATA)
  31. def invoke(self, arg, from_tty):
  32. # linux_banner should contain a newline
  33. gdb.write(gdb.parse_and_eval("linux_banner").string())
  34. LxVersion()
  35. # Resource Structure Printers
  36. # /proc/iomem
  37. # /proc/ioports
  38. def get_resources(resource, depth):
  39. while resource:
  40. yield resource, depth
  41. child = resource['child']
  42. if child:
  43. for res, deep in get_resources(child, depth + 1):
  44. yield res, deep
  45. resource = resource['sibling']
  46. def show_lx_resources(resource_str):
  47. resource = gdb.parse_and_eval(resource_str)
  48. width = 4 if resource['end'] < 0x10000 else 8
  49. # Iterate straight to the first child
  50. for res, depth in get_resources(resource['child'], 0):
  51. start = int(res['start'])
  52. end = int(res['end'])
  53. gdb.write(" " * depth * 2 +
  54. "{0:0{1}x}-".format(start, width) +
  55. "{0:0{1}x} : ".format(end, width) +
  56. res['name'].string() + "\n")
  57. class LxIOMem(gdb.Command):
  58. """Identify the IO memory resource locations defined by the kernel
  59. Equivalent to cat /proc/iomem on a running target"""
  60. def __init__(self):
  61. super(LxIOMem, self).__init__("lx-iomem", gdb.COMMAND_DATA)
  62. def invoke(self, arg, from_tty):
  63. return show_lx_resources("iomem_resource")
  64. LxIOMem()
  65. class LxIOPorts(gdb.Command):
  66. """Identify the IO port resource locations defined by the kernel
  67. Equivalent to cat /proc/ioports on a running target"""
  68. def __init__(self):
  69. super(LxIOPorts, self).__init__("lx-ioports", gdb.COMMAND_DATA)
  70. def invoke(self, arg, from_tty):
  71. return show_lx_resources("ioport_resource")
  72. LxIOPorts()
  73. # Mount namespace viewer
  74. # /proc/mounts
  75. def info_opts(lst, opt):
  76. opts = ""
  77. for key, string in lst.items():
  78. if opt & key:
  79. opts += string
  80. return opts
  81. FS_INFO = {constants.LX_MS_SYNCHRONOUS: ",sync",
  82. constants.LX_MS_MANDLOCK: ",mand",
  83. constants.LX_MS_DIRSYNC: ",dirsync",
  84. constants.LX_MS_NOATIME: ",noatime",
  85. constants.LX_MS_NODIRATIME: ",nodiratime"}
  86. MNT_INFO = {constants.LX_MNT_NOSUID: ",nosuid",
  87. constants.LX_MNT_NODEV: ",nodev",
  88. constants.LX_MNT_NOEXEC: ",noexec",
  89. constants.LX_MNT_NOATIME: ",noatime",
  90. constants.LX_MNT_NODIRATIME: ",nodiratime",
  91. constants.LX_MNT_RELATIME: ",relatime"}
  92. mount_type = utils.CachedType("struct mount")
  93. mount_ptr_type = mount_type.get_type().pointer()
  94. class LxMounts(gdb.Command):
  95. """Report the VFS mounts of the current process namespace.
  96. Equivalent to cat /proc/mounts on a running target
  97. An integer value can be supplied to display the mount
  98. values of that process namespace"""
  99. def __init__(self):
  100. super(LxMounts, self).__init__("lx-mounts", gdb.COMMAND_DATA)
  101. # Equivalent to proc_namespace.c:show_vfsmnt
  102. # However, that has the ability to call into s_op functions
  103. # whereas we cannot and must make do with the information we can obtain.
  104. def invoke(self, arg, from_tty):
  105. argv = gdb.string_to_argv(arg)
  106. if len(argv) >= 1:
  107. try:
  108. pid = int(argv[0])
  109. except:
  110. raise gdb.GdbError("Provide a PID as integer value")
  111. else:
  112. pid = 1
  113. task = tasks.get_task_by_pid(pid)
  114. if not task:
  115. raise gdb.GdbError("Couldn't find a process with PID {}"
  116. .format(pid))
  117. namespace = task['nsproxy']['mnt_ns']
  118. if not namespace:
  119. raise gdb.GdbError("No namespace for current process")
  120. for vfs in lists.list_for_each_entry(namespace['list'],
  121. mount_ptr_type, "mnt_list"):
  122. devname = vfs['mnt_devname'].string()
  123. devname = devname if devname else "none"
  124. pathname = ""
  125. parent = vfs
  126. while True:
  127. mntpoint = parent['mnt_mountpoint']
  128. pathname = utils.dentry_name(mntpoint) + pathname
  129. if (parent == parent['mnt_parent']):
  130. break
  131. parent = parent['mnt_parent']
  132. if (pathname == ""):
  133. pathname = "/"
  134. superblock = vfs['mnt']['mnt_sb']
  135. fstype = superblock['s_type']['name'].string()
  136. s_flags = int(superblock['s_flags'])
  137. m_flags = int(vfs['mnt']['mnt_flags'])
  138. rd = "ro" if (s_flags & constants.LX_MS_RDONLY) else "rw"
  139. gdb.write(
  140. "{} {} {} {}{}{} 0 0\n"
  141. .format(devname,
  142. pathname,
  143. fstype,
  144. rd,
  145. info_opts(FS_INFO, s_flags),
  146. info_opts(MNT_INFO, m_flags)))
  147. LxMounts()