]> Git Repo - linux.git/blob - scripts/gdb/linux/cpus.py
Merge tag 'input-for-v6.7-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor...
[linux.git] / scripts / gdb / linux / cpus.py
1 #
2 # gdb helper commands and functions for Linux kernel debugging
3 #
4 #  per-cpu 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
14 import gdb
15
16 from linux import tasks, utils
17
18
19 task_type = utils.CachedType("struct task_struct")
20
21
22 MAX_CPUS = 4096
23
24
25 def get_current_cpu():
26     if utils.get_gdbserver_type() == utils.GDBSERVER_QEMU:
27         return gdb.selected_thread().num - 1
28     elif utils.get_gdbserver_type() == utils.GDBSERVER_KGDB:
29         tid = gdb.selected_thread().ptid[2]
30         if tid > (0x100000000 - MAX_CPUS - 2):
31             return 0x100000000 - tid - 2
32         else:
33             return tasks.get_thread_info(tasks.get_task_by_pid(tid))['cpu']
34     else:
35         raise gdb.GdbError("Sorry, obtaining the current CPU is not yet "
36                            "supported with this gdb server.")
37
38
39 def per_cpu(var_ptr, cpu):
40     if cpu == -1:
41         cpu = get_current_cpu()
42     if utils.is_target_arch("sparc:v9"):
43         offset = gdb.parse_and_eval(
44             "trap_block[{0}].__per_cpu_base".format(str(cpu)))
45     else:
46         try:
47             offset = gdb.parse_and_eval(
48                 "__per_cpu_offset[{0}]".format(str(cpu)))
49         except gdb.error:
50             # !CONFIG_SMP case
51             offset = 0
52     pointer = var_ptr.cast(utils.get_long_type()) + offset
53     return pointer.cast(var_ptr.type).dereference()
54
55
56 cpu_mask = {}
57
58
59 def cpu_mask_invalidate(event):
60     global cpu_mask
61     cpu_mask = {}
62     gdb.events.stop.disconnect(cpu_mask_invalidate)
63     if hasattr(gdb.events, 'new_objfile'):
64         gdb.events.new_objfile.disconnect(cpu_mask_invalidate)
65
66
67 def cpu_list(mask_name):
68     global cpu_mask
69     mask = None
70     if mask_name in cpu_mask:
71         mask = cpu_mask[mask_name]
72     if mask is None:
73         mask = gdb.parse_and_eval(mask_name + ".bits")
74         if hasattr(gdb, 'events'):
75             cpu_mask[mask_name] = mask
76             gdb.events.stop.connect(cpu_mask_invalidate)
77             if hasattr(gdb.events, 'new_objfile'):
78                 gdb.events.new_objfile.connect(cpu_mask_invalidate)
79     bits_per_entry = mask[0].type.sizeof * 8
80     num_entries = mask.type.sizeof * 8 / bits_per_entry
81     entry = -1
82     bits = 0
83
84     while True:
85         while bits == 0:
86             entry += 1
87             if entry == num_entries:
88                 return
89             bits = mask[entry]
90             if bits != 0:
91                 bit = 0
92                 break
93
94         while bits & 1 == 0:
95             bits >>= 1
96             bit += 1
97
98         cpu = entry * bits_per_entry + bit
99
100         bits >>= 1
101         bit += 1
102
103         yield int(cpu)
104
105
106 def each_online_cpu():
107     for cpu in cpu_list("__cpu_online_mask"):
108         yield cpu
109
110
111 def each_present_cpu():
112     for cpu in cpu_list("__cpu_present_mask"):
113         yield cpu
114
115
116 def each_possible_cpu():
117     for cpu in cpu_list("__cpu_possible_mask"):
118         yield cpu
119
120
121 def each_active_cpu():
122     for cpu in cpu_list("__cpu_active_mask"):
123         yield cpu
124
125
126 class LxCpus(gdb.Command):
127     """List CPU status arrays
128
129 Displays the known state of each CPU based on the kernel masks
130 and can help identify the state of hotplugged CPUs"""
131
132     def __init__(self):
133         super(LxCpus, self).__init__("lx-cpus", gdb.COMMAND_DATA)
134
135     def invoke(self, arg, from_tty):
136         gdb.write("Possible CPUs : {}\n".format(list(each_possible_cpu())))
137         gdb.write("Present CPUs  : {}\n".format(list(each_present_cpu())))
138         gdb.write("Online CPUs   : {}\n".format(list(each_online_cpu())))
139         gdb.write("Active CPUs   : {}\n".format(list(each_active_cpu())))
140
141
142 LxCpus()
143
144
145 class PerCpu(gdb.Function):
146     """Return per-cpu variable.
147
148 $lx_per_cpu("VAR"[, CPU]): Return the per-cpu variable called VAR for the
149 given CPU number. If CPU is omitted, the CPU of the current context is used.
150 Note that VAR has to be quoted as string."""
151
152     def __init__(self):
153         super(PerCpu, self).__init__("lx_per_cpu")
154
155     def invoke(self, var_name, cpu=-1):
156         var_ptr = gdb.parse_and_eval("&" + var_name.string())
157         return per_cpu(var_ptr, cpu)
158
159
160 PerCpu()
161
162 def get_current_task(cpu):
163     task_ptr_type = task_type.get_type().pointer()
164
165     if utils.is_target_arch("x86"):
166         if gdb.lookup_global_symbol("cpu_tasks"):
167             # This is a UML kernel, which stores the current task
168             # differently than other x86 sub architectures
169             var_ptr = gdb.parse_and_eval("(struct task_struct *)cpu_tasks[0].task")
170             return var_ptr.dereference()
171         else:
172             var_ptr = gdb.parse_and_eval("&pcpu_hot.current_task")
173             return per_cpu(var_ptr, cpu).dereference()
174     elif utils.is_target_arch("aarch64"):
175         current_task_addr = gdb.parse_and_eval("$SP_EL0")
176         if (current_task_addr >> 63) != 0:
177             current_task = current_task_addr.cast(task_ptr_type)
178             return current_task.dereference()
179         else:
180             raise gdb.GdbError("Sorry, obtaining the current task is not allowed "
181                                "while running in userspace(EL0)")
182     elif utils.is_target_arch("riscv"):
183         current_tp = gdb.parse_and_eval("$tp")
184         scratch_reg = gdb.parse_and_eval("$sscratch")
185
186         # by default tp points to current task
187         current_task = current_tp.cast(task_ptr_type)
188
189         # scratch register is set 0 in trap handler after entering kernel.
190         # When hart is in user mode, scratch register is pointing to task_struct.
191         # and tp is used by user mode. So when scratch register holds larger value
192         # (negative address as ulong is larger value) than tp, then use scratch register.
193         if (scratch_reg.cast(utils.get_ulong_type()) > current_tp.cast(utils.get_ulong_type())):
194             current_task = scratch_reg.cast(task_ptr_type)
195
196         return current_task.dereference()
197     else:
198         raise gdb.GdbError("Sorry, obtaining the current task is not yet "
199                            "supported with this arch")
200
201 class LxCurrentFunc(gdb.Function):
202     """Return current task.
203
204 $lx_current([CPU]): Return the per-cpu task variable for the given CPU
205 number. If CPU is omitted, the CPU of the current context is used."""
206
207     def __init__(self):
208         super(LxCurrentFunc, self).__init__("lx_current")
209
210     def invoke(self, cpu=-1):
211         return get_current_task(cpu)
212
213
214 LxCurrentFunc()
This page took 0.036118 seconds and 4 git commands to generate.