* Fix for PR 18665, from sky branch.
[binutils-gdb.git] / gdb / sol-thread.c
blobccaa6dcc6c90af5868441489e4977197bf701c28
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, Boston, MA 02111-1307, USA. */
20 /* This module implements a sort of half target that sits between the
21 machine-independent parts of GDB and the /proc interface (procfs.c) to
22 provide access to the Solaris user-mode thread implementation.
24 Solaris threads are true user-mode threads, which are invoked via the thr_*
25 and pthread_* (native and Posix respectivly) interfaces. These are mostly
26 implemented in user-space, with all thread context kept in various
27 structures that live in the user's heap. These should not be confused with
28 lightweight processes (LWPs), which are implemented by the kernel, and
29 scheduled without explicit intervention by the process.
31 Just to confuse things a little, Solaris threads (both native and Posix) are
32 actually implemented using LWPs. In general, there are going to be more
33 threads than LWPs. There is no fixed correspondence between a thread and an
34 LWP. When a thread wants to run, it gets scheduled onto the first available
35 LWP and can therefore migrate from one LWP to another as time goes on. A
36 sleeping thread may not be associated with an LWP at all!
38 To make it possible to mess with threads, Sun provides a library called
39 libthread_db.so.1 (not to be confused with libthread_db.so.0, which doesn't
40 have a published interface). This interface has an upper part, which it
41 provides, and a lower part which I provide. The upper part consists of the
42 td_* routines, which allow me to find all the threads, query their state,
43 etc... The lower part consists of all of the ps_*, which are used by the
44 td_* routines to read/write memory, manipulate LWPs, lookup symbols, etc...
45 The ps_* routines actually do most of their work by calling functions in
46 procfs.c. */
48 #include "defs.h"
49 #include <thread.h>
50 #include <proc_service.h>
51 #include <thread_db.h>
52 #include "gdbthread.h"
53 #include "target.h"
54 #include "inferior.h"
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <sys/stat.h>
58 #include <dlfcn.h>
59 #include "gdbcmd.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 extern int procfs_suppress_run;
68 extern struct target_ops procfs_ops; /* target vector for procfs.c */
69 extern struct target_ops core_ops; /* target vector for corelow.c */
70 extern char *procfs_pid_to_str PARAMS ((int pid));
72 /* Note that these prototypes differ slightly from those used in procfs.c
73 for of two reasons. One, we can't use gregset_t, as that's got a whole
74 different meaning under Solaris (also, see above). Two, we can't use the
75 pointer form here as these are actually arrays of ints (for Sparc's at
76 least), and are automatically coerced into pointers to ints when used as
77 parameters. That makes it impossible to avoid a compiler warning when
78 passing pr{g fp}regset_t's from a parameter to an argument of one of
79 these functions. */
81 extern void supply_gregset PARAMS ((const prgregset_t));
82 extern void fill_gregset PARAMS ((prgregset_t, int));
83 extern void supply_fpregset PARAMS ((const prfpregset_t *));
84 extern void fill_fpregset PARAMS ((prfpregset_t *, int));
86 /* This struct is defined by us, but mainly used for the proc_service interface.
87 We don't have much use for it, except as a handy place to get a real pid
88 for memory accesses. */
90 struct ps_prochandle
92 pid_t pid;
95 struct string_map
97 int num;
98 char *str;
101 static struct ps_prochandle main_ph;
102 static td_thragent_t *main_ta;
103 static int sol_thread_active = 0;
105 static struct cleanup * save_inferior_pid PARAMS ((void));
106 static void restore_inferior_pid PARAMS ((int pid));
107 static char *td_err_string PARAMS ((td_err_e errcode));
108 static char *td_state_string PARAMS ((td_thr_state_e statecode));
109 static int thread_to_lwp PARAMS ((int thread_id, int default_lwp));
110 static void sol_thread_resume PARAMS ((int pid, int step,
111 enum target_signal signo));
112 static int lwp_to_thread PARAMS ((int lwp));
113 static int sol_thread_alive PARAMS ((int pid));
114 static void sol_core_close PARAMS ((int quitting));
116 #define THREAD_FLAG 0x80000000
117 #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
118 #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
119 #define GET_LWP(LWP_ID) (TIDGET(LWP_ID))
120 #define GET_THREAD(THREAD_ID) (((THREAD_ID) >> 16) & 0x7fff)
121 #define BUILD_LWP(LWP_ID, PID) ((LWP_ID) << 16 | (PID))
122 #define BUILD_THREAD(THREAD_ID, PID) (THREAD_FLAG | BUILD_LWP (THREAD_ID, PID))
124 /* Pointers to routines from lithread_db resolved by dlopen() */
126 static void
127 (*p_td_log) (const int on_off);
128 static td_err_e
129 (*p_td_ta_new) (const struct ps_prochandle *ph_p, td_thragent_t **ta_pp);
130 static td_err_e
131 (*p_td_ta_delete) (td_thragent_t *ta_p);
132 static td_err_e
133 (*p_td_init) (void);
134 static td_err_e
135 (*p_td_ta_get_ph) (const td_thragent_t *ta_p, struct ps_prochandle **ph_pp);
136 static td_err_e
137 (*p_td_ta_get_nthreads) (const td_thragent_t *ta_p, int *nthread_p);
138 static td_err_e
139 (*p_td_ta_tsd_iter) (const td_thragent_t *ta_p, td_key_iter_f *cb, void *cbdata_p);
140 static td_err_e
141 (*p_td_ta_thr_iter) (const td_thragent_t *ta_p, td_thr_iter_f *cb, void *cbdata_p, td_thr_state_e state,
142 int ti_pri, sigset_t *ti_sigmask_p, unsigned ti_user_flags);
143 static td_err_e
144 (*p_td_thr_validate) (const td_thrhandle_t *th_p);
145 static td_err_e
146 (*p_td_thr_tsd) (const td_thrhandle_t *th_p, const thread_key_t key, void **data_pp);
147 static td_err_e
148 (*p_td_thr_get_info) (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p);
149 static td_err_e
150 (*p_td_thr_getfpregs) (const td_thrhandle_t *th_p, prfpregset_t *fpregset);
151 static td_err_e
152 (*p_td_thr_getxregsize) (const td_thrhandle_t *th_p, int *xregsize);
153 static td_err_e
154 (*p_td_thr_getxregs) (const td_thrhandle_t *th_p, const caddr_t xregset);
155 static td_err_e
156 (*p_td_thr_sigsetmask) (const td_thrhandle_t *th_p, const sigset_t ti_sigmask);
157 static td_err_e
158 (*p_td_thr_setprio) (const td_thrhandle_t *th_p, const int ti_pri);
159 static td_err_e
160 (*p_td_thr_setsigpending) (const td_thrhandle_t *th_p, const uchar_t ti_pending_flag, const sigset_t ti_pending);
161 static td_err_e
162 (*p_td_thr_setfpregs) (const td_thrhandle_t *th_p, const prfpregset_t *fpregset);
163 static td_err_e
164 (*p_td_thr_setxregs) (const td_thrhandle_t *th_p, const caddr_t xregset);
165 static td_err_e
166 (*p_td_ta_map_id2thr) (const td_thragent_t *ta_p, thread_t tid, td_thrhandle_t *th_p);
167 static td_err_e
168 (*p_td_ta_map_lwp2thr) (const td_thragent_t *ta_p, lwpid_t lwpid, td_thrhandle_t *th_p);
169 static td_err_e
170 (*p_td_thr_getgregs) (const td_thrhandle_t *th_p, prgregset_t regset);
171 static td_err_e
172 (*p_td_thr_setgregs) (const td_thrhandle_t *th_p, const prgregset_t regset);
176 LOCAL FUNCTION
178 td_err_string - Convert a thread_db error code to a string
180 SYNOPSIS
182 char * td_err_string (errcode)
184 DESCRIPTION
186 Return the thread_db error string associated with errcode. If errcode
187 is unknown, then return a message.
191 static char *
192 td_err_string (errcode)
193 td_err_e errcode;
195 static struct string_map
196 td_err_table[] = {
197 {TD_OK, "generic \"call succeeded\""},
198 {TD_ERR, "generic error."},
199 {TD_NOTHR, "no thread can be found to satisfy query"},
200 {TD_NOSV, "no synch. variable can be found to satisfy query"},
201 {TD_NOLWP, "no lwp can be found to satisfy query"},
202 {TD_BADPH, "invalid process handle"},
203 {TD_BADTH, "invalid thread handle"},
204 {TD_BADSH, "invalid synchronization handle"},
205 {TD_BADTA, "invalid thread agent"},
206 {TD_BADKEY, "invalid key"},
207 {TD_NOMSG, "td_thr_event_getmsg() called when there was no message"},
208 {TD_NOFPREGS, "FPU register set not available for given thread"},
209 {TD_NOLIBTHREAD, "application not linked with libthread"},
210 {TD_NOEVENT, "requested event is not supported"},
211 {TD_NOCAPAB, "capability not available"},
212 {TD_DBERR, "Debugger service failed"},
213 {TD_NOAPLIC, "Operation not applicable to"},
214 {TD_NOTSD, "No thread specific data for this thread"},
215 {TD_MALLOC, "Malloc failed"},
216 {TD_PARTIALREG, "Only part of register set was writen/read"},
217 {TD_NOXREGS, "X register set not available for given thread"}
219 const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
220 int i;
221 static char buf[50];
223 for (i = 0; i < td_err_size; i++)
224 if (td_err_table[i].num == errcode)
225 return td_err_table[i].str;
227 sprintf (buf, "Unknown thread_db error code: %d", errcode);
229 return buf;
234 LOCAL FUNCTION
236 td_state_string - Convert a thread_db state code to a string
238 SYNOPSIS
240 char * td_state_string (statecode)
242 DESCRIPTION
244 Return the thread_db state string associated with statecode. If
245 statecode is unknown, then return a message.
249 static char *
250 td_state_string (statecode)
251 td_thr_state_e statecode;
253 static struct string_map
254 td_thr_state_table[] = {
255 {TD_THR_ANY_STATE, "any state"},
256 {TD_THR_UNKNOWN, "unknown"},
257 {TD_THR_STOPPED, "stopped"},
258 {TD_THR_RUN, "run"},
259 {TD_THR_ACTIVE, "active"},
260 {TD_THR_ZOMBIE, "zombie"},
261 {TD_THR_SLEEP, "sleep"},
262 {TD_THR_STOPPED_ASLEEP, "stopped asleep"}
264 const int td_thr_state_table_size = sizeof td_thr_state_table / sizeof (struct string_map);
265 int i;
266 static char buf[50];
268 for (i = 0; i < td_thr_state_table_size; i++)
269 if (td_thr_state_table[i].num == statecode)
270 return td_thr_state_table[i].str;
272 sprintf (buf, "Unknown thread_db state code: %d", statecode);
274 return buf;
279 LOCAL FUNCTION
281 thread_to_lwp - Convert a Posix or Solaris thread id to a LWP id.
283 SYNOPSIS
285 int thread_to_lwp (thread_id, default_lwp)
287 DESCRIPTION
289 This function converts a Posix or Solaris thread id to a lightweight
290 process id. If thread_id is non-existent, that's an error. If it's
291 an inactive thread, then we return default_lwp.
293 NOTES
295 This function probably shouldn't call error()...
299 static int
300 thread_to_lwp (thread_id, default_lwp)
301 int thread_id;
302 int default_lwp;
304 td_thrinfo_t ti;
305 td_thrhandle_t th;
306 td_err_e val;
308 if (is_lwp (thread_id))
309 return thread_id; /* It's already an LWP id */
311 /* It's a thread. Convert to lwp */
313 val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th);
314 if (val == TD_NOTHR)
315 return -1; /* thread must have terminated */
316 else if (val != TD_OK)
317 error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val));
319 val = p_td_thr_get_info (&th, &ti);
320 if (val == TD_NOTHR)
321 return -1; /* thread must have terminated */
322 else if (val != TD_OK)
323 error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val));
325 if (ti.ti_state != TD_THR_ACTIVE)
327 if (default_lwp != -1)
328 return default_lwp;
329 error ("thread_to_lwp: thread state not active: %s",
330 td_state_string (ti.ti_state));
333 return BUILD_LWP (ti.ti_lid, PIDGET (thread_id));
338 LOCAL FUNCTION
340 lwp_to_thread - Convert a LWP id to a Posix or Solaris thread id.
342 SYNOPSIS
344 int lwp_to_thread (lwp_id)
346 DESCRIPTION
348 This function converts a lightweight process id to a Posix or Solaris
349 thread id. If thread_id is non-existent, that's an error.
351 NOTES
353 This function probably shouldn't call error()...
357 static int
358 lwp_to_thread (lwp)
359 int lwp;
361 td_thrinfo_t ti;
362 td_thrhandle_t th;
363 td_err_e val;
365 if (is_thread (lwp))
366 return lwp; /* It's already a thread id */
368 /* It's an lwp. Convert it to a thread id. */
370 if (!sol_thread_alive (lwp))
371 return -1; /* defunct lwp */
373 val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th);
374 if (val == TD_NOTHR)
375 return -1; /* thread must have terminated */
376 else if (val != TD_OK)
377 error ("lwp_to_thread: td_ta_map_lwp2thr: %s.", td_err_string (val));
379 val = p_td_thr_validate (&th);
380 if (val == TD_NOTHR)
381 return lwp; /* libthread doesn't know about it, just return lwp */
382 else if (val != TD_OK)
383 error ("lwp_to_thread: td_thr_validate: %s.", td_err_string (val));
385 val = p_td_thr_get_info (&th, &ti);
386 if (val == TD_NOTHR)
387 return -1; /* thread must have terminated */
388 else if (val != TD_OK)
389 error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val));
391 return BUILD_THREAD (ti.ti_tid, PIDGET (lwp));
396 LOCAL FUNCTION
398 save_inferior_pid - Save inferior_pid on the cleanup list
399 restore_inferior_pid - Restore inferior_pid from the cleanup list
401 SYNOPSIS
403 struct cleanup *save_inferior_pid ()
404 void restore_inferior_pid (int pid)
406 DESCRIPTION
408 These two functions act in unison to restore inferior_pid in
409 case of an error.
411 NOTES
413 inferior_pid is a global variable that needs to be changed by many of
414 these routines before calling functions in procfs.c. In order to
415 guarantee that inferior_pid gets restored (in case of errors), you
416 need to call save_inferior_pid before changing it. At the end of the
417 function, you should invoke do_cleanups to restore it.
422 static struct cleanup *
423 save_inferior_pid ()
425 return make_cleanup (restore_inferior_pid, inferior_pid);
428 static void
429 restore_inferior_pid (pid)
430 int pid;
432 inferior_pid = pid;
436 /* Most target vector functions from here on actually just pass through to
437 procfs.c, as they don't need to do anything specific for threads. */
440 /* ARGSUSED */
441 static void
442 sol_thread_open (arg, from_tty)
443 char *arg;
444 int from_tty;
446 procfs_ops.to_open (arg, from_tty);
449 /* Attach to process PID, then initialize for debugging it
450 and wait for the trace-trap that results from attaching. */
452 static void
453 sol_thread_attach (args, from_tty)
454 char *args;
455 int from_tty;
457 procfs_ops.to_attach (args, from_tty);
458 /* Must get symbols from solibs before libthread_db can run! */
459 SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0);
460 if (sol_thread_active)
462 printf_filtered ("sol-thread active.\n");
463 main_ph.pid = inferior_pid; /* Save for xfer_memory */
464 push_target (&sol_thread_ops);
465 inferior_pid = lwp_to_thread (inferior_pid);
466 if (inferior_pid == -1)
467 inferior_pid = main_ph.pid;
468 else
469 add_thread (inferior_pid);
471 /* XXX - might want to iterate over all the threads and register them. */
474 /* Take a program previously attached to and detaches it.
475 The program resumes execution and will no longer stop
476 on signals, etc. We'd better not have left any breakpoints
477 in the program or it'll die when it hits one. For this
478 to work, it may be necessary for the process to have been
479 previously attached. It *might* work if the program was
480 started via the normal ptrace (PTRACE_TRACEME). */
482 static void
483 sol_thread_detach (args, from_tty)
484 char *args;
485 int from_tty;
487 unpush_target (&sol_thread_ops);
488 procfs_ops.to_detach (args, from_tty);
491 /* Resume execution of process PID. If STEP is nozero, then
492 just single step it. If SIGNAL is nonzero, restart it with that
493 signal activated. We may have to convert pid from a thread-id to an LWP id
494 for procfs. */
496 static void
497 sol_thread_resume (pid, step, signo)
498 int pid;
499 int step;
500 enum target_signal signo;
502 struct cleanup *old_chain;
504 old_chain = save_inferior_pid ();
506 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
507 if (inferior_pid == -1)
508 inferior_pid = procfs_first_available ();
510 if (pid != -1)
512 int save_pid = pid;
514 pid = thread_to_lwp (pid, -2);
515 if (pid == -2) /* Inactive thread */
516 error ("This version of Solaris can't start inactive threads.");
517 if (info_verbose && pid == -1)
518 warning ("Specified thread %d seems to have terminated",
519 GET_THREAD (save_pid));
522 procfs_ops.to_resume (pid, step, signo);
524 do_cleanups (old_chain);
527 /* Wait for any threads to stop. We may have to convert PID from a thread id
528 to a LWP id, and vice versa on the way out. */
530 static int
531 sol_thread_wait (pid, ourstatus)
532 int pid;
533 struct target_waitstatus *ourstatus;
535 int rtnval;
536 int save_pid;
537 struct cleanup *old_chain;
539 save_pid = inferior_pid;
540 old_chain = save_inferior_pid ();
542 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
543 if (inferior_pid == -1)
544 inferior_pid = procfs_first_available ();
546 if (pid != -1)
548 int save_pid = pid;
550 pid = thread_to_lwp (pid, -2);
551 if (pid == -2) /* Inactive thread */
552 error ("This version of Solaris can't start inactive threads.");
553 if (info_verbose && pid == -1)
554 warning ("Specified thread %d seems to have terminated",
555 GET_THREAD (save_pid));
558 rtnval = procfs_ops.to_wait (pid, ourstatus);
560 if (ourstatus->kind != TARGET_WAITKIND_EXITED)
562 /* Map the LWP of interest back to the appropriate thread ID */
563 rtnval = lwp_to_thread (rtnval);
564 if (rtnval == -1)
565 rtnval = save_pid;
567 /* See if we have a new thread */
568 if (is_thread (rtnval)
569 && rtnval != save_pid
570 && !in_thread_list (rtnval))
572 printf_filtered ("[New %s]\n", target_pid_to_str (rtnval));
573 add_thread (rtnval);
577 /* During process initialization, we may get here without the thread package
578 being initialized, since that can only happen after we've found the shared
579 libs. */
581 do_cleanups (old_chain);
583 return rtnval;
586 static void
587 sol_thread_fetch_registers (regno)
588 int regno;
590 thread_t thread;
591 td_thrhandle_t thandle;
592 td_err_e val;
593 prgregset_t gregset;
594 prfpregset_t fpregset;
595 #if 0
596 int xregsize;
597 caddr_t xregset;
598 #endif
600 if (!is_thread (inferior_pid))
601 { /* LWP: pass the request on to procfs.c */
602 if (target_has_execution)
603 procfs_ops.to_fetch_registers (regno);
604 else
605 orig_core_ops.to_fetch_registers (regno);
606 return;
609 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
611 thread = GET_THREAD (inferior_pid);
613 if (thread == 0)
614 error ("sol_thread_fetch_registers: thread == 0");
616 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
617 if (val != TD_OK)
618 error ("sol_thread_fetch_registers: td_ta_map_id2thr: %s",
619 td_err_string (val));
621 /* Get the integer regs */
623 val = p_td_thr_getgregs (&thandle, gregset);
624 if (val != TD_OK
625 && val != TD_PARTIALREG)
626 error ("sol_thread_fetch_registers: td_thr_getgregs %s",
627 td_err_string (val));
629 /* For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, pc and sp
630 are saved (by a thread context switch). */
632 /* And, now the fp regs */
634 val = p_td_thr_getfpregs (&thandle, &fpregset);
635 if (val != TD_OK
636 && val != TD_NOFPREGS)
637 error ("sol_thread_fetch_registers: td_thr_getfpregs %s",
638 td_err_string (val));
640 /* Note that we must call supply_{g fp}regset *after* calling the td routines
641 because the td routines call ps_lget* which affect the values stored in the
642 registers array. */
644 supply_gregset (gregset);
645 supply_fpregset (&fpregset);
647 #if 0
648 /* thread_db doesn't seem to handle this right */
649 val = td_thr_getxregsize (&thandle, &xregsize);
650 if (val != TD_OK && val != TD_NOXREGS)
651 error ("sol_thread_fetch_registers: td_thr_getxregsize %s",
652 td_err_string (val));
654 if (val == TD_OK)
656 xregset = alloca (xregsize);
657 val = td_thr_getxregs (&thandle, xregset);
658 if (val != TD_OK)
659 error ("sol_thread_fetch_registers: td_thr_getxregs %s",
660 td_err_string (val));
662 #endif
665 static void
666 sol_thread_store_registers (regno)
667 int regno;
669 thread_t thread;
670 td_thrhandle_t thandle;
671 td_err_e val;
672 prgregset_t regset;
673 prfpregset_t fpregset;
674 #if 0
675 int xregsize;
676 caddr_t xregset;
677 #endif
679 if (!is_thread (inferior_pid))
680 { /* LWP: pass the request on to procfs.c */
681 procfs_ops.to_store_registers (regno);
682 return;
685 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
687 thread = GET_THREAD (inferior_pid);
689 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
690 if (val != TD_OK)
691 error ("sol_thread_store_registers: td_ta_map_id2thr %s",
692 td_err_string (val));
694 if (regno != -1)
695 { /* Not writing all the regs */
696 /* save new register value */
697 char old_value[REGISTER_SIZE];
698 memcpy(old_value, & registers[REGISTER_BYTE(regno)], REGISTER_SIZE);
700 val = p_td_thr_getgregs (&thandle, regset);
701 if (val != TD_OK)
702 error ("sol_thread_store_registers: td_thr_getgregs %s",
703 td_err_string (val));
704 val = p_td_thr_getfpregs (&thandle, &fpregset);
705 if (val != TD_OK)
706 error ("sol_thread_store_registers: td_thr_getfpregs %s",
707 td_err_string (val));
709 /* restore new register value */
710 memcpy(& registers[REGISTER_BYTE(regno)], old_value, REGISTER_SIZE);
712 #if 0
713 /* thread_db doesn't seem to handle this right */
714 val = td_thr_getxregsize (&thandle, &xregsize);
715 if (val != TD_OK && val != TD_NOXREGS)
716 error ("sol_thread_store_registers: td_thr_getxregsize %s",
717 td_err_string (val));
719 if (val == TD_OK)
721 xregset = alloca (xregsize);
722 val = td_thr_getxregs (&thandle, xregset);
723 if (val != TD_OK)
724 error ("sol_thread_store_registers: td_thr_getxregs %s",
725 td_err_string (val));
727 #endif
730 fill_gregset (regset, regno);
731 fill_fpregset (&fpregset, regno);
733 val = p_td_thr_setgregs (&thandle, regset);
734 if (val != TD_OK)
735 error ("sol_thread_store_registers: td_thr_setgregs %s",
736 td_err_string (val));
737 val = p_td_thr_setfpregs (&thandle, &fpregset);
738 if (val != TD_OK)
739 error ("sol_thread_store_registers: td_thr_setfpregs %s",
740 td_err_string (val));
742 #if 0
743 /* thread_db doesn't seem to handle this right */
744 val = td_thr_getxregsize (&thandle, &xregsize);
745 if (val != TD_OK && val != TD_NOXREGS)
746 error ("sol_thread_store_registers: td_thr_getxregsize %s",
747 td_err_string (val));
749 /* Should probably do something about writing the xregs here, but what are
750 they? */
751 #endif
754 /* Get ready to modify the registers array. On machines which store
755 individual registers, this doesn't need to do anything. On machines
756 which store all the registers in one fell swoop, this makes sure
757 that registers contains all the registers from the program being
758 debugged. */
760 static void
761 sol_thread_prepare_to_store ()
763 procfs_ops.to_prepare_to_store ();
766 static int
767 sol_thread_xfer_memory (memaddr, myaddr, len, dowrite, target)
768 CORE_ADDR memaddr;
769 char *myaddr;
770 int len;
771 int dowrite;
772 struct target_ops *target; /* ignored */
774 int retval;
775 struct cleanup *old_chain;
777 old_chain = save_inferior_pid ();
779 if (is_thread (inferior_pid) || /* A thread */
780 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
781 inferior_pid = procfs_first_available (); /* Find any live lwp. */
782 /* Note: don't need to call switch_to_thread; we're just reading memory. */
784 if (target_has_execution)
785 retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
786 else
787 retval = orig_core_ops.to_xfer_memory (memaddr, myaddr, len,
788 dowrite, target);
790 do_cleanups (old_chain);
792 return retval;
795 /* Print status information about what we're accessing. */
797 static void
798 sol_thread_files_info (ignore)
799 struct target_ops *ignore;
801 procfs_ops.to_files_info (ignore);
804 static void
805 sol_thread_kill_inferior ()
807 procfs_ops.to_kill ();
810 static void
811 sol_thread_notice_signals (pid)
812 int pid;
814 procfs_ops.to_notice_signals (PIDGET (pid));
817 /* Fork an inferior process, and start debugging it with /proc. */
819 static void
820 sol_thread_create_inferior (exec_file, allargs, env)
821 char *exec_file;
822 char *allargs;
823 char **env;
825 procfs_ops.to_create_inferior (exec_file, allargs, env);
827 if (sol_thread_active && inferior_pid != 0)
829 main_ph.pid = inferior_pid; /* Save for xfer_memory */
831 push_target (&sol_thread_ops);
833 inferior_pid = lwp_to_thread (inferior_pid);
834 if (inferior_pid == -1)
835 inferior_pid = main_ph.pid;
837 add_thread (inferior_pid);
841 /* This routine is called whenever a new symbol table is read in, or when all
842 symbol tables are removed. libthread_db can only be initialized when it
843 finds the right variables in libthread.so. Since it's a shared library,
844 those variables don't show up until the library gets mapped and the symbol
845 table is read in. */
847 void
848 sol_thread_new_objfile (objfile)
849 struct objfile *objfile;
851 td_err_e val;
853 if (!objfile)
855 sol_thread_active = 0;
857 return;
860 /* don't do anything if init failed to resolve the libthread_db library */
861 if (!procfs_suppress_run)
862 return;
864 /* Now, initialize the thread debugging library. This needs to be done after
865 the shared libraries are located because it needs information from the
866 user's thread library. */
868 val = p_td_init ();
869 if (val != TD_OK)
870 error ("target_new_objfile: td_init: %s", td_err_string (val));
872 val = p_td_ta_new (&main_ph, &main_ta);
873 if (val == TD_NOLIBTHREAD)
874 return;
875 else if (val != TD_OK)
876 error ("target_new_objfile: td_ta_new: %s", td_err_string (val));
878 sol_thread_active = 1;
881 /* Clean up after the inferior dies. */
883 static void
884 sol_thread_mourn_inferior ()
886 unpush_target (&sol_thread_ops);
887 procfs_ops.to_mourn_inferior ();
890 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
892 static int
893 sol_thread_can_run ()
895 return procfs_suppress_run;
900 LOCAL FUNCTION
902 sol_thread_alive - test thread for "aliveness"
904 SYNOPSIS
906 static bool sol_thread_alive (int pid);
908 DESCRIPTION
910 returns true if thread still active in inferior.
914 static int
915 sol_thread_alive (pid)
916 int pid;
918 if (is_thread (pid)) /* non-kernel thread */
920 td_err_e val;
921 td_thrhandle_t th;
923 pid = GET_THREAD (pid);
924 if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK)
925 return 0; /* thread not found */
926 if ((val = p_td_thr_validate (&th)) != TD_OK)
927 return 0; /* thread not valid */
928 return 1; /* known thread: return true */
930 else /* kernel thread (LWP): let procfs test it */
932 if (target_has_execution)
933 return procfs_ops.to_thread_alive (pid);
934 else
935 return orig_core_ops.to_thread_alive (pid);
939 static void
940 sol_thread_stop ()
942 procfs_ops.to_stop ();
945 /* These routines implement the lower half of the thread_db interface. Ie: the
946 ps_* routines. */
948 /* Various versions of <proc_service.h> have slightly
949 different function prototypes. In particular, we have
951 NEWER OLDER
952 struct ps_prochandle * const struct ps_prochandle *
953 void* char*
954 const void* char*
955 int size_t
957 Which one you have depends on solaris version and what
958 patches you've applied. On the theory that there are
959 only two major variants, we have configure check the
960 prototype of ps_pdwrite (), and use that info to make
961 appropriate typedefs here. */
963 #ifdef PROC_SERVICE_IS_OLD
964 typedef const struct ps_prochandle * gdb_ps_prochandle_t;
965 typedef char * gdb_ps_read_buf_t;
966 typedef char * gdb_ps_write_buf_t;
967 typedef int gdb_ps_size_t;
968 #else
969 typedef struct ps_prochandle * gdb_ps_prochandle_t;
970 typedef void * gdb_ps_read_buf_t;
971 typedef const void * gdb_ps_write_buf_t;
972 typedef size_t gdb_ps_size_t;
973 #endif
976 /* The next four routines are called by thread_db to tell us to stop and stop
977 a particular process or lwp. Since GDB ensures that these are all stopped
978 by the time we call anything in thread_db, these routines need to do
979 nothing. */
981 ps_err_e
982 ps_pstop (gdb_ps_prochandle_t ph)
984 return PS_OK;
987 ps_err_e
988 ps_pcontinue (gdb_ps_prochandle_t ph)
990 return PS_OK;
993 ps_err_e
994 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
996 return PS_OK;
999 ps_err_e
1000 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1002 return PS_OK;
1005 ps_err_e
1006 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *ld_object_name,
1007 const char *ld_symbol_name, paddr_t *ld_symbol_addr)
1009 struct minimal_symbol *ms;
1011 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
1013 if (!ms)
1014 return PS_NOSYM;
1016 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
1018 return PS_OK;
1021 /* Common routine for reading and writing memory. */
1023 static ps_err_e
1024 rw_common (int dowrite, const struct ps_prochandle *ph, paddr_t addr,
1025 char *buf, int size)
1027 struct cleanup *old_chain;
1029 old_chain = save_inferior_pid ();
1031 if (is_thread (inferior_pid) || /* A thread */
1032 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
1033 inferior_pid = procfs_first_available (); /* Find any live lwp. */
1034 /* Note: don't need to call switch_to_thread; we're just reading memory. */
1036 while (size > 0)
1038 int cc;
1040 if (target_has_execution)
1041 cc = procfs_ops.to_xfer_memory (addr, buf, size, dowrite, &procfs_ops);
1042 else
1043 cc = orig_core_ops.to_xfer_memory (addr, buf, size, dowrite, &core_ops);
1045 if (cc < 0)
1047 if (dowrite == 0)
1048 print_sys_errmsg ("rw_common (): read", errno);
1049 else
1050 print_sys_errmsg ("rw_common (): write", errno);
1052 do_cleanups (old_chain);
1054 return PS_ERR;
1056 size -= cc;
1057 buf += cc;
1060 do_cleanups (old_chain);
1062 return PS_OK;
1065 ps_err_e
1066 ps_pdread (gdb_ps_prochandle_t ph, paddr_t addr,
1067 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1069 return rw_common (0, ph, addr, buf, size);
1072 ps_err_e
1073 ps_pdwrite (gdb_ps_prochandle_t ph, paddr_t addr,
1074 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1076 return rw_common (1, ph, addr, (char*) buf, size);
1079 ps_err_e
1080 ps_ptread (gdb_ps_prochandle_t ph, paddr_t addr,
1081 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1083 return rw_common (0, ph, addr, buf, size);
1086 ps_err_e
1087 ps_ptwrite (gdb_ps_prochandle_t ph, paddr_t addr,
1088 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1090 return rw_common (1, ph, addr, (char*) buf, size);
1093 /* Get integer regs */
1095 ps_err_e
1096 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1097 prgregset_t gregset)
1099 struct cleanup *old_chain;
1101 old_chain = save_inferior_pid ();
1103 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1105 if (target_has_execution)
1106 procfs_ops.to_fetch_registers (-1);
1107 else
1108 orig_core_ops.to_fetch_registers (-1);
1109 fill_gregset (gregset, -1);
1111 do_cleanups (old_chain);
1113 return PS_OK;
1116 /* Set integer regs */
1118 ps_err_e
1119 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1120 const prgregset_t gregset)
1122 struct cleanup *old_chain;
1124 old_chain = save_inferior_pid ();
1126 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1128 supply_gregset (gregset);
1129 if (target_has_execution)
1130 procfs_ops.to_store_registers (-1);
1131 else
1132 orig_core_ops.to_store_registers (-1);
1134 do_cleanups (old_chain);
1136 return PS_OK;
1139 void
1140 ps_plog (const char *fmt, ...)
1142 va_list args;
1144 va_start (args, fmt);
1146 vfprintf_filtered (gdb_stderr, fmt, args);
1149 /* Get size of extra register set. Currently a noop. */
1151 ps_err_e
1152 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
1154 #if 0
1155 int lwp_fd;
1156 int regsize;
1157 ps_err_e val;
1159 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1160 if (val != PS_OK)
1161 return val;
1163 if (ioctl (lwp_fd, PIOCGXREGSIZE, &regsize))
1165 if (errno == EINVAL)
1166 return PS_NOFREGS; /* XXX Wrong code, but this is the closest
1167 thing in proc_service.h */
1169 print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno);
1170 return PS_ERR;
1172 #endif
1174 return PS_OK;
1177 /* Get extra register set. Currently a noop. */
1179 ps_err_e
1180 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1182 #if 0
1183 int lwp_fd;
1184 ps_err_e val;
1186 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1187 if (val != PS_OK)
1188 return val;
1190 if (ioctl (lwp_fd, PIOCGXREG, xregset))
1192 print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno);
1193 return PS_ERR;
1195 #endif
1197 return PS_OK;
1200 /* Set extra register set. Currently a noop. */
1202 ps_err_e
1203 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1205 #if 0
1206 int lwp_fd;
1207 ps_err_e val;
1209 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1210 if (val != PS_OK)
1211 return val;
1213 if (ioctl (lwp_fd, PIOCSXREG, xregset))
1215 print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno);
1216 return PS_ERR;
1218 #endif
1220 return PS_OK;
1223 /* Get floating-point regs. */
1225 ps_err_e
1226 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1227 prfpregset_t *fpregset)
1229 struct cleanup *old_chain;
1231 old_chain = save_inferior_pid ();
1233 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1235 if (target_has_execution)
1236 procfs_ops.to_fetch_registers (-1);
1237 else
1238 orig_core_ops.to_fetch_registers (-1);
1239 fill_fpregset (fpregset, -1);
1241 do_cleanups (old_chain);
1243 return PS_OK;
1246 /* Set floating-point regs. */
1248 ps_err_e
1249 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1250 const prfpregset_t *fpregset)
1252 struct cleanup *old_chain;
1254 old_chain = save_inferior_pid ();
1256 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1258 supply_fpregset (fpregset);
1259 if (target_has_execution)
1260 procfs_ops.to_store_registers (-1);
1261 else
1262 orig_core_ops.to_store_registers (-1);
1264 do_cleanups (old_chain);
1266 return PS_OK;
1269 #ifdef TM_I386SOL2_H
1271 /* Get local descriptor table. */
1273 #include <sys/procfs.h>
1274 #include <sys/reg.h>
1275 #include <sys/sysi86.h>
1277 static int nldt_allocated = 0;
1278 static struct ssd *ldt_bufp = NULL;
1280 ps_err_e
1281 ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1282 struct ssd *pldt)
1284 gregset_t gregset;
1285 int lwp_fd;
1286 ps_err_e val;
1287 int nldt;
1288 int i;
1290 /* Get procfs file descriptor for the LWP. */
1291 lwp_fd = procfs_get_pid_fd (BUILD_LWP (lwpid, PIDGET (inferior_pid)));
1292 if (lwp_fd < 0)
1293 return PS_BADLID;
1295 /* Fetch registers und LDT descriptors. */
1296 if (ioctl (lwp_fd, PIOCGREG, &gregset) == -1)
1297 return PS_ERR;
1299 if (ioctl (lwp_fd, PIOCNLDT, &nldt) == -1)
1300 return PS_ERR;
1302 if (nldt_allocated < nldt)
1304 ldt_bufp
1305 = (struct ssd *) xrealloc (ldt_bufp, (nldt + 1) * sizeof (struct ssd));
1306 nldt_allocated = nldt;
1309 if (ioctl (lwp_fd, PIOCLDT, ldt_bufp) == -1)
1310 return PS_ERR;
1312 /* Search LDT for the LWP via register GS. */
1313 for (i = 0; i < nldt; i++)
1315 if (ldt_bufp[i].sel == (gregset[GS] & 0xffff))
1317 *pldt = ldt_bufp[i];
1318 return PS_OK;
1322 /* LDT not found. */
1323 return PS_ERR;
1325 #endif /* TM_I386SOL2_H */
1327 /* Convert a pid to printable form. */
1329 char *
1330 solaris_pid_to_str (pid)
1331 int pid;
1333 static char buf[100];
1335 /* in case init failed to resolve the libthread_db library */
1336 if (!procfs_suppress_run)
1337 return procfs_pid_to_str (pid);
1339 if (is_thread (pid))
1341 int lwp;
1343 lwp = thread_to_lwp (pid, -2);
1345 if (lwp == -1)
1346 sprintf (buf, "Thread %d (defunct)", GET_THREAD (pid));
1347 else if (lwp != -2)
1348 sprintf (buf, "Thread %d (LWP %d)", GET_THREAD (pid), GET_LWP (lwp));
1349 else
1350 sprintf (buf, "Thread %d ", GET_THREAD (pid));
1352 else if (GET_LWP (pid) != 0)
1353 sprintf (buf, "LWP %d ", GET_LWP (pid));
1354 else
1355 sprintf (buf, "process %d ", PIDGET (pid));
1357 return buf;
1361 /* Worker bee for find_new_threads
1362 Callback function that gets called once per USER thread (i.e., not
1363 kernel) thread. */
1365 static int
1366 sol_find_new_threads_callback(th, ignored)
1367 const td_thrhandle_t *th;
1368 void *ignored;
1370 td_err_e retval;
1371 td_thrinfo_t ti;
1372 int pid;
1374 if ((retval = p_td_thr_get_info(th, &ti)) != TD_OK)
1376 return -1;
1378 pid = BUILD_THREAD(ti.ti_tid, PIDGET(inferior_pid));
1379 if (!in_thread_list(pid))
1380 add_thread(pid);
1382 return 0;
1385 void
1386 sol_find_new_threads()
1388 /* don't do anything if init failed to resolve the libthread_db library */
1389 if (!procfs_suppress_run)
1390 return;
1392 if (inferior_pid == -1)
1394 printf_filtered("No process.\n");
1395 return;
1397 p_td_ta_thr_iter (main_ta, sol_find_new_threads_callback, (void *)0,
1398 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1399 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1402 static void
1403 sol_core_open (filename, from_tty)
1404 char *filename;
1405 int from_tty;
1407 orig_core_ops.to_open (filename, from_tty);
1410 static void
1411 sol_core_close (quitting)
1412 int quitting;
1414 orig_core_ops.to_close (quitting);
1417 static void
1418 sol_core_detach (args, from_tty)
1419 char *args;
1420 int from_tty;
1422 unpush_target (&core_ops);
1423 orig_core_ops.to_detach (args, from_tty);
1426 static void
1427 sol_core_files_info (t)
1428 struct target_ops *t;
1430 orig_core_ops.to_files_info (t);
1433 #ifdef MAINTENANCE_CMDS
1434 /* Worker bee for info sol-thread command. This is a callback function that
1435 gets called once for each Solaris thread (ie. not kernel thread) in the
1436 inferior. Print anything interesting that we can think of. */
1438 static int
1439 info_cb (th, s)
1440 const td_thrhandle_t *th;
1441 void *s;
1443 td_err_e ret;
1444 td_thrinfo_t ti;
1445 struct minimal_symbol *msym;
1447 if ((ret = p_td_thr_get_info (th, &ti)) == TD_OK)
1449 printf_filtered ("%s thread #%d, lwp %d, ",
1450 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
1451 ti.ti_tid, ti.ti_lid);
1452 switch (ti.ti_state) {
1453 default:
1454 case TD_THR_UNKNOWN: printf_filtered ("<unknown state>"); break;
1455 case TD_THR_STOPPED: printf_filtered ("(stopped)"); break;
1456 case TD_THR_RUN: printf_filtered ("(run) "); break;
1457 case TD_THR_ACTIVE: printf_filtered ("(active) "); break;
1458 case TD_THR_ZOMBIE: printf_filtered ("(zombie) "); break;
1459 case TD_THR_SLEEP: printf_filtered ("(asleep) "); break;
1460 case TD_THR_STOPPED_ASLEEP:
1461 printf_filtered ("(stopped asleep)"); break;
1463 /* Print thr_create start function: */
1464 if (ti.ti_startfunc != 0)
1465 if (msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc))
1466 printf_filtered (" startfunc: %s\n", SYMBOL_NAME (msym));
1467 else
1468 printf_filtered (" startfunc: 0x%08x\n", ti.ti_startfunc);
1470 /* If thread is asleep, print function that went to sleep: */
1471 if (ti.ti_state == TD_THR_SLEEP)
1472 if (msym = lookup_minimal_symbol_by_pc (ti.ti_pc))
1473 printf_filtered (" - Sleep func: %s\n", SYMBOL_NAME (msym));
1474 else
1475 printf_filtered (" - Sleep func: 0x%08x\n", ti.ti_startfunc);
1477 /* Wrap up line, if necessary */
1478 if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0)
1479 printf_filtered ("\n"); /* don't you hate counting newlines? */
1481 else
1482 warning ("info sol-thread: failed to get info for thread.");
1484 return 0;
1487 /* List some state about each Solaris user thread in the inferior. */
1489 static void
1490 info_solthreads (args, from_tty)
1491 char *args;
1492 int from_tty;
1494 p_td_ta_thr_iter (main_ta, info_cb, args,
1495 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1496 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1498 #endif /* MAINTENANCE_CMDS */
1500 static int
1501 ignore (addr, contents)
1502 CORE_ADDR addr;
1503 char *contents;
1505 return 0;
1508 struct target_ops sol_thread_ops = {
1509 "solaris-threads", /* to_shortname */
1510 "Solaris threads and pthread.", /* to_longname */
1511 "Solaris threads and pthread support.", /* to_doc */
1512 sol_thread_open, /* to_open */
1513 0, /* to_close */
1514 sol_thread_attach, /* to_attach */
1515 NULL, /* to_post_attach */
1516 NULL, /* to_require_attach */
1517 sol_thread_detach, /* to_detach */
1518 NULL, /* to_require_detach */
1519 sol_thread_resume, /* to_resume */
1520 sol_thread_wait, /* to_wait */
1521 NULL, /* to_post_wait */
1522 sol_thread_fetch_registers, /* to_fetch_registers */
1523 sol_thread_store_registers, /* to_store_registers */
1524 sol_thread_prepare_to_store, /* to_prepare_to_store */
1525 sol_thread_xfer_memory, /* to_xfer_memory */
1526 sol_thread_files_info, /* to_files_info */
1527 memory_insert_breakpoint, /* to_insert_breakpoint */
1528 memory_remove_breakpoint, /* to_remove_breakpoint */
1529 terminal_init_inferior, /* to_terminal_init */
1530 terminal_inferior, /* to_terminal_inferior */
1531 terminal_ours_for_output, /* to_terminal_ours_for_output */
1532 terminal_ours, /* to_terminal_ours */
1533 child_terminal_info, /* to_terminal_info */
1534 sol_thread_kill_inferior, /* to_kill */
1535 0, /* to_load */
1536 0, /* to_lookup_symbol */
1537 sol_thread_create_inferior, /* to_create_inferior */
1538 NULL, /* to_post_startup_inferior */
1539 NULL, /* to_acknowledge_created_inferior */
1540 NULL, /* to_clone_and_follow_inferior */
1541 NULL, /* to_post_follow_inferior_by_clone */
1542 NULL, /* to_insert_fork_catchpoint */
1543 NULL, /* to_remove_fork_catchpoint */
1544 NULL, /* to_insert_vfork_catchpoint */
1545 NULL, /* to_remove_vfork_catchpoint */
1546 NULL, /* to_has_forked */
1547 NULL, /* to_has_vforked */
1548 NULL, /* to_can_follow_vfork_prior_to_exec */
1549 NULL, /* to_post_follow_vfork */
1550 NULL, /* to_insert_exec_catchpoint */
1551 NULL, /* to_remove_exec_catchpoint */
1552 NULL, /* to_has_execd */
1553 NULL, /* to_reported_exec_events_per_exec_call */
1554 NULL, /* to_has_syscall_event */
1555 NULL, /* to_has_exited */
1556 sol_thread_mourn_inferior, /* to_mourn_inferior */
1557 sol_thread_can_run, /* to_can_run */
1558 sol_thread_notice_signals, /* to_notice_signals */
1559 sol_thread_alive, /* to_thread_alive */
1560 sol_thread_stop, /* to_stop */
1561 NULL, /* to_enable_exception_callback */
1562 NULL, /* to_get_current_exception_event */
1563 NULL, /* to_pid_to_exec_file */
1564 NULL, /* to_core_file_to_sym_file */
1565 process_stratum, /* to_stratum */
1566 0, /* to_next */
1567 1, /* to_has_all_memory */
1568 1, /* to_has_memory */
1569 1, /* to_has_stack */
1570 1, /* to_has_registers */
1571 1, /* to_has_execution */
1572 0, /* sections */
1573 0, /* sections_end */
1574 OPS_MAGIC /* to_magic */
1577 struct target_ops sol_core_ops = {
1578 "solaris-core", /* to_shortname */
1579 "Solaris core threads and pthread.", /* to_longname */
1580 "Solaris threads and pthread support for core files.", /* to_doc */
1581 sol_core_open, /* to_open */
1582 sol_core_close, /* to_close */
1583 sol_thread_attach, /* XXX to_attach */
1584 NULL, /* to_post_attach */
1585 NULL, /* to_require_attach */
1586 sol_core_detach, /* to_detach */
1587 NULL, /* to_require_detach */
1588 0, /* to_resume */
1589 0, /* to_wait */
1590 NULL, /* to_post_wait */
1591 sol_thread_fetch_registers, /* to_fetch_registers */
1592 0, /* to_store_registers */
1593 0, /* to_prepare_to_store */
1594 sol_thread_xfer_memory, /* XXX to_xfer_memory */
1595 sol_core_files_info, /* to_files_info */
1596 ignore, /* to_insert_breakpoint */
1597 ignore, /* to_remove_breakpoint */
1598 0, /* to_terminal_init */
1599 0, /* to_terminal_inferior */
1600 0, /* to_terminal_ours_for_output */
1601 0, /* to_terminal_ours */
1602 0, /* to_terminal_info */
1603 0, /* to_kill */
1604 0, /* to_load */
1605 0, /* to_lookup_symbol */
1606 sol_thread_create_inferior, /* XXX to_create_inferior */
1607 NULL, /* to_post_startup_inferior */
1608 NULL, /* to_acknowledge_created_inferior */
1609 NULL, /* to_clone_and_follow_inferior */
1610 NULL, /* to_post_follow_inferior_by_clone */
1611 NULL, /* to_insert_fork_catchpoint */
1612 NULL, /* to_remove_fork_catchpoint */
1613 NULL, /* to_insert_vfork_catchpoint */
1614 NULL, /* to_remove_vfork_catchpoint */
1615 NULL, /* to_has_forked */
1616 NULL, /* to_has_vforked */
1617 NULL, /* to_can_follow_vfork_prior_to_exec */
1618 NULL, /* to_post_follow_vfork */
1619 NULL, /* to_insert_exec_catchpoint */
1620 NULL, /* to_remove_exec_catchpoint */
1621 NULL, /* to_has_execd */
1622 NULL, /* to_reported_exec_events_per_exec_call */
1623 NULL, /* to_has_syscall_event */
1624 NULL, /* to_has_exited */
1625 0, /* to_mourn_inferior */
1626 0, /* to_can_run */
1627 0, /* to_notice_signals */
1628 0, /* to_thread_alive */
1629 0, /* to_stop */
1630 NULL, /* to_enable_exception_callback */
1631 NULL, /* to_get_current_exception_event */
1632 NULL, /* to_pid_to_exec_file */
1633 NULL, /* to_core_file_to_sym_file */
1634 core_stratum, /* to_stratum */
1635 0, /* to_next */
1636 0, /* to_has_all_memory */
1637 1, /* to_has_memory */
1638 1, /* to_has_stack */
1639 1, /* to_has_registers */
1640 0, /* to_has_execution */
1641 0, /* sections */
1642 0, /* sections_end */
1643 OPS_MAGIC /* to_magic */
1646 /* we suppress the call to add_target of core_ops in corelow because
1647 if there are two targets in the stratum core_stratum, find_core_target
1648 won't know which one to return. see corelow.c for an additonal
1649 comment on coreops_suppress_target. */
1650 int coreops_suppress_target = 1;
1652 void
1653 _initialize_sol_thread ()
1655 void *dlhandle;
1657 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1658 if (!dlhandle)
1659 goto die;
1661 #define resolve(X) \
1662 if (!(p_##X = dlsym (dlhandle, #X))) \
1663 goto die;
1665 resolve (td_log);
1666 resolve (td_ta_new);
1667 resolve (td_ta_delete);
1668 resolve (td_init);
1669 resolve (td_ta_get_ph);
1670 resolve (td_ta_get_nthreads);
1671 resolve (td_ta_tsd_iter);
1672 resolve (td_ta_thr_iter);
1673 resolve (td_thr_validate);
1674 resolve (td_thr_tsd);
1675 resolve (td_thr_get_info);
1676 resolve (td_thr_getfpregs);
1677 resolve (td_thr_getxregsize);
1678 resolve (td_thr_getxregs);
1679 resolve (td_thr_sigsetmask);
1680 resolve (td_thr_setprio);
1681 resolve (td_thr_setsigpending);
1682 resolve (td_thr_setfpregs);
1683 resolve (td_thr_setxregs);
1684 resolve (td_ta_map_id2thr);
1685 resolve (td_ta_map_lwp2thr);
1686 resolve (td_thr_getgregs);
1687 resolve (td_thr_setgregs);
1689 add_target (&sol_thread_ops);
1691 procfs_suppress_run = 1;
1693 #ifdef MAINTENANCE_CMDS
1694 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1695 "Show info on Solaris user threads.\n", &maintenanceinfolist);
1696 #endif /* MAINTENANCE_CMDS */
1698 memcpy(&orig_core_ops, &core_ops, sizeof (struct target_ops));
1699 memcpy(&core_ops, &sol_core_ops, sizeof (struct target_ops));
1700 add_target (&core_ops);
1702 return;
1704 die:
1706 fprintf_unfiltered (gdb_stderr, "[GDB will not be able to debug user-mode threads: %s]\n", dlerror ());
1708 if (dlhandle)
1709 dlclose (dlhandle);
1711 /* allow the user to debug non-threaded core files */
1712 add_target(&core_ops);
1714 return;