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