tasks.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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()
  57. thread_info_type = utils.CachedType("struct thread_info")
  58. ia64_task_size = None
  59. def get_thread_info(task):
  60. global thread_info_type
  61. thread_info_ptr_type = thread_info_type.get_type().pointer()
  62. if utils.is_target_arch("ia64"):
  63. global ia64_task_size
  64. if ia64_task_size is None:
  65. ia64_task_size = gdb.parse_and_eval("sizeof(struct task_struct)")
  66. thread_info_addr = task.address + ia64_task_size
  67. thread_info = thread_info_addr.cast(thread_info_ptr_type)
  68. else:
  69. thread_info = task['stack'].cast(thread_info_ptr_type)
  70. return thread_info.dereference()
  71. class LxThreadInfoFunc (gdb.Function):
  72. """Calculate Linux thread_info from task variable.
  73. $lx_thread_info(TASK): Given TASK, return the corresponding thread_info
  74. variable."""
  75. def __init__(self):
  76. super(LxThreadInfoFunc, self).__init__("lx_thread_info")
  77. def invoke(self, task):
  78. return get_thread_info(task)
  79. LxThreadInfoFunc()