Fix test for sections with different VMA<->LMA relationships so that it only applies...
[binutils-gdb.git] / gdb / sol-thread.c
blob7c17601d5608374bb9090683ffdbb29cbf0d9434
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 "gdbcmd.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 struct bound_minimal_symbol ms;
764 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
765 if (!ms.minsym)
766 return PS_NOSYM;
768 *ld_symbol_addr = ms.value_address ();
769 return PS_OK;
772 /* Common routine for reading and writing memory. */
774 static ps_err_e
775 rw_common (int dowrite, const struct ps_prochandle *ph, psaddr_t addr,
776 gdb_byte *buf, int size)
778 int ret;
780 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
782 if (inferior_ptid.tid_p () || !target_thread_alive (inferior_ptid))
784 /* It's either a thread or an LWP that isn't alive. Any live
785 LWP will do so use the first available.
787 NOTE: We don't need to call switch_to_thread; we're just
788 reading memory. */
789 inferior_ptid = procfs_first_available ();
792 #if defined (__sparcv9)
793 /* For Sparc64 cross Sparc32, make sure the address has not been
794 accidentally sign-extended (or whatever) to beyond 32 bits. */
795 if (bfd_get_arch_size (current_program_space->exec_bfd ()) == 32)
796 addr &= 0xffffffff;
797 #endif
799 if (dowrite)
800 ret = target_write_memory (addr, (gdb_byte *) buf, size);
801 else
802 ret = target_read_memory (addr, (gdb_byte *) buf, size);
804 return (ret == 0 ? PS_OK : PS_ERR);
807 /* Copies SIZE bytes from target process .data segment to debugger memory. */
809 ps_err_e
810 ps_pdread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t size)
812 return rw_common (0, ph, addr, (gdb_byte *) buf, size);
815 /* Copies SIZE bytes from debugger memory .data segment to target process. */
817 ps_err_e
818 ps_pdwrite (struct ps_prochandle *ph, psaddr_t addr,
819 const void *buf, size_t size)
821 return rw_common (1, ph, addr, (gdb_byte *) buf, size);
824 /* Copies SIZE bytes from target process .text segment to debugger memory. */
826 ps_err_e
827 ps_ptread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t size)
829 return rw_common (0, ph, addr, (gdb_byte *) buf, size);
832 /* Copies SIZE bytes from debugger memory .text segment to target process. */
834 ps_err_e
835 ps_ptwrite (struct ps_prochandle *ph, psaddr_t addr,
836 const void *buf, size_t size)
838 return rw_common (1, ph, addr, (gdb_byte *) buf, size);
841 /* Get general-purpose registers for LWP. */
843 ps_err_e
844 ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
846 ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
847 regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
848 current_inferior ()->arch ());
850 target_fetch_registers (regcache, -1);
851 fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
853 return PS_OK;
856 /* Set general-purpose registers for LWP. */
858 ps_err_e
859 ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid,
860 const prgregset_t gregset)
862 ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
863 regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
864 current_inferior ()->arch ());
866 supply_gregset (regcache, (const gdb_gregset_t *) gregset);
867 target_store_registers (regcache, -1);
869 return PS_OK;
872 /* Log a message (sends to gdb_stderr). */
874 void
875 ps_plog (const char *fmt, ...)
877 va_list args;
879 va_start (args, fmt);
881 gdb_vprintf (gdb_stderr, fmt, args);
884 /* Get size of extra register set. Currently a noop. */
886 ps_err_e
887 ps_lgetxregsize (struct ps_prochandle *ph, lwpid_t lwpid, int *xregsize)
889 return PS_OK;
892 /* Get extra register set. Currently a noop. */
894 ps_err_e
895 ps_lgetxregs (struct ps_prochandle *ph, lwpid_t lwpid, caddr_t xregset)
897 return PS_OK;
900 /* Set extra register set. Currently a noop. */
902 ps_err_e
903 ps_lsetxregs (struct ps_prochandle *ph, lwpid_t lwpid, caddr_t xregset)
905 return PS_OK;
908 /* Get floating-point registers for LWP. */
910 ps_err_e
911 ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
912 prfpregset_t *fpregset)
914 ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
915 regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
916 current_inferior ()->arch ());
918 target_fetch_registers (regcache, -1);
919 fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
921 return PS_OK;
924 /* Set floating-point regs for LWP. */
926 ps_err_e
927 ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
928 const prfpregset_t * fpregset)
930 ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
931 regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
932 current_inferior ()->arch ());
934 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
935 target_store_registers (regcache, -1);
937 return PS_OK;
940 /* Identify process as 32-bit or 64-bit. At the moment we're using
941 BFD to do this. There might be a more Solaris-specific
942 (e.g. procfs) method, but this ought to work. */
944 ps_err_e
945 ps_pdmodel (struct ps_prochandle *ph, int *data_model)
947 if (current_program_space->exec_bfd () == 0)
948 *data_model = PR_MODEL_UNKNOWN;
949 else if (bfd_get_arch_size (current_program_space->exec_bfd ()) == 32)
950 *data_model = PR_MODEL_ILP32;
951 else
952 *data_model = PR_MODEL_LP64;
954 return PS_OK;
958 /* Convert PTID to printable form. */
960 std::string
961 sol_thread_target::pid_to_str (ptid_t ptid)
963 if (ptid.tid_p ())
965 ptid_t lwp;
967 lwp = thread_to_lwp (ptid, -2);
969 if (lwp.pid () == -1)
970 return string_printf ("Thread %s (defunct)",
971 pulongest (ptid.tid ()));
972 else if (lwp.pid () != -2)
973 return string_printf ("Thread %s (LWP %ld)",
974 pulongest (ptid.tid ()), lwp.lwp ());
975 else
976 return string_printf ("Thread %s ",
977 pulongest (ptid.tid ()));
979 else if (ptid.lwp () != 0)
980 return string_printf ("LWP %ld ", ptid.lwp ());
981 else
982 return string_printf ("process %d ", ptid.pid ());
986 /* Worker bee for update_thread_list. Callback function that gets
987 called once per user-level thread (i.e. not for LWP's). */
989 static int
990 sol_update_thread_list_callback (const td_thrhandle_t *th, void *ignored)
992 td_err_e retval;
993 td_thrinfo_t ti;
995 retval = p_td_thr_get_info (th, &ti);
996 if (retval != TD_OK)
997 return -1;
999 ptid_t ptid = ptid_t (current_inferior ()->pid, 0, ti.ti_tid);
1000 thread_info *thr = current_inferior ()->find_thread (ptid);
1001 if (thr == NULL || thr->state == THREAD_EXITED)
1003 process_stratum_target *proc_target
1004 = current_inferior ()->process_target ();
1005 add_thread (proc_target, ptid);
1008 return 0;
1011 void
1012 sol_thread_target::update_thread_list ()
1014 /* Delete dead threads. */
1015 prune_threads ();
1017 /* Find any new LWP's. */
1018 beneath ()->update_thread_list ();
1020 /* Then find any new user-level threads. */
1021 p_td_ta_thr_iter (main_ta, sol_update_thread_list_callback, (void *) 0,
1022 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1023 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1026 /* Worker bee for the "info sol-thread" command. This is a callback
1027 function that gets called once for each Solaris user-level thread
1028 (i.e. not for LWPs) in the inferior. Print anything interesting
1029 that we can think of. */
1031 static int
1032 info_cb (const td_thrhandle_t *th, void *s)
1034 td_err_e ret;
1035 td_thrinfo_t ti;
1037 ret = p_td_thr_get_info (th, &ti);
1038 if (ret == TD_OK)
1040 gdb_printf ("%s thread #%d, lwp %d, ",
1041 ti.ti_type == TD_THR_SYSTEM ? "system" : "user ",
1042 ti.ti_tid, ti.ti_lid);
1043 switch (ti.ti_state)
1045 default:
1046 case TD_THR_UNKNOWN:
1047 gdb_printf ("<unknown state>");
1048 break;
1049 case TD_THR_STOPPED:
1050 gdb_printf ("(stopped)");
1051 break;
1052 case TD_THR_RUN:
1053 gdb_printf ("(run) ");
1054 break;
1055 case TD_THR_ACTIVE:
1056 gdb_printf ("(active) ");
1057 break;
1058 case TD_THR_ZOMBIE:
1059 gdb_printf ("(zombie) ");
1060 break;
1061 case TD_THR_SLEEP:
1062 gdb_printf ("(asleep) ");
1063 break;
1064 case TD_THR_STOPPED_ASLEEP:
1065 gdb_printf ("(stopped asleep)");
1066 break;
1068 /* Print thr_create start function. */
1069 if (ti.ti_startfunc != 0)
1071 const struct bound_minimal_symbol msym
1072 = lookup_minimal_symbol_by_pc (ti.ti_startfunc);
1074 gdb_printf (" startfunc=%s",
1075 msym.minsym
1076 ? msym.minsym->print_name ()
1077 : paddress (current_inferior ()->arch (),
1078 ti.ti_startfunc));
1081 /* If thread is asleep, print function that went to sleep. */
1082 if (ti.ti_state == TD_THR_SLEEP)
1084 const struct bound_minimal_symbol msym
1085 = lookup_minimal_symbol_by_pc (ti.ti_pc);
1087 gdb_printf (" sleepfunc=%s",
1088 msym.minsym
1089 ? msym.minsym->print_name ()
1090 : paddress (current_inferior ()->arch (), ti.ti_pc));
1093 gdb_printf ("\n");
1095 else
1096 warning (_("info sol-thread: failed to get info for thread."));
1098 return 0;
1101 /* List some state about each Solaris user-level thread in the
1102 inferior. */
1104 static void
1105 info_solthreads (const char *args, int from_tty)
1107 p_td_ta_thr_iter (main_ta, info_cb, (void *) args,
1108 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1109 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1112 /* Callback routine used to find a thread based on the TID part of
1113 its PTID. */
1115 static int
1116 thread_db_find_thread_from_tid (struct thread_info *thread, void *data)
1118 ULONGEST *tid = (ULONGEST *) data;
1120 if (thread->ptid.tid () == *tid)
1121 return 1;
1123 return 0;
1126 ptid_t
1127 sol_thread_target::get_ada_task_ptid (long lwp, ULONGEST thread)
1129 struct thread_info *thread_info =
1130 iterate_over_threads (thread_db_find_thread_from_tid, &thread);
1132 if (thread_info == NULL)
1134 /* The list of threads is probably not up to date. Find any
1135 thread that is missing from the list, and try again. */
1136 update_thread_list ();
1137 thread_info = iterate_over_threads (thread_db_find_thread_from_tid,
1138 &thread);
1141 gdb_assert (thread_info != NULL);
1143 return (thread_info->ptid);
1146 void _initialize_sol_thread ();
1147 void
1148 _initialize_sol_thread ()
1150 void *dlhandle;
1152 dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW);
1153 if (!dlhandle)
1154 goto die;
1156 #define resolve(X) \
1157 if (!(p_##X = (X ## _ftype *) dlsym (dlhandle, #X))) \
1158 goto die;
1160 resolve (td_log);
1161 resolve (td_ta_new);
1162 resolve (td_ta_delete);
1163 resolve (td_init);
1164 resolve (td_ta_get_ph);
1165 resolve (td_ta_get_nthreads);
1166 resolve (td_ta_tsd_iter);
1167 resolve (td_ta_thr_iter);
1168 resolve (td_thr_validate);
1169 resolve (td_thr_tsd);
1170 resolve (td_thr_get_info);
1171 resolve (td_thr_getfpregs);
1172 resolve (td_thr_getxregsize);
1173 resolve (td_thr_getxregs);
1174 resolve (td_thr_sigsetmask);
1175 resolve (td_thr_setprio);
1176 resolve (td_thr_setsigpending);
1177 resolve (td_thr_setfpregs);
1178 resolve (td_thr_setxregs);
1179 resolve (td_ta_map_id2thr);
1180 resolve (td_ta_map_lwp2thr);
1181 resolve (td_thr_getgregs);
1182 resolve (td_thr_setgregs);
1184 add_cmd ("sol-threads", class_maintenance, info_solthreads,
1185 _("Show info on Solaris user threads."), &maintenanceinfolist);
1187 /* Hook into new_objfile notification. */
1188 gdb::observers::new_objfile.attach (sol_thread_new_objfile, "sol-thread");
1189 return;
1191 die:
1192 gdb_printf (gdb_stderr, "\
1193 [GDB will not be able to debug user-mode threads: %s]\n", dlerror ());
1195 if (dlhandle)
1196 dlclose (dlhandle);
1198 return;