]>
Commit | Line | Data |
---|---|---|
ed9a39eb JM |
1 | /* Multi-threaded debugging support for the thread_db interface, |
2 | used on operating systems such as Solaris and Linux. | |
3 | Copyright 1999 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | /* This module implements a thread_stratum target that sits on top of | |
23 | a normal process_stratum target (such as procfs or ptrace). The | |
24 | process_stratum target must install this thread_stratum target when | |
25 | it detects the presence of the thread_db shared library. | |
26 | ||
27 | This module will then use the thread_db API to add thread-awareness | |
28 | to the functionality provided by the process_stratum target (or in | |
29 | some cases, to add user-level thread awareness on top of the | |
30 | kernel-level thread awareness that is already provided by the | |
31 | process_stratum target). | |
32 | ||
33 | Solaris threads (for instance) are a multi-level thread implementation; | |
34 | the kernel provides a Light Weight Process (LWP) which the procfs | |
35 | process_stratum module is aware of. This module must then mediate | |
36 | the relationship between kernel LWP threads and user (eg. posix) | |
37 | threads. | |
38 | ||
39 | Linux threads are likely to be different -- but the thread_db | |
40 | library API should make the difference largely transparent to GDB. | |
41 | ||
42 | */ | |
43 | ||
44 | /* The thread_db API provides a number of functions that give the caller | |
45 | access to the inner workings of the child process's thread library. | |
46 | We will be using the following (others may be added): | |
47 | ||
48 | td_thr_validate Confirm valid "live" thread | |
49 | td_thr_get_info Get info about a thread | |
50 | td_thr_getgregs Get thread's general registers | |
51 | td_thr_getfpregs Get thread's floating point registers | |
52 | td_thr_setgregs Set thread's general registers | |
53 | td_thr_setfpregs Set thread's floating point registers | |
54 | td_ta_map_id2thr Get thread handle from thread id | |
55 | td_ta_map_lwp2thr Get thread handle from LWP id | |
56 | td_ta_thr_iter Iterate over all threads (with callback) | |
57 | ||
58 | In return, the debugger has to provide certain services to the | |
59 | thread_db library. Some of these aren't actually required to do | |
60 | anything in practice. For instance, the thread_db expects to be | |
61 | able to stop the child process and start it again: but in our | |
62 | context, the child process will always be stopped already when we | |
63 | invoke the thread_db library, so the functions that we provide for | |
64 | the library to stop and start the child process are no-ops. | |
65 | ||
66 | Here is the list of functions which we export to the thread_db | |
67 | library, divided into no-op functions vs. functions that actually | |
68 | have to do something: | |
69 | ||
70 | No-op functions: | |
71 | ||
72 | ps_pstop Stop the child process | |
73 | ps_pcontinue Continue the child process | |
74 | ps_lstop Stop a specific LWP (kernel thread) | |
75 | ps_lcontinue Continue an LWP | |
76 | ps_lgetxregsize Get size of LWP's xregs (sparc) | |
77 | ps_lgetxregs Get LWP's xregs (sparc) | |
78 | ps_lsetxregs Set LWP's xregs (sparc) | |
79 | ||
80 | Functions that have to do useful work: | |
81 | ||
82 | ps_pglobal_lookup Get the address of a global symbol | |
83 | ps_pdread Read memory, data segment | |
84 | ps_ptread Read memory, text segment | |
85 | ps_pdwrite Write memory, data segment | |
86 | ps_ptwrite Write memory, text segment | |
87 | ps_lgetregs Get LWP's general registers | |
88 | ps_lgetfpregs Get LWP's floating point registers | |
89 | ps_lsetregs Set LWP's general registers | |
90 | ps_lsetfpregs Set LWP's floating point registers | |
91 | ps_lgetLDT Get LWP's Local Descriptor Table (x86) | |
92 | ||
93 | Thus, if we ask the thread_db library to give us the general registers | |
94 | for user thread X, thread_db may figure out that user thread X is | |
95 | actually mapped onto kernel thread Y. Thread_db does not know how | |
96 | to obtain the registers for kernel thread Y, but GDB does, so thread_db | |
97 | turns the request right back to us via the ps_lgetregs callback. */ | |
98 | ||
99 | #include "defs.h" | |
100 | #include "gdbthread.h" | |
101 | #include "target.h" | |
102 | #include "inferior.h" | |
103 | #include "gdbcmd.h" | |
104 | ||
03f2053f | 105 | #include "gdb_wait.h" |
ed9a39eb JM |
106 | |
107 | #include <time.h> | |
108 | ||
109 | #if defined(USE_PROC_FS) || defined(HAVE_GREGSET_T) | |
110 | #include <sys/procfs.h> | |
111 | #endif | |
112 | ||
113 | #if defined (HAVE_PROC_SERVICE_H) | |
114 | #include <proc_service.h> /* defines incoming API (ps_* callbacks) */ | |
115 | #else | |
116 | #include "gdb_proc_service.h" | |
117 | #endif | |
118 | ||
119 | #if defined HAVE_STDINT_H /* Pre-5.2 systems don't have this header */ | |
120 | #if defined (HAVE_THREAD_DB_H) | |
121 | #include <thread_db.h> /* defines outgoing API (td_thr_* calls) */ | |
122 | #else | |
123 | #include "gdb_thread_db.h" | |
124 | #endif | |
125 | ||
126 | #include <dlfcn.h> /* dynamic library interface */ | |
127 | ||
128 | #ifndef TIDGET | |
129 | #define TIDGET(PID) (((PID) & 0x7fffffff) >> 16) | |
130 | #define PIDGET(PID) (((PID) & 0xffff)) | |
131 | #define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16)) | |
132 | #endif | |
133 | ||
134 | /* Macros for superimposing PID and TID into inferior_pid. */ | |
135 | #define THREAD_FLAG 0x80000000 | |
136 | #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0) | |
137 | #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0) | |
138 | #define GET_LWP(PID) TIDGET (PID) | |
139 | #define GET_THREAD(PID) TIDGET (PID) | |
140 | #define BUILD_LWP(TID, PID) MERGEPID (PID, TID) | |
141 | #define BUILD_THREAD(TID, PID) (MERGEPID (PID, TID) | THREAD_FLAG) | |
142 | ||
143 | /* | |
144 | * target_beneath is a pointer to the target_ops underlying this one. | |
145 | */ | |
146 | ||
147 | static struct target_ops *target_beneath; | |
148 | ||
149 | ||
150 | /* | |
151 | * target vector defined in this module: | |
152 | */ | |
153 | ||
154 | static struct target_ops thread_db_ops; | |
155 | ||
156 | /* | |
157 | * Typedefs required to resolve differences between the thread_db | |
158 | * and proc_service API defined on different versions of Solaris: | |
159 | */ | |
160 | ||
161 | #if defined(PROC_SERVICE_IS_OLD) | |
162 | typedef const struct ps_prochandle *gdb_ps_prochandle_t; | |
163 | typedef char *gdb_ps_read_buf_t; | |
164 | typedef char *gdb_ps_write_buf_t; | |
165 | typedef int gdb_ps_size_t; | |
166 | #else | |
167 | typedef struct ps_prochandle *gdb_ps_prochandle_t; | |
168 | typedef void *gdb_ps_read_buf_t; | |
169 | typedef const void *gdb_ps_write_buf_t; | |
170 | typedef size_t gdb_ps_size_t; | |
171 | #endif | |
172 | ||
173 | /* | |
174 | * proc_service callback functions, called by thread_db. | |
175 | */ | |
176 | ||
177 | ps_err_e | |
178 | ps_pstop (gdb_ps_prochandle_t ph) /* Process stop */ | |
179 | { | |
180 | return PS_OK; | |
181 | } | |
182 | ||
183 | ps_err_e | |
184 | ps_pcontinue (gdb_ps_prochandle_t ph) /* Process continue */ | |
185 | { | |
186 | return PS_OK; | |
187 | } | |
188 | ||
189 | ps_err_e | |
190 | ps_lstop (gdb_ps_prochandle_t ph, /* LWP stop */ | |
191 | lwpid_t lwpid) | |
192 | { | |
193 | return PS_OK; | |
194 | } | |
195 | ||
196 | ps_err_e | |
197 | ps_lcontinue (gdb_ps_prochandle_t ph, /* LWP continue */ | |
198 | lwpid_t lwpid) | |
199 | { | |
200 | return PS_OK; | |
201 | } | |
202 | ||
203 | ps_err_e | |
204 | ps_lgetxregsize (gdb_ps_prochandle_t ph, /* Get XREG size */ | |
205 | lwpid_t lwpid, | |
206 | int *xregsize) | |
207 | { | |
208 | return PS_OK; | |
209 | } | |
210 | ||
211 | ps_err_e | |
212 | ps_lgetxregs (gdb_ps_prochandle_t ph, /* Get XREGS */ | |
213 | lwpid_t lwpid, | |
214 | caddr_t xregset) | |
215 | { | |
216 | return PS_OK; | |
217 | } | |
218 | ||
219 | ps_err_e | |
220 | ps_lsetxregs (gdb_ps_prochandle_t ph, /* Set XREGS */ | |
221 | lwpid_t lwpid, | |
222 | caddr_t xregset) | |
223 | { | |
224 | return PS_OK; | |
225 | } | |
226 | ||
227 | void | |
228 | ps_plog (const char *fmt, ...) | |
229 | { | |
230 | va_list args; | |
231 | ||
232 | va_start (args, fmt); | |
233 | vfprintf_filtered (gdb_stderr, fmt, args); | |
234 | } | |
235 | ||
236 | /* Look up a symbol in GDB's global symbol table. | |
237 | Return the symbol's address. | |
238 | FIXME: it would be more correct to look up the symbol in the context | |
239 | of the LD_OBJECT_NAME provided. However we're probably fairly safe | |
240 | as long as there aren't name conflicts with other libraries. */ | |
241 | ||
242 | ps_err_e | |
243 | ps_pglobal_lookup (gdb_ps_prochandle_t ph, | |
244 | const char *ld_object_name, /* the library name */ | |
245 | const char *ld_symbol_name, /* the symbol name */ | |
246 | paddr_t *ld_symbol_addr) /* return the symbol addr */ | |
247 | { | |
248 | struct minimal_symbol *ms; | |
249 | ||
250 | ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL); | |
251 | ||
252 | if (!ms) | |
253 | return PS_NOSYM; | |
254 | ||
255 | *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms); | |
256 | ||
257 | return PS_OK; | |
258 | } | |
259 | ||
260 | /* Worker function for all memory reads and writes: */ | |
261 | static ps_err_e rw_common (const struct ps_prochandle *ph, | |
262 | paddr_t addr, | |
263 | char *buf, | |
264 | int size, | |
265 | int write_p); | |
266 | ||
267 | /* target_xfer_memory direction consts */ | |
268 | enum {PS_READ = 0, PS_WRITE = 1}; | |
269 | ||
270 | ps_err_e | |
271 | ps_pdread (gdb_ps_prochandle_t ph, /* read from data segment */ | |
272 | paddr_t addr, | |
273 | gdb_ps_read_buf_t buf, | |
274 | gdb_ps_size_t size) | |
275 | { | |
276 | return rw_common (ph, addr, buf, size, PS_READ); | |
277 | } | |
278 | ||
279 | ps_err_e | |
280 | ps_pdwrite (gdb_ps_prochandle_t ph, /* write to data segment */ | |
281 | paddr_t addr, | |
282 | gdb_ps_write_buf_t buf, | |
283 | gdb_ps_size_t size) | |
284 | { | |
285 | return rw_common (ph, addr, (char *) buf, size, PS_WRITE); | |
286 | } | |
287 | ||
288 | ps_err_e | |
289 | ps_ptread (gdb_ps_prochandle_t ph, /* read from text segment */ | |
290 | paddr_t addr, | |
291 | gdb_ps_read_buf_t buf, | |
292 | gdb_ps_size_t size) | |
293 | { | |
294 | return rw_common (ph, addr, buf, size, PS_READ); | |
295 | } | |
296 | ||
297 | ps_err_e | |
298 | ps_ptwrite (gdb_ps_prochandle_t ph, /* write to text segment */ | |
299 | paddr_t addr, | |
300 | gdb_ps_write_buf_t buf, | |
301 | gdb_ps_size_t size) | |
302 | { | |
303 | return rw_common (ph, addr, (char *) buf, size, PS_WRITE); | |
304 | } | |
305 | ||
306 | static struct cleanup *save_inferior_pid (void); | |
307 | static void restore_inferior_pid (void *saved_pid); | |
308 | static char *thr_err_string (td_err_e); | |
309 | static char *thr_state_string (td_thr_state_e); | |
310 | ||
311 | struct ps_prochandle { | |
312 | int pid; | |
313 | }; | |
314 | ||
315 | struct ps_prochandle main_prochandle; | |
316 | td_thragent_t * main_threadagent; | |
317 | ||
318 | /* | |
319 | * Common proc_service routine for reading and writing memory. | |
320 | */ | |
321 | ||
322 | /* FIXME: once we've munged the inferior_pid, why can't we | |
323 | simply call target_read/write_memory and return? */ | |
324 | ||
325 | ||
326 | static ps_err_e | |
327 | rw_common (const struct ps_prochandle *ph, | |
328 | paddr_t addr, | |
329 | char *buf, | |
330 | int size, | |
331 | int write_p) | |
332 | { | |
333 | struct cleanup *old_chain = save_inferior_pid (); | |
334 | int to_do = size; | |
335 | int done = 0; | |
336 | ||
337 | inferior_pid = main_prochandle.pid; | |
338 | ||
339 | while (to_do > 0) | |
340 | { | |
341 | done = current_target.to_xfer_memory (addr, buf, size, write_p, | |
342 | ¤t_target); | |
343 | if (done <= 0) | |
344 | { | |
345 | if (write_p == PS_READ) | |
346 | print_sys_errmsg ("rw_common (): read", errno); | |
347 | else | |
348 | print_sys_errmsg ("rw_common (): write", errno); | |
349 | ||
350 | return PS_ERR; | |
351 | } | |
352 | to_do -= done; | |
353 | buf += done; | |
354 | } | |
355 | do_cleanups (old_chain); | |
356 | return PS_OK; | |
357 | } | |
358 | ||
359 | /* Cleanup functions used by the register callbacks | |
360 | (which have to manipulate the global inferior_pid). */ | |
361 | ||
362 | ps_err_e | |
363 | ps_lgetregs (gdb_ps_prochandle_t ph, /* Get LWP general regs */ | |
364 | lwpid_t lwpid, | |
365 | prgregset_t gregset) | |
366 | { | |
367 | struct cleanup *old_chain = save_inferior_pid (); | |
368 | ||
369 | inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid); | |
370 | current_target.to_fetch_registers (-1); | |
371 | ||
372 | fill_gregset (gregset, -1); | |
373 | do_cleanups (old_chain); | |
374 | ||
375 | return PS_OK; | |
376 | } | |
377 | ||
378 | ps_err_e | |
379 | ps_lsetregs (gdb_ps_prochandle_t ph, /* Set LWP general regs */ | |
380 | lwpid_t lwpid, | |
381 | const prgregset_t gregset) | |
382 | { | |
383 | struct cleanup *old_chain = save_inferior_pid (); | |
384 | ||
385 | inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid); | |
386 | supply_gregset (gregset); | |
387 | current_target.to_store_registers (-1); | |
388 | do_cleanups (old_chain); | |
389 | return PS_OK; | |
390 | } | |
391 | ||
392 | ps_err_e | |
393 | ps_lgetfpregs (gdb_ps_prochandle_t ph, /* Get LWP float regs */ | |
394 | lwpid_t lwpid, | |
395 | prfpregset_t *fpregset) | |
396 | { | |
397 | struct cleanup *old_chain = save_inferior_pid (); | |
398 | ||
399 | inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid); | |
400 | current_target.to_fetch_registers (-1); | |
401 | fill_fpregset (fpregset, -1); | |
402 | do_cleanups (old_chain); | |
403 | return PS_OK; | |
404 | } | |
405 | ||
406 | ps_err_e | |
407 | ps_lsetfpregs (gdb_ps_prochandle_t ph, /* Set LWP float regs */ | |
408 | lwpid_t lwpid, | |
409 | const prfpregset_t *fpregset) | |
410 | { | |
411 | struct cleanup *old_chain = save_inferior_pid (); | |
412 | ||
413 | inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid); | |
414 | supply_fpregset (fpregset); | |
415 | current_target.to_store_registers (-1); | |
416 | do_cleanups (old_chain); | |
417 | return PS_OK; | |
418 | } | |
419 | ||
420 | /* | |
421 | * ps_getpid | |
422 | * | |
423 | * return the main pid for the child process | |
424 | * (special for Linux -- not used on Solaris) | |
425 | */ | |
426 | ||
427 | pid_t | |
428 | ps_getpid (gdb_ps_prochandle_t ph) | |
429 | { | |
430 | return ph->pid; | |
431 | } | |
432 | ||
433 | #ifdef TM_I386SOL2_H | |
434 | ||
435 | /* Reads the local descriptor table of a LWP. */ | |
436 | ||
437 | ps_err_e | |
438 | ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid, | |
439 | struct ssd *pldt) | |
440 | { | |
441 | /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */ | |
442 | extern struct ssd *procfs_find_LDT_entry (int); | |
443 | struct ssd *ret; | |
444 | ||
445 | ret = procfs_find_LDT_entry (BUILD_LWP (lwpid, | |
446 | PIDGET (main_prochandle.pid))); | |
447 | if (ret) | |
448 | { | |
449 | memcpy (pldt, ret, sizeof (struct ssd)); | |
450 | return PS_OK; | |
451 | } | |
452 | else /* LDT not found. */ | |
453 | return PS_ERR; | |
454 | } | |
455 | #endif /* TM_I386SOL2_H */ | |
456 | ||
457 | /* | |
458 | * Pointers to thread_db functions: | |
459 | * | |
460 | * These are a dynamic library mechanism. | |
461 | * The dlfcn.h interface will be used to initialize these | |
462 | * so that they point to the appropriate functions in the | |
463 | * thread_db dynamic library. This is done dynamically | |
464 | * so that GDB can still run on systems that lack thread_db. | |
465 | */ | |
466 | ||
467 | static td_err_e (*p_td_init) (void); | |
468 | ||
469 | static td_err_e (*p_td_ta_new) (const struct ps_prochandle *ph_p, | |
470 | td_thragent_t **ta_pp); | |
471 | ||
472 | static td_err_e (*p_td_ta_delete) (td_thragent_t *ta_p); | |
473 | ||
474 | static td_err_e (*p_td_ta_get_nthreads) (const td_thragent_t *ta_p, | |
475 | int *nthread_p); | |
476 | ||
477 | ||
478 | static td_err_e (*p_td_ta_thr_iter) (const td_thragent_t *ta_p, | |
479 | td_thr_iter_f *cb, | |
480 | void *cbdata_p, | |
481 | td_thr_state_e state, | |
482 | int ti_pri, | |
483 | sigset_t *ti_sigmask_p, | |
484 | unsigned ti_user_flags); | |
485 | ||
486 | static td_err_e (*p_td_ta_event_addr) (const td_thragent_t *ta_p, | |
487 | u_long event, | |
488 | td_notify_t *notify_p); | |
489 | ||
490 | static td_err_e (*p_td_ta_event_getmsg) (const td_thragent_t *ta_p, | |
491 | td_event_msg_t *msg); | |
492 | ||
493 | static td_err_e (*p_td_ta_set_event) (const td_thragent_t *ta_p, | |
494 | td_thr_events_t *events); | |
495 | ||
496 | static td_err_e (*p_td_thr_validate) (const td_thrhandle_t *th_p); | |
497 | ||
498 | static td_err_e (*p_td_thr_event_enable) (const td_thrhandle_t *th_p, | |
499 | int on_off); | |
500 | ||
501 | static td_err_e (*p_td_thr_get_info) (const td_thrhandle_t *th_p, | |
502 | td_thrinfo_t *ti_p); | |
503 | ||
504 | static td_err_e (*p_td_thr_getgregs) (const td_thrhandle_t *th_p, | |
505 | prgregset_t regset); | |
506 | ||
507 | static td_err_e (*p_td_thr_setgregs) (const td_thrhandle_t *th_p, | |
508 | const prgregset_t regset); | |
509 | ||
510 | static td_err_e (*p_td_thr_getfpregs) (const td_thrhandle_t *th_p, | |
511 | prfpregset_t *fpregset); | |
512 | ||
513 | static td_err_e (*p_td_thr_setfpregs) (const td_thrhandle_t *th_p, | |
514 | const prfpregset_t *fpregset); | |
515 | ||
516 | static td_err_e (*p_td_ta_map_id2thr) (const td_thragent_t *ta_p, | |
517 | thread_t tid, | |
518 | td_thrhandle_t *th_p); | |
519 | ||
520 | static td_err_e (*p_td_ta_map_lwp2thr) (const td_thragent_t *ta_p, | |
521 | lwpid_t lwpid, | |
522 | td_thrhandle_t *th_p); | |
523 | ||
524 | /* | |
525 | * API and target vector initialization function: thread_db_initialize. | |
526 | * | |
527 | * NOTE: this function is deliberately NOT named with the GDB convention | |
528 | * of module initializer function names that begin with "_initialize". | |
529 | * This module is NOT intended to be auto-initialized at GDB startup. | |
530 | * Rather, it will only be initialized when a multi-threaded child | |
531 | * process is detected. | |
532 | * | |
533 | */ | |
534 | ||
535 | /* | |
536 | * Initializer for thread_db library interface. | |
537 | * This function does the dynamic library stuff (dlopen, dlsym), | |
538 | * and then calls the thread_db library's one-time initializer | |
539 | * function (td_init). If everything succeeds, this function | |
540 | * returns true; otherwise it returns false, and this module | |
541 | * cannot be used. | |
542 | */ | |
543 | ||
544 | static int | |
545 | init_thread_db_library () | |
546 | { | |
547 | void *dlhandle; | |
548 | td_err_e ret; | |
549 | ||
550 | /* Open a handle to the "thread_db" dynamic library. */ | |
551 | if ((dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW)) == NULL) | |
552 | return 0; /* fail */ | |
553 | ||
554 | /* Initialize pointers to the dynamic library functions we will use. | |
555 | * Note that we are not calling the functions here -- we are only | |
556 | * establishing pointers to them. | |
557 | */ | |
558 | ||
559 | /* td_init: initialize thread_db library. */ | |
560 | if ((p_td_init = dlsym (dlhandle, "td_init")) == NULL) | |
561 | return 0; /* fail */ | |
562 | /* td_ta_new: register a target process with thread_db. */ | |
563 | if ((p_td_ta_new = dlsym (dlhandle, "td_ta_new")) == NULL) | |
564 | return 0; /* fail */ | |
565 | /* td_ta_delete: un-register a target process with thread_db. */ | |
566 | if ((p_td_ta_delete = dlsym (dlhandle, "td_ta_delete")) == NULL) | |
567 | return 0; /* fail */ | |
568 | ||
569 | /* td_ta_map_id2thr: get thread handle from thread id. */ | |
570 | if ((p_td_ta_map_id2thr = dlsym (dlhandle, "td_ta_map_id2thr")) == NULL) | |
571 | return 0; /* fail */ | |
572 | /* td_ta_map_lwp2thr: get thread handle from lwp id. */ | |
573 | if ((p_td_ta_map_lwp2thr = dlsym (dlhandle, "td_ta_map_lwp2thr")) == NULL) | |
574 | return 0; /* fail */ | |
575 | /* td_ta_get_nthreads: get number of threads in target process. */ | |
576 | if ((p_td_ta_get_nthreads = dlsym (dlhandle, "td_ta_get_nthreads")) == NULL) | |
577 | return 0; /* fail */ | |
578 | /* td_ta_thr_iter: iterate over all thread handles. */ | |
579 | if ((p_td_ta_thr_iter = dlsym (dlhandle, "td_ta_thr_iter")) == NULL) | |
580 | return 0; /* fail */ | |
581 | ||
582 | /* td_thr_validate: make sure a thread handle is real and alive. */ | |
583 | if ((p_td_thr_validate = dlsym (dlhandle, "td_thr_validate")) == NULL) | |
584 | return 0; /* fail */ | |
585 | /* td_thr_get_info: get a bunch of info about a thread. */ | |
586 | if ((p_td_thr_get_info = dlsym (dlhandle, "td_thr_get_info")) == NULL) | |
587 | return 0; /* fail */ | |
588 | /* td_thr_getgregs: get general registers for thread. */ | |
589 | if ((p_td_thr_getgregs = dlsym (dlhandle, "td_thr_getgregs")) == NULL) | |
590 | return 0; /* fail */ | |
591 | /* td_thr_setgregs: set general registers for thread. */ | |
592 | if ((p_td_thr_setgregs = dlsym (dlhandle, "td_thr_setgregs")) == NULL) | |
593 | return 0; /* fail */ | |
594 | /* td_thr_getfpregs: get floating point registers for thread. */ | |
595 | if ((p_td_thr_getfpregs = dlsym (dlhandle, "td_thr_getfpregs")) == NULL) | |
596 | return 0; /* fail */ | |
597 | /* td_thr_setfpregs: set floating point registers for thread. */ | |
598 | if ((p_td_thr_setfpregs = dlsym (dlhandle, "td_thr_setfpregs")) == NULL) | |
599 | return 0; /* fail */ | |
600 | ||
601 | ret = p_td_init (); | |
602 | if (ret != TD_OK) | |
603 | { | |
604 | warning ("init_thread_db: td_init: %s", thr_err_string (ret)); | |
605 | return 0; | |
606 | } | |
607 | ||
608 | /* Optional functions: | |
609 | We can still debug even if the following functions are not found. */ | |
610 | ||
611 | /* td_ta_event_addr: get the breakpoint address for specified event. */ | |
612 | p_td_ta_event_addr = dlsym (dlhandle, "td_ta_event_addr"); | |
613 | ||
614 | /* td_ta_event_getmsg: get the next event message for the process. */ | |
615 | p_td_ta_event_getmsg = dlsym (dlhandle, "td_ta_event_getmsg"); | |
616 | ||
617 | /* td_ta_set_event: request notification of an event. */ | |
618 | p_td_ta_set_event = dlsym (dlhandle, "td_ta_set_event"); | |
619 | ||
620 | /* td_thr_event_enable: enable event reporting in a thread. */ | |
621 | p_td_thr_event_enable = dlsym (dlhandle, "td_thr_event_enable"); | |
622 | ||
623 | return 1; /* success */ | |
624 | } | |
625 | ||
626 | /* | |
627 | * Local utility functions: | |
628 | */ | |
629 | ||
630 | ||
631 | /* | |
632 | ||
633 | LOCAL FUNCTION | |
634 | ||
635 | save_inferior_pid - Save inferior_pid on the cleanup list | |
636 | restore_inferior_pid - Restore inferior_pid from the cleanup list | |
637 | ||
638 | SYNOPSIS | |
639 | ||
640 | struct cleanup *save_inferior_pid (void); | |
641 | void restore_inferior_pid (void *saved_pid); | |
642 | ||
643 | DESCRIPTION | |
644 | ||
645 | These two functions act in unison to restore inferior_pid in | |
646 | case of an error. | |
647 | ||
648 | NOTES | |
649 | ||
650 | inferior_pid is a global variable that needs to be changed by many | |
651 | of these routines before calling functions in procfs.c. In order | |
652 | to guarantee that inferior_pid gets restored (in case of errors), | |
653 | you need to call save_inferior_pid before changing it. At the end | |
654 | of the function, you should invoke do_cleanups to restore it. | |
655 | ||
656 | */ | |
657 | ||
658 | static struct cleanup * | |
659 | save_inferior_pid (void) | |
660 | { | |
661 | #if TARGET_PTR_BIT > TARGET_INT_BIT | |
662 | return make_cleanup (restore_inferior_pid, (void *) ((long) inferior_pid)); | |
663 | #else | |
664 | return make_cleanup (restore_inferior_pid, (void *) inferior_pid); | |
665 | #endif | |
666 | } | |
667 | ||
668 | static void | |
669 | restore_inferior_pid (void *saved_pid) | |
670 | { | |
671 | #if TARGET_PTR_BIT > TARGET_INT_BIT | |
672 | inferior_pid = (int) ((long) saved_pid); | |
673 | #else | |
674 | inferior_pid = (int) saved_pid; | |
675 | #endif | |
676 | } | |
677 | ||
678 | /* | |
679 | ||
680 | LOCAL FUNCTION | |
681 | ||
682 | thr_err_string - Convert a thread_db error code to a string | |
683 | ||
684 | SYNOPSIS | |
685 | ||
686 | char * thr_err_string (errcode) | |
687 | ||
688 | DESCRIPTION | |
689 | ||
690 | Return a string description of the thread_db errcode. If errcode | |
691 | is unknown, then return an <unknown> message. | |
692 | ||
693 | */ | |
694 | ||
695 | static char * | |
696 | thr_err_string (errcode) | |
697 | td_err_e errcode; | |
698 | { | |
699 | static char buf[50]; | |
700 | ||
701 | switch (errcode) { | |
702 | case TD_OK: return "generic 'call succeeded'"; | |
703 | case TD_ERR: return "generic error"; | |
704 | case TD_NOTHR: return "no thread to satisfy query"; | |
705 | case TD_NOSV: return "no sync handle to satisfy query"; | |
706 | case TD_NOLWP: return "no lwp to satisfy query"; | |
707 | case TD_BADPH: return "invalid process handle"; | |
708 | case TD_BADTH: return "invalid thread handle"; | |
709 | case TD_BADSH: return "invalid synchronization handle"; | |
710 | case TD_BADTA: return "invalid thread agent"; | |
711 | case TD_BADKEY: return "invalid key"; | |
712 | case TD_NOMSG: return "no event message for getmsg"; | |
713 | case TD_NOFPREGS: return "FPU register set not available"; | |
714 | case TD_NOLIBTHREAD: return "application not linked with libthread"; | |
715 | case TD_NOEVENT: return "requested event is not supported"; | |
716 | case TD_NOCAPAB: return "capability not available"; | |
717 | case TD_DBERR: return "debugger service failed"; | |
718 | case TD_NOAPLIC: return "operation not applicable to"; | |
719 | case TD_NOTSD: return "no thread-specific data for this thread"; | |
720 | case TD_MALLOC: return "malloc failed"; | |
721 | case TD_PARTIALREG: return "only part of register set was written/read"; | |
722 | case TD_NOXREGS: return "X register set not available for this thread"; | |
723 | default: | |
724 | sprintf (buf, "unknown thread_db error '%d'", errcode); | |
725 | return buf; | |
726 | } | |
727 | } | |
728 | ||
729 | /* | |
730 | ||
731 | LOCAL FUNCTION | |
732 | ||
733 | thr_state_string - Convert a thread_db state code to a string | |
734 | ||
735 | SYNOPSIS | |
736 | ||
737 | char *thr_state_string (statecode) | |
738 | ||
739 | DESCRIPTION | |
740 | ||
741 | Return the thread_db state string associated with statecode. | |
742 | If statecode is unknown, then return an <unknown> message. | |
743 | ||
744 | */ | |
745 | ||
746 | static char * | |
747 | thr_state_string (statecode) | |
748 | td_thr_state_e statecode; | |
749 | { | |
750 | static char buf[50]; | |
751 | ||
752 | switch (statecode) { | |
753 | case TD_THR_STOPPED: return "stopped by debugger"; | |
754 | case TD_THR_RUN: return "runnable"; | |
755 | case TD_THR_ACTIVE: return "active"; | |
756 | case TD_THR_ZOMBIE: return "zombie"; | |
757 | case TD_THR_SLEEP: return "sleeping"; | |
758 | case TD_THR_STOPPED_ASLEEP: return "stopped by debugger AND blocked"; | |
759 | default: | |
760 | sprintf (buf, "unknown thread_db state %d", statecode); | |
761 | return buf; | |
762 | } | |
763 | } | |
764 | ||
765 | /* | |
766 | * Local thread/event list. | |
767 | * This data structure will be used to hold a list of threads and | |
768 | * pending/deliverable events. | |
769 | */ | |
770 | ||
771 | typedef struct THREADINFO { | |
772 | thread_t tid; /* thread ID */ | |
773 | pid_t lid; /* process/lwp ID */ | |
774 | td_thr_state_e state; /* thread state (a la thread_db) */ | |
775 | td_thr_type_e type; /* thread type (a la thread_db) */ | |
776 | int pending; /* true if holding a pending event */ | |
777 | int status; /* wait status of any interesting event */ | |
778 | } threadinfo; | |
779 | ||
780 | threadinfo * threadlist; | |
781 | int threadlist_max = 0; /* current size of table */ | |
782 | int threadlist_top = 0; /* number of threads now in table */ | |
783 | #define THREADLIST_ALLOC 100 /* chunk size by which to expand table */ | |
784 | ||
785 | static threadinfo * | |
786 | insert_thread (tid, lid, state, type) | |
787 | int tid; | |
788 | int lid; | |
789 | td_thr_state_e state; | |
790 | td_thr_type_e type; | |
791 | { | |
792 | if (threadlist_top >= threadlist_max) | |
793 | { | |
794 | threadlist_max += THREADLIST_ALLOC; | |
795 | threadlist = realloc (threadlist, | |
796 | threadlist_max * sizeof (threadinfo)); | |
797 | if (threadlist == NULL) | |
798 | return NULL; | |
799 | } | |
800 | threadlist[threadlist_top].tid = tid; | |
801 | threadlist[threadlist_top].lid = lid; | |
802 | threadlist[threadlist_top].state = state; | |
803 | threadlist[threadlist_top].type = type; | |
804 | threadlist[threadlist_top].pending = 0; | |
805 | threadlist[threadlist_top].status = 0; | |
806 | ||
807 | return &threadlist[threadlist_top++]; | |
808 | } | |
809 | ||
810 | static void | |
811 | empty_threadlist () | |
812 | { | |
813 | threadlist_top = 0; | |
814 | } | |
815 | ||
816 | static threadinfo * | |
817 | next_pending_event () | |
818 | { | |
819 | int i; | |
820 | ||
821 | for (i = 0; i < threadlist_top; i++) | |
822 | if (threadlist[i].pending) | |
823 | return &threadlist[i]; | |
824 | ||
825 | return NULL; | |
826 | } | |
827 | ||
828 | static void | |
829 | threadlist_iter (func, data, state, type) | |
830 | int (*func) (); | |
831 | void *data; | |
832 | td_thr_state_e state; | |
833 | td_thr_type_e type; | |
834 | { | |
835 | int i; | |
836 | ||
837 | for (i = 0; i < threadlist_top; i++) | |
838 | if ((state == TD_THR_ANY_STATE || state == threadlist[i].state) && | |
839 | (type == TD_THR_ANY_TYPE || type == threadlist[i].type)) | |
840 | if ((*func) (&threadlist[i], data) != 0) | |
841 | break; | |
842 | ||
843 | return; | |
844 | } | |
845 | ||
846 | /* | |
847 | * Global state | |
848 | * | |
849 | * Here we keep state information all collected in one place. | |
850 | */ | |
851 | ||
852 | /* This flag is set when we activate, so that we don't do it twice. | |
853 | Defined in linux-thread.c and used for inter-target syncronization. */ | |
854 | extern int using_thread_db; | |
855 | ||
856 | /* The process id for which we've stopped. | |
857 | * This is only set when we actually stop all threads. | |
858 | * Otherwise it's zero. | |
859 | */ | |
860 | static int event_pid; | |
861 | ||
862 | /* | |
863 | * The process id for a new thread to which we've just attached. | |
864 | * This process needs special handling at resume time. | |
865 | */ | |
866 | static int attach_pid; | |
867 | ||
868 | ||
869 | /* | |
870 | * thread_db event handling: | |
871 | * | |
872 | * The mechanism for event notification via the thread_db API. | |
873 | * These events are implemented as breakpoints. The thread_db | |
874 | * library gives us an address where we can set a breakpoint. | |
875 | * When the breakpoint is hit, it represents an event of interest | |
876 | * such as: | |
877 | * Thread creation | |
878 | * Thread death | |
879 | * Thread reap | |
880 | */ | |
881 | ||
882 | /* Location of the thread creation event breakpoint. The code at this | |
883 | location in the child process will be called by the pthread library | |
884 | whenever a new thread is created. By setting a special breakpoint | |
885 | at this location, GDB can detect when a new thread is created. We | |
886 | obtain this location via the td_ta_event_addr call. */ | |
887 | ||
888 | static CORE_ADDR thread_creation_bkpt_address; | |
889 | ||
890 | /* Location of the thread death event breakpoint. The code at this | |
891 | location in the child process will be called by the pthread library | |
892 | whenever a thread is destroyed. By setting a special breakpoint at | |
893 | this location, GDB can detect when a new thread is created. We | |
894 | obtain this location via the td_ta_event_addr call. */ | |
895 | ||
896 | static CORE_ADDR thread_death_bkpt_address; | |
897 | ||
898 | /* This function handles the global parts of enabling thread events. | |
899 | The thread-specific enabling is handled per-thread elsewhere. */ | |
900 | ||
901 | static void | |
902 | enable_thread_event_reporting (ta) | |
903 | td_thragent_t *ta; | |
904 | { | |
905 | td_thr_events_t events; | |
906 | td_notify_t notify; | |
907 | CORE_ADDR addr; | |
908 | ||
909 | if (p_td_ta_set_event == NULL || | |
910 | p_td_ta_event_addr == NULL || | |
911 | p_td_ta_event_getmsg == NULL || | |
912 | p_td_thr_event_enable == NULL) | |
913 | return; /* can't do thread event reporting without these funcs */ | |
914 | ||
915 | /* set process wide mask saying which events we are interested in */ | |
916 | td_event_emptyset (&events); | |
917 | td_event_addset (&events, TD_CREATE); | |
918 | td_event_addset (&events, TD_DEATH); | |
919 | ||
920 | if (p_td_ta_set_event (ta, &events) != TD_OK) | |
921 | { | |
922 | warning ("unable to set global thread event mask"); | |
923 | return; | |
924 | } | |
925 | ||
926 | /* Delete previous thread event breakpoints, if any. */ | |
927 | remove_thread_event_breakpoints (); | |
928 | ||
929 | /* create breakpoints -- thread creation and death */ | |
930 | /* thread creation */ | |
931 | /* get breakpoint location */ | |
932 | if (p_td_ta_event_addr (ta, TD_CREATE, ¬ify) != TD_OK) | |
933 | { | |
934 | warning ("unable to get location for thread creation breakpoint"); | |
935 | return; | |
936 | } | |
937 | ||
938 | /* Set up the breakpoint. */ | |
939 | create_thread_event_breakpoint (notify.u.bptaddr); | |
940 | ||
941 | /* Save it's location. */ | |
942 | thread_creation_bkpt_address = notify.u.bptaddr; | |
943 | ||
944 | /* thread death */ | |
945 | /* get breakpoint location */ | |
946 | if (p_td_ta_event_addr (ta, TD_DEATH, ¬ify) != TD_OK) | |
947 | { | |
948 | warning ("unable to get location for thread death breakpoint"); | |
949 | return; | |
950 | } | |
951 | /* Set up the breakpoint. */ | |
952 | create_thread_event_breakpoint (notify.u.bptaddr); | |
953 | ||
954 | /* Save it's location. */ | |
955 | thread_death_bkpt_address = notify.u.bptaddr; | |
956 | } | |
957 | ||
958 | /* This function handles the global parts of disabling thread events. | |
959 | The thread-specific enabling is handled per-thread elsewhere. */ | |
960 | ||
961 | static void | |
962 | disable_thread_event_reporting (ta) | |
963 | td_thragent_t *ta; | |
964 | { | |
965 | td_thr_events_t events; | |
966 | ||
967 | /* set process wide mask saying we aren't interested in any events */ | |
968 | td_event_emptyset (&events); | |
969 | p_td_ta_set_event (main_threadagent, &events); | |
970 | ||
971 | /* Delete thread event breakpoints, if any. */ | |
972 | remove_thread_event_breakpoints (); | |
973 | thread_creation_bkpt_address = 0; | |
974 | thread_death_bkpt_address = 0; | |
975 | } | |
976 | ||
977 | /* check_for_thread_event | |
978 | ||
979 | if it's a thread event we recognize (currently | |
980 | we only recognize creation and destruction | |
981 | events), return 1; else return 0. */ | |
982 | ||
983 | ||
984 | static int | |
985 | check_for_thread_event (struct target_waitstatus *tws, int event_pid) | |
986 | { | |
987 | /* FIXME: to be more efficient, we should keep a static | |
988 | list of threads, and update it only here (with td_ta_thr_iter). */ | |
989 | } | |
990 | ||
991 | static void | |
992 | thread_db_push_target (void) | |
993 | { | |
994 | /* Called ONLY from thread_db_new_objfile after td_ta_new call succeeds. */ | |
995 | ||
996 | /* Push this target vector */ | |
997 | push_target (&thread_db_ops); | |
998 | /* Find the underlying process-layer target for calling later. */ | |
999 | target_beneath = find_target_beneath (&thread_db_ops); | |
1000 | using_thread_db = 1; | |
1001 | /* Turn on thread_db event-reporting API. */ | |
1002 | enable_thread_event_reporting (main_threadagent); | |
1003 | } | |
1004 | ||
1005 | static void | |
1006 | thread_db_unpush_target (void) | |
1007 | { | |
1008 | /* Must be called whenever we remove ourself from the target stack! */ | |
1009 | ||
1010 | using_thread_db = 0; | |
1011 | target_beneath = NULL; | |
1012 | ||
1013 | /* delete local list of threads */ | |
1014 | empty_threadlist (); | |
1015 | /* Turn off the thread_db API. */ | |
1016 | p_td_ta_delete (main_threadagent); | |
1017 | /* Unpush this target vector */ | |
1018 | unpush_target (&thread_db_ops); | |
1019 | /* Reset linuxthreads module. */ | |
1020 | linuxthreads_discard_global_state (); | |
1021 | } | |
1022 | ||
1023 | /* | |
1024 | * New objfile hook function: | |
1025 | * Called for each new objfile (image, shared lib) in the target process. | |
1026 | * | |
1027 | * The purpose of this function is to detect that the target process | |
1028 | * is linked with the (appropriate) thread library. So every time a | |
1029 | * new target shared library is detected, we will call td_ta_new. | |
1030 | * If it succeeds, we know we have a multi-threaded target process | |
1031 | * that we can debug using the thread_db API. | |
1032 | */ | |
1033 | ||
1034 | /* | |
1035 | * new_objfile function: | |
1036 | * | |
1037 | * connected to target_new_objfile_hook, this function gets called | |
1038 | * every time a new binary image is loaded. | |
1039 | * | |
1040 | * At each call, we attempt to open the thread_db connection to the | |
1041 | * child process. If it succeeds, we know we have a libthread process | |
1042 | * and we can debug it with this target vector. Therefore we push | |
1043 | * ourself onto the target stack. | |
1044 | */ | |
1045 | ||
1046 | static void (*target_new_objfile_chain) (struct objfile *objfile); | |
1047 | static int stop_or_attach_thread_callback (const td_thrhandle_t *th, | |
1048 | void *data); | |
1049 | static int wait_thread_callback (const td_thrhandle_t *th, | |
1050 | void *data); | |
1051 | ||
1052 | static void | |
1053 | thread_db_new_objfile (struct objfile *objfile) | |
1054 | { | |
1055 | td_err_e ret; | |
1056 | ||
1057 | if (using_thread_db) /* libthread already detected, and */ | |
1058 | goto quit; /* thread target vector activated. */ | |
1059 | ||
1060 | if (objfile == NULL) | |
1061 | goto quit; /* un-interesting object file */ | |
1062 | ||
1063 | /* Initialize our "main prochandle" with the main inferior pid. */ | |
1064 | main_prochandle.pid = PIDGET (inferior_pid); | |
1065 | ||
1066 | /* Now attempt to open a thread_db connection to the | |
1067 | thread library running in the child process. */ | |
1068 | ret = p_td_ta_new (&main_prochandle, &main_threadagent); | |
1069 | switch (ret) { | |
1070 | default: | |
1071 | warning ("Unexpected error initializing thread_db: %s", | |
1072 | thr_err_string (ret)); | |
1073 | break; | |
1074 | case TD_NOLIBTHREAD: /* expected: no libthread in child process (yet) */ | |
1075 | break; | |
1076 | case TD_OK: /* libthread detected in child: we go live now! */ | |
1077 | thread_db_push_target (); | |
1078 | event_pid = inferior_pid; /* for resume */ | |
1079 | ||
1080 | /* Now stop everyone else, and attach any new threads you find. */ | |
1081 | p_td_ta_thr_iter (main_threadagent, | |
1082 | stop_or_attach_thread_callback, | |
1083 | (void *) 0, | |
1084 | TD_THR_ANY_STATE, | |
1085 | TD_THR_LOWEST_PRIORITY, | |
1086 | TD_SIGNO_MASK, | |
1087 | TD_THR_ANY_USER_FLAGS); | |
1088 | ||
1089 | /* Now go call wait on all the threads you've stopped: | |
1090 | This allows us to absorb the SIGKILL event, and to make sure | |
1091 | that the thread knows that it is stopped (Linux peculiarity). */ | |
1092 | p_td_ta_thr_iter (main_threadagent, | |
1093 | wait_thread_callback, | |
1094 | (void *) 0, | |
1095 | TD_THR_ANY_STATE, | |
1096 | TD_THR_LOWEST_PRIORITY, | |
1097 | TD_SIGNO_MASK, | |
1098 | TD_THR_ANY_USER_FLAGS); | |
1099 | ||
1100 | break; | |
1101 | } | |
1102 | quit: | |
1103 | if (target_new_objfile_chain) | |
1104 | target_new_objfile_chain (objfile); | |
1105 | } | |
1106 | ||
1107 | ||
1108 | /* | |
1109 | ||
1110 | LOCAL FUNCTION | |
1111 | ||
1112 | thread_db_alive - test thread for "aliveness" | |
1113 | ||
1114 | SYNOPSIS | |
1115 | ||
1116 | static bool thread_db_alive (int pid); | |
1117 | ||
1118 | DESCRIPTION | |
1119 | ||
1120 | returns true if thread still active in inferior. | |
1121 | ||
1122 | */ | |
1123 | ||
1124 | static int | |
1125 | thread_db_alive (pid) | |
1126 | int pid; | |
1127 | { | |
1128 | if (is_thread (pid)) /* user-space (non-kernel) thread */ | |
1129 | { | |
1130 | td_thrhandle_t th; | |
1131 | td_err_e ret; | |
1132 | ||
1133 | pid = GET_THREAD (pid); | |
1134 | if ((ret = p_td_ta_map_id2thr (main_threadagent, pid, &th)) != TD_OK) | |
1135 | return 0; /* thread not found */ | |
1136 | if ((ret = p_td_thr_validate (&th)) != TD_OK) | |
1137 | return 0; /* thread not valid */ | |
1138 | return 1; /* known thread: return true */ | |
1139 | } | |
1140 | else if (target_beneath->to_thread_alive) | |
1141 | return target_beneath->to_thread_alive (pid); | |
1142 | else | |
1143 | return 0; /* default to "not alive" (shouldn't happen anyway) */ | |
1144 | } | |
1145 | ||
1146 | /* | |
1147 | * get_lwp_from_thread_handle | |
1148 | */ | |
1149 | ||
1150 | static int /* lwpid_t or pid_t */ | |
1151 | get_lwp_from_thread_handle (th) | |
1152 | td_thrhandle_t *th; | |
1153 | { | |
1154 | td_thrinfo_t ti; | |
1155 | td_err_e ret; | |
1156 | ||
1157 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1158 | error ("get_lwp_from_thread_handle: thr_get_info failed: %s", | |
1159 | thr_err_string (ret)); | |
1160 | ||
1161 | return ti.ti_lid; | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * get_lwp_from_thread_id | |
1166 | */ | |
1167 | ||
1168 | static int /* lwpid_t or pid_t */ | |
1169 | get_lwp_from_thread_id (tid) | |
1170 | int tid; /* thread_t? */ | |
1171 | { | |
1172 | td_thrhandle_t th; | |
1173 | td_err_e ret; | |
1174 | ||
1175 | if ((ret = p_td_ta_map_id2thr (main_threadagent, tid, &th)) != TD_OK) | |
1176 | error ("get_lwp_from_thread_id: map_id2thr failed: %s", | |
1177 | thr_err_string (ret)); | |
1178 | ||
1179 | return get_lwp_from_thread_handle (&th); | |
1180 | } | |
1181 | ||
1182 | /* | |
1183 | * pid_to_str has to handle user-space threads. | |
1184 | * If not a user-space thread, then pass the request on to the | |
1185 | * underlying stratum if it can handle it: else call normal_pid_to_str. | |
1186 | */ | |
1187 | ||
1188 | static char * | |
1189 | thread_db_pid_to_str (int pid) | |
1190 | { | |
1191 | static char buf[100]; | |
1192 | td_thrhandle_t th; | |
1193 | td_thrinfo_t ti; | |
1194 | td_err_e ret; | |
1195 | ||
1196 | if (is_thread (pid)) | |
1197 | { | |
1198 | if ((ret = p_td_ta_map_id2thr (main_threadagent, | |
1199 | GET_THREAD (pid), | |
1200 | &th)) != TD_OK) | |
1201 | error ("thread_db: map_id2thr failed: %s", thr_err_string (ret)); | |
1202 | ||
1203 | if ((ret = p_td_thr_get_info (&th, &ti)) != TD_OK) | |
1204 | error ("thread_db: thr_get_info failed: %s", thr_err_string (ret)); | |
1205 | ||
1206 | if (ti.ti_state == TD_THR_ACTIVE && | |
1207 | ti.ti_lid != 0) | |
1208 | sprintf (buf, "Thread %d (LWP %d)", ti.ti_tid, ti.ti_lid); | |
1209 | else | |
1210 | sprintf (buf, "Thread %d (%s)", ti.ti_tid, | |
1211 | thr_state_string (ti.ti_state)); | |
1212 | } | |
1213 | else if (GET_LWP (pid)) | |
1214 | sprintf (buf, "LWP %d", GET_LWP (pid)); | |
1215 | else return normal_pid_to_str (pid); | |
1216 | ||
1217 | return buf; | |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * thread_db target vector functions: | |
1222 | */ | |
1223 | ||
1224 | static void | |
1225 | thread_db_files_info (struct target_ops *tgt_vector) | |
1226 | { | |
1227 | /* This function will be unnecessary in real life. */ | |
1228 | printf_filtered ("thread_db stratum:\n"); | |
1229 | target_beneath->to_files_info (tgt_vector); | |
1230 | } | |
1231 | ||
1232 | /* | |
1233 | * xfer_memory has to munge the inferior_pid before passing the call | |
1234 | * down to the target layer. | |
1235 | */ | |
1236 | ||
1237 | static int | |
1238 | thread_db_xfer_memory (memaddr, myaddr, len, dowrite, target) | |
1239 | CORE_ADDR memaddr; | |
1240 | char *myaddr; | |
1241 | int len; | |
1242 | int dowrite; | |
1243 | struct target_ops *target; /* ignored */ | |
1244 | { | |
1245 | struct cleanup *old_chain; | |
1246 | int ret; | |
1247 | ||
1248 | old_chain = save_inferior_pid (); | |
1249 | ||
1250 | if (is_thread (inferior_pid) || | |
1251 | !target_thread_alive (inferior_pid)) | |
1252 | { | |
1253 | /* FIXME: use the LID/LWP, so that underlying process layer | |
1254 | can read memory from specific threads? */ | |
1255 | inferior_pid = main_prochandle.pid; | |
1256 | } | |
1257 | ||
1258 | ret = target_beneath->to_xfer_memory (memaddr, myaddr, len, | |
1259 | dowrite, target); | |
1260 | do_cleanups (old_chain); | |
1261 | return ret; | |
1262 | } | |
1263 | ||
1264 | /* | |
1265 | * fetch_registers has to determine if inferior_pid is a user-space thread. | |
1266 | * If so, we use the thread_db API to get the registers. | |
1267 | * And if not, we call the underlying process stratum. | |
1268 | */ | |
1269 | ||
1270 | static void | |
1271 | thread_db_fetch_registers (regno) | |
1272 | int regno; | |
1273 | { | |
1274 | td_thrhandle_t thandle; | |
1275 | prfpregset_t fpregset; | |
1276 | prgregset_t gregset; | |
1277 | thread_t thread; | |
1278 | td_err_e ret; | |
1279 | ||
1280 | if (!is_thread (inferior_pid)) /* kernel thread */ | |
1281 | { /* pass the request on to the target underneath. */ | |
1282 | target_beneath->to_fetch_registers (regno); | |
1283 | return; | |
1284 | } | |
1285 | ||
1286 | /* convert inferior_pid into a td_thrhandle_t */ | |
1287 | ||
1288 | if ((thread = GET_THREAD (inferior_pid)) == 0) | |
1289 | error ("fetch_registers: thread == 0"); | |
1290 | ||
1291 | if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK) | |
1292 | error ("fetch_registers: td_ta_map_id2thr: %s", thr_err_string (ret)); | |
1293 | ||
1294 | /* Get the integer regs: | |
1295 | For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, | |
1296 | pc and sp are saved (by a thread context switch). */ | |
1297 | if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK && | |
1298 | ret != TD_PARTIALREG) | |
1299 | error ("fetch_registers: td_thr_getgregs %s", thr_err_string (ret)); | |
1300 | ||
1301 | /* And, now the fp regs */ | |
1302 | if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK && | |
1303 | ret != TD_NOFPREGS) | |
1304 | error ("fetch_registers: td_thr_getfpregs %s", thr_err_string (ret)); | |
1305 | ||
1306 | /* Note that we must call supply_{g fp}regset *after* calling the td routines | |
1307 | because the td routines call ps_lget* which affect the values stored in the | |
1308 | registers array. */ | |
1309 | ||
1310 | supply_gregset (gregset); | |
1311 | supply_fpregset (&fpregset); | |
1312 | ||
1313 | } | |
1314 | ||
1315 | /* | |
1316 | * store_registers has to determine if inferior_pid is a user-space thread. | |
1317 | * If so, we use the thread_db API to get the registers. | |
1318 | * And if not, we call the underlying process stratum. | |
1319 | */ | |
1320 | ||
1321 | static void | |
1322 | thread_db_store_registers (regno) | |
1323 | int regno; | |
1324 | { | |
1325 | td_thrhandle_t thandle; | |
1326 | prfpregset_t fpregset; | |
1327 | prgregset_t gregset; | |
1328 | thread_t thread; | |
1329 | td_err_e ret; | |
1330 | ||
1331 | if (!is_thread (inferior_pid)) /* Kernel thread: */ | |
1332 | { /* pass the request on to the underlying target vector. */ | |
1333 | target_beneath->to_store_registers (regno); | |
1334 | return; | |
1335 | } | |
1336 | ||
1337 | /* convert inferior_pid into a td_thrhandle_t */ | |
1338 | ||
1339 | if ((thread = GET_THREAD (inferior_pid)) == 0) | |
1340 | error ("store_registers: thread == 0"); | |
1341 | ||
1342 | if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK) | |
1343 | error ("store_registers: td_ta_map_id2thr %s", thr_err_string (ret)); | |
1344 | ||
1345 | if (regno != -1) | |
1346 | { /* Not writing all the regs */ | |
1347 | /* save new register value */ | |
1348 | /* MVS: I don't understand this... */ | |
1349 | char old_value[REGISTER_SIZE]; | |
1350 | ||
1351 | memcpy (old_value, ®isters[REGISTER_BYTE (regno)], REGISTER_SIZE); | |
1352 | ||
1353 | if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK) | |
1354 | error ("store_registers: td_thr_getgregs %s", thr_err_string (ret)); | |
1355 | if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK) | |
1356 | error ("store_registers: td_thr_getfpregs %s", thr_err_string (ret)); | |
1357 | ||
1358 | /* restore new register value */ | |
1359 | memcpy (®isters[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE); | |
1360 | ||
1361 | } | |
1362 | ||
1363 | fill_gregset (gregset, regno); | |
1364 | fill_fpregset (&fpregset, regno); | |
1365 | ||
1366 | if ((ret = p_td_thr_setgregs (&thandle, gregset)) != TD_OK) | |
1367 | error ("store_registers: td_thr_setgregs %s", thr_err_string (ret)); | |
1368 | if ((ret = p_td_thr_setfpregs (&thandle, &fpregset)) != TD_OK && | |
1369 | ret != TD_NOFPREGS) | |
1370 | error ("store_registers: td_thr_setfpregs %s", thr_err_string (ret)); | |
1371 | } | |
1372 | ||
1373 | static void | |
1374 | handle_new_thread (tid, lid, verbose) | |
1375 | int tid; /* user thread id */ | |
1376 | int lid; /* kernel thread id */ | |
1377 | int verbose; | |
1378 | { | |
1379 | int gdb_pid = BUILD_THREAD (tid, main_prochandle.pid); | |
1380 | int wait_pid, wait_status; | |
1381 | ||
1382 | if (verbose) | |
1383 | printf_filtered ("[New %s]\n", target_pid_to_str (gdb_pid)); | |
1384 | add_thread (gdb_pid); | |
1385 | ||
1386 | if (lid != main_prochandle.pid) | |
1387 | { | |
1388 | attach_thread (lid); | |
1389 | /* According to the Eric Paire model, we now have to send | |
1390 | the restart signal to the new thread -- however, empirically, | |
1391 | I do not find that to be necessary. */ | |
1392 | attach_pid = lid; | |
1393 | } | |
1394 | } | |
1395 | ||
1396 | static void | |
1397 | test_for_new_thread (tid, lid, verbose) | |
1398 | int tid; | |
1399 | int lid; | |
1400 | int verbose; | |
1401 | { | |
1402 | if (!in_thread_list (BUILD_THREAD (tid, main_prochandle.pid))) | |
1403 | handle_new_thread (tid, lid, verbose); | |
1404 | } | |
1405 | ||
1406 | /* | |
1407 | * Callback function that gets called once per USER thread | |
1408 | * (i.e., not kernel) thread by td_ta_thr_iter. | |
1409 | */ | |
1410 | ||
1411 | static int | |
1412 | find_new_threads_callback (th, ignored) | |
1413 | const td_thrhandle_t *th; | |
1414 | void *ignored; | |
1415 | { | |
1416 | td_thrinfo_t ti; | |
1417 | td_err_e ret; | |
1418 | ||
1419 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1420 | { | |
1421 | warning ("find_new_threads_callback: %s", thr_err_string (ret)); | |
1422 | return -1; /* bail out, get_info failed. */ | |
1423 | } | |
1424 | ||
1425 | /* FIXME: | |
1426 | As things now stand, this should never detect a new thread. | |
1427 | But if it does, we could be in trouble because we aren't calling | |
1428 | wait_thread_callback for it. */ | |
1429 | test_for_new_thread (ti.ti_tid, ti.ti_lid, 0); | |
1430 | return 0; | |
1431 | } | |
1432 | ||
1433 | /* | |
1434 | * find_new_threads uses the thread_db iterator function to discover | |
1435 | * user-space threads. Then if the underlying process stratum has a | |
1436 | * find_new_threads method, we call that too. | |
1437 | */ | |
1438 | ||
1439 | static void | |
1440 | thread_db_find_new_threads () | |
1441 | { | |
1442 | if (inferior_pid == -1) /* FIXME: still necessary? */ | |
1443 | { | |
1444 | printf_filtered ("No process.\n"); | |
1445 | return; | |
1446 | } | |
1447 | p_td_ta_thr_iter (main_threadagent, | |
1448 | find_new_threads_callback, | |
1449 | (void *) 0, | |
1450 | TD_THR_ANY_STATE, | |
1451 | TD_THR_LOWEST_PRIORITY, | |
1452 | TD_SIGNO_MASK, | |
1453 | TD_THR_ANY_USER_FLAGS); | |
1454 | if (target_beneath->to_find_new_threads) | |
1455 | target_beneath->to_find_new_threads (); | |
1456 | } | |
1457 | ||
1458 | /* | |
1459 | * Resume all threads, or resume a single thread. | |
1460 | * If step is true, then single-step the appropriate thread | |
1461 | * (or single-step inferior_pid, but continue everyone else). | |
1462 | * If signo is true, then send that signal to at least one thread. | |
1463 | */ | |
1464 | ||
1465 | /* | |
1466 | * This function is called once for each thread before resuming. | |
1467 | * It sends continue (no step, and no signal) to each thread except | |
1468 | * the main thread, and | |
1469 | * the event thread (the one that stopped at a breakpoint etc.) | |
1470 | * | |
1471 | * The event thread is handled separately so that it can be sent | |
1472 | * the stepping and signal args with which target_resume was called. | |
1473 | * | |
1474 | * The main thread is resumed last, so that the thread_db proc_service | |
1475 | * callbacks will still work during the iterator function. | |
1476 | */ | |
1477 | ||
1478 | static int | |
1479 | resume_thread_callback (th, data) | |
1480 | const td_thrhandle_t *th; | |
1481 | void *data; | |
1482 | { | |
1483 | td_thrinfo_t ti; | |
1484 | td_err_e ret; | |
1485 | ||
1486 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1487 | { | |
1488 | warning ("resume_thread_callback: %s", thr_err_string (ret)); | |
1489 | return -1; /* bail out, get_info failed. */ | |
1490 | } | |
1491 | /* FIXME: | |
1492 | As things now stand, this should never detect a new thread. | |
1493 | But if it does, we could be in trouble because we aren't calling | |
1494 | wait_thread_callback for it. */ | |
1495 | test_for_new_thread (ti.ti_tid, ti.ti_lid, 1); | |
1496 | ||
1497 | if (ti.ti_lid != main_prochandle.pid && | |
1498 | ti.ti_lid != event_pid) | |
1499 | { | |
1500 | /* Unconditionally continue the thread with no signal. | |
1501 | Only the event thread will get a signal of any kind. */ | |
1502 | ||
1503 | target_beneath->to_resume (ti.ti_lid, 0, 0); | |
1504 | } | |
1505 | return 0; | |
1506 | } | |
1507 | ||
1508 | static int | |
1509 | new_resume_thread_callback (thread, data) | |
1510 | threadinfo *thread; | |
1511 | void *data; | |
1512 | { | |
1513 | if (thread->lid != event_pid && | |
1514 | thread->lid != main_prochandle.pid) | |
1515 | { | |
1516 | /* Unconditionally continue the thread with no signal (for now). */ | |
1517 | ||
1518 | target_beneath->to_resume (thread->lid, 0, 0); | |
1519 | } | |
1520 | return 0; | |
1521 | } | |
1522 | ||
1523 | static int last_resume_pid; | |
1524 | static int last_resume_step; | |
1525 | static int last_resume_signo; | |
1526 | ||
1527 | static void | |
1528 | thread_db_resume (pid, step, signo) | |
1529 | int pid; | |
1530 | int step; | |
1531 | enum target_signal signo; | |
1532 | { | |
1533 | last_resume_pid = pid; | |
1534 | last_resume_step = step; | |
1535 | last_resume_signo = signo; | |
1536 | ||
1537 | /* resuming a specific pid? */ | |
1538 | if (pid != -1) | |
1539 | { | |
1540 | if (is_thread (pid)) | |
1541 | pid = get_lwp_from_thread_id (GET_THREAD (pid)); | |
1542 | else if (GET_LWP (pid)) | |
1543 | pid = GET_LWP (pid); | |
1544 | } | |
1545 | ||
1546 | /* Apparently the interpretation of 'pid' is dependent on 'step': | |
1547 | If step is true, then a specific pid means 'step only this pid'. | |
1548 | But if step is not true, then pid means 'continue ALL pids, but | |
1549 | give the signal only to this one'. */ | |
1550 | if (pid != -1 && step) | |
1551 | { | |
1552 | /* FIXME: is this gonna work in all circumstances? */ | |
1553 | target_beneath->to_resume (pid, step, signo); | |
1554 | } | |
1555 | else | |
1556 | { | |
1557 | /* 1) Continue all threads except the event thread and the main thread. | |
1558 | 2) resume the event thread with step and signo. | |
1559 | 3) If event thread != main thread, continue the main thread. | |
1560 | ||
1561 | Note: order of 2 and 3 may need to be reversed. */ | |
1562 | ||
1563 | threadlist_iter (new_resume_thread_callback, | |
1564 | (void *) 0, | |
1565 | TD_THR_ANY_STATE, | |
1566 | TD_THR_ANY_TYPE); | |
1567 | /* now resume event thread, and if necessary also main thread. */ | |
1568 | if (event_pid) | |
1569 | { | |
1570 | target_beneath->to_resume (event_pid, step, signo); | |
1571 | } | |
1572 | if (event_pid != main_prochandle.pid) | |
1573 | { | |
1574 | target_beneath->to_resume (main_prochandle.pid, 0, 0); | |
1575 | } | |
1576 | } | |
1577 | } | |
1578 | ||
1579 | /* All new threads will be attached. | |
1580 | All previously known threads will be stopped using kill (SIGKILL). */ | |
1581 | ||
1582 | static int | |
1583 | stop_or_attach_thread_callback (const td_thrhandle_t *th, void *data) | |
1584 | { | |
1585 | td_thrinfo_t ti; | |
1586 | td_err_e ret; | |
1587 | int gdb_pid; | |
1588 | int on_off = 1; | |
1589 | ||
1590 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1591 | { | |
1592 | warning ("stop_or_attach_thread_callback: %s", thr_err_string (ret)); | |
1593 | return -1; /* bail out, get_info failed. */ | |
1594 | } | |
1595 | ||
1596 | /* First add it to our internal list. | |
1597 | We build this list anew at every wait event. */ | |
1598 | insert_thread (ti.ti_tid, ti.ti_lid, ti.ti_state, ti.ti_type); | |
1599 | /* Now: if we've already seen it, stop it, else add it and attach it. */ | |
1600 | gdb_pid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid); | |
1601 | if (!in_thread_list (gdb_pid)) /* new thread */ | |
1602 | { | |
1603 | handle_new_thread (ti.ti_tid, ti.ti_lid, 1); | |
1604 | /* Enable thread events */ | |
1605 | if (p_td_thr_event_enable) | |
1606 | if ((ret = p_td_thr_event_enable (th, on_off)) != TD_OK) | |
1607 | warning ("stop_or_attach_thread: %s", thr_err_string (ret)); | |
1608 | } | |
1609 | else if (ti.ti_lid != event_pid && | |
1610 | ti.ti_lid != main_prochandle.pid) | |
1611 | { | |
1612 | ret = (td_err_e) kill (ti.ti_lid, SIGSTOP); | |
1613 | } | |
1614 | ||
1615 | return 0; | |
1616 | } | |
1617 | ||
1618 | /* | |
1619 | * Wait for signal N from pid PID. | |
1620 | * If wait returns any other signals, put them back before returning. | |
1621 | */ | |
1622 | ||
1623 | static void | |
1624 | wait_for_stop (pid) | |
1625 | int pid; | |
1626 | { | |
1627 | int i; | |
1628 | int retpid; | |
1629 | int status; | |
1630 | ||
1631 | /* Array of wait/signal status */ | |
1632 | /* FIXME: wrong data structure, we need a queue. | |
1633 | Realtime signals may be delivered more than once. | |
1634 | And at that, we really can't handle them (see below). */ | |
1635 | #if defined (NSIG) | |
1636 | static int wstatus [NSIG]; | |
1637 | #elif defined (_NSIG) | |
1638 | static int wstatus [_NSIG]; | |
1639 | #else | |
1640 | #error No definition for number of signals! | |
1641 | #endif | |
1642 | ||
1643 | /* clear wait/status list */ | |
1644 | memset (&wstatus, 0, sizeof (wstatus)); | |
1645 | ||
1646 | /* Now look for SIGSTOP event on all threads except event thread. */ | |
1647 | do { | |
1648 | errno = 0; | |
1649 | if (pid == main_prochandle.pid) | |
1650 | retpid = waitpid (pid, &status, 0); | |
1651 | else | |
1652 | retpid = waitpid (pid, &status, __WCLONE); | |
1653 | ||
1654 | if (retpid > 0) | |
1655 | if (WSTOPSIG (status) == SIGSTOP) | |
1656 | { | |
1657 | /* Got the SIGSTOP event we're looking for. | |
1658 | Throw it away, and throw any other events back! */ | |
1659 | for (i = 0; i < sizeof(wstatus) / sizeof (wstatus[0]); i++) | |
1660 | if (wstatus[i]) | |
1661 | if (i != SIGSTOP) | |
1662 | { | |
1663 | kill (retpid, i); | |
1664 | } | |
1665 | break; /* all done */ | |
1666 | } | |
1667 | else | |
1668 | { | |
1669 | int signo; | |
1670 | /* Oops, got an event other than SIGSTOP. | |
1671 | Save it, and throw it back after we find the SIGSTOP event. */ | |
1672 | ||
1673 | /* FIXME (how?) This method is going to fail for realtime | |
1674 | signals, which cannot be put back simply by using kill. */ | |
1675 | ||
1676 | if (WIFEXITED (status)) | |
1677 | error ("Ack! Thread Exited event. What do I do now???"); | |
1678 | else if (WIFSTOPPED (status)) | |
1679 | signo = WSTOPSIG (status); | |
1680 | else | |
1681 | signo = WTERMSIG (status); | |
1682 | ||
1683 | /* If a thread other than the event thread has hit a GDB | |
1684 | breakpoint (as opposed to some random trap signal), then | |
1685 | just arrange for it to hit it again later. Back up the | |
1686 | PC if necessary. Don't forward the SIGTRAP signal to | |
1687 | the thread. We will handle the current event, eventually | |
1688 | we will resume all the threads, and this one will get | |
1689 | it's breakpoint trap again. | |
1690 | ||
1691 | If we do not do this, then we run the risk that the user | |
1692 | will delete or disable the breakpoint, but the thread will | |
1693 | have already tripped on it. */ | |
1694 | ||
1695 | if (retpid != event_pid && | |
1696 | signo == SIGTRAP && | |
1697 | breakpoint_inserted_here_p (read_pc_pid (retpid) - | |
1698 | DECR_PC_AFTER_BREAK)) | |
1699 | { | |
1700 | /* Set the pc to before the trap and DO NOT re-send the signal */ | |
1701 | if (DECR_PC_AFTER_BREAK) | |
1702 | write_pc_pid (read_pc_pid (retpid) - DECR_PC_AFTER_BREAK, | |
1703 | retpid); | |
1704 | } | |
1705 | ||
1706 | /* Since SIGINT gets forwarded to the entire process group | |
1707 | (in the case where ^C is typed at the tty / console), | |
1708 | just ignore all SIGINTs from other than the event thread. */ | |
1709 | else if (retpid != event_pid && signo == SIGINT) | |
1710 | { /* do nothing. Signal will disappear into oblivion! */ | |
1711 | ; | |
1712 | } | |
1713 | ||
1714 | else /* This is some random signal other than a breakpoint. */ | |
1715 | { | |
1716 | wstatus [signo] = 1; | |
1717 | } | |
1718 | child_resume (retpid, 0, TARGET_SIGNAL_0); | |
1719 | continue; | |
1720 | } | |
1721 | ||
1722 | } while (errno == 0 || errno == EINTR); | |
1723 | } | |
1724 | ||
1725 | /* | |
1726 | * wait_thread_callback | |
1727 | * | |
1728 | * Calls waitpid for each thread, repeatedly if necessary, until | |
1729 | * SIGSTOP is returned. Afterward, if any other signals were returned | |
1730 | * by waitpid, return them to the thread's pending queue by calling kill. | |
1731 | */ | |
1732 | ||
1733 | static int | |
1734 | wait_thread_callback (const td_thrhandle_t *th, void *data) | |
1735 | { | |
1736 | td_thrinfo_t ti; | |
1737 | td_err_e ret; | |
1738 | ||
1739 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1740 | { | |
1741 | warning ("wait_thread_callback: %s", thr_err_string (ret)); | |
1742 | return -1; /* bail out, get_info failed. */ | |
1743 | } | |
1744 | ||
1745 | /* This callback to act on all threads except the event thread: */ | |
1746 | if (ti.ti_lid == event_pid || /* no need to wait (no sigstop) */ | |
1747 | ti.ti_lid == main_prochandle.pid) /* no need to wait (already waited) */ | |
1748 | return 0; /* don't wait on the event thread. */ | |
1749 | ||
1750 | wait_for_stop (ti.ti_lid); | |
1751 | return 0; /* finished: next thread. */ | |
1752 | } | |
1753 | ||
1754 | static int | |
1755 | new_wait_thread_callback (thread, data) | |
1756 | threadinfo *thread; | |
1757 | void *data; | |
1758 | { | |
1759 | /* don't wait on the event thread -- it's already stopped and waited. | |
1760 | Ditto the main thread. */ | |
1761 | if (thread->lid != event_pid && | |
1762 | thread->lid != main_prochandle.pid) | |
1763 | { | |
1764 | wait_for_stop (thread->lid); | |
1765 | } | |
1766 | return 0; | |
1767 | } | |
1768 | ||
1769 | /* | |
1770 | * Wait for any thread to stop, by calling the underlying wait method. | |
1771 | * The PID returned by the underlying target may be a kernel thread, | |
1772 | * in which case we will want to convert it to the corresponding | |
1773 | * user-space thread. | |
1774 | */ | |
1775 | ||
1776 | static int | |
1777 | thread_db_wait (int pid, struct target_waitstatus *ourstatus) | |
1778 | { | |
1779 | td_thrhandle_t thandle; | |
1780 | td_thrinfo_t ti; | |
1781 | td_err_e ret; | |
1782 | lwpid_t lwp; | |
1783 | int retpid; | |
1784 | int status; | |
1785 | int save_errno; | |
1786 | ||
1787 | /* OK, we're about to wait for an event from the running inferior. | |
1788 | Make sure we're ignoring the right signals. */ | |
1789 | ||
1790 | check_all_signal_numbers (); /* see if magic signals changed. */ | |
1791 | ||
1792 | event_pid = 0; | |
1793 | attach_pid = 0; | |
1794 | ||
1795 | /* FIXME: should I do the wait right here inline? */ | |
1796 | #if 0 | |
1797 | if (pid == -1) | |
1798 | lwp = -1; | |
1799 | else | |
1800 | lwp = get_lwp_from_thread_id (GET_THREAD (pid)); | |
1801 | #endif | |
1802 | ||
1803 | ||
1804 | save_errno = linux_child_wait (-1, &retpid, &status); | |
1805 | store_waitstatus (ourstatus, status); | |
1806 | ||
1807 | /* Thread ID is irrelevant if the target process exited. | |
1808 | FIXME: do I have any killing to do? | |
1809 | Can I get this event mistakenly from a thread? */ | |
1810 | if (ourstatus->kind == TARGET_WAITKIND_EXITED) | |
1811 | return retpid; | |
1812 | ||
1813 | /* OK, we got an event of interest. | |
1814 | Go stop all threads and look for new ones. | |
1815 | FIXME: maybe don't do this for the restart signal? Optimization... */ | |
1816 | event_pid = retpid; | |
1817 | ||
1818 | /* If the last call to resume was for a specific thread, then we don't | |
1819 | need to stop everyone else: they should already be stopped. */ | |
1820 | if (last_resume_step == 0 || last_resume_pid == -1) | |
1821 | { | |
1822 | /* Main thread must be stopped before calling the iterator. */ | |
1823 | if (retpid != main_prochandle.pid) | |
1824 | { | |
1825 | kill (main_prochandle.pid, SIGSTOP); | |
1826 | wait_for_stop (main_prochandle.pid); | |
1827 | } | |
1828 | ||
1829 | empty_threadlist (); | |
1830 | /* Now stop everyone else, and attach any new threads you find. */ | |
1831 | p_td_ta_thr_iter (main_threadagent, | |
1832 | stop_or_attach_thread_callback, | |
1833 | (void *) 0, | |
1834 | TD_THR_ANY_STATE, | |
1835 | TD_THR_LOWEST_PRIORITY, | |
1836 | TD_SIGNO_MASK, | |
1837 | TD_THR_ANY_USER_FLAGS); | |
1838 | ||
1839 | /* Now go call wait on all the threads we've stopped: | |
1840 | This allows us to absorb the SIGKILL event, and to make sure | |
1841 | that the thread knows that it is stopped (Linux peculiarity). */ | |
1842 | ||
1843 | threadlist_iter (new_wait_thread_callback, | |
1844 | (void *) 0, | |
1845 | TD_THR_ANY_STATE, | |
1846 | TD_THR_ANY_TYPE); | |
1847 | } | |
1848 | ||
1849 | /* Convert the kernel thread id to the corresponding thread id. */ | |
1850 | ||
1851 | /* If the process layer does not furnish an lwp, | |
1852 | then perhaps the returned pid IS the lwp... */ | |
1853 | if ((lwp = GET_LWP (retpid)) == 0) | |
1854 | lwp = retpid; | |
1855 | ||
1856 | if ((ret = p_td_ta_map_lwp2thr (main_threadagent, lwp, &thandle)) != TD_OK) | |
1857 | return retpid; /* LWP is not mapped onto a user-space thread. */ | |
1858 | ||
1859 | if ((ret = p_td_thr_validate (&thandle)) != TD_OK) | |
1860 | return retpid; /* LWP is not mapped onto a valid thread. */ | |
1861 | ||
1862 | if ((ret = p_td_thr_get_info (&thandle, &ti)) != TD_OK) | |
1863 | { | |
1864 | warning ("thread_db: thr_get_info failed ('%s')", thr_err_string (ret)); | |
1865 | return retpid; | |
1866 | } | |
1867 | ||
1868 | retpid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid); | |
1869 | /* If this is a new user thread, notify GDB about it. */ | |
1870 | if (!in_thread_list (retpid)) | |
1871 | { | |
1872 | printf_filtered ("[New %s]\n", target_pid_to_str (retpid)); | |
1873 | add_thread (retpid); | |
1874 | } | |
1875 | ||
1876 | #if 0 | |
1877 | /* Now detect if this is a thread creation/deletion event: */ | |
1878 | check_for_thread_event (ourstatus, retpid); | |
1879 | #endif | |
1880 | return retpid; | |
1881 | } | |
1882 | ||
1883 | /* | |
1884 | * kill has to call the underlying kill. | |
1885 | * FIXME: I'm not sure if it's necessary to check inferior_pid any more, | |
1886 | * but we might need to fix inferior_pid up if it's a user thread. | |
1887 | */ | |
1888 | ||
1889 | static int | |
1890 | kill_thread_callback (th, data) | |
1891 | td_thrhandle_t *th; | |
1892 | void *data; | |
1893 | { | |
1894 | td_thrinfo_t ti; | |
1895 | td_err_e ret; | |
1896 | ||
1897 | /* Fixme: | |
1898 | For Linux, threads may need to be waited. */ | |
1899 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1900 | { | |
1901 | warning ("kill_thread_callback: %s", thr_err_string (ret)); | |
1902 | return -1; /* bail out, get_info failed. */ | |
1903 | } | |
1904 | ||
1905 | if (ti.ti_lid != main_prochandle.pid) | |
1906 | { | |
1907 | kill (ti.ti_lid, SIGKILL); | |
1908 | } | |
1909 | return 0; | |
1910 | } | |
1911 | ||
1912 | ||
1913 | static void thread_db_kill (void) | |
1914 | { | |
1915 | int rpid; | |
1916 | int status; | |
1917 | ||
1918 | /* Fixme: | |
1919 | For Linux, threads may need to be waited. */ | |
1920 | if (inferior_pid != 0) | |
1921 | { | |
1922 | /* Go kill the children first. Save the main thread for last. */ | |
1923 | p_td_ta_thr_iter (main_threadagent, | |
1924 | kill_thread_callback, | |
1925 | (void *) 0, | |
1926 | TD_THR_ANY_STATE, | |
1927 | TD_THR_LOWEST_PRIORITY, | |
1928 | TD_SIGNO_MASK, | |
1929 | TD_THR_ANY_USER_FLAGS); | |
1930 | ||
1931 | /* Turn off thread_db event-reporting API *before* killing the | |
1932 | main thread, since this operation requires child memory access. | |
1933 | Can't move this into thread_db_unpush target because then | |
1934 | detach would not work. */ | |
1935 | disable_thread_event_reporting (main_threadagent); | |
1936 | ||
1937 | inferior_pid = main_prochandle.pid; | |
1938 | ||
1939 | /* | |
1940 | * Since both procfs_kill and ptrace_kill call target_mourn, | |
1941 | * it should be sufficient for me to call one of them. | |
1942 | * That will result in my mourn being called, which will both | |
1943 | * unpush me and call the underlying mourn. | |
1944 | */ | |
1945 | target_beneath->to_kill (); | |
1946 | } | |
1947 | ||
1948 | /* Wait for all threads. */ | |
1949 | /* FIXME: need a universal wait_for_signal func? */ | |
1950 | do | |
1951 | { | |
1952 | rpid = waitpid (-1, &status, __WCLONE | WNOHANG); | |
1953 | } | |
1954 | while (rpid > 0 || errno == EINTR); | |
1955 | ||
1956 | do | |
1957 | { | |
1958 | rpid = waitpid (-1, &status, WNOHANG); | |
1959 | } | |
1960 | while (rpid > 0 || errno == EINTR); | |
1961 | } | |
1962 | ||
1963 | /* | |
1964 | * Mourn has to remove us from the target stack, | |
1965 | * and then call the underlying mourn. | |
1966 | */ | |
1967 | ||
1968 | static void thread_db_mourn_inferior (void) | |
1969 | { | |
1970 | thread_db_unpush_target (); | |
1971 | target_mourn_inferior (); /* call the underlying mourn */ | |
1972 | } | |
1973 | ||
1974 | /* | |
1975 | * Detach has to remove us from the target stack, | |
1976 | * and then call the underlying detach. | |
1977 | * | |
1978 | * But first, it has to detach all the cloned threads! | |
1979 | */ | |
1980 | ||
1981 | static int | |
1982 | detach_thread_callback (th, data) | |
1983 | td_thrhandle_t *th; | |
1984 | void *data; | |
1985 | { | |
1986 | /* Called once per thread. */ | |
1987 | td_thrinfo_t ti; | |
1988 | td_err_e ret; | |
1989 | ||
1990 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1991 | { | |
1992 | warning ("detach_thread_callback: %s", thr_err_string (ret)); | |
1993 | return -1; /* bail out, get_info failed. */ | |
1994 | } | |
1995 | ||
1996 | if (!in_thread_list (BUILD_THREAD (ti.ti_tid, main_prochandle.pid))) | |
1997 | return 0; /* apparently we don't know this one. */ | |
1998 | ||
1999 | /* Save main thread for last, or the iterator will fail! */ | |
2000 | if (ti.ti_lid != main_prochandle.pid) | |
2001 | { | |
2002 | struct cleanup *old_chain; | |
2003 | int off = 0; | |
2004 | ||
2005 | /* Time to detach this thread. | |
2006 | First disable thread_db event reporting for the thread. */ | |
2007 | if (p_td_thr_event_enable && | |
2008 | (ret = p_td_thr_event_enable (th, off)) != TD_OK) | |
2009 | { | |
2010 | warning ("detach_thread_callback: %s\n", thr_err_string (ret)); | |
2011 | return 0; | |
2012 | } | |
2013 | ||
2014 | /* Now cancel any pending SIGTRAPS. FIXME! */ | |
2015 | ||
2016 | /* Call underlying detach method. FIXME just detach it. */ | |
2017 | old_chain = save_inferior_pid (); | |
2018 | inferior_pid = ti.ti_lid; | |
2019 | detach (TARGET_SIGNAL_0); | |
2020 | do_cleanups (old_chain); | |
2021 | } | |
2022 | return 0; | |
2023 | } | |
2024 | ||
2025 | static void | |
2026 | thread_db_detach (char *args, int from_tty) | |
2027 | { | |
2028 | td_err_e ret; | |
2029 | ||
2030 | if ((ret = p_td_ta_thr_iter (main_threadagent, | |
2031 | detach_thread_callback, | |
2032 | (void *) 0, | |
2033 | TD_THR_ANY_STATE, | |
2034 | TD_THR_LOWEST_PRIORITY, | |
2035 | TD_SIGNO_MASK, | |
2036 | TD_THR_ANY_USER_FLAGS)) | |
2037 | != TD_OK) | |
2038 | warning ("detach (thr_iter): %s", thr_err_string (ret)); | |
2039 | ||
2040 | /* Turn off thread_db event-reporting API | |
2041 | (before detaching the main thread) */ | |
2042 | disable_thread_event_reporting (main_threadagent); | |
2043 | ||
2044 | thread_db_unpush_target (); | |
2045 | ||
2046 | /* above call nullifies target_beneath, so don't use that! */ | |
2047 | inferior_pid = PIDGET (inferior_pid); | |
2048 | target_detach (args, from_tty); | |
2049 | } | |
2050 | ||
2051 | ||
2052 | /* | |
2053 | * We never want to actually create the inferior! | |
2054 | * | |
2055 | * If this is ever called, it means we were on the target stack | |
2056 | * when the user said "run". But we don't want to be on the new | |
2057 | * inferior's target stack until the thread_db / libthread | |
2058 | * connection is ready to be made. | |
2059 | * | |
2060 | * So, what shall we do? | |
2061 | * Unpush ourselves from the stack, and then invoke | |
2062 | * find_default_create_inferior, which will invoke the | |
2063 | * appropriate process_stratum target to do the create. | |
2064 | */ | |
2065 | ||
2066 | static void | |
2067 | thread_db_create_inferior (exec_file, allargs, env) | |
2068 | char *exec_file; | |
2069 | char *allargs; | |
2070 | char **env; | |
2071 | { | |
2072 | thread_db_unpush_target (); | |
2073 | find_default_create_inferior (exec_file, allargs, env); | |
2074 | } | |
2075 | ||
2076 | /* | |
2077 | * Thread_db target vector initializer. | |
2078 | */ | |
2079 | ||
2080 | void | |
2081 | init_thread_db_ops () | |
2082 | { | |
2083 | thread_db_ops.to_shortname = "multi-thread"; | |
2084 | thread_db_ops.to_longname = "multi-threaded child process."; | |
2085 | thread_db_ops.to_doc = "Threads and pthreads support."; | |
2086 | thread_db_ops.to_files_info = thread_db_files_info; | |
2087 | thread_db_ops.to_create_inferior = thread_db_create_inferior; | |
2088 | thread_db_ops.to_detach = thread_db_detach; | |
2089 | thread_db_ops.to_wait = thread_db_wait; | |
2090 | thread_db_ops.to_resume = thread_db_resume; | |
2091 | thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior; | |
2092 | thread_db_ops.to_kill = thread_db_kill; | |
2093 | thread_db_ops.to_xfer_memory = thread_db_xfer_memory; | |
2094 | thread_db_ops.to_fetch_registers = thread_db_fetch_registers; | |
2095 | thread_db_ops.to_store_registers = thread_db_store_registers; | |
2096 | thread_db_ops.to_thread_alive = thread_db_alive; | |
2097 | thread_db_ops.to_find_new_threads = thread_db_find_new_threads; | |
2098 | thread_db_ops.to_pid_to_str = thread_db_pid_to_str; | |
2099 | thread_db_ops.to_stratum = thread_stratum; | |
2100 | thread_db_ops.to_has_thread_control = tc_schedlock; | |
2101 | thread_db_ops.to_magic = OPS_MAGIC; | |
2102 | } | |
2103 | #endif /* HAVE_STDINT_H */ | |
2104 | ||
2105 | /* | |
2106 | * Module constructor / initializer function. | |
2107 | * If connection to thread_db dynamic library is successful, | |
2108 | * then initialize this module's target vectors and the | |
2109 | * new_objfile hook. | |
2110 | */ | |
2111 | ||
2112 | ||
2113 | void | |
2114 | _initialize_thread_db () | |
2115 | { | |
2116 | #ifdef HAVE_STDINT_H /* stub out entire module, leave initializer empty */ | |
2117 | if (init_thread_db_library ()) | |
2118 | { | |
2119 | init_thread_db_ops (); | |
2120 | add_target (&thread_db_ops); | |
2121 | /* | |
2122 | * Hook up to the new_objfile event. | |
2123 | * If someone is already there, arrange for him to be called | |
2124 | * after we are. | |
2125 | */ | |
2126 | target_new_objfile_chain = target_new_objfile_hook; | |
2127 | target_new_objfile_hook = thread_db_new_objfile; | |
2128 | } | |
2129 | #endif /* HAVE_STDINT_H */ | |
2130 | } | |
2131 |