import gdb-1999-10-18 snapshot
[deliverable/binutils-gdb.git] / gdb / sol-thread.c
CommitLineData
c906108c
SS
1/* Low level interface for debugging Solaris threads for GDB, the GNU debugger.
2 Copyright 1996, 1997, 1998 Free Software Foundation, Inc.
3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
c906108c
SS
20
21/* This module implements a sort of half target that sits between the
22 machine-independent parts of GDB and the /proc interface (procfs.c) to
23 provide access to the Solaris user-mode thread implementation.
24
25 Solaris threads are true user-mode threads, which are invoked via the thr_*
26 and pthread_* (native and Posix respectivly) interfaces. These are mostly
27 implemented in user-space, with all thread context kept in various
28 structures that live in the user's heap. These should not be confused with
29 lightweight processes (LWPs), which are implemented by the kernel, and
30 scheduled without explicit intervention by the process.
31
32 Just to confuse things a little, Solaris threads (both native and Posix) are
33 actually implemented using LWPs. In general, there are going to be more
34 threads than LWPs. There is no fixed correspondence between a thread and an
35 LWP. When a thread wants to run, it gets scheduled onto the first available
36 LWP and can therefore migrate from one LWP to another as time goes on. A
37 sleeping thread may not be associated with an LWP at all!
38
39 To make it possible to mess with threads, Sun provides a library called
40 libthread_db.so.1 (not to be confused with libthread_db.so.0, which doesn't
41 have a published interface). This interface has an upper part, which it
42 provides, and a lower part which I provide. The upper part consists of the
43 td_* routines, which allow me to find all the threads, query their state,
44 etc... The lower part consists of all of the ps_*, which are used by the
45 td_* routines to read/write memory, manipulate LWPs, lookup symbols, etc...
46 The ps_* routines actually do most of their work by calling functions in
47 procfs.c. */
48
49#include "defs.h"
50#include <thread.h>
51#include <proc_service.h>
52#include <thread_db.h>
53#include "gdbthread.h"
54#include "target.h"
55#include "inferior.h"
56#include <fcntl.h>
c906108c
SS
57#include <sys/stat.h>
58#include <dlfcn.h>
59#include "gdbcmd.h"
60
c5aa993b
JM
61extern struct target_ops sol_thread_ops; /* Forward declaration */
62extern struct target_ops sol_core_ops; /* Forward declaration */
c906108c
SS
63
64/* place to store core_ops before we overwrite it */
65static struct target_ops orig_core_ops;
66
67struct target_ops sol_thread_ops;
68struct target_ops sol_core_ops;
69
70extern int procfs_suppress_run;
c5aa993b
JM
71extern struct target_ops procfs_ops; /* target vector for procfs.c */
72extern struct target_ops core_ops; /* target vector for corelow.c */
c906108c
SS
73extern char *procfs_pid_to_str PARAMS ((int pid));
74
75/* Note that these prototypes differ slightly from those used in procfs.c
76 for of two reasons. One, we can't use gregset_t, as that's got a whole
77 different meaning under Solaris (also, see above). Two, we can't use the
78 pointer form here as these are actually arrays of ints (for Sparc's at
79 least), and are automatically coerced into pointers to ints when used as
80 parameters. That makes it impossible to avoid a compiler warning when
81 passing pr{g fp}regset_t's from a parameter to an argument of one of
82 these functions. */
83
84extern void supply_gregset PARAMS ((const prgregset_t));
85extern void fill_gregset PARAMS ((prgregset_t, int));
86extern void supply_fpregset PARAMS ((const prfpregset_t *));
87extern void fill_fpregset PARAMS ((prfpregset_t *, int));
88
89/* This struct is defined by us, but mainly used for the proc_service interface.
90 We don't have much use for it, except as a handy place to get a real pid
91 for memory accesses. */
92
93struct ps_prochandle
c5aa993b
JM
94 {
95 pid_t pid;
96 };
c906108c
SS
97
98struct string_map
c5aa993b
JM
99 {
100 int num;
101 char *str;
102 };
c906108c
SS
103
104static struct ps_prochandle main_ph;
105static td_thragent_t *main_ta;
106static int sol_thread_active = 0;
107
c5aa993b 108static struct cleanup *save_inferior_pid PARAMS ((void));
7a292a7a 109static void restore_inferior_pid PARAMS ((void *pid));
c906108c
SS
110static char *td_err_string PARAMS ((td_err_e errcode));
111static char *td_state_string PARAMS ((td_thr_state_e statecode));
c5aa993b 112static int thread_to_lwp PARAMS ((int thread_id, int default_lwp));
c906108c
SS
113static void sol_thread_resume PARAMS ((int pid, int step,
114 enum target_signal signo));
115static int lwp_to_thread PARAMS ((int lwp));
116static int sol_thread_alive PARAMS ((int pid));
117static void sol_core_close PARAMS ((int quitting));
118
119static void init_sol_thread_ops PARAMS ((void));
120static void init_sol_core_ops PARAMS ((void));
121
d4f3574e
SS
122/* Default definitions: These must be defined in tm.h
123 if they are to be shared with a process module such as procfs. */
124
125#define THREAD_FLAG 0x80000000
126#define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
127#define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
128#define GET_LWP(PID) TIDGET (PID)
129#define GET_THREAD(PID) (((PID) >> 16) & 0x7fff)
130#define BUILD_LWP(TID, PID) ((TID) << 16 | (PID))
131
c906108c
SS
132#define BUILD_THREAD(THREAD_ID, PID) (THREAD_FLAG | BUILD_LWP (THREAD_ID, PID))
133
134/* Pointers to routines from lithread_db resolved by dlopen() */
135
136static void
137 (*p_td_log) (const int on_off);
138static td_err_e
c5aa993b 139 (*p_td_ta_new) (const struct ps_prochandle * ph_p, td_thragent_t ** ta_pp);
c906108c 140static td_err_e
c5aa993b 141 (*p_td_ta_delete) (td_thragent_t * ta_p);
c906108c
SS
142static td_err_e
143 (*p_td_init) (void);
144static td_err_e
c5aa993b 145 (*p_td_ta_get_ph) (const td_thragent_t * ta_p, struct ps_prochandle ** ph_pp);
c906108c 146static td_err_e
c5aa993b 147 (*p_td_ta_get_nthreads) (const td_thragent_t * ta_p, int *nthread_p);
c906108c 148static td_err_e
c5aa993b 149 (*p_td_ta_tsd_iter) (const td_thragent_t * ta_p, td_key_iter_f * cb, void *cbdata_p);
c906108c 150static td_err_e
c5aa993b
JM
151 (*p_td_ta_thr_iter) (const td_thragent_t * ta_p, td_thr_iter_f * cb, void *cbdata_p, td_thr_state_e state,
152 int ti_pri, sigset_t * ti_sigmask_p, unsigned ti_user_flags);
c906108c 153static td_err_e
c5aa993b 154 (*p_td_thr_validate) (const td_thrhandle_t * th_p);
c906108c 155static td_err_e
c5aa993b 156 (*p_td_thr_tsd) (const td_thrhandle_t * th_p, const thread_key_t key, void **data_pp);
c906108c 157static td_err_e
c5aa993b 158 (*p_td_thr_get_info) (const td_thrhandle_t * th_p, td_thrinfo_t * ti_p);
c906108c 159static td_err_e
c5aa993b 160 (*p_td_thr_getfpregs) (const td_thrhandle_t * th_p, prfpregset_t * fpregset);
c906108c 161static td_err_e
c5aa993b 162 (*p_td_thr_getxregsize) (const td_thrhandle_t * th_p, int *xregsize);
c906108c 163static td_err_e
c5aa993b 164 (*p_td_thr_getxregs) (const td_thrhandle_t * th_p, const caddr_t xregset);
c906108c 165static td_err_e
c5aa993b 166 (*p_td_thr_sigsetmask) (const td_thrhandle_t * th_p, const sigset_t ti_sigmask);
c906108c 167static td_err_e
c5aa993b 168 (*p_td_thr_setprio) (const td_thrhandle_t * th_p, const int ti_pri);
c906108c 169static td_err_e
c5aa993b 170 (*p_td_thr_setsigpending) (const td_thrhandle_t * th_p, const uchar_t ti_pending_flag, const sigset_t ti_pending);
c906108c 171static td_err_e
c5aa993b 172 (*p_td_thr_setfpregs) (const td_thrhandle_t * th_p, const prfpregset_t * fpregset);
c906108c 173static td_err_e
c5aa993b 174 (*p_td_thr_setxregs) (const td_thrhandle_t * th_p, const caddr_t xregset);
c906108c 175static td_err_e
c5aa993b 176 (*p_td_ta_map_id2thr) (const td_thragent_t * ta_p, thread_t tid, td_thrhandle_t * th_p);
c906108c 177static td_err_e
c5aa993b 178 (*p_td_ta_map_lwp2thr) (const td_thragent_t * ta_p, lwpid_t lwpid, td_thrhandle_t * th_p);
c906108c 179static td_err_e
c5aa993b 180 (*p_td_thr_getgregs) (const td_thrhandle_t * th_p, prgregset_t regset);
c906108c 181static td_err_e
c5aa993b 182 (*p_td_thr_setgregs) (const td_thrhandle_t * th_p, const prgregset_t regset);
c906108c
SS
183\f
184/*
185
c5aa993b 186 LOCAL FUNCTION
c906108c 187
c5aa993b 188 td_err_string - Convert a thread_db error code to a string
c906108c 189
c5aa993b 190 SYNOPSIS
c906108c 191
c5aa993b 192 char * td_err_string (errcode)
c906108c 193
c5aa993b 194 DESCRIPTION
c906108c 195
c5aa993b
JM
196 Return the thread_db error string associated with errcode. If errcode
197 is unknown, then return a message.
c906108c
SS
198
199 */
200
201static char *
202td_err_string (errcode)
203 td_err_e errcode;
204{
205 static struct string_map
c5aa993b
JM
206 td_err_table[] =
207 {
208 {TD_OK, "generic \"call succeeded\""},
209 {TD_ERR, "generic error."},
210 {TD_NOTHR, "no thread can be found to satisfy query"},
211 {TD_NOSV, "no synch. variable can be found to satisfy query"},
212 {TD_NOLWP, "no lwp can be found to satisfy query"},
213 {TD_BADPH, "invalid process handle"},
214 {TD_BADTH, "invalid thread handle"},
215 {TD_BADSH, "invalid synchronization handle"},
216 {TD_BADTA, "invalid thread agent"},
217 {TD_BADKEY, "invalid key"},
218 {TD_NOMSG, "td_thr_event_getmsg() called when there was no message"},
219 {TD_NOFPREGS, "FPU register set not available for given thread"},
220 {TD_NOLIBTHREAD, "application not linked with libthread"},
221 {TD_NOEVENT, "requested event is not supported"},
222 {TD_NOCAPAB, "capability not available"},
223 {TD_DBERR, "Debugger service failed"},
224 {TD_NOAPLIC, "Operation not applicable to"},
225 {TD_NOTSD, "No thread specific data for this thread"},
226 {TD_MALLOC, "Malloc failed"},
227 {TD_PARTIALREG, "Only part of register set was writen/read"},
228 {TD_NOXREGS, "X register set not available for given thread"}
229 };
c906108c
SS
230 const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
231 int i;
232 static char buf[50];
233
234 for (i = 0; i < td_err_size; i++)
235 if (td_err_table[i].num == errcode)
236 return td_err_table[i].str;
c5aa993b 237
c906108c
SS
238 sprintf (buf, "Unknown thread_db error code: %d", errcode);
239
240 return buf;
241}
242\f
243/*
244
c5aa993b 245 LOCAL FUNCTION
c906108c 246
c5aa993b 247 td_state_string - Convert a thread_db state code to a string
c906108c 248
c5aa993b 249 SYNOPSIS
c906108c 250
c5aa993b 251 char * td_state_string (statecode)
c906108c 252
c5aa993b 253 DESCRIPTION
c906108c 254
c5aa993b
JM
255 Return the thread_db state string associated with statecode. If
256 statecode is unknown, then return a message.
c906108c
SS
257
258 */
259
260static char *
261td_state_string (statecode)
262 td_thr_state_e statecode;
263{
264 static struct string_map
c5aa993b
JM
265 td_thr_state_table[] =
266 {
267 {TD_THR_ANY_STATE, "any state"},
268 {TD_THR_UNKNOWN, "unknown"},
269 {TD_THR_STOPPED, "stopped"},
270 {TD_THR_RUN, "run"},
271 {TD_THR_ACTIVE, "active"},
272 {TD_THR_ZOMBIE, "zombie"},
273 {TD_THR_SLEEP, "sleep"},
274 {TD_THR_STOPPED_ASLEEP, "stopped asleep"}
275 };
c906108c
SS
276 const int td_thr_state_table_size = sizeof td_thr_state_table / sizeof (struct string_map);
277 int i;
278 static char buf[50];
279
280 for (i = 0; i < td_thr_state_table_size; i++)
281 if (td_thr_state_table[i].num == statecode)
282 return td_thr_state_table[i].str;
c5aa993b 283
c906108c
SS
284 sprintf (buf, "Unknown thread_db state code: %d", statecode);
285
286 return buf;
287}
288\f
289/*
290
c5aa993b 291 LOCAL FUNCTION
c906108c 292
c5aa993b 293 thread_to_lwp - Convert a Posix or Solaris thread id to a LWP id.
c906108c 294
c5aa993b 295 SYNOPSIS
c906108c 296
c5aa993b 297 int thread_to_lwp (thread_id, default_lwp)
c906108c 298
c5aa993b 299 DESCRIPTION
c906108c 300
c5aa993b
JM
301 This function converts a Posix or Solaris thread id to a lightweight
302 process id. If thread_id is non-existent, that's an error. If it's
303 an inactive thread, then we return default_lwp.
c906108c 304
c5aa993b 305 NOTES
c906108c 306
c5aa993b 307 This function probably shouldn't call error()...
c906108c
SS
308
309 */
310
311static int
312thread_to_lwp (thread_id, default_lwp)
313 int thread_id;
314 int default_lwp;
315{
316 td_thrinfo_t ti;
317 td_thrhandle_t th;
318 td_err_e val;
319
320 if (is_lwp (thread_id))
c5aa993b 321 return thread_id; /* It's already an LWP id */
c906108c
SS
322
323 /* It's a thread. Convert to lwp */
324
325 val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th);
326 if (val == TD_NOTHR)
c5aa993b 327 return -1; /* thread must have terminated */
c906108c
SS
328 else if (val != TD_OK)
329 error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val));
330
331 val = p_td_thr_get_info (&th, &ti);
332 if (val == TD_NOTHR)
c5aa993b 333 return -1; /* thread must have terminated */
c906108c
SS
334 else if (val != TD_OK)
335 error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val));
336
337 if (ti.ti_state != TD_THR_ACTIVE)
338 {
339 if (default_lwp != -1)
340 return default_lwp;
341 error ("thread_to_lwp: thread state not active: %s",
342 td_state_string (ti.ti_state));
343 }
344
345 return BUILD_LWP (ti.ti_lid, PIDGET (thread_id));
346}
347\f
348/*
349
c5aa993b 350 LOCAL FUNCTION
c906108c 351
c5aa993b 352 lwp_to_thread - Convert a LWP id to a Posix or Solaris thread id.
c906108c 353
c5aa993b 354 SYNOPSIS
c906108c 355
c5aa993b 356 int lwp_to_thread (lwp_id)
c906108c 357
c5aa993b 358 DESCRIPTION
c906108c 359
c5aa993b
JM
360 This function converts a lightweight process id to a Posix or Solaris
361 thread id. If thread_id is non-existent, that's an error.
c906108c 362
c5aa993b 363 NOTES
c906108c 364
c5aa993b 365 This function probably shouldn't call error()...
c906108c
SS
366
367 */
368
369static int
370lwp_to_thread (lwp)
371 int lwp;
372{
373 td_thrinfo_t ti;
374 td_thrhandle_t th;
375 td_err_e val;
376
377 if (is_thread (lwp))
378 return lwp; /* It's already a thread id */
379
380 /* It's an lwp. Convert it to a thread id. */
381
382 if (!sol_thread_alive (lwp))
383 return -1; /* defunct lwp */
384
385 val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th);
386 if (val == TD_NOTHR)
c5aa993b 387 return -1; /* thread must have terminated */
c906108c
SS
388 else if (val != TD_OK)
389 error ("lwp_to_thread: td_ta_map_lwp2thr: %s.", td_err_string (val));
390
391 val = p_td_thr_validate (&th);
392 if (val == TD_NOTHR)
393 return lwp; /* libthread doesn't know about it, just return lwp */
394 else if (val != TD_OK)
395 error ("lwp_to_thread: td_thr_validate: %s.", td_err_string (val));
396
397 val = p_td_thr_get_info (&th, &ti);
398 if (val == TD_NOTHR)
c5aa993b 399 return -1; /* thread must have terminated */
c906108c
SS
400 else if (val != TD_OK)
401 error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val));
402
403 return BUILD_THREAD (ti.ti_tid, PIDGET (lwp));
404}
405\f
406/*
407
c5aa993b 408 LOCAL FUNCTION
c906108c 409
c5aa993b
JM
410 save_inferior_pid - Save inferior_pid on the cleanup list
411 restore_inferior_pid - Restore inferior_pid from the cleanup list
c906108c 412
c5aa993b 413 SYNOPSIS
c906108c 414
c5aa993b
JM
415 struct cleanup *save_inferior_pid ()
416 void restore_inferior_pid (int pid)
c906108c 417
c5aa993b 418 DESCRIPTION
c906108c 419
c5aa993b
JM
420 These two functions act in unison to restore inferior_pid in
421 case of an error.
c906108c 422
c5aa993b 423 NOTES
c906108c 424
c5aa993b
JM
425 inferior_pid is a global variable that needs to be changed by many of
426 these routines before calling functions in procfs.c. In order to
427 guarantee that inferior_pid gets restored (in case of errors), you
428 need to call save_inferior_pid before changing it. At the end of the
429 function, you should invoke do_cleanups to restore it.
c906108c
SS
430
431 */
432
433
434static struct cleanup *
435save_inferior_pid ()
436{
c5aa993b 437 return make_cleanup (restore_inferior_pid, (void *) inferior_pid);
c906108c
SS
438}
439
440static void
441restore_inferior_pid (pid)
7a292a7a 442 void *pid;
c906108c 443{
7a292a7a 444 inferior_pid = (int) pid;
c906108c
SS
445}
446\f
447
448/* Most target vector functions from here on actually just pass through to
449 procfs.c, as they don't need to do anything specific for threads. */
450
451
452/* ARGSUSED */
453static void
454sol_thread_open (arg, from_tty)
455 char *arg;
456 int from_tty;
457{
458 procfs_ops.to_open (arg, from_tty);
459}
460
461/* Attach to process PID, then initialize for debugging it
462 and wait for the trace-trap that results from attaching. */
463
464static void
465sol_thread_attach (args, from_tty)
466 char *args;
467 int from_tty;
468{
469 procfs_ops.to_attach (args, from_tty);
470 /* Must get symbols from solibs before libthread_db can run! */
c5aa993b 471 SOLIB_ADD ((char *) 0, from_tty, (struct target_ops *) 0);
c906108c
SS
472 if (sol_thread_active)
473 {
474 printf_filtered ("sol-thread active.\n");
c5aa993b 475 main_ph.pid = inferior_pid; /* Save for xfer_memory */
c906108c
SS
476 push_target (&sol_thread_ops);
477 inferior_pid = lwp_to_thread (inferior_pid);
478 if (inferior_pid == -1)
479 inferior_pid = main_ph.pid;
480 else
481 add_thread (inferior_pid);
482 }
483 /* XXX - might want to iterate over all the threads and register them. */
484}
485
486/* Take a program previously attached to and detaches it.
487 The program resumes execution and will no longer stop
488 on signals, etc. We'd better not have left any breakpoints
489 in the program or it'll die when it hits one. For this
490 to work, it may be necessary for the process to have been
491 previously attached. It *might* work if the program was
492 started via the normal ptrace (PTRACE_TRACEME). */
493
494static void
495sol_thread_detach (args, from_tty)
496 char *args;
497 int from_tty;
498{
d4f3574e 499 inferior_pid = PIDGET (main_ph.pid);
c906108c
SS
500 unpush_target (&sol_thread_ops);
501 procfs_ops.to_detach (args, from_tty);
502}
503
504/* Resume execution of process PID. If STEP is nozero, then
505 just single step it. If SIGNAL is nonzero, restart it with that
506 signal activated. We may have to convert pid from a thread-id to an LWP id
507 for procfs. */
508
509static void
510sol_thread_resume (pid, step, signo)
511 int pid;
512 int step;
513 enum target_signal signo;
514{
515 struct cleanup *old_chain;
516
517 old_chain = save_inferior_pid ();
518
519 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
520 if (inferior_pid == -1)
521 inferior_pid = procfs_first_available ();
522
523 if (pid != -1)
524 {
525 int save_pid = pid;
526
527 pid = thread_to_lwp (pid, -2);
528 if (pid == -2) /* Inactive thread */
529 error ("This version of Solaris can't start inactive threads.");
530 if (info_verbose && pid == -1)
c5aa993b 531 warning ("Specified thread %d seems to have terminated",
c906108c
SS
532 GET_THREAD (save_pid));
533 }
534
535 procfs_ops.to_resume (pid, step, signo);
536
537 do_cleanups (old_chain);
538}
539
540/* Wait for any threads to stop. We may have to convert PID from a thread id
541 to a LWP id, and vice versa on the way out. */
542
543static int
544sol_thread_wait (pid, ourstatus)
545 int pid;
546 struct target_waitstatus *ourstatus;
547{
548 int rtnval;
549 int save_pid;
550 struct cleanup *old_chain;
551
552 save_pid = inferior_pid;
553 old_chain = save_inferior_pid ();
554
555 inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid);
556 if (inferior_pid == -1)
557 inferior_pid = procfs_first_available ();
558
559 if (pid != -1)
560 {
561 int save_pid = pid;
562
563 pid = thread_to_lwp (pid, -2);
564 if (pid == -2) /* Inactive thread */
565 error ("This version of Solaris can't start inactive threads.");
566 if (info_verbose && pid == -1)
c5aa993b 567 warning ("Specified thread %d seems to have terminated",
c906108c
SS
568 GET_THREAD (save_pid));
569 }
570
571 rtnval = procfs_ops.to_wait (pid, ourstatus);
572
573 if (ourstatus->kind != TARGET_WAITKIND_EXITED)
574 {
575 /* Map the LWP of interest back to the appropriate thread ID */
576 rtnval = lwp_to_thread (rtnval);
577 if (rtnval == -1)
578 rtnval = save_pid;
579
580 /* See if we have a new thread */
581 if (is_thread (rtnval)
582 && rtnval != save_pid
583 && !in_thread_list (rtnval))
584 {
585 printf_filtered ("[New %s]\n", target_pid_to_str (rtnval));
586 add_thread (rtnval);
587 }
588 }
589
590 /* During process initialization, we may get here without the thread package
591 being initialized, since that can only happen after we've found the shared
592 libs. */
593
594 do_cleanups (old_chain);
595
596 return rtnval;
597}
598
599static void
600sol_thread_fetch_registers (regno)
601 int regno;
602{
603 thread_t thread;
604 td_thrhandle_t thandle;
605 td_err_e val;
606 prgregset_t gregset;
607 prfpregset_t fpregset;
608#if 0
609 int xregsize;
610 caddr_t xregset;
611#endif
612
613 if (!is_thread (inferior_pid))
c5aa993b 614 { /* LWP: pass the request on to procfs.c */
c906108c
SS
615 if (target_has_execution)
616 procfs_ops.to_fetch_registers (regno);
617 else
618 orig_core_ops.to_fetch_registers (regno);
619 return;
620 }
621
622 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
623
624 thread = GET_THREAD (inferior_pid);
625
626 if (thread == 0)
627 error ("sol_thread_fetch_registers: thread == 0");
628
629 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
630 if (val != TD_OK)
631 error ("sol_thread_fetch_registers: td_ta_map_id2thr: %s",
632 td_err_string (val));
633
634 /* Get the integer regs */
635
636 val = p_td_thr_getgregs (&thandle, gregset);
637 if (val != TD_OK
638 && val != TD_PARTIALREG)
639 error ("sol_thread_fetch_registers: td_thr_getgregs %s",
640 td_err_string (val));
641
642 /* For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, pc and sp
643 are saved (by a thread context switch). */
644
645 /* And, now the fp regs */
646
647 val = p_td_thr_getfpregs (&thandle, &fpregset);
648 if (val != TD_OK
649 && val != TD_NOFPREGS)
650 error ("sol_thread_fetch_registers: td_thr_getfpregs %s",
651 td_err_string (val));
652
653/* Note that we must call supply_{g fp}regset *after* calling the td routines
654 because the td routines call ps_lget* which affect the values stored in the
655 registers array. */
656
657 supply_gregset (gregset);
658 supply_fpregset (&fpregset);
659
660#if 0
661/* thread_db doesn't seem to handle this right */
662 val = td_thr_getxregsize (&thandle, &xregsize);
663 if (val != TD_OK && val != TD_NOXREGS)
664 error ("sol_thread_fetch_registers: td_thr_getxregsize %s",
665 td_err_string (val));
666
667 if (val == TD_OK)
668 {
669 xregset = alloca (xregsize);
670 val = td_thr_getxregs (&thandle, xregset);
671 if (val != TD_OK)
672 error ("sol_thread_fetch_registers: td_thr_getxregs %s",
673 td_err_string (val));
674 }
675#endif
676}
677
678static void
679sol_thread_store_registers (regno)
680 int regno;
681{
682 thread_t thread;
683 td_thrhandle_t thandle;
684 td_err_e val;
685 prgregset_t regset;
686 prfpregset_t fpregset;
687#if 0
688 int xregsize;
689 caddr_t xregset;
690#endif
691
692 if (!is_thread (inferior_pid))
c5aa993b 693 { /* LWP: pass the request on to procfs.c */
c906108c
SS
694 procfs_ops.to_store_registers (regno);
695 return;
696 }
697
698 /* Solaris thread: convert inferior_pid into a td_thrhandle_t */
699
700 thread = GET_THREAD (inferior_pid);
701
702 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
703 if (val != TD_OK)
704 error ("sol_thread_store_registers: td_ta_map_id2thr %s",
705 td_err_string (val));
706
707 if (regno != -1)
708 { /* Not writing all the regs */
709 /* save new register value */
710 char old_value[REGISTER_SIZE];
c5aa993b 711 memcpy (old_value, &registers[REGISTER_BYTE (regno)], REGISTER_SIZE);
c906108c
SS
712
713 val = p_td_thr_getgregs (&thandle, regset);
714 if (val != TD_OK)
715 error ("sol_thread_store_registers: td_thr_getgregs %s",
716 td_err_string (val));
717 val = p_td_thr_getfpregs (&thandle, &fpregset);
718 if (val != TD_OK)
719 error ("sol_thread_store_registers: td_thr_getfpregs %s",
720 td_err_string (val));
721
722 /* restore new register value */
c5aa993b 723 memcpy (&registers[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE);
c906108c
SS
724
725#if 0
726/* thread_db doesn't seem to handle this right */
727 val = td_thr_getxregsize (&thandle, &xregsize);
728 if (val != TD_OK && val != TD_NOXREGS)
729 error ("sol_thread_store_registers: td_thr_getxregsize %s",
730 td_err_string (val));
731
732 if (val == TD_OK)
733 {
734 xregset = alloca (xregsize);
735 val = td_thr_getxregs (&thandle, xregset);
736 if (val != TD_OK)
737 error ("sol_thread_store_registers: td_thr_getxregs %s",
738 td_err_string (val));
739 }
740#endif
741 }
742
743 fill_gregset (regset, regno);
744 fill_fpregset (&fpregset, regno);
745
746 val = p_td_thr_setgregs (&thandle, regset);
747 if (val != TD_OK)
748 error ("sol_thread_store_registers: td_thr_setgregs %s",
749 td_err_string (val));
750 val = p_td_thr_setfpregs (&thandle, &fpregset);
751 if (val != TD_OK)
752 error ("sol_thread_store_registers: td_thr_setfpregs %s",
753 td_err_string (val));
754
755#if 0
756/* thread_db doesn't seem to handle this right */
757 val = td_thr_getxregsize (&thandle, &xregsize);
758 if (val != TD_OK && val != TD_NOXREGS)
759 error ("sol_thread_store_registers: td_thr_getxregsize %s",
760 td_err_string (val));
761
762 /* Should probably do something about writing the xregs here, but what are
763 they? */
764#endif
765}
766
767/* Get ready to modify the registers array. On machines which store
768 individual registers, this doesn't need to do anything. On machines
769 which store all the registers in one fell swoop, this makes sure
770 that registers contains all the registers from the program being
771 debugged. */
772
773static void
774sol_thread_prepare_to_store ()
775{
776 procfs_ops.to_prepare_to_store ();
777}
778
779static int
780sol_thread_xfer_memory (memaddr, myaddr, len, dowrite, target)
781 CORE_ADDR memaddr;
782 char *myaddr;
783 int len;
784 int dowrite;
c5aa993b 785 struct target_ops *target; /* ignored */
c906108c
SS
786{
787 int retval;
788 struct cleanup *old_chain;
789
790 old_chain = save_inferior_pid ();
791
c5aa993b 792 if (is_thread (inferior_pid) || /* A thread */
c906108c
SS
793 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
794 inferior_pid = procfs_first_available (); /* Find any live lwp. */
795 /* Note: don't need to call switch_to_thread; we're just reading memory. */
796
797 if (target_has_execution)
798 retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
799 else
800 retval = orig_core_ops.to_xfer_memory (memaddr, myaddr, len,
801 dowrite, target);
802
803 do_cleanups (old_chain);
804
805 return retval;
806}
807
808/* Print status information about what we're accessing. */
809
810static void
811sol_thread_files_info (ignore)
812 struct target_ops *ignore;
813{
814 procfs_ops.to_files_info (ignore);
815}
816
817static void
818sol_thread_kill_inferior ()
819{
820 procfs_ops.to_kill ();
821}
822
823static void
824sol_thread_notice_signals (pid)
825 int pid;
826{
827 procfs_ops.to_notice_signals (PIDGET (pid));
828}
829
830/* Fork an inferior process, and start debugging it with /proc. */
831
832static void
833sol_thread_create_inferior (exec_file, allargs, env)
834 char *exec_file;
835 char *allargs;
836 char **env;
837{
838 procfs_ops.to_create_inferior (exec_file, allargs, env);
839
840 if (sol_thread_active && inferior_pid != 0)
841 {
c5aa993b 842 main_ph.pid = inferior_pid; /* Save for xfer_memory */
c906108c
SS
843
844 push_target (&sol_thread_ops);
845
846 inferior_pid = lwp_to_thread (inferior_pid);
847 if (inferior_pid == -1)
848 inferior_pid = main_ph.pid;
849
850 add_thread (inferior_pid);
851 }
852}
853
854/* This routine is called whenever a new symbol table is read in, or when all
855 symbol tables are removed. libthread_db can only be initialized when it
856 finds the right variables in libthread.so. Since it's a shared library,
857 those variables don't show up until the library gets mapped and the symbol
858 table is read in. */
859
860void
861sol_thread_new_objfile (objfile)
862 struct objfile *objfile;
863{
864 td_err_e val;
865
866 if (!objfile)
867 {
868 sol_thread_active = 0;
869
870 return;
871 }
872
873 /* don't do anything if init failed to resolve the libthread_db library */
874 if (!procfs_suppress_run)
875 return;
876
877 /* Now, initialize the thread debugging library. This needs to be done after
878 the shared libraries are located because it needs information from the
879 user's thread library. */
880
881 val = p_td_init ();
882 if (val != TD_OK)
883 error ("target_new_objfile: td_init: %s", td_err_string (val));
884
885 val = p_td_ta_new (&main_ph, &main_ta);
886 if (val == TD_NOLIBTHREAD)
887 return;
888 else if (val != TD_OK)
889 error ("target_new_objfile: td_ta_new: %s", td_err_string (val));
890
891 sol_thread_active = 1;
892}
893
894/* Clean up after the inferior dies. */
895
896static void
897sol_thread_mourn_inferior ()
898{
899 unpush_target (&sol_thread_ops);
900 procfs_ops.to_mourn_inferior ();
901}
902
903/* Mark our target-struct as eligible for stray "run" and "attach" commands. */
904
905static int
906sol_thread_can_run ()
907{
908 return procfs_suppress_run;
909}
910
911/*
912
c5aa993b 913 LOCAL FUNCTION
c906108c 914
c5aa993b 915 sol_thread_alive - test thread for "aliveness"
c906108c 916
c5aa993b 917 SYNOPSIS
c906108c 918
c5aa993b 919 static bool sol_thread_alive (int pid);
c906108c 920
c5aa993b 921 DESCRIPTION
c906108c 922
c5aa993b 923 returns true if thread still active in inferior.
c906108c
SS
924
925 */
926
927static int
928sol_thread_alive (pid)
929 int pid;
930{
931 if (is_thread (pid)) /* non-kernel thread */
932 {
933 td_err_e val;
934 td_thrhandle_t th;
935
936 pid = GET_THREAD (pid);
937 if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK)
c5aa993b 938 return 0; /* thread not found */
c906108c 939 if ((val = p_td_thr_validate (&th)) != TD_OK)
c5aa993b
JM
940 return 0; /* thread not valid */
941 return 1; /* known thread: return true */
c906108c 942 }
c5aa993b
JM
943 else
944 /* kernel thread (LWP): let procfs test it */
c906108c
SS
945 {
946 if (target_has_execution)
947 return procfs_ops.to_thread_alive (pid);
948 else
949 return orig_core_ops.to_thread_alive (pid);
950 }
951}
952
953static void
954sol_thread_stop ()
955{
956 procfs_ops.to_stop ();
957}
958\f
959/* These routines implement the lower half of the thread_db interface. Ie: the
960 ps_* routines. */
961
962/* Various versions of <proc_service.h> have slightly
963 different function prototypes. In particular, we have
964
c5aa993b
JM
965 NEWER OLDER
966 struct ps_prochandle * const struct ps_prochandle *
967 void* char*
968 const void* char*
969 int size_t
c906108c
SS
970
971 Which one you have depends on solaris version and what
972 patches you've applied. On the theory that there are
973 only two major variants, we have configure check the
974 prototype of ps_pdwrite (), and use that info to make
975 appropriate typedefs here. */
976
977#ifdef PROC_SERVICE_IS_OLD
c5aa993b
JM
978typedef const struct ps_prochandle *gdb_ps_prochandle_t;
979typedef char *gdb_ps_read_buf_t;
980typedef char *gdb_ps_write_buf_t;
c906108c
SS
981typedef int gdb_ps_size_t;
982#else
c5aa993b
JM
983typedef struct ps_prochandle *gdb_ps_prochandle_t;
984typedef void *gdb_ps_read_buf_t;
985typedef const void *gdb_ps_write_buf_t;
c906108c
SS
986typedef size_t gdb_ps_size_t;
987#endif
988
989
990/* The next four routines are called by thread_db to tell us to stop and stop
991 a particular process or lwp. Since GDB ensures that these are all stopped
992 by the time we call anything in thread_db, these routines need to do
993 nothing. */
994
d4f3574e
SS
995/* Process stop */
996
c906108c
SS
997ps_err_e
998ps_pstop (gdb_ps_prochandle_t ph)
999{
1000 return PS_OK;
1001}
1002
d4f3574e
SS
1003/* Process continue */
1004
c906108c
SS
1005ps_err_e
1006ps_pcontinue (gdb_ps_prochandle_t ph)
1007{
1008 return PS_OK;
1009}
1010
d4f3574e
SS
1011/* LWP stop */
1012
c906108c
SS
1013ps_err_e
1014ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1015{
1016 return PS_OK;
1017}
1018
d4f3574e
SS
1019/* LWP continue */
1020
c906108c
SS
1021ps_err_e
1022ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
1023{
1024 return PS_OK;
1025}
1026
d4f3574e
SS
1027/* Looks up the symbol LD_SYMBOL_NAME in the debugger's symbol table. */
1028
c906108c
SS
1029ps_err_e
1030ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *ld_object_name,
c5aa993b 1031 const char *ld_symbol_name, paddr_t * ld_symbol_addr)
c906108c
SS
1032{
1033 struct minimal_symbol *ms;
1034
1035 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
1036
1037 if (!ms)
1038 return PS_NOSYM;
1039
1040 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
1041
1042 return PS_OK;
1043}
1044
1045/* Common routine for reading and writing memory. */
1046
1047static ps_err_e
1048rw_common (int dowrite, const struct ps_prochandle *ph, paddr_t addr,
1049 char *buf, int size)
1050{
1051 struct cleanup *old_chain;
1052
1053 old_chain = save_inferior_pid ();
1054
c5aa993b 1055 if (is_thread (inferior_pid) || /* A thread */
c906108c
SS
1056 !target_thread_alive (inferior_pid)) /* An lwp, but not alive */
1057 inferior_pid = procfs_first_available (); /* Find any live lwp. */
1058 /* Note: don't need to call switch_to_thread; we're just reading memory. */
1059
1060 while (size > 0)
1061 {
1062 int cc;
1063
1064 if (target_has_execution)
1065 cc = procfs_ops.to_xfer_memory (addr, buf, size, dowrite, &procfs_ops);
1066 else
1067 cc = orig_core_ops.to_xfer_memory (addr, buf, size, dowrite, &core_ops);
1068
1069 if (cc < 0)
1070 {
1071 if (dowrite == 0)
1072 print_sys_errmsg ("rw_common (): read", errno);
1073 else
1074 print_sys_errmsg ("rw_common (): write", errno);
1075
1076 do_cleanups (old_chain);
1077
1078 return PS_ERR;
1079 }
1080 size -= cc;
1081 buf += cc;
1082 }
1083
1084 do_cleanups (old_chain);
1085
1086 return PS_OK;
1087}
1088
d4f3574e
SS
1089/* Copies SIZE bytes from target process .data segment to debugger memory. */
1090
c906108c
SS
1091ps_err_e
1092ps_pdread (gdb_ps_prochandle_t ph, paddr_t addr,
1093 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1094{
1095 return rw_common (0, ph, addr, buf, size);
1096}
1097
d4f3574e
SS
1098/* Copies SIZE bytes from debugger memory .data segment to target process. */
1099
c906108c
SS
1100ps_err_e
1101ps_pdwrite (gdb_ps_prochandle_t ph, paddr_t addr,
1102 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1103{
c5aa993b 1104 return rw_common (1, ph, addr, (char *) buf, size);
c906108c
SS
1105}
1106
d4f3574e
SS
1107/* Copies SIZE bytes from target process .text segment to debugger memory. */
1108
c906108c
SS
1109ps_err_e
1110ps_ptread (gdb_ps_prochandle_t ph, paddr_t addr,
1111 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
1112{
1113 return rw_common (0, ph, addr, buf, size);
1114}
1115
d4f3574e
SS
1116/* Copies SIZE bytes from debugger memory .text segment to target process. */
1117
c906108c
SS
1118ps_err_e
1119ps_ptwrite (gdb_ps_prochandle_t ph, paddr_t addr,
1120 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
1121{
c5aa993b 1122 return rw_common (1, ph, addr, (char *) buf, size);
c906108c
SS
1123}
1124
d4f3574e 1125/* Get integer regs for LWP */
c906108c
SS
1126
1127ps_err_e
1128ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1129 prgregset_t gregset)
1130{
1131 struct cleanup *old_chain;
1132
1133 old_chain = save_inferior_pid ();
1134
1135 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
c5aa993b 1136
c906108c
SS
1137 if (target_has_execution)
1138 procfs_ops.to_fetch_registers (-1);
1139 else
1140 orig_core_ops.to_fetch_registers (-1);
1141 fill_gregset (gregset, -1);
1142
1143 do_cleanups (old_chain);
1144
1145 return PS_OK;
1146}
1147
d4f3574e 1148/* Set integer regs for LWP */
c906108c
SS
1149
1150ps_err_e
1151ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1152 const prgregset_t gregset)
1153{
1154 struct cleanup *old_chain;
1155
1156 old_chain = save_inferior_pid ();
1157
1158 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
c5aa993b 1159
c906108c
SS
1160 supply_gregset (gregset);
1161 if (target_has_execution)
1162 procfs_ops.to_store_registers (-1);
1163 else
1164 orig_core_ops.to_store_registers (-1);
1165
1166 do_cleanups (old_chain);
1167
1168 return PS_OK;
1169}
1170
d4f3574e
SS
1171/* Log a message (sends to gdb_stderr). */
1172
c906108c 1173void
c5aa993b 1174ps_plog (const char *fmt,...)
c906108c
SS
1175{
1176 va_list args;
1177
1178 va_start (args, fmt);
1179
1180 vfprintf_filtered (gdb_stderr, fmt, args);
1181}
1182
1183/* Get size of extra register set. Currently a noop. */
1184
1185ps_err_e
1186ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
1187{
1188#if 0
1189 int lwp_fd;
1190 int regsize;
1191 ps_err_e val;
1192
1193 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1194 if (val != PS_OK)
1195 return val;
1196
1197 if (ioctl (lwp_fd, PIOCGXREGSIZE, &regsize))
1198 {
1199 if (errno == EINVAL)
1200 return PS_NOFREGS; /* XXX Wrong code, but this is the closest
1201 thing in proc_service.h */
1202
1203 print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno);
1204 return PS_ERR;
1205 }
1206#endif
1207
1208 return PS_OK;
1209}
1210
1211/* Get extra register set. Currently a noop. */
1212
1213ps_err_e
1214ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1215{
1216#if 0
1217 int lwp_fd;
1218 ps_err_e val;
1219
1220 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1221 if (val != PS_OK)
1222 return val;
1223
1224 if (ioctl (lwp_fd, PIOCGXREG, xregset))
1225 {
1226 print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno);
1227 return PS_ERR;
1228 }
1229#endif
1230
1231 return PS_OK;
1232}
1233
1234/* Set extra register set. Currently a noop. */
1235
1236ps_err_e
1237ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
1238{
1239#if 0
1240 int lwp_fd;
1241 ps_err_e val;
1242
1243 val = get_lwp_fd (ph, lwpid, &lwp_fd);
1244 if (val != PS_OK)
1245 return val;
1246
1247 if (ioctl (lwp_fd, PIOCSXREG, xregset))
1248 {
1249 print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno);
1250 return PS_ERR;
1251 }
1252#endif
1253
1254 return PS_OK;
1255}
1256
d4f3574e 1257/* Get floating-point regs for LWP */
c906108c
SS
1258
1259ps_err_e
1260ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
c5aa993b 1261 prfpregset_t * fpregset)
c906108c
SS
1262{
1263 struct cleanup *old_chain;
1264
1265 old_chain = save_inferior_pid ();
1266
1267 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
1268
1269 if (target_has_execution)
1270 procfs_ops.to_fetch_registers (-1);
1271 else
1272 orig_core_ops.to_fetch_registers (-1);
1273 fill_fpregset (fpregset, -1);
1274
1275 do_cleanups (old_chain);
1276
1277 return PS_OK;
1278}
1279
d4f3574e 1280/* Set floating-point regs for LWP */
c906108c
SS
1281
1282ps_err_e
1283ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
c5aa993b 1284 const prfpregset_t * fpregset)
c906108c
SS
1285{
1286 struct cleanup *old_chain;
1287
1288 old_chain = save_inferior_pid ();
1289
1290 inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid));
c5aa993b 1291
c906108c
SS
1292 supply_fpregset (fpregset);
1293 if (target_has_execution)
1294 procfs_ops.to_store_registers (-1);
1295 else
1296 orig_core_ops.to_store_registers (-1);
1297
1298 do_cleanups (old_chain);
1299
1300 return PS_OK;
1301}
1302
1303#ifdef TM_I386SOL2_H
1304
1305/* Get local descriptor table. */
1306
1307#include <sys/procfs.h>
1308#include <sys/reg.h>
1309#include <sys/sysi86.h>
1310
1311static int nldt_allocated = 0;
1312static struct ssd *ldt_bufp = NULL;
1313
d4f3574e
SS
1314/* Reads the local descriptor table of a LWP. */
1315
c906108c
SS
1316ps_err_e
1317ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
1318 struct ssd *pldt)
1319{
1320 gregset_t gregset;
1321 int lwp_fd;
1322 ps_err_e val;
1323 int nldt;
1324 int i;
1325
1326 /* Get procfs file descriptor for the LWP. */
1327 lwp_fd = procfs_get_pid_fd (BUILD_LWP (lwpid, PIDGET (inferior_pid)));
1328 if (lwp_fd < 0)
1329 return PS_BADLID;
1330
1331 /* Fetch registers und LDT descriptors. */
1332 if (ioctl (lwp_fd, PIOCGREG, &gregset) == -1)
1333 return PS_ERR;
1334
1335 if (ioctl (lwp_fd, PIOCNLDT, &nldt) == -1)
1336 return PS_ERR;
1337
1338 if (nldt_allocated < nldt)
1339 {
1340 ldt_bufp
1341 = (struct ssd *) xrealloc (ldt_bufp, (nldt + 1) * sizeof (struct ssd));
1342 nldt_allocated = nldt;
1343 }
1344
1345 if (ioctl (lwp_fd, PIOCLDT, ldt_bufp) == -1)
1346 return PS_ERR;
1347
1348 /* Search LDT for the LWP via register GS. */
1349 for (i = 0; i < nldt; i++)
1350 {
1351 if (ldt_bufp[i].sel == (gregset[GS] & 0xffff))
1352 {
1353 *pldt = ldt_bufp[i];
1354 return PS_OK;
1355 }
1356 }
1357
1358 /* LDT not found. */
1359 return PS_ERR;
c5aa993b 1360}
c906108c
SS
1361#endif /* TM_I386SOL2_H */
1362\f
1363/* Convert a pid to printable form. */
1364
1365char *
1366solaris_pid_to_str (pid)
1367 int pid;
1368{
1369 static char buf[100];
1370
1371 /* in case init failed to resolve the libthread_db library */
1372 if (!procfs_suppress_run)
1373 return procfs_pid_to_str (pid);
1374
1375 if (is_thread (pid))
1376 {
1377 int lwp;
1378
1379 lwp = thread_to_lwp (pid, -2);
1380
1381 if (lwp == -1)
1382 sprintf (buf, "Thread %d (defunct)", GET_THREAD (pid));
1383 else if (lwp != -2)
1384 sprintf (buf, "Thread %d (LWP %d)", GET_THREAD (pid), GET_LWP (lwp));
1385 else
1386 sprintf (buf, "Thread %d ", GET_THREAD (pid));
1387 }
1388 else if (GET_LWP (pid) != 0)
1389 sprintf (buf, "LWP %d ", GET_LWP (pid));
1390 else
1391 sprintf (buf, "process %d ", PIDGET (pid));
1392
1393 return buf;
1394}
1395\f
1396
1397/* Worker bee for find_new_threads
1398 Callback function that gets called once per USER thread (i.e., not
1399 kernel) thread. */
1400
1401static int
c5aa993b 1402sol_find_new_threads_callback (th, ignored)
c906108c
SS
1403 const td_thrhandle_t *th;
1404 void *ignored;
1405{
1406 td_err_e retval;
1407 td_thrinfo_t ti;
1408 int pid;
1409
c5aa993b 1410 if ((retval = p_td_thr_get_info (th, &ti)) != TD_OK)
c906108c
SS
1411 {
1412 return -1;
1413 }
c5aa993b
JM
1414 pid = BUILD_THREAD (ti.ti_tid, PIDGET (inferior_pid));
1415 if (!in_thread_list (pid))
1416 add_thread (pid);
c906108c
SS
1417
1418 return 0;
1419}
1420
d4f3574e 1421static void
b83266a0 1422sol_find_new_threads ()
c906108c
SS
1423{
1424 /* don't do anything if init failed to resolve the libthread_db library */
1425 if (!procfs_suppress_run)
1426 return;
1427
1428 if (inferior_pid == -1)
1429 {
c5aa993b 1430 printf_filtered ("No process.\n");
c906108c
SS
1431 return;
1432 }
c5aa993b 1433 p_td_ta_thr_iter (main_ta, sol_find_new_threads_callback, (void *) 0,
c906108c
SS
1434 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1435 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1436}
1437
1438static void
1439sol_core_open (filename, from_tty)
1440 char *filename;
1441 int from_tty;
1442{
1443 orig_core_ops.to_open (filename, from_tty);
1444}
1445
1446static void
1447sol_core_close (quitting)
1448 int quitting;
1449{
1450 orig_core_ops.to_close (quitting);
1451}
1452
1453static void
1454sol_core_detach (args, from_tty)
1455 char *args;
1456 int from_tty;
1457{
1458 unpush_target (&core_ops);
1459 orig_core_ops.to_detach (args, from_tty);
1460}
1461
1462static void
1463sol_core_files_info (t)
1464 struct target_ops *t;
1465{
1466 orig_core_ops.to_files_info (t);
1467}
1468
c906108c
SS
1469/* Worker bee for info sol-thread command. This is a callback function that
1470 gets called once for each Solaris thread (ie. not kernel thread) in the
1471 inferior. Print anything interesting that we can think of. */
1472
c5aa993b 1473static int
c906108c
SS
1474info_cb (th, s)
1475 const td_thrhandle_t *th;
1476 void *s;
1477{
1478 td_err_e ret;
1479 td_thrinfo_t ti;
1480 struct minimal_symbol *msym;
1481
1482 if ((ret = p_td_thr_get_info (th, &ti)) == TD_OK)
1483 {
c5aa993b
JM
1484 printf_filtered ("%s thread #%d, lwp %d, ",
1485 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
c906108c 1486 ti.ti_tid, ti.ti_lid);
c5aa993b
JM
1487 switch (ti.ti_state)
1488 {
c906108c 1489 default:
c5aa993b
JM
1490 case TD_THR_UNKNOWN:
1491 printf_filtered ("<unknown state>");
1492 break;
1493 case TD_THR_STOPPED:
1494 printf_filtered ("(stopped)");
1495 break;
1496 case TD_THR_RUN:
1497 printf_filtered ("(run) ");
1498 break;
1499 case TD_THR_ACTIVE:
1500 printf_filtered ("(active) ");
1501 break;
1502 case TD_THR_ZOMBIE:
1503 printf_filtered ("(zombie) ");
1504 break;
1505 case TD_THR_SLEEP:
1506 printf_filtered ("(asleep) ");
1507 break;
1508 case TD_THR_STOPPED_ASLEEP:
1509 printf_filtered ("(stopped asleep)");
1510 break;
1511 }
c906108c
SS
1512 /* Print thr_create start function: */
1513 if (ti.ti_startfunc != 0)
1514 if (msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc))
1515 printf_filtered (" startfunc: %s\n", SYMBOL_NAME (msym));
1516 else
1517 printf_filtered (" startfunc: 0x%08x\n", ti.ti_startfunc);
1518
1519 /* If thread is asleep, print function that went to sleep: */
1520 if (ti.ti_state == TD_THR_SLEEP)
1521 if (msym = lookup_minimal_symbol_by_pc (ti.ti_pc))
1522 printf_filtered (" - Sleep func: %s\n", SYMBOL_NAME (msym));
1523 else
1524 printf_filtered (" - Sleep func: 0x%08x\n", ti.ti_startfunc);
1525
1526 /* Wrap up line, if necessary */
1527 if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0)
1528 printf_filtered ("\n"); /* don't you hate counting newlines? */
1529 }
1530 else
1531 warning ("info sol-thread: failed to get info for thread.");
1532
c5aa993b 1533 return 0;
c906108c
SS
1534}
1535
1536/* List some state about each Solaris user thread in the inferior. */
1537
1538static void
1539info_solthreads (args, from_tty)
1540 char *args;
1541 int from_tty;
1542{
c5aa993b 1543 p_td_ta_thr_iter (main_ta, info_cb, args,
c906108c
SS
1544 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1545 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1546}
c906108c
SS
1547
1548static int
1549ignore (addr, contents)
1550 CORE_ADDR addr;
1551 char *contents;
1552{
1553 return 0;
1554}
1555
1556
1557static void
1558init_sol_thread_ops ()
1559{
1560 sol_thread_ops.to_shortname = "solaris-threads";
1561 sol_thread_ops.to_longname = "Solaris threads and pthread.";
1562 sol_thread_ops.to_doc = "Solaris threads and pthread support.";
1563 sol_thread_ops.to_open = sol_thread_open;
1564 sol_thread_ops.to_close = 0;
1565 sol_thread_ops.to_attach = sol_thread_attach;
1566 sol_thread_ops.to_detach = sol_thread_detach;
1567 sol_thread_ops.to_resume = sol_thread_resume;
1568 sol_thread_ops.to_wait = sol_thread_wait;
1569 sol_thread_ops.to_fetch_registers = sol_thread_fetch_registers;
1570 sol_thread_ops.to_store_registers = sol_thread_store_registers;
1571 sol_thread_ops.to_prepare_to_store = sol_thread_prepare_to_store;
1572 sol_thread_ops.to_xfer_memory = sol_thread_xfer_memory;
1573 sol_thread_ops.to_files_info = sol_thread_files_info;
1574 sol_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1575 sol_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1576 sol_thread_ops.to_terminal_init = terminal_init_inferior;
1577 sol_thread_ops.to_terminal_inferior = terminal_inferior;
1578 sol_thread_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1579 sol_thread_ops.to_terminal_ours = terminal_ours;
1580 sol_thread_ops.to_terminal_info = child_terminal_info;
1581 sol_thread_ops.to_kill = sol_thread_kill_inferior;
1582 sol_thread_ops.to_load = 0;
1583 sol_thread_ops.to_lookup_symbol = 0;
1584 sol_thread_ops.to_create_inferior = sol_thread_create_inferior;
1585 sol_thread_ops.to_mourn_inferior = sol_thread_mourn_inferior;
1586 sol_thread_ops.to_can_run = sol_thread_can_run;
1587 sol_thread_ops.to_notice_signals = sol_thread_notice_signals;
1588 sol_thread_ops.to_thread_alive = sol_thread_alive;
b83266a0 1589 sol_thread_ops.to_find_new_threads = sol_find_new_threads;
c906108c
SS
1590 sol_thread_ops.to_stop = sol_thread_stop;
1591 sol_thread_ops.to_stratum = process_stratum;
1592 sol_thread_ops.to_has_all_memory = 1;
1593 sol_thread_ops.to_has_memory = 1;
1594 sol_thread_ops.to_has_stack = 1;
1595 sol_thread_ops.to_has_registers = 1;
1596 sol_thread_ops.to_has_execution = 1;
1597 sol_thread_ops.to_has_thread_control = tc_none;
1598 sol_thread_ops.to_sections = 0;
1599 sol_thread_ops.to_sections_end = 0;
1600 sol_thread_ops.to_magic = OPS_MAGIC;
1601}
1602
1603
1604static void
1605init_sol_core_ops ()
1606{
c5aa993b
JM
1607 sol_core_ops.to_shortname = "solaris-core";
1608 sol_core_ops.to_longname = "Solaris core threads and pthread.";
1609 sol_core_ops.to_doc = "Solaris threads and pthread support for core files.";
1610 sol_core_ops.to_open = sol_core_open;
1611 sol_core_ops.to_close = sol_core_close;
1612 sol_core_ops.to_attach = sol_thread_attach;
1613 sol_core_ops.to_detach = sol_core_detach;
c906108c 1614 /* sol_core_ops.to_resume = 0; */
c5aa993b
JM
1615 /* sol_core_ops.to_wait = 0; */
1616 sol_core_ops.to_fetch_registers = sol_thread_fetch_registers;
c906108c
SS
1617 /* sol_core_ops.to_store_registers = 0; */
1618 /* sol_core_ops.to_prepare_to_store = 0; */
c5aa993b
JM
1619 sol_core_ops.to_xfer_memory = sol_thread_xfer_memory;
1620 sol_core_ops.to_files_info = sol_core_files_info;
1621 sol_core_ops.to_insert_breakpoint = ignore;
1622 sol_core_ops.to_remove_breakpoint = ignore;
c906108c
SS
1623 /* sol_core_ops.to_terminal_init = 0; */
1624 /* sol_core_ops.to_terminal_inferior = 0; */
1625 /* sol_core_ops.to_terminal_ours_for_output = 0; */
1626 /* sol_core_ops.to_terminal_ours = 0; */
1627 /* sol_core_ops.to_terminal_info = 0; */
1628 /* sol_core_ops.to_kill = 0; */
1629 /* sol_core_ops.to_load = 0; */
1630 /* sol_core_ops.to_lookup_symbol = 0; */
c5aa993b
JM
1631 sol_core_ops.to_create_inferior = sol_thread_create_inferior;
1632 sol_core_ops.to_stratum = core_stratum;
1633 sol_core_ops.to_has_all_memory = 0;
1634 sol_core_ops.to_has_memory = 1;
1635 sol_core_ops.to_has_stack = 1;
1636 sol_core_ops.to_has_registers = 1;
1637 sol_core_ops.to_has_execution = 0;
1638 sol_core_ops.to_has_thread_control = tc_none;
1639 sol_core_ops.to_sections = 0;
1640 sol_core_ops.to_sections_end = 0;
1641 sol_core_ops.to_magic = OPS_MAGIC;
c906108c
SS
1642}
1643
1644/* we suppress the call to add_target of core_ops in corelow because
1645 if there are two targets in the stratum core_stratum, find_core_target
1646 won't know which one to return. see corelow.c for an additonal
1647 comment on coreops_suppress_target. */
1648int coreops_suppress_target = 1;
1649
1650void
1651_initialize_sol_thread ()
1652{
1653 void *dlhandle;
1654
1655 init_sol_thread_ops ();
1656 init_sol_core_ops ();
1657
1658 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1659 if (!dlhandle)
1660 goto die;
1661
1662#define resolve(X) \
1663 if (!(p_##X = dlsym (dlhandle, #X))) \
1664 goto die;
1665
1666 resolve (td_log);
1667 resolve (td_ta_new);
1668 resolve (td_ta_delete);
1669 resolve (td_init);
1670 resolve (td_ta_get_ph);
1671 resolve (td_ta_get_nthreads);
1672 resolve (td_ta_tsd_iter);
1673 resolve (td_ta_thr_iter);
1674 resolve (td_thr_validate);
1675 resolve (td_thr_tsd);
1676 resolve (td_thr_get_info);
1677 resolve (td_thr_getfpregs);
1678 resolve (td_thr_getxregsize);
1679 resolve (td_thr_getxregs);
1680 resolve (td_thr_sigsetmask);
1681 resolve (td_thr_setprio);
1682 resolve (td_thr_setsigpending);
1683 resolve (td_thr_setfpregs);
1684 resolve (td_thr_setxregs);
1685 resolve (td_ta_map_id2thr);
1686 resolve (td_ta_map_lwp2thr);
1687 resolve (td_thr_getgregs);
1688 resolve (td_thr_setgregs);
1689
1690 add_target (&sol_thread_ops);
1691
1692 procfs_suppress_run = 1;
1693
c5aa993b
JM
1694 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1695 "Show info on Solaris user threads.\n", &maintenanceinfolist);
c906108c 1696
c5aa993b
JM
1697 memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops));
1698 memcpy (&core_ops, &sol_core_ops, sizeof (struct target_ops));
c906108c
SS
1699 add_target (&core_ops);
1700
1701 return;
1702
c5aa993b 1703die:
c906108c
SS
1704
1705 fprintf_unfiltered (gdb_stderr, "[GDB will not be able to debug user-mode threads: %s]\n", dlerror ());
1706
1707 if (dlhandle)
1708 dlclose (dlhandle);
1709
1710 /* allow the user to debug non-threaded core files */
c5aa993b 1711 add_target (&core_ops);
c906108c
SS
1712
1713 return;
1714}
This page took 0.106956 seconds and 4 git commands to generate.