1 /* Low level interface for debugging Solaris threads for GDB, the GNU debugger.
2 Copyright 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
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.
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.
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!
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
51 #include <proc_service.h>
52 #include <thread_db.h>
53 #include "gdbthread.h"
62 extern struct target_ops sol_thread_ops; /* Forward declaration */
63 extern struct target_ops sol_core_ops; /* Forward declaration */
65 /* place to store core_ops before we overwrite it */
66 static struct target_ops orig_core_ops;
68 struct target_ops sol_thread_ops;
69 struct target_ops sol_core_ops;
71 extern int procfs_suppress_run;
72 extern struct target_ops procfs_ops; /* target vector for procfs.c */
73 extern struct target_ops core_ops; /* target vector for corelow.c */
74 extern char *procfs_pid_to_str PARAMS ((int pid));
76 /* Note that these prototypes differ slightly from those used in procfs.c
77 for of two reasons. One, we can't use gregset_t, as that's got a whole
78 different meaning under Solaris (also, see above). Two, we can't use the
79 pointer form here as these are actually arrays of ints (for Sparc's at
80 least), and are automatically coerced into pointers to ints when used as
81 parameters. That makes it impossible to avoid a compiler warning when
82 passing pr{g fp}regset_t's from a parameter to an argument of one of
85 extern void supply_gregset PARAMS ((const prgregset_t));
86 extern void fill_gregset PARAMS ((prgregset_t, int));
87 extern void supply_fpregset PARAMS ((const prfpregset_t *));
88 extern void fill_fpregset PARAMS ((prfpregset_t *, int));
90 /* This struct is defined by us, but mainly used for the proc_service interface.
91 We don't have much use for it, except as a handy place to get a real pid
92 for memory accesses. */
105 static struct ps_prochandle main_ph;
106 static td_thragent_t *main_ta;
107 static int sol_thread_active = 0;
109 static struct cleanup *save_inferior_pid PARAMS ((void));
110 static void restore_inferior_pid PARAMS ((void *pid));
111 static char *td_err_string PARAMS ((td_err_e errcode));
112 static char *td_state_string PARAMS ((td_thr_state_e statecode));
113 static int thread_to_lwp PARAMS ((int thread_id, int default_lwp));
114 static void sol_thread_resume PARAMS ((int pid, int step,
115 enum target_signal signo));
116 static int lwp_to_thread PARAMS ((int lwp));
117 static int sol_thread_alive PARAMS ((int pid));
118 static void sol_core_close PARAMS ((int quitting));
120 static void init_sol_thread_ops PARAMS ((void));
121 static void init_sol_core_ops PARAMS ((void));
123 #define THREAD_FLAG 0x80000000
124 #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
125 #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
126 #define GET_LWP(LWP_ID) (TIDGET(LWP_ID))
127 #define GET_THREAD(THREAD_ID) (((THREAD_ID) >> 16) & 0x7fff)
128 #define BUILD_LWP(LWP_ID, PID) ((LWP_ID) << 16 | (PID))
129 #define BUILD_THREAD(THREAD_ID, PID) (THREAD_FLAG | BUILD_LWP (THREAD_ID, PID))
131 /* Pointers to routines from lithread_db resolved by dlopen() */
134 (*p_td_log) (const int on_off);
136 (*p_td_ta_new) (const struct ps_prochandle * ph_p, td_thragent_t ** ta_pp);
138 (*p_td_ta_delete) (td_thragent_t * ta_p);
142 (*p_td_ta_get_ph) (const td_thragent_t * ta_p, struct ps_prochandle ** ph_pp);
144 (*p_td_ta_get_nthreads) (const td_thragent_t * ta_p, int *nthread_p);
146 (*p_td_ta_tsd_iter) (const td_thragent_t * ta_p, td_key_iter_f * cb, void *cbdata_p);
148 (*p_td_ta_thr_iter) (const td_thragent_t * ta_p, td_thr_iter_f * cb, void *cbdata_p, td_thr_state_e state,
149 int ti_pri, sigset_t * ti_sigmask_p, unsigned ti_user_flags);
151 (*p_td_thr_validate) (const td_thrhandle_t * th_p);
153 (*p_td_thr_tsd) (const td_thrhandle_t * th_p, const thread_key_t key, void **data_pp);
155 (*p_td_thr_get_info) (const td_thrhandle_t * th_p, td_thrinfo_t * ti_p);
157 (*p_td_thr_getfpregs) (const td_thrhandle_t * th_p, prfpregset_t * fpregset);
159 (*p_td_thr_getxregsize) (const td_thrhandle_t * th_p, int *xregsize);
161 (*p_td_thr_getxregs) (const td_thrhandle_t * th_p, const caddr_t xregset);
163 (*p_td_thr_sigsetmask) (const td_thrhandle_t * th_p, const sigset_t ti_sigmask);
165 (*p_td_thr_setprio) (const td_thrhandle_t * th_p, const int ti_pri);
167 (*p_td_thr_setsigpending) (const td_thrhandle_t * th_p, const uchar_t ti_pending_flag, const sigset_t ti_pending);
169 (*p_td_thr_setfpregs) (const td_thrhandle_t * th_p, const prfpregset_t * fpregset);
171 (*p_td_thr_setxregs) (const td_thrhandle_t * th_p, const caddr_t xregset);
173 (*p_td_ta_map_id2thr) (const td_thragent_t * ta_p, thread_t tid, td_thrhandle_t * th_p);
175 (*p_td_ta_map_lwp2thr) (const td_thragent_t * ta_p, lwpid_t lwpid, td_thrhandle_t * th_p);
177 (*p_td_thr_getgregs) (const td_thrhandle_t * th_p, prgregset_t regset);
179 (*p_td_thr_setgregs) (const td_thrhandle_t * th_p, const prgregset_t regset);
185 td_err_string - Convert a thread_db error code to a string
189 char * td_err_string (errcode)
193 Return the thread_db error string associated with errcode. If errcode
194 is unknown, then return a message.
199 td_err_string (errcode)
202 static struct string_map
205 {TD_OK, "generic \"call succeeded\""},
206 {TD_ERR, "generic error."},
207 {TD_NOTHR, "no thread can be found to satisfy query"},
208 {TD_NOSV, "no synch. variable can be found to satisfy query"},
209 {TD_NOLWP, "no lwp can be found to satisfy query"},
210 {TD_BADPH, "invalid process handle"},
211 {TD_BADTH, "invalid thread handle"},
212 {TD_BADSH, "invalid synchronization handle"},
213 {TD_BADTA, "invalid thread agent"},
214 {TD_BADKEY, "invalid key"},
215 {TD_NOMSG, "td_thr_event_getmsg() called when there was no message"},
216 {TD_NOFPREGS, "FPU register set not available for given thread"},
217 {TD_NOLIBTHREAD, "application not linked with libthread"},
218 {TD_NOEVENT, "requested event is not supported"},
219 {TD_NOCAPAB, "capability not available"},
220 {TD_DBERR, "Debugger service failed"},
221 {TD_NOAPLIC, "Operation not applicable to"},
222 {TD_NOTSD, "No thread specific data for this thread"},
223 {TD_MALLOC, "Malloc failed"},
224 {TD_PARTIALREG, "Only part of register set was writen/read"},
225 {TD_NOXREGS, "X register set not available for given thread"}
227 const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
231 for (i = 0; i < td_err_size; i++)
232 if (td_err_table[i].num == errcode)
233 return td_err_table[i].str;
235 sprintf (buf, "Unknown thread_db error code: %d", errcode);
244 td_state_string - Convert a thread_db state code to a string
248 char * td_state_string (statecode)
252 Return the thread_db state string associated with statecode. If
253 statecode is unknown, then return a message.
258 td_state_string (statecode)
259 td_thr_state_e statecode;
261 static struct string_map
262 td_thr_state_table[] =
264 {TD_THR_ANY_STATE, "any state"},
265 {TD_THR_UNKNOWN, "unknown"},
266 {TD_THR_STOPPED, "stopped"},
268 {TD_THR_ACTIVE, "active"},
269 {TD_THR_ZOMBIE, "zombie"},
270 {TD_THR_SLEEP, "sleep"},
271 {TD_THR_STOPPED_ASLEEP, "stopped asleep"}
273 const int td_thr_state_table_size = sizeof td_thr_state_table / sizeof (struct string_map);
277 for (i = 0; i < td_thr_state_table_size; i++)
278 if (td_thr_state_table[i].num == statecode)
279 return td_thr_state_table[i].str;
281 sprintf (buf, "Unknown thread_db state code: %d", statecode);
290 thread_to_lwp - Convert a Posix or Solaris thread id to a LWP id.
294 int thread_to_lwp (thread_id, default_lwp)
298 This function converts a Posix or Solaris thread id to a lightweight
299 process id. If thread_id is non-existent, that's an error. If it's
300 an inactive thread, then we return default_lwp.
304 This function probably shouldn't call error()...
309 thread_to_lwp (thread_id, default_lwp)
317 if (is_lwp (thread_id))
318 return thread_id; /* It's already an LWP id */
320 /* It's a thread. Convert to lwp */
322 val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th);
324 return -1; /* thread must have terminated */
325 else if (val != TD_OK)
326 error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val));
328 val = p_td_thr_get_info (&th, &ti);
330 return -1; /* thread must have terminated */
331 else if (val != TD_OK)
332 error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val));
334 if (ti.ti_state != TD_THR_ACTIVE)
336 if (default_lwp != -1)
338 error ("thread_to_lwp: thread state not active: %s",
339 td_state_string (ti.ti_state));
342 return BUILD_LWP (ti.ti_lid, PIDGET (thread_id));
349 lwp_to_thread - Convert a LWP id to a Posix or Solaris thread id.
353 int lwp_to_thread (lwp_id)
357 This function converts a lightweight process id to a Posix or Solaris
358 thread id. If thread_id is non-existent, that's an error.
362 This function probably shouldn't call error()...
375 return lwp; /* It's already a thread id */
377 /* It's an lwp. Convert it to a thread id. */
379 if (!sol_thread_alive (lwp))
380 return -1; /* defunct lwp */
382 val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th);
384 return -1; /* thread must have terminated */
385 else if (val != TD_OK)
386 error ("lwp_to_thread: td_ta_map_lwp2thr: %s.", td_err_string (val));
388 val = p_td_thr_validate (&th);
390 return lwp; /* libthread doesn't know about it, just return lwp */
391 else if (val != TD_OK)
392 error ("lwp_to_thread: td_thr_validate: %s.", td_err_string (val));
394 val = p_td_thr_get_info (&th, &ti);
396 return -1; /* thread must have terminated */
397 else if (val != TD_OK)
398 error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val));
400 return BUILD_THREAD (ti.ti_tid, PIDGET (lwp));
407 save_inferior_pid - Save inferior_pid on the cleanup list
408 restore_inferior_pid - Restore inferior_pid from the cleanup list
412 struct cleanup *save_inferior_pid ()
413 void restore_inferior_pid (int pid)
417 These two functions act in unison to restore inferior_pid in
422 inferior_pid is a global variable that needs to be changed by many of
423 these routines before calling functions in procfs.c. In order to
424 guarantee that inferior_pid gets restored (in case of errors), you
425 need to call save_inferior_pid before changing it. At the end of the
426 function, you should invoke do_cleanups to restore it.
431 static struct cleanup *
434 return make_cleanup (restore_inferior_pid, (void *) inferior_pid);
438 restore_inferior_pid (pid)
441 inferior_pid = (int) pid;
445 /* Most target vector functions from here on actually just pass through to
446 procfs.c, as they don't need to do anything specific for threads. */
451 sol_thread_open (arg, from_tty)
455 procfs_ops.to_open (arg, from_tty);
458 /* Attach to process PID, then initialize for debugging it
459 and wait for the trace-trap that results from attaching. */
462 sol_thread_attach (args, from_tty)
466 procfs_ops.to_attach (args, from_tty);
467 /* Must get symbols from solibs before libthread_db can run! */
468 SOLIB_ADD ((char *) 0, from_tty, (struct target_ops *) 0);
469 if (sol_thread_active)
471 printf_filtered ("sol-thread active.\n");
472 main_ph.pid = inferior_pid; /* Save for xfer_memory */
473 push_target (&sol_thread_ops);
474 inferior_pid = lwp_to_thread (inferior_pid);
475 if (inferior_pid == -1)
476 inferior_pid = main_ph.pid;
478 add_thread (inferior_pid);
480 /* XXX - might want to iterate over all the threads and register them. */
483 /* Take a program previously attached to and detaches it.
484 The program resumes execution and will no longer stop
485 on signals, etc. We'd better not have left any breakpoints
486 in the program or it'll die when it hits one. For this
487 to work, it may be necessary for the process to have been
488 previously attached. It *might* work if the program was
489 started via the normal ptrace (PTRACE_TRACEME). */
492 sol_thread_detach (args, from_tty)
496 unpush_target (&sol_thread_ops);
497 procfs_ops.to_detach (args, from_tty);
500 /* Resume execution of process PID. If STEP is nozero, then
501 just single step it. If SIGNAL is nonzero, restart it with that
502 signal activated. We may have to convert pid from a thread-id to an LWP id
506 sol_thread_resume (pid, step, signo)
509 enum target_signal signo;
511 struct cleanup *old_chain;
513 old_chain = save_inferior_pid ();
515 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
516 if (inferior_pid == -1)
517 inferior_pid = procfs_first_available ();
523 pid = thread_to_lwp (pid, -2);
524 if (pid == -2) /* Inactive thread */
525 error ("This version of Solaris can't start inactive threads.");
526 if (info_verbose && pid == -1)
527 warning ("Specified thread %d seems to have terminated",
528 GET_THREAD (save_pid));
531 procfs_ops.to_resume (pid, step, signo);
533 do_cleanups (old_chain);
536 /* Wait for any threads to stop. We may have to convert PID from a thread id
537 to a LWP id, and vice versa on the way out. */
540 sol_thread_wait (pid, ourstatus)
542 struct target_waitstatus *ourstatus;
546 struct cleanup *old_chain;
548 save_pid = inferior_pid;
549 old_chain = save_inferior_pid ();
551 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
552 if (inferior_pid == -1)
553 inferior_pid = procfs_first_available ();
559 pid = thread_to_lwp (pid, -2);
560 if (pid == -2) /* Inactive thread */
561 error ("This version of Solaris can't start inactive threads.");
562 if (info_verbose && pid == -1)
563 warning ("Specified thread %d seems to have terminated",
564 GET_THREAD (save_pid));
567 rtnval = procfs_ops.to_wait (pid, ourstatus);
569 if (ourstatus->kind != TARGET_WAITKIND_EXITED)
571 /* Map the LWP of interest back to the appropriate thread ID */
572 rtnval = lwp_to_thread (rtnval);
576 /* See if we have a new thread */
577 if (is_thread (rtnval)
578 && rtnval != save_pid
579 && !in_thread_list (rtnval))
581 printf_filtered ("[New %s]\n", target_pid_to_str (rtnval));
586 /* During process initialization, we may get here without the thread package
587 being initialized, since that can only happen after we've found the shared
590 do_cleanups (old_chain);
596 sol_thread_fetch_registers (regno)
600 td_thrhandle_t thandle;
603 prfpregset_t fpregset;
609 if (!is_thread (inferior_pid))
610 { /* LWP: pass the request on to procfs.c */
611 if (target_has_execution)
612 procfs_ops.to_fetch_registers (regno);
614 orig_core_ops.to_fetch_registers (regno);
618 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
620 thread = GET_THREAD (inferior_pid);
623 error ("sol_thread_fetch_registers: thread == 0");
625 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
627 error ("sol_thread_fetch_registers: td_ta_map_id2thr: %s",
628 td_err_string (val));
630 /* Get the integer regs */
632 val = p_td_thr_getgregs (&thandle, gregset);
634 && val != TD_PARTIALREG)
635 error ("sol_thread_fetch_registers: td_thr_getgregs %s",
636 td_err_string (val));
638 /* For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, pc and sp
639 are saved (by a thread context switch). */
641 /* And, now the fp regs */
643 val = p_td_thr_getfpregs (&thandle, &fpregset);
645 && val != TD_NOFPREGS)
646 error ("sol_thread_fetch_registers: td_thr_getfpregs %s",
647 td_err_string (val));
649 /* Note that we must call supply_{g fp}regset *after* calling the td routines
650 because the td routines call ps_lget* which affect the values stored in the
653 supply_gregset (gregset);
654 supply_fpregset (&fpregset);
657 /* thread_db doesn't seem to handle this right */
658 val = td_thr_getxregsize (&thandle, &xregsize);
659 if (val != TD_OK && val != TD_NOXREGS)
660 error ("sol_thread_fetch_registers: td_thr_getxregsize %s",
661 td_err_string (val));
665 xregset = alloca (xregsize);
666 val = td_thr_getxregs (&thandle, xregset);
668 error ("sol_thread_fetch_registers: td_thr_getxregs %s",
669 td_err_string (val));
675 sol_thread_store_registers (regno)
679 td_thrhandle_t thandle;
682 prfpregset_t fpregset;
688 if (!is_thread (inferior_pid))
689 { /* LWP: pass the request on to procfs.c */
690 procfs_ops.to_store_registers (regno);
694 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
696 thread = GET_THREAD (inferior_pid);
698 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
700 error ("sol_thread_store_registers: td_ta_map_id2thr %s",
701 td_err_string (val));
704 { /* Not writing all the regs */
705 /* save new register value */
706 char old_value[REGISTER_SIZE];
707 memcpy (old_value, ®isters[REGISTER_BYTE (regno)], REGISTER_SIZE);
709 val = p_td_thr_getgregs (&thandle, regset);
711 error ("sol_thread_store_registers: td_thr_getgregs %s",
712 td_err_string (val));
713 val = p_td_thr_getfpregs (&thandle, &fpregset);
715 error ("sol_thread_store_registers: td_thr_getfpregs %s",
716 td_err_string (val));
718 /* restore new register value */
719 memcpy (®isters[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE);
722 /* thread_db doesn't seem to handle this right */
723 val = td_thr_getxregsize (&thandle, &xregsize);
724 if (val != TD_OK && val != TD_NOXREGS)
725 error ("sol_thread_store_registers: td_thr_getxregsize %s",
726 td_err_string (val));
730 xregset = alloca (xregsize);
731 val = td_thr_getxregs (&thandle, xregset);
733 error ("sol_thread_store_registers: td_thr_getxregs %s",
734 td_err_string (val));
739 fill_gregset (regset, regno);
740 fill_fpregset (&fpregset, regno);
742 val = p_td_thr_setgregs (&thandle, regset);
744 error ("sol_thread_store_registers: td_thr_setgregs %s",
745 td_err_string (val));
746 val = p_td_thr_setfpregs (&thandle, &fpregset);
748 error ("sol_thread_store_registers: td_thr_setfpregs %s",
749 td_err_string (val));
752 /* thread_db doesn't seem to handle this right */
753 val = td_thr_getxregsize (&thandle, &xregsize);
754 if (val != TD_OK && val != TD_NOXREGS)
755 error ("sol_thread_store_registers: td_thr_getxregsize %s",
756 td_err_string (val));
758 /* Should probably do something about writing the xregs here, but what are
763 /* Get ready to modify the registers array. On machines which store
764 individual registers, this doesn't need to do anything. On machines
765 which store all the registers in one fell swoop, this makes sure
766 that registers contains all the registers from the program being
770 sol_thread_prepare_to_store ()
772 procfs_ops.to_prepare_to_store ();
776 sol_thread_xfer_memory (memaddr, myaddr, len, dowrite, target)
781 struct target_ops *target; /* ignored */
784 struct cleanup *old_chain;
786 old_chain = save_inferior_pid ();
788 if (is_thread (inferior_pid) || /* A thread */
789 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
790 inferior_pid = procfs_first_available (); /* Find any live lwp. */
791 /* Note: don't need to call switch_to_thread; we're just reading memory. */
793 if (target_has_execution)
794 retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
796 retval = orig_core_ops.to_xfer_memory (memaddr, myaddr, len,
799 do_cleanups (old_chain);
804 /* Print status information about what we're accessing. */
807 sol_thread_files_info (ignore)
808 struct target_ops *ignore;
810 procfs_ops.to_files_info (ignore);
814 sol_thread_kill_inferior ()
816 procfs_ops.to_kill ();
820 sol_thread_notice_signals (pid)
823 procfs_ops.to_notice_signals (PIDGET (pid));
826 /* Fork an inferior process, and start debugging it with /proc. */
829 sol_thread_create_inferior (exec_file, allargs, env)
834 procfs_ops.to_create_inferior (exec_file, allargs, env);
836 if (sol_thread_active && inferior_pid != 0)
838 main_ph.pid = inferior_pid; /* Save for xfer_memory */
840 push_target (&sol_thread_ops);
842 inferior_pid = lwp_to_thread (inferior_pid);
843 if (inferior_pid == -1)
844 inferior_pid = main_ph.pid;
846 add_thread (inferior_pid);
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
857 sol_thread_new_objfile (objfile)
858 struct objfile *objfile;
864 sol_thread_active = 0;
869 /* don't do anything if init failed to resolve the libthread_db library */
870 if (!procfs_suppress_run)
873 /* Now, initialize the thread debugging library. This needs to be done after
874 the shared libraries are located because it needs information from the
875 user's thread library. */
879 error ("target_new_objfile: td_init: %s", td_err_string (val));
881 val = p_td_ta_new (&main_ph, &main_ta);
882 if (val == TD_NOLIBTHREAD)
884 else if (val != TD_OK)
885 error ("target_new_objfile: td_ta_new: %s", td_err_string (val));
887 sol_thread_active = 1;
890 /* Clean up after the inferior dies. */
893 sol_thread_mourn_inferior ()
895 unpush_target (&sol_thread_ops);
896 procfs_ops.to_mourn_inferior ();
899 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
902 sol_thread_can_run ()
904 return procfs_suppress_run;
911 sol_thread_alive - test thread for "aliveness"
915 static bool sol_thread_alive (int pid);
919 returns true if thread still active in inferior.
924 sol_thread_alive (pid)
927 if (is_thread (pid)) /* non-kernel thread */
932 pid = GET_THREAD (pid);
933 if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK)
934 return 0; /* thread not found */
935 if ((val = p_td_thr_validate (&th)) != TD_OK)
936 return 0; /* thread not valid */
937 return 1; /* known thread: return true */
940 /* kernel thread (LWP): let procfs test it */
942 if (target_has_execution)
943 return procfs_ops.to_thread_alive (pid);
945 return orig_core_ops.to_thread_alive (pid);
952 procfs_ops.to_stop ();
955 /* These routines implement the lower half of the thread_db interface. Ie: the
958 /* Various versions of <proc_service.h> have slightly
959 different function prototypes. In particular, we have
962 struct ps_prochandle * const struct ps_prochandle *
967 Which one you have depends on solaris version and what
968 patches you've applied. On the theory that there are
969 only two major variants, we have configure check the
970 prototype of ps_pdwrite (), and use that info to make
971 appropriate typedefs here. */
973 #ifdef PROC_SERVICE_IS_OLD
974 typedef const struct ps_prochandle *gdb_ps_prochandle_t;
975 typedef char *gdb_ps_read_buf_t;
976 typedef char *gdb_ps_write_buf_t;
977 typedef int gdb_ps_size_t;
979 typedef struct ps_prochandle *gdb_ps_prochandle_t;
980 typedef void *gdb_ps_read_buf_t;
981 typedef const void *gdb_ps_write_buf_t;
982 typedef size_t gdb_ps_size_t;
986 /* The next four routines are called by thread_db to tell us to stop and stop
987 a particular process or lwp. Since GDB ensures that these are all stopped
988 by the time we call anything in thread_db, these routines need to do
992 ps_pstop (gdb_ps_prochandle_t ph)
998 ps_pcontinue (gdb_ps_prochandle_t ph)
1004 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1010 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1016 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *ld_object_name,
1017 const char *ld_symbol_name, paddr_t * ld_symbol_addr)
1019 struct minimal_symbol *ms;
1021 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
1026 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
1031 /* Common routine for reading and writing memory. */
1034 rw_common (int dowrite, const struct ps_prochandle *ph, paddr_t addr,
1035 char *buf, int size)
1037 struct cleanup *old_chain;
1039 old_chain = save_inferior_pid ();
1041 if (is_thread (inferior_pid) || /* A thread */
1042 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
1043 inferior_pid = procfs_first_available (); /* Find any live lwp. */
1044 /* Note: don't need to call switch_to_thread; we're just reading memory. */
1050 if (target_has_execution)
1051 cc = procfs_ops.to_xfer_memory (addr, buf, size, dowrite, &procfs_ops);
1053 cc = orig_core_ops.to_xfer_memory (addr, buf, size, dowrite, &core_ops);
1058 print_sys_errmsg ("rw_common (): read", errno);
1060 print_sys_errmsg ("rw_common (): write", errno);
1062 do_cleanups (old_chain);
1070 do_cleanups (old_chain);
1076 ps_pdread (gdb_ps_prochandle_t ph, paddr_t addr,
1077 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1079 return rw_common (0, ph, addr, buf, size);
1083 ps_pdwrite (gdb_ps_prochandle_t ph, paddr_t addr,
1084 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1086 return rw_common (1, ph, addr, (char *) buf, size);
1090 ps_ptread (gdb_ps_prochandle_t ph, paddr_t addr,
1091 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1093 return rw_common (0, ph, addr, buf, size);
1097 ps_ptwrite (gdb_ps_prochandle_t ph, paddr_t addr,
1098 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1100 return rw_common (1, ph, addr, (char *) buf, size);
1103 /* Get integer regs */
1106 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1107 prgregset_t gregset)
1109 struct cleanup *old_chain;
1111 old_chain = save_inferior_pid ();
1113 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1115 if (target_has_execution)
1116 procfs_ops.to_fetch_registers (-1);
1118 orig_core_ops.to_fetch_registers (-1);
1119 fill_gregset (gregset, -1);
1121 do_cleanups (old_chain);
1126 /* Set integer regs */
1129 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1130 const prgregset_t gregset)
1132 struct cleanup *old_chain;
1134 old_chain = save_inferior_pid ();
1136 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1138 supply_gregset (gregset);
1139 if (target_has_execution)
1140 procfs_ops.to_store_registers (-1);
1142 orig_core_ops.to_store_registers (-1);
1144 do_cleanups (old_chain);
1150 ps_plog (const char *fmt,...)
1154 va_start (args, fmt);
1156 vfprintf_filtered (gdb_stderr, fmt, args);
1159 /* Get size of extra register set. Currently a noop. */
1162 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
1169 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1173 if (ioctl (lwp_fd, PIOCGXREGSIZE, ®size))
1175 if (errno == EINVAL)
1176 return PS_NOFREGS; /* XXX Wrong code, but this is the closest
1177 thing in proc_service.h */
1179 print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno);
1187 /* Get extra register set. Currently a noop. */
1190 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1196 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1200 if (ioctl (lwp_fd, PIOCGXREG, xregset))
1202 print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno);
1210 /* Set extra register set. Currently a noop. */
1213 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1219 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1223 if (ioctl (lwp_fd, PIOCSXREG, xregset))
1225 print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno);
1233 /* Get floating-point regs. */
1236 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1237 prfpregset_t * fpregset)
1239 struct cleanup *old_chain;
1241 old_chain = save_inferior_pid ();
1243 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1245 if (target_has_execution)
1246 procfs_ops.to_fetch_registers (-1);
1248 orig_core_ops.to_fetch_registers (-1);
1249 fill_fpregset (fpregset, -1);
1251 do_cleanups (old_chain);
1256 /* Set floating-point regs. */
1259 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1260 const prfpregset_t * fpregset)
1262 struct cleanup *old_chain;
1264 old_chain = save_inferior_pid ();
1266 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1268 supply_fpregset (fpregset);
1269 if (target_has_execution)
1270 procfs_ops.to_store_registers (-1);
1272 orig_core_ops.to_store_registers (-1);
1274 do_cleanups (old_chain);
1279 #ifdef TM_I386SOL2_H
1281 /* Get local descriptor table. */
1283 #include <sys/procfs.h>
1284 #include <sys/reg.h>
1285 #include <sys/sysi86.h>
1287 static int nldt_allocated = 0;
1288 static struct ssd *ldt_bufp = NULL;
1291 ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1300 /* Get procfs file descriptor for the LWP. */
1301 lwp_fd = procfs_get_pid_fd (BUILD_LWP (lwpid, PIDGET (inferior_pid)));
1305 /* Fetch registers und LDT descriptors. */
1306 if (ioctl (lwp_fd, PIOCGREG, &gregset) == -1)
1309 if (ioctl (lwp_fd, PIOCNLDT, &nldt) == -1)
1312 if (nldt_allocated < nldt)
1315 = (struct ssd *) xrealloc (ldt_bufp, (nldt + 1) * sizeof (struct ssd));
1316 nldt_allocated = nldt;
1319 if (ioctl (lwp_fd, PIOCLDT, ldt_bufp) == -1)
1322 /* Search LDT for the LWP via register GS. */
1323 for (i = 0; i < nldt; i++)
1325 if (ldt_bufp[i].sel == (gregset[GS] & 0xffff))
1327 *pldt = ldt_bufp[i];
1332 /* LDT not found. */
1335 #endif /* TM_I386SOL2_H */
1337 /* Convert a pid to printable form. */
1340 solaris_pid_to_str (pid)
1343 static char buf[100];
1345 /* in case init failed to resolve the libthread_db library */
1346 if (!procfs_suppress_run)
1347 return procfs_pid_to_str (pid);
1349 if (is_thread (pid))
1353 lwp = thread_to_lwp (pid, -2);
1356 sprintf (buf, "Thread %d (defunct)", GET_THREAD (pid));
1358 sprintf (buf, "Thread %d (LWP %d)", GET_THREAD (pid), GET_LWP (lwp));
1360 sprintf (buf, "Thread %d ", GET_THREAD (pid));
1362 else if (GET_LWP (pid) != 0)
1363 sprintf (buf, "LWP %d ", GET_LWP (pid));
1365 sprintf (buf, "process %d ", PIDGET (pid));
1371 /* Worker bee for find_new_threads
1372 Callback function that gets called once per USER thread (i.e., not
1376 sol_find_new_threads_callback (th, ignored)
1377 const td_thrhandle_t *th;
1384 if ((retval = p_td_thr_get_info (th, &ti)) != TD_OK)
1388 pid = BUILD_THREAD (ti.ti_tid, PIDGET (inferior_pid));
1389 if (!in_thread_list (pid))
1396 sol_find_new_threads ()
1398 /* don't do anything if init failed to resolve the libthread_db library */
1399 if (!procfs_suppress_run)
1402 if (inferior_pid == -1)
1404 printf_filtered ("No process.\n");
1407 p_td_ta_thr_iter (main_ta, sol_find_new_threads_callback, (void *) 0,
1408 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1409 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1413 sol_core_open (filename, from_tty)
1417 orig_core_ops.to_open (filename, from_tty);
1421 sol_core_close (quitting)
1424 orig_core_ops.to_close (quitting);
1428 sol_core_detach (args, from_tty)
1432 unpush_target (&core_ops);
1433 orig_core_ops.to_detach (args, from_tty);
1437 sol_core_files_info (t)
1438 struct target_ops *t;
1440 orig_core_ops.to_files_info (t);
1443 /* Worker bee for info sol-thread command. This is a callback function that
1444 gets called once for each Solaris thread (ie. not kernel thread) in the
1445 inferior. Print anything interesting that we can think of. */
1449 const td_thrhandle_t *th;
1454 struct minimal_symbol *msym;
1456 if ((ret = p_td_thr_get_info (th, &ti)) == TD_OK)
1458 printf_filtered ("%s thread #%d, lwp %d, ",
1459 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
1460 ti.ti_tid, ti.ti_lid);
1461 switch (ti.ti_state)
1464 case TD_THR_UNKNOWN:
1465 printf_filtered ("<unknown state>");
1467 case TD_THR_STOPPED:
1468 printf_filtered ("(stopped)");
1471 printf_filtered ("(run) ");
1474 printf_filtered ("(active) ");
1477 printf_filtered ("(zombie) ");
1480 printf_filtered ("(asleep) ");
1482 case TD_THR_STOPPED_ASLEEP:
1483 printf_filtered ("(stopped asleep)");
1486 /* Print thr_create start function: */
1487 if (ti.ti_startfunc != 0)
1488 if (msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc))
1489 printf_filtered (" startfunc: %s\n", SYMBOL_NAME (msym));
1491 printf_filtered (" startfunc: 0x%08x\n", ti.ti_startfunc);
1493 /* If thread is asleep, print function that went to sleep: */
1494 if (ti.ti_state == TD_THR_SLEEP)
1495 if (msym = lookup_minimal_symbol_by_pc (ti.ti_pc))
1496 printf_filtered (" - Sleep func: %s\n", SYMBOL_NAME (msym));
1498 printf_filtered (" - Sleep func: 0x%08x\n", ti.ti_startfunc);
1500 /* Wrap up line, if necessary */
1501 if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0)
1502 printf_filtered ("\n"); /* don't you hate counting newlines? */
1505 warning ("info sol-thread: failed to get info for thread.");
1510 /* List some state about each Solaris user thread in the inferior. */
1513 info_solthreads (args, from_tty)
1517 p_td_ta_thr_iter (main_ta, info_cb, args,
1518 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1519 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1523 ignore (addr, contents)
1532 init_sol_thread_ops ()
1534 sol_thread_ops.to_shortname = "solaris-threads";
1535 sol_thread_ops.to_longname = "Solaris threads and pthread.";
1536 sol_thread_ops.to_doc = "Solaris threads and pthread support.";
1537 sol_thread_ops.to_open = sol_thread_open;
1538 sol_thread_ops.to_close = 0;
1539 sol_thread_ops.to_attach = sol_thread_attach;
1540 sol_thread_ops.to_detach = sol_thread_detach;
1541 sol_thread_ops.to_resume = sol_thread_resume;
1542 sol_thread_ops.to_wait = sol_thread_wait;
1543 sol_thread_ops.to_fetch_registers = sol_thread_fetch_registers;
1544 sol_thread_ops.to_store_registers = sol_thread_store_registers;
1545 sol_thread_ops.to_prepare_to_store = sol_thread_prepare_to_store;
1546 sol_thread_ops.to_xfer_memory = sol_thread_xfer_memory;
1547 sol_thread_ops.to_files_info = sol_thread_files_info;
1548 sol_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1549 sol_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1550 sol_thread_ops.to_terminal_init = terminal_init_inferior;
1551 sol_thread_ops.to_terminal_inferior = terminal_inferior;
1552 sol_thread_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1553 sol_thread_ops.to_terminal_ours = terminal_ours;
1554 sol_thread_ops.to_terminal_info = child_terminal_info;
1555 sol_thread_ops.to_kill = sol_thread_kill_inferior;
1556 sol_thread_ops.to_load = 0;
1557 sol_thread_ops.to_lookup_symbol = 0;
1558 sol_thread_ops.to_create_inferior = sol_thread_create_inferior;
1559 sol_thread_ops.to_mourn_inferior = sol_thread_mourn_inferior;
1560 sol_thread_ops.to_can_run = sol_thread_can_run;
1561 sol_thread_ops.to_notice_signals = sol_thread_notice_signals;
1562 sol_thread_ops.to_thread_alive = sol_thread_alive;
1563 sol_thread_ops.to_find_new_threads = sol_find_new_threads;
1564 sol_thread_ops.to_stop = sol_thread_stop;
1565 sol_thread_ops.to_stratum = process_stratum;
1566 sol_thread_ops.to_has_all_memory = 1;
1567 sol_thread_ops.to_has_memory = 1;
1568 sol_thread_ops.to_has_stack = 1;
1569 sol_thread_ops.to_has_registers = 1;
1570 sol_thread_ops.to_has_execution = 1;
1571 sol_thread_ops.to_has_thread_control = tc_none;
1572 sol_thread_ops.to_sections = 0;
1573 sol_thread_ops.to_sections_end = 0;
1574 sol_thread_ops.to_magic = OPS_MAGIC;
1579 init_sol_core_ops ()
1581 sol_core_ops.to_shortname = "solaris-core";
1582 sol_core_ops.to_longname = "Solaris core threads and pthread.";
1583 sol_core_ops.to_doc = "Solaris threads and pthread support for core files.";
1584 sol_core_ops.to_open = sol_core_open;
1585 sol_core_ops.to_close = sol_core_close;
1586 sol_core_ops.to_attach = sol_thread_attach;
1587 sol_core_ops.to_detach = sol_core_detach;
1588 /* sol_core_ops.to_resume = 0; */
1589 /* sol_core_ops.to_wait = 0; */
1590 sol_core_ops.to_fetch_registers = sol_thread_fetch_registers;
1591 /* sol_core_ops.to_store_registers = 0; */
1592 /* sol_core_ops.to_prepare_to_store = 0; */
1593 sol_core_ops.to_xfer_memory = sol_thread_xfer_memory;
1594 sol_core_ops.to_files_info = sol_core_files_info;
1595 sol_core_ops.to_insert_breakpoint = ignore;
1596 sol_core_ops.to_remove_breakpoint = ignore;
1597 /* sol_core_ops.to_terminal_init = 0; */
1598 /* sol_core_ops.to_terminal_inferior = 0; */
1599 /* sol_core_ops.to_terminal_ours_for_output = 0; */
1600 /* sol_core_ops.to_terminal_ours = 0; */
1601 /* sol_core_ops.to_terminal_info = 0; */
1602 /* sol_core_ops.to_kill = 0; */
1603 /* sol_core_ops.to_load = 0; */
1604 /* sol_core_ops.to_lookup_symbol = 0; */
1605 sol_core_ops.to_create_inferior = sol_thread_create_inferior;
1606 sol_core_ops.to_stratum = core_stratum;
1607 sol_core_ops.to_has_all_memory = 0;
1608 sol_core_ops.to_has_memory = 1;
1609 sol_core_ops.to_has_stack = 1;
1610 sol_core_ops.to_has_registers = 1;
1611 sol_core_ops.to_has_execution = 0;
1612 sol_core_ops.to_has_thread_control = tc_none;
1613 sol_core_ops.to_sections = 0;
1614 sol_core_ops.to_sections_end = 0;
1615 sol_core_ops.to_magic = OPS_MAGIC;
1618 /* we suppress the call to add_target of core_ops in corelow because
1619 if there are two targets in the stratum core_stratum, find_core_target
1620 won't know which one to return. see corelow.c for an additonal
1621 comment on coreops_suppress_target. */
1622 int coreops_suppress_target = 1;
1625 _initialize_sol_thread ()
1629 init_sol_thread_ops ();
1630 init_sol_core_ops ();
1632 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1636 #define resolve(X) \
1637 if (!(p_##X = dlsym (dlhandle, #X))) \
1641 resolve (td_ta_new);
1642 resolve (td_ta_delete);
1644 resolve (td_ta_get_ph);
1645 resolve (td_ta_get_nthreads);
1646 resolve (td_ta_tsd_iter);
1647 resolve (td_ta_thr_iter);
1648 resolve (td_thr_validate);
1649 resolve (td_thr_tsd);
1650 resolve (td_thr_get_info);
1651 resolve (td_thr_getfpregs);
1652 resolve (td_thr_getxregsize);
1653 resolve (td_thr_getxregs);
1654 resolve (td_thr_sigsetmask);
1655 resolve (td_thr_setprio);
1656 resolve (td_thr_setsigpending);
1657 resolve (td_thr_setfpregs);
1658 resolve (td_thr_setxregs);
1659 resolve (td_ta_map_id2thr);
1660 resolve (td_ta_map_lwp2thr);
1661 resolve (td_thr_getgregs);
1662 resolve (td_thr_setgregs);
1664 add_target (&sol_thread_ops);
1666 procfs_suppress_run = 1;
1668 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1669 "Show info on Solaris user threads.\n", &maintenanceinfolist);
1671 memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops));
1672 memcpy (&core_ops, &sol_core_ops, sizeof (struct target_ops));
1673 add_target (&core_ops);
1679 fprintf_unfiltered (gdb_stderr, "[GDB will not be able to debug user-mode threads: %s]\n", dlerror ());
1684 /* allow the user to debug non-threaded core files */
1685 add_target (&core_ops);