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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
/*-
* Copyright (c) 2004 David Xu <davidxu@freebsd.org>
* 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 AUTHOR AND CONTRIBUTORS ``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 AUTHOR OR CONTRIBUTORS 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/cdefs.h>
__FBSDID("$FreeBSD$");
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <thr_private.h>
#include <sys/types.h>
#include <sys/kse.h>
#include "pthread_dbg_md.h"
#include "pthread_dbg.h"
#include "pthread_dbg_int.h"
static void td_empty_thread_list(td_proc_t *proc);
static int td_refresh_thread_list(td_proc_t *proc);
static int td_get_thread(td_proc_t *proc, caddr_t addr, int type,
td_thread_t **threadp);
static void td_remove_map(td_proc_t *proc, int type);
int
td_open(td_proc_callbacks_t *cb, void *arg, td_proc_t **procp)
{
#define LOOKUP_SYM(proc, sym, addr) \
ret = LOOKUP(proc, sym, addr); \
if (ret != 0) { \
if (ret == TD_ERR_NOSYM) \
ret = TD_ERR_NOLIB; \
goto error; \
}
td_proc_t *proc;
int dbg;
int ret;
td_md_init();
proc = malloc(sizeof(*proc));
if (proc == NULL)
return (TD_ERR_NOMEM);
proc->cb = cb;
proc->arg = arg;
proc->thread_activated = 0;
proc->thread_listgen = -1;
TAILQ_INIT(&proc->threads);
LOOKUP_SYM(proc, "_libkse_debug", &proc->libkse_debug_addr);
LOOKUP_SYM(proc, "_thread_list", &proc->thread_list_addr);
LOOKUP_SYM(proc, "_thread_listgen", &proc->thread_listgen_addr);
LOOKUP_SYM(proc, "_thread_activated", &proc->thread_activated_addr);
/* LOOKUP_SYM(proc, "_thread_active_kseq", &proc->thread_active_kseq_addr);*/
dbg = getpid();
/*
* If this fails it probably means we're debugging a core file and
* can't write to it.
* If it's something else we'll lose the next time we hit WRITE,
* but not before, and that's OK.
*/
WRITE(proc, proc->libkse_debug_addr, &dbg, sizeof(int));
*procp = proc;
return (0);
error:
free(proc);
return (ret);
}
int
td_close(td_proc_t *proc)
{
int dbg;
td_empty_thread_list(proc);
dbg = 0;
/*
* Error returns from this write are not really a problem;
* the process doesn't exist any more.
*/
WRITE(proc, proc->libkse_debug_addr, &dbg, sizeof(int));
free(proc);
return 0;
}
int
td_thr_iter(td_proc_t *proc, int (*call)(td_thread_t *, void *),
void *callarg)
{
int ret;
td_thread_t *thread;
td_refresh_thread_list(proc);
TAILQ_FOREACH(thread, &proc->threads, tle) {
ret = (*call)(thread, callarg);
if (ret != 0)
return (ret);
}
return (0);
}
int
td_thr_info(td_thread_t *thread, td_thread_info_t *info)
{
int ret = 0;
struct pthread pt;
memset(info, 0, sizeof(*info));
if (thread->type == TD_TYPE_UPCALL) {
info->thread_id = (long)thread->kthread;
info->thread_state = TD_STATE_RUNNING;
info->thread_type = TD_TYPE_UPCALL;
return (ret);
}
/* Handle normal thread */
ret = READ(thread->proc, thread->addr, &pt, sizeof(pt));
if (ret != 0)
return (ret);
if (pt.magic != THR_MAGIC)
return (TD_ERR_BADTHREAD);
info->thread_id = (long)thread->addr;
info->thread_addr = thread->addr;
info->thread_type = TD_TYPE_NORMAL;
switch (pt.state) {
case PS_RUNNING:
info->thread_state = TD_STATE_RUNNING;
break;
case PS_LOCKWAIT:
info->thread_state = TD_STATE_LOCKWAIT;
break;
case PS_MUTEX_WAIT:
info->thread_state = TD_STATE_MUTEXWAIT;
break;
case PS_COND_WAIT:
info->thread_state = TD_STATE_SLEEPING;
break;
case PS_SIGSUSPEND:
info->thread_state = TD_STATE_SIGSUSPEND;
break;
case PS_SIGWAIT:
info->thread_state = TD_STATE_SIGWAIT;
break;
case PS_JOIN:
info->thread_state = TD_STATE_JOIN;
break;
case PS_SUSPENDED:
info->thread_state = TD_STATE_SUSPENDED;
break;
case PS_DEAD:
info->thread_state = TD_STATE_DEAD;
break;
case PS_DEADLOCK:
info->thread_state = TD_STATE_DEADLOCK;
break;
default:
info->thread_state = TD_STATE_UNKNOWN;
break;
}
info->thread_scope = (pt.attr.flags & PTHREAD_SCOPE_SYSTEM) ?
TD_SCOPE_SYSTEM : TD_SCOPE_PROCESS;
info->thread_stack.ss_sp = pt.attr.stackaddr_attr;
info->thread_stack.ss_size = pt.attr.stacksize_attr;
info->thread_joiner = (caddr_t)pt.joiner;
info->thread_errno = pt.error;
info->thread_sigmask = pt.sigmask;
info->thread_sigpend = pt.sigpend;
info->thread_sigstk = pt.sigstk;
info->thread_base_priority = pt.base_priority;
info->thread_inherited_priority = pt.inherited_priority;
info->thread_active_priority = pt.active_priority;
info->thread_cancelflags = pt.cancelflags;
info->thread_retval = pt.join_status.ret;
info->thread_tls = (caddr_t)pt.specific;
info->thread_tlscount = pt.specific_data_count;
return (0);
}
int
td_thr_getregs(td_thread_t *thread, int regset, void *buf)
{
td_proc_t *proc = thread->proc;
caddr_t tmbx_addr, ptr;
struct kse_thr_mailbox tmbx;
int ret;
if (regset != 0 && regset != 1)
return (TD_ERR_INVAL);
if (thread->kthread != NULL)
return GETREGS(proc, regset, thread->kthread, buf);
tmbx_addr = thread->tcb_addr + offsetof(struct tcb, tcb_tmbx);
ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_kthread);
ret = READ(proc, ptr, &ptr, sizeof(ptr));
if (ret != 0)
return (ret);
if (ptr != NULL)
return GETREGS(proc, regset, ptr, buf);
ret = READ(proc, tmbx_addr, &tmbx, sizeof(tmbx));
if (ret != 0)
return (ret);
if (regset == 0)
td_ucontext_to_reg(&tmbx.tm_context, (struct reg *)buf);
else
td_ucontext_to_fpreg(&tmbx.tm_context, (struct fpreg *)buf);
return (0);
}
int
td_thr_setregs(td_thread_t *thread, int regset, void *buf)
{
td_proc_t *proc = thread->proc;
caddr_t tmbx_addr, ptr;
struct kse_thr_mailbox tmbx;
int ret;
if (regset != 0 && regset != 1)
return (TD_ERR_INVAL);
if (thread->kthread != NULL)
return SETREGS(proc, regset, thread->kthread, buf);
tmbx_addr = thread->tcb_addr + offsetof(struct tcb, tcb_tmbx);
ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_kthread);
ret = READ(proc, ptr, &ptr, sizeof(ptr));
if (ret != 0)
return (ret);
if (ptr != NULL)
return SETREGS(proc, regset, ptr, buf);
/*
* Read a copy of context, this makes sure that registers
* not covered by structure reg won't be clobbered
*/
ret = READ(proc, tmbx_addr, &tmbx, sizeof(tmbx));
if (ret != 0)
return (ret);
if (regset == 0)
td_reg_to_ucontext((struct reg *)buf, &tmbx.tm_context);
else
td_fpreg_to_ucontext((struct fpreg *)buf, &tmbx.tm_context);
return (WRITE(proc, tmbx_addr, &tmbx, sizeof(tmbx)));
}
int
td_thr_getname(td_thread_t *thread, char *name, int len)
{
int ret;
caddr_t nameaddr;
if ((ret = READ(thread->proc,
thread->addr + offsetof(struct pthread, name),
&nameaddr, sizeof(nameaddr))) != 0)
return (ret);
if (nameaddr == 0)
name[0] = '\0';
else if ((ret = READ_STRING(thread->proc, nameaddr,
name, len)) != 0)
return (ret);
return (0);
}
int
td_activated(td_proc_t *proc)
{
if (proc->thread_activated)
return (1);
READ(proc, proc->thread_activated_addr, &proc->thread_activated,
sizeof(proc->thread_activated));
return (proc->thread_activated != 0);
}
int
td_map_lwp2thr(td_proc_t *proc, void *lwp, td_thread_t **threadp)
{
int ret;
caddr_t next, ptr;
td_thread_t *thread;
TAILQ_HEAD(, pthread) thread_list;
ret = READ(proc, proc->thread_list_addr, &thread_list,
sizeof(thread_list));
if (ret != 0)
return (ret);
/*
* We have to iterate through thread list to find which
* userland thread is running on the kernel thread.
*/
next = (caddr_t)thread_list.tqh_first;
while (next != NULL) {
ret = READ(proc, next + offsetof(struct pthread, tcb),
&ptr, sizeof(ptr));
if (ret != 0)
return (ret);
ptr += offsetof(struct tcb, tcb_tmbx.tm_kthread);
ret = READ(proc, ptr, &ptr, sizeof(ptr));
if (ret != 0)
return (ret);
if (ptr == lwp) {
ret = td_get_thread(proc, next, TD_TYPE_NORMAL,
&thread);
if (ret != 0)
return (ret);
*threadp = thread;
return (0);
}
/* get next thread */
ret = READ(proc,
next + offsetof(struct pthread, tle.tqe_next),
&next, sizeof(next));
if (ret != 0)
return (ret);
}
return (TD_ERR_NOOBJ);
}
int
td_map_id2thr(td_proc_t *proc, long threadid, td_thread_t **threadp)
{
td_thread_t *thread;
td_refresh_thread_list(proc);
TAILQ_FOREACH(thread, &proc->threads, tle) {
if (thread->type == TD_TYPE_UPCALL) {
if ((long)thread->kthread == threadid)
break;
} else if ((long)thread->addr == threadid)
break;
}
if (thread) {
*threadp = thread;
return (0);
}
return (TD_ERR_NOOBJ);
}
int
td_thr_sstep(td_thread_t *thread, int step)
{
int ret;
uint32_t tmp;
struct reg reg;
td_proc_t *proc;
caddr_t kthread;
proc = thread->proc;
if ((kthread=thread->kthread) != NULL)
return SSTEP(proc, kthread, step);
if (thread->step == step)
return (0);
/* Clear or set single step flag in thread mailbox */
tmp = step ? TMDF_SSTEP : 0;
ret = WRITE(proc, thread->tcb_addr + offsetof(struct tcb,
tcb_tmbx.tm_dflags), &tmp, sizeof(tmp));
/* Get kthread */
ret = READ(proc, thread->tcb_addr + offsetof(struct tcb,
tcb_tmbx.tm_kthread), &kthread, sizeof(kthread));
if (ret != 0)
return (ret);
thread->step = step;
if (kthread != NULL)
return SSTEP(proc, kthread, step);
/*
* context is in userland, some architectures store
* single step status in registers, we should change
* these registers.
*/
ret = td_thr_getregs(thread, 0, ®);
if (ret == 0) {
/* only write out if it is really changed. */
if (td_reg_sstep(®, step) != 0)
ret = td_thr_setregs(thread, 0, ®);
}
return (ret);
}
void
td_remove_lwp_map(td_proc_t *proc)
{
td_remove_map(proc, TD_TYPE_UPCALL);
}
static void
td_remove_map(td_proc_t *proc, int type)
{
td_thread_t *thread, *next;
for (thread = TAILQ_FIRST(&proc->threads); thread; thread = next) {
next = TAILQ_NEXT(thread, tle);
if (type == type)
TAILQ_REMOVE(&proc->threads, thread, tle);
}
}
static int
td_refresh_thread_list(td_proc_t *proc)
{
int ret, gen;
caddr_t next;
td_thread_t *thread;
TAILQ_HEAD(, pthread) thread_list;
ret = READ(proc, proc->thread_listgen_addr, &gen, sizeof(gen));
if (ret != 0)
return (ret);
if (gen == proc->thread_listgen)
return (TD_ERR_OK);
proc->thread_listgen = gen;
td_remove_map(proc, TD_TYPE_NORMAL);
ret = READ(proc, proc->thread_list_addr, &thread_list,
sizeof(thread_list));
if (ret != 0)
return (ret);
next = (caddr_t)thread_list.tqh_first;
while (next != NULL) {
ret = td_get_thread(proc, next, TD_TYPE_NORMAL, &thread);
if (ret != 0)
return (ret);
ret = READ(proc,
next + offsetof(struct pthread, tle.tqe_next),
&next, sizeof(next));
if (ret != 0)
return (ret);
}
return (0);
}
static void
td_empty_thread_list(td_proc_t *proc)
{
td_thread_t *thread;
while ((thread = TAILQ_FIRST(&proc->threads)) != NULL) {
TAILQ_REMOVE(&proc->threads, thread, tle);
free(thread);
}
}
static int
td_get_thread(td_proc_t *proc, caddr_t addr, int type, td_thread_t **threadp)
{
td_thread_t *thread;
caddr_t tcb_addr;
int ret;
TAILQ_FOREACH(thread, &proc->threads, tle) {
if (type == TD_TYPE_UPCALL) {
/* match upcall thread */
if (thread->kthread == addr)
break;
} else {
/* match normal thread */
if (thread->addr == addr)
break;
}
}
if (thread == NULL) {
tcb_addr = NULL;
if (type != TD_TYPE_UPCALL) {
ret = READ(proc, addr + offsetof(struct pthread, tcb),
&tcb_addr, sizeof(tcb_addr));
if (ret)
return (ret);
}
thread = malloc(sizeof(*thread));
if (thread == NULL)
return (TD_ERR_NOMEM);
thread->proc = proc;
thread->step = -1;
thread->tcb_addr = tcb_addr;
if (type == TD_TYPE_NORMAL) {
thread->addr = addr;
thread->kthread = NULL;
} else {
thread->addr = NULL;
thread->kthread = addr;
}
thread->type = type;
TAILQ_INSERT_TAIL(&proc->threads, thread, tle);
}
*threadp = thread;
return (0);
}
struct string_map {
int num;
char *str;
};
char *
td_err_string (int errcode)
{
static struct string_map err_table[] = {
{TD_ERR_OK, "generic \"call succeeded\""},
{TD_ERR_ERR, "generic error."},
{TD_ERR_NOSYM, "symbol not found"},
{TD_ERR_NOOBJ, "no object can be found to satisfy query"},
{TD_ERR_BADTHREAD, "thread can not answer request"},
{TD_ERR_INUSE, "debugging interface already in use for this process"},
{TD_ERR_NOLIB, "process is not using libpthread"},
{TD_ERR_NOMEM, "out of memory"},
{TD_ERR_IO, "process callback error"},
{TD_ERR_INVAL, "invalid argument"},
};
const int err_size = sizeof(err_table) / sizeof (struct string_map);
int i;
static char buf[90];
for (i = 0; i < err_size; i++)
if (err_table[i].num == errcode)
return err_table[i].str;
sprintf (buf, "Unknown thread library debug error code: %d", errcode);
return buf;
}
|