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