xref: /linux-6.15/scripts/gdb/linux/tasks.py (revision 0df8e970)
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 <[email protected]>
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
22def task_lists():
23    task_ptr_type = task_type.get_type().pointer()
24    init_task = gdb.parse_and_eval("init_task").address
25    t = g = init_task
26
27    while True:
28        while True:
29            yield t
30
31            t = utils.container_of(t['thread_group']['next'],
32                                   task_ptr_type, "thread_group")
33            if t == g:
34                break
35
36        t = g = utils.container_of(g['tasks']['next'],
37                                   task_ptr_type, "tasks")
38        if t == init_task:
39            return
40
41
42def get_task_by_pid(pid):
43    for task in task_lists():
44        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()
67
68
69class 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        gdb.write("{:>10} {:>12} {:>7}\n".format("TASK", "PID", "COMM"))
77        for task in task_lists():
78            gdb.write("{} {:^5} {}\n".format(
79                task.format_string().split()[0],
80                task["pid"].format_string(),
81                task["comm"].string()))
82
83
84LxPs()
85
86
87thread_info_type = utils.CachedType("struct thread_info")
88
89
90def get_thread_info(task):
91    thread_info_ptr_type = thread_info_type.get_type().pointer()
92    if task.type.fields()[0].type == thread_info_type.get_type():
93        return task['thread_info']
94    thread_info = task['stack'].cast(thread_info_ptr_type)
95    return thread_info.dereference()
96
97
98class LxThreadInfoFunc (gdb.Function):
99    """Calculate Linux thread_info from task variable.
100
101$lx_thread_info(TASK): Given TASK, return the corresponding thread_info
102variable."""
103
104    def __init__(self):
105        super(LxThreadInfoFunc, self).__init__("lx_thread_info")
106
107    def invoke(self, task):
108        return get_thread_info(task)
109
110
111LxThreadInfoFunc()
112
113
114class LxThreadInfoByPidFunc (gdb.Function):
115    """Calculate Linux thread_info from task variable found by pid
116
117$lx_thread_info_by_pid(PID): Given PID, return the corresponding thread_info
118variable."""
119
120    def __init__(self):
121        super(LxThreadInfoByPidFunc, self).__init__("lx_thread_info_by_pid")
122
123    def invoke(self, pid):
124        task = get_task_by_pid(pid)
125        if task:
126            return get_thread_info(task.dereference())
127        else:
128            raise gdb.GdbError("No task of PID " + str(pid))
129
130
131LxThreadInfoByPidFunc()
132