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