]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/lin-thread.c
Phase 1 of the ptid_t changes.
[thirdparty/binutils-gdb.git] / gdb / lin-thread.c
CommitLineData
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)
39f77062
KB
130#define PIDGET0(PID) (((PID) & 0xffff))
131#define PIDGET(PID) ((PIDGET0 (PID) == 0xffff) ? -1 : PIDGET0 (PID))
ed9a39eb
JM
132#define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
133#endif
134
39f77062 135/* Macros for superimposing PID and TID into inferior_ptid. */
ed9a39eb
JM
136#define THREAD_FLAG 0x80000000
137#define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
138#define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
139#define GET_LWP(PID) TIDGET (PID)
140#define GET_THREAD(PID) TIDGET (PID)
141#define BUILD_LWP(TID, PID) MERGEPID (PID, TID)
142#define BUILD_THREAD(TID, PID) (MERGEPID (PID, TID) | THREAD_FLAG)
143
144/*
145 * target_beneath is a pointer to the target_ops underlying this one.
146 */
147
148static struct target_ops *target_beneath;
149
150
151/*
152 * target vector defined in this module:
153 */
154
155static struct target_ops thread_db_ops;
156
157/*
158 * Typedefs required to resolve differences between the thread_db
159 * and proc_service API defined on different versions of Solaris:
160 */
161
162#if defined(PROC_SERVICE_IS_OLD)
163typedef const struct ps_prochandle *gdb_ps_prochandle_t;
164typedef char *gdb_ps_read_buf_t;
165typedef char *gdb_ps_write_buf_t;
166typedef int gdb_ps_size_t;
167#else
168typedef struct ps_prochandle *gdb_ps_prochandle_t;
169typedef void *gdb_ps_read_buf_t;
170typedef const void *gdb_ps_write_buf_t;
171typedef size_t gdb_ps_size_t;
172#endif
173
174/*
175 * proc_service callback functions, called by thread_db.
176 */
177
178ps_err_e
179ps_pstop (gdb_ps_prochandle_t ph) /* Process stop */
180{
181 return PS_OK;
182}
183
184ps_err_e
185ps_pcontinue (gdb_ps_prochandle_t ph) /* Process continue */
186{
187 return PS_OK;
188}
189
190ps_err_e
191ps_lstop (gdb_ps_prochandle_t ph, /* LWP stop */
192 lwpid_t lwpid)
193{
194 return PS_OK;
195}
196
197ps_err_e
198ps_lcontinue (gdb_ps_prochandle_t ph, /* LWP continue */
199 lwpid_t lwpid)
200{
201 return PS_OK;
202}
203
204ps_err_e
205ps_lgetxregsize (gdb_ps_prochandle_t ph, /* Get XREG size */
206 lwpid_t lwpid,
207 int *xregsize)
208{
209 return PS_OK;
210}
211
212ps_err_e
213ps_lgetxregs (gdb_ps_prochandle_t ph, /* Get XREGS */
214 lwpid_t lwpid,
215 caddr_t xregset)
216{
217 return PS_OK;
218}
219
220ps_err_e
221ps_lsetxregs (gdb_ps_prochandle_t ph, /* Set XREGS */
222 lwpid_t lwpid,
223 caddr_t xregset)
224{
225 return PS_OK;
226}
227
228void
229ps_plog (const char *fmt, ...)
230{
231 va_list args;
232
233 va_start (args, fmt);
234 vfprintf_filtered (gdb_stderr, fmt, args);
235}
236
237/* Look up a symbol in GDB's global symbol table.
238 Return the symbol's address.
239 FIXME: it would be more correct to look up the symbol in the context
240 of the LD_OBJECT_NAME provided. However we're probably fairly safe
241 as long as there aren't name conflicts with other libraries. */
242
243ps_err_e
244ps_pglobal_lookup (gdb_ps_prochandle_t ph,
245 const char *ld_object_name, /* the library name */
246 const char *ld_symbol_name, /* the symbol name */
247 paddr_t *ld_symbol_addr) /* return the symbol addr */
248{
249 struct minimal_symbol *ms;
250
251 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
252
253 if (!ms)
254 return PS_NOSYM;
255
256 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
257
258 return PS_OK;
259}
260
261/* Worker function for all memory reads and writes: */
262static ps_err_e rw_common (const struct ps_prochandle *ph,
263 paddr_t addr,
264 char *buf,
265 int size,
266 int write_p);
267
268/* target_xfer_memory direction consts */
269enum {PS_READ = 0, PS_WRITE = 1};
270
271ps_err_e
272ps_pdread (gdb_ps_prochandle_t ph, /* read from data segment */
273 paddr_t addr,
274 gdb_ps_read_buf_t buf,
275 gdb_ps_size_t size)
276{
277 return rw_common (ph, addr, buf, size, PS_READ);
278}
279
280ps_err_e
281ps_pdwrite (gdb_ps_prochandle_t ph, /* write to data segment */
282 paddr_t addr,
283 gdb_ps_write_buf_t buf,
284 gdb_ps_size_t size)
285{
286 return rw_common (ph, addr, (char *) buf, size, PS_WRITE);
287}
288
289ps_err_e
290ps_ptread (gdb_ps_prochandle_t ph, /* read from text segment */
291 paddr_t addr,
292 gdb_ps_read_buf_t buf,
293 gdb_ps_size_t size)
294{
295 return rw_common (ph, addr, buf, size, PS_READ);
296}
297
298ps_err_e
299ps_ptwrite (gdb_ps_prochandle_t ph, /* write to text segment */
300 paddr_t addr,
301 gdb_ps_write_buf_t buf,
302 gdb_ps_size_t size)
303{
304 return rw_common (ph, addr, (char *) buf, size, PS_WRITE);
305}
306
39f77062
KB
307static struct cleanup *save_inferior_ptid (void);
308static void restore_inferior_ptid (void *saved_pid);
ed9a39eb
JM
309static char *thr_err_string (td_err_e);
310static char *thr_state_string (td_thr_state_e);
311
ed9a39eb
JM
312struct ps_prochandle main_prochandle;
313td_thragent_t * main_threadagent;
314
315/*
316 * Common proc_service routine for reading and writing memory.
317 */
318
39f77062 319/* FIXME: once we've munged the inferior_ptid, why can't we
ed9a39eb
JM
320 simply call target_read/write_memory and return? */
321
ed9a39eb
JM
322static ps_err_e
323rw_common (const struct ps_prochandle *ph,
324 paddr_t addr,
325 char *buf,
326 int size,
327 int write_p)
328{
39f77062 329 struct cleanup *old_chain = save_inferior_ptid ();
ed9a39eb
JM
330 int to_do = size;
331 int done = 0;
332
39f77062 333 inferior_ptid = pid_to_ptid (main_prochandle.pid);
ed9a39eb
JM
334
335 while (to_do > 0)
336 {
337 done = current_target.to_xfer_memory (addr, buf, size, write_p,
338 &current_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
39f77062 356 (which have to manipulate the global inferior_ptid). */
ed9a39eb
JM
357
358ps_err_e
359ps_lgetregs (gdb_ps_prochandle_t ph, /* Get LWP general regs */
360 lwpid_t lwpid,
361 prgregset_t gregset)
362{
39f77062 363 struct cleanup *old_chain = save_inferior_ptid ();
ed9a39eb 364
39f77062 365 inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid);
ed9a39eb
JM
366 current_target.to_fetch_registers (-1);
367
39f77062 368 fill_gregset ((gdb_gregset_t *) gregset, -1);
ed9a39eb
JM
369 do_cleanups (old_chain);
370
371 return PS_OK;
372}
373
374ps_err_e
375ps_lsetregs (gdb_ps_prochandle_t ph, /* Set LWP general regs */
376 lwpid_t lwpid,
377 const prgregset_t gregset)
378{
39f77062 379 struct cleanup *old_chain = save_inferior_ptid ();
ed9a39eb 380
39f77062
KB
381 inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid);
382 supply_gregset ((gdb_gregset_t *) gregset);
ed9a39eb
JM
383 current_target.to_store_registers (-1);
384 do_cleanups (old_chain);
385 return PS_OK;
386}
387
388ps_err_e
389ps_lgetfpregs (gdb_ps_prochandle_t ph, /* Get LWP float regs */
390 lwpid_t lwpid,
d84dd0c5 391 gdb_prfpregset_t *fpregset)
ed9a39eb 392{
39f77062 393 struct cleanup *old_chain = save_inferior_ptid ();
ed9a39eb 394
39f77062 395 inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid);
ed9a39eb
JM
396 current_target.to_fetch_registers (-1);
397 fill_fpregset (fpregset, -1);
398 do_cleanups (old_chain);
399 return PS_OK;
400}
401
402ps_err_e
403ps_lsetfpregs (gdb_ps_prochandle_t ph, /* Set LWP float regs */
404 lwpid_t lwpid,
d84dd0c5 405 const gdb_prfpregset_t *fpregset)
ed9a39eb 406{
39f77062 407 struct cleanup *old_chain = save_inferior_ptid ();
ed9a39eb 408
39f77062 409 inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid);
ed9a39eb
JM
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
423pid_t
424ps_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
433ps_err_e
434ps_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
463static td_err_e (*p_td_init) (void);
464
465static td_err_e (*p_td_ta_new) (const struct ps_prochandle *ph_p,
466 td_thragent_t **ta_pp);
467
468static td_err_e (*p_td_ta_delete) (td_thragent_t *ta_p);
469
470static td_err_e (*p_td_ta_get_nthreads) (const td_thragent_t *ta_p,
471 int *nthread_p);
472
473
474static 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
482static 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
486static td_err_e (*p_td_ta_event_getmsg) (const td_thragent_t *ta_p,
487 td_event_msg_t *msg);
488
489static td_err_e (*p_td_ta_set_event) (const td_thragent_t *ta_p,
490 td_thr_events_t *events);
491
492static td_err_e (*p_td_thr_validate) (const td_thrhandle_t *th_p);
493
494static td_err_e (*p_td_thr_event_enable) (const td_thrhandle_t *th_p,
495 int on_off);
496
497static td_err_e (*p_td_thr_get_info) (const td_thrhandle_t *th_p,
498 td_thrinfo_t *ti_p);
499
500static td_err_e (*p_td_thr_getgregs) (const td_thrhandle_t *th_p,
501 prgregset_t regset);
502
503static td_err_e (*p_td_thr_setgregs) (const td_thrhandle_t *th_p,
504 const prgregset_t regset);
505
506static td_err_e (*p_td_thr_getfpregs) (const td_thrhandle_t *th_p,
d84dd0c5 507 gdb_prfpregset_t *fpregset);
ed9a39eb
JM
508
509static td_err_e (*p_td_thr_setfpregs) (const td_thrhandle_t *th_p,
d84dd0c5 510 const gdb_prfpregset_t *fpregset);
ed9a39eb
JM
511
512static 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
516static 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
540static int
fba45db2 541init_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
39f77062
KB
631 save_inferior_ptid - Save inferior_ptid on the cleanup list
632 restore_inferior_ptid - Restore inferior_ptid from the cleanup list
ed9a39eb
JM
633
634 SYNOPSIS
635
39f77062
KB
636 struct cleanup *save_inferior_ptid (void);
637 void restore_inferior_ptid (void *saved_pid);
ed9a39eb
JM
638
639 DESCRIPTION
640
39f77062 641 These two functions act in unison to restore inferior_ptid in
ed9a39eb
JM
642 case of an error.
643
644 NOTES
645
39f77062 646 inferior_ptid is a global variable that needs to be changed by many
ed9a39eb 647 of these routines before calling functions in procfs.c. In order
39f77062
KB
648 to guarantee that inferior_ptid gets restored (in case of errors),
649 you need to call save_inferior_ptid before changing it. At the end
ed9a39eb
JM
650 of the function, you should invoke do_cleanups to restore it.
651
652 */
653
654static struct cleanup *
39f77062 655save_inferior_ptid (void)
ed9a39eb 656{
39f77062 657 ptid_t *saved_ptid_ptr;
a91f7ea9 658
39f77062
KB
659 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
660 *saved_ptid_ptr = inferior_ptid;
661 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
ed9a39eb
JM
662}
663
664static void
39f77062 665restore_inferior_ptid (void *arg)
ed9a39eb 666{
39f77062
KB
667 ptid_t *saved_ptid_ptr = arg;
668 inferior_ptid = *saved_ptid_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
689static char *
fba45db2 690thr_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
739static char *
fba45db2 740thr_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
763typedef 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
772threadinfo * threadlist;
773int threadlist_max = 0; /* current size of table */
774int threadlist_top = 0; /* number of threads now in table */
775#define THREADLIST_ALLOC 100 /* chunk size by which to expand table */
776
777static threadinfo *
fba45db2 778insert_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
798static void
fba45db2 799empty_threadlist (void)
ed9a39eb
JM
800{
801 threadlist_top = 0;
802}
803
804static threadinfo *
fba45db2 805next_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
816static void
064002de
KB
817threadlist_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. */
839extern 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 */
845static 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 */
851static 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
873static 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
881static 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
886static void
fba45db2 887enable_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, &notify) != TD_OK)
917 {
918 warning ("unable to get location for thread creation breakpoint");
919 return;
920 }
921
922 /* Set up the breakpoint. */
39f77062 923 create_thread_event_breakpoint ((CORE_ADDR) notify.u.bptaddr);
ed9a39eb
JM
924
925 /* Save it's location. */
39f77062 926 thread_creation_bkpt_address = (CORE_ADDR) notify.u.bptaddr;
ed9a39eb
JM
927
928 /* thread death */
929 /* get breakpoint location */
930 if (p_td_ta_event_addr (ta, TD_DEATH, &notify) != TD_OK)
931 {
932 warning ("unable to get location for thread death breakpoint");
933 return;
934 }
935 /* Set up the breakpoint. */
39f77062 936 create_thread_event_breakpoint ((CORE_ADDR) notify.u.bptaddr);
ed9a39eb
JM
937
938 /* Save it's location. */
39f77062 939 thread_death_bkpt_address = (CORE_ADDR) notify.u.bptaddr;
ed9a39eb
JM
940}
941
942/* This function handles the global parts of disabling thread events.
943 The thread-specific enabling is handled per-thread elsewhere. */
944
945static void
fba45db2 946disable_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
967static int
968check_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). */
39f77062 972 return 0;
ed9a39eb
JM
973}
974
975static void
976thread_db_push_target (void)
977{
978 /* Called ONLY from thread_db_new_objfile after td_ta_new call succeeds. */
979
980 /* Push this target vector */
981 push_target (&thread_db_ops);
982 /* Find the underlying process-layer target for calling later. */
983 target_beneath = find_target_beneath (&thread_db_ops);
984 using_thread_db = 1;
985 /* Turn on thread_db event-reporting API. */
986 enable_thread_event_reporting (main_threadagent);
987}
988
989static void
990thread_db_unpush_target (void)
991{
992 /* Must be called whenever we remove ourself from the target stack! */
993
994 using_thread_db = 0;
995 target_beneath = NULL;
996
997 /* delete local list of threads */
998 empty_threadlist ();
999 /* Turn off the thread_db API. */
1000 p_td_ta_delete (main_threadagent);
1001 /* Unpush this target vector */
1002 unpush_target (&thread_db_ops);
1003 /* Reset linuxthreads module. */
1004 linuxthreads_discard_global_state ();
1005}
1006
1007/*
1008 * New objfile hook function:
1009 * Called for each new objfile (image, shared lib) in the target process.
1010 *
1011 * The purpose of this function is to detect that the target process
1012 * is linked with the (appropriate) thread library. So every time a
1013 * new target shared library is detected, we will call td_ta_new.
1014 * If it succeeds, we know we have a multi-threaded target process
1015 * that we can debug using the thread_db API.
1016 */
1017
1018/*
1019 * new_objfile function:
1020 *
1021 * connected to target_new_objfile_hook, this function gets called
1022 * every time a new binary image is loaded.
1023 *
1024 * At each call, we attempt to open the thread_db connection to the
1025 * child process. If it succeeds, we know we have a libthread process
1026 * and we can debug it with this target vector. Therefore we push
1027 * ourself onto the target stack.
1028 */
1029
1030static void (*target_new_objfile_chain) (struct objfile *objfile);
1031static int stop_or_attach_thread_callback (const td_thrhandle_t *th,
1032 void *data);
1033static int wait_thread_callback (const td_thrhandle_t *th,
1034 void *data);
1035
1036static void
1037thread_db_new_objfile (struct objfile *objfile)
1038{
1039 td_err_e ret;
1040
1041 if (using_thread_db) /* libthread already detected, and */
1042 goto quit; /* thread target vector activated. */
1043
1044 if (objfile == NULL)
1045 goto quit; /* un-interesting object file */
1046
1047 /* Initialize our "main prochandle" with the main inferior pid. */
39f77062 1048 main_prochandle.pid = PIDGET (inferior_ptid);
ed9a39eb
JM
1049
1050 /* Now attempt to open a thread_db connection to the
1051 thread library running in the child process. */
1052 ret = p_td_ta_new (&main_prochandle, &main_threadagent);
1053 switch (ret) {
1054 default:
1055 warning ("Unexpected error initializing thread_db: %s",
1056 thr_err_string (ret));
1057 break;
1058 case TD_NOLIBTHREAD: /* expected: no libthread in child process (yet) */
1059 break;
1060 case TD_OK: /* libthread detected in child: we go live now! */
1061 thread_db_push_target ();
39f77062 1062 event_pid = PIDGET (inferior_ptid); /* for resume */
ed9a39eb
JM
1063
1064 /* Now stop everyone else, and attach any new threads you find. */
1065 p_td_ta_thr_iter (main_threadagent,
1066 stop_or_attach_thread_callback,
1067 (void *) 0,
1068 TD_THR_ANY_STATE,
1069 TD_THR_LOWEST_PRIORITY,
1070 TD_SIGNO_MASK,
1071 TD_THR_ANY_USER_FLAGS);
1072
1073 /* Now go call wait on all the threads you've stopped:
1074 This allows us to absorb the SIGKILL event, and to make sure
1075 that the thread knows that it is stopped (Linux peculiarity). */
1076 p_td_ta_thr_iter (main_threadagent,
1077 wait_thread_callback,
1078 (void *) 0,
1079 TD_THR_ANY_STATE,
1080 TD_THR_LOWEST_PRIORITY,
1081 TD_SIGNO_MASK,
1082 TD_THR_ANY_USER_FLAGS);
1083
1084 break;
1085 }
1086quit:
1087 if (target_new_objfile_chain)
1088 target_new_objfile_chain (objfile);
1089}
1090
1091
1092/*
1093
1094 LOCAL FUNCTION
1095
1096 thread_db_alive - test thread for "aliveness"
1097
1098 SYNOPSIS
1099
1100 static bool thread_db_alive (int pid);
1101
1102 DESCRIPTION
1103
1104 returns true if thread still active in inferior.
1105
1106 */
1107
1108static int
39f77062 1109thread_db_alive (ptid_t ptid)
ed9a39eb 1110{
39f77062 1111 if (is_thread (ptid)) /* user-space (non-kernel) thread */
ed9a39eb
JM
1112 {
1113 td_thrhandle_t th;
1114 td_err_e ret;
39f77062 1115 int pid = GET_THREAD (ptid);
ed9a39eb 1116
ed9a39eb
JM
1117 if ((ret = p_td_ta_map_id2thr (main_threadagent, pid, &th)) != TD_OK)
1118 return 0; /* thread not found */
1119 if ((ret = p_td_thr_validate (&th)) != TD_OK)
1120 return 0; /* thread not valid */
1121 return 1; /* known thread: return true */
1122 }
1123 else if (target_beneath->to_thread_alive)
39f77062 1124 return target_beneath->to_thread_alive (ptid);
ed9a39eb
JM
1125 else
1126 return 0; /* default to "not alive" (shouldn't happen anyway) */
1127}
1128
1129/*
1130 * get_lwp_from_thread_handle
1131 */
1132
1133static int /* lwpid_t or pid_t */
fba45db2 1134get_lwp_from_thread_handle (td_thrhandle_t *th)
ed9a39eb
JM
1135{
1136 td_thrinfo_t ti;
1137 td_err_e ret;
1138
1139 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1140 error ("get_lwp_from_thread_handle: thr_get_info failed: %s",
1141 thr_err_string (ret));
1142
1143 return ti.ti_lid;
1144}
1145
1146/*
1147 * get_lwp_from_thread_id
1148 */
1149
1150static int /* lwpid_t or pid_t */
064002de 1151get_lwp_from_thread_id (int tid /* thread_t? */)
ed9a39eb
JM
1152{
1153 td_thrhandle_t th;
1154 td_err_e ret;
1155
1156 if ((ret = p_td_ta_map_id2thr (main_threadagent, tid, &th)) != TD_OK)
1157 error ("get_lwp_from_thread_id: map_id2thr failed: %s",
1158 thr_err_string (ret));
1159
1160 return get_lwp_from_thread_handle (&th);
1161}
1162
1163/*
1164 * pid_to_str has to handle user-space threads.
1165 * If not a user-space thread, then pass the request on to the
1166 * underlying stratum if it can handle it: else call normal_pid_to_str.
1167 */
1168
1169static char *
39f77062 1170thread_db_pid_to_str (ptid_t ptid)
ed9a39eb
JM
1171{
1172 static char buf[100];
1173 td_thrhandle_t th;
1174 td_thrinfo_t ti;
1175 td_err_e ret;
1176
39f77062 1177 if (is_thread (ptid))
ed9a39eb
JM
1178 {
1179 if ((ret = p_td_ta_map_id2thr (main_threadagent,
39f77062 1180 GET_THREAD (ptid),
ed9a39eb
JM
1181 &th)) != TD_OK)
1182 error ("thread_db: map_id2thr failed: %s", thr_err_string (ret));
1183
1184 if ((ret = p_td_thr_get_info (&th, &ti)) != TD_OK)
1185 error ("thread_db: thr_get_info failed: %s", thr_err_string (ret));
1186
1187 if (ti.ti_state == TD_THR_ACTIVE &&
1188 ti.ti_lid != 0)
39f77062 1189 sprintf (buf, "Thread %ld (LWP %d)", ti.ti_tid, ti.ti_lid);
ed9a39eb 1190 else
39f77062 1191 sprintf (buf, "Thread %ld (%s)", ti.ti_tid,
ed9a39eb
JM
1192 thr_state_string (ti.ti_state));
1193 }
39f77062
KB
1194 else if (GET_LWP (ptid))
1195 sprintf (buf, "LWP %ld", GET_LWP (ptid));
1196 else return normal_pid_to_str (ptid);
ed9a39eb
JM
1197
1198 return buf;
1199}
1200
1201/*
1202 * thread_db target vector functions:
1203 */
1204
1205static void
1206thread_db_files_info (struct target_ops *tgt_vector)
1207{
1208 /* This function will be unnecessary in real life. */
1209 printf_filtered ("thread_db stratum:\n");
1210 target_beneath->to_files_info (tgt_vector);
1211}
1212
1213/*
39f77062 1214 * xfer_memory has to munge the inferior_ptid before passing the call
ed9a39eb
JM
1215 * down to the target layer.
1216 */
1217
1218static int
064002de 1219thread_db_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
043780a1 1220 struct mem_attrib *attrib,
064002de 1221 struct target_ops *target)
ed9a39eb
JM
1222{
1223 struct cleanup *old_chain;
1224 int ret;
1225
39f77062 1226 old_chain = save_inferior_ptid ();
ed9a39eb 1227
39f77062
KB
1228 if (is_thread (inferior_ptid) ||
1229 !target_thread_alive (inferior_ptid))
ed9a39eb
JM
1230 {
1231 /* FIXME: use the LID/LWP, so that underlying process layer
1232 can read memory from specific threads? */
39f77062 1233 inferior_ptid = pid_to_ptid (main_prochandle.pid);
ed9a39eb
JM
1234 }
1235
1236 ret = target_beneath->to_xfer_memory (memaddr, myaddr, len,
043780a1 1237 dowrite, attrib, target);
ed9a39eb
JM
1238 do_cleanups (old_chain);
1239 return ret;
1240}
1241
1242/*
39f77062 1243 * fetch_registers has to determine if inferior_ptid is a user-space thread.
ed9a39eb
JM
1244 * If so, we use the thread_db API to get the registers.
1245 * And if not, we call the underlying process stratum.
1246 */
1247
1248static void
fba45db2 1249thread_db_fetch_registers (int regno)
ed9a39eb
JM
1250{
1251 td_thrhandle_t thandle;
d84dd0c5 1252 gdb_prfpregset_t fpregset;
ed9a39eb
JM
1253 prgregset_t gregset;
1254 thread_t thread;
1255 td_err_e ret;
1256
39f77062 1257 if (!is_thread (inferior_ptid)) /* kernel thread */
ed9a39eb
JM
1258 { /* pass the request on to the target underneath. */
1259 target_beneath->to_fetch_registers (regno);
1260 return;
1261 }
1262
39f77062 1263 /* convert inferior_ptid into a td_thrhandle_t */
ed9a39eb 1264
39f77062 1265 if ((thread = GET_THREAD (inferior_ptid)) == 0)
ed9a39eb
JM
1266 error ("fetch_registers: thread == 0");
1267
1268 if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK)
1269 error ("fetch_registers: td_ta_map_id2thr: %s", thr_err_string (ret));
1270
1271 /* Get the integer regs:
1272 For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7,
1273 pc and sp are saved (by a thread context switch). */
1274 if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK &&
1275 ret != TD_PARTIALREG)
1276 error ("fetch_registers: td_thr_getgregs %s", thr_err_string (ret));
1277
1278 /* And, now the fp regs */
1279 if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK &&
1280 ret != TD_NOFPREGS)
1281 error ("fetch_registers: td_thr_getfpregs %s", thr_err_string (ret));
1282
1283/* Note that we must call supply_{g fp}regset *after* calling the td routines
1284 because the td routines call ps_lget* which affect the values stored in the
1285 registers array. */
1286
39f77062 1287 supply_gregset ((gdb_gregset_t *) gregset);
ed9a39eb
JM
1288 supply_fpregset (&fpregset);
1289
1290}
1291
1292/*
39f77062 1293 * store_registers has to determine if inferior_ptid is a user-space thread.
ed9a39eb
JM
1294 * If so, we use the thread_db API to get the registers.
1295 * And if not, we call the underlying process stratum.
1296 */
1297
1298static void
fba45db2 1299thread_db_store_registers (int regno)
ed9a39eb
JM
1300{
1301 td_thrhandle_t thandle;
d84dd0c5 1302 gdb_prfpregset_t fpregset;
ed9a39eb
JM
1303 prgregset_t gregset;
1304 thread_t thread;
1305 td_err_e ret;
1306
39f77062 1307 if (!is_thread (inferior_ptid)) /* Kernel thread: */
ed9a39eb
JM
1308 { /* pass the request on to the underlying target vector. */
1309 target_beneath->to_store_registers (regno);
1310 return;
1311 }
1312
39f77062 1313 /* convert inferior_ptid into a td_thrhandle_t */
ed9a39eb 1314
39f77062 1315 if ((thread = GET_THREAD (inferior_ptid)) == 0)
ed9a39eb
JM
1316 error ("store_registers: thread == 0");
1317
1318 if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK)
1319 error ("store_registers: td_ta_map_id2thr %s", thr_err_string (ret));
1320
1321 if (regno != -1)
1322 { /* Not writing all the regs */
1323 /* save new register value */
1324 /* MVS: I don't understand this... */
1325 char old_value[REGISTER_SIZE];
1326
1327 memcpy (old_value, &registers[REGISTER_BYTE (regno)], REGISTER_SIZE);
1328
1329 if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK)
1330 error ("store_registers: td_thr_getgregs %s", thr_err_string (ret));
1331 if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK)
1332 error ("store_registers: td_thr_getfpregs %s", thr_err_string (ret));
1333
1334 /* restore new register value */
1335 memcpy (&registers[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE);
1336
1337 }
1338
39f77062 1339 fill_gregset ((gdb_gregset_t *) gregset, regno);
ed9a39eb
JM
1340 fill_fpregset (&fpregset, regno);
1341
1342 if ((ret = p_td_thr_setgregs (&thandle, gregset)) != TD_OK)
1343 error ("store_registers: td_thr_setgregs %s", thr_err_string (ret));
1344 if ((ret = p_td_thr_setfpregs (&thandle, &fpregset)) != TD_OK &&
1345 ret != TD_NOFPREGS)
1346 error ("store_registers: td_thr_setfpregs %s", thr_err_string (ret));
1347}
1348
1349static void
fba45db2
KB
1350handle_new_thread (int tid, /* user thread id */
1351 int lid, /* kernel thread id */
1352 int verbose)
ed9a39eb 1353{
39f77062 1354 ptid_t gdb_ptid = BUILD_THREAD (tid, main_prochandle.pid);
ed9a39eb
JM
1355 int wait_pid, wait_status;
1356
1357 if (verbose)
39f77062
KB
1358 printf_filtered ("[New %s]\n", target_pid_to_str (gdb_ptid));
1359 add_thread (gdb_ptid);
ed9a39eb
JM
1360
1361 if (lid != main_prochandle.pid)
1362 {
1363 attach_thread (lid);
1364 /* According to the Eric Paire model, we now have to send
1365 the restart signal to the new thread -- however, empirically,
1366 I do not find that to be necessary. */
1367 attach_pid = lid;
1368 }
1369}
1370
1371static void
fba45db2 1372test_for_new_thread (int tid, int lid, int verbose)
ed9a39eb
JM
1373{
1374 if (!in_thread_list (BUILD_THREAD (tid, main_prochandle.pid)))
1375 handle_new_thread (tid, lid, verbose);
1376}
1377
1378/*
1379 * Callback function that gets called once per USER thread
1380 * (i.e., not kernel) thread by td_ta_thr_iter.
1381 */
1382
1383static int
fba45db2 1384find_new_threads_callback (const td_thrhandle_t *th, void *ignored)
ed9a39eb
JM
1385{
1386 td_thrinfo_t ti;
1387 td_err_e ret;
1388
1389 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1390 {
1391 warning ("find_new_threads_callback: %s", thr_err_string (ret));
1392 return -1; /* bail out, get_info failed. */
1393 }
1394
1395 /* FIXME:
1396 As things now stand, this should never detect a new thread.
1397 But if it does, we could be in trouble because we aren't calling
1398 wait_thread_callback for it. */
1399 test_for_new_thread (ti.ti_tid, ti.ti_lid, 0);
1400 return 0;
1401}
1402
1403/*
1404 * find_new_threads uses the thread_db iterator function to discover
1405 * user-space threads. Then if the underlying process stratum has a
1406 * find_new_threads method, we call that too.
1407 */
1408
1409static void
fba45db2 1410thread_db_find_new_threads (void)
ed9a39eb 1411{
39f77062 1412 if (PIDGET (inferior_ptid) == -1) /* FIXME: still necessary? */
ed9a39eb
JM
1413 {
1414 printf_filtered ("No process.\n");
1415 return;
1416 }
1417 p_td_ta_thr_iter (main_threadagent,
1418 find_new_threads_callback,
1419 (void *) 0,
1420 TD_THR_ANY_STATE,
1421 TD_THR_LOWEST_PRIORITY,
1422 TD_SIGNO_MASK,
1423 TD_THR_ANY_USER_FLAGS);
1424 if (target_beneath->to_find_new_threads)
1425 target_beneath->to_find_new_threads ();
1426}
1427
1428/*
1429 * Resume all threads, or resume a single thread.
1430 * If step is true, then single-step the appropriate thread
39f77062 1431 * (or single-step inferior_ptid, but continue everyone else).
ed9a39eb
JM
1432 * If signo is true, then send that signal to at least one thread.
1433 */
1434
1435/*
1436 * This function is called once for each thread before resuming.
1437 * It sends continue (no step, and no signal) to each thread except
1438 * the main thread, and
1439 * the event thread (the one that stopped at a breakpoint etc.)
1440 *
1441 * The event thread is handled separately so that it can be sent
1442 * the stepping and signal args with which target_resume was called.
1443 *
1444 * The main thread is resumed last, so that the thread_db proc_service
1445 * callbacks will still work during the iterator function.
1446 */
1447
1448static int
fba45db2 1449resume_thread_callback (const td_thrhandle_t *th, void *data)
ed9a39eb
JM
1450{
1451 td_thrinfo_t ti;
1452 td_err_e ret;
1453
1454 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1455 {
1456 warning ("resume_thread_callback: %s", thr_err_string (ret));
1457 return -1; /* bail out, get_info failed. */
1458 }
1459 /* FIXME:
1460 As things now stand, this should never detect a new thread.
1461 But if it does, we could be in trouble because we aren't calling
1462 wait_thread_callback for it. */
1463 test_for_new_thread (ti.ti_tid, ti.ti_lid, 1);
1464
1465 if (ti.ti_lid != main_prochandle.pid &&
1466 ti.ti_lid != event_pid)
1467 {
1468 /* Unconditionally continue the thread with no signal.
1469 Only the event thread will get a signal of any kind. */
1470
39f77062 1471 target_beneath->to_resume (pid_to_ptid (ti.ti_lid), 0, 0);
ed9a39eb
JM
1472 }
1473 return 0;
1474}
1475
1476static int
fba45db2 1477new_resume_thread_callback (threadinfo *thread, void *data)
ed9a39eb
JM
1478{
1479 if (thread->lid != event_pid &&
1480 thread->lid != main_prochandle.pid)
1481 {
1482 /* Unconditionally continue the thread with no signal (for now). */
1483
39f77062 1484 target_beneath->to_resume (pid_to_ptid (thread->lid), 0, 0);
ed9a39eb
JM
1485 }
1486 return 0;
1487}
1488
1489static int last_resume_pid;
1490static int last_resume_step;
1491static int last_resume_signo;
1492
1493static void
39f77062 1494thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
ed9a39eb 1495{
39f77062 1496 last_resume_pid = PIDGET (ptid);
ed9a39eb
JM
1497 last_resume_step = step;
1498 last_resume_signo = signo;
1499
1500 /* resuming a specific pid? */
39f77062 1501 if (PIDGET (ptid) != -1)
ed9a39eb 1502 {
39f77062
KB
1503 if (is_thread (ptid))
1504 ptid = pid_to_ptid (get_lwp_from_thread_id (GET_THREAD (ptid)));
1505 else if (GET_LWP (ptid))
1506 ptid = pid_to_ptid (GET_LWP (ptid));
ed9a39eb
JM
1507 }
1508
1509 /* Apparently the interpretation of 'pid' is dependent on 'step':
1510 If step is true, then a specific pid means 'step only this pid'.
1511 But if step is not true, then pid means 'continue ALL pids, but
1512 give the signal only to this one'. */
39f77062 1513 if (PIDGET (ptid) != -1 && step)
ed9a39eb
JM
1514 {
1515 /* FIXME: is this gonna work in all circumstances? */
39f77062 1516 target_beneath->to_resume (ptid, step, signo);
ed9a39eb
JM
1517 }
1518 else
1519 {
1520 /* 1) Continue all threads except the event thread and the main thread.
1521 2) resume the event thread with step and signo.
1522 3) If event thread != main thread, continue the main thread.
1523
1524 Note: order of 2 and 3 may need to be reversed. */
1525
1526 threadlist_iter (new_resume_thread_callback,
1527 (void *) 0,
1528 TD_THR_ANY_STATE,
1529 TD_THR_ANY_TYPE);
1530 /* now resume event thread, and if necessary also main thread. */
1531 if (event_pid)
1532 {
39f77062 1533 target_beneath->to_resume (pid_to_ptid (event_pid), step, signo);
ed9a39eb
JM
1534 }
1535 if (event_pid != main_prochandle.pid)
1536 {
39f77062 1537 target_beneath->to_resume (pid_to_ptid (main_prochandle.pid), 0, 0);
ed9a39eb
JM
1538 }
1539 }
1540}
1541
1542/* All new threads will be attached.
1543 All previously known threads will be stopped using kill (SIGKILL). */
1544
1545static int
1546stop_or_attach_thread_callback (const td_thrhandle_t *th, void *data)
1547{
1548 td_thrinfo_t ti;
1549 td_err_e ret;
39f77062 1550 ptid_t gdb_ptid;
ed9a39eb
JM
1551 int on_off = 1;
1552
1553 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1554 {
1555 warning ("stop_or_attach_thread_callback: %s", thr_err_string (ret));
1556 return -1; /* bail out, get_info failed. */
1557 }
1558
1559 /* First add it to our internal list.
1560 We build this list anew at every wait event. */
1561 insert_thread (ti.ti_tid, ti.ti_lid, ti.ti_state, ti.ti_type);
1562 /* Now: if we've already seen it, stop it, else add it and attach it. */
39f77062
KB
1563 gdb_ptid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid);
1564 if (!in_thread_list (gdb_ptid)) /* new thread */
ed9a39eb
JM
1565 {
1566 handle_new_thread (ti.ti_tid, ti.ti_lid, 1);
1567 /* Enable thread events */
1568 if (p_td_thr_event_enable)
1569 if ((ret = p_td_thr_event_enable (th, on_off)) != TD_OK)
1570 warning ("stop_or_attach_thread: %s", thr_err_string (ret));
1571 }
1572 else if (ti.ti_lid != event_pid &&
1573 ti.ti_lid != main_prochandle.pid)
1574 {
1575 ret = (td_err_e) kill (ti.ti_lid, SIGSTOP);
1576 }
1577
1578 return 0;
1579}
1580
1581/*
1582 * Wait for signal N from pid PID.
1583 * If wait returns any other signals, put them back before returning.
1584 */
1585
1586static void
fba45db2 1587wait_for_stop (int pid)
ed9a39eb
JM
1588{
1589 int i;
1590 int retpid;
1591 int status;
1592
1593 /* Array of wait/signal status */
1594 /* FIXME: wrong data structure, we need a queue.
1595 Realtime signals may be delivered more than once.
1596 And at that, we really can't handle them (see below). */
1597#if defined (NSIG)
1598 static int wstatus [NSIG];
1599#elif defined (_NSIG)
1600 static int wstatus [_NSIG];
1601#else
1602#error No definition for number of signals!
1603#endif
1604
1605 /* clear wait/status list */
1606 memset (&wstatus, 0, sizeof (wstatus));
1607
1608 /* Now look for SIGSTOP event on all threads except event thread. */
1609 do {
1610 errno = 0;
1611 if (pid == main_prochandle.pid)
1612 retpid = waitpid (pid, &status, 0);
1613 else
1614 retpid = waitpid (pid, &status, __WCLONE);
1615
1616 if (retpid > 0)
1617 if (WSTOPSIG (status) == SIGSTOP)
1618 {
1619 /* Got the SIGSTOP event we're looking for.
1620 Throw it away, and throw any other events back! */
1621 for (i = 0; i < sizeof(wstatus) / sizeof (wstatus[0]); i++)
1622 if (wstatus[i])
1623 if (i != SIGSTOP)
1624 {
1625 kill (retpid, i);
1626 }
1627 break; /* all done */
1628 }
1629 else
1630 {
1631 int signo;
1632 /* Oops, got an event other than SIGSTOP.
1633 Save it, and throw it back after we find the SIGSTOP event. */
1634
1635 /* FIXME (how?) This method is going to fail for realtime
1636 signals, which cannot be put back simply by using kill. */
1637
1638 if (WIFEXITED (status))
1639 error ("Ack! Thread Exited event. What do I do now???");
1640 else if (WIFSTOPPED (status))
1641 signo = WSTOPSIG (status);
1642 else
1643 signo = WTERMSIG (status);
1644
1645 /* If a thread other than the event thread has hit a GDB
1646 breakpoint (as opposed to some random trap signal), then
1647 just arrange for it to hit it again later. Back up the
1648 PC if necessary. Don't forward the SIGTRAP signal to
1649 the thread. We will handle the current event, eventually
1650 we will resume all the threads, and this one will get
1651 it's breakpoint trap again.
1652
1653 If we do not do this, then we run the risk that the user
1654 will delete or disable the breakpoint, but the thread will
1655 have already tripped on it. */
1656
1657 if (retpid != event_pid &&
1658 signo == SIGTRAP &&
39f77062 1659 breakpoint_inserted_here_p (read_pc_pid (pid_to_ptid (retpid)) -
ed9a39eb
JM
1660 DECR_PC_AFTER_BREAK))
1661 {
1662 /* Set the pc to before the trap and DO NOT re-send the signal */
1663 if (DECR_PC_AFTER_BREAK)
39f77062
KB
1664 write_pc_pid (read_pc_pid (pid_to_ptid (retpid))
1665 - DECR_PC_AFTER_BREAK,
1666 pid_to_ptid (retpid));
ed9a39eb
JM
1667 }
1668
1669 /* Since SIGINT gets forwarded to the entire process group
1670 (in the case where ^C is typed at the tty / console),
1671 just ignore all SIGINTs from other than the event thread. */
1672 else if (retpid != event_pid && signo == SIGINT)
1673 { /* do nothing. Signal will disappear into oblivion! */
1674 ;
1675 }
1676
1677 else /* This is some random signal other than a breakpoint. */
1678 {
1679 wstatus [signo] = 1;
1680 }
39f77062 1681 child_resume (pid_to_ptid (retpid), 0, TARGET_SIGNAL_0);
ed9a39eb
JM
1682 continue;
1683 }
1684
1685 } while (errno == 0 || errno == EINTR);
1686}
1687
1688/*
1689 * wait_thread_callback
1690 *
1691 * Calls waitpid for each thread, repeatedly if necessary, until
1692 * SIGSTOP is returned. Afterward, if any other signals were returned
1693 * by waitpid, return them to the thread's pending queue by calling kill.
1694 */
1695
1696static int
1697wait_thread_callback (const td_thrhandle_t *th, void *data)
1698{
1699 td_thrinfo_t ti;
1700 td_err_e ret;
1701
1702 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1703 {
1704 warning ("wait_thread_callback: %s", thr_err_string (ret));
1705 return -1; /* bail out, get_info failed. */
1706 }
1707
1708 /* This callback to act on all threads except the event thread: */
1709 if (ti.ti_lid == event_pid || /* no need to wait (no sigstop) */
1710 ti.ti_lid == main_prochandle.pid) /* no need to wait (already waited) */
1711 return 0; /* don't wait on the event thread. */
1712
1713 wait_for_stop (ti.ti_lid);
1714 return 0; /* finished: next thread. */
1715}
1716
1717static int
fba45db2 1718new_wait_thread_callback (threadinfo *thread, void *data)
ed9a39eb
JM
1719{
1720 /* don't wait on the event thread -- it's already stopped and waited.
1721 Ditto the main thread. */
1722 if (thread->lid != event_pid &&
1723 thread->lid != main_prochandle.pid)
1724 {
1725 wait_for_stop (thread->lid);
1726 }
1727 return 0;
1728}
1729
1730/*
1731 * Wait for any thread to stop, by calling the underlying wait method.
1732 * The PID returned by the underlying target may be a kernel thread,
1733 * in which case we will want to convert it to the corresponding
1734 * user-space thread.
1735 */
1736
39f77062
KB
1737static ptid_t
1738thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
ed9a39eb
JM
1739{
1740 td_thrhandle_t thandle;
1741 td_thrinfo_t ti;
1742 td_err_e ret;
1743 lwpid_t lwp;
1744 int retpid;
39f77062 1745 ptid_t retptid;
ed9a39eb
JM
1746 int status;
1747 int save_errno;
1748
1749 /* OK, we're about to wait for an event from the running inferior.
1750 Make sure we're ignoring the right signals. */
1751
1752 check_all_signal_numbers (); /* see if magic signals changed. */
1753
1754 event_pid = 0;
1755 attach_pid = 0;
1756
1757 /* FIXME: should I do the wait right here inline? */
1758#if 0
39f77062 1759 if (PIDGET (ptid) == -1)
ed9a39eb
JM
1760 lwp = -1;
1761 else
39f77062 1762 lwp = get_lwp_from_thread_id (GET_THREAD (ptid));
ed9a39eb
JM
1763#endif
1764
1765
1766 save_errno = linux_child_wait (-1, &retpid, &status);
1767 store_waitstatus (ourstatus, status);
1768
1769 /* Thread ID is irrelevant if the target process exited.
1770 FIXME: do I have any killing to do?
1771 Can I get this event mistakenly from a thread? */
1772 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
39f77062 1773 return pid_to_ptid (retpid);
ed9a39eb
JM
1774
1775 /* OK, we got an event of interest.
1776 Go stop all threads and look for new ones.
1777 FIXME: maybe don't do this for the restart signal? Optimization... */
1778 event_pid = retpid;
1779
1780 /* If the last call to resume was for a specific thread, then we don't
1781 need to stop everyone else: they should already be stopped. */
1782 if (last_resume_step == 0 || last_resume_pid == -1)
1783 {
1784 /* Main thread must be stopped before calling the iterator. */
1785 if (retpid != main_prochandle.pid)
1786 {
1787 kill (main_prochandle.pid, SIGSTOP);
1788 wait_for_stop (main_prochandle.pid);
1789 }
1790
1791 empty_threadlist ();
1792 /* Now stop everyone else, and attach any new threads you find. */
1793 p_td_ta_thr_iter (main_threadagent,
1794 stop_or_attach_thread_callback,
1795 (void *) 0,
1796 TD_THR_ANY_STATE,
1797 TD_THR_LOWEST_PRIORITY,
1798 TD_SIGNO_MASK,
1799 TD_THR_ANY_USER_FLAGS);
1800
1801 /* Now go call wait on all the threads we've stopped:
1802 This allows us to absorb the SIGKILL event, and to make sure
1803 that the thread knows that it is stopped (Linux peculiarity). */
1804
1805 threadlist_iter (new_wait_thread_callback,
1806 (void *) 0,
1807 TD_THR_ANY_STATE,
1808 TD_THR_ANY_TYPE);
1809 }
1810
1811 /* Convert the kernel thread id to the corresponding thread id. */
1812
1813 /* If the process layer does not furnish an lwp,
1814 then perhaps the returned pid IS the lwp... */
39f77062
KB
1815#if 0 /* Always true (if it'd compile...) */
1816 if ((lwp = GET_LWP (pid_to_ptid (retpid))) == 0)
1817#endif
ed9a39eb
JM
1818 lwp = retpid;
1819
1820 if ((ret = p_td_ta_map_lwp2thr (main_threadagent, lwp, &thandle)) != TD_OK)
39f77062 1821 return pid_to_ptid (retpid); /* LWP is not mapped onto a user-space thread. */
ed9a39eb
JM
1822
1823 if ((ret = p_td_thr_validate (&thandle)) != TD_OK)
39f77062 1824 return pid_to_ptid (retpid); /* LWP is not mapped onto a valid thread. */
ed9a39eb
JM
1825
1826 if ((ret = p_td_thr_get_info (&thandle, &ti)) != TD_OK)
1827 {
1828 warning ("thread_db: thr_get_info failed ('%s')", thr_err_string (ret));
39f77062 1829 return pid_to_ptid (retpid);
ed9a39eb
JM
1830 }
1831
39f77062 1832 retptid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid);
ed9a39eb 1833 /* If this is a new user thread, notify GDB about it. */
39f77062 1834 if (!in_thread_list (retptid))
ed9a39eb 1835 {
39f77062
KB
1836 printf_filtered ("[New %s]\n", target_pid_to_str (retptid));
1837 add_thread (retptid);
ed9a39eb
JM
1838 }
1839
1840#if 0
1841 /* Now detect if this is a thread creation/deletion event: */
1842 check_for_thread_event (ourstatus, retpid);
1843#endif
39f77062 1844 return retptid;
ed9a39eb
JM
1845}
1846
1847/*
1848 * kill has to call the underlying kill.
39f77062
KB
1849 * FIXME: I'm not sure if it's necessary to check inferior_ptid any more,
1850 * but we might need to fix inferior_ptid up if it's a user thread.
ed9a39eb
JM
1851 */
1852
1853static int
39f77062 1854kill_thread_callback (const td_thrhandle_t *th, void *data)
ed9a39eb
JM
1855{
1856 td_thrinfo_t ti;
1857 td_err_e ret;
1858
1859 /* Fixme:
1860 For Linux, threads may need to be waited. */
1861 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1862 {
1863 warning ("kill_thread_callback: %s", thr_err_string (ret));
1864 return -1; /* bail out, get_info failed. */
1865 }
1866
1867 if (ti.ti_lid != main_prochandle.pid)
1868 {
1869 kill (ti.ti_lid, SIGKILL);
1870 }
1871 return 0;
1872}
1873
1874
1875static void thread_db_kill (void)
1876{
1877 int rpid;
1878 int status;
1879
1880 /* Fixme:
1881 For Linux, threads may need to be waited. */
39f77062 1882 if (! ptid_equal (inferior_ptid, null_ptid))
ed9a39eb
JM
1883 {
1884 /* Go kill the children first. Save the main thread for last. */
1885 p_td_ta_thr_iter (main_threadagent,
1886 kill_thread_callback,
1887 (void *) 0,
1888 TD_THR_ANY_STATE,
1889 TD_THR_LOWEST_PRIORITY,
1890 TD_SIGNO_MASK,
1891 TD_THR_ANY_USER_FLAGS);
1892
1893 /* Turn off thread_db event-reporting API *before* killing the
1894 main thread, since this operation requires child memory access.
1895 Can't move this into thread_db_unpush target because then
1896 detach would not work. */
1897 disable_thread_event_reporting (main_threadagent);
1898
39f77062 1899 inferior_ptid = pid_to_ptid (main_prochandle.pid);
ed9a39eb
JM
1900
1901 /*
1902 * Since both procfs_kill and ptrace_kill call target_mourn,
1903 * it should be sufficient for me to call one of them.
1904 * That will result in my mourn being called, which will both
1905 * unpush me and call the underlying mourn.
1906 */
1907 target_beneath->to_kill ();
1908 }
1909
1910 /* Wait for all threads. */
1911 /* FIXME: need a universal wait_for_signal func? */
1912 do
1913 {
1914 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1915 }
1916 while (rpid > 0 || errno == EINTR);
1917
1918 do
1919 {
1920 rpid = waitpid (-1, &status, WNOHANG);
1921 }
1922 while (rpid > 0 || errno == EINTR);
1923}
1924
1925/*
1926 * Mourn has to remove us from the target stack,
1927 * and then call the underlying mourn.
1928 */
1929
1930static void thread_db_mourn_inferior (void)
1931{
1932 thread_db_unpush_target ();
1933 target_mourn_inferior (); /* call the underlying mourn */
1934}
1935
1936/*
1937 * Detach has to remove us from the target stack,
1938 * and then call the underlying detach.
1939 *
1940 * But first, it has to detach all the cloned threads!
1941 */
1942
1943static int
39f77062 1944detach_thread_callback (const td_thrhandle_t *th, void *data)
ed9a39eb
JM
1945{
1946 /* Called once per thread. */
1947 td_thrinfo_t ti;
1948 td_err_e ret;
1949
1950 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1951 {
1952 warning ("detach_thread_callback: %s", thr_err_string (ret));
1953 return -1; /* bail out, get_info failed. */
1954 }
1955
1956 if (!in_thread_list (BUILD_THREAD (ti.ti_tid, main_prochandle.pid)))
1957 return 0; /* apparently we don't know this one. */
1958
1959 /* Save main thread for last, or the iterator will fail! */
1960 if (ti.ti_lid != main_prochandle.pid)
1961 {
1962 struct cleanup *old_chain;
1963 int off = 0;
1964
1965 /* Time to detach this thread.
1966 First disable thread_db event reporting for the thread. */
1967 if (p_td_thr_event_enable &&
1968 (ret = p_td_thr_event_enable (th, off)) != TD_OK)
1969 {
1970 warning ("detach_thread_callback: %s\n", thr_err_string (ret));
1971 return 0;
1972 }
1973
1974 /* Now cancel any pending SIGTRAPS. FIXME! */
1975
1976 /* Call underlying detach method. FIXME just detach it. */
39f77062
KB
1977 old_chain = save_inferior_ptid ();
1978 inferior_ptid = pid_to_ptid (ti.ti_lid);
ed9a39eb
JM
1979 detach (TARGET_SIGNAL_0);
1980 do_cleanups (old_chain);
1981 }
1982 return 0;
1983}
1984
1985static void
1986thread_db_detach (char *args, int from_tty)
1987{
1988 td_err_e ret;
1989
1990 if ((ret = p_td_ta_thr_iter (main_threadagent,
1991 detach_thread_callback,
1992 (void *) 0,
1993 TD_THR_ANY_STATE,
1994 TD_THR_LOWEST_PRIORITY,
1995 TD_SIGNO_MASK,
1996 TD_THR_ANY_USER_FLAGS))
1997 != TD_OK)
1998 warning ("detach (thr_iter): %s", thr_err_string (ret));
1999
2000 /* Turn off thread_db event-reporting API
2001 (before detaching the main thread) */
2002 disable_thread_event_reporting (main_threadagent);
2003
2004 thread_db_unpush_target ();
2005
2006 /* above call nullifies target_beneath, so don't use that! */
39f77062 2007 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
ed9a39eb
JM
2008 target_detach (args, from_tty);
2009}
2010
2011
2012/*
2013 * We never want to actually create the inferior!
2014 *
2015 * If this is ever called, it means we were on the target stack
2016 * when the user said "run". But we don't want to be on the new
2017 * inferior's target stack until the thread_db / libthread
2018 * connection is ready to be made.
2019 *
2020 * So, what shall we do?
2021 * Unpush ourselves from the stack, and then invoke
2022 * find_default_create_inferior, which will invoke the
2023 * appropriate process_stratum target to do the create.
2024 */
2025
2026static void
fba45db2 2027thread_db_create_inferior (char *exec_file, char *allargs, char **env)
ed9a39eb
JM
2028{
2029 thread_db_unpush_target ();
2030 find_default_create_inferior (exec_file, allargs, env);
2031}
2032
2033/*
2034 * Thread_db target vector initializer.
2035 */
2036
2037void
fba45db2 2038init_thread_db_ops (void)
ed9a39eb
JM
2039{
2040 thread_db_ops.to_shortname = "multi-thread";
2041 thread_db_ops.to_longname = "multi-threaded child process.";
2042 thread_db_ops.to_doc = "Threads and pthreads support.";
2043 thread_db_ops.to_files_info = thread_db_files_info;
2044 thread_db_ops.to_create_inferior = thread_db_create_inferior;
2045 thread_db_ops.to_detach = thread_db_detach;
2046 thread_db_ops.to_wait = thread_db_wait;
2047 thread_db_ops.to_resume = thread_db_resume;
2048 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
2049 thread_db_ops.to_kill = thread_db_kill;
2050 thread_db_ops.to_xfer_memory = thread_db_xfer_memory;
2051 thread_db_ops.to_fetch_registers = thread_db_fetch_registers;
2052 thread_db_ops.to_store_registers = thread_db_store_registers;
2053 thread_db_ops.to_thread_alive = thread_db_alive;
2054 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
2055 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
2056 thread_db_ops.to_stratum = thread_stratum;
2057 thread_db_ops.to_has_thread_control = tc_schedlock;
2058 thread_db_ops.to_magic = OPS_MAGIC;
2059}
2060#endif /* HAVE_STDINT_H */
2061
2062/*
2063 * Module constructor / initializer function.
2064 * If connection to thread_db dynamic library is successful,
2065 * then initialize this module's target vectors and the
2066 * new_objfile hook.
2067 */
2068
2069
2070void
fba45db2 2071_initialize_thread_db (void)
ed9a39eb
JM
2072{
2073#ifdef HAVE_STDINT_H /* stub out entire module, leave initializer empty */
2074 if (init_thread_db_library ())
2075 {
2076 init_thread_db_ops ();
2077 add_target (&thread_db_ops);
2078 /*
2079 * Hook up to the new_objfile event.
2080 * If someone is already there, arrange for him to be called
2081 * after we are.
2082 */
2083 target_new_objfile_chain = target_new_objfile_hook;
2084 target_new_objfile_hook = thread_db_new_objfile;
2085 }
2086#endif /* HAVE_STDINT_H */
2087}
2088