tasks.py 2.8 KB

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