1 /* Solaris threads debugging interface.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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)
23 to provide access to the Solaris user-mode thread implementation.
25 Solaris threads are true user-mode threads, which are invoked via
26 the thr_* and pthread_* (native and POSIX respectivly) interfaces.
27 These are mostly implemented in user-space, with all thread context
28 kept in various structures that live in the user's heap. These
29 should not be confused with lightweight processes (LWPs), which are
30 implemented by the kernel, and scheduled without explicit
31 intervention by the process.
33 Just to confuse things a little, Solaris threads (both native and
34 POSIX) are actually implemented using LWPs. In general, there are
35 going to be more threads than LWPs. There is no fixed
36 correspondence between a thread and an LWP. When a thread wants to
37 run, it gets scheduled onto the first available LWP and can
38 therefore migrate from one LWP to another as time goes on. A
39 sleeping thread may not be associated with an LWP at all!
41 To make it possible to mess with threads, Sun provides a library
42 called libthread_db.so.1 (not to be confused with
43 libthread_db.so.0, which doesn't have a published interface). This
44 interface has an upper part, which it provides, and a lower part
45 which we provide. The upper part consists of the td_* routines,
46 which allow us to find all the threads, query their state, etc...
47 The lower part consists of all of the ps_*, which are used by the
48 td_* routines to read/write memory, manipulate LWPs, lookup
49 symbols, etc... The ps_* routines actually do most of their work
50 by calling functions in procfs.c. */
54 #include <proc_service.h>
55 #include <thread_db.h>
56 #include "gdbthread.h"
68 #include "gdb_string.h"
69 #include <sys/procfs.h>
70 #include "proc-utils.h"
72 struct target_ops sol_thread_ops;
74 extern char *procfs_pid_to_str (struct target_ops *ops, ptid_t ptid);
76 /* Prototypes for supply_gregset etc. */
79 /* This struct is defined by us, but mainly used for the proc_service
80 interface. We don't have much use for it, except as a handy place
81 to get a real PID for memory accesses. */
94 static struct ps_prochandle main_ph;
95 static td_thragent_t *main_ta;
96 static int sol_thread_active = 0;
98 static void init_sol_thread_ops (void);
100 /* Default definitions: These must be defined in tm.h if they are to
101 be shared with a process module such as procfs. */
103 #define GET_PID(ptid) ptid_get_pid (ptid)
104 #define GET_LWP(ptid) ptid_get_lwp (ptid)
105 #define GET_THREAD(ptid) ptid_get_tid (ptid)
107 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
108 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
110 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
111 #define BUILD_THREAD(tid, pid) ptid_build (pid, 0, tid)
113 /* Pointers to routines from libthread_db resolved by dlopen(). */
115 static void (*p_td_log)(const int on_off);
116 static td_err_e (*p_td_ta_new)(const struct ps_prochandle *ph_p,
117 td_thragent_t **ta_pp);
118 static td_err_e (*p_td_ta_delete)(td_thragent_t *ta_p);
119 static td_err_e (*p_td_init)(void);
120 static td_err_e (*p_td_ta_get_ph)(const td_thragent_t *ta_p,
121 struct ps_prochandle **ph_pp);
122 static td_err_e (*p_td_ta_get_nthreads)(const td_thragent_t *ta_p,
124 static td_err_e (*p_td_ta_tsd_iter)(const td_thragent_t *ta_p,
125 td_key_iter_f *cb, void *cbdata_p);
126 static td_err_e (*p_td_ta_thr_iter)(const td_thragent_t *ta_p,
127 td_thr_iter_f *cb, void *cbdata_p,
128 td_thr_state_e state, int ti_pri,
129 sigset_t *ti_sigmask_p,
130 unsigned ti_user_flags);
131 static td_err_e (*p_td_thr_validate)(const td_thrhandle_t *th_p);
132 static td_err_e (*p_td_thr_tsd)(const td_thrhandle_t * th_p,
133 const thread_key_t key, void **data_pp);
134 static td_err_e (*p_td_thr_get_info)(const td_thrhandle_t *th_p,
136 static td_err_e (*p_td_thr_getfpregs)(const td_thrhandle_t *th_p,
137 prfpregset_t *fpregset);
138 static td_err_e (*p_td_thr_getxregsize)(const td_thrhandle_t *th_p,
140 static td_err_e (*p_td_thr_getxregs)(const td_thrhandle_t *th_p,
141 const caddr_t xregset);
142 static td_err_e (*p_td_thr_sigsetmask)(const td_thrhandle_t *th_p,
143 const sigset_t ti_sigmask);
144 static td_err_e (*p_td_thr_setprio)(const td_thrhandle_t *th_p,
146 static td_err_e (*p_td_thr_setsigpending)(const td_thrhandle_t *th_p,
147 const uchar_t ti_pending_flag,
148 const sigset_t ti_pending);
149 static td_err_e (*p_td_thr_setfpregs)(const td_thrhandle_t *th_p,
150 const prfpregset_t *fpregset);
151 static td_err_e (*p_td_thr_setxregs)(const td_thrhandle_t *th_p,
152 const caddr_t xregset);
153 static td_err_e (*p_td_ta_map_id2thr)(const td_thragent_t *ta_p,
155 td_thrhandle_t *th_p);
156 static td_err_e (*p_td_ta_map_lwp2thr)(const td_thragent_t *ta_p,
158 td_thrhandle_t *th_p);
159 static td_err_e (*p_td_thr_getgregs)(const td_thrhandle_t *th_p,
161 static td_err_e (*p_td_thr_setgregs)(const td_thrhandle_t *th_p,
162 const prgregset_t regset);
165 /* Return the libthread_db error string associated with ERRCODE. If
166 ERRCODE is unknown, return an appropriate message. */
169 td_err_string (td_err_e errcode)
171 static struct string_map td_err_table[] =
173 { TD_OK, "generic \"call succeeded\"" },
174 { TD_ERR, "generic error." },
175 { TD_NOTHR, "no thread can be found to satisfy query" },
176 { TD_NOSV, "no synch. variable can be found to satisfy query" },
177 { TD_NOLWP, "no lwp can be found to satisfy query" },
178 { TD_BADPH, "invalid process handle" },
179 { TD_BADTH, "invalid thread handle" },
180 { TD_BADSH, "invalid synchronization handle" },
181 { TD_BADTA, "invalid thread agent" },
182 { TD_BADKEY, "invalid key" },
183 { TD_NOMSG, "td_thr_event_getmsg() called when there was no message" },
184 { TD_NOFPREGS, "FPU register set not available for given thread" },
185 { TD_NOLIBTHREAD, "application not linked with libthread" },
186 { TD_NOEVENT, "requested event is not supported" },
187 { TD_NOCAPAB, "capability not available" },
188 { TD_DBERR, "Debugger service failed" },
189 { TD_NOAPLIC, "Operation not applicable to" },
190 { TD_NOTSD, "No thread specific data for this thread" },
191 { TD_MALLOC, "Malloc failed" },
192 { TD_PARTIALREG, "Only part of register set was written/read" },
193 { TD_NOXREGS, "X register set not available for given thread" }
195 const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
199 for (i = 0; i < td_err_size; i++)
200 if (td_err_table[i].num == errcode)
201 return td_err_table[i].str;
203 sprintf (buf, "Unknown libthread_db error code: %d", errcode);
208 /* Return the the libthread_db state string assicoated with STATECODE.
209 If STATECODE is unknown, return an appropriate message. */
212 td_state_string (td_thr_state_e statecode)
214 static struct string_map td_thr_state_table[] =
216 { TD_THR_ANY_STATE, "any state" },
217 { TD_THR_UNKNOWN, "unknown" },
218 { TD_THR_STOPPED, "stopped" },
219 { TD_THR_RUN, "run" },
220 { TD_THR_ACTIVE, "active" },
221 { TD_THR_ZOMBIE, "zombie" },
222 { TD_THR_SLEEP, "sleep" },
223 { TD_THR_STOPPED_ASLEEP, "stopped asleep" }
225 const int td_thr_state_table_size =
226 sizeof td_thr_state_table / sizeof (struct string_map);
230 for (i = 0; i < td_thr_state_table_size; i++)
231 if (td_thr_state_table[i].num == statecode)
232 return td_thr_state_table[i].str;
234 sprintf (buf, "Unknown libthread_db state code: %d", statecode);
240 /* Convert a POSIX or Solaris thread ID into a LWP ID. If THREAD_ID
241 doesn't exist, that's an error. If it's an inactive thread, return
244 NOTE: This function probably shouldn't call error(). */
247 thread_to_lwp (ptid_t thread_id, int default_lwp)
253 if (is_lwp (thread_id))
254 return thread_id; /* It's already an LWP ID. */
256 /* It's a thread. Convert to LWP. */
258 val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th);
260 return pid_to_ptid (-1); /* Thread must have terminated. */
261 else if (val != TD_OK)
262 error (_("thread_to_lwp: td_ta_map_id2thr %s"), td_err_string (val));
264 val = p_td_thr_get_info (&th, &ti);
266 return pid_to_ptid (-1); /* Thread must have terminated. */
267 else if (val != TD_OK)
268 error (_("thread_to_lwp: td_thr_get_info: %s"), td_err_string (val));
270 if (ti.ti_state != TD_THR_ACTIVE)
272 if (default_lwp != -1)
273 return pid_to_ptid (default_lwp);
274 error (_("thread_to_lwp: thread state not active: %s"),
275 td_state_string (ti.ti_state));
278 return BUILD_LWP (ti.ti_lid, PIDGET (thread_id));
281 /* Convert an LWP ID into a POSIX or Solaris thread ID. If LWP_ID
282 doesn't exists, that's an error.
284 NOTE: This function probably shouldn't call error(). */
287 lwp_to_thread (ptid_t lwp)
294 return lwp; /* It's already a thread ID. */
296 /* It's an LWP. Convert it to a thread ID. */
298 if (!target_thread_alive (lwp))
299 return pid_to_ptid (-1); /* Must be a defunct LPW. */
301 val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th);
303 return pid_to_ptid (-1); /* Thread must have terminated. */
304 else if (val != TD_OK)
305 error (_("lwp_to_thread: td_ta_map_lwp2thr: %s."), td_err_string (val));
307 val = p_td_thr_validate (&th);
309 return lwp; /* Unknown to libthread; just return LPW, */
310 else if (val != TD_OK)
311 error (_("lwp_to_thread: td_thr_validate: %s."), td_err_string (val));
313 val = p_td_thr_get_info (&th, &ti);
315 return pid_to_ptid (-1); /* Thread must have terminated. */
316 else if (val != TD_OK)
317 error (_("lwp_to_thread: td_thr_get_info: %s."), td_err_string (val));
319 return BUILD_THREAD (ti.ti_tid, PIDGET (lwp));
323 /* Most target vector functions from here on actually just pass
324 through to the layer beneath, as they don't need to do anything
325 specific for threads. */
327 /* Take a program previously attached to and detaches it. The program
328 resumes execution and will no longer stop on signals, etc. We'd
329 better not have left any breakpoints in the program or it'll die
330 when it hits one. For this to work, it may be necessary for the
331 process to have been previously attached. It *might* work if the
332 program was started via the normal ptrace (PTRACE_TRACEME). */
335 sol_thread_detach (struct target_ops *ops, char *args, int from_tty)
337 struct target_ops *beneath = find_target_beneath (ops);
339 sol_thread_active = 0;
340 inferior_ptid = pid_to_ptid (PIDGET (main_ph.ptid));
342 beneath->to_detach (beneath, args, from_tty);
345 /* Resume execution of process PTID. If STEP is nozero, then just
346 single step it. If SIGNAL is nonzero, restart it with that signal
347 activated. We may have to convert PTID from a thread ID to an LWP
351 sol_thread_resume (struct target_ops *ops,
352 ptid_t ptid, int step, enum target_signal signo)
354 struct cleanup *old_chain;
355 struct target_ops *beneath = find_target_beneath (ops);
357 old_chain = save_inferior_ptid ();
359 inferior_ptid = thread_to_lwp (inferior_ptid, PIDGET (main_ph.ptid));
360 if (PIDGET (inferior_ptid) == -1)
361 inferior_ptid = procfs_first_available ();
363 if (PIDGET (ptid) != -1)
365 ptid_t save_ptid = ptid;
367 ptid = thread_to_lwp (ptid, -2);
368 if (PIDGET (ptid) == -2) /* Inactive thread. */
369 error (_("This version of Solaris can't start inactive threads."));
370 if (info_verbose && PIDGET (ptid) == -1)
371 warning (_("Specified thread %ld seems to have terminated"),
372 GET_THREAD (save_ptid));
375 beneath->to_resume (beneath, ptid, step, signo);
377 do_cleanups (old_chain);
380 /* Wait for any threads to stop. We may have to convert PTID from a
381 thread ID to an LWP ID, and vice versa on the way out. */
384 sol_thread_wait (struct target_ops *ops,
385 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
389 struct target_ops *beneath = find_target_beneath (ops);
390 struct cleanup *old_chain;
392 save_ptid = inferior_ptid;
393 old_chain = save_inferior_ptid ();
395 inferior_ptid = thread_to_lwp (inferior_ptid, PIDGET (main_ph.ptid));
396 if (PIDGET (inferior_ptid) == -1)
397 inferior_ptid = procfs_first_available ();
399 if (PIDGET (ptid) != -1)
401 ptid_t save_ptid = ptid;
403 ptid = thread_to_lwp (ptid, -2);
404 if (PIDGET (ptid) == -2) /* Inactive thread. */
405 error (_("This version of Solaris can't start inactive threads."));
406 if (info_verbose && PIDGET (ptid) == -1)
407 warning (_("Specified thread %ld seems to have terminated"),
408 GET_THREAD (save_ptid));
411 rtnval = beneath->to_wait (beneath, ptid, ourstatus, options);
413 if (ourstatus->kind != TARGET_WAITKIND_EXITED)
415 /* Map the LWP of interest back to the appropriate thread ID. */
416 rtnval = lwp_to_thread (rtnval);
417 if (PIDGET (rtnval) == -1)
420 /* See if we have a new thread. */
421 if (is_thread (rtnval)
422 && !ptid_equal (rtnval, save_ptid)
423 && (!in_thread_list (rtnval)
424 || is_exited (rtnval)))
428 /* During process initialization, we may get here without the thread
429 package being initialized, since that can only happen after we've
430 found the shared libs. */
432 do_cleanups (old_chain);
438 sol_thread_fetch_registers (struct target_ops *ops,
439 struct regcache *regcache, int regnum)
442 td_thrhandle_t thandle;
445 prfpregset_t fpregset;
446 gdb_gregset_t *gregset_p = &gregset;
447 gdb_fpregset_t *fpregset_p = &fpregset;
448 struct target_ops *beneath = find_target_beneath (ops);
455 if (!is_thread (inferior_ptid))
457 /* It's an LWP; pass the request on to the layer beneath. */
458 beneath->to_fetch_registers (beneath, regcache, regnum);
462 /* Solaris thread: convert INFERIOR_PTID into a td_thrhandle_t. */
463 thread = GET_THREAD (inferior_ptid);
465 error (_("sol_thread_fetch_registers: thread == 0"));
467 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
469 error (_("sol_thread_fetch_registers: td_ta_map_id2thr: %s"),
470 td_err_string (val));
472 /* Get the general-purpose registers. */
474 val = p_td_thr_getgregs (&thandle, gregset);
475 if (val != TD_OK && val != TD_PARTIALREG)
476 error (_("sol_thread_fetch_registers: td_thr_getgregs %s"),
477 td_err_string (val));
479 /* For SPARC, TD_PARTIALREG means that only %i0...%i7, %l0..%l7, %pc
480 and %sp are saved (by a thread context switch). */
482 /* And, now the floating-point registers. */
484 val = p_td_thr_getfpregs (&thandle, &fpregset);
485 if (val != TD_OK && val != TD_NOFPREGS)
486 error (_("sol_thread_fetch_registers: td_thr_getfpregs %s"),
487 td_err_string (val));
489 /* Note that we must call supply_gregset and supply_fpregset *after*
490 calling the td routines because the td routines call ps_lget*
491 which affect the values stored in the registers array. */
493 supply_gregset (regcache, (const gdb_gregset_t *) gregset_p);
494 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset_p);
497 /* FIXME: libthread_db doesn't seem to handle this right. */
498 val = td_thr_getxregsize (&thandle, &xregsize);
499 if (val != TD_OK && val != TD_NOXREGS)
500 error (_("sol_thread_fetch_registers: td_thr_getxregsize %s"),
501 td_err_string (val));
505 xregset = alloca (xregsize);
506 val = td_thr_getxregs (&thandle, xregset);
508 error (_("sol_thread_fetch_registers: td_thr_getxregs %s"),
509 td_err_string (val));
515 sol_thread_store_registers (struct target_ops *ops,
516 struct regcache *regcache, int regnum)
519 td_thrhandle_t thandle;
522 prfpregset_t fpregset;
528 if (!is_thread (inferior_ptid))
530 struct target_ops *beneath = find_target_beneath (ops);
532 /* It's an LWP; pass the request on to the layer beneath. */
533 beneath->to_store_registers (beneath, regcache, regnum);
537 /* Solaris thread: convert INFERIOR_PTID into a td_thrhandle_t. */
538 thread = GET_THREAD (inferior_ptid);
540 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
542 error (_("sol_thread_store_registers: td_ta_map_id2thr %s"),
543 td_err_string (val));
547 /* Not writing all the registers. */
548 char old_value[MAX_REGISTER_SIZE];
550 /* Save new register value. */
551 regcache_raw_collect (regcache, regnum, old_value);
553 val = p_td_thr_getgregs (&thandle, gregset);
555 error (_("sol_thread_store_registers: td_thr_getgregs %s"),
556 td_err_string (val));
557 val = p_td_thr_getfpregs (&thandle, &fpregset);
559 error (_("sol_thread_store_registers: td_thr_getfpregs %s"),
560 td_err_string (val));
562 /* Restore new register value. */
563 regcache_raw_supply (regcache, regnum, old_value);
566 /* FIXME: libthread_db doesn't seem to handle this right. */
567 val = td_thr_getxregsize (&thandle, &xregsize);
568 if (val != TD_OK && val != TD_NOXREGS)
569 error (_("sol_thread_store_registers: td_thr_getxregsize %s"),
570 td_err_string (val));
574 xregset = alloca (xregsize);
575 val = td_thr_getxregs (&thandle, xregset);
577 error (_("sol_thread_store_registers: td_thr_getxregs %s"),
578 td_err_string (val));
583 fill_gregset (regcache, (gdb_gregset_t *) &gregset, regnum);
584 fill_fpregset (regcache, (gdb_fpregset_t *) &fpregset, regnum);
586 val = p_td_thr_setgregs (&thandle, gregset);
588 error (_("sol_thread_store_registers: td_thr_setgregs %s"),
589 td_err_string (val));
590 val = p_td_thr_setfpregs (&thandle, &fpregset);
592 error (_("sol_thread_store_registers: td_thr_setfpregs %s"),
593 td_err_string (val));
596 /* FIXME: libthread_db doesn't seem to handle this right. */
597 val = td_thr_getxregsize (&thandle, &xregsize);
598 if (val != TD_OK && val != TD_NOXREGS)
599 error (_("sol_thread_store_registers: td_thr_getxregsize %s"),
600 td_err_string (val));
602 /* ??? Should probably do something about writing the xregs here,
603 but what are they? */
607 /* Perform partial transfers on OBJECT. See target_read_partial and
608 target_write_partial for details of each variant. One, and only
609 one, of readbuf or writebuf must be non-NULL. */
612 sol_thread_xfer_partial (struct target_ops *ops, enum target_object object,
613 const char *annex, gdb_byte *readbuf,
614 const gdb_byte *writebuf,
615 ULONGEST offset, LONGEST len)
618 struct cleanup *old_chain;
619 struct target_ops *beneath = find_target_beneath (ops);
621 old_chain = save_inferior_ptid ();
623 if (is_thread (inferior_ptid) || !target_thread_alive (inferior_ptid))
625 /* It's either a thread or an LWP that isn't alive. Any live
626 LWP will do so use the first available.
628 NOTE: We don't need to call switch_to_thread; we're just
630 inferior_ptid = procfs_first_available ();
633 retval = beneath->to_xfer_partial (beneath, object, annex,
634 readbuf, writebuf, offset, len);
636 do_cleanups (old_chain);
642 check_for_thread_db (void)
647 /* Do nothing if we couldn't load libthread_db.so.1. */
648 if (p_td_ta_new == NULL)
651 if (sol_thread_active)
652 /* Nothing to do. The thread library was already detected and the
653 target vector was already activated. */
656 /* Now, initialize libthread_db. This needs to be done after the
657 shared libraries are located because it needs information from
658 the user's thread library. */
663 warning (_("sol_thread_new_objfile: td_init: %s"), td_err_string (err));
667 /* Now attempt to open a connection to the thread library. */
668 err = p_td_ta_new (&main_ph, &main_ta);
672 /* No thread library was detected. */
676 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
678 /* The thread library was detected. Activate the sol_thread target. */
679 push_target (&sol_thread_ops);
680 sol_thread_active = 1;
682 main_ph.ptid = inferior_ptid; /* Save for xfer_memory. */
683 ptid = lwp_to_thread (inferior_ptid);
684 if (PIDGET (ptid) != -1)
685 inferior_ptid = ptid;
687 target_find_new_threads ();
691 warning (_("Cannot initialize thread debugging library: %s"),
692 td_err_string (err));
697 /* This routine is called whenever a new symbol table is read in, or
698 when all symbol tables are removed. libthread_db can only be
699 initialized when it finds the right variables in libthread.so.
700 Since it's a shared library, those variables don't show up until
701 the library gets mapped and the symbol table is read in. */
704 sol_thread_new_objfile (struct objfile *objfile)
707 check_for_thread_db ();
710 /* Clean up after the inferior dies. */
713 sol_thread_mourn_inferior (struct target_ops *ops)
715 struct target_ops *beneath = find_target_beneath (ops);
717 sol_thread_active = 0;
721 beneath->to_mourn_inferior (beneath);
724 /* Return true if PTID is still active in the inferior. */
727 sol_thread_alive (struct target_ops *ops, ptid_t ptid)
729 if (is_thread (ptid))
731 /* It's a (user-level) thread. */
736 pid = GET_THREAD (ptid);
737 if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK)
738 return 0; /* Thread not found. */
739 if ((val = p_td_thr_validate (&th)) != TD_OK)
740 return 0; /* Thread not valid. */
741 return 1; /* Known thread. */
745 struct target_ops *beneath = find_target_beneath (ops);
747 /* It's an LPW; pass the request on to the layer below. */
748 return beneath->to_thread_alive (beneath, ptid);
753 /* These routines implement the lower half of the thread_db interface,
754 i.e. the ps_* routines. */
756 /* Various versions of <proc_service.h> have slightly different
757 function prototypes. In particular, we have
760 struct ps_prochandle * const struct ps_prochandle *
765 Which one you have depends on the Solaris version and what patches
766 you've applied. On the theory that there are only two major
767 variants, we have configure check the prototype of ps_pdwrite (),
768 and use that info to make appropriate typedefs here. */
770 #ifdef PROC_SERVICE_IS_OLD
771 typedef const struct ps_prochandle *gdb_ps_prochandle_t;
772 typedef char *gdb_ps_read_buf_t;
773 typedef char *gdb_ps_write_buf_t;
774 typedef int gdb_ps_size_t;
775 typedef psaddr_t gdb_ps_addr_t;
777 typedef struct ps_prochandle *gdb_ps_prochandle_t;
778 typedef void *gdb_ps_read_buf_t;
779 typedef const void *gdb_ps_write_buf_t;
780 typedef size_t gdb_ps_size_t;
781 typedef psaddr_t gdb_ps_addr_t;
784 /* The next four routines are called by libthread_db to tell us to
785 stop and stop a particular process or lwp. Since GDB ensures that
786 these are all stopped by the time we call anything in thread_db,
787 these routines need to do nothing. */
792 ps_pstop (gdb_ps_prochandle_t ph)
797 /* Process continue. */
800 ps_pcontinue (gdb_ps_prochandle_t ph)
808 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
816 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
821 /* Looks up the symbol LD_SYMBOL_NAME in the debugger's symbol table. */
824 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *ld_object_name,
825 const char *ld_symbol_name, gdb_ps_addr_t *ld_symbol_addr)
827 struct minimal_symbol *ms;
829 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
833 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
837 /* Common routine for reading and writing memory. */
840 rw_common (int dowrite, const struct ps_prochandle *ph, gdb_ps_addr_t addr,
844 struct cleanup *old_chain;
846 old_chain = save_inferior_ptid ();
848 if (is_thread (inferior_ptid) || !target_thread_alive (inferior_ptid))
850 /* It's either a thread or an LWP that isn't alive. Any live
851 LWP will do so use the first available.
853 NOTE: We don't need to call switch_to_thread; we're just
855 inferior_ptid = procfs_first_available ();
858 #if defined (__sparcv9)
859 /* For Sparc64 cross Sparc32, make sure the address has not been
860 accidentally sign-extended (or whatever) to beyond 32 bits. */
861 if (bfd_get_arch_size (exec_bfd) == 32)
866 ret = target_write_memory (addr, buf, size);
868 ret = target_read_memory (addr, buf, size);
870 do_cleanups (old_chain);
872 return (ret == 0 ? PS_OK : PS_ERR);
875 /* Copies SIZE bytes from target process .data segment to debugger memory. */
878 ps_pdread (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
879 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
881 return rw_common (0, ph, addr, buf, size);
884 /* Copies SIZE bytes from debugger memory .data segment to target process. */
887 ps_pdwrite (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
888 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
890 return rw_common (1, ph, addr, (char *) buf, size);
893 /* Copies SIZE bytes from target process .text segment to debugger memory. */
896 ps_ptread (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
897 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
899 return rw_common (0, ph, addr, buf, size);
902 /* Copies SIZE bytes from debugger memory .text segment to target process. */
905 ps_ptwrite (gdb_ps_prochandle_t ph, gdb_ps_addr_t addr,
906 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
908 return rw_common (1, ph, addr, (char *) buf, size);
911 /* Get general-purpose registers for LWP. */
914 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
916 struct cleanup *old_chain;
917 struct regcache *regcache;
919 old_chain = save_inferior_ptid ();
921 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
922 regcache = get_thread_regcache (inferior_ptid);
924 target_fetch_registers (regcache, -1);
925 fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
927 do_cleanups (old_chain);
932 /* Set general-purpose registers for LWP. */
935 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
936 const prgregset_t gregset)
938 struct cleanup *old_chain;
939 struct regcache *regcache;
941 old_chain = save_inferior_ptid ();
943 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
944 regcache = get_thread_regcache (inferior_ptid);
946 supply_gregset (regcache, (const gdb_gregset_t *) gregset);
947 target_store_registers (regcache, -1);
949 do_cleanups (old_chain);
954 /* Log a message (sends to gdb_stderr). */
957 ps_plog (const char *fmt, ...)
961 va_start (args, fmt);
963 vfprintf_filtered (gdb_stderr, fmt, args);
966 /* Get size of extra register set. Currently a noop. */
969 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
976 val = get_lwp_fd (ph, lwpid, &lwp_fd);
980 if (ioctl (lwp_fd, PIOCGXREGSIZE, ®size))
983 return PS_NOFREGS; /* XXX Wrong code, but this is the closest
984 thing in proc_service.h */
986 print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno);
994 /* Get extra register set. Currently a noop. */
997 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1003 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1007 if (ioctl (lwp_fd, PIOCGXREG, xregset))
1009 print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno);
1017 /* Set extra register set. Currently a noop. */
1020 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1026 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1030 if (ioctl (lwp_fd, PIOCSXREG, xregset))
1032 print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno);
1040 /* Get floating-point registers for LWP. */
1043 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1044 prfpregset_t *fpregset)
1046 struct cleanup *old_chain;
1047 struct regcache *regcache;
1049 old_chain = save_inferior_ptid ();
1051 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1052 regcache = get_thread_regcache (inferior_ptid);
1054 target_fetch_registers (regcache, -1);
1055 fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
1057 do_cleanups (old_chain);
1062 /* Set floating-point regs for LWP */
1065 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1066 const prfpregset_t * fpregset)
1068 struct cleanup *old_chain;
1069 struct regcache *regcache;
1071 old_chain = save_inferior_ptid ();
1073 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1074 regcache = get_thread_regcache (inferior_ptid);
1076 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
1077 target_store_registers (regcache, -1);
1079 do_cleanups (old_chain);
1084 #ifdef PR_MODEL_LP64
1085 /* Identify process as 32-bit or 64-bit. At the moment we're using
1086 BFD to do this. There might be a more Solaris-specific
1087 (e.g. procfs) method, but this ought to work. */
1090 ps_pdmodel (gdb_ps_prochandle_t ph, int *data_model)
1093 *data_model = PR_MODEL_UNKNOWN;
1094 else if (bfd_get_arch_size (exec_bfd) == 32)
1095 *data_model = PR_MODEL_ILP32;
1097 *data_model = PR_MODEL_LP64;
1101 #endif /* PR_MODEL_LP64 */
1103 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
1105 /* Reads the local descriptor table of a LWP.
1107 This function is necessary on x86-solaris only. Without it, the loading
1108 of libthread_db would fail because of ps_lgetLDT being undefined. */
1111 ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1114 /* NOTE: only used on Solaris, therefore OK to refer to procfs.c. */
1115 extern struct ssd *procfs_find_LDT_entry (ptid_t);
1118 /* FIXME: can't I get the process ID from the prochandle or
1121 if (PIDGET (inferior_ptid) <= 0 || lwpid <= 0)
1124 ret = procfs_find_LDT_entry (BUILD_LWP (lwpid, PIDGET (inferior_ptid)));
1127 memcpy (pldt, ret, sizeof (struct ssd));
1131 /* LDT not found. */
1137 /* Convert PTID to printable form. */
1140 solaris_pid_to_str (struct target_ops *ops, ptid_t ptid)
1142 static char buf[100];
1144 if (is_thread (ptid))
1148 lwp = thread_to_lwp (ptid, -2);
1150 if (PIDGET (lwp) == -1)
1151 sprintf (buf, "Thread %ld (defunct)", GET_THREAD (ptid));
1152 else if (PIDGET (lwp) != -2)
1153 sprintf (buf, "Thread %ld (LWP %ld)",
1154 GET_THREAD (ptid), GET_LWP (lwp));
1156 sprintf (buf, "Thread %ld ", GET_THREAD (ptid));
1158 else if (GET_LWP (ptid) != 0)
1159 sprintf (buf, "LWP %ld ", GET_LWP (ptid));
1161 sprintf (buf, "process %d ", PIDGET (ptid));
1167 /* Worker bee for find_new_threads. Callback function that gets
1168 called once per user-level thread (i.e. not for LWP's). */
1171 sol_find_new_threads_callback (const td_thrhandle_t *th, void *ignored)
1177 retval = p_td_thr_get_info (th, &ti);
1178 if (retval != TD_OK)
1181 ptid = BUILD_THREAD (ti.ti_tid, PIDGET (inferior_ptid));
1182 if (!in_thread_list (ptid) || is_exited (ptid))
1189 sol_find_new_threads (struct target_ops *ops)
1191 struct target_ops *beneath = find_target_beneath (ops);
1193 /* First Find any new LWP's. */
1194 if (beneath->to_find_new_threads != NULL)
1195 beneath->to_find_new_threads (beneath);
1197 /* Then find any new user-level threads. */
1198 p_td_ta_thr_iter (main_ta, sol_find_new_threads_callback, (void *) 0,
1199 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1200 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1203 /* Worker bee for the "info sol-thread" command. This is a callback
1204 function that gets called once for each Solaris user-level thread
1205 (i.e. not for LWPs) in the inferior. Print anything interesting
1206 that we can think of. */
1209 info_cb (const td_thrhandle_t *th, void *s)
1214 ret = p_td_thr_get_info (th, &ti);
1217 printf_filtered ("%s thread #%d, lwp %d, ",
1218 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
1219 ti.ti_tid, ti.ti_lid);
1220 switch (ti.ti_state)
1223 case TD_THR_UNKNOWN:
1224 printf_filtered ("<unknown state>");
1226 case TD_THR_STOPPED:
1227 printf_filtered ("(stopped)");
1230 printf_filtered ("(run) ");
1233 printf_filtered ("(active) ");
1236 printf_filtered ("(zombie) ");
1239 printf_filtered ("(asleep) ");
1241 case TD_THR_STOPPED_ASLEEP:
1242 printf_filtered ("(stopped asleep)");
1245 /* Print thr_create start function. */
1246 if (ti.ti_startfunc != 0)
1248 struct minimal_symbol *msym;
1249 msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc);
1251 printf_filtered (" startfunc: %s\n",
1252 SYMBOL_PRINT_NAME (msym));
1254 printf_filtered (" startfunc: 0x%s\n", paddr (ti.ti_startfunc));
1257 /* If thread is asleep, print function that went to sleep. */
1258 if (ti.ti_state == TD_THR_SLEEP)
1260 struct minimal_symbol *msym;
1261 msym = lookup_minimal_symbol_by_pc (ti.ti_pc);
1263 printf_filtered (" - Sleep func: %s\n",
1264 SYMBOL_PRINT_NAME (msym));
1266 printf_filtered (" - Sleep func: 0x%s\n", paddr (ti.ti_startfunc));
1269 /* Wrap up line, if necessary. */
1270 if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0)
1271 printf_filtered ("\n"); /* don't you hate counting newlines? */
1274 warning (_("info sol-thread: failed to get info for thread."));
1279 /* List some state about each Solaris user-level thread in the
1283 info_solthreads (char *args, int from_tty)
1285 p_td_ta_thr_iter (main_ta, info_cb, args,
1286 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1287 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1291 init_sol_thread_ops (void)
1293 sol_thread_ops.to_shortname = "solaris-threads";
1294 sol_thread_ops.to_longname = "Solaris threads and pthread.";
1295 sol_thread_ops.to_doc = "Solaris threads and pthread support.";
1296 sol_thread_ops.to_detach = sol_thread_detach;
1297 sol_thread_ops.to_resume = sol_thread_resume;
1298 sol_thread_ops.to_wait = sol_thread_wait;
1299 sol_thread_ops.to_fetch_registers = sol_thread_fetch_registers;
1300 sol_thread_ops.to_store_registers = sol_thread_store_registers;
1301 sol_thread_ops.to_xfer_partial = sol_thread_xfer_partial;
1302 sol_thread_ops.to_mourn_inferior = sol_thread_mourn_inferior;
1303 sol_thread_ops.to_thread_alive = sol_thread_alive;
1304 sol_thread_ops.to_pid_to_str = solaris_pid_to_str;
1305 sol_thread_ops.to_find_new_threads = sol_find_new_threads;
1306 sol_thread_ops.to_stratum = thread_stratum;
1307 sol_thread_ops.to_magic = OPS_MAGIC;
1311 _initialize_sol_thread (void)
1315 init_sol_thread_ops ();
1317 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1321 #define resolve(X) \
1322 if (!(p_##X = dlsym (dlhandle, #X))) \
1326 resolve (td_ta_new);
1327 resolve (td_ta_delete);
1329 resolve (td_ta_get_ph);
1330 resolve (td_ta_get_nthreads);
1331 resolve (td_ta_tsd_iter);
1332 resolve (td_ta_thr_iter);
1333 resolve (td_thr_validate);
1334 resolve (td_thr_tsd);
1335 resolve (td_thr_get_info);
1336 resolve (td_thr_getfpregs);
1337 resolve (td_thr_getxregsize);
1338 resolve (td_thr_getxregs);
1339 resolve (td_thr_sigsetmask);
1340 resolve (td_thr_setprio);
1341 resolve (td_thr_setsigpending);
1342 resolve (td_thr_setfpregs);
1343 resolve (td_thr_setxregs);
1344 resolve (td_ta_map_id2thr);
1345 resolve (td_ta_map_lwp2thr);
1346 resolve (td_thr_getgregs);
1347 resolve (td_thr_setgregs);
1349 add_target (&sol_thread_ops);
1351 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1352 _("Show info on Solaris user threads."), &maintenanceinfolist);
1354 /* Hook into new_objfile notification. */
1355 observer_attach_new_objfile (sol_thread_new_objfile);
1359 fprintf_unfiltered (gdb_stderr, "\
1360 [GDB will not be able to debug user-mode threads: %s]\n", dlerror ());