Commit | Line | Data |
---|---|---|
fb0e1ba7 | 1 | /* libthread_db assisted debugging support, generic parts. |
1bac305b | 2 | |
a0841d7a | 3 | Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006 |
10d6c8cd | 4 | Free Software Foundation, Inc. |
fb0e1ba7 MK |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
197e01b6 EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
fb0e1ba7 MK |
22 | |
23 | #include "defs.h" | |
24 | ||
25 | #include "gdb_assert.h" | |
26 | #include <dlfcn.h> | |
27 | #include "gdb_proc_service.h" | |
28 | #include "gdb_thread_db.h" | |
29 | ||
bda9cb72 | 30 | #include "bfd.h" |
93ad78a7 | 31 | #include "exceptions.h" |
fb0e1ba7 MK |
32 | #include "gdbthread.h" |
33 | #include "inferior.h" | |
bda9cb72 MK |
34 | #include "symfile.h" |
35 | #include "objfiles.h" | |
fb0e1ba7 | 36 | #include "target.h" |
4e052eda | 37 | #include "regcache.h" |
3f47be5c | 38 | #include "solib-svr4.h" |
16451949 | 39 | #include "gdbcore.h" |
0ec9a092 | 40 | #include "linux-nat.h" |
fb0e1ba7 | 41 | |
a2f23071 DJ |
42 | #ifdef HAVE_GNU_LIBC_VERSION_H |
43 | #include <gnu/libc-version.h> | |
44 | #endif | |
45 | ||
fb0e1ba7 MK |
46 | #ifndef LIBTHREAD_DB_SO |
47 | #define LIBTHREAD_DB_SO "libthread_db.so.1" | |
48 | #endif | |
49 | ||
8605d56e AC |
50 | /* If we're running on GNU/Linux, we must explicitly attach to any new |
51 | threads. */ | |
fb0e1ba7 MK |
52 | |
53 | /* FIXME: There is certainly some room for improvements: | |
54 | - Cache LWP ids. | |
55 | - Bypass libthread_db when fetching or storing registers for | |
56 | threads bound to a LWP. */ | |
57 | ||
58 | /* This module's target vector. */ | |
59 | static struct target_ops thread_db_ops; | |
60 | ||
61 | /* The target vector that we call for things this module can't handle. */ | |
62 | static struct target_ops *target_beneath; | |
63 | ||
64 | /* Pointer to the next function on the objfile event chain. */ | |
b4acd559 | 65 | static void (*target_new_objfile_chain) (struct objfile * objfile); |
fb0e1ba7 MK |
66 | |
67 | /* Non-zero if we're using this module's target vector. */ | |
68 | static int using_thread_db; | |
69 | ||
70 | /* Non-zero if we have determined the signals used by the threads | |
71 | library. */ | |
72 | static int thread_signals; | |
73 | static sigset_t thread_stop_set; | |
74 | static sigset_t thread_print_set; | |
75 | ||
76 | /* Structure that identifies the child process for the | |
77 | <proc_service.h> interface. */ | |
78 | static struct ps_prochandle proc_handle; | |
79 | ||
80 | /* Connection to the libthread_db library. */ | |
81 | static td_thragent_t *thread_agent; | |
82 | ||
83 | /* Pointers to the libthread_db functions. */ | |
84 | ||
85 | static td_err_e (*td_init_p) (void); | |
86 | ||
b4acd559 JJ |
87 | static td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, |
88 | td_thragent_t **ta); | |
fb0e1ba7 MK |
89 | static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt, |
90 | td_thrhandle_t *__th); | |
b4acd559 JJ |
91 | static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, |
92 | lwpid_t lwpid, td_thrhandle_t *th); | |
fb0e1ba7 | 93 | static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta, |
b4acd559 JJ |
94 | td_thr_iter_f *callback, void *cbdata_p, |
95 | td_thr_state_e state, int ti_pri, | |
96 | sigset_t *ti_sigmask_p, | |
fb0e1ba7 MK |
97 | unsigned int ti_user_flags); |
98 | static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta, | |
99 | td_event_e event, td_notify_t *ptr); | |
100 | static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta, | |
101 | td_thr_events_t *event); | |
102 | static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta, | |
103 | td_event_msg_t *msg); | |
104 | ||
105 | static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th); | |
106 | static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th, | |
107 | td_thrinfo_t *infop); | |
b4acd559 JJ |
108 | static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, |
109 | int event); | |
fb0e1ba7 | 110 | |
3f47be5c | 111 | static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th, |
b4acd559 JJ |
112 | void *map_address, |
113 | size_t offset, void **address); | |
3f47be5c | 114 | |
fb0e1ba7 MK |
115 | /* Location of the thread creation event breakpoint. The code at this |
116 | location in the child process will be called by the pthread library | |
117 | whenever a new thread is created. By setting a special breakpoint | |
118 | at this location, GDB can detect when a new thread is created. We | |
119 | obtain this location via the td_ta_event_addr call. */ | |
120 | static CORE_ADDR td_create_bp_addr; | |
121 | ||
122 | /* Location of the thread death event breakpoint. */ | |
123 | static CORE_ADDR td_death_bp_addr; | |
124 | ||
125 | /* Prototypes for local functions. */ | |
bda9cb72 | 126 | static void thread_db_find_new_threads (void); |
5365276c DJ |
127 | static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p, |
128 | const td_thrinfo_t *ti_p, int verbose); | |
a2f23071 | 129 | static void detach_thread (ptid_t ptid, int verbose); |
fb0e1ba7 MK |
130 | \f |
131 | ||
132 | /* Building process ids. */ | |
133 | ||
ca6724c1 KB |
134 | #define GET_PID(ptid) ptid_get_pid (ptid) |
135 | #define GET_LWP(ptid) ptid_get_lwp (ptid) | |
136 | #define GET_THREAD(ptid) ptid_get_tid (ptid) | |
fb0e1ba7 | 137 | |
ca6724c1 KB |
138 | #define is_lwp(ptid) (GET_LWP (ptid) != 0) |
139 | #define is_thread(ptid) (GET_THREAD (ptid) != 0) | |
fb0e1ba7 | 140 | |
ca6724c1 | 141 | #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) |
fb0e1ba7 MK |
142 | \f |
143 | ||
5365276c DJ |
144 | /* Use "struct private_thread_info" to cache thread state. This is |
145 | a substantial optimization. */ | |
146 | ||
fb0e1ba7 MK |
147 | struct private_thread_info |
148 | { | |
a2f23071 DJ |
149 | /* Flag set when we see a TD_DEATH event for this thread. */ |
150 | unsigned int dying:1; | |
151 | ||
5365276c | 152 | /* Cached thread state. */ |
b4acd559 JJ |
153 | unsigned int th_valid:1; |
154 | unsigned int ti_valid:1; | |
5365276c DJ |
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 | } | |
fb0e1ba7 | 215 | \f |
5365276c | 216 | /* A callback function for td_ta_thr_iter, which we use to map all |
cdbc0b18 | 217 | threads to LWPs. |
5365276c DJ |
218 | |
219 | THP is a handle to the current thread; if INFOP is not NULL, the | |
220 | struct thread_info associated with this thread is returned in | |
b9b5d7ea JJ |
221 | *INFOP. |
222 | ||
223 | If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise, | |
224 | zero is returned to indicate success. */ | |
5365276c DJ |
225 | |
226 | static int | |
227 | thread_get_info_callback (const td_thrhandle_t *thp, void *infop) | |
228 | { | |
229 | td_thrinfo_t ti; | |
230 | td_err_e err; | |
231 | struct thread_info *thread_info; | |
232 | ptid_t thread_ptid; | |
233 | ||
234 | err = td_thr_get_info_p (thp, &ti); | |
235 | if (err != TD_OK) | |
8a3fe4f8 | 236 | error (_("thread_get_info_callback: cannot get thread info: %s"), |
5365276c DJ |
237 | thread_db_err_str (err)); |
238 | ||
239 | /* Fill the cache. */ | |
1bac0d4d | 240 | thread_ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid); |
5365276c DJ |
241 | thread_info = find_thread_pid (thread_ptid); |
242 | ||
b9b5d7ea | 243 | /* In the case of a zombie thread, don't continue. We don't want to |
f90ef764 | 244 | attach to it thinking it is a new thread. */ |
b9b5d7ea JJ |
245 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) |
246 | { | |
247 | if (infop != NULL) | |
248 | *(struct thread_info **) infop = thread_info; | |
f90ef764 JJ |
249 | if (thread_info != NULL) |
250 | { | |
251 | memcpy (&thread_info->private->th, thp, sizeof (*thp)); | |
252 | thread_info->private->th_valid = 1; | |
253 | memcpy (&thread_info->private->ti, &ti, sizeof (ti)); | |
254 | thread_info->private->ti_valid = 1; | |
255 | } | |
b9b5d7ea JJ |
256 | return TD_THR_ZOMBIE; |
257 | } | |
258 | ||
5365276c DJ |
259 | if (thread_info == NULL) |
260 | { | |
261 | /* New thread. Attach to it now (why wait?). */ | |
262 | attach_thread (thread_ptid, thp, &ti, 1); | |
263 | thread_info = find_thread_pid (thread_ptid); | |
264 | gdb_assert (thread_info != NULL); | |
265 | } | |
266 | ||
267 | memcpy (&thread_info->private->th, thp, sizeof (*thp)); | |
268 | thread_info->private->th_valid = 1; | |
269 | memcpy (&thread_info->private->ti, &ti, sizeof (ti)); | |
270 | thread_info->private->ti_valid = 1; | |
271 | ||
272 | if (infop != NULL) | |
273 | *(struct thread_info **) infop = thread_info; | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
278 | /* Accessor functions for the thread_db information, with caching. */ | |
fb0e1ba7 | 279 | |
5365276c DJ |
280 | static void |
281 | thread_db_map_id2thr (struct thread_info *thread_info, int fatal) | |
282 | { | |
283 | td_err_e err; | |
284 | ||
285 | if (thread_info->private->th_valid) | |
286 | return; | |
287 | ||
288 | err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid), | |
289 | &thread_info->private->th); | |
290 | if (err != TD_OK) | |
291 | { | |
292 | if (fatal) | |
8a3fe4f8 | 293 | error (_("Cannot find thread %ld: %s"), |
b4acd559 JJ |
294 | (long) GET_THREAD (thread_info->ptid), |
295 | thread_db_err_str (err)); | |
5365276c DJ |
296 | } |
297 | else | |
298 | thread_info->private->th_valid = 1; | |
299 | } | |
5365276c | 300 | \f |
fb0e1ba7 MK |
301 | /* Convert between user-level thread ids and LWP ids. */ |
302 | ||
39f77062 KB |
303 | static ptid_t |
304 | thread_from_lwp (ptid_t ptid) | |
fb0e1ba7 | 305 | { |
fb0e1ba7 MK |
306 | td_thrhandle_t th; |
307 | td_err_e err; | |
5365276c DJ |
308 | struct thread_info *thread_info; |
309 | ptid_t thread_ptid; | |
fb0e1ba7 | 310 | |
39f77062 KB |
311 | if (GET_LWP (ptid) == 0) |
312 | ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid)); | |
fb0e1ba7 | 313 | |
39f77062 | 314 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 | 315 | |
39f77062 | 316 | err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); |
fb0e1ba7 | 317 | if (err != TD_OK) |
8a3fe4f8 | 318 | error (_("Cannot find user-level thread for LWP %ld: %s"), |
39f77062 | 319 | GET_LWP (ptid), thread_db_err_str (err)); |
fb0e1ba7 | 320 | |
5365276c | 321 | thread_info = NULL; |
b9b5d7ea JJ |
322 | |
323 | /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the | |
324 | event thread has already died. If another gdb interface has called | |
325 | thread_alive() previously, the thread won't be found on the thread list | |
326 | anymore. In that case, we don't want to process this ptid anymore | |
327 | to avoid the possibility of later treating it as a newly | |
328 | discovered thread id that we should add to the list. Thus, | |
329 | we return a -1 ptid which is also how the thread list marks a | |
330 | dead thread. */ | |
331 | if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE | |
332 | && thread_info == NULL) | |
333 | return pid_to_ptid (-1); | |
334 | ||
5365276c | 335 | gdb_assert (thread_info && thread_info->private->ti_valid); |
fb0e1ba7 | 336 | |
1bac0d4d DJ |
337 | return ptid_build (GET_PID (ptid), GET_LWP (ptid), |
338 | thread_info->private->ti.ti_tid); | |
fb0e1ba7 MK |
339 | } |
340 | ||
39f77062 KB |
341 | static ptid_t |
342 | lwp_from_thread (ptid_t ptid) | |
fb0e1ba7 | 343 | { |
1bac0d4d | 344 | return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid)); |
fb0e1ba7 MK |
345 | } |
346 | \f | |
347 | ||
348 | void | |
349 | thread_db_init (struct target_ops *target) | |
350 | { | |
351 | target_beneath = target; | |
352 | } | |
353 | ||
5220ea4c AC |
354 | static void * |
355 | verbose_dlsym (void *handle, const char *name) | |
356 | { | |
357 | void *sym = dlsym (handle, name); | |
358 | if (sym == NULL) | |
8a3fe4f8 | 359 | warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ()); |
5220ea4c AC |
360 | return sym; |
361 | } | |
362 | ||
fb0e1ba7 MK |
363 | static int |
364 | thread_db_load (void) | |
365 | { | |
366 | void *handle; | |
367 | td_err_e err; | |
368 | ||
369 | handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW); | |
370 | if (handle == NULL) | |
f7c1e0f3 | 371 | { |
b4acd559 | 372 | fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n", |
f7c1e0f3 | 373 | LIBTHREAD_DB_SO, dlerror ()); |
b4acd559 | 374 | fprintf_filtered (gdb_stderr, |
f7c1e0f3 MS |
375 | "GDB will not be able to debug pthreads.\n\n"); |
376 | return 0; | |
377 | } | |
fb0e1ba7 MK |
378 | |
379 | /* Initialize pointers to the dynamic library functions we will use. | |
380 | Essential functions first. */ | |
381 | ||
5220ea4c | 382 | td_init_p = verbose_dlsym (handle, "td_init"); |
fb0e1ba7 MK |
383 | if (td_init_p == NULL) |
384 | return 0; | |
385 | ||
5220ea4c | 386 | td_ta_new_p = verbose_dlsym (handle, "td_ta_new"); |
fb0e1ba7 MK |
387 | if (td_ta_new_p == NULL) |
388 | return 0; | |
389 | ||
5220ea4c | 390 | td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr"); |
fb0e1ba7 MK |
391 | if (td_ta_map_id2thr_p == NULL) |
392 | return 0; | |
393 | ||
5220ea4c | 394 | td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr"); |
fb0e1ba7 MK |
395 | if (td_ta_map_lwp2thr_p == NULL) |
396 | return 0; | |
397 | ||
5220ea4c | 398 | td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter"); |
fb0e1ba7 MK |
399 | if (td_ta_thr_iter_p == NULL) |
400 | return 0; | |
401 | ||
5220ea4c | 402 | td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate"); |
fb0e1ba7 MK |
403 | if (td_thr_validate_p == NULL) |
404 | return 0; | |
405 | ||
5220ea4c | 406 | td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info"); |
fb0e1ba7 MK |
407 | if (td_thr_get_info_p == NULL) |
408 | return 0; | |
409 | ||
fb0e1ba7 MK |
410 | /* Initialize the library. */ |
411 | err = td_init_p (); | |
412 | if (err != TD_OK) | |
413 | { | |
8a3fe4f8 | 414 | warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err)); |
fb0e1ba7 MK |
415 | return 0; |
416 | } | |
417 | ||
418 | /* These are not essential. */ | |
419 | td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"); | |
420 | td_ta_set_event_p = dlsym (handle, "td_ta_set_event"); | |
421 | td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"); | |
422 | td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"); | |
3f47be5c | 423 | td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"); |
fb0e1ba7 MK |
424 | |
425 | return 1; | |
426 | } | |
427 | ||
cdbc0b18 | 428 | static td_err_e |
24557e30 AC |
429 | enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp) |
430 | { | |
431 | td_notify_t notify; | |
cdbc0b18 | 432 | td_err_e err; |
24557e30 AC |
433 | |
434 | /* Get the breakpoint address for thread EVENT. */ | |
435 | err = td_ta_event_addr_p (thread_agent, event, ¬ify); | |
436 | if (err != TD_OK) | |
cdbc0b18 | 437 | return err; |
24557e30 AC |
438 | |
439 | /* Set up the breakpoint. */ | |
16451949 AS |
440 | gdb_assert (exec_bfd); |
441 | (*bp) = (gdbarch_convert_from_func_ptr_addr | |
442 | (current_gdbarch, | |
443 | /* Do proper sign extension for the target. */ | |
444 | (bfd_get_sign_extend_vma (exec_bfd) > 0 | |
445 | ? (CORE_ADDR) (intptr_t) notify.u.bptaddr | |
446 | : (CORE_ADDR) (uintptr_t) notify.u.bptaddr), | |
447 | ¤t_target)); | |
24557e30 AC |
448 | create_thread_event_breakpoint ((*bp)); |
449 | ||
cdbc0b18 | 450 | return TD_OK; |
24557e30 AC |
451 | } |
452 | ||
fb0e1ba7 MK |
453 | static void |
454 | enable_thread_event_reporting (void) | |
455 | { | |
456 | td_thr_events_t events; | |
457 | td_notify_t notify; | |
458 | td_err_e err; | |
a2f23071 DJ |
459 | #ifdef HAVE_GNU_LIBC_VERSION_H |
460 | const char *libc_version; | |
461 | int libc_major, libc_minor; | |
462 | #endif | |
fb0e1ba7 MK |
463 | |
464 | /* We cannot use the thread event reporting facility if these | |
465 | functions aren't available. */ | |
466 | if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL | |
467 | || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL) | |
468 | return; | |
469 | ||
470 | /* Set the process wide mask saying which events we're interested in. */ | |
471 | td_event_emptyset (&events); | |
472 | td_event_addset (&events, TD_CREATE); | |
a2f23071 DJ |
473 | |
474 | #ifdef HAVE_GNU_LIBC_VERSION_H | |
fb0e1ba7 MK |
475 | /* FIXME: kettenis/2000-04-23: The event reporting facility is |
476 | broken for TD_DEATH events in glibc 2.1.3, so don't enable it for | |
477 | now. */ | |
a2f23071 DJ |
478 | libc_version = gnu_get_libc_version (); |
479 | if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2 | |
480 | && (libc_major > 2 || (libc_major == 2 && libc_minor > 1))) | |
fb0e1ba7 | 481 | #endif |
a2f23071 | 482 | td_event_addset (&events, TD_DEATH); |
fb0e1ba7 MK |
483 | |
484 | err = td_ta_set_event_p (thread_agent, &events); | |
485 | if (err != TD_OK) | |
486 | { | |
8a3fe4f8 | 487 | warning (_("Unable to set global thread event mask: %s"), |
fb0e1ba7 MK |
488 | thread_db_err_str (err)); |
489 | return; | |
490 | } | |
491 | ||
492 | /* Delete previous thread event breakpoints, if any. */ | |
493 | remove_thread_event_breakpoints (); | |
24557e30 AC |
494 | td_create_bp_addr = 0; |
495 | td_death_bp_addr = 0; | |
fb0e1ba7 | 496 | |
24557e30 | 497 | /* Set up the thread creation event. */ |
cdbc0b18 RM |
498 | err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr); |
499 | if (err != TD_OK) | |
fb0e1ba7 | 500 | { |
8a3fe4f8 | 501 | warning (_("Unable to get location for thread creation breakpoint: %s"), |
fb0e1ba7 MK |
502 | thread_db_err_str (err)); |
503 | return; | |
504 | } | |
505 | ||
24557e30 | 506 | /* Set up the thread death event. */ |
cdbc0b18 RM |
507 | err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr); |
508 | if (err != TD_OK) | |
fb0e1ba7 | 509 | { |
8a3fe4f8 | 510 | warning (_("Unable to get location for thread death breakpoint: %s"), |
fb0e1ba7 MK |
511 | thread_db_err_str (err)); |
512 | return; | |
513 | } | |
fb0e1ba7 MK |
514 | } |
515 | ||
516 | static void | |
517 | disable_thread_event_reporting (void) | |
518 | { | |
519 | td_thr_events_t events; | |
520 | ||
521 | /* Set the process wide mask saying we aren't interested in any | |
522 | events anymore. */ | |
523 | td_event_emptyset (&events); | |
524 | td_ta_set_event_p (thread_agent, &events); | |
525 | ||
526 | /* Delete thread event breakpoints, if any. */ | |
527 | remove_thread_event_breakpoints (); | |
528 | td_create_bp_addr = 0; | |
529 | td_death_bp_addr = 0; | |
530 | } | |
531 | ||
532 | static void | |
533 | check_thread_signals (void) | |
534 | { | |
535 | #ifdef GET_THREAD_SIGNALS | |
21bf60fe | 536 | if (!thread_signals) |
fb0e1ba7 MK |
537 | { |
538 | sigset_t mask; | |
539 | int i; | |
540 | ||
541 | GET_THREAD_SIGNALS (&mask); | |
542 | sigemptyset (&thread_stop_set); | |
543 | sigemptyset (&thread_print_set); | |
544 | ||
b9569773 | 545 | for (i = 1; i < NSIG; i++) |
fb0e1ba7 MK |
546 | { |
547 | if (sigismember (&mask, i)) | |
548 | { | |
549 | if (signal_stop_update (target_signal_from_host (i), 0)) | |
550 | sigaddset (&thread_stop_set, i); | |
551 | if (signal_print_update (target_signal_from_host (i), 0)) | |
552 | sigaddset (&thread_print_set, i); | |
553 | thread_signals = 1; | |
554 | } | |
555 | } | |
556 | } | |
557 | #endif | |
558 | } | |
559 | ||
0ec9a092 DJ |
560 | /* Check whether thread_db is usable. This function is called when |
561 | an inferior is created (or otherwise acquired, e.g. attached to) | |
562 | and when new shared libraries are loaded into a running process. */ | |
563 | ||
564 | void | |
565 | check_for_thread_db (void) | |
fb0e1ba7 MK |
566 | { |
567 | td_err_e err; | |
0ec9a092 | 568 | static int already_loaded; |
fb0e1ba7 | 569 | |
5220ea4c AC |
570 | /* First time through, report that libthread_db was successfuly |
571 | loaded. Can't print this in in thread_db_load as, at that stage, | |
0ec9a092 | 572 | the interpreter and it's console haven't started. */ |
5220ea4c | 573 | |
0ec9a092 | 574 | if (!already_loaded) |
bda9cb72 | 575 | { |
0ec9a092 DJ |
576 | Dl_info info; |
577 | const char *library = NULL; | |
578 | if (dladdr ((*td_ta_new_p), &info) != 0) | |
579 | library = info.dli_fname; | |
580 | ||
581 | /* Try dlinfo? */ | |
582 | ||
583 | if (library == NULL) | |
584 | /* Paranoid - don't let a NULL path slip through. */ | |
585 | library = LIBTHREAD_DB_SO; | |
c194fbe1 | 586 | |
0ec9a092 DJ |
587 | printf_unfiltered (_("Using host libthread_db library \"%s\".\n"), |
588 | library); | |
589 | already_loaded = 1; | |
bda9cb72 MK |
590 | } |
591 | ||
fb0e1ba7 MK |
592 | if (using_thread_db) |
593 | /* Nothing to do. The thread library was already detected and the | |
594 | target vector was already activated. */ | |
0ec9a092 | 595 | return; |
fb0e1ba7 | 596 | |
0ec9a092 DJ |
597 | /* Don't attempt to use thread_db on targets which can not run |
598 | (executables not running yet, core files) for now. */ | |
599 | if (!target_has_execution) | |
600 | return; | |
601 | ||
602 | /* Initialize the structure that identifies the child process. */ | |
39f77062 | 603 | proc_handle.pid = GET_PID (inferior_ptid); |
fb0e1ba7 | 604 | |
bda9cb72 | 605 | /* Now attempt to open a connection to the thread library. */ |
fb0e1ba7 MK |
606 | err = td_ta_new_p (&proc_handle, &thread_agent); |
607 | switch (err) | |
608 | { | |
609 | case TD_NOLIBTHREAD: | |
bda9cb72 | 610 | /* No thread library was detected. */ |
fb0e1ba7 MK |
611 | break; |
612 | ||
613 | case TD_OK: | |
a3f17187 | 614 | printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n")); |
5220ea4c | 615 | |
bda9cb72 MK |
616 | /* The thread library was detected. Activate the thread_db target. */ |
617 | push_target (&thread_db_ops); | |
618 | using_thread_db = 1; | |
619 | ||
95575b2e AC |
620 | enable_thread_event_reporting (); |
621 | thread_db_find_new_threads (); | |
fb0e1ba7 MK |
622 | break; |
623 | ||
624 | default: | |
8a3fe4f8 | 625 | warning (_("Cannot initialize thread debugging library: %s"), |
fb0e1ba7 MK |
626 | thread_db_err_str (err)); |
627 | break; | |
628 | } | |
0ec9a092 DJ |
629 | } |
630 | ||
631 | static void | |
632 | thread_db_new_objfile (struct objfile *objfile) | |
633 | { | |
634 | if (objfile != NULL) | |
635 | check_for_thread_db (); | |
fb0e1ba7 | 636 | |
540af400 MS |
637 | if (target_new_objfile_chain) |
638 | target_new_objfile_chain (objfile); | |
fb0e1ba7 MK |
639 | } |
640 | ||
a2f23071 DJ |
641 | /* Attach to a new thread. This function is called when we receive a |
642 | TD_CREATE event or when we iterate over all threads and find one | |
643 | that wasn't already in our list. */ | |
644 | ||
fb0e1ba7 | 645 | static void |
39f77062 | 646 | attach_thread (ptid_t ptid, const td_thrhandle_t *th_p, |
fb0e1ba7 MK |
647 | const td_thrinfo_t *ti_p, int verbose) |
648 | { | |
649 | struct thread_info *tp; | |
650 | td_err_e err; | |
651 | ||
a2f23071 DJ |
652 | /* If we're being called after a TD_CREATE event, we may already |
653 | know about this thread. There are two ways this can happen. We | |
654 | may have iterated over all threads between the thread creation | |
655 | and the TD_CREATE event, for instance when the user has issued | |
656 | the `info threads' command before the SIGTRAP for hitting the | |
657 | thread creation breakpoint was reported. Alternatively, the | |
658 | thread may have exited and a new one been created with the same | |
659 | thread ID. In the first case we don't need to do anything; in | |
660 | the second case we should discard information about the dead | |
661 | thread and attach to the new one. */ | |
662 | if (in_thread_list (ptid)) | |
663 | { | |
664 | tp = find_thread_pid (ptid); | |
665 | gdb_assert (tp != NULL); | |
666 | ||
667 | if (!tp->private->dying) | |
668 | return; | |
669 | ||
670 | delete_thread (ptid); | |
671 | } | |
672 | ||
fb0e1ba7 MK |
673 | check_thread_signals (); |
674 | ||
fb0e1ba7 | 675 | /* Add the thread to GDB's thread list. */ |
39f77062 | 676 | tp = add_thread (ptid); |
fb0e1ba7 | 677 | tp->private = xmalloc (sizeof (struct private_thread_info)); |
5365276c DJ |
678 | memset (tp->private, 0, sizeof (struct private_thread_info)); |
679 | ||
680 | if (verbose) | |
a3f17187 | 681 | printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid)); |
fb0e1ba7 | 682 | |
21bf60fe MK |
683 | if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE) |
684 | return; /* A zombie thread -- do not attach. */ | |
5fd913cc | 685 | |
8605d56e | 686 | /* Under GNU/Linux, we have to attach to each and every thread. */ |
fb0e1ba7 | 687 | #ifdef ATTACH_LWP |
c194fbe1 | 688 | ATTACH_LWP (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0); |
fb0e1ba7 MK |
689 | #endif |
690 | ||
691 | /* Enable thread event reporting for this thread. */ | |
692 | err = td_thr_event_enable_p (th_p, 1); | |
693 | if (err != TD_OK) | |
8a3fe4f8 | 694 | error (_("Cannot enable thread event reporting for %s: %s"), |
39f77062 | 695 | target_pid_to_str (ptid), thread_db_err_str (err)); |
fb0e1ba7 MK |
696 | } |
697 | ||
c194fbe1 MK |
698 | static void |
699 | thread_db_attach (char *args, int from_tty) | |
700 | { | |
701 | target_beneath->to_attach (args, from_tty); | |
702 | ||
703 | /* Destroy thread info; it's no longer valid. */ | |
704 | init_thread_list (); | |
705 | ||
706 | /* The child process is now the actual multi-threaded | |
707 | program. Snatch its process ID... */ | |
708 | proc_handle.pid = GET_PID (inferior_ptid); | |
709 | ||
710 | /* ...and perform the remaining initialization steps. */ | |
711 | enable_thread_event_reporting (); | |
b4acd559 | 712 | thread_db_find_new_threads (); |
c194fbe1 MK |
713 | } |
714 | ||
fb0e1ba7 | 715 | static void |
39f77062 | 716 | detach_thread (ptid_t ptid, int verbose) |
fb0e1ba7 | 717 | { |
a2f23071 DJ |
718 | struct thread_info *thread_info; |
719 | ||
fb0e1ba7 | 720 | if (verbose) |
a3f17187 | 721 | printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid)); |
a2f23071 DJ |
722 | |
723 | /* Don't delete the thread now, because it still reports as active | |
724 | until it has executed a few instructions after the event | |
725 | breakpoint - if we deleted it now, "info threads" would cause us | |
726 | to re-attach to it. Just mark it as having had a TD_DEATH | |
727 | event. This means that we won't delete it from our thread list | |
728 | until we notice that it's dead (via prune_threads), or until | |
729 | something re-uses its thread ID. */ | |
730 | thread_info = find_thread_pid (ptid); | |
731 | gdb_assert (thread_info != NULL); | |
732 | thread_info->private->dying = 1; | |
fb0e1ba7 MK |
733 | } |
734 | ||
735 | static void | |
736 | thread_db_detach (char *args, int from_tty) | |
737 | { | |
738 | disable_thread_event_reporting (); | |
c194fbe1 MK |
739 | |
740 | /* There's no need to save & restore inferior_ptid here, since the | |
741 | inferior is supposed to be survive this function call. */ | |
742 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
743 | ||
744 | /* Forget about the child's process ID. We shouldn't need it | |
745 | anymore. */ | |
746 | proc_handle.pid = 0; | |
fb0e1ba7 MK |
747 | |
748 | target_beneath->to_detach (args, from_tty); | |
749 | } | |
750 | ||
5365276c DJ |
751 | static int |
752 | clear_lwpid_callback (struct thread_info *thread, void *dummy) | |
753 | { | |
754 | /* If we know that our thread implementation is 1-to-1, we could save | |
755 | a certain amount of information; it's not clear how much, so we | |
756 | are always conservative. */ | |
757 | ||
758 | thread->private->th_valid = 0; | |
759 | thread->private->ti_valid = 0; | |
760 | ||
761 | return 0; | |
762 | } | |
763 | ||
fb0e1ba7 | 764 | static void |
39f77062 | 765 | thread_db_resume (ptid_t ptid, int step, enum target_signal signo) |
fb0e1ba7 | 766 | { |
39f77062 | 767 | struct cleanup *old_chain = save_inferior_ptid (); |
fb0e1ba7 | 768 | |
39f77062 KB |
769 | if (GET_PID (ptid) == -1) |
770 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
771 | else if (is_thread (ptid)) | |
772 | ptid = lwp_from_thread (ptid); | |
fb0e1ba7 | 773 | |
5365276c DJ |
774 | /* Clear cached data which may not be valid after the resume. */ |
775 | iterate_over_threads (clear_lwpid_callback, NULL); | |
776 | ||
39f77062 | 777 | target_beneath->to_resume (ptid, step, signo); |
fb0e1ba7 MK |
778 | |
779 | do_cleanups (old_chain); | |
780 | } | |
781 | ||
782 | /* Check if PID is currently stopped at the location of a thread event | |
783 | breakpoint location. If it is, read the event message and act upon | |
784 | the event. */ | |
785 | ||
786 | static void | |
39f77062 | 787 | check_event (ptid_t ptid) |
fb0e1ba7 MK |
788 | { |
789 | td_event_msg_t msg; | |
790 | td_thrinfo_t ti; | |
791 | td_err_e err; | |
792 | CORE_ADDR stop_pc; | |
4d9850d3 | 793 | int loop = 0; |
fb0e1ba7 MK |
794 | |
795 | /* Bail out early if we're not at a thread event breakpoint. */ | |
39f77062 | 796 | stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK; |
fb0e1ba7 MK |
797 | if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr) |
798 | return; | |
799 | ||
4d9850d3 JJ |
800 | /* If we are at a create breakpoint, we do not know what new lwp |
801 | was created and cannot specifically locate the event message for it. | |
802 | We have to call td_ta_event_getmsg() to get | |
803 | the latest message. Since we have no way of correlating whether | |
cdbc0b18 | 804 | the event message we get back corresponds to our breakpoint, we must |
4d9850d3 | 805 | loop and read all event messages, processing them appropriately. |
cdbc0b18 RM |
806 | This guarantees we will process the correct message before continuing |
807 | from the breakpoint. | |
4d9850d3 JJ |
808 | |
809 | Currently, death events are not enabled. If they are enabled, | |
810 | the death event can use the td_thr_event_getmsg() interface to | |
811 | get the message specifically for that lwp and avoid looping | |
812 | below. */ | |
813 | ||
814 | loop = 1; | |
815 | ||
816 | do | |
fb0e1ba7 | 817 | { |
4d9850d3 JJ |
818 | err = td_ta_event_getmsg_p (thread_agent, &msg); |
819 | if (err != TD_OK) | |
820 | { | |
821 | if (err == TD_NOMSG) | |
822 | return; | |
fb0e1ba7 | 823 | |
8a3fe4f8 | 824 | error (_("Cannot get thread event message: %s"), |
4d9850d3 JJ |
825 | thread_db_err_str (err)); |
826 | } | |
fb0e1ba7 | 827 | |
4d9850d3 JJ |
828 | err = td_thr_get_info_p (msg.th_p, &ti); |
829 | if (err != TD_OK) | |
8a3fe4f8 | 830 | error (_("Cannot get thread info: %s"), thread_db_err_str (err)); |
fb0e1ba7 | 831 | |
1bac0d4d | 832 | ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid); |
fb0e1ba7 | 833 | |
4d9850d3 JJ |
834 | switch (msg.event) |
835 | { | |
836 | case TD_CREATE: | |
a2f23071 DJ |
837 | /* Call attach_thread whether or not we already know about a |
838 | thread with this thread ID. */ | |
839 | attach_thread (ptid, msg.th_p, &ti, 1); | |
fb0e1ba7 | 840 | |
4d9850d3 | 841 | break; |
fb0e1ba7 | 842 | |
4d9850d3 | 843 | case TD_DEATH: |
fb0e1ba7 | 844 | |
4d9850d3 | 845 | if (!in_thread_list (ptid)) |
8a3fe4f8 | 846 | error (_("Spurious thread death event.")); |
fb0e1ba7 | 847 | |
4d9850d3 | 848 | detach_thread (ptid, 1); |
fb0e1ba7 | 849 | |
4d9850d3 | 850 | break; |
fb0e1ba7 | 851 | |
4d9850d3 | 852 | default: |
8a3fe4f8 | 853 | error (_("Spurious thread event.")); |
4d9850d3 | 854 | } |
fb0e1ba7 | 855 | } |
4d9850d3 | 856 | while (loop); |
fb0e1ba7 MK |
857 | } |
858 | ||
39f77062 KB |
859 | static ptid_t |
860 | thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
fb0e1ba7 | 861 | { |
39f77062 | 862 | extern ptid_t trap_ptid; |
fb0e1ba7 | 863 | |
39f77062 KB |
864 | if (GET_PID (ptid) != -1 && is_thread (ptid)) |
865 | ptid = lwp_from_thread (ptid); | |
fb0e1ba7 | 866 | |
39f77062 | 867 | ptid = target_beneath->to_wait (ptid, ourstatus); |
fb0e1ba7 | 868 | |
bda9cb72 MK |
869 | if (proc_handle.pid == 0) |
870 | /* The current child process isn't the actual multi-threaded | |
871 | program yet, so don't try to do any special thread-specific | |
872 | post-processing and bail out early. */ | |
39f77062 | 873 | return ptid; |
bda9cb72 | 874 | |
fb0e1ba7 | 875 | if (ourstatus->kind == TARGET_WAITKIND_EXITED) |
39f77062 | 876 | return pid_to_ptid (-1); |
fb0e1ba7 | 877 | |
3f64f7b1 DJ |
878 | if (ourstatus->kind == TARGET_WAITKIND_EXECD) |
879 | { | |
880 | remove_thread_event_breakpoints (); | |
881 | unpush_target (&thread_db_ops); | |
882 | using_thread_db = 0; | |
883 | ||
884 | return pid_to_ptid (GET_PID (ptid)); | |
885 | } | |
886 | ||
fb0e1ba7 MK |
887 | if (ourstatus->kind == TARGET_WAITKIND_STOPPED |
888 | && ourstatus->value.sig == TARGET_SIGNAL_TRAP) | |
889 | /* Check for a thread event. */ | |
39f77062 | 890 | check_event (ptid); |
fb0e1ba7 | 891 | |
39f77062 KB |
892 | if (!ptid_equal (trap_ptid, null_ptid)) |
893 | trap_ptid = thread_from_lwp (trap_ptid); | |
fb0e1ba7 | 894 | |
b9b5d7ea JJ |
895 | /* Change the ptid back into the higher level PID + TID format. |
896 | If the thread is dead and no longer on the thread list, we will | |
897 | get back a dead ptid. This can occur if the thread death event | |
898 | gets postponed by other simultaneous events. In such a case, | |
899 | we want to just ignore the event and continue on. */ | |
900 | ptid = thread_from_lwp (ptid); | |
901 | if (GET_PID (ptid) == -1) | |
902 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
903 | ||
904 | return ptid; | |
fb0e1ba7 MK |
905 | } |
906 | ||
10d6c8cd DJ |
907 | static LONGEST |
908 | thread_db_xfer_partial (struct target_ops *ops, enum target_object object, | |
909 | const char *annex, gdb_byte *readbuf, | |
910 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
fb0e1ba7 | 911 | { |
39f77062 | 912 | struct cleanup *old_chain = save_inferior_ptid (); |
10d6c8cd | 913 | LONGEST xfer; |
fb0e1ba7 | 914 | |
39f77062 | 915 | if (is_thread (inferior_ptid)) |
fb0e1ba7 MK |
916 | { |
917 | /* FIXME: This seems to be necessary to make sure breakpoints | |
918 | are removed. */ | |
21bf60fe | 919 | if (!target_thread_alive (inferior_ptid)) |
39f77062 | 920 | inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid)); |
fb0e1ba7 | 921 | else |
39f77062 | 922 | inferior_ptid = lwp_from_thread (inferior_ptid); |
fb0e1ba7 MK |
923 | } |
924 | ||
10d6c8cd DJ |
925 | xfer = target_beneath->to_xfer_partial (ops, object, annex, |
926 | readbuf, writebuf, offset, len); | |
fb0e1ba7 MK |
927 | |
928 | do_cleanups (old_chain); | |
929 | return xfer; | |
930 | } | |
931 | ||
fb0e1ba7 MK |
932 | static void |
933 | thread_db_kill (void) | |
934 | { | |
c194fbe1 MK |
935 | /* There's no need to save & restore inferior_ptid here, since the |
936 | inferior isn't supposed to survive this function call. */ | |
937 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
fb0e1ba7 MK |
938 | target_beneath->to_kill (); |
939 | } | |
940 | ||
941 | static void | |
c27cda74 AC |
942 | thread_db_create_inferior (char *exec_file, char *allargs, char **env, |
943 | int from_tty) | |
fb0e1ba7 | 944 | { |
b26a6851 AC |
945 | unpush_target (&thread_db_ops); |
946 | using_thread_db = 0; | |
c27cda74 | 947 | target_beneath->to_create_inferior (exec_file, allargs, env, from_tty); |
bda9cb72 | 948 | } |
fb0e1ba7 | 949 | |
bda9cb72 | 950 | static void |
39f77062 | 951 | thread_db_post_startup_inferior (ptid_t ptid) |
bda9cb72 MK |
952 | { |
953 | if (proc_handle.pid == 0) | |
954 | { | |
955 | /* The child process is now the actual multi-threaded | |
956 | program. Snatch its process ID... */ | |
39f77062 | 957 | proc_handle.pid = GET_PID (ptid); |
fb0e1ba7 | 958 | |
bda9cb72 MK |
959 | /* ...and perform the remaining initialization steps. */ |
960 | enable_thread_event_reporting (); | |
21bf60fe | 961 | thread_db_find_new_threads (); |
bda9cb72 | 962 | } |
fb0e1ba7 MK |
963 | } |
964 | ||
965 | static void | |
966 | thread_db_mourn_inferior (void) | |
967 | { | |
c194fbe1 MK |
968 | /* Forget about the child's process ID. We shouldn't need it |
969 | anymore. */ | |
970 | proc_handle.pid = 0; | |
fb0e1ba7 MK |
971 | |
972 | target_beneath->to_mourn_inferior (); | |
043b2f77 | 973 | |
e23fc6de DJ |
974 | /* Delete the old thread event breakpoints. Do this after mourning |
975 | the inferior, so that we don't try to uninsert them. */ | |
976 | remove_thread_event_breakpoints (); | |
977 | ||
b26a6851 AC |
978 | /* Detach thread_db target ops. */ |
979 | unpush_target (&thread_db_ops); | |
980 | using_thread_db = 0; | |
fb0e1ba7 MK |
981 | } |
982 | ||
fb0e1ba7 MK |
983 | static int |
984 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) | |
985 | { | |
986 | td_thrinfo_t ti; | |
987 | td_err_e err; | |
39f77062 | 988 | ptid_t ptid; |
fb0e1ba7 MK |
989 | |
990 | err = td_thr_get_info_p (th_p, &ti); | |
991 | if (err != TD_OK) | |
8a3fe4f8 | 992 | error (_("find_new_threads_callback: cannot get thread info: %s"), |
3197744f | 993 | thread_db_err_str (err)); |
fb0e1ba7 | 994 | |
21bf60fe MK |
995 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) |
996 | return 0; /* A zombie -- ignore. */ | |
5fd913cc | 997 | |
1bac0d4d | 998 | ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid); |
fb0e1ba7 | 999 | |
21bf60fe | 1000 | if (!in_thread_list (ptid)) |
39f77062 | 1001 | attach_thread (ptid, th_p, &ti, 1); |
fb0e1ba7 MK |
1002 | |
1003 | return 0; | |
1004 | } | |
1005 | ||
1006 | static void | |
1007 | thread_db_find_new_threads (void) | |
1008 | { | |
1009 | td_err_e err; | |
1010 | ||
1011 | /* Iterate over all user-space threads to discover new threads. */ | |
1012 | err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL, | |
1013 | TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, | |
1014 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); | |
1015 | if (err != TD_OK) | |
8a3fe4f8 | 1016 | error (_("Cannot find new threads: %s"), thread_db_err_str (err)); |
fb0e1ba7 MK |
1017 | } |
1018 | ||
1019 | static char * | |
39f77062 | 1020 | thread_db_pid_to_str (ptid_t ptid) |
fb0e1ba7 | 1021 | { |
39f77062 | 1022 | if (is_thread (ptid)) |
fb0e1ba7 MK |
1023 | { |
1024 | static char buf[64]; | |
5365276c | 1025 | struct thread_info *thread_info; |
fb0e1ba7 | 1026 | |
5365276c | 1027 | thread_info = find_thread_pid (ptid); |
28b17333 DJ |
1028 | if (thread_info == NULL) |
1029 | snprintf (buf, sizeof (buf), "Thread %ld (LWP %ld) (Missing)", | |
1030 | GET_THREAD (ptid), GET_LWP (ptid)); | |
fb0e1ba7 | 1031 | else |
28b17333 DJ |
1032 | snprintf (buf, sizeof (buf), "Thread %ld (LWP %ld)", |
1033 | GET_THREAD (ptid), GET_LWP (ptid)); | |
fb0e1ba7 MK |
1034 | |
1035 | return buf; | |
1036 | } | |
1037 | ||
39f77062 KB |
1038 | if (target_beneath->to_pid_to_str (ptid)) |
1039 | return target_beneath->to_pid_to_str (ptid); | |
fb0e1ba7 | 1040 | |
39f77062 | 1041 | return normal_pid_to_str (ptid); |
fb0e1ba7 MK |
1042 | } |
1043 | ||
28b17333 DJ |
1044 | /* Return a string describing the state of the thread specified by |
1045 | INFO. */ | |
1046 | ||
1047 | static char * | |
1048 | thread_db_extra_thread_info (struct thread_info *info) | |
1049 | { | |
1050 | if (info->private->dying) | |
1051 | return "Exiting"; | |
1052 | ||
1053 | return NULL; | |
1054 | } | |
1055 | ||
b2756930 KB |
1056 | /* Get the address of the thread local variable in load module LM which |
1057 | is stored at OFFSET within the thread local storage for thread PTID. */ | |
3f47be5c EZ |
1058 | |
1059 | static CORE_ADDR | |
b2756930 KB |
1060 | thread_db_get_thread_local_address (ptid_t ptid, |
1061 | CORE_ADDR lm, | |
b4acd559 | 1062 | CORE_ADDR offset) |
3f47be5c EZ |
1063 | { |
1064 | if (is_thread (ptid)) | |
1065 | { | |
3f47be5c | 1066 | td_err_e err; |
3f47be5c | 1067 | void *address; |
5365276c | 1068 | struct thread_info *thread_info; |
3f47be5c EZ |
1069 | |
1070 | /* glibc doesn't provide the needed interface. */ | |
b4acd559 | 1071 | if (!td_thr_tls_get_addr_p) |
109c3e39 AC |
1072 | throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR, |
1073 | _("No TLS library support")); | |
3f47be5c | 1074 | |
b2756930 KB |
1075 | /* Caller should have verified that lm != 0. */ |
1076 | gdb_assert (lm != 0); | |
3f47be5c EZ |
1077 | |
1078 | /* Get info about the thread. */ | |
5365276c DJ |
1079 | thread_info = find_thread_pid (ptid); |
1080 | thread_db_map_id2thr (thread_info, 1); | |
1081 | ||
3f47be5c | 1082 | /* Finally, get the address of the variable. */ |
87177905 TS |
1083 | err = td_thr_tls_get_addr_p (&thread_info->private->th, |
1084 | (void *)(size_t) lm, | |
5365276c | 1085 | offset, &address); |
3f47be5c EZ |
1086 | |
1087 | #ifdef THREAD_DB_HAS_TD_NOTALLOC | |
1088 | /* The memory hasn't been allocated, yet. */ | |
1089 | if (err == TD_NOTALLOC) | |
b4acd559 JJ |
1090 | /* Now, if libthread_db provided the initialization image's |
1091 | address, we *could* try to build a non-lvalue value from | |
1092 | the initialization image. */ | |
109c3e39 AC |
1093 | throw_error (TLS_NOT_ALLOCATED_YET_ERROR, |
1094 | _("TLS not allocated yet")); | |
3f47be5c EZ |
1095 | #endif |
1096 | ||
1097 | /* Something else went wrong. */ | |
1098 | if (err != TD_OK) | |
109c3e39 AC |
1099 | throw_error (TLS_GENERIC_ERROR, |
1100 | (("%s")), thread_db_err_str (err)); | |
3f47be5c EZ |
1101 | |
1102 | /* Cast assuming host == target. Joy. */ | |
16451949 AS |
1103 | /* Do proper sign extension for the target. */ |
1104 | gdb_assert (exec_bfd); | |
1105 | return (bfd_get_sign_extend_vma (exec_bfd) > 0 | |
1106 | ? (CORE_ADDR) (intptr_t) address | |
1107 | : (CORE_ADDR) (uintptr_t) address); | |
3f47be5c EZ |
1108 | } |
1109 | ||
1110 | if (target_beneath->to_get_thread_local_address) | |
b2756930 | 1111 | return target_beneath->to_get_thread_local_address (ptid, lm, offset); |
93ad78a7 | 1112 | else |
109c3e39 AC |
1113 | throw_error (TLS_GENERIC_ERROR, |
1114 | _("TLS not supported on this target")); | |
3f47be5c EZ |
1115 | } |
1116 | ||
fb0e1ba7 MK |
1117 | static void |
1118 | init_thread_db_ops (void) | |
1119 | { | |
1120 | thread_db_ops.to_shortname = "multi-thread"; | |
1121 | thread_db_ops.to_longname = "multi-threaded child process."; | |
1122 | thread_db_ops.to_doc = "Threads and pthreads support."; | |
c194fbe1 | 1123 | thread_db_ops.to_attach = thread_db_attach; |
fb0e1ba7 MK |
1124 | thread_db_ops.to_detach = thread_db_detach; |
1125 | thread_db_ops.to_resume = thread_db_resume; | |
1126 | thread_db_ops.to_wait = thread_db_wait; | |
10d6c8cd | 1127 | thread_db_ops.to_xfer_partial = thread_db_xfer_partial; |
fb0e1ba7 MK |
1128 | thread_db_ops.to_kill = thread_db_kill; |
1129 | thread_db_ops.to_create_inferior = thread_db_create_inferior; | |
bda9cb72 | 1130 | thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior; |
fb0e1ba7 | 1131 | thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior; |
fb0e1ba7 MK |
1132 | thread_db_ops.to_find_new_threads = thread_db_find_new_threads; |
1133 | thread_db_ops.to_pid_to_str = thread_db_pid_to_str; | |
1134 | thread_db_ops.to_stratum = thread_stratum; | |
1135 | thread_db_ops.to_has_thread_control = tc_schedlock; | |
3f47be5c EZ |
1136 | thread_db_ops.to_get_thread_local_address |
1137 | = thread_db_get_thread_local_address; | |
28b17333 | 1138 | thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info; |
fb0e1ba7 MK |
1139 | thread_db_ops.to_magic = OPS_MAGIC; |
1140 | } | |
1141 | ||
1142 | void | |
1143 | _initialize_thread_db (void) | |
1144 | { | |
1145 | /* Only initialize the module if we can load libthread_db. */ | |
1146 | if (thread_db_load ()) | |
1147 | { | |
1148 | init_thread_db_ops (); | |
1149 | add_target (&thread_db_ops); | |
1150 | ||
1151 | /* Add ourselves to objfile event chain. */ | |
9a4105ab AC |
1152 | target_new_objfile_chain = deprecated_target_new_objfile_hook; |
1153 | deprecated_target_new_objfile_hook = thread_db_new_objfile; | |
fb0e1ba7 MK |
1154 | } |
1155 | } |