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