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