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