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