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