Commit | Line | Data |
---|---|---|
fb0e1ba7 | 1 | /* libthread_db assisted debugging support, generic parts. |
1bac305b AC |
2 | |
3 | Copyright 1999, 2000, 2001, 2003 Free Software Foundation, Inc. | |
fb0e1ba7 MK |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | #include "defs.h" | |
23 | ||
24 | #include "gdb_assert.h" | |
25 | #include <dlfcn.h> | |
26 | #include "gdb_proc_service.h" | |
27 | #include "gdb_thread_db.h" | |
28 | ||
bda9cb72 | 29 | #include "bfd.h" |
fb0e1ba7 MK |
30 | #include "gdbthread.h" |
31 | #include "inferior.h" | |
bda9cb72 MK |
32 | #include "symfile.h" |
33 | #include "objfiles.h" | |
fb0e1ba7 | 34 | #include "target.h" |
4e052eda | 35 | #include "regcache.h" |
3f47be5c | 36 | #include "solib-svr4.h" |
fb0e1ba7 MK |
37 | |
38 | #ifndef LIBTHREAD_DB_SO | |
39 | #define LIBTHREAD_DB_SO "libthread_db.so.1" | |
40 | #endif | |
41 | ||
8605d56e AC |
42 | /* If we're running on GNU/Linux, we must explicitly attach to any new |
43 | threads. */ | |
fb0e1ba7 MK |
44 | |
45 | /* FIXME: There is certainly some room for improvements: | |
46 | - Cache LWP ids. | |
47 | - Bypass libthread_db when fetching or storing registers for | |
48 | threads bound to a LWP. */ | |
49 | ||
50 | /* This module's target vector. */ | |
51 | static struct target_ops thread_db_ops; | |
52 | ||
53 | /* The target vector that we call for things this module can't handle. */ | |
54 | static struct target_ops *target_beneath; | |
55 | ||
56 | /* Pointer to the next function on the objfile event chain. */ | |
540af400 | 57 | static void (*target_new_objfile_chain) (struct objfile *objfile); |
fb0e1ba7 MK |
58 | |
59 | /* Non-zero if we're using this module's target vector. */ | |
60 | static int using_thread_db; | |
61 | ||
c194fbe1 MK |
62 | /* Non-zero if we have to keep this module's target vector active |
63 | across re-runs. */ | |
bda9cb72 MK |
64 | static int keep_thread_db; |
65 | ||
fb0e1ba7 MK |
66 | /* Non-zero if we have determined the signals used by the threads |
67 | library. */ | |
68 | static int thread_signals; | |
69 | static sigset_t thread_stop_set; | |
70 | static sigset_t thread_print_set; | |
71 | ||
72 | /* Structure that identifies the child process for the | |
73 | <proc_service.h> interface. */ | |
74 | static struct ps_prochandle proc_handle; | |
75 | ||
76 | /* Connection to the libthread_db library. */ | |
77 | static td_thragent_t *thread_agent; | |
78 | ||
79 | /* Pointers to the libthread_db functions. */ | |
80 | ||
81 | static td_err_e (*td_init_p) (void); | |
82 | ||
83 | static td_err_e (*td_ta_new_p) (struct ps_prochandle *ps, td_thragent_t **ta); | |
84 | static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt, | |
85 | td_thrhandle_t *__th); | |
86 | static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid, | |
87 | td_thrhandle_t *th); | |
88 | static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta, | |
89 | td_thr_iter_f *callback, | |
90 | void *cbdata_p, td_thr_state_e state, | |
91 | int ti_pri, sigset_t *ti_sigmask_p, | |
92 | unsigned int ti_user_flags); | |
93 | static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta, | |
94 | td_event_e event, td_notify_t *ptr); | |
95 | static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta, | |
96 | td_thr_events_t *event); | |
97 | static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta, | |
98 | td_event_msg_t *msg); | |
99 | ||
100 | static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th); | |
101 | static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th, | |
102 | td_thrinfo_t *infop); | |
103 | static td_err_e (*td_thr_getfpregs_p) (const td_thrhandle_t *th, | |
104 | gdb_prfpregset_t *regset); | |
105 | static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th, | |
106 | prgregset_t gregs); | |
107 | static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th, | |
108 | const gdb_prfpregset_t *fpregs); | |
109 | static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th, | |
110 | prgregset_t gregs); | |
111 | static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event); | |
112 | ||
3f47be5c EZ |
113 | static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th, |
114 | void *map_address, | |
115 | size_t offset, | |
116 | void **address); | |
117 | ||
fb0e1ba7 MK |
118 | /* Location of the thread creation event breakpoint. The code at this |
119 | location in the child process will be called by the pthread library | |
120 | whenever a new thread is created. By setting a special breakpoint | |
121 | at this location, GDB can detect when a new thread is created. We | |
122 | obtain this location via the td_ta_event_addr call. */ | |
123 | static CORE_ADDR td_create_bp_addr; | |
124 | ||
125 | /* Location of the thread death event breakpoint. */ | |
126 | static CORE_ADDR td_death_bp_addr; | |
127 | ||
128 | /* Prototypes for local functions. */ | |
bda9cb72 | 129 | static void thread_db_find_new_threads (void); |
5365276c DJ |
130 | static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p, |
131 | const td_thrinfo_t *ti_p, int verbose); | |
fb0e1ba7 MK |
132 | \f |
133 | ||
134 | /* Building process ids. */ | |
135 | ||
ca6724c1 KB |
136 | #define GET_PID(ptid) ptid_get_pid (ptid) |
137 | #define GET_LWP(ptid) ptid_get_lwp (ptid) | |
138 | #define GET_THREAD(ptid) ptid_get_tid (ptid) | |
fb0e1ba7 | 139 | |
ca6724c1 KB |
140 | #define is_lwp(ptid) (GET_LWP (ptid) != 0) |
141 | #define is_thread(ptid) (GET_THREAD (ptid) != 0) | |
fb0e1ba7 | 142 | |
ca6724c1 KB |
143 | #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) |
144 | #define BUILD_THREAD(tid, pid) ptid_build (pid, 0, tid) | |
fb0e1ba7 MK |
145 | \f |
146 | ||
5365276c DJ |
147 | /* Use "struct private_thread_info" to cache thread state. This is |
148 | a substantial optimization. */ | |
149 | ||
fb0e1ba7 MK |
150 | struct private_thread_info |
151 | { | |
5365276c DJ |
152 | /* Cached thread state. */ |
153 | unsigned int th_valid : 1; | |
154 | unsigned int ti_valid : 1; | |
155 | ||
156 | td_thrhandle_t th; | |
157 | td_thrinfo_t ti; | |
fb0e1ba7 | 158 | }; |
fb0e1ba7 | 159 | \f |
21bf60fe | 160 | |
fb0e1ba7 MK |
161 | static char * |
162 | thread_db_err_str (td_err_e err) | |
163 | { | |
164 | static char buf[64]; | |
165 | ||
166 | switch (err) | |
167 | { | |
168 | case TD_OK: | |
169 | return "generic 'call succeeded'"; | |
170 | case TD_ERR: | |
171 | return "generic error"; | |
172 | case TD_NOTHR: | |
173 | return "no thread to satisfy query"; | |
174 | case TD_NOSV: | |
175 | return "no sync handle to satisfy query"; | |
176 | case TD_NOLWP: | |
177 | return "no LWP to satisfy query"; | |
178 | case TD_BADPH: | |
179 | return "invalid process handle"; | |
180 | case TD_BADTH: | |
181 | return "invalid thread handle"; | |
182 | case TD_BADSH: | |
183 | return "invalid synchronization handle"; | |
184 | case TD_BADTA: | |
185 | return "invalid thread agent"; | |
186 | case TD_BADKEY: | |
187 | return "invalid key"; | |
188 | case TD_NOMSG: | |
189 | return "no event message for getmsg"; | |
190 | case TD_NOFPREGS: | |
191 | return "FPU register set not available"; | |
192 | case TD_NOLIBTHREAD: | |
193 | return "application not linked with libthread"; | |
194 | case TD_NOEVENT: | |
195 | return "requested event is not supported"; | |
196 | case TD_NOCAPAB: | |
197 | return "capability not available"; | |
198 | case TD_DBERR: | |
199 | return "debugger service failed"; | |
200 | case TD_NOAPLIC: | |
201 | return "operation not applicable to"; | |
202 | case TD_NOTSD: | |
203 | return "no thread-specific data for this thread"; | |
204 | case TD_MALLOC: | |
205 | return "malloc failed"; | |
206 | case TD_PARTIALREG: | |
207 | return "only part of register set was written/read"; | |
208 | case TD_NOXREGS: | |
209 | return "X register set not available for this thread"; | |
210 | default: | |
211 | snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err); | |
212 | return buf; | |
213 | } | |
214 | } | |
215 | ||
216 | static char * | |
217 | thread_db_state_str (td_thr_state_e state) | |
218 | { | |
219 | static char buf[64]; | |
220 | ||
221 | switch (state) | |
222 | { | |
223 | case TD_THR_STOPPED: | |
224 | return "stopped by debugger"; | |
225 | case TD_THR_RUN: | |
226 | return "runnable"; | |
227 | case TD_THR_ACTIVE: | |
228 | return "active"; | |
229 | case TD_THR_ZOMBIE: | |
230 | return "zombie"; | |
231 | case TD_THR_SLEEP: | |
232 | return "sleeping"; | |
233 | case TD_THR_STOPPED_ASLEEP: | |
234 | return "stopped by debugger AND blocked"; | |
235 | default: | |
236 | snprintf (buf, sizeof (buf), "unknown thread_db state %d", state); | |
237 | return buf; | |
238 | } | |
239 | } | |
240 | \f | |
5365276c DJ |
241 | /* A callback function for td_ta_thr_iter, which we use to map all |
242 | threads to LWPs. | |
243 | ||
244 | THP is a handle to the current thread; if INFOP is not NULL, the | |
245 | struct thread_info associated with this thread is returned in | |
246 | *INFOP. */ | |
247 | ||
248 | static int | |
249 | thread_get_info_callback (const td_thrhandle_t *thp, void *infop) | |
250 | { | |
251 | td_thrinfo_t ti; | |
252 | td_err_e err; | |
253 | struct thread_info *thread_info; | |
254 | ptid_t thread_ptid; | |
255 | ||
256 | err = td_thr_get_info_p (thp, &ti); | |
257 | if (err != TD_OK) | |
258 | error ("thread_get_info_callback: cannot get thread info: %s", | |
259 | thread_db_err_str (err)); | |
260 | ||
261 | /* Fill the cache. */ | |
262 | thread_ptid = BUILD_THREAD (ti.ti_tid, GET_PID (inferior_ptid)); | |
263 | thread_info = find_thread_pid (thread_ptid); | |
264 | ||
265 | if (thread_info == NULL) | |
266 | { | |
267 | /* New thread. Attach to it now (why wait?). */ | |
268 | attach_thread (thread_ptid, thp, &ti, 1); | |
269 | thread_info = find_thread_pid (thread_ptid); | |
270 | gdb_assert (thread_info != NULL); | |
271 | } | |
272 | ||
273 | memcpy (&thread_info->private->th, thp, sizeof (*thp)); | |
274 | thread_info->private->th_valid = 1; | |
275 | memcpy (&thread_info->private->ti, &ti, sizeof (ti)); | |
276 | thread_info->private->ti_valid = 1; | |
277 | ||
278 | if (infop != NULL) | |
279 | *(struct thread_info **) infop = thread_info; | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
284 | /* Accessor functions for the thread_db information, with caching. */ | |
fb0e1ba7 | 285 | |
5365276c DJ |
286 | static void |
287 | thread_db_map_id2thr (struct thread_info *thread_info, int fatal) | |
288 | { | |
289 | td_err_e err; | |
290 | ||
291 | if (thread_info->private->th_valid) | |
292 | return; | |
293 | ||
294 | err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid), | |
295 | &thread_info->private->th); | |
296 | if (err != TD_OK) | |
297 | { | |
298 | if (fatal) | |
299 | error ("Cannot find thread %ld: %s", | |
300 | (long) GET_THREAD (thread_info->ptid), thread_db_err_str (err)); | |
301 | } | |
302 | else | |
303 | thread_info->private->th_valid = 1; | |
304 | } | |
305 | ||
306 | static td_thrinfo_t * | |
307 | thread_db_get_info (struct thread_info *thread_info) | |
308 | { | |
309 | td_err_e err; | |
310 | ||
311 | if (thread_info->private->ti_valid) | |
312 | return &thread_info->private->ti; | |
313 | ||
314 | if (! thread_info->private->th_valid) | |
315 | thread_db_map_id2thr (thread_info, 1); | |
316 | ||
317 | err = td_thr_get_info_p (&thread_info->private->th, &thread_info->private->ti); | |
318 | if (err != TD_OK) | |
319 | error ("thread_db_get_info: cannot get thread info: %s", | |
320 | thread_db_err_str (err)); | |
321 | ||
322 | thread_info->private->ti_valid = 1; | |
323 | return &thread_info->private->ti; | |
324 | } | |
325 | \f | |
fb0e1ba7 MK |
326 | /* Convert between user-level thread ids and LWP ids. */ |
327 | ||
39f77062 KB |
328 | static ptid_t |
329 | thread_from_lwp (ptid_t ptid) | |
fb0e1ba7 | 330 | { |
fb0e1ba7 MK |
331 | td_thrhandle_t th; |
332 | td_err_e err; | |
5365276c DJ |
333 | struct thread_info *thread_info; |
334 | ptid_t thread_ptid; | |
fb0e1ba7 | 335 | |
39f77062 KB |
336 | if (GET_LWP (ptid) == 0) |
337 | ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid)); | |
fb0e1ba7 | 338 | |
39f77062 | 339 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 | 340 | |
39f77062 | 341 | err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); |
fb0e1ba7 | 342 | if (err != TD_OK) |
823ca731 | 343 | error ("Cannot find user-level thread for LWP %ld: %s", |
39f77062 | 344 | GET_LWP (ptid), thread_db_err_str (err)); |
fb0e1ba7 | 345 | |
5365276c DJ |
346 | thread_info = NULL; |
347 | thread_get_info_callback (&th, &thread_info); | |
348 | gdb_assert (thread_info && thread_info->private->ti_valid); | |
fb0e1ba7 | 349 | |
5365276c | 350 | return BUILD_THREAD (thread_info->private->ti.ti_tid, GET_PID (ptid)); |
fb0e1ba7 MK |
351 | } |
352 | ||
39f77062 KB |
353 | static ptid_t |
354 | lwp_from_thread (ptid_t ptid) | |
fb0e1ba7 | 355 | { |
5365276c DJ |
356 | struct thread_info *thread_info; |
357 | ptid_t thread_ptid; | |
fb0e1ba7 | 358 | |
21bf60fe | 359 | if (!is_thread (ptid)) |
39f77062 | 360 | return ptid; |
fb0e1ba7 | 361 | |
5365276c DJ |
362 | thread_info = find_thread_pid (ptid); |
363 | thread_db_get_info (thread_info); | |
fb0e1ba7 | 364 | |
5365276c | 365 | return BUILD_LWP (thread_info->private->ti.ti_lid, GET_PID (ptid)); |
fb0e1ba7 MK |
366 | } |
367 | \f | |
368 | ||
369 | void | |
370 | thread_db_init (struct target_ops *target) | |
371 | { | |
372 | target_beneath = target; | |
373 | } | |
374 | ||
375 | static int | |
376 | thread_db_load (void) | |
377 | { | |
378 | void *handle; | |
379 | td_err_e err; | |
380 | ||
381 | handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW); | |
382 | if (handle == NULL) | |
f7c1e0f3 MS |
383 | { |
384 | fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n", | |
385 | LIBTHREAD_DB_SO, dlerror ()); | |
386 | fprintf_filtered (gdb_stderr, | |
387 | "GDB will not be able to debug pthreads.\n\n"); | |
388 | return 0; | |
389 | } | |
fb0e1ba7 MK |
390 | |
391 | /* Initialize pointers to the dynamic library functions we will use. | |
392 | Essential functions first. */ | |
393 | ||
394 | td_init_p = dlsym (handle, "td_init"); | |
395 | if (td_init_p == NULL) | |
396 | return 0; | |
397 | ||
398 | td_ta_new_p = dlsym (handle, "td_ta_new"); | |
399 | if (td_ta_new_p == NULL) | |
400 | return 0; | |
401 | ||
402 | td_ta_map_id2thr_p = dlsym (handle, "td_ta_map_id2thr"); | |
403 | if (td_ta_map_id2thr_p == NULL) | |
404 | return 0; | |
405 | ||
406 | td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr"); | |
407 | if (td_ta_map_lwp2thr_p == NULL) | |
408 | return 0; | |
409 | ||
410 | td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter"); | |
411 | if (td_ta_thr_iter_p == NULL) | |
412 | return 0; | |
413 | ||
414 | td_thr_validate_p = dlsym (handle, "td_thr_validate"); | |
415 | if (td_thr_validate_p == NULL) | |
416 | return 0; | |
417 | ||
418 | td_thr_get_info_p = dlsym (handle, "td_thr_get_info"); | |
419 | if (td_thr_get_info_p == NULL) | |
420 | return 0; | |
421 | ||
422 | td_thr_getfpregs_p = dlsym (handle, "td_thr_getfpregs"); | |
423 | if (td_thr_getfpregs_p == NULL) | |
424 | return 0; | |
425 | ||
426 | td_thr_getgregs_p = dlsym (handle, "td_thr_getgregs"); | |
427 | if (td_thr_getgregs_p == NULL) | |
428 | return 0; | |
429 | ||
430 | td_thr_setfpregs_p = dlsym (handle, "td_thr_setfpregs"); | |
431 | if (td_thr_setfpregs_p == NULL) | |
432 | return 0; | |
433 | ||
434 | td_thr_setgregs_p = dlsym (handle, "td_thr_setgregs"); | |
435 | if (td_thr_setgregs_p == NULL) | |
436 | return 0; | |
437 | ||
438 | /* Initialize the library. */ | |
439 | err = td_init_p (); | |
440 | if (err != TD_OK) | |
441 | { | |
442 | warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err)); | |
443 | return 0; | |
444 | } | |
445 | ||
446 | /* These are not essential. */ | |
447 | td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"); | |
448 | td_ta_set_event_p = dlsym (handle, "td_ta_set_event"); | |
449 | td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"); | |
450 | td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"); | |
3f47be5c | 451 | td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"); |
fb0e1ba7 MK |
452 | |
453 | return 1; | |
454 | } | |
455 | ||
456 | static void | |
457 | enable_thread_event_reporting (void) | |
458 | { | |
459 | td_thr_events_t events; | |
460 | td_notify_t notify; | |
461 | td_err_e err; | |
462 | ||
463 | /* We cannot use the thread event reporting facility if these | |
464 | functions aren't available. */ | |
465 | if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL | |
466 | || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL) | |
467 | return; | |
468 | ||
469 | /* Set the process wide mask saying which events we're interested in. */ | |
470 | td_event_emptyset (&events); | |
471 | td_event_addset (&events, TD_CREATE); | |
472 | #if 0 | |
473 | /* FIXME: kettenis/2000-04-23: The event reporting facility is | |
474 | broken for TD_DEATH events in glibc 2.1.3, so don't enable it for | |
475 | now. */ | |
476 | td_event_addset (&events, TD_DEATH); | |
477 | #endif | |
478 | ||
479 | err = td_ta_set_event_p (thread_agent, &events); | |
480 | if (err != TD_OK) | |
481 | { | |
482 | warning ("Unable to set global thread event mask: %s", | |
483 | thread_db_err_str (err)); | |
484 | return; | |
485 | } | |
486 | ||
487 | /* Delete previous thread event breakpoints, if any. */ | |
488 | remove_thread_event_breakpoints (); | |
489 | ||
490 | /* Get address for thread creation breakpoint. */ | |
491 | err = td_ta_event_addr_p (thread_agent, TD_CREATE, ¬ify); | |
492 | if (err != TD_OK) | |
493 | { | |
494 | warning ("Unable to get location for thread creation breakpoint: %s", | |
495 | thread_db_err_str (err)); | |
496 | return; | |
497 | } | |
498 | ||
499 | /* Set up the breakpoint. */ | |
500 | td_create_bp_addr = (CORE_ADDR) notify.u.bptaddr; | |
501 | create_thread_event_breakpoint (td_create_bp_addr); | |
502 | ||
503 | /* Get address for thread death breakpoint. */ | |
504 | err = td_ta_event_addr_p (thread_agent, TD_DEATH, ¬ify); | |
505 | if (err != TD_OK) | |
506 | { | |
936742ab | 507 | warning ("Unable to get location for thread death breakpoint: %s", |
fb0e1ba7 MK |
508 | thread_db_err_str (err)); |
509 | return; | |
510 | } | |
511 | ||
512 | /* Set up the breakpoint. */ | |
513 | td_death_bp_addr = (CORE_ADDR) notify.u.bptaddr; | |
514 | create_thread_event_breakpoint (td_death_bp_addr); | |
515 | } | |
516 | ||
517 | static void | |
518 | disable_thread_event_reporting (void) | |
519 | { | |
520 | td_thr_events_t events; | |
521 | ||
522 | /* Set the process wide mask saying we aren't interested in any | |
523 | events anymore. */ | |
524 | td_event_emptyset (&events); | |
525 | td_ta_set_event_p (thread_agent, &events); | |
526 | ||
527 | /* Delete thread event breakpoints, if any. */ | |
528 | remove_thread_event_breakpoints (); | |
529 | td_create_bp_addr = 0; | |
530 | td_death_bp_addr = 0; | |
531 | } | |
532 | ||
533 | static void | |
534 | check_thread_signals (void) | |
535 | { | |
536 | #ifdef GET_THREAD_SIGNALS | |
21bf60fe | 537 | if (!thread_signals) |
fb0e1ba7 MK |
538 | { |
539 | sigset_t mask; | |
540 | int i; | |
541 | ||
542 | GET_THREAD_SIGNALS (&mask); | |
543 | sigemptyset (&thread_stop_set); | |
544 | sigemptyset (&thread_print_set); | |
545 | ||
b9569773 | 546 | for (i = 1; i < NSIG; i++) |
fb0e1ba7 MK |
547 | { |
548 | if (sigismember (&mask, i)) | |
549 | { | |
550 | if (signal_stop_update (target_signal_from_host (i), 0)) | |
551 | sigaddset (&thread_stop_set, i); | |
552 | if (signal_print_update (target_signal_from_host (i), 0)) | |
553 | sigaddset (&thread_print_set, i); | |
554 | thread_signals = 1; | |
555 | } | |
556 | } | |
557 | } | |
558 | #endif | |
559 | } | |
560 | ||
561 | static void | |
562 | disable_thread_signals (void) | |
563 | { | |
564 | #ifdef GET_THREAD_SIGNALS | |
565 | if (thread_signals) | |
566 | { | |
567 | int i; | |
568 | ||
b9569773 | 569 | for (i = 1; i < NSIG; i++) |
fb0e1ba7 MK |
570 | { |
571 | if (sigismember (&thread_stop_set, i)) | |
572 | signal_stop_update (target_signal_from_host (i), 1); | |
573 | if (sigismember (&thread_print_set, i)) | |
574 | signal_print_update (target_signal_from_host (i), 1); | |
575 | } | |
576 | ||
577 | thread_signals = 0; | |
578 | } | |
579 | #endif | |
580 | } | |
581 | ||
fb0e1ba7 MK |
582 | static void |
583 | thread_db_new_objfile (struct objfile *objfile) | |
584 | { | |
585 | td_err_e err; | |
586 | ||
5bbd998e MS |
587 | /* Don't attempt to use thread_db on targets which can not run |
588 | (core files). */ | |
589 | if (objfile == NULL || !target_has_execution) | |
bda9cb72 MK |
590 | { |
591 | /* All symbols have been discarded. If the thread_db target is | |
c194fbe1 | 592 | active, deactivate it now. */ |
bda9cb72 | 593 | if (using_thread_db) |
c194fbe1 MK |
594 | { |
595 | gdb_assert (proc_handle.pid == 0); | |
596 | unpush_target (&thread_db_ops); | |
597 | using_thread_db = 0; | |
598 | } | |
599 | ||
600 | keep_thread_db = 0; | |
bda9cb72 MK |
601 | |
602 | goto quit; | |
603 | } | |
604 | ||
fb0e1ba7 MK |
605 | if (using_thread_db) |
606 | /* Nothing to do. The thread library was already detected and the | |
607 | target vector was already activated. */ | |
608 | goto quit; | |
609 | ||
bda9cb72 MK |
610 | /* Initialize the structure that identifies the child process. Note |
611 | that at this point there is no guarantee that we actually have a | |
612 | child process. */ | |
39f77062 | 613 | proc_handle.pid = GET_PID (inferior_ptid); |
fb0e1ba7 | 614 | |
bda9cb72 | 615 | /* Now attempt to open a connection to the thread library. */ |
fb0e1ba7 MK |
616 | err = td_ta_new_p (&proc_handle, &thread_agent); |
617 | switch (err) | |
618 | { | |
619 | case TD_NOLIBTHREAD: | |
bda9cb72 | 620 | /* No thread library was detected. */ |
fb0e1ba7 MK |
621 | break; |
622 | ||
623 | case TD_OK: | |
bda9cb72 MK |
624 | /* The thread library was detected. Activate the thread_db target. */ |
625 | push_target (&thread_db_ops); | |
626 | using_thread_db = 1; | |
627 | ||
628 | /* If the thread library was detected in the main symbol file | |
629 | itself, we assume that the program was statically linked | |
630 | against the thread library and well have to keep this | |
631 | module's target vector activated until forever... Well, at | |
632 | least until all symbols have been discarded anyway (see | |
633 | above). */ | |
634 | if (objfile == symfile_objfile) | |
635 | { | |
636 | gdb_assert (proc_handle.pid == 0); | |
637 | keep_thread_db = 1; | |
638 | } | |
639 | ||
640 | /* We can only poke around if there actually is a child process. | |
641 | If there is no child process alive, postpone the steps below | |
642 | until one has been created. */ | |
643 | if (proc_handle.pid != 0) | |
644 | { | |
645 | enable_thread_event_reporting (); | |
646 | thread_db_find_new_threads (); | |
647 | } | |
fb0e1ba7 MK |
648 | break; |
649 | ||
650 | default: | |
651 | warning ("Cannot initialize thread debugging library: %s", | |
652 | thread_db_err_str (err)); | |
653 | break; | |
654 | } | |
655 | ||
656 | quit: | |
540af400 MS |
657 | if (target_new_objfile_chain) |
658 | target_new_objfile_chain (objfile); | |
fb0e1ba7 MK |
659 | } |
660 | ||
661 | static void | |
39f77062 | 662 | attach_thread (ptid_t ptid, const td_thrhandle_t *th_p, |
fb0e1ba7 MK |
663 | const td_thrinfo_t *ti_p, int verbose) |
664 | { | |
665 | struct thread_info *tp; | |
666 | td_err_e err; | |
667 | ||
668 | check_thread_signals (); | |
669 | ||
fb0e1ba7 | 670 | /* Add the thread to GDB's thread list. */ |
39f77062 | 671 | tp = add_thread (ptid); |
fb0e1ba7 | 672 | tp->private = xmalloc (sizeof (struct private_thread_info)); |
5365276c DJ |
673 | memset (tp->private, 0, sizeof (struct private_thread_info)); |
674 | ||
675 | if (verbose) | |
676 | printf_unfiltered ("[New %s]\n", target_pid_to_str (ptid)); | |
fb0e1ba7 | 677 | |
21bf60fe MK |
678 | if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE) |
679 | return; /* A zombie thread -- do not attach. */ | |
5fd913cc | 680 | |
8605d56e | 681 | /* Under GNU/Linux, we have to attach to each and every thread. */ |
fb0e1ba7 | 682 | #ifdef ATTACH_LWP |
c194fbe1 | 683 | ATTACH_LWP (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0); |
fb0e1ba7 MK |
684 | #endif |
685 | ||
686 | /* Enable thread event reporting for this thread. */ | |
687 | err = td_thr_event_enable_p (th_p, 1); | |
688 | if (err != TD_OK) | |
689 | error ("Cannot enable thread event reporting for %s: %s", | |
39f77062 | 690 | target_pid_to_str (ptid), thread_db_err_str (err)); |
fb0e1ba7 MK |
691 | } |
692 | ||
c194fbe1 MK |
693 | static void |
694 | thread_db_attach (char *args, int from_tty) | |
695 | { | |
696 | target_beneath->to_attach (args, from_tty); | |
697 | ||
698 | /* Destroy thread info; it's no longer valid. */ | |
699 | init_thread_list (); | |
700 | ||
701 | /* The child process is now the actual multi-threaded | |
702 | program. Snatch its process ID... */ | |
703 | proc_handle.pid = GET_PID (inferior_ptid); | |
704 | ||
705 | /* ...and perform the remaining initialization steps. */ | |
706 | enable_thread_event_reporting (); | |
707 | thread_db_find_new_threads(); | |
708 | } | |
709 | ||
fb0e1ba7 | 710 | static void |
39f77062 | 711 | detach_thread (ptid_t ptid, int verbose) |
fb0e1ba7 MK |
712 | { |
713 | if (verbose) | |
39f77062 | 714 | printf_unfiltered ("[%s exited]\n", target_pid_to_str (ptid)); |
fb0e1ba7 MK |
715 | } |
716 | ||
717 | static void | |
718 | thread_db_detach (char *args, int from_tty) | |
719 | { | |
720 | disable_thread_event_reporting (); | |
c194fbe1 MK |
721 | |
722 | /* There's no need to save & restore inferior_ptid here, since the | |
723 | inferior is supposed to be survive this function call. */ | |
724 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
725 | ||
726 | /* Forget about the child's process ID. We shouldn't need it | |
727 | anymore. */ | |
728 | proc_handle.pid = 0; | |
fb0e1ba7 MK |
729 | |
730 | target_beneath->to_detach (args, from_tty); | |
731 | } | |
732 | ||
5365276c DJ |
733 | static int |
734 | clear_lwpid_callback (struct thread_info *thread, void *dummy) | |
735 | { | |
736 | /* If we know that our thread implementation is 1-to-1, we could save | |
737 | a certain amount of information; it's not clear how much, so we | |
738 | are always conservative. */ | |
739 | ||
740 | thread->private->th_valid = 0; | |
741 | thread->private->ti_valid = 0; | |
742 | ||
743 | return 0; | |
744 | } | |
745 | ||
fb0e1ba7 | 746 | static void |
39f77062 | 747 | thread_db_resume (ptid_t ptid, int step, enum target_signal signo) |
fb0e1ba7 | 748 | { |
39f77062 | 749 | struct cleanup *old_chain = save_inferior_ptid (); |
fb0e1ba7 | 750 | |
39f77062 KB |
751 | if (GET_PID (ptid) == -1) |
752 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
753 | else if (is_thread (ptid)) | |
754 | ptid = lwp_from_thread (ptid); | |
fb0e1ba7 | 755 | |
5365276c DJ |
756 | /* Clear cached data which may not be valid after the resume. */ |
757 | iterate_over_threads (clear_lwpid_callback, NULL); | |
758 | ||
39f77062 | 759 | target_beneath->to_resume (ptid, step, signo); |
fb0e1ba7 MK |
760 | |
761 | do_cleanups (old_chain); | |
762 | } | |
763 | ||
764 | /* Check if PID is currently stopped at the location of a thread event | |
765 | breakpoint location. If it is, read the event message and act upon | |
766 | the event. */ | |
767 | ||
768 | static void | |
39f77062 | 769 | check_event (ptid_t ptid) |
fb0e1ba7 MK |
770 | { |
771 | td_event_msg_t msg; | |
772 | td_thrinfo_t ti; | |
773 | td_err_e err; | |
774 | CORE_ADDR stop_pc; | |
775 | ||
776 | /* Bail out early if we're not at a thread event breakpoint. */ | |
39f77062 | 777 | stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK; |
fb0e1ba7 MK |
778 | if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr) |
779 | return; | |
780 | ||
781 | err = td_ta_event_getmsg_p (thread_agent, &msg); | |
782 | if (err != TD_OK) | |
783 | { | |
784 | if (err == TD_NOMSG) | |
785 | return; | |
786 | ||
787 | error ("Cannot get thread event message: %s", thread_db_err_str (err)); | |
788 | } | |
789 | ||
790 | err = td_thr_get_info_p (msg.th_p, &ti); | |
791 | if (err != TD_OK) | |
3197744f MS |
792 | error ("check_event: cannot get thread info: %s", |
793 | thread_db_err_str (err)); | |
fb0e1ba7 | 794 | |
39f77062 | 795 | ptid = BUILD_THREAD (ti.ti_tid, GET_PID (ptid)); |
fb0e1ba7 MK |
796 | |
797 | switch (msg.event) | |
798 | { | |
799 | case TD_CREATE: | |
800 | #if 0 | |
801 | /* FIXME: kettenis/2000-08-26: Since we use td_ta_event_getmsg, | |
802 | there is no guarantee that the breakpoint will match the | |
803 | event. Should we use td_thr_event_getmsg instead? */ | |
804 | ||
805 | if (stop_pc != td_create_bp_addr) | |
806 | error ("Thread creation event doesn't match breakpoint."); | |
807 | #endif | |
808 | ||
540af400 MS |
809 | /* We may already know about this thread, for instance when the |
810 | user has issued the `info threads' command before the SIGTRAP | |
811 | for hitting the thread creation breakpoint was reported. */ | |
21bf60fe | 812 | if (!in_thread_list (ptid)) |
39f77062 | 813 | attach_thread (ptid, msg.th_p, &ti, 1); |
fb0e1ba7 MK |
814 | return; |
815 | ||
816 | case TD_DEATH: | |
817 | #if 0 | |
818 | /* FIXME: See TD_CREATE. */ | |
819 | ||
820 | if (stop_pc != td_death_bp_addr) | |
821 | error ("Thread death event doesn't match breakpoint."); | |
822 | #endif | |
823 | ||
21bf60fe | 824 | if (!in_thread_list (ptid)) |
fb0e1ba7 MK |
825 | error ("Spurious thread death event."); |
826 | ||
39f77062 | 827 | detach_thread (ptid, 1); |
fb0e1ba7 MK |
828 | return; |
829 | ||
830 | default: | |
831 | error ("Spurious thread event."); | |
832 | } | |
833 | } | |
834 | ||
39f77062 KB |
835 | static ptid_t |
836 | thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
fb0e1ba7 | 837 | { |
39f77062 | 838 | extern ptid_t trap_ptid; |
fb0e1ba7 | 839 | |
39f77062 KB |
840 | if (GET_PID (ptid) != -1 && is_thread (ptid)) |
841 | ptid = lwp_from_thread (ptid); | |
fb0e1ba7 | 842 | |
39f77062 | 843 | ptid = target_beneath->to_wait (ptid, ourstatus); |
fb0e1ba7 | 844 | |
bda9cb72 MK |
845 | if (proc_handle.pid == 0) |
846 | /* The current child process isn't the actual multi-threaded | |
847 | program yet, so don't try to do any special thread-specific | |
848 | post-processing and bail out early. */ | |
39f77062 | 849 | return ptid; |
bda9cb72 | 850 | |
fb0e1ba7 | 851 | if (ourstatus->kind == TARGET_WAITKIND_EXITED) |
39f77062 | 852 | return pid_to_ptid (-1); |
fb0e1ba7 MK |
853 | |
854 | if (ourstatus->kind == TARGET_WAITKIND_STOPPED | |
855 | && ourstatus->value.sig == TARGET_SIGNAL_TRAP) | |
856 | /* Check for a thread event. */ | |
39f77062 | 857 | check_event (ptid); |
fb0e1ba7 | 858 | |
39f77062 KB |
859 | if (!ptid_equal (trap_ptid, null_ptid)) |
860 | trap_ptid = thread_from_lwp (trap_ptid); | |
fb0e1ba7 | 861 | |
39f77062 | 862 | return thread_from_lwp (ptid); |
fb0e1ba7 MK |
863 | } |
864 | ||
865 | static int | |
866 | thread_db_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, | |
e5da8f38 | 867 | struct mem_attrib *attrib, |
fb0e1ba7 MK |
868 | struct target_ops *target) |
869 | { | |
39f77062 | 870 | struct cleanup *old_chain = save_inferior_ptid (); |
fb0e1ba7 MK |
871 | int xfer; |
872 | ||
39f77062 | 873 | if (is_thread (inferior_ptid)) |
fb0e1ba7 MK |
874 | { |
875 | /* FIXME: This seems to be necessary to make sure breakpoints | |
876 | are removed. */ | |
21bf60fe | 877 | if (!target_thread_alive (inferior_ptid)) |
39f77062 | 878 | inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid)); |
fb0e1ba7 | 879 | else |
39f77062 | 880 | inferior_ptid = lwp_from_thread (inferior_ptid); |
fb0e1ba7 MK |
881 | } |
882 | ||
e5da8f38 | 883 | xfer = target_beneath->to_xfer_memory (memaddr, myaddr, len, write, attrib, target); |
fb0e1ba7 MK |
884 | |
885 | do_cleanups (old_chain); | |
886 | return xfer; | |
887 | } | |
888 | ||
889 | static void | |
890 | thread_db_fetch_registers (int regno) | |
891 | { | |
5365276c | 892 | struct thread_info *thread_info; |
fb0e1ba7 MK |
893 | prgregset_t gregset; |
894 | gdb_prfpregset_t fpregset; | |
895 | td_err_e err; | |
896 | ||
21bf60fe | 897 | if (!is_thread (inferior_ptid)) |
fb0e1ba7 MK |
898 | { |
899 | /* Pass the request to the target beneath us. */ | |
900 | target_beneath->to_fetch_registers (regno); | |
901 | return; | |
902 | } | |
903 | ||
5365276c DJ |
904 | thread_info = find_thread_pid (inferior_ptid); |
905 | thread_db_map_id2thr (thread_info, 1); | |
fb0e1ba7 | 906 | |
5365276c | 907 | err = td_thr_getgregs_p (&thread_info->private->th, gregset); |
fb0e1ba7 MK |
908 | if (err != TD_OK) |
909 | error ("Cannot fetch general-purpose registers for thread %ld: %s", | |
39f77062 | 910 | (long) GET_THREAD (inferior_ptid), thread_db_err_str (err)); |
fb0e1ba7 | 911 | |
5365276c | 912 | err = td_thr_getfpregs_p (&thread_info->private->th, &fpregset); |
fb0e1ba7 MK |
913 | if (err != TD_OK) |
914 | error ("Cannot get floating-point registers for thread %ld: %s", | |
39f77062 | 915 | (long) GET_THREAD (inferior_ptid), thread_db_err_str (err)); |
fb0e1ba7 MK |
916 | |
917 | /* Note that we must call supply_gregset after calling the thread_db | |
918 | routines because the thread_db routines call ps_lgetgregs and | |
919 | friends which clobber GDB's register cache. */ | |
920 | supply_gregset ((gdb_gregset_t *) gregset); | |
921 | supply_fpregset (&fpregset); | |
922 | } | |
923 | ||
924 | static void | |
925 | thread_db_store_registers (int regno) | |
926 | { | |
fb0e1ba7 MK |
927 | prgregset_t gregset; |
928 | gdb_prfpregset_t fpregset; | |
929 | td_err_e err; | |
5365276c | 930 | struct thread_info *thread_info; |
fb0e1ba7 | 931 | |
21bf60fe | 932 | if (!is_thread (inferior_ptid)) |
fb0e1ba7 MK |
933 | { |
934 | /* Pass the request to the target beneath us. */ | |
935 | target_beneath->to_store_registers (regno); | |
936 | return; | |
937 | } | |
938 | ||
5365276c DJ |
939 | thread_info = find_thread_pid (inferior_ptid); |
940 | thread_db_map_id2thr (thread_info, 1); | |
fb0e1ba7 MK |
941 | |
942 | if (regno != -1) | |
943 | { | |
6789195b | 944 | char *raw = alloca (max_register_size (current_gdbarch)); |
fb0e1ba7 | 945 | |
4caf0990 | 946 | deprecated_read_register_gen (regno, raw); |
fb0e1ba7 MK |
947 | thread_db_fetch_registers (-1); |
948 | supply_register (regno, raw); | |
949 | } | |
950 | ||
951 | fill_gregset ((gdb_gregset_t *) gregset, -1); | |
952 | fill_fpregset (&fpregset, -1); | |
953 | ||
5365276c | 954 | err = td_thr_setgregs_p (&thread_info->private->th, gregset); |
fb0e1ba7 MK |
955 | if (err != TD_OK) |
956 | error ("Cannot store general-purpose registers for thread %ld: %s", | |
39f77062 | 957 | (long) GET_THREAD (inferior_ptid), thread_db_err_str (err)); |
5365276c | 958 | err = td_thr_setfpregs_p (&thread_info->private->th, &fpregset); |
fb0e1ba7 MK |
959 | if (err != TD_OK) |
960 | error ("Cannot store floating-point registers for thread %ld: %s", | |
39f77062 | 961 | (long) GET_THREAD (inferior_ptid), thread_db_err_str (err)); |
fb0e1ba7 MK |
962 | } |
963 | ||
964 | static void | |
965 | thread_db_kill (void) | |
966 | { | |
c194fbe1 MK |
967 | /* There's no need to save & restore inferior_ptid here, since the |
968 | inferior isn't supposed to survive this function call. */ | |
969 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
fb0e1ba7 MK |
970 | target_beneath->to_kill (); |
971 | } | |
972 | ||
973 | static void | |
974 | thread_db_create_inferior (char *exec_file, char *allargs, char **env) | |
975 | { | |
21bf60fe | 976 | if (!keep_thread_db) |
c194fbe1 MK |
977 | { |
978 | unpush_target (&thread_db_ops); | |
979 | using_thread_db = 0; | |
980 | } | |
981 | ||
bda9cb72 MK |
982 | target_beneath->to_create_inferior (exec_file, allargs, env); |
983 | } | |
fb0e1ba7 | 984 | |
bda9cb72 | 985 | static void |
39f77062 | 986 | thread_db_post_startup_inferior (ptid_t ptid) |
bda9cb72 MK |
987 | { |
988 | if (proc_handle.pid == 0) | |
989 | { | |
990 | /* The child process is now the actual multi-threaded | |
991 | program. Snatch its process ID... */ | |
39f77062 | 992 | proc_handle.pid = GET_PID (ptid); |
fb0e1ba7 | 993 | |
bda9cb72 MK |
994 | /* ...and perform the remaining initialization steps. */ |
995 | enable_thread_event_reporting (); | |
21bf60fe | 996 | thread_db_find_new_threads (); |
bda9cb72 | 997 | } |
fb0e1ba7 MK |
998 | } |
999 | ||
1000 | static void | |
1001 | thread_db_mourn_inferior (void) | |
1002 | { | |
1003 | remove_thread_event_breakpoints (); | |
c194fbe1 MK |
1004 | |
1005 | /* Forget about the child's process ID. We shouldn't need it | |
1006 | anymore. */ | |
1007 | proc_handle.pid = 0; | |
fb0e1ba7 MK |
1008 | |
1009 | target_beneath->to_mourn_inferior (); | |
1010 | } | |
1011 | ||
1012 | static int | |
39f77062 | 1013 | thread_db_thread_alive (ptid_t ptid) |
fb0e1ba7 | 1014 | { |
5fd913cc | 1015 | td_thrhandle_t th; |
21bf60fe | 1016 | td_err_e err; |
5fd913cc | 1017 | |
39f77062 | 1018 | if (is_thread (ptid)) |
fb0e1ba7 | 1019 | { |
5365276c DJ |
1020 | struct thread_info *thread_info; |
1021 | thread_info = find_thread_pid (ptid); | |
fb0e1ba7 | 1022 | |
5365276c DJ |
1023 | thread_db_map_id2thr (thread_info, 0); |
1024 | if (! thread_info->private->th_valid) | |
fb0e1ba7 MK |
1025 | return 0; |
1026 | ||
5365276c | 1027 | err = td_thr_validate_p (&thread_info->private->th); |
5fd913cc MS |
1028 | if (err != TD_OK) |
1029 | return 0; | |
1030 | ||
5365276c DJ |
1031 | if (! thread_info->private->ti_valid) |
1032 | { | |
1033 | err = td_thr_get_info_p (&thread_info->private->th, &thread_info->private->ti); | |
1034 | if (err != TD_OK) | |
1035 | return 0; | |
1036 | thread_info->private->ti_valid = 1; | |
1037 | } | |
1038 | ||
1039 | if (thread_info->private->ti.ti_state == TD_THR_UNKNOWN | |
1040 | || thread_info->private->ti.ti_state == TD_THR_ZOMBIE) | |
21bf60fe | 1041 | return 0; /* A zombie thread. */ |
5fd913cc | 1042 | |
fb0e1ba7 MK |
1043 | return 1; |
1044 | } | |
1045 | ||
1046 | if (target_beneath->to_thread_alive) | |
39f77062 | 1047 | return target_beneath->to_thread_alive (ptid); |
fb0e1ba7 MK |
1048 | |
1049 | return 0; | |
1050 | } | |
1051 | ||
1052 | static int | |
1053 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) | |
1054 | { | |
1055 | td_thrinfo_t ti; | |
1056 | td_err_e err; | |
39f77062 | 1057 | ptid_t ptid; |
fb0e1ba7 MK |
1058 | |
1059 | err = td_thr_get_info_p (th_p, &ti); | |
1060 | if (err != TD_OK) | |
3197744f MS |
1061 | error ("find_new_threads_callback: cannot get thread info: %s", |
1062 | thread_db_err_str (err)); | |
fb0e1ba7 | 1063 | |
21bf60fe MK |
1064 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) |
1065 | return 0; /* A zombie -- ignore. */ | |
5fd913cc | 1066 | |
39f77062 | 1067 | ptid = BUILD_THREAD (ti.ti_tid, GET_PID (inferior_ptid)); |
fb0e1ba7 | 1068 | |
21bf60fe | 1069 | if (!in_thread_list (ptid)) |
39f77062 | 1070 | attach_thread (ptid, th_p, &ti, 1); |
fb0e1ba7 MK |
1071 | |
1072 | return 0; | |
1073 | } | |
1074 | ||
1075 | static void | |
1076 | thread_db_find_new_threads (void) | |
1077 | { | |
1078 | td_err_e err; | |
1079 | ||
1080 | /* Iterate over all user-space threads to discover new threads. */ | |
1081 | err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL, | |
1082 | TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, | |
1083 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); | |
1084 | if (err != TD_OK) | |
1085 | error ("Cannot find new threads: %s", thread_db_err_str (err)); | |
1086 | } | |
1087 | ||
1088 | static char * | |
39f77062 | 1089 | thread_db_pid_to_str (ptid_t ptid) |
fb0e1ba7 | 1090 | { |
39f77062 | 1091 | if (is_thread (ptid)) |
fb0e1ba7 MK |
1092 | { |
1093 | static char buf[64]; | |
5365276c | 1094 | td_thrinfo_t *ti_p; |
fb0e1ba7 | 1095 | td_err_e err; |
5365276c | 1096 | struct thread_info *thread_info; |
fb0e1ba7 | 1097 | |
5365276c DJ |
1098 | thread_info = find_thread_pid (ptid); |
1099 | thread_db_map_id2thr (thread_info, 0); | |
1100 | if (! thread_info->private->th_valid) | |
1101 | { | |
1102 | snprintf (buf, sizeof (buf), "Thread %ld (Missing)", GET_THREAD (ptid)); | |
1103 | return buf; | |
1104 | } | |
fb0e1ba7 | 1105 | |
5365276c | 1106 | ti_p = thread_db_get_info (thread_info); |
fb0e1ba7 | 1107 | |
5365276c | 1108 | if (ti_p->ti_state == TD_THR_ACTIVE && ti_p->ti_lid != 0) |
fb0e1ba7 MK |
1109 | { |
1110 | snprintf (buf, sizeof (buf), "Thread %ld (LWP %d)", | |
5365276c | 1111 | (long) ti_p->ti_tid, ti_p->ti_lid); |
fb0e1ba7 MK |
1112 | } |
1113 | else | |
1114 | { | |
1115 | snprintf (buf, sizeof (buf), "Thread %ld (%s)", | |
5365276c | 1116 | (long) ti_p->ti_tid, thread_db_state_str (ti_p->ti_state)); |
fb0e1ba7 MK |
1117 | } |
1118 | ||
1119 | return buf; | |
1120 | } | |
1121 | ||
39f77062 KB |
1122 | if (target_beneath->to_pid_to_str (ptid)) |
1123 | return target_beneath->to_pid_to_str (ptid); | |
fb0e1ba7 | 1124 | |
39f77062 | 1125 | return normal_pid_to_str (ptid); |
fb0e1ba7 MK |
1126 | } |
1127 | ||
3f47be5c EZ |
1128 | /* Get the address of the thread local variable in OBJFILE which is |
1129 | stored at OFFSET within the thread local storage for thread PTID. */ | |
1130 | ||
1131 | static CORE_ADDR | |
1132 | thread_db_get_thread_local_address (ptid_t ptid, struct objfile *objfile, | |
1133 | CORE_ADDR offset) | |
1134 | { | |
1135 | if (is_thread (ptid)) | |
1136 | { | |
1137 | int objfile_is_library = (objfile->flags & OBJF_SHARED); | |
1138 | td_err_e err; | |
3f47be5c EZ |
1139 | void *address; |
1140 | CORE_ADDR lm; | |
5365276c | 1141 | struct thread_info *thread_info; |
3f47be5c EZ |
1142 | |
1143 | /* glibc doesn't provide the needed interface. */ | |
1144 | if (! td_thr_tls_get_addr_p) | |
1145 | error ("Cannot find thread-local variables in this thread library."); | |
1146 | ||
1147 | /* Get the address of the link map for this objfile. */ | |
1148 | lm = svr4_fetch_objfile_link_map (objfile); | |
1149 | ||
1150 | /* Whoops, we couldn't find one. Bail out. */ | |
1151 | if (!lm) | |
1152 | { | |
1153 | if (objfile_is_library) | |
1154 | error ("Cannot find shared library `%s' link_map in dynamic" | |
1155 | " linker's module list", objfile->name); | |
1156 | else | |
1157 | error ("Cannot find executable file `%s' link_map in dynamic" | |
1158 | " linker's module list", objfile->name); | |
1159 | } | |
1160 | ||
1161 | /* Get info about the thread. */ | |
5365276c DJ |
1162 | thread_info = find_thread_pid (ptid); |
1163 | thread_db_map_id2thr (thread_info, 1); | |
1164 | ||
3f47be5c | 1165 | /* Finally, get the address of the variable. */ |
5365276c DJ |
1166 | err = td_thr_tls_get_addr_p (&thread_info->private->th, (void *) lm, |
1167 | offset, &address); | |
3f47be5c EZ |
1168 | |
1169 | #ifdef THREAD_DB_HAS_TD_NOTALLOC | |
1170 | /* The memory hasn't been allocated, yet. */ | |
1171 | if (err == TD_NOTALLOC) | |
1172 | { | |
1173 | /* Now, if libthread_db provided the initialization image's | |
1174 | address, we *could* try to build a non-lvalue value from | |
1175 | the initialization image. */ | |
1176 | if (objfile_is_library) | |
1177 | error ("The inferior has not yet allocated storage for" | |
1178 | " thread-local variables in\n" | |
1179 | "the shared library `%s'\n" | |
1180 | "for the thread %ld", | |
1181 | objfile->name, (long) GET_THREAD (ptid)); | |
1182 | else | |
1183 | error ("The inferior has not yet allocated storage for" | |
1184 | " thread-local variables in\n" | |
1185 | "the executable `%s'\n" | |
1186 | "for the thread %ld", | |
1187 | objfile->name, (long) GET_THREAD (ptid)); | |
1188 | } | |
1189 | #endif | |
1190 | ||
1191 | /* Something else went wrong. */ | |
1192 | if (err != TD_OK) | |
1193 | { | |
1194 | if (objfile_is_library) | |
1195 | error ("Cannot find thread-local storage for thread %ld, " | |
1196 | "shared library %s:\n%s", | |
1197 | (long) GET_THREAD (ptid), | |
1198 | objfile->name, | |
1199 | thread_db_err_str (err)); | |
1200 | else | |
1201 | error ("Cannot find thread-local storage for thread %ld, " | |
1202 | "executable file %s:\n%s", | |
1203 | (long) GET_THREAD (ptid), | |
1204 | objfile->name, | |
1205 | thread_db_err_str (err)); | |
1206 | } | |
1207 | ||
1208 | /* Cast assuming host == target. Joy. */ | |
1209 | return (CORE_ADDR) address; | |
1210 | } | |
1211 | ||
1212 | if (target_beneath->to_get_thread_local_address) | |
1213 | return target_beneath->to_get_thread_local_address (ptid, objfile, offset); | |
1214 | ||
1215 | error ("Cannot find thread-local values on this target."); | |
1216 | } | |
1217 | ||
fb0e1ba7 MK |
1218 | static void |
1219 | init_thread_db_ops (void) | |
1220 | { | |
1221 | thread_db_ops.to_shortname = "multi-thread"; | |
1222 | thread_db_ops.to_longname = "multi-threaded child process."; | |
1223 | thread_db_ops.to_doc = "Threads and pthreads support."; | |
c194fbe1 | 1224 | thread_db_ops.to_attach = thread_db_attach; |
fb0e1ba7 MK |
1225 | thread_db_ops.to_detach = thread_db_detach; |
1226 | thread_db_ops.to_resume = thread_db_resume; | |
1227 | thread_db_ops.to_wait = thread_db_wait; | |
1228 | thread_db_ops.to_fetch_registers = thread_db_fetch_registers; | |
1229 | thread_db_ops.to_store_registers = thread_db_store_registers; | |
1230 | thread_db_ops.to_xfer_memory = thread_db_xfer_memory; | |
1231 | thread_db_ops.to_kill = thread_db_kill; | |
1232 | thread_db_ops.to_create_inferior = thread_db_create_inferior; | |
bda9cb72 | 1233 | thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior; |
fb0e1ba7 MK |
1234 | thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior; |
1235 | thread_db_ops.to_thread_alive = thread_db_thread_alive; | |
1236 | thread_db_ops.to_find_new_threads = thread_db_find_new_threads; | |
1237 | thread_db_ops.to_pid_to_str = thread_db_pid_to_str; | |
1238 | thread_db_ops.to_stratum = thread_stratum; | |
1239 | thread_db_ops.to_has_thread_control = tc_schedlock; | |
3f47be5c EZ |
1240 | thread_db_ops.to_get_thread_local_address |
1241 | = thread_db_get_thread_local_address; | |
fb0e1ba7 MK |
1242 | thread_db_ops.to_magic = OPS_MAGIC; |
1243 | } | |
1244 | ||
1245 | void | |
1246 | _initialize_thread_db (void) | |
1247 | { | |
1248 | /* Only initialize the module if we can load libthread_db. */ | |
1249 | if (thread_db_load ()) | |
1250 | { | |
1251 | init_thread_db_ops (); | |
1252 | add_target (&thread_db_ops); | |
1253 | ||
1254 | /* Add ourselves to objfile event chain. */ | |
540af400 | 1255 | target_new_objfile_chain = target_new_objfile_hook; |
fb0e1ba7 MK |
1256 | target_new_objfile_hook = thread_db_new_objfile; |
1257 | } | |
1258 | } |