3 #include <sys/ptrace.h>
5 #include <sys/procfs.h> /* psaddr_t */
10 int td_get_allthreads(td_thragent_t *ta, td_thrhandle_t **pall);
12 static char *tderrstr[] =
15 [TD_ERR] "some error",
16 [TD_NOTHR] "no matching thread found",
17 [TD_NOSV] "no matching synchronization handle found",
18 [TD_NOLWP] "no matching light-weight process found",
19 [TD_BADPH] "invalid process handle",
20 [TD_BADTH] "invalid thread handle",
21 [TD_BADSH] "invalid synchronization handle",
22 [TD_BADTA] "invalid thread agent",
23 [TD_BADKEY] "invalid key",
24 [TD_NOMSG] "no event available",
25 [TD_NOFPREGS] "no floating-point register content available",
26 [TD_NOLIBTHREAD] "application not linked with thread library",
27 [TD_NOEVENT] "requested event is not supported",
28 [TD_NOEVENT] "requested event is not supported",
29 [TD_NOCAPAB] "capability not available",
30 [TD_DBERR] "internal debug library error",
31 [TD_NOAPLIC] "operation is not applicable",
32 [TD_NOTSD] "no thread-specific data available",
33 [TD_MALLOC] "out of memory",
34 [TD_PARTIALREG] "not entire register set was read or written",
35 [TD_NOXREGS] "X register set not available for given threads",
36 [TD_TLSDEFER] "thread has not yet allocated TLS for given module",
37 [TD_VERSION] "version mismatch twixt libpthread and libthread_db",
38 [TD_NOTLS] "there is no TLS segment in the given module",
46 if(e < 0 || e >= nelem(tderrstr) || tderrstr[e] == nil){
47 snprint(buf, sizeof buf, "thread err %d", e);
56 fprint(2, "usage: t pid\n");
63 * print the value of dot as file:line
70 if (fileline(dot, str, STRINGSZ) >= 0)
75 printlocals(Symbol *fn, Regs *regs)
81 for (i = 0; indexlsym(fn, i, &s)>=0; i++) {
84 if(lget4(cormap, regs, s.loc, &v) >= 0)
85 print("\t%s.%s/\t%#lux\n", fn->name, s.name, v);
87 print("\t%s.%s/\t?\n", fn->name, s.name);
92 printparams(Symbol *fn, Regs *regs)
100 if(0) print("pc=%lux sp=%lux bp=%lux ",
101 (rget(regs, "PC", &pc), pc),
102 (rget(regs, "SP", &sp), sp),
103 (rget(regs, "BP", &bp), bp));
104 for (i = 0; indexlsym(fn, i, &s)>=0; i++) {
105 if (s.class != CPARAM)
109 if(0) print("(%d.%s.%ux.%x)", s.loc.type, s.loc.reg, s.loc.addr, s.loc.offset);
110 if(lget4(cormap, regs, s.loc, &v) >= 0)
111 print("%s=%#lux", s.name, v);
113 print("%s=?", s.name);
118 * callback on stack trace
121 xtrace(Map *map, Regs *regs, ulong pc, ulong nextpc, Symbol *sym, int depth)
128 print("%s(", sym->name);
129 printparams(sym, regs);
130 print(")+0x%ux ", pc-sym->loc.addr);
135 print(" called from ");
136 symoff(buf, 512, nextpc, CTEXT);
138 /* printsource(nextpc); */
141 printlocals(sym, regs);
146 main(int argc, char **argv)
148 struct ps_prochandle p;
163 attachargs(argc, argv, OREAD);
166 /* if(!corpid && !corhdr) */
167 /* sysfatal("could not attach to process"); */
170 if((e = td_ta_new(&p, &ta)) != TD_OK)
171 sysfatal("td_ta_new: %s", terr(e));
172 if((e = td_ta_get_nthreads(ta, &n)) != TD_OK)
173 sysfatal("td_ta_get_nthreads: %s", terr(e));
174 print("%d threads\n", n);
176 if((n = td_get_allthreads(ta, &ts)) < 0)
177 sysfatal("td_get_allthreads: %r");
178 print("%d threads - regs = %p\n", n, regs);
180 if((e = td_thr_get_info(&ts[i], &info)) != TD_OK)
181 sysfatal("td_thr_get_info: %s", terr(e));
182 print("%d: startfunc=%lux stkbase=%lux pc=%lux sp=%lux lid=%d\n",
183 i, info.ti_startfunc, info.ti_stkbase, info.ti_pc, info.ti_sp, info.ti_lid);
184 if((e = td_thr_getgregs(&ts[i], regs)) != TD_OK)
185 sysfatal("td_thr_getregs: %s", terr(e));
186 print("%d: pc=%lux sp=%lux gs=%lux\n", i, regs[12], regs[15], regs[10]);
187 if((u = _linux2ureg386((UregLinux386*)regs)) == nil)
191 stacktrace(cormap, &r.r, xtrace);
196 typedef struct AllThread AllThread;
205 thritercb(const td_thrhandle_t *th, void *cb)
212 if((a->n&(a->n-1)) == 0){
217 if((p = realloc(a->a, n*sizeof a->a[0])) == 0){
219 return -1; /* stop iteration */
228 td_get_allthreads(td_thragent_t *ta, td_thrhandle_t **pall)
236 if((e = td_ta_thr_iter(ta, thritercb, &a,
238 TD_THR_LOWEST_PRIORITY,
240 TD_THR_ANY_USER_FLAGS)) != TD_OK){
241 werrstr("%s", terr(e));
255 td_err_e td_ta_map_id2thr(const td_thragent_t *ta_p, thread_t tid,td_thrhandle_t *th_p);
260 threadregs(int tid, Regs **rp)
263 look up tid (td_ta_map_id2thr)
264 create Regs with thr handle inside
265 rw function calls thr_getregs and then
266 pulls out the desired register