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