arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / sol-thread.c
blobdb86079b3fa667429403c6f1dbf2c66e0a55bb17
1 /* Solaris threads debugging interface.
3 Copyright (C) 1996-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This module implements a sort of half target that sits between the
21 machine-independent parts of GDB and the /proc interface (procfs.c)
22 to provide access to the Solaris user-mode thread implementation.
24 Solaris threads are true user-mode threads, which are invoked via
25 the thr_* and pthread_* (native and POSIX respectively) interfaces.
26 These are mostly implemented in user-space, with all thread context
27 kept in various structures that live in the user's heap. These
28 should not be confused with lightweight processes (LWPs), which are
29 implemented by the kernel, and scheduled without explicit
30 intervention by the process.
32 Just to confuse things a little, Solaris threads (both native and
33 POSIX) are actually implemented using LWPs. In general, there are
34 going to be more threads than LWPs. There is no fixed
35 correspondence between a thread and an LWP. When a thread wants to
36 run, it gets scheduled onto the first available LWP and can
37 therefore migrate from one LWP to another as time goes on. A
38 sleeping thread may not be associated with an LWP at all!
40 To make it possible to mess with threads, Sun provides a library
41 called libthread_db.so.1 (not to be confused with
42 libthread_db.so.0, which doesn't have a published interface). This
43 interface has an upper part, which it provides, and a lower part
44 which we provide. The upper part consists of the td_* routines,
45 which allow us to find all the threads, query their state, etc...
46 The lower part consists of all of the ps_*, which are used by the
47 td_* routines to read/write memory, manipulate LWPs, lookup
48 symbols, etc... The ps_* routines actually do most of their work
49 by calling functions in procfs.c. */
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>
58 #include <sys/stat.h>
59 #include <dlfcn.h>
60 #include "cli/cli-cmds.h"
61 #include "gdbcore.h"
62 #include "regcache.h"
63 #include "solib.h"
64 #include "symfile.h"
65 #include "observable.h"
66 #include "procfs.h"
67 #include "symtab.h"
68 #include "minsyms.h"
69 #include "objfiles.h"
71 static const target_info thread_db_target_info = {
72 "solaris-threads",
73 N_("Solaris threads and pthread."),
74 N_("Solaris threads and pthread support.")
77 class sol_thread_target final : public target_ops
79 public:
80 const target_info &info () const override
81 { return thread_db_target_info; }
83 strata stratum () const override { return thread_stratum; }
85 void detach (inferior *, int) override;
86 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
87 void resume (ptid_t, int, enum gdb_signal) override;
88 void mourn_inferior () override;
89 std::string pid_to_str (ptid_t) override;
90 ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
92 void fetch_registers (struct regcache *, int) override;
93 void store_registers (struct regcache *, int) override;
95 enum target_xfer_status xfer_partial (enum target_object object,
96 const char *annex,
97 gdb_byte *readbuf,
98 const gdb_byte *writebuf,
99 ULONGEST offset, ULONGEST len,
100 ULONGEST *xfered_len) override;
102 bool thread_alive (ptid_t ptid) override;
103 void update_thread_list () override;
106 static sol_thread_target sol_thread_ops;
108 /* Prototypes for supply_gregset etc. */
109 #include "gregset.h"
111 /* This struct is defined by us, but mainly used for the proc_service
112 interface. We don't have much use for it, except as a handy place
113 to get a real PID for memory accesses. */
115 struct ps_prochandle
117 ptid_t ptid;
120 struct string_map
122 int num;
123 const char *str;
126 static struct ps_prochandle main_ph;
127 static td_thragent_t *main_ta;
128 static int sol_thread_active = 0;
130 /* Default definitions: These must be defined in tm.h if they are to
131 be shared with a process module such as procfs. */
133 /* Types of the libthread_db functions. */
135 typedef void (td_log_ftype)(const int on_off);
136 typedef td_err_e (td_ta_new_ftype)(const struct ps_prochandle *ph_p,
137 td_thragent_t **ta_pp);
138 typedef td_err_e (td_ta_delete_ftype)(td_thragent_t *ta_p);
139 typedef td_err_e (td_init_ftype)(void);
140 typedef td_err_e (td_ta_get_ph_ftype)(const td_thragent_t *ta_p,
141 struct ps_prochandle **ph_pp);
142 typedef td_err_e (td_ta_get_nthreads_ftype)(const td_thragent_t *ta_p,
143 int *nthread_p);
144 typedef td_err_e (td_ta_tsd_iter_ftype)(const td_thragent_t *ta_p,
145 td_key_iter_f *cb, void *cbdata_p);
146 typedef td_err_e (td_ta_thr_iter_ftype)(const td_thragent_t *ta_p,
147 td_thr_iter_f *cb, void *cbdata_p,
148 td_thr_state_e state, int ti_pri,
149 sigset_t *ti_sigmask_p,
150 unsigned ti_user_flags);
151 typedef td_err_e (td_thr_validate_ftype)(const td_thrhandle_t *th_p);
152 typedef td_err_e (td_thr_tsd_ftype)(const td_thrhandle_t * th_p,
153 const thread_key_t key, void **data_pp);
154 typedef td_err_e (td_thr_get_info_ftype)(const td_thrhandle_t *th_p,
155 td_thrinfo_t *ti_p);
156 typedef td_err_e (td_thr_getfpregs_ftype)(const td_thrhandle_t *th_p,
157 prfpregset_t *fpregset);
158 typedef td_err_e (td_thr_getxregsize_ftype)(const td_thrhandle_t *th_p,
159 int *xregsize);
160 typedef td_err_e (td_thr_getxregs_ftype)(const td_thrhandle_t *th_p,
161 const caddr_t xregset);
162 typedef td_err_e (td_thr_sigsetmask_ftype)(const td_thrhandle_t *th_p,
163 const sigset_t ti_sigmask);
164 typedef td_err_e (td_thr_setprio_ftype)(const td_thrhandle_t *th_p,
165 const int ti_pri);
166 typedef td_err_e (td_thr_setsigpending_ftype)(const td_thrhandle_t *th_p,
167 const uchar_t ti_pending_flag,
168 const sigset_t ti_pending);
169 typedef td_err_e (td_thr_setfpregs_ftype)(const td_thrhandle_t *th_p,
170 const prfpregset_t *fpregset);
171 typedef td_err_e (td_thr_setxregs_ftype)(const td_thrhandle_t *th_p,
172 const caddr_t xregset);
173 typedef td_err_e (td_ta_map_id2thr_ftype)(const td_thragent_t *ta_p,
174 thread_t tid,
175 td_thrhandle_t *th_p);
176 typedef td_err_e (td_ta_map_lwp2thr_ftype)(const td_thragent_t *ta_p,
177 lwpid_t lwpid,
178 td_thrhandle_t *th_p);
179 typedef td_err_e (td_thr_getgregs_ftype)(const td_thrhandle_t *th_p,
180 prgregset_t regset);
181 typedef td_err_e (td_thr_setgregs_ftype)(const td_thrhandle_t *th_p,
182 const prgregset_t regset);
184 /* Pointers to routines from libthread_db resolved by dlopen(). */
186 static td_log_ftype *p_td_log;
187 static td_ta_new_ftype *p_td_ta_new;
188 static td_ta_delete_ftype *p_td_ta_delete;
189 static td_init_ftype *p_td_init;
190 static td_ta_get_ph_ftype *p_td_ta_get_ph;
191 static td_ta_get_nthreads_ftype *p_td_ta_get_nthreads;
192 static td_ta_tsd_iter_ftype *p_td_ta_tsd_iter;
193 static td_ta_thr_iter_ftype *p_td_ta_thr_iter;
194 static td_thr_validate_ftype *p_td_thr_validate;
195 static td_thr_tsd_ftype *p_td_thr_tsd;
196 static td_thr_get_info_ftype *p_td_thr_get_info;
197 static td_thr_getfpregs_ftype *p_td_thr_getfpregs;
198 static td_thr_getxregsize_ftype *p_td_thr_getxregsize;
199 static td_thr_getxregs_ftype *p_td_thr_getxregs;
200 static td_thr_sigsetmask_ftype *p_td_thr_sigsetmask;
201 static td_thr_setprio_ftype *p_td_thr_setprio;
202 static td_thr_setsigpending_ftype *p_td_thr_setsigpending;
203 static td_thr_setfpregs_ftype *p_td_thr_setfpregs;
204 static td_thr_setxregs_ftype *p_td_thr_setxregs;
205 static td_ta_map_id2thr_ftype *p_td_ta_map_id2thr;
206 static td_ta_map_lwp2thr_ftype *p_td_ta_map_lwp2thr;
207 static td_thr_getgregs_ftype *p_td_thr_getgregs;
208 static td_thr_setgregs_ftype *p_td_thr_setgregs;
211 /* Return the libthread_db error string associated with ERRCODE. If
212 ERRCODE is unknown, return an appropriate message. */
214 static const char *
215 td_err_string (td_err_e errcode)
217 static struct string_map td_err_table[] =
219 { TD_OK, "generic \"call succeeded\"" },
220 { TD_ERR, "generic error." },
221 { TD_NOTHR, "no thread can be found to satisfy query" },
222 { TD_NOSV, "no synch. variable can be found to satisfy query" },
223 { TD_NOLWP, "no lwp can be found to satisfy query" },
224 { TD_BADPH, "invalid process handle" },
225 { TD_BADTH, "invalid thread handle" },
226 { TD_BADSH, "invalid synchronization handle" },
227 { TD_BADTA, "invalid thread agent" },
228 { TD_BADKEY, "invalid key" },
229 { TD_NOMSG, "td_thr_event_getmsg() called when there was no message" },
230 { TD_NOFPREGS, "FPU register set not available for given thread" },
231 { TD_NOLIBTHREAD, "application not linked with libthread" },
232 { TD_NOEVENT, "requested event is not supported" },
233 { TD_NOCAPAB, "capability not available" },
234 { TD_DBERR, "Debugger service failed" },
235 { TD_NOAPLIC, "Operation not applicable to" },
236 { TD_NOTSD, "No thread specific data for this thread" },
237 { TD_MALLOC, "Malloc failed" },
238 { TD_PARTIALREG, "Only part of register set was written/read" },
239 { TD_NOXREGS, "X register set not available for given thread" }
241 const int td_err_size = sizeof td_err_table / sizeof (struct string_map);
242 int i;
243 static char buf[50];
245 for (i = 0; i < td_err_size; i++)
246 if (td_err_table[i].num == errcode)
247 return td_err_table[i].str;
249 xsnprintf (buf, sizeof (buf), "Unknown libthread_db error code: %d",
250 errcode);
252 return buf;
255 /* Return the libthread_db state string associated with STATECODE.
256 If STATECODE is unknown, return an appropriate message. */
258 static const char *
259 td_state_string (td_thr_state_e statecode)
261 static struct string_map td_thr_state_table[] =
263 { TD_THR_ANY_STATE, "any state" },
264 { TD_THR_UNKNOWN, "unknown" },
265 { TD_THR_STOPPED, "stopped" },
266 { TD_THR_RUN, "run" },
267 { TD_THR_ACTIVE, "active" },
268 { TD_THR_ZOMBIE, "zombie" },
269 { TD_THR_SLEEP, "sleep" },
270 { TD_THR_STOPPED_ASLEEP, "stopped asleep" }
272 const int td_thr_state_table_size =
273 sizeof td_thr_state_table / sizeof (struct string_map);
274 int i;
275 static char buf[50];
277 for (i = 0; i < td_thr_state_table_size; i++)
278 if (td_thr_state_table[i].num == statecode)
279 return td_thr_state_table[i].str;
281 xsnprintf (buf, sizeof (buf), "Unknown libthread_db state code: %d",
282 statecode);
284 return buf;
288 /* Convert a POSIX or Solaris thread ID into a LWP ID. If THREAD_ID
289 doesn't exist, that's an error. If it's an inactive thread, return
290 DEFAULT_LWP.
292 NOTE: This function probably shouldn't call error(). */
294 static ptid_t
295 thread_to_lwp (ptid_t thread_id, int default_lwp)
297 td_thrinfo_t ti;
298 td_thrhandle_t th;
299 td_err_e val;
301 if (thread_id.lwp_p ())
302 return thread_id; /* It's already an LWP ID. */
304 /* It's a thread. Convert to LWP. */
306 val = p_td_ta_map_id2thr (main_ta, thread_id.tid (), &th);
307 if (val == TD_NOTHR)
308 return ptid_t (-1); /* Thread must have terminated. */
309 else if (val != TD_OK)
310 error (_("thread_to_lwp: td_ta_map_id2thr %s"), td_err_string (val));
312 val = p_td_thr_get_info (&th, &ti);
313 if (val == TD_NOTHR)
314 return ptid_t (-1); /* Thread must have terminated. */
315 else if (val != TD_OK)
316 error (_("thread_to_lwp: td_thr_get_info: %s"), td_err_string (val));
318 if (ti.ti_state != TD_THR_ACTIVE)
320 if (default_lwp != -1)
321 return ptid_t (default_lwp);
322 error (_("thread_to_lwp: thread state not active: %s"),
323 td_state_string (ti.ti_state));
326 return ptid_t (thread_id.pid (), ti.ti_lid, 0);
329 /* Convert an LWP ID into a POSIX or Solaris thread ID. If LWP_ID
330 doesn't exists, that's an error.
332 NOTE: This function probably shouldn't call error(). */
334 static ptid_t
335 lwp_to_thread (ptid_t lwp)
337 td_thrinfo_t ti;
338 td_thrhandle_t th;
339 td_err_e val;
341 if (lwp.tid_p ())
342 return lwp; /* It's already a thread ID. */
344 /* It's an LWP. Convert it to a thread ID. */
346 if (!target_thread_alive (lwp))
347 return ptid_t (-1); /* Must be a defunct LPW. */
349 val = p_td_ta_map_lwp2thr (main_ta, lwp.lwp (), &th);
350 if (val == TD_NOTHR)
351 return ptid_t (-1); /* Thread must have terminated. */
352 else if (val != TD_OK)
353 error (_("lwp_to_thread: td_ta_map_lwp2thr: %s."), td_err_string (val));
355 val = p_td_thr_validate (&th);
356 if (val == TD_NOTHR)
357 return lwp; /* Unknown to libthread; just return LPW, */
358 else if (val != TD_OK)
359 error (_("lwp_to_thread: td_thr_validate: %s."), td_err_string (val));
361 val = p_td_thr_get_info (&th, &ti);
362 if (val == TD_NOTHR)
363 return ptid_t (-1); /* Thread must have terminated. */
364 else if (val != TD_OK)
365 error (_("lwp_to_thread: td_thr_get_info: %s."), td_err_string (val));
367 return ptid_t (lwp.pid (), 0 , ti.ti_tid);
371 /* Most target vector functions from here on actually just pass
372 through to the layer beneath, as they don't need to do anything
373 specific for threads. */
375 /* Take a program previously attached to and detaches it. The program
376 resumes execution and will no longer stop on signals, etc. We'd
377 better not have left any breakpoints in the program or it'll die
378 when it hits one. For this to work, it may be necessary for the
379 process to have been previously attached. It *might* work if the
380 program was started via the normal ptrace (PTRACE_TRACEME). */
382 void
383 sol_thread_target::detach (inferior *inf, int from_tty)
385 target_ops *beneath = this->beneath ();
387 sol_thread_active = 0;
388 inferior_ptid = ptid_t (main_ph.ptid.pid ());
389 inf->unpush_target (this);
390 beneath->detach (inf, from_tty);
393 /* Resume execution of process PTID. If STEP is nonzero, then just
394 single step it. If SIGNAL is nonzero, restart it with that signal
395 activated. We may have to convert PTID from a thread ID to an LWP
396 ID for procfs. */
398 void
399 sol_thread_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
401 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
403 inferior_ptid = thread_to_lwp (inferior_ptid, main_ph.ptid.pid ());
404 if (inferior_ptid.pid () == -1)
405 inferior_ptid = procfs_first_available ();
407 if (ptid.pid () != -1)
409 ptid_t save_ptid = ptid;
411 ptid = thread_to_lwp (ptid, -2);
412 if (ptid.pid () == -2) /* Inactive thread. */
413 error (_("This version of Solaris can't start inactive threads."));
414 if (info_verbose && ptid.pid () == -1)
415 warning (_("Specified thread %s seems to have terminated"),
416 pulongest (save_ptid.tid ()));
419 beneath ()->resume (ptid, step, signo);
422 /* Wait for any threads to stop. We may have to convert PTID from a
423 thread ID to an LWP ID, and vice versa on the way out. */
425 ptid_t
426 sol_thread_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
427 target_wait_flags options)
429 if (ptid.pid () != -1)
431 ptid_t ptid_for_warning = ptid;
433 ptid = thread_to_lwp (ptid, -2);
434 if (ptid.pid () == -2) /* Inactive thread. */
435 error (_("This version of Solaris can't start inactive threads."));
436 if (info_verbose && ptid.pid () == -1)
437 warning (_("Specified thread %s seems to have terminated"),
438 pulongest (ptid_for_warning.tid ()));
441 ptid_t rtnval = beneath ()->wait (ptid, ourstatus, options);
443 if (ourstatus->kind () != TARGET_WAITKIND_EXITED)
445 /* Map the LWP of interest back to the appropriate thread ID. */
446 ptid_t thr_ptid = lwp_to_thread (rtnval);
447 if (thr_ptid.pid () != -1)
448 rtnval = thr_ptid;
450 /* See if we have a new thread. */
451 if (rtnval.tid_p ())
453 thread_info *thr = current_inferior ()->find_thread (rtnval);
454 if (thr == NULL || thr->state == THREAD_EXITED)
456 process_stratum_target *proc_target
457 = current_inferior ()->process_target ();
458 add_thread (proc_target, rtnval);
463 /* During process initialization, we may get here without the thread
464 package being initialized, since that can only happen after we've
465 found the shared libs. */
467 return rtnval;
470 void
471 sol_thread_target::fetch_registers (struct regcache *regcache, int regnum)
473 thread_t thread;
474 td_thrhandle_t thandle;
475 td_err_e val;
476 prgregset_t gregset;
477 prfpregset_t fpregset;
478 gdb_gregset_t *gregset_p = &gregset;
479 gdb_fpregset_t *fpregset_p = &fpregset;
480 ptid_t ptid = regcache->ptid ();
482 if (!ptid.tid_p ())
484 /* It's an LWP; pass the request on to the layer beneath. */
485 beneath ()->fetch_registers (regcache, regnum);
486 return;
489 /* Solaris thread: convert PTID into a td_thrhandle_t. */
490 thread = ptid.tid ();
491 if (thread == 0)
492 error (_("sol_thread_fetch_registers: thread == 0"));
494 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
495 if (val != TD_OK)
496 error (_("sol_thread_fetch_registers: td_ta_map_id2thr: %s"),
497 td_err_string (val));
499 /* Get the general-purpose registers. */
501 val = p_td_thr_getgregs (&thandle, gregset);
502 if (val != TD_OK && val != TD_PARTIALREG)
503 error (_("sol_thread_fetch_registers: td_thr_getgregs %s"),
504 td_err_string (val));
506 /* For SPARC, TD_PARTIALREG means that only %i0...%i7, %l0..%l7, %pc
507 and %sp are saved (by a thread context switch). */
509 /* And, now the floating-point registers. */
511 val = p_td_thr_getfpregs (&thandle, &fpregset);
512 if (val != TD_OK && val != TD_NOFPREGS)
513 error (_("sol_thread_fetch_registers: td_thr_getfpregs %s"),
514 td_err_string (val));
516 /* Note that we must call supply_gregset and supply_fpregset *after*
517 calling the td routines because the td routines call ps_lget*
518 which affect the values stored in the registers array. */
520 supply_gregset (regcache, (const gdb_gregset_t *) gregset_p);
521 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset_p);
524 void
525 sol_thread_target::store_registers (struct regcache *regcache, int regnum)
527 thread_t thread;
528 td_thrhandle_t thandle;
529 td_err_e val;
530 prgregset_t gregset;
531 prfpregset_t fpregset;
532 ptid_t ptid = regcache->ptid ();
534 if (!ptid.tid_p ())
536 /* It's an LWP; pass the request on to the layer beneath. */
537 beneath ()->store_registers (regcache, regnum);
538 return;
541 /* Solaris thread: convert PTID into a td_thrhandle_t. */
542 thread = ptid.tid ();
544 val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
545 if (val != TD_OK)
546 error (_("sol_thread_store_registers: td_ta_map_id2thr %s"),
547 td_err_string (val));
549 if (regnum != -1)
551 val = p_td_thr_getgregs (&thandle, gregset);
552 if (val != TD_OK)
553 error (_("sol_thread_store_registers: td_thr_getgregs %s"),
554 td_err_string (val));
555 val = p_td_thr_getfpregs (&thandle, &fpregset);
556 if (val != TD_OK)
557 error (_("sol_thread_store_registers: td_thr_getfpregs %s"),
558 td_err_string (val));
561 fill_gregset (regcache, (gdb_gregset_t *) &gregset, regnum);
562 fill_fpregset (regcache, (gdb_fpregset_t *) &fpregset, regnum);
564 val = p_td_thr_setgregs (&thandle, gregset);
565 if (val != TD_OK)
566 error (_("sol_thread_store_registers: td_thr_setgregs %s"),
567 td_err_string (val));
568 val = p_td_thr_setfpregs (&thandle, &fpregset);
569 if (val != TD_OK)
570 error (_("sol_thread_store_registers: td_thr_setfpregs %s"),
571 td_err_string (val));
574 /* Perform partial transfers on OBJECT. See target_read_partial and
575 target_write_partial for details of each variant. One, and only
576 one, of readbuf or writebuf must be non-NULL. */
578 enum target_xfer_status
579 sol_thread_target::xfer_partial (enum target_object object,
580 const char *annex, gdb_byte *readbuf,
581 const gdb_byte *writebuf,
582 ULONGEST offset, ULONGEST len,
583 ULONGEST *xfered_len)
585 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
587 if (inferior_ptid.tid_p () || !target_thread_alive (inferior_ptid))
589 /* It's either a thread or an LWP that isn't alive. Any live
590 LWP will do so use the first available.
592 NOTE: We don't need to call switch_to_thread; we're just
593 reading memory. */
594 inferior_ptid = procfs_first_available ();
597 return beneath ()->xfer_partial (object, annex, readbuf,
598 writebuf, offset, len, xfered_len);
601 static void
602 check_for_thread_db (void)
604 td_err_e err;
605 ptid_t ptid;
607 /* Don't attempt to use thread_db for remote targets. */
608 if (!(target_can_run () || current_program_space->core_bfd () != nullptr))
609 return;
611 /* Do nothing if we couldn't load libthread_db.so.1. */
612 if (p_td_ta_new == NULL)
613 return;
615 if (sol_thread_active)
616 /* Nothing to do. The thread library was already detected and the
617 target vector was already activated. */
618 return;
620 /* Now, initialize libthread_db. This needs to be done after the
621 shared libraries are located because it needs information from
622 the user's thread library. */
624 err = p_td_init ();
625 if (err != TD_OK)
627 warning (_("sol_thread_new_objfile: td_init: %s"), td_err_string (err));
628 return;
631 /* Now attempt to open a connection to the thread library. */
632 err = p_td_ta_new (&main_ph, &main_ta);
633 switch (err)
635 case TD_NOLIBTHREAD:
636 /* No thread library was detected. */
637 break;
639 case TD_OK:
640 gdb_printf (_("[Thread debugging using libthread_db enabled]\n"));
642 /* The thread library was detected. Activate the sol_thread target. */
643 current_inferior ()->push_target (&sol_thread_ops);
644 sol_thread_active = 1;
646 main_ph.ptid = inferior_ptid; /* Save for xfer_memory. */
647 ptid = lwp_to_thread (inferior_ptid);
648 if (ptid.pid () != -1)
649 inferior_ptid = ptid;
651 target_update_thread_list ();
652 break;
654 default:
655 warning (_("Cannot initialize thread debugging library: %s"),
656 td_err_string (err));
657 break;
661 /* This routine is called whenever a new symbol table is read in, or
662 when all symbol tables are removed. libthread_db can only be
663 initialized when it finds the right variables in libthread.so.
664 Since it's a shared library, those variables don't show up until
665 the library gets mapped and the symbol table is read in. */
667 static void
668 sol_thread_new_objfile (struct objfile *objfile)
670 check_for_thread_db ();
673 /* Clean up after the inferior dies. */
675 void
676 sol_thread_target::mourn_inferior ()
678 target_ops *beneath = this->beneath ();
680 sol_thread_active = 0;
682 current_inferior ()->unpush_target (this);
684 beneath->mourn_inferior ();
687 /* Return true if PTID is still active in the inferior. */
689 bool
690 sol_thread_target::thread_alive (ptid_t ptid)
692 if (ptid.tid_p ())
694 /* It's a (user-level) thread. */
695 td_err_e val;
696 td_thrhandle_t th;
697 int pid;
699 pid = ptid.tid ();
700 val = p_td_ta_map_id2thr (main_ta, pid, &th);
701 if (val != TD_OK)
702 return false; /* Thread not found. */
703 val = p_td_thr_validate (&th);
704 if (val != TD_OK)
705 return false; /* Thread not valid. */
706 return true; /* Known thread. */
708 else
710 /* It's an LPW; pass the request on to the layer below. */
711 return beneath ()->thread_alive (ptid);
716 /* These routines implement the lower half of the thread_db interface,
717 i.e. the ps_* routines. */
719 /* The next four routines are called by libthread_db to tell us to
720 stop and stop a particular process or lwp. Since GDB ensures that
721 these are all stopped by the time we call anything in thread_db,
722 these routines need to do nothing. */
724 /* Process stop. */
726 ps_err_e
727 ps_pstop (struct ps_prochandle *ph)
729 return PS_OK;
732 /* Process continue. */
734 ps_err_e
735 ps_pcontinue (struct ps_prochandle *ph)
737 return PS_OK;
740 /* LWP stop. */
742 ps_err_e
743 ps_lstop (struct ps_prochandle *ph, lwpid_t lwpid)
745 return PS_OK;
748 /* LWP continue. */
750 ps_err_e
751 ps_lcontinue (struct ps_prochandle *ph, lwpid_t lwpid)
753 return PS_OK;
756 /* Looks up the symbol LD_SYMBOL_NAME in the debugger's symbol table. */
758 ps_err_e
759 ps_pglobal_lookup (struct ps_prochandle *ph, const char *ld_object_name,
760 const char *ld_symbol_name, psaddr_t *ld_symbol_addr)
762 bound_minimal_symbol ms
763 = lookup_minimal_symbol (current_program_space, ld_symbol_name);
764 if (!ms.minsym)
765 return PS_NOSYM;
767 *ld_symbol_addr = ms.value_address ();
768 return PS_OK;
771 /* Common routine for reading and writing memory. */
773 static ps_err_e
774 rw_common (int dowrite, const struct ps_prochandle *ph, psaddr_t addr,
775 gdb_byte *buf, int size)
777 int ret;
779 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
781 if (inferior_ptid.tid_p () || !target_thread_alive (inferior_ptid))
783 /* It's either a thread or an LWP that isn't alive. Any live
784 LWP will do so use the first available.
786 NOTE: We don't need to call switch_to_thread; we're just
787 reading memory. */
788 inferior_ptid = procfs_first_available ();
791 #if defined (__sparcv9)
792 /* For Sparc64 cross Sparc32, make sure the address has not been
793 accidentally sign-extended (or whatever) to beyond 32 bits. */
794 if (bfd_get_arch_size (current_program_space->exec_bfd ()) == 32)
795 addr &= 0xffffffff;
796 #endif
798 if (dowrite)
799 ret = target_write_memory (addr, (gdb_byte *) buf, size);
800 else
801 ret = target_read_memory (addr, (gdb_byte *) buf, size);
803 return (ret == 0 ? PS_OK : PS_ERR);
806 /* Copies SIZE bytes from target process .data segment to debugger memory. */
808 ps_err_e
809 ps_pdread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t size)
811 return rw_common (0, ph, addr, (gdb_byte *) buf, size);
814 /* Copies SIZE bytes from debugger memory .data segment to target process. */
816 ps_err_e
817 ps_pdwrite (struct ps_prochandle *ph, psaddr_t addr,
818 const void *buf, size_t size)
820 return rw_common (1, ph, addr, (gdb_byte *) buf, size);
823 /* Copies SIZE bytes from target process .text segment to debugger memory. */
825 ps_err_e
826 ps_ptread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t size)
828 return rw_common (0, ph, addr, (gdb_byte *) buf, size);
831 /* Copies SIZE bytes from debugger memory .text segment to target process. */
833 ps_err_e
834 ps_ptwrite (struct ps_prochandle *ph, psaddr_t addr,
835 const void *buf, size_t size)
837 return rw_common (1, ph, addr, (gdb_byte *) buf, size);
840 /* Get general-purpose registers for LWP. */
842 ps_err_e
843 ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
845 ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
846 regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
847 current_inferior ()->arch ());
849 target_fetch_registers (regcache, -1);
850 fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
852 return PS_OK;
855 /* Set general-purpose registers for LWP. */
857 ps_err_e
858 ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid,
859 const prgregset_t gregset)
861 ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
862 regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
863 current_inferior ()->arch ());
865 supply_gregset (regcache, (const gdb_gregset_t *) gregset);
866 target_store_registers (regcache, -1);
868 return PS_OK;
871 /* Log a message (sends to gdb_stderr). */
873 void
874 ps_plog (const char *fmt, ...)
876 va_list args;
878 va_start (args, fmt);
880 gdb_vprintf (gdb_stderr, fmt, args);
883 /* Get size of extra register set. Currently a noop. */
885 ps_err_e
886 ps_lgetxregsize (struct ps_prochandle *ph, lwpid_t lwpid, int *xregsize)
888 return PS_OK;
891 /* Get extra register set. Currently a noop. */
893 ps_err_e
894 ps_lgetxregs (struct ps_prochandle *ph, lwpid_t lwpid, caddr_t xregset)
896 return PS_OK;
899 /* Set extra register set. Currently a noop. */
901 ps_err_e
902 ps_lsetxregs (struct ps_prochandle *ph, lwpid_t lwpid, caddr_t xregset)
904 return PS_OK;
907 /* Get floating-point registers for LWP. */
909 ps_err_e
910 ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
911 prfpregset_t *fpregset)
913 ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
914 regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
915 current_inferior ()->arch ());
917 target_fetch_registers (regcache, -1);
918 fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
920 return PS_OK;
923 /* Set floating-point regs for LWP. */
925 ps_err_e
926 ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
927 const prfpregset_t * fpregset)
929 ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
930 regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
931 current_inferior ()->arch ());
933 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
934 target_store_registers (regcache, -1);
936 return PS_OK;
939 /* Identify process as 32-bit or 64-bit. At the moment we're using
940 BFD to do this. There might be a more Solaris-specific
941 (e.g. procfs) method, but this ought to work. */
943 ps_err_e
944 ps_pdmodel (struct ps_prochandle *ph, int *data_model)
946 if (current_program_space->exec_bfd () == 0)
947 *data_model = PR_MODEL_UNKNOWN;
948 else if (bfd_get_arch_size (current_program_space->exec_bfd ()) == 32)
949 *data_model = PR_MODEL_ILP32;
950 else
951 *data_model = PR_MODEL_LP64;
953 return PS_OK;
957 /* Convert PTID to printable form. */
959 std::string
960 sol_thread_target::pid_to_str (ptid_t ptid)
962 if (ptid.tid_p ())
964 ptid_t lwp;
966 lwp = thread_to_lwp (ptid, -2);
968 if (lwp.pid () == -1)
969 return string_printf ("Thread %s (defunct)",
970 pulongest (ptid.tid ()));
971 else if (lwp.pid () != -2)
972 return string_printf ("Thread %s (LWP %ld)",
973 pulongest (ptid.tid ()), lwp.lwp ());
974 else
975 return string_printf ("Thread %s ",
976 pulongest (ptid.tid ()));
978 else if (ptid.lwp () != 0)
979 return string_printf ("LWP %ld ", ptid.lwp ());
980 else
981 return string_printf ("process %d ", ptid.pid ());
985 /* Worker bee for update_thread_list. Callback function that gets
986 called once per user-level thread (i.e. not for LWP's). */
988 static int
989 sol_update_thread_list_callback (const td_thrhandle_t *th, void *ignored)
991 td_err_e retval;
992 td_thrinfo_t ti;
994 retval = p_td_thr_get_info (th, &ti);
995 if (retval != TD_OK)
996 return -1;
998 ptid_t ptid = ptid_t (current_inferior ()->pid, 0, ti.ti_tid);
999 thread_info *thr = current_inferior ()->find_thread (ptid);
1000 if (thr == NULL || thr->state == THREAD_EXITED)
1002 process_stratum_target *proc_target
1003 = current_inferior ()->process_target ();
1004 add_thread (proc_target, ptid);
1007 return 0;
1010 void
1011 sol_thread_target::update_thread_list ()
1013 /* Delete dead threads. */
1014 prune_threads ();
1016 /* Find any new LWP's. */
1017 beneath ()->update_thread_list ();
1019 /* Then find any new user-level threads. */
1020 p_td_ta_thr_iter (main_ta, sol_update_thread_list_callback, (void *) 0,
1021 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1022 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1025 /* Worker bee for the "info sol-thread" command. This is a callback
1026 function that gets called once for each Solaris user-level thread
1027 (i.e. not for LWPs) in the inferior. Print anything interesting
1028 that we can think of. */
1030 static int
1031 info_cb (const td_thrhandle_t *th, void *s)
1033 td_err_e ret;
1034 td_thrinfo_t ti;
1036 ret = p_td_thr_get_info (th, &ti);
1037 if (ret == TD_OK)
1039 gdb_printf ("%s thread #%d, lwp %d, ",
1040 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
1041 ti.ti_tid, ti.ti_lid);
1042 switch (ti.ti_state)
1044 default:
1045 case TD_THR_UNKNOWN:
1046 gdb_printf ("<unknown state>");
1047 break;
1048 case TD_THR_STOPPED:
1049 gdb_printf ("(stopped)");
1050 break;
1051 case TD_THR_RUN:
1052 gdb_printf ("(run) ");
1053 break;
1054 case TD_THR_ACTIVE:
1055 gdb_printf ("(active) ");
1056 break;
1057 case TD_THR_ZOMBIE:
1058 gdb_printf ("(zombie) ");
1059 break;
1060 case TD_THR_SLEEP:
1061 gdb_printf ("(asleep) ");
1062 break;
1063 case TD_THR_STOPPED_ASLEEP:
1064 gdb_printf ("(stopped asleep)");
1065 break;
1067 /* Print thr_create start function. */
1068 if (ti.ti_startfunc != 0)
1070 const bound_minimal_symbol msym
1071 = lookup_minimal_symbol_by_pc (ti.ti_startfunc);
1073 gdb_printf (" startfunc=%s",
1074 msym.minsym
1075 ? msym.minsym->print_name ()
1076 : paddress (current_inferior ()->arch (),
1077 ti.ti_startfunc));
1080 /* If thread is asleep, print function that went to sleep. */
1081 if (ti.ti_state == TD_THR_SLEEP)
1083 const bound_minimal_symbol msym
1084 = lookup_minimal_symbol_by_pc (ti.ti_pc);
1086 gdb_printf (" sleepfunc=%s",
1087 msym.minsym
1088 ? msym.minsym->print_name ()
1089 : paddress (current_inferior ()->arch (), ti.ti_pc));
1092 gdb_printf ("\n");
1094 else
1095 warning (_("info sol-thread: failed to get info for thread."));
1097 return 0;
1100 /* List some state about each Solaris user-level thread in the
1101 inferior. */
1103 static void
1104 info_solthreads (const char *args, int from_tty)
1106 p_td_ta_thr_iter (main_ta, info_cb, (void *) args,
1107 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1108 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1111 /* Callback routine used to find a thread based on the TID part of
1112 its PTID. */
1114 static int
1115 thread_db_find_thread_from_tid (struct thread_info *thread, void *data)
1117 ULONGEST *tid = (ULONGEST *) data;
1119 if (thread->ptid.tid () == *tid)
1120 return 1;
1122 return 0;
1125 ptid_t
1126 sol_thread_target::get_ada_task_ptid (long lwp, ULONGEST thread)
1128 struct thread_info *thread_info =
1129 iterate_over_threads (thread_db_find_thread_from_tid, &thread);
1131 if (thread_info == NULL)
1133 /* The list of threads is probably not up to date. Find any
1134 thread that is missing from the list, and try again. */
1135 update_thread_list ();
1136 thread_info = iterate_over_threads (thread_db_find_thread_from_tid,
1137 &thread);
1140 gdb_assert (thread_info != NULL);
1142 return (thread_info->ptid);
1145 void _initialize_sol_thread ();
1146 void
1147 _initialize_sol_thread ()
1149 void *dlhandle;
1151 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1152 if (!dlhandle)
1153 goto die;
1155 #define resolve(X) \
1156 if (!(p_##X = (X ## _ftype *) dlsym (dlhandle, #X))) \
1157 goto die;
1159 resolve (td_log);
1160 resolve (td_ta_new);
1161 resolve (td_ta_delete);
1162 resolve (td_init);
1163 resolve (td_ta_get_ph);
1164 resolve (td_ta_get_nthreads);
1165 resolve (td_ta_tsd_iter);
1166 resolve (td_ta_thr_iter);
1167 resolve (td_thr_validate);
1168 resolve (td_thr_tsd);
1169 resolve (td_thr_get_info);
1170 resolve (td_thr_getfpregs);
1171 resolve (td_thr_getxregsize);
1172 resolve (td_thr_getxregs);
1173 resolve (td_thr_sigsetmask);
1174 resolve (td_thr_setprio);
1175 resolve (td_thr_setsigpending);
1176 resolve (td_thr_setfpregs);
1177 resolve (td_thr_setxregs);
1178 resolve (td_ta_map_id2thr);
1179 resolve (td_ta_map_lwp2thr);
1180 resolve (td_thr_getgregs);
1181 resolve (td_thr_setgregs);
1183 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1184 _("Show info on Solaris user threads."), &maintenanceinfolist);
1186 /* Hook into new_objfile notification. */
1187 gdb::observers::new_objfile.attach (sol_thread_new_objfile, "sol-thread");
1188 return;
1190 die:
1191 gdb_printf (gdb_stderr, "\
1192 [GDB will not be able to debug user-mode threads: %s]\n", dlerror ());
1194 if (dlhandle)
1195 dlclose (dlhandle);
1197 return;