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