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