tasks.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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
  15. task_type = utils.CachedType("struct task_struct")
  16. class TaskList:
  17. def __init__(self):
  18. global task_type
  19. self.task_ptr_type = task_type.get_type().pointer()
  20. self.init_task = gdb.parse_and_eval("init_task")
  21. self.curr_group = self.init_task.address
  22. self.curr_task = None
  23. def __iter__(self):
  24. return self
  25. def next(self):
  26. t = self.curr_task
  27. if not t or t == self.curr_group:
  28. self.curr_group = \
  29. utils.container_of(self.curr_group['tasks']['next'],
  30. self.task_ptr_type, "tasks")
  31. if self.curr_group == self.init_task.address:
  32. raise StopIteration
  33. t = self.curr_task = self.curr_group
  34. else:
  35. self.curr_task = \
  36. utils.container_of(t['thread_group']['next'],
  37. self.task_ptr_type, "thread_group")
  38. return t
  39. def get_task_by_pid(pid):
  40. for task in TaskList():
  41. if int(task['pid']) == pid:
  42. return task
  43. return None
  44. class LxTaskByPidFunc(gdb.Function):
  45. """Find Linux task by PID and return the task_struct variable.
  46. $lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
  47. return that task_struct variable which PID matches."""
  48. def __init__(self):
  49. super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
  50. def invoke(self, pid):
  51. task = get_task_by_pid(pid)
  52. if task:
  53. return task.dereference()
  54. else:
  55. raise gdb.GdbError("No task of PID " + str(pid))
  56. LxTaskByPidFunc()