tasks.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <jan.kiszka@siemens.com>
  10. #
  11. # This work is licensed under the terms of the GNU GPL version 2.
  12. #
  13. import gdb
  14. from linux import utils, lists
  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 = init_task
  20. while True:
  21. thread_head = t['signal']['thread_head']
  22. for thread in lists.list_for_each_entry(thread_head, task_ptr_type, 'thread_node'):
  23. yield thread
  24. t = utils.container_of(t['tasks']['next'],
  25. task_ptr_type, "tasks")
  26. if t == init_task:
  27. return
  28. def get_task_by_pid(pid):
  29. for task in task_lists():
  30. if int(task['pid']) == pid:
  31. return task
  32. return None
  33. class LxTaskByPidFunc(gdb.Function):
  34. """Find Linux task by PID and return the task_struct variable.
  35. $lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
  36. return that task_struct variable which PID matches."""
  37. def __init__(self):
  38. super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
  39. def invoke(self, pid):
  40. task = get_task_by_pid(pid)
  41. if task:
  42. return task.dereference()
  43. else:
  44. raise gdb.GdbError("No task of PID " + str(pid))
  45. LxTaskByPidFunc()
  46. class LxPs(gdb.Command):
  47. """Dump Linux tasks."""
  48. def __init__(self):
  49. super(LxPs, self).__init__("lx-ps", gdb.COMMAND_DATA)
  50. def invoke(self, arg, from_tty):
  51. gdb.write("{:>10} {:>12} {:>7}\n".format("TASK", "PID", "COMM"))
  52. for task in task_lists():
  53. gdb.write("{} {:^5} {}\n".format(
  54. task.format_string().split()[0],
  55. task["pid"].format_string(),
  56. task["comm"].string()))
  57. LxPs()
  58. thread_info_type = utils.CachedType("struct thread_info")
  59. def get_thread_info(task):
  60. thread_info_ptr_type = thread_info_type.get_type().pointer()
  61. if task_type.get_type().fields()[0].type == thread_info_type.get_type():
  62. return task['thread_info']
  63. thread_info = task['stack'].cast(thread_info_ptr_type)
  64. return thread_info.dereference()
  65. class LxThreadInfoFunc (gdb.Function):
  66. """Calculate Linux thread_info from task variable.
  67. $lx_thread_info(TASK): Given TASK, return the corresponding thread_info
  68. variable."""
  69. def __init__(self):
  70. super(LxThreadInfoFunc, self).__init__("lx_thread_info")
  71. def invoke(self, task):
  72. return get_thread_info(task)
  73. LxThreadInfoFunc()
  74. class LxThreadInfoByPidFunc (gdb.Function):
  75. """Calculate Linux thread_info from task variable found by pid
  76. $lx_thread_info_by_pid(PID): Given PID, return the corresponding thread_info
  77. variable."""
  78. def __init__(self):
  79. super(LxThreadInfoByPidFunc, self).__init__("lx_thread_info_by_pid")
  80. def invoke(self, pid):
  81. task = get_task_by_pid(pid)
  82. if task:
  83. return get_thread_info(task.dereference())
  84. else:
  85. raise gdb.GdbError("No task of PID " + str(pid))
  86. LxThreadInfoByPidFunc()