1 /* Low level interface for debugging Solaris threads for GDB, the GNU debugger.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This module implements a sort of half target that sits between the
23 machine-independent parts of GDB and the /proc interface (procfs.c) to
24 provide access to the Solaris user-mode thread implementation.
26 Solaris threads are true user-mode threads, which are invoked via the thr_*
27 and pthread_* (native and Posix respectivly) interfaces. These are mostly
28 implemented in user-space, with all thread context kept in various
29 structures that live in the user's heap. These should not be confused with
30 lightweight processes (LWPs), which are implemented by the kernel, and
31 scheduled without explicit intervention by the process.
33 Just to confuse things a little, Solaris threads (both native and Posix) are
34 actually implemented using LWPs. In general, there are going to be more
35 threads than LWPs. There is no fixed correspondence between a thread and an
36 LWP. When a thread wants to run, it gets scheduled onto the first available
37 LWP and can therefore migrate from one LWP to another as time goes on. A
38 sleeping thread may not be associated with an LWP at all!
40 To make it possible to mess with threads, Sun provides a library called
41 libthread_db.so.1 (not to be confused with libthread_db.so.0, which doesn't
42 have a published interface). This interface has an upper part, which it
43 provides, and a lower part which I provide. The upper part consists of the
44 td_* routines, which allow me to find all the threads, query their state,
45 etc... The lower part consists of all of the ps_*, which are used by the
46 td_* routines to read/write memory, manipulate LWPs, lookup symbols, etc...
47 The ps_* routines actually do most of their work by calling functions in
52 #include <proc_service.h>
53 #include <thread_db.h>
54 #include "gdbthread.h"
65 extern struct target_ops sol_thread_ops; /* Forward declaration */
66 extern struct target_ops sol_core_ops; /* Forward declaration */
68 /* place to store core_ops before we overwrite it */
69 static struct target_ops orig_core_ops;
71 struct target_ops sol_thread_ops;
72 struct target_ops sol_core_ops;
74 extern int procfs_suppress_run;
75 extern struct target_ops procfs_ops; /* target vector for procfs.c */
76 extern struct target_ops core_ops; /* target vector for corelow.c */
77 extern char *procfs_pid_to_str (ptid_t ptid);
79 /* Prototypes for supply_gregset etc. */
82 /* This struct is defined by us, but mainly used for the proc_service interface.
83 We don't have much use for it, except as a handy place to get a real pid
84 for memory accesses. */
97 static struct ps_prochandle main_ph;
98 static td_thragent_t *main_ta;
99 static int sol_thread_active = 0;
101 static char *td_err_string (td_err_e errcode);
102 static char *td_state_string (td_thr_state_e statecode);
103 static ptid_t thread_to_lwp (ptid_t thread_id, int default_lwp);
104 static void sol_thread_resume (ptid_t ptid, int step, enum target_signal signo);
105 static ptid_t lwp_to_thread (ptid_t lwp);
106 static int sol_thread_alive (ptid_t ptid);
107 static void sol_core_close (int quitting);
109 static void init_sol_thread_ops (void);
110 static void init_sol_core_ops (void);
112 /* Default definitions: These must be defined in tm.h
113 if they are to be shared with a process module such as procfs. */
115 #define GET_PID(ptid) ptid_get_pid (ptid)
116 #define GET_LWP(ptid) ptid_get_lwp (ptid)
117 #define GET_THREAD(ptid) ptid_get_tid (ptid)
119 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
120 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
122 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
123 #define BUILD_THREAD(tid, pid) ptid_build (pid, 0, tid)
125 /* Pointers to routines from lithread_db resolved by dlopen() */
127 static void (*p_td_log) (const int on_off);
128 static td_err_e (*p_td_ta_new) (const struct ps_prochandle * ph_p,
129 td_thragent_t ** ta_pp);
130 static td_err_e (*p_td_ta_delete) (td_thragent_t * ta_p);
131 static td_err_e (*p_td_init) (void);
132 static td_err_e (*p_td_ta_get_ph) (const td_thragent_t * ta_p,
133 struct ps_prochandle ** ph_pp);
134 static td_err_e (*p_td_ta_get_nthreads) (const td_thragent_t * ta_p,
136 static td_err_e (*p_td_ta_tsd_iter) (const td_thragent_t * ta_p,
139 static td_err_e (*p_td_ta_thr_iter) (const td_thragent_t * ta_p,
142 td_thr_state_e state,
144 sigset_t * ti_sigmask_p,
145 unsigned ti_user_flags);
146 static td_err_e (*p_td_thr_validate) (const td_thrhandle_t * th_p);
147 static td_err_e (*p_td_thr_tsd) (const td_thrhandle_t * th_p,
148 const thread_key_t key,
150 static td_err_e (*p_td_thr_get_info) (const td_thrhandle_t * th_p,
151 td_thrinfo_t * ti_p);
152 static td_err_e (*p_td_thr_getfpregs) (const td_thrhandle_t * th_p,
153 prfpregset_t * fpregset);
154 static td_err_e (*p_td_thr_getxregsize) (const td_thrhandle_t * th_p,
156 static td_err_e (*p_td_thr_getxregs) (const td_thrhandle_t * th_p,
157 const caddr_t xregset);
158 static td_err_e (*p_td_thr_sigsetmask) (const td_thrhandle_t * th_p,
159 const sigset_t ti_sigmask);
160 static td_err_e (*p_td_thr_setprio) (const td_thrhandle_t * th_p,
162 static td_err_e (*p_td_thr_setsigpending) (const td_thrhandle_t * th_p,
163 const uchar_t ti_pending_flag,
164 const sigset_t ti_pending);
165 static td_err_e (*p_td_thr_setfpregs) (const td_thrhandle_t * th_p,
166 const prfpregset_t * fpregset);
167 static td_err_e (*p_td_thr_setxregs) (const td_thrhandle_t * th_p,
168 const caddr_t xregset);
169 static td_err_e (*p_td_ta_map_id2thr) (const td_thragent_t * ta_p,
171 td_thrhandle_t * th_p);
172 static td_err_e (*p_td_ta_map_lwp2thr) (const td_thragent_t * ta_p,
174 td_thrhandle_t * th_p);
175 static td_err_e (*p_td_thr_getgregs) (const td_thrhandle_t * th_p,
177 static td_err_e (*p_td_thr_setgregs) (const td_thrhandle_t * th_p,
178 const prgregset_t regset);
184 td_err_string - Convert a thread_db error code to a string
188 char * td_err_string (errcode)
192 Return the thread_db error string associated with errcode. If errcode
193 is unknown, then return a message.
198 td_err_string (td_err_e errcode)
200 static struct string_map
203 {TD_OK, "generic \"call succeeded\""},
204 {TD_ERR, "generic error."},
205 {TD_NOTHR, "no thread can be found to satisfy query"},
206 {TD_NOSV, "no synch. variable can be found to satisfy query"},
207 {TD_NOLWP, "no lwp can be found to satisfy query"},
208 {TD_BADPH, "invalid process handle"},
209 {TD_BADTH, "invalid thread handle"},
210 {TD_BADSH, "invalid synchronization handle"},
211 {TD_BADTA, "invalid thread agent"},
212 {TD_BADKEY, "invalid key"},
213 {TD_NOMSG, "td_thr_event_getmsg() called when there was no message"},
214 {TD_NOFPREGS, "FPU register set not available for given thread"},
215 {TD_NOLIBTHREAD, "application not linked with libthread"},
216 {TD_NOEVENT, "requested event is not supported"},
217 {TD_NOCAPAB, "capability not available"},
218 {TD_DBERR, "Debugger service failed"},
219 {TD_NOAPLIC, "Operation not applicable to"},
220 {TD_NOTSD, "No thread specific data for this thread"},
221 {TD_MALLOC, "Malloc failed"},
222 {TD_PARTIALREG, "Only part of register set was written/read"},
223 {TD_NOXREGS, "X register set not available for given thread"}
225 const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
229 for (i = 0; i < td_err_size; i++)
230 if (td_err_table[i].num == errcode)
231 return td_err_table[i].str;
233 sprintf (buf, "Unknown thread_db error code: %d", errcode);
242 td_state_string - Convert a thread_db state code to a string
246 char * td_state_string (statecode)
250 Return the thread_db state string associated with statecode. If
251 statecode is unknown, then return a message.
256 td_state_string (td_thr_state_e statecode)
258 static struct string_map
259 td_thr_state_table[] =
261 {TD_THR_ANY_STATE, "any state"},
262 {TD_THR_UNKNOWN, "unknown"},
263 {TD_THR_STOPPED, "stopped"},
265 {TD_THR_ACTIVE, "active"},
266 {TD_THR_ZOMBIE, "zombie"},
267 {TD_THR_SLEEP, "sleep"},
268 {TD_THR_STOPPED_ASLEEP, "stopped asleep"}
270 const int td_thr_state_table_size = sizeof td_thr_state_table / sizeof (struct string_map);
274 for (i = 0; i < td_thr_state_table_size; i++)
275 if (td_thr_state_table[i].num == statecode)
276 return td_thr_state_table[i].str;
278 sprintf (buf, "Unknown thread_db state code: %d", statecode);
287 thread_to_lwp - Convert a Posix or Solaris thread id to a LWP id.
291 tpid_t thread_to_lwp (thread_id, default_lwp)
295 This function converts a Posix or Solaris thread id to a lightweight
296 process id. If thread_id is non-existent, that's an error. If it's
297 an inactive thread, then we return default_lwp.
301 This function probably shouldn't call error()...
306 thread_to_lwp (ptid_t thread_id, int default_lwp)
312 if (is_lwp (thread_id))
313 return thread_id; /* It's already an LWP id */
315 /* It's a thread. Convert to lwp */
317 val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th);
319 return pid_to_ptid (-1); /* thread must have terminated */
320 else if (val != TD_OK)
321 error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val));
323 val = p_td_thr_get_info (&th, &ti);
325 return pid_to_ptid (-1); /* thread must have terminated */
326 else if (val != TD_OK)
327 error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val));
329 if (ti.ti_state != TD_THR_ACTIVE)
331 if (default_lwp != -1)
332 return pid_to_ptid (default_lwp);
333 error ("thread_to_lwp: thread state not active: %s",
334 td_state_string (ti.ti_state));
337 return BUILD_LWP (ti.ti_lid, PIDGET (thread_id));
344 lwp_to_thread - Convert a LWP id to a Posix or Solaris thread id.
348 int lwp_to_thread (lwp_id)
352 This function converts a lightweight process id to a Posix or Solaris
353 thread id. If thread_id is non-existent, that's an error.
357 This function probably shouldn't call error()...
362 lwp_to_thread (ptid_t lwp)
369 return lwp; /* It's already a thread id */
371 /* It's an lwp. Convert it to a thread id. */
373 if (!sol_thread_alive (lwp))
374 return pid_to_ptid (-1); /* defunct lwp */
376 val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th);
378 return pid_to_ptid (-1); /* thread must have terminated */
379 else if (val != TD_OK)
380 error ("lwp_to_thread: td_ta_map_lwp2thr: %s.", td_err_string (val));
382 val = p_td_thr_validate (&th);
384 return lwp; /* libthread doesn't know about it;
386 else if (val != TD_OK)
387 error ("lwp_to_thread: td_thr_validate: %s.", td_err_string (val));
389 val = p_td_thr_get_info (&th, &ti);
391 return pid_to_ptid (-1); /* thread must have terminated */
392 else if (val != TD_OK)
393 error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val));
395 return BUILD_THREAD (ti.ti_tid, PIDGET (lwp));
399 /* Most target vector functions from here on actually just pass through to
400 procfs.c, as they don't need to do anything specific for threads. */
405 sol_thread_open (char *arg, int from_tty)
407 procfs_ops.to_open (arg, from_tty);
410 /* Attach to process PID, then initialize for debugging it
411 and wait for the trace-trap that results from attaching. */
414 sol_thread_attach (char *args, int from_tty)
416 procfs_ops.to_attach (args, from_tty);
418 /* Must get symbols from solibs before libthread_db can run! */
419 SOLIB_ADD ((char *) 0, from_tty, (struct target_ops *) 0, auto_solib_add);
421 if (sol_thread_active)
423 printf_filtered ("sol-thread active.\n");
424 main_ph.ptid = inferior_ptid; /* Save for xfer_memory */
425 push_target (&sol_thread_ops);
426 inferior_ptid = lwp_to_thread (inferior_ptid);
427 if (PIDGET (inferior_ptid) == -1)
428 inferior_ptid = main_ph.ptid;
430 add_thread (inferior_ptid);
432 /* XXX - might want to iterate over all the threads and register them. */
435 /* Take a program previously attached to and detaches it.
436 The program resumes execution and will no longer stop
437 on signals, etc. We'd better not have left any breakpoints
438 in the program or it'll die when it hits one. For this
439 to work, it may be necessary for the process to have been
440 previously attached. It *might* work if the program was
441 started via the normal ptrace (PTRACE_TRACEME). */
444 sol_thread_detach (char *args, int from_tty)
446 inferior_ptid = pid_to_ptid (PIDGET (main_ph.ptid));
447 unpush_target (&sol_thread_ops);
448 procfs_ops.to_detach (args, from_tty);
451 /* Resume execution of process PID. If STEP is nozero, then
452 just single step it. If SIGNAL is nonzero, restart it with that
453 signal activated. We may have to convert pid from a thread-id to an LWP id
457 sol_thread_resume (ptid_t ptid, int step, enum target_signal signo)
459 struct cleanup *old_chain;
461 old_chain = save_inferior_ptid ();
463 inferior_ptid = thread_to_lwp (inferior_ptid, PIDGET (main_ph.ptid));
464 if (PIDGET (inferior_ptid) == -1)
465 inferior_ptid = procfs_first_available ();
467 if (PIDGET (ptid) != -1)
469 ptid_t save_ptid = ptid;
471 ptid = thread_to_lwp (ptid, -2);
472 if (PIDGET (ptid) == -2) /* Inactive thread */
473 error ("This version of Solaris can't start inactive threads.");
474 if (info_verbose && PIDGET (ptid) == -1)
475 warning ("Specified thread %ld seems to have terminated",
476 GET_THREAD (save_ptid));
479 procfs_ops.to_resume (ptid, step, signo);
481 do_cleanups (old_chain);
484 /* Wait for any threads to stop. We may have to convert PID from a thread id
485 to a LWP id, and vice versa on the way out. */
488 sol_thread_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
492 struct cleanup *old_chain;
494 save_ptid = inferior_ptid;
495 old_chain = save_inferior_ptid ();
497 inferior_ptid = thread_to_lwp (inferior_ptid, PIDGET (main_ph.ptid));
498 if (PIDGET (inferior_ptid) == -1)
499 inferior_ptid = procfs_first_available ();
501 if (PIDGET (ptid) != -1)
503 ptid_t save_ptid = ptid;
505 ptid = thread_to_lwp (ptid, -2);
506 if (PIDGET (ptid) == -2) /* Inactive thread */
507 error ("This version of Solaris can't start inactive threads.");
508 if (info_verbose && PIDGET (ptid) == -1)
509 warning ("Specified thread %ld seems to have terminated",
510 GET_THREAD (save_ptid));
513 rtnval = procfs_ops.to_wait (ptid, ourstatus);
515 if (ourstatus->kind != TARGET_WAITKIND_EXITED)
517 /* Map the LWP of interest back to the appropriate thread ID */
518 rtnval = lwp_to_thread (rtnval);
519 if (PIDGET (rtnval) == -1)
522 /* See if we have a new thread */
523 if (is_thread (rtnval)
524 && !ptid_equal (rtnval, save_ptid)
525 && !in_thread_list (rtnval))
527 printf_filtered ("[New %s]\n", target_pid_to_str (rtnval));
532 /* During process initialization, we may get here without the thread package
533 being initialized, since that can only happen after we've found the shared
536 do_cleanups (old_chain);
542 sol_thread_fetch_registers (int regno)
545 td_thrhandle_t thandle;
548 prfpregset_t fpregset;
554 if (!is_thread (inferior_ptid))
555 { /* LWP: pass the request on to procfs.c */
556 if (target_has_execution)
557 procfs_ops.to_fetch_registers (regno);
559 orig_core_ops.to_fetch_registers (regno);
563 /* Solaris thread: convert inferior_ptid into a td_thrhandle_t */
565 thread = GET_THREAD (inferior_ptid);
568 error ("sol_thread_fetch_registers: thread == 0");
570 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
572 error ("sol_thread_fetch_registers: td_ta_map_id2thr: %s",
573 td_err_string (val));
575 /* Get the integer regs */
577 val = p_td_thr_getgregs (&thandle, gregset);
579 && val != TD_PARTIALREG)
580 error ("sol_thread_fetch_registers: td_thr_getgregs %s",
581 td_err_string (val));
583 /* For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, pc and sp
584 are saved (by a thread context switch). */
586 /* And, now the fp regs */
588 val = p_td_thr_getfpregs (&thandle, &fpregset);
590 && val != TD_NOFPREGS)
591 error ("sol_thread_fetch_registers: td_thr_getfpregs %s",
592 td_err_string (val));
594 /* Note that we must call supply_{g fp}regset *after* calling the td routines
595 because the td routines call ps_lget* which affect the values stored in the
598 supply_gregset ((gdb_gregset_t *) &gregset);
599 supply_fpregset ((gdb_fpregset_t *) &fpregset);
602 /* thread_db doesn't seem to handle this right */
603 val = td_thr_getxregsize (&thandle, &xregsize);
604 if (val != TD_OK && val != TD_NOXREGS)
605 error ("sol_thread_fetch_registers: td_thr_getxregsize %s",
606 td_err_string (val));
610 xregset = alloca (xregsize);
611 val = td_thr_getxregs (&thandle, xregset);
613 error ("sol_thread_fetch_registers: td_thr_getxregs %s",
614 td_err_string (val));
620 sol_thread_store_registers (int regno)
623 td_thrhandle_t thandle;
626 prfpregset_t fpregset;
632 if (!is_thread (inferior_ptid))
633 { /* LWP: pass the request on to procfs.c */
634 procfs_ops.to_store_registers (regno);
638 /* Solaris thread: convert inferior_ptid into a td_thrhandle_t */
640 thread = GET_THREAD (inferior_ptid);
642 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
644 error ("sol_thread_store_registers: td_ta_map_id2thr %s",
645 td_err_string (val));
648 { /* Not writing all the regs */
649 /* save new register value */
650 char* old_value = (char*) alloca (REGISTER_SIZE);
651 memcpy (old_value, &deprecated_registers[REGISTER_BYTE (regno)],
654 val = p_td_thr_getgregs (&thandle, gregset);
656 error ("sol_thread_store_registers: td_thr_getgregs %s",
657 td_err_string (val));
658 val = p_td_thr_getfpregs (&thandle, &fpregset);
660 error ("sol_thread_store_registers: td_thr_getfpregs %s",
661 td_err_string (val));
663 /* restore new register value */
664 memcpy (&deprecated_registers[REGISTER_BYTE (regno)], old_value,
668 /* thread_db doesn't seem to handle this right */
669 val = td_thr_getxregsize (&thandle, &xregsize);
670 if (val != TD_OK && val != TD_NOXREGS)
671 error ("sol_thread_store_registers: td_thr_getxregsize %s",
672 td_err_string (val));
676 xregset = alloca (xregsize);
677 val = td_thr_getxregs (&thandle, xregset);
679 error ("sol_thread_store_registers: td_thr_getxregs %s",
680 td_err_string (val));
685 fill_gregset ((gdb_gregset_t *) &gregset, regno);
686 fill_fpregset ((gdb_fpregset_t *) &fpregset, regno);
688 val = p_td_thr_setgregs (&thandle, gregset);
690 error ("sol_thread_store_registers: td_thr_setgregs %s",
691 td_err_string (val));
692 val = p_td_thr_setfpregs (&thandle, &fpregset);
694 error ("sol_thread_store_registers: td_thr_setfpregs %s",
695 td_err_string (val));
698 /* thread_db doesn't seem to handle this right */
699 val = td_thr_getxregsize (&thandle, &xregsize);
700 if (val != TD_OK && val != TD_NOXREGS)
701 error ("sol_thread_store_registers: td_thr_getxregsize %s",
702 td_err_string (val));
704 /* Should probably do something about writing the xregs here, but what are
709 /* Get ready to modify the registers array. On machines which store
710 individual registers, this doesn't need to do anything. On machines
711 which store all the registers in one fell swoop, this makes sure
712 that registers contains all the registers from the program being
716 sol_thread_prepare_to_store (void)
718 procfs_ops.to_prepare_to_store ();
721 /* Transfer LEN bytes between GDB address MYADDR and target address
722 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
723 otherwise transfer them from the target. TARGET is unused.
725 Returns the number of bytes transferred. */
728 sol_thread_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
729 struct mem_attrib *attrib,
730 struct target_ops *target)
733 struct cleanup *old_chain;
735 old_chain = save_inferior_ptid ();
737 if (is_thread (inferior_ptid) || /* A thread */
738 !target_thread_alive (inferior_ptid)) /* An lwp, but not alive */
739 inferior_ptid = procfs_first_available (); /* Find any live lwp. */
740 /* Note: don't need to call switch_to_thread; we're just reading memory. */
742 if (target_has_execution)
743 retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len,
744 dowrite, attrib, target);
746 retval = orig_core_ops.to_xfer_memory (memaddr, myaddr, len,
747 dowrite, attrib, target);
749 do_cleanups (old_chain);
754 /* Print status information about what we're accessing. */
757 sol_thread_files_info (struct target_ops *ignore)
759 procfs_ops.to_files_info (ignore);
763 sol_thread_kill_inferior (void)
765 procfs_ops.to_kill ();
769 sol_thread_notice_signals (ptid_t ptid)
771 procfs_ops.to_notice_signals (pid_to_ptid (PIDGET (ptid)));
774 /* Fork an inferior process, and start debugging it with /proc. */
777 sol_thread_create_inferior (char *exec_file, char *allargs, char **env)
779 procfs_ops.to_create_inferior (exec_file, allargs, env);
781 if (sol_thread_active && !ptid_equal (inferior_ptid, null_ptid))
783 main_ph.ptid = inferior_ptid; /* Save for xfer_memory */
785 push_target (&sol_thread_ops);
787 inferior_ptid = lwp_to_thread (inferior_ptid);
788 if (PIDGET (inferior_ptid) == -1)
789 inferior_ptid = main_ph.ptid;
791 if (!in_thread_list (inferior_ptid))
792 add_thread (inferior_ptid);
796 /* This routine is called whenever a new symbol table is read in, or when all
797 symbol tables are removed. libthread_db can only be initialized when it
798 finds the right variables in libthread.so. Since it's a shared library,
799 those variables don't show up until the library gets mapped and the symbol
802 /* This new_objfile event is now managed by a chained function pointer.
803 * It is the callee's responsability to call the next client on the chain.
806 /* Saved pointer to previous owner of the new_objfile event. */
807 static void (*target_new_objfile_chain) (struct objfile *);
810 sol_thread_new_objfile (struct objfile *objfile)
816 sol_thread_active = 0;
820 /* don't do anything if init failed to resolve the libthread_db library */
821 if (!procfs_suppress_run)
824 /* Now, initialize the thread debugging library. This needs to be done after
825 the shared libraries are located because it needs information from the
826 user's thread library. */
831 warning ("sol_thread_new_objfile: td_init: %s", td_err_string (val));
835 val = p_td_ta_new (&main_ph, &main_ta);
836 if (val == TD_NOLIBTHREAD)
838 else if (val != TD_OK)
840 warning ("sol_thread_new_objfile: td_ta_new: %s", td_err_string (val));
844 sol_thread_active = 1;
846 /* Call predecessor on chain, if any. */
847 if (target_new_objfile_chain)
848 target_new_objfile_chain (objfile);
851 /* Clean up after the inferior dies. */
854 sol_thread_mourn_inferior (void)
856 unpush_target (&sol_thread_ops);
857 procfs_ops.to_mourn_inferior ();
860 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
863 sol_thread_can_run (void)
865 return procfs_suppress_run;
872 sol_thread_alive - test thread for "aliveness"
876 static bool sol_thread_alive (ptid_t ptid);
880 returns true if thread still active in inferior.
885 sol_thread_alive (ptid_t ptid)
887 if (is_thread (ptid)) /* non-kernel thread */
893 pid = GET_THREAD (ptid);
894 if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK)
895 return 0; /* thread not found */
896 if ((val = p_td_thr_validate (&th)) != TD_OK)
897 return 0; /* thread not valid */
898 return 1; /* known thread: return true */
901 /* kernel thread (LWP): let procfs test it */
903 if (target_has_execution)
904 return procfs_ops.to_thread_alive (ptid);
906 return orig_core_ops.to_thread_alive (ptid);
911 sol_thread_stop (void)
913 procfs_ops.to_stop ();
916 /* These routines implement the lower half of the thread_db interface. Ie: the
919 /* Various versions of <proc_service.h> have slightly
920 different function prototypes. In particular, we have
923 struct ps_prochandle * const struct ps_prochandle *
928 Which one you have depends on solaris version and what
929 patches you've applied. On the theory that there are
930 only two major variants, we have configure check the
931 prototype of ps_pdwrite (), and use that info to make
932 appropriate typedefs here. */
934 #ifdef PROC_SERVICE_IS_OLD
935 typedef const struct ps_prochandle *gdb_ps_prochandle_t;
936 typedef char *gdb_ps_read_buf_t;
937 typedef char *gdb_ps_write_buf_t;
938 typedef int gdb_ps_size_t;
939 typedef paddr_t gdb_ps_addr_t;
941 typedef struct ps_prochandle *gdb_ps_prochandle_t;
942 typedef void *gdb_ps_read_buf_t;
943 typedef const void *gdb_ps_write_buf_t;
944 typedef size_t gdb_ps_size_t;
945 typedef psaddr_t gdb_ps_addr_t;
949 /* The next four routines are called by thread_db to tell us to stop and stop
950 a particular process or lwp. Since GDB ensures that these are all stopped
951 by the time we call anything in thread_db, these routines need to do
957 ps_pstop (gdb_ps_prochandle_t ph)
962 /* Process continue */
965 ps_pcontinue (gdb_ps_prochandle_t ph)
973 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
981 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
986 /* Looks up the symbol LD_SYMBOL_NAME in the debugger's symbol table. */
989 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *ld_object_name,
990 const char *ld_symbol_name, gdb_ps_addr_t * ld_symbol_addr)
992 struct minimal_symbol *ms;
994 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
999 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
1004 /* Common routine for reading and writing memory. */
1007 rw_common (int dowrite, const struct ps_prochandle *ph, gdb_ps_addr_t addr,
1008 char *buf, int size)
1010 struct cleanup *old_chain;
1012 old_chain = save_inferior_ptid ();
1014 if (is_thread (inferior_ptid) || /* A thread */
1015 !target_thread_alive (inferior_ptid)) /* An lwp, but not alive */
1016 inferior_ptid = procfs_first_available (); /* Find any live lwp. */
1017 /* Note: don't need to call switch_to_thread; we're just reading memory. */
1019 #if defined (__sparcv9)
1020 /* For Sparc64 cross Sparc32, make sure the address has not been
1021 accidentally sign-extended (or whatever) to beyond 32 bits. */
1022 if (bfd_get_arch_size (exec_bfd) == 32)
1030 /* FIXME: passing 0 as attrib argument. */
1031 if (target_has_execution)
1032 cc = procfs_ops.to_xfer_memory (addr, buf, size,
1033 dowrite, 0, &procfs_ops);
1035 cc = orig_core_ops.to_xfer_memory (addr, buf, size,
1036 dowrite, 0, &core_ops);
1041 print_sys_errmsg ("rw_common (): read", errno);
1043 print_sys_errmsg ("rw_common (): write", errno);
1045 do_cleanups (old_chain);
1052 warning ("rw_common (): unable to read at addr 0x%lx",
1055 warning ("rw_common (): unable to write at addr 0x%lx",
1058 do_cleanups (old_chain);
1067 do_cleanups (old_chain);
1072 /* Copies SIZE bytes from target process .data segment to debugger memory. */
1075 ps_pdread (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1076 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1078 return rw_common (0, ph, addr, buf, size);
1081 /* Copies SIZE bytes from debugger memory .data segment to target process. */
1084 ps_pdwrite (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1085 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1087 return rw_common (1, ph, addr, (char *) buf, size);
1090 /* Copies SIZE bytes from target process .text segment to debugger memory. */
1093 ps_ptread (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1094 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1096 return rw_common (0, ph, addr, buf, size);
1099 /* Copies SIZE bytes from debugger memory .text segment to target process. */
1102 ps_ptwrite (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
1103 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1105 return rw_common (1, ph, addr, (char *) buf, size);
1108 /* Get integer regs for LWP */
1111 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1112 prgregset_t gregset)
1114 struct cleanup *old_chain;
1116 old_chain = save_inferior_ptid ();
1118 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1120 if (target_has_execution)
1121 procfs_ops.to_fetch_registers (-1);
1123 orig_core_ops.to_fetch_registers (-1);
1124 fill_gregset ((gdb_gregset_t *) gregset, -1);
1126 do_cleanups (old_chain);
1131 /* Set integer regs for LWP */
1134 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1135 const prgregset_t gregset)
1137 struct cleanup *old_chain;
1139 old_chain = save_inferior_ptid ();
1141 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1143 supply_gregset ((gdb_gregset_t *) gregset);
1144 if (target_has_execution)
1145 procfs_ops.to_store_registers (-1);
1147 orig_core_ops.to_store_registers (-1);
1149 do_cleanups (old_chain);
1154 /* Log a message (sends to gdb_stderr). */
1157 ps_plog (const char *fmt,...)
1161 va_start (args, fmt);
1163 vfprintf_filtered (gdb_stderr, fmt, args);
1166 /* Get size of extra register set. Currently a noop. */
1169 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
1176 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1180 if (ioctl (lwp_fd, PIOCGXREGSIZE, ®size))
1182 if (errno == EINVAL)
1183 return PS_NOFREGS; /* XXX Wrong code, but this is the closest
1184 thing in proc_service.h */
1186 print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno);
1194 /* Get extra register set. Currently a noop. */
1197 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1203 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1207 if (ioctl (lwp_fd, PIOCGXREG, xregset))
1209 print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno);
1217 /* Set extra register set. Currently a noop. */
1220 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1226 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1230 if (ioctl (lwp_fd, PIOCSXREG, xregset))
1232 print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno);
1240 /* Get floating-point regs for LWP */
1243 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1244 prfpregset_t * fpregset)
1246 struct cleanup *old_chain;
1248 old_chain = save_inferior_ptid ();
1250 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1252 if (target_has_execution)
1253 procfs_ops.to_fetch_registers (-1);
1255 orig_core_ops.to_fetch_registers (-1);
1256 fill_fpregset ((gdb_fpregset_t *) fpregset, -1);
1258 do_cleanups (old_chain);
1263 /* Set floating-point regs for LWP */
1266 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1267 const prfpregset_t * fpregset)
1269 struct cleanup *old_chain;
1271 old_chain = save_inferior_ptid ();
1273 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1275 supply_fpregset ((gdb_fpregset_t *) fpregset);
1276 if (target_has_execution)
1277 procfs_ops.to_store_registers (-1);
1279 orig_core_ops.to_store_registers (-1);
1281 do_cleanups (old_chain);
1286 #ifdef PR_MODEL_LP64
1287 /* Identify process as 32-bit or 64-bit.
1288 At the moment I'm using bfd to do this.
1289 There might be a more solaris-specific (eg. procfs) method,
1290 but this ought to work. */
1293 ps_pdmodel (gdb_ps_prochandle_t ph, int *data_model)
1296 *data_model = PR_MODEL_UNKNOWN;
1297 else if (bfd_get_arch_size (exec_bfd) == 32)
1298 *data_model = PR_MODEL_ILP32;
1300 *data_model = PR_MODEL_LP64;
1304 #endif /* PR_MODEL_LP64 */
1306 #ifdef TM_I386SOL2_H
1308 /* Reads the local descriptor table of a LWP. */
1311 ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1314 /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */
1315 extern struct ssd *procfs_find_LDT_entry (ptid_t);
1318 /* FIXME: can't I get the process ID from the prochandle or something?
1321 if (PIDGET (inferior_ptid) <= 0 || lwpid <= 0)
1324 ret = procfs_find_LDT_entry (BUILD_LWP (lwpid, PIDGET (inferior_ptid)));
1327 memcpy (pldt, ret, sizeof (struct ssd));
1330 else /* LDT not found. */
1333 #endif /* TM_I386SOL2_H */
1335 /* Convert a pid to printable form. */
1338 solaris_pid_to_str (ptid_t ptid)
1340 static char buf[100];
1342 /* in case init failed to resolve the libthread_db library */
1343 if (!procfs_suppress_run)
1344 return procfs_pid_to_str (ptid);
1346 if (is_thread (ptid))
1350 lwp = thread_to_lwp (ptid, -2);
1352 if (PIDGET (lwp) == -1)
1353 sprintf (buf, "Thread %ld (defunct)", GET_THREAD (ptid));
1354 else if (PIDGET (lwp) != -2)
1355 sprintf (buf, "Thread %ld (LWP %ld)", GET_THREAD (ptid), GET_LWP (lwp));
1357 sprintf (buf, "Thread %ld ", GET_THREAD (ptid));
1359 else if (GET_LWP (ptid) != 0)
1360 sprintf (buf, "LWP %ld ", GET_LWP (ptid));
1362 sprintf (buf, "process %d ", PIDGET (ptid));
1368 /* Worker bee for find_new_threads
1369 Callback function that gets called once per USER thread (i.e., not
1373 sol_find_new_threads_callback (const td_thrhandle_t *th, void *ignored)
1379 if ((retval = p_td_thr_get_info (th, &ti)) != TD_OK)
1383 ptid = BUILD_THREAD (ti.ti_tid, PIDGET (inferior_ptid));
1384 if (!in_thread_list (ptid))
1391 sol_find_new_threads (void)
1393 /* don't do anything if init failed to resolve the libthread_db library */
1394 if (!procfs_suppress_run)
1397 if (PIDGET (inferior_ptid) == -1)
1399 printf_filtered ("No process.\n");
1402 procfs_ops.to_find_new_threads (); /* first find new kernel threads */
1403 p_td_ta_thr_iter (main_ta, sol_find_new_threads_callback, (void *) 0,
1404 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1405 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1409 sol_core_open (char *filename, int from_tty)
1411 orig_core_ops.to_open (filename, from_tty);
1415 sol_core_close (int quitting)
1417 orig_core_ops.to_close (quitting);
1421 sol_core_detach (char *args, int from_tty)
1423 unpush_target (&core_ops);
1424 orig_core_ops.to_detach (args, from_tty);
1428 sol_core_files_info (struct target_ops *t)
1430 orig_core_ops.to_files_info (t);
1433 /* Worker bee for info sol-thread command. This is a callback function that
1434 gets called once for each Solaris thread (ie. not kernel thread) in the
1435 inferior. Print anything interesting that we can think of. */
1438 info_cb (const td_thrhandle_t *th, void *s)
1443 if ((ret = p_td_thr_get_info (th, &ti)) == TD_OK)
1445 printf_filtered ("%s thread #%d, lwp %d, ",
1446 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
1447 ti.ti_tid, ti.ti_lid);
1448 switch (ti.ti_state)
1451 case TD_THR_UNKNOWN:
1452 printf_filtered ("<unknown state>");
1454 case TD_THR_STOPPED:
1455 printf_filtered ("(stopped)");
1458 printf_filtered ("(run) ");
1461 printf_filtered ("(active) ");
1464 printf_filtered ("(zombie) ");
1467 printf_filtered ("(asleep) ");
1469 case TD_THR_STOPPED_ASLEEP:
1470 printf_filtered ("(stopped asleep)");
1473 /* Print thr_create start function: */
1474 if (ti.ti_startfunc != 0)
1476 struct minimal_symbol *msym;
1477 msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc);
1479 printf_filtered (" startfunc: %s\n", SYMBOL_NAME (msym));
1481 printf_filtered (" startfunc: 0x%s\n", paddr (ti.ti_startfunc));
1484 /* If thread is asleep, print function that went to sleep: */
1485 if (ti.ti_state == TD_THR_SLEEP)
1487 struct minimal_symbol *msym;
1488 msym = lookup_minimal_symbol_by_pc (ti.ti_pc);
1490 printf_filtered (" - Sleep func: %s\n", SYMBOL_NAME (msym));
1492 printf_filtered (" - Sleep func: 0x%s\n", paddr (ti.ti_startfunc));
1495 /* Wrap up line, if necessary */
1496 if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0)
1497 printf_filtered ("\n"); /* don't you hate counting newlines? */
1500 warning ("info sol-thread: failed to get info for thread.");
1505 /* List some state about each Solaris user thread in the inferior. */
1508 info_solthreads (char *args, int from_tty)
1510 p_td_ta_thr_iter (main_ta, info_cb, args,
1511 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1512 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1516 sol_find_memory_regions (int (*func) (CORE_ADDR,
1522 return procfs_ops.to_find_memory_regions (func, data);
1526 sol_make_note_section (bfd *obfd, int *note_size)
1528 return procfs_ops.to_make_corefile_notes (obfd, note_size);
1532 ignore (CORE_ADDR addr, char *contents)
1539 init_sol_thread_ops (void)
1541 sol_thread_ops.to_shortname = "solaris-threads";
1542 sol_thread_ops.to_longname = "Solaris threads and pthread.";
1543 sol_thread_ops.to_doc = "Solaris threads and pthread support.";
1544 sol_thread_ops.to_open = sol_thread_open;
1545 sol_thread_ops.to_attach = sol_thread_attach;
1546 sol_thread_ops.to_detach = sol_thread_detach;
1547 sol_thread_ops.to_resume = sol_thread_resume;
1548 sol_thread_ops.to_wait = sol_thread_wait;
1549 sol_thread_ops.to_fetch_registers = sol_thread_fetch_registers;
1550 sol_thread_ops.to_store_registers = sol_thread_store_registers;
1551 sol_thread_ops.to_prepare_to_store = sol_thread_prepare_to_store;
1552 sol_thread_ops.to_xfer_memory = sol_thread_xfer_memory;
1553 sol_thread_ops.to_files_info = sol_thread_files_info;
1554 sol_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1555 sol_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1556 sol_thread_ops.to_terminal_init = terminal_init_inferior;
1557 sol_thread_ops.to_terminal_inferior = terminal_inferior;
1558 sol_thread_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1559 sol_thread_ops.to_terminal_ours = terminal_ours;
1560 sol_thread_ops.to_terminal_save_ours = terminal_save_ours;
1561 sol_thread_ops.to_terminal_info = child_terminal_info;
1562 sol_thread_ops.to_kill = sol_thread_kill_inferior;
1563 sol_thread_ops.to_create_inferior = sol_thread_create_inferior;
1564 sol_thread_ops.to_mourn_inferior = sol_thread_mourn_inferior;
1565 sol_thread_ops.to_can_run = sol_thread_can_run;
1566 sol_thread_ops.to_notice_signals = sol_thread_notice_signals;
1567 sol_thread_ops.to_thread_alive = sol_thread_alive;
1568 sol_thread_ops.to_pid_to_str = solaris_pid_to_str;
1569 sol_thread_ops.to_find_new_threads = sol_find_new_threads;
1570 sol_thread_ops.to_stop = sol_thread_stop;
1571 sol_thread_ops.to_stratum = process_stratum;
1572 sol_thread_ops.to_has_all_memory = 1;
1573 sol_thread_ops.to_has_memory = 1;
1574 sol_thread_ops.to_has_stack = 1;
1575 sol_thread_ops.to_has_registers = 1;
1576 sol_thread_ops.to_has_execution = 1;
1577 sol_thread_ops.to_has_thread_control = tc_none;
1578 sol_thread_ops.to_find_memory_regions = sol_find_memory_regions;
1579 sol_thread_ops.to_make_corefile_notes = sol_make_note_section;
1580 sol_thread_ops.to_magic = OPS_MAGIC;
1585 init_sol_core_ops (void)
1587 sol_core_ops.to_shortname = "solaris-core";
1588 sol_core_ops.to_longname = "Solaris core threads and pthread.";
1589 sol_core_ops.to_doc = "Solaris threads and pthread support for core files.";
1590 sol_core_ops.to_open = sol_core_open;
1591 sol_core_ops.to_close = sol_core_close;
1592 sol_core_ops.to_attach = sol_thread_attach;
1593 sol_core_ops.to_detach = sol_core_detach;
1594 sol_core_ops.to_fetch_registers = sol_thread_fetch_registers;
1595 sol_core_ops.to_xfer_memory = sol_thread_xfer_memory;
1596 sol_core_ops.to_files_info = sol_core_files_info;
1597 sol_core_ops.to_insert_breakpoint = ignore;
1598 sol_core_ops.to_remove_breakpoint = ignore;
1599 sol_core_ops.to_create_inferior = sol_thread_create_inferior;
1600 sol_core_ops.to_stratum = core_stratum;
1601 sol_core_ops.to_has_memory = 1;
1602 sol_core_ops.to_has_stack = 1;
1603 sol_core_ops.to_has_registers = 1;
1604 sol_core_ops.to_has_thread_control = tc_none;
1605 sol_core_ops.to_thread_alive = sol_thread_alive;
1606 sol_core_ops.to_pid_to_str = solaris_pid_to_str;
1607 /* On Solaris/x86, when debugging a threaded core file from process <n>,
1608 the following causes "info threads" to produce "procfs: couldn't find pid
1609 <n> in procinfo list" where <n> is the pid of the process that produced
1610 the core file. Disable it for now. */
1611 /* sol_core_ops.to_find_new_threads = sol_find_new_threads; */
1612 sol_core_ops.to_magic = OPS_MAGIC;
1615 /* we suppress the call to add_target of core_ops in corelow because
1616 if there are two targets in the stratum core_stratum, find_core_target
1617 won't know which one to return. see corelow.c for an additonal
1618 comment on coreops_suppress_target. */
1619 int coreops_suppress_target = 1;
1622 _initialize_sol_thread (void)
1626 init_sol_thread_ops ();
1627 init_sol_core_ops ();
1629 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1633 #define resolve(X) \
1634 if (!(p_##X = dlsym (dlhandle, #X))) \
1638 resolve (td_ta_new);
1639 resolve (td_ta_delete);
1641 resolve (td_ta_get_ph);
1642 resolve (td_ta_get_nthreads);
1643 resolve (td_ta_tsd_iter);
1644 resolve (td_ta_thr_iter);
1645 resolve (td_thr_validate);
1646 resolve (td_thr_tsd);
1647 resolve (td_thr_get_info);
1648 resolve (td_thr_getfpregs);
1649 resolve (td_thr_getxregsize);
1650 resolve (td_thr_getxregs);
1651 resolve (td_thr_sigsetmask);
1652 resolve (td_thr_setprio);
1653 resolve (td_thr_setsigpending);
1654 resolve (td_thr_setfpregs);
1655 resolve (td_thr_setxregs);
1656 resolve (td_ta_map_id2thr);
1657 resolve (td_ta_map_lwp2thr);
1658 resolve (td_thr_getgregs);
1659 resolve (td_thr_setgregs);
1661 add_target (&sol_thread_ops);
1663 procfs_suppress_run = 1;
1665 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1666 "Show info on Solaris user threads.\n", &maintenanceinfolist);
1668 memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops));
1669 memcpy (&core_ops, &sol_core_ops, sizeof (struct target_ops));
1670 add_target (&core_ops);
1672 /* Hook into new_objfile notification. */
1673 target_new_objfile_chain = target_new_objfile_hook;
1674 target_new_objfile_hook = sol_thread_new_objfile;
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);