tasks.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #
  2. # gdb helper commands and functions for Linux kernel debugging
  3. #
  4. # task & thread tools
  5. #
  6. # Copyright (c) Siemens AG, 2011-2013
  7. #
  8. # Authors:
  9. # Jan Kiszka <[email protected]>
  10. #
  11. # This work is licensed under the terms of the GNU GPL version 2.
  12. #
  13. import gdb
  14. from linux import utils
  15. task_type = utils.CachedType("struct task_struct")
  16. def task_lists():
  17. task_ptr_type = task_type.get_type().pointer()
  18. init_task = gdb.parse_and_eval("init_task").address
  19. t = g = init_task
  20. while True:
  21. while True:
  22. yield t
  23. t = utils.container_of(t['thread_group']['next'],
  24. task_ptr_type, "thread_group")
  25. if t == g:
  26. break
  27. t = g = utils.container_of(g['tasks']['next'],
  28. task_ptr_type, "tasks")
  29. if t == init_task:
  30. return
  31. def get_task_by_pid(pid):
  32. for task in task_lists():
  33. if int(task['pid']) == pid:
  34. return task
  35. return None
  36. class LxTaskByPidFunc(gdb.Function):
  37. """Find Linux task by PID and return the task_struct variable.
  38. $lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
  39. return that task_struct variable which PID matches."""
  40. def __init__(self):
  41. super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
  42. def invoke(self, pid):
  43. task = get_task_by_pid(pid)
  44. if task:
  45. return task.dereference()
  46. else:
  47. raise gdb.GdbError("No task of PID " + str(pid))
  48. LxTaskByPidFunc()
  49. class LxPs(gdb.Command):
  50. """Dump Linux tasks."""
  51. def __init__(self):
  52. super(LxPs, self).__init__("lx-ps", gdb.COMMAND_DATA)
  53. def invoke(self, arg, from_tty):
  54. gdb.write("{:>10} {:>12} {:>7}\n".format("TASK", "PID", "COMM"))
  55. for task in task_lists():
  56. gdb.write("{} {:^5} {}\n".format(
  57. task.format_string().split()[0],
  58. task["pid"].format_string(),
  59. task["comm"].string()))
  60. LxPs()
  61. thread_info_type = utils.CachedType("struct thread_info")
  62. ia64_task_size = None
  63. def get_thread_info(task):
  64. thread_info_ptr_type = thread_info_type.get_type().pointer()
  65. if utils.is_target_arch("ia64"):
  66. global ia64_task_size
  67. if ia64_task_size is None:
  68. ia64_task_size = gdb.parse_and_eval("sizeof(struct task_struct)")
  69. thread_info_addr = task.address + ia64_task_size
  70. thread_info = thread_info_addr.cast(thread_info_ptr_type)
  71. else:
  72. if task.type.fields()[0].type == thread_info_type.get_type():
  73. return task['thread_info']
  74. thread_info = task['stack'].cast(thread_info_ptr_type)
  75. return thread_info.dereference()
  76. class LxThreadInfoFunc (gdb.Function):
  77. """Calculate Linux thread_info from task variable.
  78. $lx_thread_info(TASK): Given TASK, return the corresponding thread_info
  79. variable."""
  80. def __init__(self):
  81. super(LxThreadInfoFunc, self).__init__("lx_thread_info")
  82. def invoke(self, task):
  83. return get_thread_info(task)
  84. LxThreadInfoFunc()
  85. class LxThreadInfoByPidFunc (gdb.Function):
  86. """Calculate Linux thread_info from task variable found by pid
  87. $lx_thread_info_by_pid(PID): Given PID, return the corresponding thread_info
  88. variable."""
  89. def __init__(self):
  90. super(LxThreadInfoByPidFunc, self).__init__("lx_thread_info_by_pid")
  91. def invoke(self, pid):
  92. task = get_task_by_pid(pid)
  93. if task:
  94. return get_thread_info(task.dereference())
  95. else:
  96. raise gdb.GdbError("No task of PID " + str(pid))
  97. LxThreadInfoByPidFunc()