2000-05-26 Michael Snyder <msnyder@seadog.cygnus.com>
[external/binutils.git] / gdb / sol-thread.c
1 /* Low level interface for debugging Solaris threads for GDB, the GNU debugger.
2    Copyright 1996, 1997, 1998 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 /* This module implements a sort of half target that sits between the
22    machine-independent parts of GDB and the /proc interface (procfs.c) to
23    provide access to the Solaris user-mode thread implementation.
24
25    Solaris threads are true user-mode threads, which are invoked via the thr_*
26    and pthread_* (native and Posix respectivly) interfaces.  These are mostly
27    implemented in user-space, with all thread context kept in various
28    structures that live in the user's heap.  These should not be confused with
29    lightweight processes (LWPs), which are implemented by the kernel, and
30    scheduled without explicit intervention by the process.
31
32    Just to confuse things a little, Solaris threads (both native and Posix) are
33    actually implemented using LWPs.  In general, there are going to be more
34    threads than LWPs.  There is no fixed correspondence between a thread and an
35    LWP.  When a thread wants to run, it gets scheduled onto the first available
36    LWP and can therefore migrate from one LWP to another as time goes on.  A
37    sleeping thread may not be associated with an LWP at all!
38
39    To make it possible to mess with threads, Sun provides a library called
40    libthread_db.so.1 (not to be confused with libthread_db.so.0, which doesn't
41    have a published interface).  This interface has an upper part, which it
42    provides, and a lower part which I provide.  The upper part consists of the
43    td_* routines, which allow me to find all the threads, query their state,
44    etc...  The lower part consists of all of the ps_*, which are used by the
45    td_* routines to read/write memory, manipulate LWPs, lookup symbols, etc...
46    The ps_* routines actually do most of their work by calling functions in
47    procfs.c.  */
48
49 #include "defs.h"
50 #include <thread.h>
51 #include <proc_service.h>
52 #include <thread_db.h>
53 #include "gdbthread.h"
54 #include "target.h"
55 #include "inferior.h"
56 #include <fcntl.h>
57 #include <sys/stat.h>
58 #include <dlfcn.h>
59 #include "gdbcmd.h"
60
61 extern struct target_ops sol_thread_ops;        /* Forward declaration */
62 extern struct target_ops sol_core_ops;  /* Forward declaration */
63
64 /* place to store core_ops before we overwrite it */
65 static struct target_ops orig_core_ops;
66
67 struct target_ops sol_thread_ops;
68 struct target_ops sol_core_ops;
69
70 extern int procfs_suppress_run;
71 extern struct target_ops procfs_ops;    /* target vector for procfs.c */
72 extern struct target_ops core_ops;      /* target vector for corelow.c */
73 extern char *procfs_pid_to_str PARAMS ((int pid));
74
75 /* Prototypes for supply_gregset etc. */
76 #include "gregset.h"
77
78 /* This struct is defined by us, but mainly used for the proc_service interface.
79    We don't have much use for it, except as a handy place to get a real pid
80    for memory accesses.  */
81
82 struct ps_prochandle
83   {
84     pid_t pid;
85   };
86
87 struct string_map
88   {
89     int num;
90     char *str;
91   };
92
93 static struct ps_prochandle main_ph;
94 static td_thragent_t *main_ta;
95 static int sol_thread_active = 0;
96
97 static struct cleanup *save_inferior_pid PARAMS ((void));
98 static void restore_inferior_pid PARAMS ((void *pid));
99 static char *td_err_string PARAMS ((td_err_e errcode));
100 static char *td_state_string PARAMS ((td_thr_state_e statecode));
101 static int thread_to_lwp PARAMS ((int thread_id, int default_lwp));
102 static void sol_thread_resume PARAMS ((int pid, int step,
103                                        enum target_signal signo));
104 static int lwp_to_thread PARAMS ((int lwp));
105 static int sol_thread_alive PARAMS ((int pid));
106 static void sol_core_close PARAMS ((int quitting));
107
108 static void init_sol_thread_ops PARAMS ((void));
109 static void init_sol_core_ops PARAMS ((void));
110
111 /* Default definitions: These must be defined in tm.h 
112    if they are to be shared with a process module such as procfs.  */
113
114 #define THREAD_FLAG             0x80000000
115 #define is_thread(ARG)          (((ARG) & THREAD_FLAG) != 0)
116 #define is_lwp(ARG)             (((ARG) & THREAD_FLAG) == 0)
117 #define GET_LWP(PID)            TIDGET (PID)
118 #define GET_THREAD(PID)         TIDGET (PID)
119 #define BUILD_LWP(TID, PID)     MERGEPID (PID, TID)
120
121 #define BUILD_THREAD(TID, PID)  (MERGEPID (PID, TID) | THREAD_FLAG)
122
123 /* Pointers to routines from lithread_db resolved by dlopen() */
124
125 static void     (*p_td_log)               (const int on_off);
126 static td_err_e (*p_td_ta_new)            (const struct ps_prochandle * ph_p, 
127                                            td_thragent_t ** ta_pp);
128 static td_err_e (*p_td_ta_delete)         (td_thragent_t * ta_p);
129 static td_err_e (*p_td_init)              (void);
130 static td_err_e (*p_td_ta_get_ph)         (const td_thragent_t * ta_p, 
131                                            struct ps_prochandle ** ph_pp);
132 static td_err_e (*p_td_ta_get_nthreads)   (const td_thragent_t * ta_p, 
133                                            int *nthread_p);
134 static td_err_e (*p_td_ta_tsd_iter)       (const td_thragent_t * ta_p, 
135                                            td_key_iter_f * cb, 
136                                            void *cbdata_p);
137 static td_err_e (*p_td_ta_thr_iter)       (const td_thragent_t * ta_p, 
138                                            td_thr_iter_f * cb, 
139                                            void *cbdata_p, 
140                                            td_thr_state_e state,
141                                            int ti_pri, 
142                                            sigset_t * ti_sigmask_p, 
143                                            unsigned ti_user_flags);
144 static td_err_e (*p_td_thr_validate)      (const td_thrhandle_t * th_p);
145 static td_err_e (*p_td_thr_tsd)           (const td_thrhandle_t * th_p, 
146                                            const thread_key_t key, 
147                                            void **data_pp);
148 static td_err_e (*p_td_thr_get_info)      (const td_thrhandle_t * th_p, 
149                                            td_thrinfo_t * ti_p);
150 static td_err_e (*p_td_thr_getfpregs)     (const td_thrhandle_t * th_p, 
151                                            prfpregset_t * fpregset);
152 static td_err_e (*p_td_thr_getxregsize)   (const td_thrhandle_t * th_p, 
153                                            int *xregsize);
154 static td_err_e (*p_td_thr_getxregs)      (const td_thrhandle_t * th_p, 
155                                            const caddr_t xregset);
156 static td_err_e (*p_td_thr_sigsetmask)    (const td_thrhandle_t * th_p, 
157                                            const sigset_t ti_sigmask);
158 static td_err_e (*p_td_thr_setprio)       (const td_thrhandle_t * th_p, 
159                                            const int ti_pri);
160 static td_err_e (*p_td_thr_setsigpending) (const td_thrhandle_t * th_p, 
161                                            const uchar_t ti_pending_flag, 
162                                            const sigset_t ti_pending);
163 static td_err_e (*p_td_thr_setfpregs)     (const td_thrhandle_t * th_p, 
164                                            const prfpregset_t * fpregset);
165 static td_err_e (*p_td_thr_setxregs)      (const td_thrhandle_t * th_p, 
166                                            const caddr_t xregset);
167 static td_err_e (*p_td_ta_map_id2thr)     (const td_thragent_t * ta_p, 
168                                            thread_t tid, 
169                                            td_thrhandle_t * th_p);
170 static td_err_e (*p_td_ta_map_lwp2thr)    (const td_thragent_t * ta_p, 
171                                            lwpid_t lwpid, 
172                                            td_thrhandle_t * th_p);
173 static td_err_e (*p_td_thr_getgregs)      (const td_thrhandle_t * th_p, 
174                                            prgregset_t regset);
175 static td_err_e (*p_td_thr_setgregs)      (const td_thrhandle_t * th_p, 
176                                            const prgregset_t regset);
177
178 /*
179
180    LOCAL FUNCTION
181
182    td_err_string - Convert a thread_db error code to a string
183
184    SYNOPSIS
185
186    char * td_err_string (errcode)
187
188    DESCRIPTION
189
190    Return the thread_db error string associated with errcode.  If errcode
191    is unknown, then return a message.
192
193  */
194
195 static char *
196 td_err_string (errcode)
197      td_err_e errcode;
198 {
199   static struct string_map
200     td_err_table[] =
201   {
202     {TD_OK, "generic \"call succeeded\""},
203     {TD_ERR, "generic error."},
204     {TD_NOTHR, "no thread can be found to satisfy query"},
205     {TD_NOSV, "no synch. variable can be found to satisfy query"},
206     {TD_NOLWP, "no lwp can be found to satisfy query"},
207     {TD_BADPH, "invalid process handle"},
208     {TD_BADTH, "invalid thread handle"},
209     {TD_BADSH, "invalid synchronization handle"},
210     {TD_BADTA, "invalid thread agent"},
211     {TD_BADKEY, "invalid key"},
212     {TD_NOMSG, "td_thr_event_getmsg() called when there was no message"},
213     {TD_NOFPREGS, "FPU register set not available for given thread"},
214     {TD_NOLIBTHREAD, "application not linked with libthread"},
215     {TD_NOEVENT, "requested event is not supported"},
216     {TD_NOCAPAB, "capability not available"},
217     {TD_DBERR, "Debugger service failed"},
218     {TD_NOAPLIC, "Operation not applicable to"},
219     {TD_NOTSD, "No thread specific data for this thread"},
220     {TD_MALLOC, "Malloc failed"},
221     {TD_PARTIALREG, "Only part of register set was writen/read"},
222     {TD_NOXREGS, "X register set not available for given thread"}
223   };
224   const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
225   int i;
226   static char buf[50];
227
228   for (i = 0; i < td_err_size; i++)
229     if (td_err_table[i].num == errcode)
230       return td_err_table[i].str;
231
232   sprintf (buf, "Unknown thread_db error code: %d", errcode);
233
234   return buf;
235 }
236 \f
237 /*
238
239    LOCAL FUNCTION
240
241    td_state_string - Convert a thread_db state code to a string
242
243    SYNOPSIS
244
245    char * td_state_string (statecode)
246
247    DESCRIPTION
248
249    Return the thread_db state string associated with statecode.  If
250    statecode is unknown, then return a message.
251
252  */
253
254 static char *
255 td_state_string (statecode)
256      td_thr_state_e statecode;
257 {
258   static struct string_map
259     td_thr_state_table[] =
260   {
261     {TD_THR_ANY_STATE, "any state"},
262     {TD_THR_UNKNOWN, "unknown"},
263     {TD_THR_STOPPED, "stopped"},
264     {TD_THR_RUN, "run"},
265     {TD_THR_ACTIVE, "active"},
266     {TD_THR_ZOMBIE, "zombie"},
267     {TD_THR_SLEEP, "sleep"},
268     {TD_THR_STOPPED_ASLEEP, "stopped asleep"}
269   };
270   const int td_thr_state_table_size = sizeof td_thr_state_table / sizeof (struct string_map);
271   int i;
272   static char buf[50];
273
274   for (i = 0; i < td_thr_state_table_size; i++)
275     if (td_thr_state_table[i].num == statecode)
276       return td_thr_state_table[i].str;
277
278   sprintf (buf, "Unknown thread_db state code: %d", statecode);
279
280   return buf;
281 }
282 \f
283 /*
284
285    LOCAL FUNCTION
286
287    thread_to_lwp - Convert a Posix or Solaris thread id to a LWP id.
288
289    SYNOPSIS
290
291    int thread_to_lwp (thread_id, default_lwp)
292
293    DESCRIPTION
294
295    This function converts a Posix or Solaris thread id to a lightweight
296    process id.  If thread_id is non-existent, that's an error.  If it's
297    an inactive thread, then we return default_lwp.
298
299    NOTES
300
301    This function probably shouldn't call error()...
302
303  */
304
305 static int
306 thread_to_lwp (thread_id, default_lwp)
307      int thread_id;
308      int default_lwp;
309 {
310   td_thrinfo_t ti;
311   td_thrhandle_t th;
312   td_err_e val;
313
314   if (is_lwp (thread_id))
315     return thread_id;           /* It's already an LWP id */
316
317   /* It's a thread.  Convert to lwp */
318
319   val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th);
320   if (val == TD_NOTHR)
321     return -1;                  /* thread must have terminated */
322   else if (val != TD_OK)
323     error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val));
324
325   val = p_td_thr_get_info (&th, &ti);
326   if (val == TD_NOTHR)
327     return -1;                  /* thread must have terminated */
328   else if (val != TD_OK)
329     error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val));
330
331   if (ti.ti_state != TD_THR_ACTIVE)
332     {
333       if (default_lwp != -1)
334         return default_lwp;
335       error ("thread_to_lwp: thread state not active: %s",
336              td_state_string (ti.ti_state));
337     }
338
339   return BUILD_LWP (ti.ti_lid, PIDGET (thread_id));
340 }
341 \f
342 /*
343
344    LOCAL FUNCTION
345
346    lwp_to_thread - Convert a LWP id to a Posix or Solaris thread id.
347
348    SYNOPSIS
349
350    int lwp_to_thread (lwp_id)
351
352    DESCRIPTION
353
354    This function converts a lightweight process id to a Posix or Solaris
355    thread id.  If thread_id is non-existent, that's an error.
356
357    NOTES
358
359    This function probably shouldn't call error()...
360
361  */
362
363 static int
364 lwp_to_thread (lwp)
365      int lwp;
366 {
367   td_thrinfo_t ti;
368   td_thrhandle_t th;
369   td_err_e val;
370
371   if (is_thread (lwp))
372     return lwp;                 /* It's already a thread id */
373
374   /* It's an lwp.  Convert it to a thread id.  */
375
376   if (!sol_thread_alive (lwp))
377     return -1;                  /* defunct lwp */
378
379   val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th);
380   if (val == TD_NOTHR)
381     return -1;                  /* thread must have terminated */
382   else if (val != TD_OK)
383     error ("lwp_to_thread: td_ta_map_lwp2thr: %s.", td_err_string (val));
384
385   val = p_td_thr_validate (&th);
386   if (val == TD_NOTHR)
387     return lwp;                 /* libthread doesn't know about it;
388                                    just return lwp */
389   else if (val != TD_OK)
390     error ("lwp_to_thread: td_thr_validate: %s.", td_err_string (val));
391
392   val = p_td_thr_get_info (&th, &ti);
393   if (val == TD_NOTHR)
394     return -1;                  /* thread must have terminated */
395   else if (val != TD_OK)
396     error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val));
397
398   return BUILD_THREAD (ti.ti_tid, PIDGET (lwp));
399 }
400 \f
401 /*
402
403    LOCAL FUNCTION
404
405    save_inferior_pid - Save inferior_pid on the cleanup list
406    restore_inferior_pid - Restore inferior_pid from the cleanup list
407
408    SYNOPSIS
409
410    struct cleanup *save_inferior_pid ()
411    void restore_inferior_pid (int pid)
412
413    DESCRIPTION
414
415    These two functions act in unison to restore inferior_pid in
416    case of an error.
417
418    NOTES
419
420    inferior_pid is a global variable that needs to be changed by many of
421    these routines before calling functions in procfs.c.  In order to
422    guarantee that inferior_pid gets restored (in case of errors), you
423    need to call save_inferior_pid before changing it.  At the end of the
424    function, you should invoke do_cleanups to restore it.
425
426  */
427
428
429 static struct cleanup *
430 save_inferior_pid ()
431 {
432   return make_cleanup (restore_inferior_pid, (void *) inferior_pid);
433 }
434
435 static void
436 restore_inferior_pid (pid)
437      void *pid;
438 {
439   inferior_pid = (int) pid;
440 }
441 \f
442
443 /* Most target vector functions from here on actually just pass through to
444    procfs.c, as they don't need to do anything specific for threads.  */
445
446
447 /* ARGSUSED */
448 static void
449 sol_thread_open (arg, from_tty)
450      char *arg;
451      int from_tty;
452 {
453   procfs_ops.to_open (arg, from_tty);
454 }
455
456 /* Attach to process PID, then initialize for debugging it
457    and wait for the trace-trap that results from attaching.  */
458
459 static void
460 sol_thread_attach (args, from_tty)
461      char *args;
462      int from_tty;
463 {
464   procfs_ops.to_attach (args, from_tty);
465   /* Must get symbols from solibs before libthread_db can run! */
466   SOLIB_ADD ((char *) 0, from_tty, (struct target_ops *) 0);
467   if (sol_thread_active)
468     {
469       printf_filtered ("sol-thread active.\n");
470       main_ph.pid = inferior_pid;       /* Save for xfer_memory */
471       push_target (&sol_thread_ops);
472       inferior_pid = lwp_to_thread (inferior_pid);
473       if (inferior_pid == -1)
474         inferior_pid = main_ph.pid;
475       else
476         add_thread (inferior_pid);
477     }
478   /* XXX - might want to iterate over all the threads and register them. */
479 }
480
481 /* Take a program previously attached to and detaches it.
482    The program resumes execution and will no longer stop
483    on signals, etc.  We'd better not have left any breakpoints
484    in the program or it'll die when it hits one.  For this
485    to work, it may be necessary for the process to have been
486    previously attached.  It *might* work if the program was
487    started via the normal ptrace (PTRACE_TRACEME).  */
488
489 static void
490 sol_thread_detach (args, from_tty)
491      char *args;
492      int from_tty;
493 {
494   inferior_pid = PIDGET (main_ph.pid);
495   unpush_target (&sol_thread_ops);
496   procfs_ops.to_detach (args, from_tty);
497 }
498
499 /* Resume execution of process PID.  If STEP is nozero, then
500    just single step it.  If SIGNAL is nonzero, restart it with that
501    signal activated.  We may have to convert pid from a thread-id to an LWP id
502    for procfs.  */
503
504 static void
505 sol_thread_resume (pid, step, signo)
506      int pid;
507      int step;
508      enum target_signal signo;
509 {
510   struct cleanup *old_chain;
511
512   old_chain = save_inferior_pid ();
513
514   inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
515   if (inferior_pid == -1)
516     inferior_pid = procfs_first_available ();
517
518   if (pid != -1)
519     {
520       int save_pid = pid;
521
522       pid = thread_to_lwp (pid, -2);
523       if (pid == -2)            /* Inactive thread */
524         error ("This version of Solaris can't start inactive threads.");
525       if (info_verbose && pid == -1)
526         warning ("Specified thread %d seems to have terminated",
527                  GET_THREAD (save_pid));
528     }
529
530   procfs_ops.to_resume (pid, step, signo);
531
532   do_cleanups (old_chain);
533 }
534
535 /* Wait for any threads to stop.  We may have to convert PID from a thread id
536    to a LWP id, and vice versa on the way out.  */
537
538 static int
539 sol_thread_wait (pid, ourstatus)
540      int pid;
541      struct target_waitstatus *ourstatus;
542 {
543   int rtnval;
544   int save_pid;
545   struct cleanup *old_chain;
546
547   save_pid = inferior_pid;
548   old_chain = save_inferior_pid ();
549
550   inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
551   if (inferior_pid == -1)
552     inferior_pid = procfs_first_available ();
553
554   if (pid != -1)
555     {
556       int save_pid = pid;
557
558       pid = thread_to_lwp (pid, -2);
559       if (pid == -2)            /* Inactive thread */
560         error ("This version of Solaris can't start inactive threads.");
561       if (info_verbose && pid == -1)
562         warning ("Specified thread %d seems to have terminated",
563                  GET_THREAD (save_pid));
564     }
565
566   rtnval = procfs_ops.to_wait (pid, ourstatus);
567
568   if (ourstatus->kind != TARGET_WAITKIND_EXITED)
569     {
570       /* Map the LWP of interest back to the appropriate thread ID */
571       rtnval = lwp_to_thread (rtnval);
572       if (rtnval == -1)
573         rtnval = save_pid;
574
575       /* See if we have a new thread */
576       if (is_thread (rtnval)
577           && rtnval != save_pid
578           && !in_thread_list (rtnval))
579         {
580           printf_filtered ("[New %s]\n", target_pid_to_str (rtnval));
581           add_thread (rtnval);
582         }
583     }
584
585   /* During process initialization, we may get here without the thread package
586      being initialized, since that can only happen after we've found the shared
587      libs.  */
588
589   do_cleanups (old_chain);
590
591   return rtnval;
592 }
593
594 static void
595 sol_thread_fetch_registers (regno)
596      int regno;
597 {
598   thread_t thread;
599   td_thrhandle_t thandle;
600   td_err_e val;
601   prgregset_t gregset;
602   prfpregset_t fpregset;
603 #if 0
604   int xregsize;
605   caddr_t xregset;
606 #endif
607
608   if (!is_thread (inferior_pid))
609     {                           /* LWP: pass the request on to procfs.c */
610       if (target_has_execution)
611         procfs_ops.to_fetch_registers (regno);
612       else
613         orig_core_ops.to_fetch_registers (regno);
614       return;
615     }
616
617   /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
618
619   thread = GET_THREAD (inferior_pid);
620
621   if (thread == 0)
622     error ("sol_thread_fetch_registers:  thread == 0");
623
624   val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
625   if (val != TD_OK)
626     error ("sol_thread_fetch_registers: td_ta_map_id2thr: %s",
627            td_err_string (val));
628
629   /* Get the integer regs */
630
631   val = p_td_thr_getgregs (&thandle, gregset);
632   if (val != TD_OK
633       && val != TD_PARTIALREG)
634     error ("sol_thread_fetch_registers: td_thr_getgregs %s",
635            td_err_string (val));
636
637   /* For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, pc and sp
638      are saved (by a thread context switch).  */
639
640   /* And, now the fp regs */
641
642   val = p_td_thr_getfpregs (&thandle, &fpregset);
643   if (val != TD_OK
644       && val != TD_NOFPREGS)
645     error ("sol_thread_fetch_registers: td_thr_getfpregs %s",
646            td_err_string (val));
647
648 /* Note that we must call supply_{g fp}regset *after* calling the td routines
649    because the td routines call ps_lget* which affect the values stored in the
650    registers array.  */
651
652   supply_gregset  ((gdb_gregset_t *)  &gregset);
653   supply_fpregset ((gdb_fpregset_t *) &fpregset);
654
655 #if 0
656 /* thread_db doesn't seem to handle this right */
657   val = td_thr_getxregsize (&thandle, &xregsize);
658   if (val != TD_OK && val != TD_NOXREGS)
659     error ("sol_thread_fetch_registers: td_thr_getxregsize %s",
660            td_err_string (val));
661
662   if (val == TD_OK)
663     {
664       xregset = alloca (xregsize);
665       val = td_thr_getxregs (&thandle, xregset);
666       if (val != TD_OK)
667         error ("sol_thread_fetch_registers: td_thr_getxregs %s",
668                td_err_string (val));
669     }
670 #endif
671 }
672
673 static void
674 sol_thread_store_registers (regno)
675      int regno;
676 {
677   thread_t thread;
678   td_thrhandle_t thandle;
679   td_err_e val;
680   prgregset_t  gregset;
681   prfpregset_t fpregset;
682 #if 0
683   int xregsize;
684   caddr_t xregset;
685 #endif
686
687   if (!is_thread (inferior_pid))
688     {                           /* LWP: pass the request on to procfs.c */
689       procfs_ops.to_store_registers (regno);
690       return;
691     }
692
693   /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
694
695   thread = GET_THREAD (inferior_pid);
696
697   val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
698   if (val != TD_OK)
699     error ("sol_thread_store_registers: td_ta_map_id2thr %s",
700            td_err_string (val));
701
702   if (regno != -1)
703     {                           /* Not writing all the regs */
704       /* save new register value */
705       char old_value[REGISTER_SIZE];
706       memcpy (old_value, &registers[REGISTER_BYTE (regno)], REGISTER_SIZE);
707
708       val = p_td_thr_getgregs (&thandle, gregset);
709       if (val != TD_OK)
710         error ("sol_thread_store_registers: td_thr_getgregs %s",
711                td_err_string (val));
712       val = p_td_thr_getfpregs (&thandle, &fpregset);
713       if (val != TD_OK)
714         error ("sol_thread_store_registers: td_thr_getfpregs %s",
715                td_err_string (val));
716
717       /* restore new register value */
718       memcpy (&registers[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE);
719
720 #if 0
721 /* thread_db doesn't seem to handle this right */
722       val = td_thr_getxregsize (&thandle, &xregsize);
723       if (val != TD_OK && val != TD_NOXREGS)
724         error ("sol_thread_store_registers: td_thr_getxregsize %s",
725                td_err_string (val));
726
727       if (val == TD_OK)
728         {
729           xregset = alloca (xregsize);
730           val = td_thr_getxregs (&thandle, xregset);
731           if (val != TD_OK)
732             error ("sol_thread_store_registers: td_thr_getxregs %s",
733                    td_err_string (val));
734         }
735 #endif
736     }
737
738   fill_gregset  ((gdb_gregset_t *)  &gregset,  regno);
739   fill_fpregset ((gdb_fpregset_t *) &fpregset, regno);
740
741   val = p_td_thr_setgregs (&thandle, gregset);
742   if (val != TD_OK)
743     error ("sol_thread_store_registers: td_thr_setgregs %s",
744            td_err_string (val));
745   val = p_td_thr_setfpregs (&thandle, &fpregset);
746   if (val != TD_OK)
747     error ("sol_thread_store_registers: td_thr_setfpregs %s",
748            td_err_string (val));
749
750 #if 0
751 /* thread_db doesn't seem to handle this right */
752   val = td_thr_getxregsize (&thandle, &xregsize);
753   if (val != TD_OK && val != TD_NOXREGS)
754     error ("sol_thread_store_registers: td_thr_getxregsize %s",
755            td_err_string (val));
756
757   /* Should probably do something about writing the xregs here, but what are
758      they? */
759 #endif
760 }
761
762 /* Get ready to modify the registers array.  On machines which store
763    individual registers, this doesn't need to do anything.  On machines
764    which store all the registers in one fell swoop, this makes sure
765    that registers contains all the registers from the program being
766    debugged.  */
767
768 static void
769 sol_thread_prepare_to_store ()
770 {
771   procfs_ops.to_prepare_to_store ();
772 }
773
774 static int
775 sol_thread_xfer_memory (memaddr, myaddr, len, dowrite, target)
776      CORE_ADDR memaddr;
777      char *myaddr;
778      int len;
779      int dowrite;
780      struct target_ops *target; /* ignored */
781 {
782   int retval;
783   struct cleanup *old_chain;
784
785   old_chain = save_inferior_pid ();
786
787   if (is_thread (inferior_pid) ||       /* A thread */
788       !target_thread_alive (inferior_pid))      /* An lwp, but not alive */
789     inferior_pid = procfs_first_available ();   /* Find any live lwp.  */
790   /* Note: don't need to call switch_to_thread; we're just reading memory.  */
791
792   if (target_has_execution)
793     retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
794   else
795     retval = orig_core_ops.to_xfer_memory (memaddr, myaddr, len,
796                                            dowrite, target);
797
798   do_cleanups (old_chain);
799
800   return retval;
801 }
802
803 /* Print status information about what we're accessing.  */
804
805 static void
806 sol_thread_files_info (ignore)
807      struct target_ops *ignore;
808 {
809   procfs_ops.to_files_info (ignore);
810 }
811
812 static void
813 sol_thread_kill_inferior ()
814 {
815   procfs_ops.to_kill ();
816 }
817
818 static void
819 sol_thread_notice_signals (pid)
820      int pid;
821 {
822   procfs_ops.to_notice_signals (PIDGET (pid));
823 }
824
825 /* Fork an inferior process, and start debugging it with /proc.  */
826
827 static void
828 sol_thread_create_inferior (exec_file, allargs, env)
829      char *exec_file;
830      char *allargs;
831      char **env;
832 {
833   procfs_ops.to_create_inferior (exec_file, allargs, env);
834
835   if (sol_thread_active && inferior_pid != 0)
836     {
837       main_ph.pid = inferior_pid;       /* Save for xfer_memory */
838
839       push_target (&sol_thread_ops);
840
841       inferior_pid = lwp_to_thread (inferior_pid);
842       if (inferior_pid == -1)
843         inferior_pid = main_ph.pid;
844
845       if (!in_thread_list (inferior_pid))
846         add_thread (inferior_pid);
847     }
848 }
849
850 /* This routine is called whenever a new symbol table is read in, or when all
851    symbol tables are removed.  libthread_db can only be initialized when it
852    finds the right variables in libthread.so.  Since it's a shared library,
853    those variables don't show up until the library gets mapped and the symbol
854    table is read in.  */
855
856 /* This new_objfile event is now managed by a chained function pointer. 
857  * It is the callee's responsability to call the next client on the chain.
858  */
859
860 /* Saved pointer to previous owner of the new_objfile event. */
861 static void (*target_new_objfile_chain) PARAMS ((struct objfile *));
862
863 void
864 sol_thread_new_objfile (objfile)
865      struct objfile *objfile;
866 {
867   td_err_e val;
868
869   if (!objfile)
870     {
871       sol_thread_active = 0;
872       goto quit;
873     }
874
875   /* don't do anything if init failed to resolve the libthread_db library */
876   if (!procfs_suppress_run)
877     goto quit;
878
879   /* Now, initialize the thread debugging library.  This needs to be done after
880      the shared libraries are located because it needs information from the
881      user's thread library.  */
882
883   val = p_td_init ();
884   if (val != TD_OK)
885     {
886       warning ("sol_thread_new_objfile: td_init: %s", td_err_string (val));
887       goto quit;
888     }
889
890   val = p_td_ta_new (&main_ph, &main_ta);
891   if (val == TD_NOLIBTHREAD)
892     goto quit;
893   else if (val != TD_OK)
894     {
895       warning ("sol_thread_new_objfile: td_ta_new: %s", td_err_string (val));
896       goto quit;
897     }
898
899   sol_thread_active = 1;
900 quit:
901   /* Call predecessor on chain, if any. */
902   if (target_new_objfile_chain)
903     target_new_objfile_chain (objfile);
904 }
905
906 /* Clean up after the inferior dies.  */
907
908 static void
909 sol_thread_mourn_inferior ()
910 {
911   unpush_target (&sol_thread_ops);
912   procfs_ops.to_mourn_inferior ();
913 }
914
915 /* Mark our target-struct as eligible for stray "run" and "attach" commands.  */
916
917 static int
918 sol_thread_can_run ()
919 {
920   return procfs_suppress_run;
921 }
922
923 /* 
924
925    LOCAL FUNCTION
926
927    sol_thread_alive     - test thread for "aliveness"
928
929    SYNOPSIS
930
931    static bool sol_thread_alive (int pid);
932
933    DESCRIPTION
934
935    returns true if thread still active in inferior.
936
937  */
938
939 static int
940 sol_thread_alive (pid)
941      int pid;
942 {
943   if (is_thread (pid))          /* non-kernel thread */
944     {
945       td_err_e val;
946       td_thrhandle_t th;
947
948       pid = GET_THREAD (pid);
949       if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK)
950         return 0;               /* thread not found */
951       if ((val = p_td_thr_validate (&th)) != TD_OK)
952         return 0;               /* thread not valid */
953       return 1;                 /* known thread: return true */
954     }
955   else
956     /* kernel thread (LWP): let procfs test it */
957     {
958       if (target_has_execution)
959         return procfs_ops.to_thread_alive (pid);
960       else
961         return orig_core_ops.to_thread_alive (pid);
962     }
963 }
964
965 static void
966 sol_thread_stop ()
967 {
968   procfs_ops.to_stop ();
969 }
970 \f
971 /* These routines implement the lower half of the thread_db interface.  Ie: the
972    ps_* routines.  */
973
974 /* Various versions of <proc_service.h> have slightly
975    different function prototypes.  In particular, we have
976
977    NEWER                        OLDER
978    struct ps_prochandle *       const struct ps_prochandle *
979    void*                        char*
980    const void*          char*
981    int                  size_t
982
983    Which one you have depends on solaris version and what
984    patches you've applied.  On the theory that there are
985    only two major variants, we have configure check the
986    prototype of ps_pdwrite (), and use that info to make
987    appropriate typedefs here. */
988
989 #ifdef PROC_SERVICE_IS_OLD
990 typedef const struct ps_prochandle *gdb_ps_prochandle_t;
991 typedef char *gdb_ps_read_buf_t;
992 typedef char *gdb_ps_write_buf_t;
993 typedef int gdb_ps_size_t;
994 typedef paddr_t gdb_ps_addr_t;
995 #else
996 typedef struct ps_prochandle *gdb_ps_prochandle_t;
997 typedef void *gdb_ps_read_buf_t;
998 typedef const void *gdb_ps_write_buf_t;
999 typedef size_t gdb_ps_size_t;
1000 typedef psaddr_t gdb_ps_addr_t;
1001 #endif
1002
1003
1004 /* The next four routines are called by thread_db to tell us to stop and stop
1005    a particular process or lwp.  Since GDB ensures that these are all stopped
1006    by the time we call anything in thread_db, these routines need to do
1007    nothing.  */
1008
1009 /* Process stop */
1010
1011 ps_err_e
1012 ps_pstop (gdb_ps_prochandle_t ph)
1013 {
1014   return PS_OK;
1015 }
1016
1017 /* Process continue */
1018
1019 ps_err_e
1020 ps_pcontinue (gdb_ps_prochandle_t ph)
1021 {
1022   return PS_OK;
1023 }
1024
1025 /* LWP stop */
1026
1027 ps_err_e
1028 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1029 {
1030   return PS_OK;
1031 }
1032
1033 /* LWP continue */
1034
1035 ps_err_e
1036 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1037 {
1038   return PS_OK;
1039 }
1040
1041 /* Looks up the symbol LD_SYMBOL_NAME in the debugger's symbol table.  */
1042
1043 ps_err_e
1044 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *ld_object_name,
1045                    const char *ld_symbol_name, gdb_ps_addr_t * ld_symbol_addr)
1046 {
1047   struct minimal_symbol *ms;
1048
1049   ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
1050
1051   if (!ms)
1052     return PS_NOSYM;
1053
1054   *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
1055
1056   return PS_OK;
1057 }
1058
1059 /* Common routine for reading and writing memory.  */
1060
1061 static ps_err_e
1062 rw_common (int dowrite, const struct ps_prochandle *ph, gdb_ps_addr_t addr,
1063            char *buf, int size)
1064 {
1065   struct cleanup *old_chain;
1066
1067   old_chain = save_inferior_pid ();
1068
1069   if (is_thread (inferior_pid) ||       /* A thread */
1070       !target_thread_alive (inferior_pid))      /* An lwp, but not alive */
1071     inferior_pid = procfs_first_available ();   /* Find any live lwp.  */
1072   /* Note: don't need to call switch_to_thread; we're just reading memory.  */
1073
1074   while (size > 0)
1075     {
1076       int cc;
1077
1078       if (target_has_execution)
1079         cc = procfs_ops.to_xfer_memory (addr, buf, size, dowrite, &procfs_ops);
1080       else
1081         cc = orig_core_ops.to_xfer_memory (addr, buf, size, dowrite, &core_ops);
1082
1083       if (cc < 0)
1084         {
1085           if (dowrite == 0)
1086             print_sys_errmsg ("rw_common (): read", errno);
1087           else
1088             print_sys_errmsg ("rw_common (): write", errno);
1089
1090           do_cleanups (old_chain);
1091
1092           return PS_ERR;
1093         }
1094       size -= cc;
1095       buf += cc;
1096     }
1097
1098   do_cleanups (old_chain);
1099
1100   return PS_OK;
1101 }
1102
1103 /* Copies SIZE bytes from target process .data segment to debugger memory.  */
1104
1105 ps_err_e
1106 ps_pdread (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1107            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1108 {
1109   return rw_common (0, ph, addr, buf, size);
1110 }
1111
1112 /* Copies SIZE bytes from debugger memory .data segment to target process.  */
1113
1114 ps_err_e
1115 ps_pdwrite (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1116             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1117 {
1118   return rw_common (1, ph, addr, (char *) buf, size);
1119 }
1120
1121 /* Copies SIZE bytes from target process .text segment to debugger memory.  */
1122
1123 ps_err_e
1124 ps_ptread (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1125            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1126 {
1127   return rw_common (0, ph, addr, buf, size);
1128 }
1129
1130 /* Copies SIZE bytes from debugger memory .text segment to target process.  */
1131
1132 ps_err_e
1133 ps_ptwrite (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1134             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1135 {
1136   return rw_common (1, ph, addr, (char *) buf, size);
1137 }
1138
1139 /* Get integer regs for LWP */
1140
1141 ps_err_e
1142 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1143              prgregset_t gregset)
1144 {
1145   struct cleanup *old_chain;
1146
1147   old_chain = save_inferior_pid ();
1148
1149   inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1150
1151   if (target_has_execution)
1152     procfs_ops.to_fetch_registers (-1);
1153   else
1154     orig_core_ops.to_fetch_registers (-1);
1155   fill_gregset ((gdb_gregset_t *) gregset, -1);
1156
1157   do_cleanups (old_chain);
1158
1159   return PS_OK;
1160 }
1161
1162 /* Set integer regs for LWP */
1163
1164 ps_err_e
1165 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1166              const prgregset_t gregset)
1167 {
1168   struct cleanup *old_chain;
1169
1170   old_chain = save_inferior_pid ();
1171
1172   inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1173
1174   supply_gregset ((gdb_gregset_t *) gregset);
1175   if (target_has_execution)
1176     procfs_ops.to_store_registers (-1);
1177   else
1178     orig_core_ops.to_store_registers (-1);
1179
1180   do_cleanups (old_chain);
1181
1182   return PS_OK;
1183 }
1184
1185 /* Log a message (sends to gdb_stderr).  */
1186
1187 void
1188 ps_plog (const char *fmt,...)
1189 {
1190   va_list args;
1191
1192   va_start (args, fmt);
1193
1194   vfprintf_filtered (gdb_stderr, fmt, args);
1195 }
1196
1197 /* Get size of extra register set.  Currently a noop.  */
1198
1199 ps_err_e
1200 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
1201 {
1202 #if 0
1203   int lwp_fd;
1204   int regsize;
1205   ps_err_e val;
1206
1207   val = get_lwp_fd (ph, lwpid, &lwp_fd);
1208   if (val != PS_OK)
1209     return val;
1210
1211   if (ioctl (lwp_fd, PIOCGXREGSIZE, &regsize))
1212     {
1213       if (errno == EINVAL)
1214         return PS_NOFREGS;      /* XXX Wrong code, but this is the closest
1215                                    thing in proc_service.h  */
1216
1217       print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno);
1218       return PS_ERR;
1219     }
1220 #endif
1221
1222   return PS_OK;
1223 }
1224
1225 /* Get extra register set.  Currently a noop.  */
1226
1227 ps_err_e
1228 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1229 {
1230 #if 0
1231   int lwp_fd;
1232   ps_err_e val;
1233
1234   val = get_lwp_fd (ph, lwpid, &lwp_fd);
1235   if (val != PS_OK)
1236     return val;
1237
1238   if (ioctl (lwp_fd, PIOCGXREG, xregset))
1239     {
1240       print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno);
1241       return PS_ERR;
1242     }
1243 #endif
1244
1245   return PS_OK;
1246 }
1247
1248 /* Set extra register set.  Currently a noop.  */
1249
1250 ps_err_e
1251 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1252 {
1253 #if 0
1254   int lwp_fd;
1255   ps_err_e val;
1256
1257   val = get_lwp_fd (ph, lwpid, &lwp_fd);
1258   if (val != PS_OK)
1259     return val;
1260
1261   if (ioctl (lwp_fd, PIOCSXREG, xregset))
1262     {
1263       print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno);
1264       return PS_ERR;
1265     }
1266 #endif
1267
1268   return PS_OK;
1269 }
1270
1271 /* Get floating-point regs for LWP */
1272
1273 ps_err_e
1274 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1275                prfpregset_t * fpregset)
1276 {
1277   struct cleanup *old_chain;
1278
1279   old_chain = save_inferior_pid ();
1280
1281   inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1282
1283   if (target_has_execution)
1284     procfs_ops.to_fetch_registers (-1);
1285   else
1286     orig_core_ops.to_fetch_registers (-1);
1287   fill_fpregset ((gdb_fpregset_t *) fpregset, -1);
1288
1289   do_cleanups (old_chain);
1290
1291   return PS_OK;
1292 }
1293
1294 /* Set floating-point regs for LWP */
1295
1296 ps_err_e
1297 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1298                const prfpregset_t * fpregset)
1299 {
1300   struct cleanup *old_chain;
1301
1302   old_chain = save_inferior_pid ();
1303
1304   inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1305
1306   supply_fpregset ((gdb_fpregset_t *) fpregset);
1307   if (target_has_execution)
1308     procfs_ops.to_store_registers (-1);
1309   else
1310     orig_core_ops.to_store_registers (-1);
1311
1312   do_cleanups (old_chain);
1313
1314   return PS_OK;
1315 }
1316
1317 #ifdef TM_I386SOL2_H
1318
1319 /* Reads the local descriptor table of a LWP.  */
1320
1321 ps_err_e
1322 ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1323             struct ssd *pldt)
1324 {
1325   /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */
1326   extern struct ssd *procfs_find_LDT_entry (int);
1327   struct ssd *ret;
1328
1329   /* FIXME: can't I get the process ID from the prochandle or something?
1330    */
1331
1332   if (inferior_pid <= 0 || lwpid <= 0)
1333     return PS_BADLID;
1334
1335   ret = procfs_find_LDT_entry (BUILD_LWP (lwpid, PIDGET (inferior_pid)));
1336   if (ret)
1337     {
1338       memcpy (pldt, ret, sizeof (struct ssd));
1339       return PS_OK;
1340     }
1341   else  /* LDT not found. */
1342     return PS_ERR;
1343 }
1344 #endif /* TM_I386SOL2_H */
1345 \f
1346 /* Convert a pid to printable form. */
1347
1348 char *
1349 solaris_pid_to_str (pid)
1350      int pid;
1351 {
1352   static char buf[100];
1353
1354   /* in case init failed to resolve the libthread_db library */
1355   if (!procfs_suppress_run)
1356     return procfs_pid_to_str (pid);
1357
1358   if (is_thread (pid))
1359     {
1360       int lwp;
1361
1362       lwp = thread_to_lwp (pid, -2);
1363
1364       if (lwp == -1)
1365         sprintf (buf, "Thread %d (defunct)", GET_THREAD (pid));
1366       else if (lwp != -2)
1367         sprintf (buf, "Thread %d (LWP %d)", GET_THREAD (pid), GET_LWP (lwp));
1368       else
1369         sprintf (buf, "Thread %d        ", GET_THREAD (pid));
1370     }
1371   else if (GET_LWP (pid) != 0)
1372     sprintf (buf, "LWP    %d        ", GET_LWP (pid));
1373   else
1374     sprintf (buf, "process %d    ", PIDGET (pid));
1375
1376   return buf;
1377 }
1378 \f
1379
1380 /* Worker bee for find_new_threads
1381    Callback function that gets called once per USER thread (i.e., not
1382    kernel) thread. */
1383
1384 static int
1385 sol_find_new_threads_callback (th, ignored)
1386      const td_thrhandle_t *th;
1387      void *ignored;
1388 {
1389   td_err_e retval;
1390   td_thrinfo_t ti;
1391   int pid;
1392
1393   if ((retval = p_td_thr_get_info (th, &ti)) != TD_OK)
1394     {
1395       return -1;
1396     }
1397   pid = BUILD_THREAD (ti.ti_tid, PIDGET (inferior_pid));
1398   if (!in_thread_list (pid))
1399     add_thread (pid);
1400
1401   return 0;
1402 }
1403
1404 static void
1405 sol_find_new_threads ()
1406 {
1407   /* don't do anything if init failed to resolve the libthread_db library */
1408   if (!procfs_suppress_run)
1409     return;
1410
1411   if (inferior_pid == -1)
1412     {
1413       printf_filtered ("No process.\n");
1414       return;
1415     }
1416   procfs_find_new_threads ();   /* first find new kernel threads. */
1417   p_td_ta_thr_iter (main_ta, sol_find_new_threads_callback, (void *) 0,
1418                     TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1419                     TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1420 }
1421
1422 static void
1423 sol_core_open (filename, from_tty)
1424      char *filename;
1425      int from_tty;
1426 {
1427   orig_core_ops.to_open (filename, from_tty);
1428 }
1429
1430 static void
1431 sol_core_close (quitting)
1432      int quitting;
1433 {
1434   orig_core_ops.to_close (quitting);
1435 }
1436
1437 static void
1438 sol_core_detach (args, from_tty)
1439      char *args;
1440      int from_tty;
1441 {
1442   unpush_target (&core_ops);
1443   orig_core_ops.to_detach (args, from_tty);
1444 }
1445
1446 static void
1447 sol_core_files_info (t)
1448      struct target_ops *t;
1449 {
1450   orig_core_ops.to_files_info (t);
1451 }
1452
1453 /* Worker bee for info sol-thread command.  This is a callback function that
1454    gets called once for each Solaris thread (ie. not kernel thread) in the 
1455    inferior.  Print anything interesting that we can think of.  */
1456
1457 static int
1458 info_cb (th, s)
1459      const td_thrhandle_t *th;
1460      void *s;
1461 {
1462   td_err_e ret;
1463   td_thrinfo_t ti;
1464
1465   if ((ret = p_td_thr_get_info (th, &ti)) == TD_OK)
1466     {
1467       printf_filtered ("%s thread #%d, lwp %d, ",
1468                        ti.ti_type == TD_THR_SYSTEM ? "system" : "user  ",
1469                        ti.ti_tid, ti.ti_lid);
1470       switch (ti.ti_state)
1471         {
1472         default:
1473         case TD_THR_UNKNOWN:
1474           printf_filtered ("<unknown state>");
1475           break;
1476         case TD_THR_STOPPED:
1477           printf_filtered ("(stopped)");
1478           break;
1479         case TD_THR_RUN:
1480           printf_filtered ("(run)    ");
1481           break;
1482         case TD_THR_ACTIVE:
1483           printf_filtered ("(active) ");
1484           break;
1485         case TD_THR_ZOMBIE:
1486           printf_filtered ("(zombie) ");
1487           break;
1488         case TD_THR_SLEEP:
1489           printf_filtered ("(asleep) ");
1490           break;
1491         case TD_THR_STOPPED_ASLEEP:
1492           printf_filtered ("(stopped asleep)");
1493           break;
1494         }
1495       /* Print thr_create start function: */
1496       if (ti.ti_startfunc != 0)
1497         {
1498           struct minimal_symbol *msym;
1499           msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc);
1500           if (msym)
1501             printf_filtered ("   startfunc: %s\n", SYMBOL_NAME (msym));
1502           else
1503             printf_filtered ("   startfunc: 0x%s\n", paddr (ti.ti_startfunc));
1504         }
1505
1506       /* If thread is asleep, print function that went to sleep: */
1507       if (ti.ti_state == TD_THR_SLEEP)
1508         {
1509           struct minimal_symbol *msym;
1510           msym = lookup_minimal_symbol_by_pc (ti.ti_pc);
1511           if (msym)
1512             printf_filtered (" - Sleep func: %s\n", SYMBOL_NAME (msym));
1513           else
1514             printf_filtered (" - Sleep func: 0x%s\n", paddr (ti.ti_startfunc));
1515         }
1516
1517       /* Wrap up line, if necessary */
1518       if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0)
1519         printf_filtered ("\n"); /* don't you hate counting newlines? */
1520     }
1521   else
1522     warning ("info sol-thread: failed to get info for thread.");
1523
1524   return 0;
1525 }
1526
1527 /* List some state about each Solaris user thread in the inferior.  */
1528
1529 static void
1530 info_solthreads (args, from_tty)
1531      char *args;
1532      int from_tty;
1533 {
1534   p_td_ta_thr_iter (main_ta, info_cb, args,
1535                     TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1536                     TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1537 }
1538
1539 static int
1540 ignore (addr, contents)
1541      CORE_ADDR addr;
1542      char *contents;
1543 {
1544   return 0;
1545 }
1546
1547
1548 static void
1549 init_sol_thread_ops ()
1550 {
1551   sol_thread_ops.to_shortname = "solaris-threads";
1552   sol_thread_ops.to_longname = "Solaris threads and pthread.";
1553   sol_thread_ops.to_doc = "Solaris threads and pthread support.";
1554   sol_thread_ops.to_open = sol_thread_open;
1555   sol_thread_ops.to_close = 0;
1556   sol_thread_ops.to_attach = sol_thread_attach;
1557   sol_thread_ops.to_detach = sol_thread_detach;
1558   sol_thread_ops.to_resume = sol_thread_resume;
1559   sol_thread_ops.to_wait = sol_thread_wait;
1560   sol_thread_ops.to_fetch_registers = sol_thread_fetch_registers;
1561   sol_thread_ops.to_store_registers = sol_thread_store_registers;
1562   sol_thread_ops.to_prepare_to_store = sol_thread_prepare_to_store;
1563   sol_thread_ops.to_xfer_memory = sol_thread_xfer_memory;
1564   sol_thread_ops.to_files_info = sol_thread_files_info;
1565   sol_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1566   sol_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1567   sol_thread_ops.to_terminal_init = terminal_init_inferior;
1568   sol_thread_ops.to_terminal_inferior = terminal_inferior;
1569   sol_thread_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1570   sol_thread_ops.to_terminal_ours = terminal_ours;
1571   sol_thread_ops.to_terminal_info = child_terminal_info;
1572   sol_thread_ops.to_kill = sol_thread_kill_inferior;
1573   sol_thread_ops.to_load = 0;
1574   sol_thread_ops.to_lookup_symbol = 0;
1575   sol_thread_ops.to_create_inferior = sol_thread_create_inferior;
1576   sol_thread_ops.to_mourn_inferior = sol_thread_mourn_inferior;
1577   sol_thread_ops.to_can_run = sol_thread_can_run;
1578   sol_thread_ops.to_notice_signals = sol_thread_notice_signals;
1579   sol_thread_ops.to_thread_alive = sol_thread_alive;
1580   sol_thread_ops.to_pid_to_str = solaris_pid_to_str;
1581   sol_thread_ops.to_find_new_threads = sol_find_new_threads;
1582   sol_thread_ops.to_stop = sol_thread_stop;
1583   sol_thread_ops.to_stratum = process_stratum;
1584   sol_thread_ops.to_has_all_memory = 1;
1585   sol_thread_ops.to_has_memory = 1;
1586   sol_thread_ops.to_has_stack = 1;
1587   sol_thread_ops.to_has_registers = 1;
1588   sol_thread_ops.to_has_execution = 1;
1589   sol_thread_ops.to_has_thread_control = tc_none;
1590   sol_thread_ops.to_sections = 0;
1591   sol_thread_ops.to_sections_end = 0;
1592   sol_thread_ops.to_magic = OPS_MAGIC;
1593 }
1594
1595
1596 static void
1597 init_sol_core_ops ()
1598 {
1599   sol_core_ops.to_shortname = "solaris-core";
1600   sol_core_ops.to_longname = "Solaris core threads and pthread.";
1601   sol_core_ops.to_doc = "Solaris threads and pthread support for core files.";
1602   sol_core_ops.to_open = sol_core_open;
1603   sol_core_ops.to_close = sol_core_close;
1604   sol_core_ops.to_attach = sol_thread_attach;
1605   sol_core_ops.to_detach = sol_core_detach;
1606   /* sol_core_ops.to_resume  = 0; */
1607   /* sol_core_ops.to_wait  = 0;  */
1608   sol_core_ops.to_fetch_registers = sol_thread_fetch_registers;
1609   /* sol_core_ops.to_store_registers  = 0; */
1610   /* sol_core_ops.to_prepare_to_store  = 0; */
1611   sol_core_ops.to_xfer_memory = sol_thread_xfer_memory;
1612   sol_core_ops.to_files_info = sol_core_files_info;
1613   sol_core_ops.to_insert_breakpoint = ignore;
1614   sol_core_ops.to_remove_breakpoint = ignore;
1615   /* sol_core_ops.to_terminal_init  = 0; */
1616   /* sol_core_ops.to_terminal_inferior  = 0; */
1617   /* sol_core_ops.to_terminal_ours_for_output  = 0; */
1618   /* sol_core_ops.to_terminal_ours  = 0; */
1619   /* sol_core_ops.to_terminal_info  = 0; */
1620   /* sol_core_ops.to_kill  = 0; */
1621   /* sol_core_ops.to_load  = 0; */
1622   /* sol_core_ops.to_lookup_symbol  = 0; */
1623   sol_core_ops.to_create_inferior = sol_thread_create_inferior;
1624   sol_core_ops.to_stratum = core_stratum;
1625   sol_core_ops.to_has_all_memory = 0;
1626   sol_core_ops.to_has_memory = 1;
1627   sol_core_ops.to_has_stack = 1;
1628   sol_core_ops.to_has_registers = 1;
1629   sol_core_ops.to_has_execution = 0;
1630   sol_core_ops.to_has_thread_control = tc_none;
1631   sol_core_ops.to_thread_alive = sol_thread_alive;
1632   sol_core_ops.to_pid_to_str = solaris_pid_to_str;
1633   /* On Solaris/x86, when debugging a threaded core file from process <n>,
1634      the following causes "info threads" to produce "procfs: couldn't find pid
1635      <n> in procinfo list" where <n> is the pid of the process that produced
1636      the core file.  Disable it for now. */
1637   /* sol_core_ops.to_find_new_threads = sol_find_new_threads; */
1638   sol_core_ops.to_sections = 0;
1639   sol_core_ops.to_sections_end = 0;
1640   sol_core_ops.to_magic = OPS_MAGIC;
1641 }
1642
1643 /* we suppress the call to add_target of core_ops in corelow because
1644    if there are two targets in the stratum core_stratum, find_core_target
1645    won't know which one to return.  see corelow.c for an additonal
1646    comment on coreops_suppress_target. */
1647 int coreops_suppress_target = 1;
1648
1649 void
1650 _initialize_sol_thread ()
1651 {
1652   void *dlhandle;
1653
1654   init_sol_thread_ops ();
1655   init_sol_core_ops ();
1656
1657   dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1658   if (!dlhandle)
1659     goto die;
1660
1661 #define resolve(X) \
1662   if (!(p_##X = dlsym (dlhandle, #X))) \
1663     goto die;
1664
1665   resolve (td_log);
1666   resolve (td_ta_new);
1667   resolve (td_ta_delete);
1668   resolve (td_init);
1669   resolve (td_ta_get_ph);
1670   resolve (td_ta_get_nthreads);
1671   resolve (td_ta_tsd_iter);
1672   resolve (td_ta_thr_iter);
1673   resolve (td_thr_validate);
1674   resolve (td_thr_tsd);
1675   resolve (td_thr_get_info);
1676   resolve (td_thr_getfpregs);
1677   resolve (td_thr_getxregsize);
1678   resolve (td_thr_getxregs);
1679   resolve (td_thr_sigsetmask);
1680   resolve (td_thr_setprio);
1681   resolve (td_thr_setsigpending);
1682   resolve (td_thr_setfpregs);
1683   resolve (td_thr_setxregs);
1684   resolve (td_ta_map_id2thr);
1685   resolve (td_ta_map_lwp2thr);
1686   resolve (td_thr_getgregs);
1687   resolve (td_thr_setgregs);
1688
1689   add_target (&sol_thread_ops);
1690
1691   procfs_suppress_run = 1;
1692
1693   add_cmd ("sol-threads", class_maintenance, info_solthreads,
1694            "Show info on Solaris user threads.\n", &maintenanceinfolist);
1695
1696   memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops));
1697   memcpy (&core_ops, &sol_core_ops, sizeof (struct target_ops));
1698   add_target (&core_ops);
1699
1700   /* Hook into new_objfile notification. */
1701   target_new_objfile_chain = target_new_objfile_hook;
1702   target_new_objfile_hook  = sol_thread_new_objfile;
1703   return;
1704
1705 die:
1706
1707   fprintf_unfiltered (gdb_stderr, "[GDB will not be able to debug user-mode threads: %s]\n", dlerror ());
1708
1709   if (dlhandle)
1710     dlclose (dlhandle);
1711
1712   /* allow the user to debug non-threaded core files */
1713   add_target (&core_ops);
1714
1715   return;
1716 }