2 # gdb helper commands and functions for Linux kernel debugging
6 # Copyright (c) Siemens AG, 2011-2013
9 # Jan Kiszka <jan.kiszka@siemens.com>
11 # This work is licensed under the terms of the GNU GPL version 2.
16 from linux
import utils
19 task_type
= utils
.CachedType("struct task_struct")
23 task_ptr_type
= task_type
.get_type().pointer()
24 init_task
= gdb
.parse_and_eval("init_task").address
31 t
= utils
.container_of(t
['thread_group']['next'],
32 task_ptr_type
, "thread_group")
36 t
= g
= utils
.container_of(g
['tasks']['next'],
37 task_ptr_type
, "tasks")
42 def get_task_by_pid(pid
):
43 for task
in task_lists():
44 if int(task
['pid']) == pid
:
49 class LxTaskByPidFunc(gdb
.Function
):
50 """Find Linux task by PID and return the task_struct variable.
52 $lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
53 return that task_struct variable which PID matches."""
56 super(LxTaskByPidFunc
, self
).__init
__("lx_task_by_pid")
58 def invoke(self
, pid
):
59 task
= get_task_by_pid(pid
)
61 return task
.dereference()
63 raise gdb
.GdbError("No task of PID " + str(pid
))
69 class LxPs(gdb
.Command
):
70 """Dump Linux tasks."""
73 super(LxPs
, self
).__init
__("lx-ps", gdb
.COMMAND_DATA
)
75 def invoke(self
, arg
, from_tty
):
76 for task
in task_lists():
77 gdb
.write("{address} {pid} {comm}\n".format(
80 comm
=task
["comm"].string()))
85 thread_info_type
= utils
.CachedType("struct thread_info")
90 def get_thread_info(task
):
91 thread_info_ptr_type
= thread_info_type
.get_type().pointer()
92 if utils
.is_target_arch("ia64"):
94 if ia64_task_size
is None:
95 ia64_task_size
= gdb
.parse_and_eval("sizeof(struct task_struct)")
96 thread_info_addr
= task
.address
+ ia64_task_size
97 thread_info
= thread_info_addr
.cast(thread_info_ptr_type
)
99 if task
.type.fields()[0].type == thread_info_type
.get_type():
100 return task
['thread_info']
101 thread_info
= task
['stack'].cast(thread_info_ptr_type
)
102 return thread_info
.dereference()
105 class LxThreadInfoFunc (gdb
.Function
):
106 """Calculate Linux thread_info from task variable.
108 $lx_thread_info(TASK): Given TASK, return the corresponding thread_info
112 super(LxThreadInfoFunc
, self
).__init
__("lx_thread_info")
114 def invoke(self
, task
):
115 return get_thread_info(task
)
121 class LxThreadInfoByPidFunc (gdb
.Function
):
122 """Calculate Linux thread_info from task variable found by pid
124 $lx_thread_info_by_pid(PID): Given PID, return the corresponding thread_info
128 super(LxThreadInfoByPidFunc
, self
).__init
__("lx_thread_info_by_pid")
130 def invoke(self
, pid
):
131 task
= get_task_by_pid(pid
)
133 return get_thread_info(task
.dereference())
135 raise gdb
.GdbError("No task of PID " + str(pid
))
137 LxThreadInfoByPidFunc()