blob: 862a4ae24d4996caec315ddbe10a859456a04cf4 [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
Thiébaud Weksteen6ad18b72015-06-30 14:58:18 -070021
Daniel Wagner54e22892015-02-17 13:47:41 -080022def task_lists():
Daniel Wagner54e22892015-02-17 13:47:41 -080023 task_ptr_type = task_type.get_type().pointer()
24 init_task = gdb.parse_and_eval("init_task").address
25 t = g = init_task
Jan Kiszka7704d582015-02-17 13:47:07 -080026
Daniel Wagner54e22892015-02-17 13:47:41 -080027 while True:
28 while True:
29 yield t
Jan Kiszka7704d582015-02-17 13:47:07 -080030
Daniel Wagner54e22892015-02-17 13:47:41 -080031 t = utils.container_of(t['thread_group']['next'],
32 task_ptr_type, "thread_group")
33 if t == g:
34 break
Jan Kiszka7704d582015-02-17 13:47:07 -080035
Daniel Wagner54e22892015-02-17 13:47:41 -080036 t = g = utils.container_of(g['tasks']['next'],
37 task_ptr_type, "tasks")
38 if t == init_task:
39 return
Jan Kiszka47528712015-02-17 13:47:10 -080040
Thiébaud Weksteen6ad18b72015-06-30 14:58:18 -070041
Jan Kiszka47528712015-02-17 13:47:10 -080042def get_task_by_pid(pid):
Daniel Wagner54e22892015-02-17 13:47:41 -080043 for task in task_lists():
Jan Kiszka47528712015-02-17 13:47:10 -080044 if int(task['pid']) == pid:
45 return task
46 return None
47
48
49class LxTaskByPidFunc(gdb.Function):
50 """Find Linux task by PID and return the task_struct variable.
51
52$lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
53return that task_struct variable which PID matches."""
54
55 def __init__(self):
56 super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
57
58 def invoke(self, pid):
59 task = get_task_by_pid(pid)
60 if task:
61 return task.dereference()
62 else:
63 raise gdb.GdbError("No task of PID " + str(pid))
64
65
66LxTaskByPidFunc()
Jan Kiszkacf7492e2015-02-17 13:47:15 -080067
68
Thiébaud Weksteena9308502015-06-30 14:58:21 -070069class LxPs(gdb.Command):
70 """Dump Linux tasks."""
71
72 def __init__(self):
73 super(LxPs, self).__init__("lx-ps", gdb.COMMAND_DATA)
74
75 def invoke(self, arg, from_tty):
76 for task in task_lists():
77 gdb.write("{address} {pid} {comm}\n".format(
78 address=task,
79 pid=task["pid"],
80 comm=task["comm"].string()))
81
82LxPs()
83
84
Jan Kiszkacf7492e2015-02-17 13:47:15 -080085thread_info_type = utils.CachedType("struct thread_info")
86
87ia64_task_size = None
88
89
90def get_thread_info(task):
Jan Kiszkacf7492e2015-02-17 13:47:15 -080091 thread_info_ptr_type = thread_info_type.get_type().pointer()
92 if utils.is_target_arch("ia64"):
93 global ia64_task_size
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)
98 else:
99 thread_info = task['stack'].cast(thread_info_ptr_type)
100 return thread_info.dereference()
101
102
103class LxThreadInfoFunc (gdb.Function):
104 """Calculate Linux thread_info from task variable.
105
106$lx_thread_info(TASK): Given TASK, return the corresponding thread_info
107variable."""
108
109 def __init__(self):
110 super(LxThreadInfoFunc, self).__init__("lx_thread_info")
111
112 def invoke(self, task):
113 return get_thread_info(task)
114
115
116LxThreadInfoFunc()