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