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"
61 extern struct target_ops sol_thread_ops; /* Forward declaration */
62 extern struct target_ops sol_core_ops; /* Forward declaration */
64 /* place to store core_ops before we overwrite it */
65 static struct target_ops orig_core_ops;
67 struct target_ops sol_thread_ops;
68 struct target_ops sol_core_ops;
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 (int pid);
75 /* Prototypes for supply_gregset etc. */
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. */
93 static struct ps_prochandle main_ph;
94 static td_thragent_t *main_ta;
95 static int sol_thread_active = 0;
97 static struct cleanup *save_inferior_pid (void);
98 static void restore_inferior_pid (void *pid);
99 static char *td_err_string (td_err_e errcode);
100 static char *td_state_string (td_thr_state_e statecode);
101 static int thread_to_lwp (int thread_id, int default_lwp);
102 static void sol_thread_resume (int pid, int step, enum target_signal signo);
103 static int lwp_to_thread (int lwp);
104 static int sol_thread_alive (int pid);
105 static void sol_core_close (int quitting);
107 static void init_sol_thread_ops (void);
108 static void init_sol_core_ops (void);
110 /* Default definitions: These must be defined in tm.h
111 if they are to be shared with a process module such as procfs. */
113 #define THREAD_FLAG 0x80000000
114 #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
115 #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
116 #define GET_LWP(PID) TIDGET (PID)
117 #define GET_THREAD(PID) TIDGET (PID)
118 #define BUILD_LWP(TID, PID) MERGEPID (PID, TID)
120 #define BUILD_THREAD(TID, PID) (MERGEPID (PID, TID) | THREAD_FLAG)
122 /* Pointers to routines from lithread_db resolved by dlopen() */
124 static void (*p_td_log) (const int on_off);
125 static td_err_e (*p_td_ta_new) (const struct ps_prochandle * ph_p,
126 td_thragent_t ** ta_pp);
127 static td_err_e (*p_td_ta_delete) (td_thragent_t * ta_p);
128 static td_err_e (*p_td_init) (void);
129 static td_err_e (*p_td_ta_get_ph) (const td_thragent_t * ta_p,
130 struct ps_prochandle ** ph_pp);
131 static td_err_e (*p_td_ta_get_nthreads) (const td_thragent_t * ta_p,
133 static td_err_e (*p_td_ta_tsd_iter) (const td_thragent_t * ta_p,
136 static td_err_e (*p_td_ta_thr_iter) (const td_thragent_t * ta_p,
139 td_thr_state_e state,
141 sigset_t * ti_sigmask_p,
142 unsigned ti_user_flags);
143 static td_err_e (*p_td_thr_validate) (const td_thrhandle_t * th_p);
144 static td_err_e (*p_td_thr_tsd) (const td_thrhandle_t * th_p,
145 const thread_key_t key,
147 static td_err_e (*p_td_thr_get_info) (const td_thrhandle_t * th_p,
148 td_thrinfo_t * ti_p);
149 static td_err_e (*p_td_thr_getfpregs) (const td_thrhandle_t * th_p,
150 prfpregset_t * fpregset);
151 static td_err_e (*p_td_thr_getxregsize) (const td_thrhandle_t * th_p,
153 static td_err_e (*p_td_thr_getxregs) (const td_thrhandle_t * th_p,
154 const caddr_t xregset);
155 static td_err_e (*p_td_thr_sigsetmask) (const td_thrhandle_t * th_p,
156 const sigset_t ti_sigmask);
157 static td_err_e (*p_td_thr_setprio) (const td_thrhandle_t * th_p,
159 static td_err_e (*p_td_thr_setsigpending) (const td_thrhandle_t * th_p,
160 const uchar_t ti_pending_flag,
161 const sigset_t ti_pending);
162 static td_err_e (*p_td_thr_setfpregs) (const td_thrhandle_t * th_p,
163 const prfpregset_t * fpregset);
164 static td_err_e (*p_td_thr_setxregs) (const td_thrhandle_t * th_p,
165 const caddr_t xregset);
166 static td_err_e (*p_td_ta_map_id2thr) (const td_thragent_t * ta_p,
168 td_thrhandle_t * th_p);
169 static td_err_e (*p_td_ta_map_lwp2thr) (const td_thragent_t * ta_p,
171 td_thrhandle_t * th_p);
172 static td_err_e (*p_td_thr_getgregs) (const td_thrhandle_t * th_p,
174 static td_err_e (*p_td_thr_setgregs) (const td_thrhandle_t * th_p,
175 const prgregset_t regset);
181 td_err_string - Convert a thread_db error code to a string
185 char * td_err_string (errcode)
189 Return the thread_db error string associated with errcode. If errcode
190 is unknown, then return a message.
195 td_err_string (errcode)
198 static struct string_map
201 {TD_OK, "generic \"call succeeded\""},
202 {TD_ERR, "generic error."},
203 {TD_NOTHR, "no thread can be found to satisfy query"},
204 {TD_NOSV, "no synch. variable can be found to satisfy query"},
205 {TD_NOLWP, "no lwp can be found to satisfy query"},
206 {TD_BADPH, "invalid process handle"},
207 {TD_BADTH, "invalid thread handle"},
208 {TD_BADSH, "invalid synchronization handle"},
209 {TD_BADTA, "invalid thread agent"},
210 {TD_BADKEY, "invalid key"},
211 {TD_NOMSG, "td_thr_event_getmsg() called when there was no message"},
212 {TD_NOFPREGS, "FPU register set not available for given thread"},
213 {TD_NOLIBTHREAD, "application not linked with libthread"},
214 {TD_NOEVENT, "requested event is not supported"},
215 {TD_NOCAPAB, "capability not available"},
216 {TD_DBERR, "Debugger service failed"},
217 {TD_NOAPLIC, "Operation not applicable to"},
218 {TD_NOTSD, "No thread specific data for this thread"},
219 {TD_MALLOC, "Malloc failed"},
220 {TD_PARTIALREG, "Only part of register set was writen/read"},
221 {TD_NOXREGS, "X register set not available for given thread"}
223 const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
227 for (i = 0; i < td_err_size; i++)
228 if (td_err_table[i].num == errcode)
229 return td_err_table[i].str;
231 sprintf (buf, "Unknown thread_db error code: %d", errcode);
240 td_state_string - Convert a thread_db state code to a string
244 char * td_state_string (statecode)
248 Return the thread_db state string associated with statecode. If
249 statecode is unknown, then return a message.
254 td_state_string (statecode)
255 td_thr_state_e statecode;
257 static struct string_map
258 td_thr_state_table[] =
260 {TD_THR_ANY_STATE, "any state"},
261 {TD_THR_UNKNOWN, "unknown"},
262 {TD_THR_STOPPED, "stopped"},
264 {TD_THR_ACTIVE, "active"},
265 {TD_THR_ZOMBIE, "zombie"},
266 {TD_THR_SLEEP, "sleep"},
267 {TD_THR_STOPPED_ASLEEP, "stopped asleep"}
269 const int td_thr_state_table_size = sizeof td_thr_state_table / sizeof (struct string_map);
273 for (i = 0; i < td_thr_state_table_size; i++)
274 if (td_thr_state_table[i].num == statecode)
275 return td_thr_state_table[i].str;
277 sprintf (buf, "Unknown thread_db state code: %d", statecode);
286 thread_to_lwp - Convert a Posix or Solaris thread id to a LWP id.
290 int thread_to_lwp (thread_id, default_lwp)
294 This function converts a Posix or Solaris thread id to a lightweight
295 process id. If thread_id is non-existent, that's an error. If it's
296 an inactive thread, then we return default_lwp.
300 This function probably shouldn't call error()...
305 thread_to_lwp (thread_id, default_lwp)
313 if (is_lwp (thread_id))
314 return thread_id; /* It's already an LWP id */
316 /* It's a thread. Convert to lwp */
318 val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th);
320 return -1; /* thread must have terminated */
321 else if (val != TD_OK)
322 error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val));
324 val = p_td_thr_get_info (&th, &ti);
326 return -1; /* thread must have terminated */
327 else if (val != TD_OK)
328 error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val));
330 if (ti.ti_state != TD_THR_ACTIVE)
332 if (default_lwp != -1)
334 error ("thread_to_lwp: thread state not active: %s",
335 td_state_string (ti.ti_state));
338 return BUILD_LWP (ti.ti_lid, PIDGET (thread_id));
345 lwp_to_thread - Convert a LWP id to a Posix or Solaris thread id.
349 int lwp_to_thread (lwp_id)
353 This function converts a lightweight process id to a Posix or Solaris
354 thread id. If thread_id is non-existent, that's an error.
358 This function probably shouldn't call error()...
371 return lwp; /* It's already a thread id */
373 /* It's an lwp. Convert it to a thread id. */
375 if (!sol_thread_alive (lwp))
376 return -1; /* defunct lwp */
378 val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th);
380 return -1; /* thread must have terminated */
381 else if (val != TD_OK)
382 error ("lwp_to_thread: td_ta_map_lwp2thr: %s.", td_err_string (val));
384 val = p_td_thr_validate (&th);
386 return lwp; /* libthread doesn't know about it;
388 else if (val != TD_OK)
389 error ("lwp_to_thread: td_thr_validate: %s.", td_err_string (val));
391 val = p_td_thr_get_info (&th, &ti);
393 return -1; /* thread must have terminated */
394 else if (val != TD_OK)
395 error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val));
397 return BUILD_THREAD (ti.ti_tid, PIDGET (lwp));
404 save_inferior_pid - Save inferior_pid on the cleanup list
405 restore_inferior_pid - Restore inferior_pid from the cleanup list
409 struct cleanup *save_inferior_pid ()
410 void restore_inferior_pid (int pid)
414 These two functions act in unison to restore inferior_pid in
419 inferior_pid is a global variable that needs to be changed by many of
420 these routines before calling functions in procfs.c. In order to
421 guarantee that inferior_pid gets restored (in case of errors), you
422 need to call save_inferior_pid before changing it. At the end of the
423 function, you should invoke do_cleanups to restore it.
428 static struct cleanup *
431 return make_cleanup (restore_inferior_pid, (void *) inferior_pid);
435 restore_inferior_pid (pid)
438 inferior_pid = (int) pid;
442 /* Most target vector functions from here on actually just pass through to
443 procfs.c, as they don't need to do anything specific for threads. */
448 sol_thread_open (arg, from_tty)
452 procfs_ops.to_open (arg, from_tty);
455 /* Attach to process PID, then initialize for debugging it
456 and wait for the trace-trap that results from attaching. */
459 sol_thread_attach (args, from_tty)
463 procfs_ops.to_attach (args, from_tty);
464 /* Must get symbols from solibs before libthread_db can run! */
465 SOLIB_ADD ((char *) 0, from_tty, (struct target_ops *) 0);
466 if (sol_thread_active)
468 printf_filtered ("sol-thread active.\n");
469 main_ph.pid = inferior_pid; /* Save for xfer_memory */
470 push_target (&sol_thread_ops);
471 inferior_pid = lwp_to_thread (inferior_pid);
472 if (inferior_pid == -1)
473 inferior_pid = main_ph.pid;
475 add_thread (inferior_pid);
477 /* XXX - might want to iterate over all the threads and register them. */
480 /* Take a program previously attached to and detaches it.
481 The program resumes execution and will no longer stop
482 on signals, etc. We'd better not have left any breakpoints
483 in the program or it'll die when it hits one. For this
484 to work, it may be necessary for the process to have been
485 previously attached. It *might* work if the program was
486 started via the normal ptrace (PTRACE_TRACEME). */
489 sol_thread_detach (args, from_tty)
493 inferior_pid = PIDGET (main_ph.pid);
494 unpush_target (&sol_thread_ops);
495 procfs_ops.to_detach (args, from_tty);
498 /* Resume execution of process PID. If STEP is nozero, then
499 just single step it. If SIGNAL is nonzero, restart it with that
500 signal activated. We may have to convert pid from a thread-id to an LWP id
504 sol_thread_resume (pid, step, signo)
507 enum target_signal signo;
509 struct cleanup *old_chain;
511 old_chain = save_inferior_pid ();
513 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
514 if (inferior_pid == -1)
515 inferior_pid = procfs_first_available ();
521 pid = thread_to_lwp (pid, -2);
522 if (pid == -2) /* Inactive thread */
523 error ("This version of Solaris can't start inactive threads.");
524 if (info_verbose && pid == -1)
525 warning ("Specified thread %d seems to have terminated",
526 GET_THREAD (save_pid));
529 procfs_ops.to_resume (pid, step, signo);
531 do_cleanups (old_chain);
534 /* Wait for any threads to stop. We may have to convert PID from a thread id
535 to a LWP id, and vice versa on the way out. */
538 sol_thread_wait (pid, ourstatus)
540 struct target_waitstatus *ourstatus;
544 struct cleanup *old_chain;
546 save_pid = inferior_pid;
547 old_chain = save_inferior_pid ();
549 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
550 if (inferior_pid == -1)
551 inferior_pid = procfs_first_available ();
557 pid = thread_to_lwp (pid, -2);
558 if (pid == -2) /* Inactive thread */
559 error ("This version of Solaris can't start inactive threads.");
560 if (info_verbose && pid == -1)
561 warning ("Specified thread %d seems to have terminated",
562 GET_THREAD (save_pid));
565 rtnval = procfs_ops.to_wait (pid, ourstatus);
567 if (ourstatus->kind != TARGET_WAITKIND_EXITED)
569 /* Map the LWP of interest back to the appropriate thread ID */
570 rtnval = lwp_to_thread (rtnval);
574 /* See if we have a new thread */
575 if (is_thread (rtnval)
576 && rtnval != save_pid
577 && !in_thread_list (rtnval))
579 printf_filtered ("[New %s]\n", target_pid_to_str (rtnval));
584 /* During process initialization, we may get here without the thread package
585 being initialized, since that can only happen after we've found the shared
588 do_cleanups (old_chain);
594 sol_thread_fetch_registers (regno)
598 td_thrhandle_t thandle;
601 prfpregset_t fpregset;
607 if (!is_thread (inferior_pid))
608 { /* LWP: pass the request on to procfs.c */
609 if (target_has_execution)
610 procfs_ops.to_fetch_registers (regno);
612 orig_core_ops.to_fetch_registers (regno);
616 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
618 thread = GET_THREAD (inferior_pid);
621 error ("sol_thread_fetch_registers: thread == 0");
623 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
625 error ("sol_thread_fetch_registers: td_ta_map_id2thr: %s",
626 td_err_string (val));
628 /* Get the integer regs */
630 val = p_td_thr_getgregs (&thandle, gregset);
632 && val != TD_PARTIALREG)
633 error ("sol_thread_fetch_registers: td_thr_getgregs %s",
634 td_err_string (val));
636 /* For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, pc and sp
637 are saved (by a thread context switch). */
639 /* And, now the fp regs */
641 val = p_td_thr_getfpregs (&thandle, &fpregset);
643 && val != TD_NOFPREGS)
644 error ("sol_thread_fetch_registers: td_thr_getfpregs %s",
645 td_err_string (val));
647 /* Note that we must call supply_{g fp}regset *after* calling the td routines
648 because the td routines call ps_lget* which affect the values stored in the
651 supply_gregset ((gdb_gregset_t *) &gregset);
652 supply_fpregset ((gdb_fpregset_t *) &fpregset);
655 /* thread_db doesn't seem to handle this right */
656 val = td_thr_getxregsize (&thandle, &xregsize);
657 if (val != TD_OK && val != TD_NOXREGS)
658 error ("sol_thread_fetch_registers: td_thr_getxregsize %s",
659 td_err_string (val));
663 xregset = alloca (xregsize);
664 val = td_thr_getxregs (&thandle, xregset);
666 error ("sol_thread_fetch_registers: td_thr_getxregs %s",
667 td_err_string (val));
673 sol_thread_store_registers (regno)
677 td_thrhandle_t thandle;
680 prfpregset_t fpregset;
686 if (!is_thread (inferior_pid))
687 { /* LWP: pass the request on to procfs.c */
688 procfs_ops.to_store_registers (regno);
692 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
694 thread = GET_THREAD (inferior_pid);
696 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
698 error ("sol_thread_store_registers: td_ta_map_id2thr %s",
699 td_err_string (val));
702 { /* Not writing all the regs */
703 /* save new register value */
704 char old_value[REGISTER_SIZE];
705 memcpy (old_value, ®isters[REGISTER_BYTE (regno)], REGISTER_SIZE);
707 val = p_td_thr_getgregs (&thandle, gregset);
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));
716 /* restore new register value */
717 memcpy (®isters[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE);
720 /* thread_db doesn't seem to handle this right */
721 val = td_thr_getxregsize (&thandle, &xregsize);
722 if (val != TD_OK && val != TD_NOXREGS)
723 error ("sol_thread_store_registers: td_thr_getxregsize %s",
724 td_err_string (val));
728 xregset = alloca (xregsize);
729 val = td_thr_getxregs (&thandle, xregset);
731 error ("sol_thread_store_registers: td_thr_getxregs %s",
732 td_err_string (val));
737 fill_gregset ((gdb_gregset_t *) &gregset, regno);
738 fill_fpregset ((gdb_fpregset_t *) &fpregset, regno);
740 val = p_td_thr_setgregs (&thandle, gregset);
742 error ("sol_thread_store_registers: td_thr_setgregs %s",
743 td_err_string (val));
744 val = p_td_thr_setfpregs (&thandle, &fpregset);
746 error ("sol_thread_store_registers: td_thr_setfpregs %s",
747 td_err_string (val));
750 /* thread_db doesn't seem to handle this right */
751 val = td_thr_getxregsize (&thandle, &xregsize);
752 if (val != TD_OK && val != TD_NOXREGS)
753 error ("sol_thread_store_registers: td_thr_getxregsize %s",
754 td_err_string (val));
756 /* Should probably do something about writing the xregs here, but what are
761 /* Get ready to modify the registers array. On machines which store
762 individual registers, this doesn't need to do anything. On machines
763 which store all the registers in one fell swoop, this makes sure
764 that registers contains all the registers from the program being
768 sol_thread_prepare_to_store ()
770 procfs_ops.to_prepare_to_store ();
774 sol_thread_xfer_memory (memaddr, myaddr, len, dowrite, target)
779 struct target_ops *target; /* ignored */
782 struct cleanup *old_chain;
784 old_chain = save_inferior_pid ();
786 if (is_thread (inferior_pid) || /* A thread */
787 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
788 inferior_pid = procfs_first_available (); /* Find any live lwp. */
789 /* Note: don't need to call switch_to_thread; we're just reading memory. */
791 if (target_has_execution)
792 retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
794 retval = orig_core_ops.to_xfer_memory (memaddr, myaddr, len,
797 do_cleanups (old_chain);
802 /* Print status information about what we're accessing. */
805 sol_thread_files_info (ignore)
806 struct target_ops *ignore;
808 procfs_ops.to_files_info (ignore);
812 sol_thread_kill_inferior ()
814 procfs_ops.to_kill ();
818 sol_thread_notice_signals (pid)
821 procfs_ops.to_notice_signals (PIDGET (pid));
824 /* Fork an inferior process, and start debugging it with /proc. */
827 sol_thread_create_inferior (exec_file, allargs, env)
832 procfs_ops.to_create_inferior (exec_file, allargs, env);
834 if (sol_thread_active && inferior_pid != 0)
836 main_ph.pid = inferior_pid; /* Save for xfer_memory */
838 push_target (&sol_thread_ops);
840 inferior_pid = lwp_to_thread (inferior_pid);
841 if (inferior_pid == -1)
842 inferior_pid = main_ph.pid;
844 if (!in_thread_list (inferior_pid))
845 add_thread (inferior_pid);
849 /* This routine is called whenever a new symbol table is read in, or when all
850 symbol tables are removed. libthread_db can only be initialized when it
851 finds the right variables in libthread.so. Since it's a shared library,
852 those variables don't show up until the library gets mapped and the symbol
855 /* This new_objfile event is now managed by a chained function pointer.
856 * It is the callee's responsability to call the next client on the chain.
859 /* Saved pointer to previous owner of the new_objfile event. */
860 static void (*target_new_objfile_chain) PARAMS ((struct objfile *));
863 sol_thread_new_objfile (objfile)
864 struct objfile *objfile;
870 sol_thread_active = 0;
874 /* don't do anything if init failed to resolve the libthread_db library */
875 if (!procfs_suppress_run)
878 /* Now, initialize the thread debugging library. This needs to be done after
879 the shared libraries are located because it needs information from the
880 user's thread library. */
885 warning ("sol_thread_new_objfile: td_init: %s", td_err_string (val));
889 val = p_td_ta_new (&main_ph, &main_ta);
890 if (val == TD_NOLIBTHREAD)
892 else if (val != TD_OK)
894 warning ("sol_thread_new_objfile: td_ta_new: %s", td_err_string (val));
898 sol_thread_active = 1;
900 /* Call predecessor on chain, if any. */
901 if (target_new_objfile_chain)
902 target_new_objfile_chain (objfile);
905 /* Clean up after the inferior dies. */
908 sol_thread_mourn_inferior ()
910 unpush_target (&sol_thread_ops);
911 procfs_ops.to_mourn_inferior ();
914 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
917 sol_thread_can_run ()
919 return procfs_suppress_run;
926 sol_thread_alive - test thread for "aliveness"
930 static bool sol_thread_alive (int pid);
934 returns true if thread still active in inferior.
939 sol_thread_alive (pid)
942 if (is_thread (pid)) /* non-kernel thread */
947 pid = GET_THREAD (pid);
948 if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK)
949 return 0; /* thread not found */
950 if ((val = p_td_thr_validate (&th)) != TD_OK)
951 return 0; /* thread not valid */
952 return 1; /* known thread: return true */
955 /* kernel thread (LWP): let procfs test it */
957 if (target_has_execution)
958 return procfs_ops.to_thread_alive (pid);
960 return orig_core_ops.to_thread_alive (pid);
967 procfs_ops.to_stop ();
970 /* These routines implement the lower half of the thread_db interface. Ie: the
973 /* Various versions of <proc_service.h> have slightly
974 different function prototypes. In particular, we have
977 struct ps_prochandle * const struct ps_prochandle *
982 Which one you have depends on solaris version and what
983 patches you've applied. On the theory that there are
984 only two major variants, we have configure check the
985 prototype of ps_pdwrite (), and use that info to make
986 appropriate typedefs here. */
988 #ifdef PROC_SERVICE_IS_OLD
989 typedef const struct ps_prochandle *gdb_ps_prochandle_t;
990 typedef char *gdb_ps_read_buf_t;
991 typedef char *gdb_ps_write_buf_t;
992 typedef int gdb_ps_size_t;
993 typedef paddr_t gdb_ps_addr_t;
995 typedef struct ps_prochandle *gdb_ps_prochandle_t;
996 typedef void *gdb_ps_read_buf_t;
997 typedef const void *gdb_ps_write_buf_t;
998 typedef size_t gdb_ps_size_t;
999 typedef psaddr_t gdb_ps_addr_t;
1003 /* The next four routines are called by thread_db to tell us to stop and stop
1004 a particular process or lwp. Since GDB ensures that these are all stopped
1005 by the time we call anything in thread_db, these routines need to do
1011 ps_pstop (gdb_ps_prochandle_t ph)
1016 /* Process continue */
1019 ps_pcontinue (gdb_ps_prochandle_t ph)
1027 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1035 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1040 /* Looks up the symbol LD_SYMBOL_NAME in the debugger's symbol table. */
1043 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *ld_object_name,
1044 const char *ld_symbol_name, gdb_ps_addr_t * ld_symbol_addr)
1046 struct minimal_symbol *ms;
1048 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
1053 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
1058 /* Common routine for reading and writing memory. */
1061 rw_common (int dowrite, const struct ps_prochandle *ph, gdb_ps_addr_t addr,
1062 char *buf, int size)
1064 struct cleanup *old_chain;
1066 old_chain = save_inferior_pid ();
1068 if (is_thread (inferior_pid) || /* A thread */
1069 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
1070 inferior_pid = procfs_first_available (); /* Find any live lwp. */
1071 /* Note: don't need to call switch_to_thread; we're just reading memory. */
1077 if (target_has_execution)
1078 cc = procfs_ops.to_xfer_memory (addr, buf, size, dowrite, &procfs_ops);
1080 cc = orig_core_ops.to_xfer_memory (addr, buf, size, dowrite, &core_ops);
1085 print_sys_errmsg ("rw_common (): read", errno);
1087 print_sys_errmsg ("rw_common (): write", errno);
1089 do_cleanups (old_chain);
1096 warning ("rw_common (): unable to read at addr 0x%lx",
1099 warning ("rw_common (): unable to write at addr 0x%lx",
1102 do_cleanups (old_chain);
1111 do_cleanups (old_chain);
1116 /* Copies SIZE bytes from target process .data segment to debugger memory. */
1119 ps_pdread (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1120 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1122 return rw_common (0, ph, addr, buf, size);
1125 /* Copies SIZE bytes from debugger memory .data segment to target process. */
1128 ps_pdwrite (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1129 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1131 return rw_common (1, ph, addr, (char *) buf, size);
1134 /* Copies SIZE bytes from target process .text segment to debugger memory. */
1137 ps_ptread (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1138 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1140 return rw_common (0, ph, addr, buf, size);
1143 /* Copies SIZE bytes from debugger memory .text segment to target process. */
1146 ps_ptwrite (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1147 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1149 return rw_common (1, ph, addr, (char *) buf, size);
1152 /* Get integer regs for LWP */
1155 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1156 prgregset_t gregset)
1158 struct cleanup *old_chain;
1160 old_chain = save_inferior_pid ();
1162 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1164 if (target_has_execution)
1165 procfs_ops.to_fetch_registers (-1);
1167 orig_core_ops.to_fetch_registers (-1);
1168 fill_gregset ((gdb_gregset_t *) gregset, -1);
1170 do_cleanups (old_chain);
1175 /* Set integer regs for LWP */
1178 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1179 const prgregset_t gregset)
1181 struct cleanup *old_chain;
1183 old_chain = save_inferior_pid ();
1185 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1187 supply_gregset ((gdb_gregset_t *) gregset);
1188 if (target_has_execution)
1189 procfs_ops.to_store_registers (-1);
1191 orig_core_ops.to_store_registers (-1);
1193 do_cleanups (old_chain);
1198 /* Log a message (sends to gdb_stderr). */
1201 ps_plog (const char *fmt,...)
1205 va_start (args, fmt);
1207 vfprintf_filtered (gdb_stderr, fmt, args);
1210 /* Get size of extra register set. Currently a noop. */
1213 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
1220 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1224 if (ioctl (lwp_fd, PIOCGXREGSIZE, ®size))
1226 if (errno == EINVAL)
1227 return PS_NOFREGS; /* XXX Wrong code, but this is the closest
1228 thing in proc_service.h */
1230 print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno);
1238 /* Get extra register set. Currently a noop. */
1241 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1247 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1251 if (ioctl (lwp_fd, PIOCGXREG, xregset))
1253 print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno);
1261 /* Set extra register set. Currently a noop. */
1264 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1270 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1274 if (ioctl (lwp_fd, PIOCSXREG, xregset))
1276 print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno);
1284 /* Get floating-point regs for LWP */
1287 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1288 prfpregset_t * fpregset)
1290 struct cleanup *old_chain;
1292 old_chain = save_inferior_pid ();
1294 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1296 if (target_has_execution)
1297 procfs_ops.to_fetch_registers (-1);
1299 orig_core_ops.to_fetch_registers (-1);
1300 fill_fpregset ((gdb_fpregset_t *) fpregset, -1);
1302 do_cleanups (old_chain);
1307 /* Set floating-point regs for LWP */
1310 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1311 const prfpregset_t * fpregset)
1313 struct cleanup *old_chain;
1315 old_chain = save_inferior_pid ();
1317 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1319 supply_fpregset ((gdb_fpregset_t *) fpregset);
1320 if (target_has_execution)
1321 procfs_ops.to_store_registers (-1);
1323 orig_core_ops.to_store_registers (-1);
1325 do_cleanups (old_chain);
1330 #ifdef TM_I386SOL2_H
1332 /* Reads the local descriptor table of a LWP. */
1335 ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1338 /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */
1339 extern struct ssd *procfs_find_LDT_entry (int);
1342 /* FIXME: can't I get the process ID from the prochandle or something?
1345 if (inferior_pid <= 0 || lwpid <= 0)
1348 ret = procfs_find_LDT_entry (BUILD_LWP (lwpid, PIDGET (inferior_pid)));
1351 memcpy (pldt, ret, sizeof (struct ssd));
1354 else /* LDT not found. */
1357 #endif /* TM_I386SOL2_H */
1359 /* Convert a pid to printable form. */
1362 solaris_pid_to_str (pid)
1365 static char buf[100];
1367 /* in case init failed to resolve the libthread_db library */
1368 if (!procfs_suppress_run)
1369 return procfs_pid_to_str (pid);
1371 if (is_thread (pid))
1375 lwp = thread_to_lwp (pid, -2);
1378 sprintf (buf, "Thread %d (defunct)", GET_THREAD (pid));
1380 sprintf (buf, "Thread %d (LWP %d)", GET_THREAD (pid), GET_LWP (lwp));
1382 sprintf (buf, "Thread %d ", GET_THREAD (pid));
1384 else if (GET_LWP (pid) != 0)
1385 sprintf (buf, "LWP %d ", GET_LWP (pid));
1387 sprintf (buf, "process %d ", PIDGET (pid));
1393 /* Worker bee for find_new_threads
1394 Callback function that gets called once per USER thread (i.e., not
1398 sol_find_new_threads_callback (th, ignored)
1399 const td_thrhandle_t *th;
1406 if ((retval = p_td_thr_get_info (th, &ti)) != TD_OK)
1410 pid = BUILD_THREAD (ti.ti_tid, PIDGET (inferior_pid));
1411 if (!in_thread_list (pid))
1418 sol_find_new_threads ()
1420 /* don't do anything if init failed to resolve the libthread_db library */
1421 if (!procfs_suppress_run)
1424 if (inferior_pid == -1)
1426 printf_filtered ("No process.\n");
1429 procfs_find_new_threads (); /* first find new kernel threads. */
1430 p_td_ta_thr_iter (main_ta, sol_find_new_threads_callback, (void *) 0,
1431 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1432 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1436 sol_core_open (filename, from_tty)
1440 orig_core_ops.to_open (filename, from_tty);
1444 sol_core_close (quitting)
1447 orig_core_ops.to_close (quitting);
1451 sol_core_detach (args, from_tty)
1455 unpush_target (&core_ops);
1456 orig_core_ops.to_detach (args, from_tty);
1460 sol_core_files_info (t)
1461 struct target_ops *t;
1463 orig_core_ops.to_files_info (t);
1466 /* Worker bee for info sol-thread command. This is a callback function that
1467 gets called once for each Solaris thread (ie. not kernel thread) in the
1468 inferior. Print anything interesting that we can think of. */
1472 const td_thrhandle_t *th;
1478 if ((ret = p_td_thr_get_info (th, &ti)) == TD_OK)
1480 printf_filtered ("%s thread #%d, lwp %d, ",
1481 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
1482 ti.ti_tid, ti.ti_lid);
1483 switch (ti.ti_state)
1486 case TD_THR_UNKNOWN:
1487 printf_filtered ("<unknown state>");
1489 case TD_THR_STOPPED:
1490 printf_filtered ("(stopped)");
1493 printf_filtered ("(run) ");
1496 printf_filtered ("(active) ");
1499 printf_filtered ("(zombie) ");
1502 printf_filtered ("(asleep) ");
1504 case TD_THR_STOPPED_ASLEEP:
1505 printf_filtered ("(stopped asleep)");
1508 /* Print thr_create start function: */
1509 if (ti.ti_startfunc != 0)
1511 struct minimal_symbol *msym;
1512 msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc);
1514 printf_filtered (" startfunc: %s\n", SYMBOL_NAME (msym));
1516 printf_filtered (" startfunc: 0x%s\n", paddr (ti.ti_startfunc));
1519 /* If thread is asleep, print function that went to sleep: */
1520 if (ti.ti_state == TD_THR_SLEEP)
1522 struct minimal_symbol *msym;
1523 msym = lookup_minimal_symbol_by_pc (ti.ti_pc);
1525 printf_filtered (" - Sleep func: %s\n", SYMBOL_NAME (msym));
1527 printf_filtered (" - Sleep func: 0x%s\n", paddr (ti.ti_startfunc));
1530 /* Wrap up line, if necessary */
1531 if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0)
1532 printf_filtered ("\n"); /* don't you hate counting newlines? */
1535 warning ("info sol-thread: failed to get info for thread.");
1540 /* List some state about each Solaris user thread in the inferior. */
1543 info_solthreads (args, from_tty)
1547 p_td_ta_thr_iter (main_ta, info_cb, args,
1548 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1549 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1553 ignore (addr, contents)
1562 init_sol_thread_ops ()
1564 sol_thread_ops.to_shortname = "solaris-threads";
1565 sol_thread_ops.to_longname = "Solaris threads and pthread.";
1566 sol_thread_ops.to_doc = "Solaris threads and pthread support.";
1567 sol_thread_ops.to_open = sol_thread_open;
1568 sol_thread_ops.to_close = 0;
1569 sol_thread_ops.to_attach = sol_thread_attach;
1570 sol_thread_ops.to_detach = sol_thread_detach;
1571 sol_thread_ops.to_resume = sol_thread_resume;
1572 sol_thread_ops.to_wait = sol_thread_wait;
1573 sol_thread_ops.to_fetch_registers = sol_thread_fetch_registers;
1574 sol_thread_ops.to_store_registers = sol_thread_store_registers;
1575 sol_thread_ops.to_prepare_to_store = sol_thread_prepare_to_store;
1576 sol_thread_ops.to_xfer_memory = sol_thread_xfer_memory;
1577 sol_thread_ops.to_files_info = sol_thread_files_info;
1578 sol_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1579 sol_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1580 sol_thread_ops.to_terminal_init = terminal_init_inferior;
1581 sol_thread_ops.to_terminal_inferior = terminal_inferior;
1582 sol_thread_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1583 sol_thread_ops.to_terminal_ours = terminal_ours;
1584 sol_thread_ops.to_terminal_info = child_terminal_info;
1585 sol_thread_ops.to_kill = sol_thread_kill_inferior;
1586 sol_thread_ops.to_load = 0;
1587 sol_thread_ops.to_lookup_symbol = 0;
1588 sol_thread_ops.to_create_inferior = sol_thread_create_inferior;
1589 sol_thread_ops.to_mourn_inferior = sol_thread_mourn_inferior;
1590 sol_thread_ops.to_can_run = sol_thread_can_run;
1591 sol_thread_ops.to_notice_signals = sol_thread_notice_signals;
1592 sol_thread_ops.to_thread_alive = sol_thread_alive;
1593 sol_thread_ops.to_pid_to_str = solaris_pid_to_str;
1594 sol_thread_ops.to_find_new_threads = sol_find_new_threads;
1595 sol_thread_ops.to_stop = sol_thread_stop;
1596 sol_thread_ops.to_stratum = process_stratum;
1597 sol_thread_ops.to_has_all_memory = 1;
1598 sol_thread_ops.to_has_memory = 1;
1599 sol_thread_ops.to_has_stack = 1;
1600 sol_thread_ops.to_has_registers = 1;
1601 sol_thread_ops.to_has_execution = 1;
1602 sol_thread_ops.to_has_thread_control = tc_none;
1603 sol_thread_ops.to_sections = 0;
1604 sol_thread_ops.to_sections_end = 0;
1605 sol_thread_ops.to_magic = OPS_MAGIC;
1610 init_sol_core_ops ()
1612 sol_core_ops.to_shortname = "solaris-core";
1613 sol_core_ops.to_longname = "Solaris core threads and pthread.";
1614 sol_core_ops.to_doc = "Solaris threads and pthread support for core files.";
1615 sol_core_ops.to_open = sol_core_open;
1616 sol_core_ops.to_close = sol_core_close;
1617 sol_core_ops.to_attach = sol_thread_attach;
1618 sol_core_ops.to_detach = sol_core_detach;
1619 /* sol_core_ops.to_resume = 0; */
1620 /* sol_core_ops.to_wait = 0; */
1621 sol_core_ops.to_fetch_registers = sol_thread_fetch_registers;
1622 /* sol_core_ops.to_store_registers = 0; */
1623 /* sol_core_ops.to_prepare_to_store = 0; */
1624 sol_core_ops.to_xfer_memory = sol_thread_xfer_memory;
1625 sol_core_ops.to_files_info = sol_core_files_info;
1626 sol_core_ops.to_insert_breakpoint = ignore;
1627 sol_core_ops.to_remove_breakpoint = ignore;
1628 /* sol_core_ops.to_terminal_init = 0; */
1629 /* sol_core_ops.to_terminal_inferior = 0; */
1630 /* sol_core_ops.to_terminal_ours_for_output = 0; */
1631 /* sol_core_ops.to_terminal_ours = 0; */
1632 /* sol_core_ops.to_terminal_info = 0; */
1633 /* sol_core_ops.to_kill = 0; */
1634 /* sol_core_ops.to_load = 0; */
1635 /* sol_core_ops.to_lookup_symbol = 0; */
1636 sol_core_ops.to_create_inferior = sol_thread_create_inferior;
1637 sol_core_ops.to_stratum = core_stratum;
1638 sol_core_ops.to_has_all_memory = 0;
1639 sol_core_ops.to_has_memory = 1;
1640 sol_core_ops.to_has_stack = 1;
1641 sol_core_ops.to_has_registers = 1;
1642 sol_core_ops.to_has_execution = 0;
1643 sol_core_ops.to_has_thread_control = tc_none;
1644 sol_core_ops.to_thread_alive = sol_thread_alive;
1645 sol_core_ops.to_pid_to_str = solaris_pid_to_str;
1646 /* On Solaris/x86, when debugging a threaded core file from process <n>,
1647 the following causes "info threads" to produce "procfs: couldn't find pid
1648 <n> in procinfo list" where <n> is the pid of the process that produced
1649 the core file. Disable it for now. */
1650 /* sol_core_ops.to_find_new_threads = sol_find_new_threads; */
1651 sol_core_ops.to_sections = 0;
1652 sol_core_ops.to_sections_end = 0;
1653 sol_core_ops.to_magic = OPS_MAGIC;
1656 /* we suppress the call to add_target of core_ops in corelow because
1657 if there are two targets in the stratum core_stratum, find_core_target
1658 won't know which one to return. see corelow.c for an additonal
1659 comment on coreops_suppress_target. */
1660 int coreops_suppress_target = 1;
1663 _initialize_sol_thread ()
1667 init_sol_thread_ops ();
1668 init_sol_core_ops ();
1670 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1674 #define resolve(X) \
1675 if (!(p_##X = dlsym (dlhandle, #X))) \
1679 resolve (td_ta_new);
1680 resolve (td_ta_delete);
1682 resolve (td_ta_get_ph);
1683 resolve (td_ta_get_nthreads);
1684 resolve (td_ta_tsd_iter);
1685 resolve (td_ta_thr_iter);
1686 resolve (td_thr_validate);
1687 resolve (td_thr_tsd);
1688 resolve (td_thr_get_info);
1689 resolve (td_thr_getfpregs);
1690 resolve (td_thr_getxregsize);
1691 resolve (td_thr_getxregs);
1692 resolve (td_thr_sigsetmask);
1693 resolve (td_thr_setprio);
1694 resolve (td_thr_setsigpending);
1695 resolve (td_thr_setfpregs);
1696 resolve (td_thr_setxregs);
1697 resolve (td_ta_map_id2thr);
1698 resolve (td_ta_map_lwp2thr);
1699 resolve (td_thr_getgregs);
1700 resolve (td_thr_setgregs);
1702 add_target (&sol_thread_ops);
1704 procfs_suppress_run = 1;
1706 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1707 "Show info on Solaris user threads.\n", &maintenanceinfolist);
1709 memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops));
1710 memcpy (&core_ops, &sol_core_ops, sizeof (struct target_ops));
1711 add_target (&core_ops);
1713 /* Hook into new_objfile notification. */
1714 target_new_objfile_chain = target_new_objfile_hook;
1715 target_new_objfile_hook = sol_thread_new_objfile;
1720 fprintf_unfiltered (gdb_stderr, "[GDB will not be able to debug user-mode threads: %s]\n", dlerror ());
1725 /* allow the user to debug non-threaded core files */
1726 add_target (&core_ops);