tasks.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. def task_lists():
  17. global task_type
  18. task_ptr_type = task_type.get_type().pointer()
  19. init_task = gdb.parse_and_eval("init_task").address
  20. t = g = init_task
  21. while True:
  22. while True:
  23. yield t
  24. t = utils.container_of(t['thread_group']['next'],
  25. task_ptr_type, "thread_group")
  26. if t == g:
  27. break
  28. t = g = utils.container_of(g['tasks']['next'],
  29. task_ptr_type, "tasks")
  30. if t == init_task:
  31. return
  32. def get_task_by_pid(pid):
  33. for task in task_lists():
  34. if int(task['pid']) == pid:
  35. return task
  36. return None
  37. class LxTaskByPidFunc(gdb.Function):
  38. """Find Linux task by PID and return the task_struct variable.
  39. $lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
  40. return that task_struct variable which PID matches."""
  41. def __init__(self):
  42. super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
  43. def invoke(self, pid):
  44. task = get_task_by_pid(pid)
  45. if task:
  46. return task.dereference()
  47. else:
  48. raise gdb.GdbError("No task of PID " + str(pid))
  49. LxTaskByPidFunc()
  50. class LxPs(gdb.Command):
  51. """Dump Linux tasks."""
  52. def __init__(self):
  53. super(LxPs, self).__init__("lx-ps", gdb.COMMAND_DATA)
  54. def invoke(self, arg, from_tty):
  55. for task in task_lists():
  56. gdb.write("{address} {pid} {comm}\n".format(
  57. address=task,
  58. pid=task["pid"],
  59. comm=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. global thread_info_type
  65. thread_info_ptr_type = thread_info_type.get_type().pointer()
  66. if utils.is_target_arch("ia64"):
  67. global ia64_task_size
  68. if ia64_task_size is None:
  69. ia64_task_size = gdb.parse_and_eval("sizeof(struct task_struct)")
  70. thread_info_addr = task.address + ia64_task_size
  71. thread_info = thread_info_addr.cast(thread_info_ptr_type)
  72. else:
  73. thread_info = task['stack'].cast(thread_info_ptr_type)
  74. return thread_info.dereference()
  75. class LxThreadInfoFunc (gdb.Function):
  76. """Calculate Linux thread_info from task variable.
  77. $lx_thread_info(TASK): Given TASK, return the corresponding thread_info
  78. variable."""
  79. def __init__(self):
  80. super(LxThreadInfoFunc, self).__init__("lx_thread_info")
  81. def invoke(self, task):
  82. return get_thread_info(task)
  83. LxThreadInfoFunc()