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