-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththread_db.c
360 lines (302 loc) · 8.22 KB
/
thread_db.c
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
/*
* Copyright (c) 2002 Peter Edwards
*
* 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.
*
* $FreeBSD$
* $Id: pstack.c,v 1.3 2002/11/26 10:28:31 pmedwards Exp $
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/ptrace.h>
#include <dlfcn.h>
#include <elf.h>
#include <err.h>
#include <libgen.h>
#include <proc_service.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread_db.h>
#include "elfinfo.h"
#include "pstack.h"
#define LIBTHREAD_DB_SO "libthread_db.so"
static void thread_db_startup(void);
static int thread_db_probe(struct Process *proc);
static void thread_db_free(struct Process *proc);
static void thread_db_read_threads(struct Process *proc);
struct ps_prochandle {
struct Process *proc;
};
struct thread_db_info {
struct ps_prochandle proc_handle;
td_thragent_t *thread_agent;
};
struct thread_ops thread_db_ops = {
thread_db_startup,
thread_db_probe,
thread_db_read_threads,
thread_db_free
};
/* Pointers to the libthread_db functions. */
static td_err_e (*td_init_p) (void);
static td_err_e (*td_ta_new_p) (struct ps_prochandle *ps, td_thragent_t **ta);
static td_err_e (*td_ta_delete_p) (td_thragent_t *);
static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
td_thr_iter_f *callback,
void *cbdata_p, td_thr_state_e state,
int ti_pri, sigset_t *ti_sigmask_p,
unsigned int ti_user_flags);
static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
td_thrinfo_t *infop);
static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th,
prgregset_t gregs);
static int thread_db_loaded;
static void
thread_db_startup(void)
{
void *handle;
td_err_e err;
handle = dlopen(LIBTHREAD_DB_SO, RTLD_NOW);
if (handle == NULL)
return;
#define resolve(X) \
if (!(X##_p = dlsym(handle, #X))) \
return;
resolve(td_init);
resolve(td_ta_new);
resolve(td_ta_delete);
resolve(td_ta_thr_iter);
resolve(td_thr_get_info);
resolve(td_thr_getgregs);
/* Initialize the library. */
err = td_init_p();
if (err != TD_OK) {
warnx("Cannot initialize libthread_db: %d", err);
return;
}
thread_db_loaded = 1;
}
static int
thread_db_probe(struct Process *proc)
{
struct ElfObject *obj;
struct thread_db_info *info;
td_err_e err;
char *base;
if (!thread_db_loaded)
return (0);
/* Explicitly ignore 4.x binaries. */
for (obj = proc->objectList; obj != NULL; obj = obj->next) {
base = basename(obj->fileName);
if (base == NULL)
continue;
}
info = malloc(sizeof(struct thread_db_info));
info->proc_handle.proc = proc;
err = td_ta_new_p(&info->proc_handle, &info->thread_agent);
if (err == TD_OK) {
proc->threadInfo = info;
return (1);
}
free(info);
return (0);
}
static int
find_new_threads_callback(const td_thrhandle_t *th_p, void *data)
{
struct Process *proc;
struct Thread *t;
prgregset_t gregset;
td_thrinfo_t ti;
td_err_e err;
err = td_thr_get_info_p(th_p, &ti);
if (err != TD_OK) {
warnx("Cannot get thread info: %d", err);
return (0);
}
/* Ignore zombie */
if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
return (0);
err = td_thr_getgregs_p(th_p, gregset);
if (err != TD_OK) {
warnx("Cannot fetch registers for thread %ld: %d", ti.ti_tid,
err);
return (0);
}
proc = data;
#ifdef __LP64__
t = procReadThread(proc, gregset[0].r_rbp, gregset[0].r_rip);
#else
t = procReadThread(proc, gregset[0].r_ebp, gregset[0].r_eip);
#endif
if (t != NULL) {
t->id = ti.ti_tid;
if (ti.ti_state == TD_THR_RUN)
t->running = 1;
}
return 0;
}
static void
thread_db_read_threads(struct Process *proc)
{
struct thread_db_info *info;
td_err_e err;
/* Iterate over all user-space threads to discover new threads. */
info = proc->threadInfo;
err = td_ta_thr_iter_p(info->thread_agent, find_new_threads_callback,
proc, TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, TD_SIGNO_MASK,
TD_THR_ANY_USER_FLAGS);
if (err != TD_OK)
warnx("Cannot find new threads: %d", err);
}
static void
thread_db_free(struct Process *proc)
{
struct thread_db_info *info;
info = proc->threadInfo;
td_ta_delete_p(info->thread_agent);
free(proc->threadInfo);
proc->threadInfo = NULL;
}
/* proc service functions */
void
ps_plog(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
ps_err_e
ps_pglobal_lookup(struct ps_prochandle *ph, const char *objname,
const char *name, psaddr_t *sym_addr)
{
struct ElfObject *obj;
const Elf_Sym *sym;
obj = ph->proc->objectList;
while (obj != NULL) {
if (elfFindSymbolByName(obj, name, &sym) == 0) {
*sym_addr = (uintptr_t)(obj->baseAddr + sym->st_value);
return (PS_OK);
}
obj = obj->next;
}
return (PS_NOSYM);
}
ps_err_e
ps_pread(struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t len)
{
if (procReadMem(ph->proc, buf, (uintptr_t)addr, len) != len)
return (PS_ERR);
return (PS_OK);
}
ps_err_e
ps_pwrite(struct ps_prochandle *ph, psaddr_t addr, const void *buf, size_t len)
{
if (procWriteMem(ph->proc, buf, (uintptr_t)addr, len) != len)
return (PS_ERR);
return (PS_OK);
}
ps_err_e
ps_lgetregs(struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
{
struct ElfObject *core;
const prstatus_t *prstatus;
const void *regs;
int len;
if (ph->proc->pid == -1) {
core = ph->proc->coreImage;
if (elfGetNote(core, "FreeBSD", NT_PRSTATUS,
(const void **)&prstatus, &len) == -1)
return (PS_ERR);
while (prstatus->pr_pid != lwpid) {
if (elfGetNextNote(core, "FreeBSD", NT_PRSTATUS,
(const void **)&prstatus, &len) == -1)
return (PS_ERR);
}
memcpy(gregset, &prstatus->pr_reg, sizeof(*gregset));
return (PS_OK);
}
if (ptrace(PT_GETREGS, lwpid, (void *)gregset, 0) == -1)
return (PS_ERR);
return (PS_OK);
}
ps_err_e
ps_lsetregs(struct ps_prochandle *ph, lwpid_t lwpid, const prgregset_t gregset)
{
warnx("%s called\n", __func__);
return (PS_ERR);
}
ps_err_e
ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lwpid, prfpregset_t *fpregset)
{
warnx("%s called\n", __func__);
return (PS_ERR);
}
ps_err_e
ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lwpid,
const prfpregset_t *fpregset)
{
warnx("%s called\n", __func__);
return (PS_ERR);
}
#ifdef PT_GETXMMREGS
ps_err_e
ps_lgetxmmregs(struct ps_prochandle *ph, lwpid_t lwpid, char *xmmregs)
{
warnx("%s called\n", __func__);
return (PS_ERR);
}
ps_err_e
ps_lsetxmmregs(struct ps_prochandle *ph, lwpid_t lwpid,
const char *xmmregs)
{
warnx("%s called\n", __func__);
return (PS_ERR);
}
#endif
ps_err_e
ps_lstop(struct ps_prochandle *ph, lwpid_t lwpid)
{
warnx("%s called\n", __func__);
return (PS_ERR);
}
ps_err_e
ps_lcontinue(struct ps_prochandle *ph, lwpid_t lwpid)
{
warnx("%s called\n", __func__);
return (PS_ERR);
}
ps_err_e
ps_linfo(struct ps_prochandle *ph, lwpid_t lwpid, void *info)
{
if (ph->proc->pid == -1) {
/* XXX should verify lwpid and make a pseudo lwp info */
memset(info, 0, sizeof(struct ptrace_lwpinfo));
return (PS_OK);
}
if (ptrace(PT_LWPINFO, lwpid, info, sizeof(struct ptrace_lwpinfo)) == -1)
return (PS_ERR);
return (PS_OK);
}