xref: /linux-6.15/scripts/gdb/linux/tasks.py (revision a930850b)
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    global task_type
24    task_ptr_type = task_type.get_type().pointer()
25    init_task = gdb.parse_and_eval("init_task").address
26    t = g = init_task
27
28    while True:
29        while True:
30            yield t
31
32            t = utils.container_of(t['thread_group']['next'],
33                                   task_ptr_type, "thread_group")
34            if t == g:
35                break
36
37        t = g = utils.container_of(g['tasks']['next'],
38                                   task_ptr_type, "tasks")
39        if t == init_task:
40            return
41
42
43def get_task_by_pid(pid):
44    for task in task_lists():
45        if int(task['pid']) == pid:
46            return task
47    return None
48
49
50class LxTaskByPidFunc(gdb.Function):
51    """Find Linux task by PID and return the task_struct variable.
52
53$lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
54return that task_struct variable which PID matches."""
55
56    def __init__(self):
57        super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
58
59    def invoke(self, pid):
60        task = get_task_by_pid(pid)
61        if task:
62            return task.dereference()
63        else:
64            raise gdb.GdbError("No task of PID " + str(pid))
65
66
67LxTaskByPidFunc()
68
69
70class LxPs(gdb.Command):
71    """Dump Linux tasks."""
72
73    def __init__(self):
74        super(LxPs, self).__init__("lx-ps", gdb.COMMAND_DATA)
75
76    def invoke(self, arg, from_tty):
77        for task in task_lists():
78            gdb.write("{address} {pid} {comm}\n".format(
79                address=task,
80                pid=task["pid"],
81                comm=task["comm"].string()))
82
83LxPs()
84
85
86thread_info_type = utils.CachedType("struct thread_info")
87
88ia64_task_size = None
89
90
91def get_thread_info(task):
92    global thread_info_type
93    thread_info_ptr_type = thread_info_type.get_type().pointer()
94    if utils.is_target_arch("ia64"):
95        global ia64_task_size
96        if ia64_task_size is None:
97            ia64_task_size = gdb.parse_and_eval("sizeof(struct task_struct)")
98        thread_info_addr = task.address + ia64_task_size
99        thread_info = thread_info_addr.cast(thread_info_ptr_type)
100    else:
101        thread_info = task['stack'].cast(thread_info_ptr_type)
102    return thread_info.dereference()
103
104
105class LxThreadInfoFunc (gdb.Function):
106    """Calculate Linux thread_info from task variable.
107
108$lx_thread_info(TASK): Given TASK, return the corresponding thread_info
109variable."""
110
111    def __init__(self):
112        super(LxThreadInfoFunc, self).__init__("lx_thread_info")
113
114    def invoke(self, task):
115        return get_thread_info(task)
116
117
118LxThreadInfoFunc()
119