1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*
* Copyright (c) 2004 Marcel Moolenaar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/proc.h>
#include <stdbool.h>
#include "defs.h"
#include "gdbcore.h"
#include "objfiles.h"
#include "value.h"
#include "kgdb.h"
static CORE_ADDR dumppcb;
static LONGEST dumptid;
static CORE_ADDR stopped_cpus;
static LONGEST mp_maxid;
static struct kthr *first, *last;
struct kthr *curkthr;
static int proc_off_p_pid, proc_off_p_comm, proc_off_p_hash, proc_off_p_list;
static int proc_off_p_threads;
static int thread_off_td_tid, thread_off_td_oncpu, thread_off_td_pcb;
static int thread_off_td_name, thread_off_td_plist;
static int thread_oncpu_size;
CORE_ADDR
kgdb_lookup(const char *sym)
{
struct bound_minimal_symbol msym;
msym = lookup_minimal_symbol(sym, NULL, NULL);
if (msym.minsym == NULL)
return (0);
return (BMSYMBOL_VALUE_ADDRESS(msym));
}
/*
* Perform the equivalent of CPU_ISSET() to see if 'cpu' is set in the
* kernel's stopped_cpus set. The set contains an array of longs.
* This function determines the specific long to read and tests the
* necessary bit in the long.
*/
static bool
cpu_stopped(int cpu)
{
struct gdbarch *gdbarch = target_gdbarch ();
CORE_ADDR addr;
ULONGEST mask;
int bit, long_bytes, word;
if (cpu < 0 || cpu > mp_maxid || stopped_cpus == 0)
return (false);
bit = cpu % gdbarch_long_bit (gdbarch);
word = cpu / gdbarch_long_bit (gdbarch);
long_bytes = gdbarch_long_bit (gdbarch) / 8;
addr = stopped_cpus + word * long_bytes;
mask = read_memory_unsigned_integer (addr, long_bytes,
gdbarch_byte_order (gdbarch));
return (mask & ((ULONGEST)1 << bit)) != 0;
}
struct kthr *
kgdb_thr_first(void)
{
return (first);
}
static void
kgdb_thr_add_proc(CORE_ADDR paddr, CORE_ADDR (*cpu_pcb_addr) (u_int))
{
struct gdbarch *gdbarch = target_gdbarch ();
struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
struct kthr *kt;
CORE_ADDR pcb, tdaddr, tdnext;
ULONGEST oncpu;
LONGEST pid, tid;
try {
tdaddr = read_memory_typed_address (paddr + proc_off_p_threads,
ptr_type);
pid = read_memory_integer (paddr + proc_off_p_pid, 4, byte_order);
} catch (const gdb_exception_error &e) {
return;
}
while (tdaddr != 0) {
try {
tid = read_memory_integer (tdaddr + thread_off_td_tid,
4, byte_order);
oncpu = read_memory_unsigned_integer (tdaddr +
thread_off_td_oncpu, thread_oncpu_size, byte_order);
pcb = read_memory_typed_address (tdaddr +
thread_off_td_pcb, ptr_type);
tdnext = read_memory_typed_address (tdaddr +
thread_off_td_plist, ptr_type);
} catch (const gdb_exception_error &e) {
return;
}
kt = XNEW (struct kthr);
if (last == NULL)
first = last = kt;
else
last->next = kt;
kt->next = NULL;
kt->kaddr = tdaddr;
if (tid == dumptid)
kt->pcb = dumppcb;
else if (cpu_stopped(oncpu))
kt->pcb = cpu_pcb_addr(oncpu);
else
kt->pcb = pcb;
kt->tid = tid;
kt->pid = pid;
kt->paddr = paddr;
kt->cpu = oncpu;
last = kt;
tdaddr = tdnext;
}
}
static void
kgdb_thr_add_procs_hash(CORE_ADDR pidhashtbl, CORE_ADDR (*cpu_pcb_addr) (u_int))
{
struct gdbarch *gdbarch = target_gdbarch ();
struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
CORE_ADDR paddr, pnext;
ULONGEST i, pidhash;
pidhash = parse_and_eval_long("pidhash");
for (i = 0; i < pidhash; i++) {
try {
paddr = read_memory_typed_address (pidhashtbl +
i * TYPE_LENGTH(ptr_type), ptr_type);
} catch (const gdb_exception_error &e) {
continue;
}
while (paddr != 0) {
try {
pnext = read_memory_typed_address (paddr +
proc_off_p_hash, ptr_type);
} catch (const gdb_exception_error &e) {
break;
}
kgdb_thr_add_proc(paddr, cpu_pcb_addr);
paddr = pnext;
}
}
}
static void
kgdb_thr_add_procs_list(CORE_ADDR paddr, CORE_ADDR (*cpu_pcb_addr) (u_int))
{
struct gdbarch *gdbarch = target_gdbarch ();
struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
CORE_ADDR pnext;
while (paddr != 0) {
try {
pnext = read_memory_typed_address (paddr +
proc_off_p_list, ptr_type);
} catch (const gdb_exception_error &e) {
break;
}
kgdb_thr_add_proc(paddr, cpu_pcb_addr);
paddr = pnext;
}
}
struct kthr *
kgdb_thr_init(CORE_ADDR (*cpu_pcb_addr) (u_int))
{
struct gdbarch *gdbarch = target_gdbarch ();
struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
struct kthr *kt;
CORE_ADDR addr, paddr;
while (first != NULL) {
kt = first;
first = kt->next;
free(kt);
}
last = NULL;
dumppcb = kgdb_lookup("dumppcb");
if (dumppcb == 0)
return (NULL);
try {
dumptid = parse_and_eval_long("dumptid");
} catch (const gdb_exception_error &e) {
dumptid = -1;
}
try {
mp_maxid = parse_and_eval_long("mp_maxid");
} catch (const gdb_exception_error &e) {
mp_maxid = 0;
}
stopped_cpus = kgdb_lookup("stopped_cpus");
/*
* Newer kernels export a set of global variables with the offsets
* of certain members in struct proc and struct thread. For older
* kernels, try to extract these offsets using debug symbols. If
* that fails, use native values.
*/
try {
proc_off_p_pid = parse_and_eval_long("proc_off_p_pid");
proc_off_p_comm = parse_and_eval_long("proc_off_p_comm");
proc_off_p_list = parse_and_eval_long("proc_off_p_list");
proc_off_p_threads = parse_and_eval_long("proc_off_p_threads");
thread_off_td_tid = parse_and_eval_long("thread_off_td_tid");
thread_off_td_name = parse_and_eval_long("thread_off_td_name");
thread_off_td_oncpu = parse_and_eval_long("thread_off_td_oncpu");
thread_off_td_pcb = parse_and_eval_long("thread_off_td_pcb");
thread_off_td_plist = parse_and_eval_long("thread_off_td_plist");
thread_oncpu_size = 4;
} catch (const gdb_exception_error &e) {
try {
struct symbol *proc_sym =
lookup_symbol_in_language ("struct proc", NULL,
STRUCT_DOMAIN, language_c, NULL).symbol;
if (proc_sym == NULL)
error (_("Unable to find struct proc symbol"));
proc_off_p_pid =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "p_pid",
0).offset / 8;
proc_off_p_comm =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "p_comm",
0).offset / 8;
proc_off_p_list =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "p_list",
0).offset / 8;
proc_off_p_threads =
lookup_struct_elt (SYMBOL_TYPE (proc_sym),
"p_threads", 0).offset / 8;
struct symbol *thread_sym =
lookup_symbol_in_language ("struct thread", NULL,
STRUCT_DOMAIN, language_c, NULL).symbol;
if (thread_sym == NULL)
error (_("Unable to find struct thread symbol"));
thread_off_td_tid =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "td_tid",
0).offset / 8;
thread_off_td_name =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "td_name",
0).offset / 8;
thread_off_td_pcb =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "td_pcb",
0).offset / 8;
thread_off_td_plist =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "td_plist",
0).offset / 8;
struct_elt td_oncpu =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "td_oncpu",
0);
thread_off_td_oncpu = td_oncpu.offset / 8;
thread_oncpu_size = FIELD_BITSIZE(*td_oncpu.field) / 8;
} catch (const gdb_exception_error &e2) {
proc_off_p_pid = offsetof(struct proc, p_pid);
proc_off_p_comm = offsetof(struct proc, p_comm);
proc_off_p_list = offsetof(struct proc, p_list);
proc_off_p_threads = offsetof(struct proc, p_threads);
thread_off_td_tid = offsetof(struct thread, td_tid);
thread_off_td_name = offsetof(struct thread, td_name);
thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
thread_off_td_pcb = offsetof(struct thread, td_pcb);
thread_off_td_plist = offsetof(struct thread, td_plist);
thread_oncpu_size =
sizeof(((struct thread *)0)->td_oncpu);
}
}
/*
* Handle p_hash separately.
*/
try {
proc_off_p_hash = parse_and_eval_long("proc_off_p_hash");
} catch (const gdb_exception_error &e) {
try {
struct symbol *proc_sym =
lookup_symbol_in_language ("struct proc", NULL,
STRUCT_DOMAIN, language_c, NULL).symbol;
if (proc_sym == NULL)
error (_("Unable to find struct proc symbol"));
proc_off_p_hash =
lookup_struct_elt (SYMBOL_TYPE (proc_sym), "p_hash",
0).offset / 8;
} catch (const gdb_exception_error &e2) {
proc_off_p_hash = offsetof(struct proc, p_hash);
}
}
addr = kgdb_lookup("zombproc");
if (addr != 0) {
addr = kgdb_lookup("allproc");
try {
paddr = read_memory_typed_address (addr, ptr_type);
kgdb_thr_add_procs_list(paddr, cpu_pcb_addr);
} catch (const gdb_exception_error &e) {
return (NULL);
}
try {
paddr = read_memory_typed_address (addr, ptr_type);
kgdb_thr_add_procs_list(paddr, cpu_pcb_addr);
} catch (const gdb_exception_error &e) {
}
} else {
addr = kgdb_lookup("pidhashtbl");
try {
addr = read_memory_typed_address (addr, ptr_type);
kgdb_thr_add_procs_hash(addr, cpu_pcb_addr);
} catch (const gdb_exception_error &e) {
return (NULL);
}
}
curkthr = kgdb_thr_lookup_tid(dumptid);
if (curkthr == NULL)
curkthr = first;
return (first);
}
struct kthr *
kgdb_thr_lookup_tid(int tid)
{
struct kthr *kt;
kt = first;
while (kt != NULL && kt->tid != tid)
kt = kt->next;
return (kt);
}
struct kthr *
kgdb_thr_lookup_taddr(uintptr_t taddr)
{
struct kthr *kt;
kt = first;
while (kt != NULL && kt->kaddr != taddr)
kt = kt->next;
return (kt);
}
struct kthr *
kgdb_thr_lookup_pid(int pid)
{
struct kthr *kt;
kt = first;
while (kt != NULL && kt->pid != pid)
kt = kt->next;
return (kt);
}
struct kthr *
kgdb_thr_lookup_paddr(uintptr_t paddr)
{
struct kthr *kt;
kt = first;
while (kt != NULL && kt->paddr != paddr)
kt = kt->next;
return (kt);
}
struct kthr *
kgdb_thr_next(struct kthr *kt)
{
return (kt->next);
}
const char *
kgdb_thr_extra_thread_info(int tid)
{
static char buf[64];
struct kthr *kt = kgdb_thr_lookup_tid(tid);
if (kt == nullptr)
return (nullptr);
snprintf(buf, sizeof (buf), "PID=%d", kt->pid);
gdb::unique_xmalloc_ptr<char> comm
= target_read_string (kt->paddr + proc_off_p_comm, MAXCOMLEN + 1);
if (comm != nullptr)
{
strlcat(buf, ": ", sizeof (buf));
strlcat(buf, comm.get (), sizeof (buf));
gdb::unique_xmalloc_ptr<char> td_name
= target_read_string (kt->kaddr + thread_off_td_name, MAXCOMLEN + 1);
if (td_name != nullptr && strcmp (comm.get (), td_name.get ()) != 0)
{
strlcat(buf, "/", sizeof (buf));
strlcat(buf, td_name.get (), sizeof (buf));
}
}
return (buf);
}
|