blob: 0008e75f1c4fb5b28d865d5264183e46b2c336d4 [file] [log] [blame]
Jan Kiszka7704d582015-02-17 13:47:07 -08001#
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
14import gdb
15
16from linux import utils
17
18
19task_type = utils.CachedType("struct task_struct")
20
21
22class TaskList:
23 def __init__(self):
24 global task_type
25 self.task_ptr_type = task_type.get_type().pointer()
26 self.init_task = gdb.parse_and_eval("init_task")
27 self.curr_group = self.init_task.address
28 self.curr_task = None
29
30 def __iter__(self):
31 return self
32
Pantelis Koukousoulas276d97d2015-02-17 13:47:35 -080033 def __next__(self):
Jan Kiszka7704d582015-02-17 13:47:07 -080034 t = self.curr_task
35 if not t or t == self.curr_group:
36 self.curr_group = \
37 utils.container_of(self.curr_group['tasks']['next'],
38 self.task_ptr_type, "tasks")
39 if self.curr_group == self.init_task.address:
40 raise StopIteration
41 t = self.curr_task = self.curr_group
42 else:
43 self.curr_task = \
44 utils.container_of(t['thread_group']['next'],
45 self.task_ptr_type, "thread_group")
46 return t
Jan Kiszka47528712015-02-17 13:47:10 -080047
Pantelis Koukousoulas276d97d2015-02-17 13:47:35 -080048 def next(self):
49 return self.__next__()
Jan Kiszka47528712015-02-17 13:47:10 -080050
51def get_task_by_pid(pid):
52 for task in TaskList():
53 if int(task['pid']) == pid:
54 return task
55 return None
56
57
58class LxTaskByPidFunc(gdb.Function):
59 """Find Linux task by PID and return the task_struct variable.
60
61$lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
62return that task_struct variable which PID matches."""
63
64 def __init__(self):
65 super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
66
67 def invoke(self, pid):
68 task = get_task_by_pid(pid)
69 if task:
70 return task.dereference()
71 else:
72 raise gdb.GdbError("No task of PID " + str(pid))
73
74
75LxTaskByPidFunc()
Jan Kiszkacf7492e2015-02-17 13:47:15 -080076
77
78thread_info_type = utils.CachedType("struct thread_info")
79
80ia64_task_size = None
81
82
83def get_thread_info(task):
84 global thread_info_type
85 thread_info_ptr_type = thread_info_type.get_type().pointer()
86 if utils.is_target_arch("ia64"):
87 global ia64_task_size
88 if ia64_task_size is None:
89 ia64_task_size = gdb.parse_and_eval("sizeof(struct task_struct)")
90 thread_info_addr = task.address + ia64_task_size
91 thread_info = thread_info_addr.cast(thread_info_ptr_type)
92 else:
93 thread_info = task['stack'].cast(thread_info_ptr_type)
94 return thread_info.dereference()
95
96
97class LxThreadInfoFunc (gdb.Function):
98 """Calculate Linux thread_info from task variable.
99
100$lx_thread_info(TASK): Given TASK, return the corresponding thread_info
101variable."""
102
103 def __init__(self):
104 super(LxThreadInfoFunc, self).__init__("lx_thread_info")
105
106 def invoke(self, task):
107 return get_thread_info(task)
108
109
110LxThreadInfoFunc()