1 /* Low level interface for debugging Solaris threads for GDB, the GNU debugger.
2 Copyright 1996 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, Boston, MA 02111-1307, USA. */
20 /* This module implements a sort of half target that sits between the
21 machine-independent parts of GDB and the /proc interface (procfs.c) to
22 provide access to the Solaris user-mode thread implementation.
24 Solaris threads are true user-mode threads, which are invoked via the thr_*
25 and pthread_* (native and Posix respectivly) interfaces. These are mostly
26 implemented in user-space, with all thread context kept in various
27 structures that live in the user's heap. These should not be confused with
28 lightweight processes (LWPs), which are implemented by the kernel, and
29 scheduled without explicit intervention by the process.
31 Just to confuse things a little, Solaris threads (both native and Posix) are
32 actually implemented using LWPs. In general, there are going to be more
33 threads than LWPs. There is no fixed correspondence between a thread and an
34 LWP. When a thread wants to run, it gets scheduled onto the first available
35 LWP and can therefore migrate from one LWP to another as time goes on. A
36 sleeping thread may not be associated with an LWP at all!
38 To make it possible to mess with threads, Sun provides a library called
39 libthread_db.so.1 (not to be confused with libthread_db.so.0, which doesn't
40 have a published interface). This interface has an upper part, which it
41 provides, and a lower part which I provide. The upper part consists of the
42 td_* routines, which allow me to find all the threads, query their state,
43 etc... The lower part consists of all of the ps_*, which are used by the
44 td_* routines to read/write memory, manipulate LWPs, lookup symbols, etc...
45 The ps_* routines actually do most of their work by calling functions in
50 /* Undefine gregset_t and fpregset_t to avoid conflict with defs in xm file. */
61 #include <proc_service.h>
62 #include <thread_db.h>
63 #include "gdbthread.h"
72 extern struct target_ops sol_thread_ops; /* Forward declaration */
73 extern struct target_ops sol_core_ops; /* Forward declaration */
75 /* place to store core_ops before we overwrite it */
76 static struct target_ops orig_core_ops;
78 extern int procfs_suppress_run;
79 extern struct target_ops procfs_ops; /* target vector for procfs.c */
80 extern struct target_ops core_ops; /* target vector for corelow.c */
81 extern char *procfs_pid_to_str PARAMS ((int pid));
83 /* Note that these prototypes differ slightly from those used in procfs.c
84 for of two reasons. One, we can't use gregset_t, as that's got a whole
85 different meaning under Solaris (also, see above). Two, we can't use the
86 pointer form here as these are actually arrays of ints (for Sparc's at
87 least), and are automatically coerced into pointers to ints when used as
88 parameters. That makes it impossible to avoid a compiler warning when
89 passing pr{g fp}regset_t's from a parameter to an argument of one of
92 extern void supply_gregset PARAMS ((const prgregset_t));
93 extern void fill_gregset PARAMS ((prgregset_t, int));
94 extern void supply_fpregset PARAMS ((const prfpregset_t));
95 extern void fill_fpregset PARAMS ((prfpregset_t, int));
97 /* This struct is defined by us, but mainly used for the proc_service interface.
98 We don't have much use for it, except as a handy place to get a real pid
99 for memory accesses. */
112 static struct ps_prochandle main_ph;
113 static td_thragent_t *main_ta;
114 static int sol_thread_active = 0;
116 static struct cleanup * save_inferior_pid PARAMS ((void));
117 static void restore_inferior_pid PARAMS ((int pid));
118 static char *td_err_string PARAMS ((td_err_e errcode));
119 static char *td_state_string PARAMS ((td_thr_state_e statecode));
120 static int thread_to_lwp PARAMS ((int thread_id, int default_lwp));
121 static void sol_thread_resume PARAMS ((int pid, int step,
122 enum target_signal signo));
123 static int lwp_to_thread PARAMS ((int lwp));
124 static int sol_thread_alive PARAMS ((int pid));
125 static void sol_core_close PARAMS ((int quitting));
127 #define THREAD_FLAG 0x80000000
128 #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
129 #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
130 #define GET_LWP(LWP_ID) (TIDGET(LWP_ID))
131 #define GET_THREAD(THREAD_ID) (((THREAD_ID) >> 16) & 0x7fff)
132 #define BUILD_LWP(LWP_ID, PID) ((LWP_ID) << 16 | (PID))
133 #define BUILD_THREAD(THREAD_ID, PID) (THREAD_FLAG | BUILD_LWP (THREAD_ID, PID))
135 /* Pointers to routines from lithread_db resolved by dlopen() */
138 (*p_td_log) (const int on_off);
140 (*p_td_ta_new) (const struct ps_prochandle *ph_p, td_thragent_t **ta_pp);
142 (*p_td_ta_delete) (td_thragent_t *ta_p);
146 (*p_td_ta_get_ph) (const td_thragent_t *ta_p, struct ps_prochandle **ph_pp);
148 (*p_td_ta_get_nthreads) (const td_thragent_t *ta_p, int *nthread_p);
150 (*p_td_ta_tsd_iter) (const td_thragent_t *ta_p, td_key_iter_f *cb, void *cbdata_p);
152 (*p_td_ta_thr_iter) (const td_thragent_t *ta_p, td_thr_iter_f *cb, void *cbdata_p, td_thr_state_e state,
153 int ti_pri, sigset_t *ti_sigmask_p, unsigned ti_user_flags);
155 (*p_td_thr_validate) (const td_thrhandle_t *th_p);
157 (*p_td_thr_tsd) (const td_thrhandle_t *th_p, const thread_key_t key, void **data_pp);
159 (*p_td_thr_get_info) (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p);
161 (*p_td_thr_getfpregs) (const td_thrhandle_t *th_p, prfpregset_t *fpregset);
163 (*p_td_thr_getxregsize) (const td_thrhandle_t *th_p, int *xregsize);
165 (*p_td_thr_getxregs) (const td_thrhandle_t *th_p, const caddr_t xregset);
167 (*p_td_thr_sigsetmask) (const td_thrhandle_t *th_p, const sigset_t ti_sigmask);
169 (*p_td_thr_setprio) (const td_thrhandle_t *th_p, const int ti_pri);
171 (*p_td_thr_setsigpending) (const td_thrhandle_t *th_p, const uchar_t ti_pending_flag, const sigset_t ti_pending);
173 (*p_td_thr_setfpregs) (const td_thrhandle_t *th_p, const prfpregset_t *fpregset);
175 (*p_td_thr_setxregs) (const td_thrhandle_t *th_p, const caddr_t xregset);
177 (*p_td_ta_map_id2thr) (const td_thragent_t *ta_p, thread_t tid, td_thrhandle_t *th_p);
179 (*p_td_ta_map_lwp2thr) (const td_thragent_t *ta_p, lwpid_t lwpid, td_thrhandle_t *th_p);
181 (*p_td_thr_getgregs) (const td_thrhandle_t *th_p, prgregset_t regset);
183 (*p_td_thr_setgregs) (const td_thrhandle_t *th_p, const prgregset_t regset);
189 td_err_string - Convert a thread_db error code to a string
193 char * td_err_string (errcode)
197 Return the thread_db error string associated with errcode. If errcode
198 is unknown, then return a message.
203 td_err_string (errcode)
206 static struct string_map
208 {TD_OK, "generic \"call succeeded\""},
209 {TD_ERR, "generic error."},
210 {TD_NOTHR, "no thread can be found to satisfy query"},
211 {TD_NOSV, "no synch. variable can be found to satisfy query"},
212 {TD_NOLWP, "no lwp can be found to satisfy query"},
213 {TD_BADPH, "invalid process handle"},
214 {TD_BADTH, "invalid thread handle"},
215 {TD_BADSH, "invalid synchronization handle"},
216 {TD_BADTA, "invalid thread agent"},
217 {TD_BADKEY, "invalid key"},
218 {TD_NOMSG, "td_thr_event_getmsg() called when there was no message"},
219 {TD_NOFPREGS, "FPU register set not available for given thread"},
220 {TD_NOLIBTHREAD, "application not linked with libthread"},
221 {TD_NOEVENT, "requested event is not supported"},
222 {TD_NOCAPAB, "capability not available"},
223 {TD_DBERR, "Debugger service failed"},
224 {TD_NOAPLIC, "Operation not applicable to"},
225 {TD_NOTSD, "No thread specific data for this thread"},
226 {TD_MALLOC, "Malloc failed"},
227 {TD_PARTIALREG, "Only part of register set was writen/read"},
228 {TD_NOXREGS, "X register set not available for given thread"}
230 const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
234 for (i = 0; i < td_err_size; i++)
235 if (td_err_table[i].num == errcode)
236 return td_err_table[i].str;
238 sprintf (buf, "Unknown thread_db error code: %d", errcode);
247 td_state_string - Convert a thread_db state code to a string
251 char * td_state_string (statecode)
255 Return the thread_db state string associated with statecode. If
256 statecode is unknown, then return a message.
261 td_state_string (statecode)
262 td_thr_state_e statecode;
264 static struct string_map
265 td_thr_state_table[] = {
266 {TD_THR_ANY_STATE, "any state"},
267 {TD_THR_UNKNOWN, "unknown"},
268 {TD_THR_STOPPED, "stopped"},
270 {TD_THR_ACTIVE, "active"},
271 {TD_THR_ZOMBIE, "zombie"},
272 {TD_THR_SLEEP, "sleep"},
273 {TD_THR_STOPPED_ASLEEP, "stopped asleep"}
275 const int td_thr_state_table_size = sizeof td_thr_state_table / sizeof (struct string_map);
279 for (i = 0; i < td_thr_state_table_size; i++)
280 if (td_thr_state_table[i].num == statecode)
281 return td_thr_state_table[i].str;
283 sprintf (buf, "Unknown thread_db state code: %d", statecode);
292 thread_to_lwp - Convert a Posix or Solaris thread id to a LWP id.
296 int thread_to_lwp (thread_id, default_lwp)
300 This function converts a Posix or Solaris thread id to a lightweight
301 process id. If thread_id is non-existent, that's an error. If it's
302 an inactive thread, then we return default_lwp.
306 This function probably shouldn't call error()...
311 thread_to_lwp (thread_id, default_lwp)
319 if (is_lwp (thread_id))
320 return thread_id; /* It's already an LWP id */
322 /* It's a thread. Convert to lwp */
324 val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th);
326 return -1; /* thread must have terminated */
327 else if (val != TD_OK)
328 error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val));
330 val = p_td_thr_get_info (&th, &ti);
332 return -1; /* thread must have terminated */
333 else if (val != TD_OK)
334 error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val));
336 if (ti.ti_state != TD_THR_ACTIVE)
338 if (default_lwp != -1)
340 error ("thread_to_lwp: thread state not active: %s",
341 td_state_string (ti.ti_state));
344 return BUILD_LWP (ti.ti_lid, PIDGET (thread_id));
351 lwp_to_thread - Convert a LWP id to a Posix or Solaris thread id.
355 int lwp_to_thread (lwp_id)
359 This function converts a lightweight process id to a Posix or Solaris
360 thread id. If thread_id is non-existent, that's an error.
364 This function probably shouldn't call error()...
377 return lwp; /* It's already a thread id */
379 /* It's an lwp. Convert it to a thread id. */
381 if (!sol_thread_alive (lwp))
382 return -1; /* defunct lwp */
384 val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th);
386 return -1; /* thread must have terminated */
387 else if (val != TD_OK)
388 error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val));
390 val = p_td_thr_validate (&th);
392 return lwp; /* libthread doesn't know about it, just return lwp */
393 else if (val != TD_OK)
394 error ("lwp_to_thread: td_thr_validate: %s.", td_err_string (val));
396 val = p_td_thr_get_info (&th, &ti);
398 return -1; /* thread must have terminated */
399 else if (val != TD_OK)
400 error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val));
402 return BUILD_THREAD (ti.ti_tid, PIDGET (lwp));
409 save_inferior_pid - Save inferior_pid on the cleanup list
410 restore_inferior_pid - Restore inferior_pid from the cleanup list
414 struct cleanup *save_inferior_pid ()
415 void restore_inferior_pid (int pid)
419 These two functions act in unison to restore inferior_pid in
424 inferior_pid is a global variable that needs to be changed by many of
425 these routines before calling functions in procfs.c. In order to
426 guarantee that inferior_pid gets restored (in case of errors), you
427 need to call save_inferior_pid before changing it. At the end of the
428 function, you should invoke do_cleanups to restore it.
433 static struct cleanup *
436 return make_cleanup (restore_inferior_pid, inferior_pid);
440 restore_inferior_pid (pid)
447 /* Most target vector functions from here on actually just pass through to
448 procfs.c, as they don't need to do anything specific for threads. */
453 sol_thread_open (arg, from_tty)
457 procfs_ops.to_open (arg, from_tty);
460 /* Attach to process PID, then initialize for debugging it
461 and wait for the trace-trap that results from attaching. */
464 sol_thread_attach (args, from_tty)
468 procfs_ops.to_attach (args, from_tty);
469 /* Must get symbols from solibs before libthread_db can run! */
470 SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0);
471 if (sol_thread_active)
473 printf_filtered ("sol-thread active.\n");
474 main_ph.pid = inferior_pid; /* Save for xfer_memory */
475 push_target (&sol_thread_ops);
476 inferior_pid = lwp_to_thread (inferior_pid);
477 if (inferior_pid == -1)
478 inferior_pid = main_ph.pid;
480 add_thread (inferior_pid);
482 /* XXX - might want to iterate over all the threads and register them. */
485 /* Take a program previously attached to and detaches it.
486 The program resumes execution and will no longer stop
487 on signals, etc. We'd better not have left any breakpoints
488 in the program or it'll die when it hits one. For this
489 to work, it may be necessary for the process to have been
490 previously attached. It *might* work if the program was
491 started via the normal ptrace (PTRACE_TRACEME). */
494 sol_thread_detach (args, from_tty)
498 unpush_target (&sol_thread_ops);
499 procfs_ops.to_detach (args, from_tty);
502 /* Resume execution of process PID. If STEP is nozero, then
503 just single step it. If SIGNAL is nonzero, restart it with that
504 signal activated. We may have to convert pid from a thread-id to an LWP id
508 sol_thread_resume (pid, step, signo)
511 enum target_signal signo;
513 struct cleanup *old_chain;
515 old_chain = save_inferior_pid ();
517 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
518 if (inferior_pid == -1)
519 inferior_pid = procfs_first_available ();
525 pid = thread_to_lwp (pid, -2);
526 if (pid == -2) /* Inactive thread */
527 error ("This version of Solaris can't start inactive threads.");
528 if (info_verbose && pid == -1)
529 warning ("Specified thread %d seems to have terminated",
530 GET_THREAD (save_pid));
533 procfs_ops.to_resume (pid, step, signo);
535 do_cleanups (old_chain);
538 /* Wait for any threads to stop. We may have to convert PID from a thread id
539 to a LWP id, and vice versa on the way out. */
542 sol_thread_wait (pid, ourstatus)
544 struct target_waitstatus *ourstatus;
548 struct cleanup *old_chain;
550 save_pid = inferior_pid;
551 old_chain = save_inferior_pid ();
553 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
554 if (inferior_pid == -1)
555 inferior_pid = procfs_first_available ();
561 pid = thread_to_lwp (pid, -2);
562 if (pid == -2) /* Inactive thread */
563 error ("This version of Solaris can't start inactive threads.");
564 if (info_verbose && pid == -1)
565 warning ("Specified thread %d seems to have terminated",
566 GET_THREAD (save_pid));
569 rtnval = procfs_ops.to_wait (pid, ourstatus);
571 if (ourstatus->kind != TARGET_WAITKIND_EXITED)
573 /* Map the LWP of interest back to the appropriate thread ID */
574 rtnval = lwp_to_thread (rtnval);
578 /* See if we have a new thread */
579 if (is_thread (rtnval)
580 && rtnval != save_pid
581 && !in_thread_list (rtnval))
583 printf_filtered ("[New %s]\n", target_pid_to_str (rtnval));
588 /* During process initialization, we may get here without the thread package
589 being initialized, since that can only happen after we've found the shared
592 do_cleanups (old_chain);
598 sol_thread_fetch_registers (regno)
602 td_thrhandle_t thandle;
605 prfpregset_t fpregset;
611 if (!is_thread (inferior_pid))
612 { /* LWP: pass the request on to procfs.c */
613 if (target_has_execution)
614 procfs_ops.to_fetch_registers (regno);
616 orig_core_ops.to_fetch_registers (regno);
620 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
622 thread = GET_THREAD (inferior_pid);
625 error ("sol_thread_fetch_registers: thread == 0");
627 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
629 error ("sol_thread_fetch_registers: td_ta_map_id2thr: %s",
630 td_err_string (val));
632 /* Get the integer regs */
634 val = p_td_thr_getgregs (&thandle, gregset);
636 && val != TD_PARTIALREG)
637 error ("sol_thread_fetch_registers: td_thr_getgregs %s",
638 td_err_string (val));
640 /* For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, pc and sp
641 are saved (by a thread context switch). */
643 /* And, now the fp regs */
645 val = p_td_thr_getfpregs (&thandle, &fpregset);
647 && val != TD_NOFPREGS)
648 error ("sol_thread_fetch_registers: td_thr_getfpregs %s",
649 td_err_string (val));
651 /* Note that we must call supply_{g fp}regset *after* calling the td routines
652 because the td routines call ps_lget* which affect the values stored in the
655 supply_gregset (gregset);
656 supply_fpregset (fpregset);
659 /* thread_db doesn't seem to handle this right */
660 val = td_thr_getxregsize (&thandle, &xregsize);
661 if (val != TD_OK && val != TD_NOXREGS)
662 error ("sol_thread_fetch_registers: td_thr_getxregsize %s",
663 td_err_string (val));
667 xregset = alloca (xregsize);
668 val = td_thr_getxregs (&thandle, xregset);
670 error ("sol_thread_fetch_registers: td_thr_getxregs %s",
671 td_err_string (val));
677 sol_thread_store_registers (regno)
681 td_thrhandle_t thandle;
684 prfpregset_t fpregset;
690 if (!is_thread (inferior_pid))
691 { /* LWP: pass the request on to procfs.c */
692 procfs_ops.to_store_registers (regno);
696 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
698 thread = GET_THREAD (inferior_pid);
700 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
702 error ("sol_thread_store_registers: td_ta_map_id2thr %s",
703 td_err_string (val));
706 { /* Not writing all the regs */
707 val = p_td_thr_getgregs (&thandle, regset);
709 error ("sol_thread_store_registers: td_thr_getgregs %s",
710 td_err_string (val));
711 val = p_td_thr_getfpregs (&thandle, &fpregset);
713 error ("sol_thread_store_registers: td_thr_getfpregs %s",
714 td_err_string (val));
717 /* thread_db doesn't seem to handle this right */
718 val = td_thr_getxregsize (&thandle, &xregsize);
719 if (val != TD_OK && val != TD_NOXREGS)
720 error ("sol_thread_store_registers: td_thr_getxregsize %s",
721 td_err_string (val));
725 xregset = alloca (xregsize);
726 val = td_thr_getxregs (&thandle, xregset);
728 error ("sol_thread_store_registers: td_thr_getxregs %s",
729 td_err_string (val));
734 fill_gregset (regset, regno);
735 fill_fpregset (fpregset, regno);
737 val = p_td_thr_setgregs (&thandle, regset);
739 error ("sol_thread_store_registers: td_thr_setgregs %s",
740 td_err_string (val));
741 val = p_td_thr_setfpregs (&thandle, &fpregset);
743 error ("sol_thread_store_registers: td_thr_setfpregs %s",
744 td_err_string (val));
747 /* thread_db doesn't seem to handle this right */
748 val = td_thr_getxregsize (&thandle, &xregsize);
749 if (val != TD_OK && val != TD_NOXREGS)
750 error ("sol_thread_store_registers: td_thr_getxregsize %s",
751 td_err_string (val));
753 /* Should probably do something about writing the xregs here, but what are
758 /* Get ready to modify the registers array. On machines which store
759 individual registers, this doesn't need to do anything. On machines
760 which store all the registers in one fell swoop, this makes sure
761 that registers contains all the registers from the program being
765 sol_thread_prepare_to_store ()
767 procfs_ops.to_prepare_to_store ();
771 sol_thread_xfer_memory (memaddr, myaddr, len, dowrite, target)
776 struct target_ops *target; /* ignored */
779 struct cleanup *old_chain;
781 old_chain = save_inferior_pid ();
783 if (is_thread (inferior_pid) || /* A thread */
784 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
785 inferior_pid = procfs_first_available (); /* Find any live lwp. */
786 /* Note: don't need to call switch_to_thread; we're just reading memory. */
788 if (target_has_execution)
789 retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
791 retval = orig_core_ops.to_xfer_memory (memaddr, myaddr, len,
794 do_cleanups (old_chain);
799 /* Print status information about what we're accessing. */
802 sol_thread_files_info (ignore)
803 struct target_ops *ignore;
805 procfs_ops.to_files_info (ignore);
809 sol_thread_kill_inferior ()
811 procfs_ops.to_kill ();
815 sol_thread_notice_signals (pid)
818 procfs_ops.to_notice_signals (pid);
821 /* Fork an inferior process, and start debugging it with /proc. */
824 sol_thread_create_inferior (exec_file, allargs, env)
829 procfs_ops.to_create_inferior (exec_file, allargs, env);
831 if (sol_thread_active && inferior_pid != 0)
833 main_ph.pid = inferior_pid; /* Save for xfer_memory */
835 push_target (&sol_thread_ops);
837 inferior_pid = lwp_to_thread (inferior_pid);
838 if (inferior_pid == -1)
839 inferior_pid = main_ph.pid;
841 add_thread (inferior_pid);
845 /* This routine is called whenever a new symbol table is read in, or when all
846 symbol tables are removed. libthread_db can only be initialized when it
847 finds the right variables in libthread.so. Since it's a shared library,
848 those variables don't show up until the library gets mapped and the symbol
852 sol_thread_new_objfile (objfile)
853 struct objfile *objfile;
859 sol_thread_active = 0;
864 /* don't do anything if init failed to resolve the libthread_db library */
865 if (!procfs_suppress_run)
868 /* Now, initialize the thread debugging library. This needs to be done after
869 the shared libraries are located because it needs information from the
870 user's thread library. */
874 error ("target_new_objfile: td_init: %s", td_err_string (val));
876 val = p_td_ta_new (&main_ph, &main_ta);
877 if (val == TD_NOLIBTHREAD)
879 else if (val != TD_OK)
880 error ("target_new_objfile: td_ta_new: %s", td_err_string (val));
882 sol_thread_active = 1;
885 /* Clean up after the inferior dies. */
888 sol_thread_mourn_inferior ()
890 unpush_target (&sol_thread_ops);
891 procfs_ops.to_mourn_inferior ();
894 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
897 sol_thread_can_run ()
899 return procfs_suppress_run;
906 sol_thread_alive - test thread for "aliveness"
910 static bool sol_thread_alive (int pid);
914 returns true if thread still active in inferior.
919 sol_thread_alive (pid)
922 if (is_thread (pid)) /* non-kernel thread */
927 pid = GET_THREAD (pid);
928 if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK)
929 return 0; /* thread not found */
930 if ((val = p_td_thr_validate (&th)) != TD_OK)
931 return 0; /* thread not valid */
932 return 1; /* known thread: return true */
934 else /* kernel thread (LWP): let procfs test it */
936 if (target_has_execution)
937 return procfs_ops.to_thread_alive (pid);
939 return orig_core_ops.to_thread_alive (pid);
946 procfs_ops.to_stop ();
949 /* These routines implement the lower half of the thread_db interface. Ie: the
952 /* The next four routines are called by thread_db to tell us to stop and stop
953 a particular process or lwp. Since GDB ensures that these are all stopped
954 by the time we call anything in thread_db, these routines need to do
958 ps_pstop (const struct ps_prochandle *ph)
964 ps_pcontinue (const struct ps_prochandle *ph)
970 ps_lstop (const struct ps_prochandle *ph, lwpid_t lwpid)
976 ps_lcontinue (const struct ps_prochandle *ph, lwpid_t lwpid)
982 ps_pglobal_lookup (const struct ps_prochandle *ph, const char *ld_object_name,
983 const char *ld_symbol_name, paddr_t *ld_symbol_addr)
985 struct minimal_symbol *ms;
987 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
992 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
997 /* Common routine for reading and writing memory. */
1000 rw_common (int dowrite, const struct ps_prochandle *ph, paddr_t addr,
1001 char *buf, int size)
1003 struct cleanup *old_chain;
1005 old_chain = save_inferior_pid ();
1007 if (is_thread (inferior_pid) || /* A thread */
1008 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
1009 inferior_pid = procfs_first_available (); /* Find any live lwp. */
1010 /* Note: don't need to call switch_to_thread; we're just reading memory. */
1016 if (target_has_execution)
1017 cc = procfs_ops.to_xfer_memory (addr, buf, size, dowrite, &procfs_ops);
1019 cc = orig_core_ops.to_xfer_memory (addr, buf, size, dowrite, &core_ops);
1024 print_sys_errmsg ("rw_common (): read", errno);
1026 print_sys_errmsg ("rw_common (): write", errno);
1028 do_cleanups (old_chain);
1036 do_cleanups (old_chain);
1042 ps_pdread (const struct ps_prochandle *ph, paddr_t addr, char *buf, int size)
1044 return rw_common (0, ph, addr, buf, size);
1048 ps_pdwrite (const struct ps_prochandle *ph, paddr_t addr, char *buf, int size)
1050 return rw_common (1, ph, addr, buf, size);
1054 ps_ptread (const struct ps_prochandle *ph, paddr_t addr, char *buf, int size)
1056 return rw_common (0, ph, addr, buf, size);
1060 ps_ptwrite (const struct ps_prochandle *ph, paddr_t addr, char *buf, int size)
1062 return rw_common (1, ph, addr, buf, size);
1065 /* Get integer regs */
1068 ps_lgetregs (const struct ps_prochandle *ph, lwpid_t lwpid,
1069 prgregset_t gregset)
1071 struct cleanup *old_chain;
1073 old_chain = save_inferior_pid ();
1075 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1077 if (target_has_execution)
1078 procfs_ops.to_fetch_registers (-1);
1080 orig_core_ops.to_fetch_registers (-1);
1081 fill_gregset (gregset, -1);
1083 do_cleanups (old_chain);
1088 /* Set integer regs */
1091 ps_lsetregs (const struct ps_prochandle *ph, lwpid_t lwpid,
1092 const prgregset_t gregset)
1094 struct cleanup *old_chain;
1096 old_chain = save_inferior_pid ();
1098 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1100 supply_gregset (gregset);
1101 if (target_has_execution)
1102 procfs_ops.to_store_registers (-1);
1104 orig_core_ops.to_store_registers (-1);
1106 do_cleanups (old_chain);
1112 ps_plog (const char *fmt, ...)
1116 va_start (args, fmt);
1118 vfprintf_filtered (gdb_stderr, fmt, args);
1121 /* Get size of extra register set. Currently a noop. */
1124 ps_lgetxregsize (const struct ps_prochandle *ph, lwpid_t lwpid, int *xregsize)
1131 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1135 if (ioctl (lwp_fd, PIOCGXREGSIZE, ®size))
1137 if (errno == EINVAL)
1138 return PS_NOFREGS; /* XXX Wrong code, but this is the closest
1139 thing in proc_service.h */
1141 print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno);
1149 /* Get extra register set. Currently a noop. */
1152 ps_lgetxregs (const struct ps_prochandle *ph, lwpid_t lwpid, caddr_t xregset)
1158 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1162 if (ioctl (lwp_fd, PIOCGXREG, xregset))
1164 print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno);
1172 /* Set extra register set. Currently a noop. */
1175 ps_lsetxregs (const struct ps_prochandle *ph, lwpid_t lwpid, caddr_t xregset)
1181 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1185 if (ioctl (lwp_fd, PIOCSXREG, xregset))
1187 print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno);
1195 /* Get floating-point regs. */
1198 ps_lgetfpregs (const struct ps_prochandle *ph, lwpid_t lwpid,
1199 prfpregset_t *fpregset)
1201 struct cleanup *old_chain;
1203 old_chain = save_inferior_pid ();
1205 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1207 if (target_has_execution)
1208 procfs_ops.to_fetch_registers (-1);
1210 orig_core_ops.to_fetch_registers (-1);
1211 fill_fpregset (*fpregset, -1);
1213 do_cleanups (old_chain);
1218 /* Set floating-point regs. */
1221 ps_lsetfpregs (const struct ps_prochandle *ph, lwpid_t lwpid,
1222 const prfpregset_t *fpregset)
1224 struct cleanup *old_chain;
1226 old_chain = save_inferior_pid ();
1228 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1230 supply_fpregset (*fpregset);
1231 if (target_has_execution)
1232 procfs_ops.to_store_registers (-1);
1234 orig_core_ops.to_store_registers (-1);
1236 do_cleanups (old_chain);
1241 /* Convert a pid to printable form. */
1244 solaris_pid_to_str (pid)
1247 static char buf[100];
1249 /* in case init failed to resolve the libthread_db library */
1250 if (!procfs_suppress_run)
1251 return procfs_pid_to_str (pid);
1253 if (is_thread (pid))
1257 lwp = thread_to_lwp (pid, -2);
1260 sprintf (buf, "Thread %d (defunct)", GET_THREAD (pid));
1262 sprintf (buf, "Thread %d (LWP %d)", GET_THREAD (pid), GET_LWP (lwp));
1264 sprintf (buf, "Thread %d ", GET_THREAD (pid));
1266 else if (GET_LWP (pid) != 0)
1267 sprintf (buf, "LWP %d ", GET_LWP (pid));
1269 sprintf (buf, "process %d ", PIDGET (pid));
1275 /* Worker bee for find_new_threads
1276 Callback function that gets called once per USER thread (i.e., not
1280 sol_find_new_threads_callback(th, ignored)
1281 const td_thrhandle_t *th;
1288 if ((retval = p_td_thr_get_info(th, &ti)) != TD_OK)
1292 pid = BUILD_THREAD(ti.ti_tid, PIDGET(inferior_pid));
1293 if (!in_thread_list(pid))
1300 sol_find_new_threads()
1302 if (inferior_pid == -1)
1304 printf_filtered("No process.\n");
1307 p_td_ta_thr_iter (main_ta, sol_find_new_threads_callback, (void *)0,
1308 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1309 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1313 sol_core_open (filename, from_tty)
1317 orig_core_ops.to_open (filename, from_tty);
1321 sol_core_close (quitting)
1324 orig_core_ops.to_close (quitting);
1328 sol_core_detach (args, from_tty)
1332 unpush_target (&core_ops);
1333 orig_core_ops.to_detach (args, from_tty);
1337 sol_core_files_info (t)
1338 struct target_ops *t;
1340 orig_core_ops.to_files_info (t);
1343 #ifdef MAINTENANCE_CMDS
1344 /* Worker bee for info sol-thread command. This is a callback function that
1345 gets called once for each Solaris thread (ie. not kernel thread) in the
1346 inferior. Print anything interesting that we can think of. */
1350 const td_thrhandle_t *th;
1355 struct minimal_symbol *msym;
1357 if ((ret = p_td_thr_get_info (th, &ti)) == TD_OK)
1359 printf_filtered ("%s thread #%d, lwp %d, ",
1360 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
1361 ti.ti_tid, ti.ti_lid);
1362 switch (ti.ti_state) {
1364 case TD_THR_UNKNOWN: printf_filtered ("<unknown state>"); break;
1365 case TD_THR_STOPPED: printf_filtered ("(stopped)"); break;
1366 case TD_THR_RUN: printf_filtered ("(run) "); break;
1367 case TD_THR_ACTIVE: printf_filtered ("(active) "); break;
1368 case TD_THR_ZOMBIE: printf_filtered ("(zombie) "); break;
1369 case TD_THR_SLEEP: printf_filtered ("(asleep) "); break;
1370 case TD_THR_STOPPED_ASLEEP:
1371 printf_filtered ("(stopped asleep)"); break;
1373 /* Print thr_create start function: */
1374 if (ti.ti_startfunc != 0)
1375 if (msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc))
1376 printf_filtered (" startfunc: %s\n", SYMBOL_NAME (msym));
1378 printf_filtered (" startfunc: 0x%08x\n", ti.ti_startfunc);
1380 /* If thread is asleep, print function that went to sleep: */
1381 if (ti.ti_state == TD_THR_SLEEP)
1382 if (msym = lookup_minimal_symbol_by_pc (ti.ti_pc))
1383 printf_filtered (" - Sleep func: %s\n", SYMBOL_NAME (msym));
1385 printf_filtered (" - Sleep func: 0x%08x\n", ti.ti_startfunc);
1387 /* Wrap up line, if necessary */
1388 if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0)
1389 printf_filtered ("\n"); /* don't you hate counting newlines? */
1392 warning ("info sol-thread: failed to get info for thread.");
1397 /* List some state about each Solaris user thread in the inferior. */
1400 info_solthreads (args, from_tty)
1404 p_td_ta_thr_iter (main_ta, info_cb, args,
1405 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1406 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1408 #endif /* MAINTENANCE_CMDS */
1411 ignore (addr, contents)
1418 struct target_ops sol_thread_ops = {
1419 "solaris-threads", /* to_shortname */
1420 "Solaris threads and pthread.", /* to_longname */
1421 "Solaris threads and pthread support.", /* to_doc */
1422 sol_thread_open, /* to_open */
1424 sol_thread_attach, /* to_attach */
1425 sol_thread_detach, /* to_detach */
1426 sol_thread_resume, /* to_resume */
1427 sol_thread_wait, /* to_wait */
1428 sol_thread_fetch_registers, /* to_fetch_registers */
1429 sol_thread_store_registers, /* to_store_registers */
1430 sol_thread_prepare_to_store, /* to_prepare_to_store */
1431 sol_thread_xfer_memory, /* to_xfer_memory */
1432 sol_thread_files_info, /* to_files_info */
1433 memory_insert_breakpoint, /* to_insert_breakpoint */
1434 memory_remove_breakpoint, /* to_remove_breakpoint */
1435 terminal_init_inferior, /* to_terminal_init */
1436 terminal_inferior, /* to_terminal_inferior */
1437 terminal_ours_for_output, /* to_terminal_ours_for_output */
1438 terminal_ours, /* to_terminal_ours */
1439 child_terminal_info, /* to_terminal_info */
1440 sol_thread_kill_inferior, /* to_kill */
1442 0, /* to_lookup_symbol */
1443 sol_thread_create_inferior, /* to_create_inferior */
1444 sol_thread_mourn_inferior, /* to_mourn_inferior */
1445 sol_thread_can_run, /* to_can_run */
1446 sol_thread_notice_signals, /* to_notice_signals */
1447 sol_thread_alive, /* to_thread_alive */
1448 sol_thread_stop, /* to_stop */
1449 process_stratum, /* to_stratum */
1451 1, /* to_has_all_memory */
1452 1, /* to_has_memory */
1453 1, /* to_has_stack */
1454 1, /* to_has_registers */
1455 1, /* to_has_execution */
1457 0, /* sections_end */
1458 OPS_MAGIC /* to_magic */
1461 struct target_ops sol_core_ops = {
1462 "solaris-core", /* to_shortname */
1463 "Solaris core threads and pthread.", /* to_longname */
1464 "Solaris threads and pthread support for core files.", /* to_doc */
1465 sol_core_open, /* to_open */
1466 sol_core_close, /* to_close */
1467 sol_thread_attach, /* XXX to_attach */
1468 sol_core_detach, /* to_detach */
1471 sol_thread_fetch_registers, /* to_fetch_registers */
1472 0, /* to_store_registers */
1473 0, /* to_prepare_to_store */
1474 sol_thread_xfer_memory, /* XXX to_xfer_memory */
1475 sol_core_files_info, /* to_files_info */
1476 ignore, /* to_insert_breakpoint */
1477 ignore, /* to_remove_breakpoint */
1478 0, /* to_terminal_init */
1479 0, /* to_terminal_inferior */
1480 0, /* to_terminal_ours_for_output */
1481 0, /* to_terminal_ours */
1482 0, /* to_terminal_info */
1485 0, /* to_lookup_symbol */
1486 sol_thread_create_inferior, /* XXX to_create_inferior */
1487 0, /* to_mourn_inferior */
1489 0, /* to_notice_signals */
1490 0, /* to_thread_alive */
1492 core_stratum, /* to_stratum */
1494 0, /* to_has_all_memory */
1495 1, /* to_has_memory */
1496 1, /* to_has_stack */
1497 1, /* to_has_registers */
1498 0, /* to_has_execution */
1500 0, /* sections_end */
1501 OPS_MAGIC /* to_magic */
1504 /* we suppress the call to add_target of core_ops in corelow because
1505 if there are two targets in the stratum core_stratum, find_core_target
1506 won't know which one to return. see corelow.c for an additonal
1507 comment on coreops_suppress_target. */
1508 int coreops_suppress_target = 1;
1511 _initialize_sol_thread ()
1515 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1519 #define resolve(X) \
1520 if (!(p_##X = dlsym (dlhandle, #X))) \
1524 resolve (td_ta_new);
1525 resolve (td_ta_delete);
1527 resolve (td_ta_get_ph);
1528 resolve (td_ta_get_nthreads);
1529 resolve (td_ta_tsd_iter);
1530 resolve (td_ta_thr_iter);
1531 resolve (td_thr_validate);
1532 resolve (td_thr_tsd);
1533 resolve (td_thr_get_info);
1534 resolve (td_thr_getfpregs);
1535 resolve (td_thr_getxregsize);
1536 resolve (td_thr_getxregs);
1537 resolve (td_thr_sigsetmask);
1538 resolve (td_thr_setprio);
1539 resolve (td_thr_setsigpending);
1540 resolve (td_thr_setfpregs);
1541 resolve (td_thr_setxregs);
1542 resolve (td_ta_map_id2thr);
1543 resolve (td_ta_map_lwp2thr);
1544 resolve (td_thr_getgregs);
1545 resolve (td_thr_setgregs);
1547 add_target (&sol_thread_ops);
1549 procfs_suppress_run = 1;
1551 #ifdef MAINTENANCE_CMDS
1552 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1553 "Show info on Solaris user threads.\n", &maintenanceinfolist);
1554 #endif /* MAINTENANCE_CMDS */
1556 memcpy(&orig_core_ops, &core_ops, sizeof (struct target_ops));
1557 memcpy(&core_ops, &sol_core_ops, sizeof (struct target_ops));
1558 add_target (&core_ops);
1564 fprintf_unfiltered (gdb_stderr, "[GDB will not be able to debug user-mode threads: %s]\n", dlerror ());
1569 /* allow the user to debug non-threaded core files */
1570 add_target(&core_ops);