]>
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" |
17a37d48 | 29 | #include "command.h" |
93ad78a7 | 30 | #include "exceptions.h" |
17a37d48 | 31 | #include "gdbcmd.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" |
17a37d48 | 38 | #include "solib.h" |
3f47be5c | 39 | #include "solib-svr4.h" |
16451949 | 40 | #include "gdbcore.h" |
06d3b283 | 41 | #include "observer.h" |
0ec9a092 | 42 | #include "linux-nat.h" |
fb0e1ba7 | 43 | |
979894f2 NR |
44 | #include <signal.h> |
45 | ||
a2f23071 DJ |
46 | #ifdef HAVE_GNU_LIBC_VERSION_H |
47 | #include <gnu/libc-version.h> | |
48 | #endif | |
49 | ||
17faa917 DJ |
50 | /* GNU/Linux libthread_db support. |
51 | ||
52 | libthread_db is a library, provided along with libpthread.so, which | |
53 | exposes the internals of the thread library to a debugger. It | |
54 | allows GDB to find existing threads, new threads as they are | |
55 | created, thread IDs (usually, the result of pthread_self), and | |
56 | thread-local variables. | |
57 | ||
58 | The libthread_db interface originates on Solaris, where it is | |
59 | both more powerful and more complicated. This implementation | |
60 | only works for LinuxThreads and NPTL, the two glibc threading | |
61 | libraries. It assumes that each thread is permanently assigned | |
62 | to a single light-weight process (LWP). | |
63 | ||
64 | libthread_db-specific information is stored in the "private" field | |
65 | of struct thread_info. When the field is NULL we do not yet have | |
66 | information about the new thread; this could be temporary (created, | |
67 | but the thread library's data structures do not reflect it yet) | |
68 | or permanent (created using clone instead of pthread_create). | |
69 | ||
70 | Process IDs managed by linux-thread-db.c match those used by | |
71 | linux-nat.c: a common PID for all processes, an LWP ID for each | |
72 | thread, and no TID. We save the TID in private. Keeping it out | |
73 | of the ptid_t prevents thread IDs changing when libpthread is | |
74 | loaded or unloaded. */ | |
75 | ||
17a37d48 PP |
76 | static char *libthread_db_search_path; |
77 | ||
8605d56e AC |
78 | /* If we're running on GNU/Linux, we must explicitly attach to any new |
79 | threads. */ | |
fb0e1ba7 | 80 | |
fb0e1ba7 MK |
81 | /* This module's target vector. */ |
82 | static struct target_ops thread_db_ops; | |
83 | ||
fb0e1ba7 MK |
84 | /* Non-zero if we have determined the signals used by the threads |
85 | library. */ | |
86 | static int thread_signals; | |
87 | static sigset_t thread_stop_set; | |
88 | static sigset_t thread_print_set; | |
89 | ||
d90e17a7 PA |
90 | struct thread_db_info |
91 | { | |
92 | struct thread_db_info *next; | |
93 | ||
94 | /* Process id this object refers to. */ | |
95 | int pid; | |
96 | ||
97 | /* Handle from dlopen for libthread_db.so. */ | |
98 | void *handle; | |
99 | ||
100 | /* Structure that identifies the child process for the | |
101 | <proc_service.h> interface. */ | |
102 | struct ps_prochandle proc_handle; | |
103 | ||
104 | /* Connection to the libthread_db library. */ | |
105 | td_thragent_t *thread_agent; | |
106 | ||
4d062f1a PA |
107 | /* True if we need to apply the workaround for glibc/BZ5983. When |
108 | we catch a PTRACE_O_TRACEFORK, and go query the child's thread | |
109 | list, nptl_db returns the parent's threads in addition to the new | |
110 | (single) child thread. If this flag is set, we do extra work to | |
111 | be able to ignore such stale entries. */ | |
112 | int need_stale_parent_threads_check; | |
113 | ||
d90e17a7 PA |
114 | /* Location of the thread creation event breakpoint. The code at |
115 | this location in the child process will be called by the pthread | |
116 | library whenever a new thread is created. By setting a special | |
117 | breakpoint at this location, GDB can detect when a new thread is | |
118 | created. We obtain this location via the td_ta_event_addr | |
119 | call. */ | |
120 | CORE_ADDR td_create_bp_addr; | |
fb0e1ba7 | 121 | |
d90e17a7 PA |
122 | /* Location of the thread death event breakpoint. */ |
123 | CORE_ADDR td_death_bp_addr; | |
fb0e1ba7 | 124 | |
d90e17a7 | 125 | /* Pointers to the libthread_db functions. */ |
fb0e1ba7 | 126 | |
d90e17a7 | 127 | td_err_e (*td_init_p) (void); |
fb0e1ba7 | 128 | |
d90e17a7 | 129 | td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, |
b4acd559 | 130 | td_thragent_t **ta); |
d90e17a7 PA |
131 | td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt, |
132 | td_thrhandle_t *__th); | |
133 | td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, | |
134 | lwpid_t lwpid, td_thrhandle_t *th); | |
135 | td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta, | |
136 | td_thr_iter_f *callback, void *cbdata_p, | |
137 | td_thr_state_e state, int ti_pri, | |
138 | sigset_t *ti_sigmask_p, | |
139 | unsigned int ti_user_flags); | |
140 | td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta, | |
141 | td_event_e event, td_notify_t *ptr); | |
142 | td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta, | |
143 | td_thr_events_t *event); | |
21e1bee4 PP |
144 | td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta, |
145 | td_thr_events_t *event); | |
d90e17a7 PA |
146 | td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta, |
147 | td_event_msg_t *msg); | |
148 | ||
149 | td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th); | |
150 | td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th, | |
151 | td_thrinfo_t *infop); | |
152 | td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, | |
153 | int event); | |
154 | ||
155 | td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th, | |
156 | void *map_address, | |
157 | size_t offset, void **address); | |
158 | }; | |
159 | ||
160 | /* List of known processes using thread_db, and the required | |
161 | bookkeeping. */ | |
162 | struct thread_db_info *thread_db_list; | |
163 | ||
164 | static void thread_db_find_new_threads_1 (ptid_t ptid); | |
02c6c942 | 165 | static void thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new); |
d90e17a7 PA |
166 | |
167 | /* Add the current inferior to the list of processes using libpthread. | |
168 | Return a pointer to the newly allocated object that was added to | |
169 | THREAD_DB_LIST. HANDLE is the handle returned by dlopen'ing | |
170 | LIBTHREAD_DB_SO. */ | |
171 | ||
172 | static struct thread_db_info * | |
173 | add_thread_db_info (void *handle) | |
174 | { | |
175 | int pid; | |
176 | struct thread_db_info *info; | |
177 | ||
178 | info = xcalloc (1, sizeof (*info)); | |
179 | info->pid = ptid_get_pid (inferior_ptid); | |
180 | info->handle = handle; | |
4d062f1a | 181 | info->need_stale_parent_threads_check = 1; |
d90e17a7 PA |
182 | |
183 | info->next = thread_db_list; | |
184 | thread_db_list = info; | |
185 | ||
186 | return info; | |
187 | } | |
188 | ||
189 | /* Return the thread_db_info object representing the bookkeeping | |
190 | related to process PID, if any; NULL otherwise. */ | |
191 | ||
192 | static struct thread_db_info * | |
193 | get_thread_db_info (int pid) | |
194 | { | |
195 | struct thread_db_info *info; | |
196 | ||
197 | for (info = thread_db_list; info; info = info->next) | |
198 | if (pid == info->pid) | |
199 | return info; | |
200 | ||
201 | return NULL; | |
202 | } | |
203 | ||
204 | /* When PID has exited or has been detached, we no longer want to keep | |
205 | track of it as using libpthread. Call this function to discard | |
206 | thread_db related info related to PID. Note that this closes | |
207 | LIBTHREAD_DB_SO's dlopen'ed handle. */ | |
208 | ||
209 | static void | |
210 | delete_thread_db_info (int pid) | |
211 | { | |
212 | struct thread_db_info *info, *info_prev; | |
213 | ||
214 | info_prev = NULL; | |
215 | ||
216 | for (info = thread_db_list; info; info_prev = info, info = info->next) | |
217 | if (pid == info->pid) | |
218 | break; | |
219 | ||
220 | if (info == NULL) | |
221 | return; | |
222 | ||
223 | if (info->handle != NULL) | |
224 | dlclose (info->handle); | |
225 | ||
226 | if (info_prev) | |
227 | info_prev->next = info->next; | |
228 | else | |
229 | thread_db_list = info->next; | |
230 | ||
231 | xfree (info); | |
232 | } | |
fb0e1ba7 MK |
233 | |
234 | /* Prototypes for local functions. */ | |
02c6c942 PP |
235 | static int attach_thread (ptid_t ptid, const td_thrhandle_t *th_p, |
236 | const td_thrinfo_t *ti_p); | |
17faa917 | 237 | static void detach_thread (ptid_t ptid); |
fb0e1ba7 MK |
238 | \f |
239 | ||
5365276c DJ |
240 | /* Use "struct private_thread_info" to cache thread state. This is |
241 | a substantial optimization. */ | |
242 | ||
fb0e1ba7 MK |
243 | struct private_thread_info |
244 | { | |
a2f23071 DJ |
245 | /* Flag set when we see a TD_DEATH event for this thread. */ |
246 | unsigned int dying:1; | |
247 | ||
5365276c | 248 | /* Cached thread state. */ |
5365276c | 249 | td_thrhandle_t th; |
17faa917 | 250 | thread_t tid; |
fb0e1ba7 | 251 | }; |
fb0e1ba7 | 252 | \f |
21bf60fe | 253 | |
fb0e1ba7 MK |
254 | static char * |
255 | thread_db_err_str (td_err_e err) | |
256 | { | |
257 | static char buf[64]; | |
258 | ||
259 | switch (err) | |
260 | { | |
261 | case TD_OK: | |
262 | return "generic 'call succeeded'"; | |
263 | case TD_ERR: | |
264 | return "generic error"; | |
265 | case TD_NOTHR: | |
266 | return "no thread to satisfy query"; | |
267 | case TD_NOSV: | |
268 | return "no sync handle to satisfy query"; | |
269 | case TD_NOLWP: | |
270 | return "no LWP to satisfy query"; | |
271 | case TD_BADPH: | |
272 | return "invalid process handle"; | |
273 | case TD_BADTH: | |
274 | return "invalid thread handle"; | |
275 | case TD_BADSH: | |
276 | return "invalid synchronization handle"; | |
277 | case TD_BADTA: | |
278 | return "invalid thread agent"; | |
279 | case TD_BADKEY: | |
280 | return "invalid key"; | |
281 | case TD_NOMSG: | |
282 | return "no event message for getmsg"; | |
283 | case TD_NOFPREGS: | |
284 | return "FPU register set not available"; | |
285 | case TD_NOLIBTHREAD: | |
286 | return "application not linked with libthread"; | |
287 | case TD_NOEVENT: | |
288 | return "requested event is not supported"; | |
289 | case TD_NOCAPAB: | |
290 | return "capability not available"; | |
291 | case TD_DBERR: | |
292 | return "debugger service failed"; | |
293 | case TD_NOAPLIC: | |
294 | return "operation not applicable to"; | |
295 | case TD_NOTSD: | |
296 | return "no thread-specific data for this thread"; | |
297 | case TD_MALLOC: | |
298 | return "malloc failed"; | |
299 | case TD_PARTIALREG: | |
300 | return "only part of register set was written/read"; | |
301 | case TD_NOXREGS: | |
302 | return "X register set not available for this thread"; | |
59f80f10 DJ |
303 | #ifdef THREAD_DB_HAS_TD_NOTALLOC |
304 | case TD_NOTALLOC: | |
305 | return "thread has not yet allocated TLS for given module"; | |
306 | #endif | |
307 | #ifdef THREAD_DB_HAS_TD_VERSION | |
308 | case TD_VERSION: | |
309 | return "versions of libpthread and libthread_db do not match"; | |
310 | #endif | |
311 | #ifdef THREAD_DB_HAS_TD_NOTLS | |
312 | case TD_NOTLS: | |
313 | return "there is no TLS segment in the given module"; | |
314 | #endif | |
fb0e1ba7 MK |
315 | default: |
316 | snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err); | |
317 | return buf; | |
318 | } | |
319 | } | |
fb0e1ba7 | 320 | \f |
4105de34 DJ |
321 | /* Return 1 if any threads have been registered. There may be none if |
322 | the threading library is not fully initialized yet. */ | |
323 | ||
324 | static int | |
d90e17a7 | 325 | have_threads_callback (struct thread_info *thread, void *args) |
4105de34 | 326 | { |
d90e17a7 PA |
327 | int pid = * (int *) args; |
328 | if (ptid_get_pid (thread->ptid) != pid) | |
329 | return 0; | |
330 | ||
e3bc4218 | 331 | return thread->private != NULL; |
4105de34 DJ |
332 | } |
333 | ||
334 | static int | |
d90e17a7 | 335 | have_threads (ptid_t ptid) |
4105de34 | 336 | { |
d90e17a7 PA |
337 | int pid = ptid_get_pid (ptid); |
338 | ||
339 | return iterate_over_threads (have_threads_callback, &pid) != NULL; | |
4105de34 DJ |
340 | } |
341 | ||
d90e17a7 PA |
342 | struct thread_get_info_inout |
343 | { | |
344 | struct thread_info *thread_info; | |
345 | struct thread_db_info *thread_db_info; | |
346 | }; | |
347 | ||
5365276c | 348 | /* A callback function for td_ta_thr_iter, which we use to map all |
cdbc0b18 | 349 | threads to LWPs. |
5365276c DJ |
350 | |
351 | THP is a handle to the current thread; if INFOP is not NULL, the | |
352 | struct thread_info associated with this thread is returned in | |
b9b5d7ea JJ |
353 | *INFOP. |
354 | ||
355 | If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise, | |
356 | zero is returned to indicate success. */ | |
5365276c DJ |
357 | |
358 | static int | |
d90e17a7 | 359 | thread_get_info_callback (const td_thrhandle_t *thp, void *argp) |
5365276c DJ |
360 | { |
361 | td_thrinfo_t ti; | |
362 | td_err_e err; | |
5365276c | 363 | ptid_t thread_ptid; |
d90e17a7 PA |
364 | struct thread_get_info_inout *inout; |
365 | struct thread_db_info *info; | |
366 | ||
367 | inout = argp; | |
368 | info = inout->thread_db_info; | |
5365276c | 369 | |
d90e17a7 | 370 | err = info->td_thr_get_info_p (thp, &ti); |
5365276c | 371 | if (err != TD_OK) |
8a3fe4f8 | 372 | error (_("thread_get_info_callback: cannot get thread info: %s"), |
5365276c DJ |
373 | thread_db_err_str (err)); |
374 | ||
375 | /* Fill the cache. */ | |
d90e17a7 | 376 | thread_ptid = ptid_build (info->pid, ti.ti_lid, 0); |
e09875d4 | 377 | inout->thread_info = find_thread_ptid (thread_ptid); |
5365276c | 378 | |
b9b5d7ea | 379 | /* In the case of a zombie thread, don't continue. We don't want to |
f90ef764 | 380 | attach to it thinking it is a new thread. */ |
b9b5d7ea | 381 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) |
d90e17a7 | 382 | return TD_THR_ZOMBIE; |
b9b5d7ea | 383 | |
d90e17a7 | 384 | if (inout->thread_info == NULL) |
5365276c DJ |
385 | { |
386 | /* New thread. Attach to it now (why wait?). */ | |
d90e17a7 PA |
387 | if (!have_threads (thread_ptid)) |
388 | thread_db_find_new_threads_1 (thread_ptid); | |
4c28f408 PA |
389 | else |
390 | attach_thread (thread_ptid, thp, &ti); | |
e09875d4 | 391 | inout->thread_info = find_thread_ptid (thread_ptid); |
d90e17a7 | 392 | gdb_assert (inout->thread_info != NULL); |
5365276c DJ |
393 | } |
394 | ||
5365276c DJ |
395 | return 0; |
396 | } | |
5365276c | 397 | \f |
fb0e1ba7 MK |
398 | /* Convert between user-level thread ids and LWP ids. */ |
399 | ||
39f77062 KB |
400 | static ptid_t |
401 | thread_from_lwp (ptid_t ptid) | |
fb0e1ba7 | 402 | { |
fb0e1ba7 MK |
403 | td_thrhandle_t th; |
404 | td_err_e err; | |
5365276c | 405 | ptid_t thread_ptid; |
d90e17a7 PA |
406 | struct thread_db_info *info; |
407 | struct thread_get_info_inout io = {0}; | |
fb0e1ba7 | 408 | |
17faa917 DJ |
409 | /* This ptid comes from linux-nat.c, which should always fill in the |
410 | LWP. */ | |
411 | gdb_assert (GET_LWP (ptid) != 0); | |
fb0e1ba7 | 412 | |
d90e17a7 PA |
413 | info = get_thread_db_info (GET_PID (ptid)); |
414 | ||
4c28f408 | 415 | /* Access an lwp we know is stopped. */ |
d90e17a7 PA |
416 | info->proc_handle.ptid = ptid; |
417 | err = info->td_ta_map_lwp2thr_p (info->thread_agent, GET_LWP (ptid), &th); | |
fb0e1ba7 | 418 | if (err != TD_OK) |
8a3fe4f8 | 419 | error (_("Cannot find user-level thread for LWP %ld: %s"), |
39f77062 | 420 | GET_LWP (ptid), thread_db_err_str (err)); |
fb0e1ba7 | 421 | |
b9b5d7ea JJ |
422 | /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the |
423 | event thread has already died. If another gdb interface has called | |
424 | thread_alive() previously, the thread won't be found on the thread list | |
425 | anymore. In that case, we don't want to process this ptid anymore | |
426 | to avoid the possibility of later treating it as a newly | |
427 | discovered thread id that we should add to the list. Thus, | |
428 | we return a -1 ptid which is also how the thread list marks a | |
429 | dead thread. */ | |
d90e17a7 PA |
430 | io.thread_db_info = info; |
431 | io.thread_info = NULL; | |
432 | if (thread_get_info_callback (&th, &io) == TD_THR_ZOMBIE | |
433 | && io.thread_info == NULL) | |
434 | return minus_one_ptid; | |
b9b5d7ea | 435 | |
17faa917 DJ |
436 | gdb_assert (ptid_get_tid (ptid) == 0); |
437 | return ptid; | |
fb0e1ba7 MK |
438 | } |
439 | \f | |
440 | ||
4c28f408 PA |
441 | /* Attach to lwp PTID, doing whatever else is required to have this |
442 | LWP under the debugger's control --- e.g., enabling event | |
443 | reporting. Returns true on success. */ | |
444 | int | |
445 | thread_db_attach_lwp (ptid_t ptid) | |
446 | { | |
447 | td_thrhandle_t th; | |
448 | td_thrinfo_t ti; | |
449 | td_err_e err; | |
d90e17a7 | 450 | struct thread_db_info *info; |
4c28f408 | 451 | |
d90e17a7 PA |
452 | info = get_thread_db_info (GET_PID (ptid)); |
453 | ||
454 | if (info == NULL) | |
4c28f408 PA |
455 | return 0; |
456 | ||
457 | /* This ptid comes from linux-nat.c, which should always fill in the | |
458 | LWP. */ | |
459 | gdb_assert (GET_LWP (ptid) != 0); | |
460 | ||
461 | /* Access an lwp we know is stopped. */ | |
d90e17a7 | 462 | info->proc_handle.ptid = ptid; |
4c28f408 PA |
463 | |
464 | /* If we have only looked at the first thread before libpthread was | |
465 | initialized, we may not know its thread ID yet. Make sure we do | |
466 | before we add another thread to the list. */ | |
d90e17a7 PA |
467 | if (!have_threads (ptid)) |
468 | thread_db_find_new_threads_1 (ptid); | |
4c28f408 | 469 | |
d90e17a7 | 470 | err = info->td_ta_map_lwp2thr_p (info->thread_agent, GET_LWP (ptid), &th); |
4c28f408 PA |
471 | if (err != TD_OK) |
472 | /* Cannot find user-level thread. */ | |
473 | return 0; | |
474 | ||
d90e17a7 | 475 | err = info->td_thr_get_info_p (&th, &ti); |
4c28f408 PA |
476 | if (err != TD_OK) |
477 | { | |
478 | warning (_("Cannot get thread info: %s"), thread_db_err_str (err)); | |
479 | return 0; | |
480 | } | |
481 | ||
482 | attach_thread (ptid, &th, &ti); | |
483 | return 1; | |
484 | } | |
485 | ||
5220ea4c AC |
486 | static void * |
487 | verbose_dlsym (void *handle, const char *name) | |
488 | { | |
489 | void *sym = dlsym (handle, name); | |
490 | if (sym == NULL) | |
8a3fe4f8 | 491 | warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ()); |
5220ea4c AC |
492 | return sym; |
493 | } | |
494 | ||
cdbc0b18 | 495 | static td_err_e |
d90e17a7 | 496 | enable_thread_event (int event, CORE_ADDR *bp) |
24557e30 AC |
497 | { |
498 | td_notify_t notify; | |
cdbc0b18 | 499 | td_err_e err; |
d90e17a7 PA |
500 | struct thread_db_info *info; |
501 | ||
502 | info = get_thread_db_info (GET_PID (inferior_ptid)); | |
24557e30 | 503 | |
4c28f408 | 504 | /* Access an lwp we know is stopped. */ |
d90e17a7 | 505 | info->proc_handle.ptid = inferior_ptid; |
4c28f408 | 506 | |
24557e30 | 507 | /* Get the breakpoint address for thread EVENT. */ |
d90e17a7 | 508 | err = info->td_ta_event_addr_p (info->thread_agent, event, ¬ify); |
24557e30 | 509 | if (err != TD_OK) |
cdbc0b18 | 510 | return err; |
24557e30 AC |
511 | |
512 | /* Set up the breakpoint. */ | |
16451949 AS |
513 | gdb_assert (exec_bfd); |
514 | (*bp) = (gdbarch_convert_from_func_ptr_addr | |
a97b0ac8 | 515 | (target_gdbarch, |
16451949 AS |
516 | /* Do proper sign extension for the target. */ |
517 | (bfd_get_sign_extend_vma (exec_bfd) > 0 | |
518 | ? (CORE_ADDR) (intptr_t) notify.u.bptaddr | |
519 | : (CORE_ADDR) (uintptr_t) notify.u.bptaddr), | |
520 | ¤t_target)); | |
a6d9a66e | 521 | create_thread_event_breakpoint (target_gdbarch, *bp); |
24557e30 | 522 | |
cdbc0b18 | 523 | return TD_OK; |
24557e30 AC |
524 | } |
525 | ||
fb0e1ba7 MK |
526 | static void |
527 | enable_thread_event_reporting (void) | |
528 | { | |
529 | td_thr_events_t events; | |
530 | td_notify_t notify; | |
531 | td_err_e err; | |
a2f23071 DJ |
532 | #ifdef HAVE_GNU_LIBC_VERSION_H |
533 | const char *libc_version; | |
534 | int libc_major, libc_minor; | |
535 | #endif | |
d90e17a7 PA |
536 | struct thread_db_info *info; |
537 | ||
538 | info = get_thread_db_info (GET_PID (inferior_ptid)); | |
fb0e1ba7 MK |
539 | |
540 | /* We cannot use the thread event reporting facility if these | |
541 | functions aren't available. */ | |
d90e17a7 PA |
542 | if (info->td_ta_event_addr_p == NULL |
543 | || info->td_ta_set_event_p == NULL | |
544 | || info->td_ta_event_getmsg_p == NULL | |
545 | || info->td_thr_event_enable_p == NULL) | |
fb0e1ba7 MK |
546 | return; |
547 | ||
548 | /* Set the process wide mask saying which events we're interested in. */ | |
549 | td_event_emptyset (&events); | |
550 | td_event_addset (&events, TD_CREATE); | |
a2f23071 DJ |
551 | |
552 | #ifdef HAVE_GNU_LIBC_VERSION_H | |
34091d9b | 553 | /* The event reporting facility is broken for TD_DEATH events in |
2ef52e77 | 554 | glibc 2.1.3, so don't enable it if we have glibc but a lower |
34091d9b | 555 | version. */ |
a2f23071 DJ |
556 | libc_version = gnu_get_libc_version (); |
557 | if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2 | |
558 | && (libc_major > 2 || (libc_major == 2 && libc_minor > 1))) | |
fb0e1ba7 | 559 | #endif |
a2f23071 | 560 | td_event_addset (&events, TD_DEATH); |
fb0e1ba7 | 561 | |
d90e17a7 | 562 | err = info->td_ta_set_event_p (info->thread_agent, &events); |
fb0e1ba7 MK |
563 | if (err != TD_OK) |
564 | { | |
8a3fe4f8 | 565 | warning (_("Unable to set global thread event mask: %s"), |
fb0e1ba7 MK |
566 | thread_db_err_str (err)); |
567 | return; | |
568 | } | |
569 | ||
570 | /* Delete previous thread event breakpoints, if any. */ | |
571 | remove_thread_event_breakpoints (); | |
d90e17a7 PA |
572 | info->td_create_bp_addr = 0; |
573 | info->td_death_bp_addr = 0; | |
fb0e1ba7 | 574 | |
24557e30 | 575 | /* Set up the thread creation event. */ |
d90e17a7 | 576 | err = enable_thread_event (TD_CREATE, &info->td_create_bp_addr); |
cdbc0b18 | 577 | if (err != TD_OK) |
fb0e1ba7 | 578 | { |
8a3fe4f8 | 579 | warning (_("Unable to get location for thread creation breakpoint: %s"), |
fb0e1ba7 MK |
580 | thread_db_err_str (err)); |
581 | return; | |
582 | } | |
583 | ||
24557e30 | 584 | /* Set up the thread death event. */ |
d90e17a7 | 585 | err = enable_thread_event (TD_DEATH, &info->td_death_bp_addr); |
cdbc0b18 | 586 | if (err != TD_OK) |
fb0e1ba7 | 587 | { |
8a3fe4f8 | 588 | warning (_("Unable to get location for thread death breakpoint: %s"), |
fb0e1ba7 MK |
589 | thread_db_err_str (err)); |
590 | return; | |
591 | } | |
fb0e1ba7 MK |
592 | } |
593 | ||
456b0e24 PP |
594 | /* Same as thread_db_find_new_threads_1, but silently ignore errors. */ |
595 | ||
596 | static void | |
597 | thread_db_find_new_threads_silently (ptid_t ptid) | |
598 | { | |
599 | volatile struct gdb_exception except; | |
600 | ||
601 | TRY_CATCH (except, RETURN_MASK_ERROR) | |
602 | { | |
02c6c942 | 603 | thread_db_find_new_threads_2 (ptid, 1); |
456b0e24 PP |
604 | } |
605 | ||
606 | if (except.reason < 0 && info_verbose) | |
607 | { | |
608 | exception_fprintf (gdb_stderr, except, | |
609 | "Warning: thread_db_find_new_threads_silently: "); | |
610 | } | |
611 | } | |
612 | ||
d90e17a7 PA |
613 | /* Lookup a library in which given symbol resides. |
614 | Note: this is looking in GDB process, not in the inferior. | |
615 | Returns library name, or NULL. */ | |
616 | ||
617 | static const char * | |
618 | dladdr_to_soname (const void *addr) | |
619 | { | |
620 | Dl_info info; | |
621 | ||
622 | if (dladdr (addr, &info) != 0) | |
623 | return info.dli_fname; | |
624 | return NULL; | |
625 | } | |
626 | ||
17a37d48 PP |
627 | /* Attempt to initialize dlopen()ed libthread_db, described by HANDLE. |
628 | Return 1 on success. | |
629 | Failure could happen if libthread_db does not have symbols we expect, | |
630 | or when it refuses to work with the current inferior (e.g. due to | |
631 | version mismatch between libthread_db and libpthread). */ | |
632 | ||
633 | static int | |
d90e17a7 | 634 | try_thread_db_load_1 (struct thread_db_info *info) |
17a37d48 PP |
635 | { |
636 | td_err_e err; | |
637 | ||
638 | /* Initialize pointers to the dynamic library functions we will use. | |
639 | Essential functions first. */ | |
640 | ||
d90e17a7 PA |
641 | info->td_init_p = verbose_dlsym (info->handle, "td_init"); |
642 | if (info->td_init_p == NULL) | |
17a37d48 PP |
643 | return 0; |
644 | ||
d90e17a7 | 645 | err = info->td_init_p (); |
17a37d48 PP |
646 | if (err != TD_OK) |
647 | { | |
648 | warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err)); | |
649 | return 0; | |
650 | } | |
651 | ||
d90e17a7 PA |
652 | info->td_ta_new_p = verbose_dlsym (info->handle, "td_ta_new"); |
653 | if (info->td_ta_new_p == NULL) | |
17a37d48 PP |
654 | return 0; |
655 | ||
656 | /* Initialize the structure that identifies the child process. */ | |
d90e17a7 | 657 | info->proc_handle.ptid = inferior_ptid; |
17a37d48 PP |
658 | |
659 | /* Now attempt to open a connection to the thread library. */ | |
d90e17a7 | 660 | err = info->td_ta_new_p (&info->proc_handle, &info->thread_agent); |
17a37d48 PP |
661 | if (err != TD_OK) |
662 | { | |
17a37d48 PP |
663 | if (info_verbose) |
664 | printf_unfiltered (_("td_ta_new failed: %s\n"), | |
665 | thread_db_err_str (err)); | |
666 | else | |
667 | switch (err) | |
668 | { | |
669 | case TD_NOLIBTHREAD: | |
670 | #ifdef THREAD_DB_HAS_TD_VERSION | |
671 | case TD_VERSION: | |
672 | #endif | |
673 | /* The errors above are not unexpected and silently ignored: | |
674 | they just mean we haven't found correct version of | |
675 | libthread_db yet. */ | |
676 | break; | |
677 | default: | |
678 | warning (_("td_ta_new failed: %s"), thread_db_err_str (err)); | |
679 | } | |
680 | return 0; | |
681 | } | |
682 | ||
d90e17a7 PA |
683 | info->td_ta_map_id2thr_p = verbose_dlsym (info->handle, "td_ta_map_id2thr"); |
684 | if (info->td_ta_map_id2thr_p == NULL) | |
17a37d48 PP |
685 | return 0; |
686 | ||
d90e17a7 PA |
687 | info->td_ta_map_lwp2thr_p = verbose_dlsym (info->handle, "td_ta_map_lwp2thr"); |
688 | if (info->td_ta_map_lwp2thr_p == NULL) | |
17a37d48 PP |
689 | return 0; |
690 | ||
d90e17a7 PA |
691 | info->td_ta_thr_iter_p = verbose_dlsym (info->handle, "td_ta_thr_iter"); |
692 | if (info->td_ta_thr_iter_p == NULL) | |
17a37d48 PP |
693 | return 0; |
694 | ||
d90e17a7 PA |
695 | info->td_thr_validate_p = verbose_dlsym (info->handle, "td_thr_validate"); |
696 | if (info->td_thr_validate_p == NULL) | |
17a37d48 PP |
697 | return 0; |
698 | ||
d90e17a7 PA |
699 | info->td_thr_get_info_p = verbose_dlsym (info->handle, "td_thr_get_info"); |
700 | if (info->td_thr_get_info_p == NULL) | |
17a37d48 PP |
701 | return 0; |
702 | ||
703 | /* These are not essential. */ | |
d90e17a7 PA |
704 | info->td_ta_event_addr_p = dlsym (info->handle, "td_ta_event_addr"); |
705 | info->td_ta_set_event_p = dlsym (info->handle, "td_ta_set_event"); | |
21e1bee4 | 706 | info->td_ta_clear_event_p = dlsym (info->handle, "td_ta_clear_event"); |
d90e17a7 PA |
707 | info->td_ta_event_getmsg_p = dlsym (info->handle, "td_ta_event_getmsg"); |
708 | info->td_thr_event_enable_p = dlsym (info->handle, "td_thr_event_enable"); | |
709 | info->td_thr_tls_get_addr_p = dlsym (info->handle, "td_thr_tls_get_addr"); | |
17a37d48 PP |
710 | |
711 | printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n")); | |
712 | ||
d90e17a7 PA |
713 | if (info_verbose || *libthread_db_search_path) |
714 | { | |
715 | const char *library; | |
17a37d48 | 716 | |
d90e17a7 PA |
717 | library = dladdr_to_soname (*info->td_ta_new_p); |
718 | if (library == NULL) | |
719 | library = LIBTHREAD_DB_SO; | |
17a37d48 | 720 | |
d90e17a7 PA |
721 | printf_unfiltered (_("Using host libthread_db library \"%s\".\n"), |
722 | library); | |
723 | } | |
17a37d48 | 724 | |
d90e17a7 PA |
725 | /* The thread library was detected. Activate the thread_db target |
726 | if this is the first process using it. */ | |
727 | if (thread_db_list->next == NULL) | |
728 | push_target (&thread_db_ops); | |
17a37d48 | 729 | |
d90e17a7 | 730 | enable_thread_event_reporting (); |
456b0e24 | 731 | |
099cb4fb | 732 | /* There appears to be a bug in glibc-2.3.6: calls to td_thr_get_info fail |
456b0e24 PP |
733 | with TD_ERR for statically linked executables if td_thr_get_info is |
734 | called before glibc has initialized itself. Silently ignore such | |
099cb4fb | 735 | errors, and let gdb enumerate threads again later. */ |
456b0e24 | 736 | thread_db_find_new_threads_silently (inferior_ptid); |
099cb4fb | 737 | |
d90e17a7 | 738 | return 1; |
17a37d48 PP |
739 | } |
740 | ||
741 | /* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute, | |
742 | relative, or just LIBTHREAD_DB. */ | |
743 | ||
744 | static int | |
745 | try_thread_db_load (const char *library) | |
746 | { | |
747 | void *handle; | |
d90e17a7 | 748 | struct thread_db_info *info; |
17a37d48 PP |
749 | |
750 | if (info_verbose) | |
751 | printf_unfiltered (_("Trying host libthread_db library: %s.\n"), | |
752 | library); | |
753 | handle = dlopen (library, RTLD_NOW); | |
754 | if (handle == NULL) | |
755 | { | |
756 | if (info_verbose) | |
757 | printf_unfiltered (_("dlopen failed: %s.\n"), dlerror ()); | |
758 | return 0; | |
759 | } | |
760 | ||
761 | if (info_verbose && strchr (library, '/') == NULL) | |
762 | { | |
763 | void *td_init; | |
764 | ||
765 | td_init = dlsym (handle, "td_init"); | |
766 | if (td_init != NULL) | |
767 | { | |
768 | const char *const libpath = dladdr_to_soname (td_init); | |
769 | ||
770 | if (libpath != NULL) | |
771 | printf_unfiltered (_("Host %s resolved to: %s.\n"), | |
772 | library, libpath); | |
773 | } | |
774 | } | |
775 | ||
d90e17a7 PA |
776 | info = add_thread_db_info (handle); |
777 | ||
778 | if (try_thread_db_load_1 (info)) | |
17a37d48 PP |
779 | return 1; |
780 | ||
781 | /* This library "refused" to work on current inferior. */ | |
d90e17a7 | 782 | delete_thread_db_info (GET_PID (inferior_ptid)); |
17a37d48 PP |
783 | return 0; |
784 | } | |
785 | ||
786 | ||
787 | /* Search libthread_db_search_path for libthread_db which "agrees" | |
788 | to work on current inferior. */ | |
789 | ||
790 | static int | |
791 | thread_db_load_search (void) | |
792 | { | |
793 | char path[PATH_MAX]; | |
794 | const char *search_path = libthread_db_search_path; | |
795 | int rc = 0; | |
796 | ||
797 | while (*search_path) | |
798 | { | |
799 | const char *end = strchr (search_path, ':'); | |
800 | if (end) | |
801 | { | |
802 | size_t len = end - search_path; | |
803 | if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path)) | |
804 | { | |
805 | char *cp = xmalloc (len + 1); | |
806 | memcpy (cp, search_path, len); | |
807 | cp[len] = '\0'; | |
808 | warning (_("libthread_db_search_path component too long," | |
809 | " ignored: %s."), cp); | |
810 | xfree (cp); | |
811 | search_path += len + 1; | |
812 | continue; | |
813 | } | |
814 | memcpy (path, search_path, len); | |
815 | path[len] = '\0'; | |
816 | search_path += len + 1; | |
817 | } | |
818 | else | |
819 | { | |
820 | size_t len = strlen (search_path); | |
821 | ||
822 | if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path)) | |
823 | { | |
824 | warning (_("libthread_db_search_path component too long," | |
825 | " ignored: %s."), search_path); | |
826 | break; | |
827 | } | |
828 | memcpy (path, search_path, len + 1); | |
829 | search_path += len; | |
830 | } | |
831 | strcat (path, "/"); | |
832 | strcat (path, LIBTHREAD_DB_SO); | |
833 | if (try_thread_db_load (path)) | |
834 | { | |
835 | rc = 1; | |
836 | break; | |
837 | } | |
838 | } | |
839 | if (rc == 0) | |
840 | rc = try_thread_db_load (LIBTHREAD_DB_SO); | |
841 | return rc; | |
842 | } | |
843 | ||
844 | /* Attempt to load and initialize libthread_db. | |
845 | Return 1 on success. | |
846 | */ | |
847 | ||
848 | static int | |
849 | thread_db_load (void) | |
850 | { | |
851 | struct objfile *obj; | |
d90e17a7 | 852 | struct thread_db_info *info; |
17a37d48 | 853 | |
d90e17a7 PA |
854 | info = get_thread_db_info (GET_PID (inferior_ptid)); |
855 | ||
856 | if (info != NULL) | |
17a37d48 PP |
857 | return 1; |
858 | ||
859 | /* Don't attempt to use thread_db on targets which can not run | |
860 | (executables not running yet, core files) for now. */ | |
861 | if (!target_has_execution) | |
862 | return 0; | |
863 | ||
864 | /* Don't attempt to use thread_db for remote targets. */ | |
865 | if (!target_can_run (¤t_target)) | |
866 | return 0; | |
867 | ||
868 | if (thread_db_load_search ()) | |
869 | return 1; | |
870 | ||
871 | /* None of the libthread_db's on our search path, not the system default | |
872 | ones worked. If the executable is dynamically linked against | |
873 | libpthread, try loading libthread_db from the same directory. */ | |
874 | ||
875 | ALL_OBJFILES (obj) | |
876 | if (libpthread_name_p (obj->name)) | |
877 | { | |
878 | char path[PATH_MAX], *cp; | |
879 | ||
880 | gdb_assert (strlen (obj->name) < sizeof (path)); | |
881 | strcpy (path, obj->name); | |
882 | cp = strrchr (path, '/'); | |
883 | ||
884 | if (cp == NULL) | |
885 | { | |
886 | warning (_("Expected absolute pathname for libpthread in the" | |
887 | " inferior, but got %s."), path); | |
888 | } | |
889 | else if (cp + 1 + strlen (LIBTHREAD_DB_SO) + 1 > path + sizeof (path)) | |
890 | { | |
891 | warning (_("Unexpected: path to libpthread in the inferior is" | |
892 | " too long: %s"), path); | |
893 | } | |
894 | else | |
895 | { | |
896 | strcpy (cp + 1, LIBTHREAD_DB_SO); | |
897 | if (try_thread_db_load (path)) | |
898 | return 1; | |
899 | } | |
900 | warning (_("Unable to find libthread_db matching inferior's thread" | |
901 | " library, thread debugging will not be available.")); | |
902 | return 0; | |
903 | } | |
904 | /* Either this executable isn't using libpthread at all, or it is | |
905 | statically linked. Since we can't easily distinguish these two cases, | |
906 | no warning is issued. */ | |
907 | return 0; | |
908 | } | |
909 | ||
fb0e1ba7 | 910 | static void |
12b6a110 | 911 | disable_thread_event_reporting (struct thread_db_info *info) |
fb0e1ba7 | 912 | { |
21e1bee4 | 913 | if (info->td_ta_clear_event_p != NULL) |
12b6a110 PP |
914 | { |
915 | td_thr_events_t events; | |
fb0e1ba7 | 916 | |
12b6a110 PP |
917 | /* Set the process wide mask saying we aren't interested in any |
918 | events anymore. */ | |
21e1bee4 PP |
919 | td_event_fillset (&events); |
920 | info->td_ta_clear_event_p (info->thread_agent, &events); | |
12b6a110 | 921 | } |
fb0e1ba7 | 922 | |
d90e17a7 PA |
923 | info->td_create_bp_addr = 0; |
924 | info->td_death_bp_addr = 0; | |
fb0e1ba7 MK |
925 | } |
926 | ||
927 | static void | |
928 | check_thread_signals (void) | |
929 | { | |
930 | #ifdef GET_THREAD_SIGNALS | |
21bf60fe | 931 | if (!thread_signals) |
fb0e1ba7 MK |
932 | { |
933 | sigset_t mask; | |
934 | int i; | |
935 | ||
936 | GET_THREAD_SIGNALS (&mask); | |
937 | sigemptyset (&thread_stop_set); | |
938 | sigemptyset (&thread_print_set); | |
939 | ||
b9569773 | 940 | for (i = 1; i < NSIG; i++) |
fb0e1ba7 MK |
941 | { |
942 | if (sigismember (&mask, i)) | |
943 | { | |
944 | if (signal_stop_update (target_signal_from_host (i), 0)) | |
945 | sigaddset (&thread_stop_set, i); | |
946 | if (signal_print_update (target_signal_from_host (i), 0)) | |
947 | sigaddset (&thread_print_set, i); | |
948 | thread_signals = 1; | |
949 | } | |
950 | } | |
951 | } | |
952 | #endif | |
953 | } | |
954 | ||
0ec9a092 DJ |
955 | /* Check whether thread_db is usable. This function is called when |
956 | an inferior is created (or otherwise acquired, e.g. attached to) | |
957 | and when new shared libraries are loaded into a running process. */ | |
958 | ||
959 | void | |
960 | check_for_thread_db (void) | |
fb0e1ba7 MK |
961 | { |
962 | td_err_e err; | |
17a37d48 | 963 | static void *last_loaded; |
fb0e1ba7 | 964 | |
b5057acd | 965 | /* Do nothing if we couldn't load libthread_db.so.1. */ |
17a37d48 | 966 | if (!thread_db_load ()) |
b5057acd | 967 | return; |
0ec9a092 DJ |
968 | } |
969 | ||
970 | static void | |
971 | thread_db_new_objfile (struct objfile *objfile) | |
972 | { | |
d90e17a7 PA |
973 | /* This observer must always be called with inferior_ptid set |
974 | correctly. */ | |
975 | ||
0ec9a092 DJ |
976 | if (objfile != NULL) |
977 | check_for_thread_db (); | |
fb0e1ba7 MK |
978 | } |
979 | ||
a2f23071 DJ |
980 | /* Attach to a new thread. This function is called when we receive a |
981 | TD_CREATE event or when we iterate over all threads and find one | |
02c6c942 | 982 | that wasn't already in our list. Returns true on success. */ |
a2f23071 | 983 | |
02c6c942 | 984 | static int |
39f77062 | 985 | attach_thread (ptid_t ptid, const td_thrhandle_t *th_p, |
93815fbf | 986 | const td_thrinfo_t *ti_p) |
fb0e1ba7 | 987 | { |
17faa917 DJ |
988 | struct private_thread_info *private; |
989 | struct thread_info *tp = NULL; | |
fb0e1ba7 | 990 | td_err_e err; |
d90e17a7 | 991 | struct thread_db_info *info; |
fb0e1ba7 | 992 | |
a2f23071 DJ |
993 | /* If we're being called after a TD_CREATE event, we may already |
994 | know about this thread. There are two ways this can happen. We | |
995 | may have iterated over all threads between the thread creation | |
996 | and the TD_CREATE event, for instance when the user has issued | |
997 | the `info threads' command before the SIGTRAP for hitting the | |
998 | thread creation breakpoint was reported. Alternatively, the | |
999 | thread may have exited and a new one been created with the same | |
1000 | thread ID. In the first case we don't need to do anything; in | |
1001 | the second case we should discard information about the dead | |
1002 | thread and attach to the new one. */ | |
1003 | if (in_thread_list (ptid)) | |
1004 | { | |
e09875d4 | 1005 | tp = find_thread_ptid (ptid); |
a2f23071 DJ |
1006 | gdb_assert (tp != NULL); |
1007 | ||
17faa917 DJ |
1008 | /* If tp->private is NULL, then GDB is already attached to this |
1009 | thread, but we do not know anything about it. We can learn | |
1010 | about it here. This can only happen if we have some other | |
1011 | way besides libthread_db to notice new threads (i.e. | |
1012 | PTRACE_EVENT_CLONE); assume the same mechanism notices thread | |
1013 | exit, so this can not be a stale thread recreated with the | |
1014 | same ID. */ | |
1015 | if (tp->private != NULL) | |
1016 | { | |
1017 | if (!tp->private->dying) | |
02c6c942 | 1018 | return 0; |
a2f23071 | 1019 | |
17faa917 DJ |
1020 | delete_thread (ptid); |
1021 | tp = NULL; | |
1022 | } | |
a2f23071 DJ |
1023 | } |
1024 | ||
fb0e1ba7 MK |
1025 | check_thread_signals (); |
1026 | ||
9ee57c33 | 1027 | if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE) |
02c6c942 | 1028 | return 0; /* A zombie thread -- do not attach. */ |
9ee57c33 DJ |
1029 | |
1030 | /* Under GNU/Linux, we have to attach to each and every thread. */ | |
17faa917 DJ |
1031 | if (tp == NULL |
1032 | && lin_lwp_attach_lwp (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid))) < 0) | |
02c6c942 | 1033 | return 0; |
9ee57c33 | 1034 | |
17faa917 DJ |
1035 | /* Construct the thread's private data. */ |
1036 | private = xmalloc (sizeof (struct private_thread_info)); | |
1037 | memset (private, 0, sizeof (struct private_thread_info)); | |
1038 | ||
1039 | /* A thread ID of zero may mean the thread library has not initialized | |
1040 | yet. But we shouldn't even get here if that's the case. FIXME: | |
1041 | if we change GDB to always have at least one thread in the thread | |
1042 | list this will have to go somewhere else; maybe private == NULL | |
1043 | until the thread_db target claims it. */ | |
1044 | gdb_assert (ti_p->ti_tid != 0); | |
1045 | private->th = *th_p; | |
1046 | private->tid = ti_p->ti_tid; | |
1047 | ||
fb0e1ba7 | 1048 | /* Add the thread to GDB's thread list. */ |
17faa917 DJ |
1049 | if (tp == NULL) |
1050 | tp = add_thread_with_info (ptid, private); | |
1051 | else | |
1052 | tp->private = private; | |
5365276c | 1053 | |
d90e17a7 PA |
1054 | info = get_thread_db_info (GET_PID (ptid)); |
1055 | ||
fb0e1ba7 | 1056 | /* Enable thread event reporting for this thread. */ |
d90e17a7 | 1057 | err = info->td_thr_event_enable_p (th_p, 1); |
fb0e1ba7 | 1058 | if (err != TD_OK) |
8a3fe4f8 | 1059 | error (_("Cannot enable thread event reporting for %s: %s"), |
39f77062 | 1060 | target_pid_to_str (ptid), thread_db_err_str (err)); |
02c6c942 | 1061 | return 1; |
fb0e1ba7 MK |
1062 | } |
1063 | ||
1064 | static void | |
17faa917 | 1065 | detach_thread (ptid_t ptid) |
fb0e1ba7 | 1066 | { |
a2f23071 DJ |
1067 | struct thread_info *thread_info; |
1068 | ||
a2f23071 DJ |
1069 | /* Don't delete the thread now, because it still reports as active |
1070 | until it has executed a few instructions after the event | |
1071 | breakpoint - if we deleted it now, "info threads" would cause us | |
1072 | to re-attach to it. Just mark it as having had a TD_DEATH | |
1073 | event. This means that we won't delete it from our thread list | |
1074 | until we notice that it's dead (via prune_threads), or until | |
17faa917 DJ |
1075 | something re-uses its thread ID. We'll report the thread exit |
1076 | when the underlying LWP dies. */ | |
e09875d4 | 1077 | thread_info = find_thread_ptid (ptid); |
17faa917 | 1078 | gdb_assert (thread_info != NULL && thread_info->private != NULL); |
a2f23071 | 1079 | thread_info->private->dying = 1; |
fb0e1ba7 MK |
1080 | } |
1081 | ||
1082 | static void | |
136d6dae | 1083 | thread_db_detach (struct target_ops *ops, char *args, int from_tty) |
fb0e1ba7 | 1084 | { |
117de6a9 | 1085 | struct target_ops *target_beneath = find_target_beneath (ops); |
d90e17a7 | 1086 | struct thread_db_info *info; |
117de6a9 | 1087 | |
d90e17a7 | 1088 | info = get_thread_db_info (GET_PID (inferior_ptid)); |
c194fbe1 | 1089 | |
d90e17a7 PA |
1090 | if (info) |
1091 | { | |
12b6a110 | 1092 | disable_thread_event_reporting (info); |
7a7d3353 | 1093 | |
d90e17a7 PA |
1094 | /* Delete the old thread event breakpoints. Note that unlike |
1095 | when mourning, we can remove them here because there's still | |
1096 | a live inferior to poke at. In any case, GDB will not try to | |
1097 | insert anything in the inferior when removing a | |
1098 | breakpoint. */ | |
1099 | remove_thread_event_breakpoints (); | |
1100 | ||
1101 | delete_thread_db_info (GET_PID (inferior_ptid)); | |
1102 | } | |
4105de34 | 1103 | |
7a7d3353 | 1104 | target_beneath->to_detach (target_beneath, args, from_tty); |
d90e17a7 PA |
1105 | |
1106 | /* NOTE: From this point on, inferior_ptid is null_ptid. */ | |
1107 | ||
1108 | /* If there are no more processes using libpthread, detach the | |
1109 | thread_db target ops. */ | |
1110 | if (!thread_db_list) | |
1111 | unpush_target (&thread_db_ops); | |
fb0e1ba7 MK |
1112 | } |
1113 | ||
fb0e1ba7 MK |
1114 | /* Check if PID is currently stopped at the location of a thread event |
1115 | breakpoint location. If it is, read the event message and act upon | |
1116 | the event. */ | |
1117 | ||
1118 | static void | |
39f77062 | 1119 | check_event (ptid_t ptid) |
fb0e1ba7 | 1120 | { |
515630c5 UW |
1121 | struct regcache *regcache = get_thread_regcache (ptid); |
1122 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
fb0e1ba7 MK |
1123 | td_event_msg_t msg; |
1124 | td_thrinfo_t ti; | |
1125 | td_err_e err; | |
1126 | CORE_ADDR stop_pc; | |
4d9850d3 | 1127 | int loop = 0; |
d90e17a7 PA |
1128 | struct thread_db_info *info; |
1129 | ||
1130 | info = get_thread_db_info (GET_PID (ptid)); | |
fb0e1ba7 MK |
1131 | |
1132 | /* Bail out early if we're not at a thread event breakpoint. */ | |
515630c5 UW |
1133 | stop_pc = regcache_read_pc (regcache) |
1134 | - gdbarch_decr_pc_after_break (gdbarch); | |
d90e17a7 PA |
1135 | if (stop_pc != info->td_create_bp_addr |
1136 | && stop_pc != info->td_death_bp_addr) | |
fb0e1ba7 MK |
1137 | return; |
1138 | ||
4c28f408 | 1139 | /* Access an lwp we know is stopped. */ |
d90e17a7 | 1140 | info->proc_handle.ptid = ptid; |
4c28f408 PA |
1141 | |
1142 | /* If we have only looked at the first thread before libpthread was | |
1143 | initialized, we may not know its thread ID yet. Make sure we do | |
1144 | before we add another thread to the list. */ | |
d90e17a7 PA |
1145 | if (!have_threads (ptid)) |
1146 | thread_db_find_new_threads_1 (ptid); | |
4c28f408 | 1147 | |
4d9850d3 JJ |
1148 | /* If we are at a create breakpoint, we do not know what new lwp |
1149 | was created and cannot specifically locate the event message for it. | |
1150 | We have to call td_ta_event_getmsg() to get | |
1151 | the latest message. Since we have no way of correlating whether | |
cdbc0b18 | 1152 | the event message we get back corresponds to our breakpoint, we must |
4d9850d3 | 1153 | loop and read all event messages, processing them appropriately. |
cdbc0b18 RM |
1154 | This guarantees we will process the correct message before continuing |
1155 | from the breakpoint. | |
4d9850d3 JJ |
1156 | |
1157 | Currently, death events are not enabled. If they are enabled, | |
1158 | the death event can use the td_thr_event_getmsg() interface to | |
1159 | get the message specifically for that lwp and avoid looping | |
1160 | below. */ | |
1161 | ||
1162 | loop = 1; | |
1163 | ||
1164 | do | |
fb0e1ba7 | 1165 | { |
d90e17a7 | 1166 | err = info->td_ta_event_getmsg_p (info->thread_agent, &msg); |
4d9850d3 JJ |
1167 | if (err != TD_OK) |
1168 | { | |
1169 | if (err == TD_NOMSG) | |
1170 | return; | |
fb0e1ba7 | 1171 | |
8a3fe4f8 | 1172 | error (_("Cannot get thread event message: %s"), |
4d9850d3 JJ |
1173 | thread_db_err_str (err)); |
1174 | } | |
fb0e1ba7 | 1175 | |
d90e17a7 | 1176 | err = info->td_thr_get_info_p (msg.th_p, &ti); |
4d9850d3 | 1177 | if (err != TD_OK) |
8a3fe4f8 | 1178 | error (_("Cannot get thread info: %s"), thread_db_err_str (err)); |
fb0e1ba7 | 1179 | |
17faa917 | 1180 | ptid = ptid_build (GET_PID (ptid), ti.ti_lid, 0); |
fb0e1ba7 | 1181 | |
4d9850d3 JJ |
1182 | switch (msg.event) |
1183 | { | |
1184 | case TD_CREATE: | |
a2f23071 DJ |
1185 | /* Call attach_thread whether or not we already know about a |
1186 | thread with this thread ID. */ | |
93815fbf | 1187 | attach_thread (ptid, msg.th_p, &ti); |
fb0e1ba7 | 1188 | |
4d9850d3 | 1189 | break; |
fb0e1ba7 | 1190 | |
4d9850d3 | 1191 | case TD_DEATH: |
fb0e1ba7 | 1192 | |
4d9850d3 | 1193 | if (!in_thread_list (ptid)) |
8a3fe4f8 | 1194 | error (_("Spurious thread death event.")); |
fb0e1ba7 | 1195 | |
17faa917 | 1196 | detach_thread (ptid); |
fb0e1ba7 | 1197 | |
4d9850d3 | 1198 | break; |
fb0e1ba7 | 1199 | |
4d9850d3 | 1200 | default: |
8a3fe4f8 | 1201 | error (_("Spurious thread event.")); |
4d9850d3 | 1202 | } |
fb0e1ba7 | 1203 | } |
4d9850d3 | 1204 | while (loop); |
fb0e1ba7 MK |
1205 | } |
1206 | ||
39f77062 | 1207 | static ptid_t |
117de6a9 | 1208 | thread_db_wait (struct target_ops *ops, |
47608cb1 PA |
1209 | ptid_t ptid, struct target_waitstatus *ourstatus, |
1210 | int options) | |
fb0e1ba7 | 1211 | { |
d90e17a7 | 1212 | struct thread_db_info *info; |
117de6a9 PA |
1213 | struct target_ops *beneath = find_target_beneath (ops); |
1214 | ||
47608cb1 | 1215 | ptid = beneath->to_wait (beneath, ptid, ourstatus, options); |
fb0e1ba7 | 1216 | |
b84876c2 PA |
1217 | if (ourstatus->kind == TARGET_WAITKIND_IGNORE) |
1218 | return ptid; | |
1219 | ||
1111f4aa | 1220 | if (ourstatus->kind == TARGET_WAITKIND_EXITED |
fb66883a PA |
1221 | || ourstatus->kind == TARGET_WAITKIND_SIGNALLED) |
1222 | return ptid; | |
fb0e1ba7 | 1223 | |
d90e17a7 PA |
1224 | info = get_thread_db_info (GET_PID (ptid)); |
1225 | ||
1226 | /* If this process isn't using thread_db, we're done. */ | |
1227 | if (info == NULL) | |
1228 | return ptid; | |
1229 | ||
3f64f7b1 DJ |
1230 | if (ourstatus->kind == TARGET_WAITKIND_EXECD) |
1231 | { | |
d90e17a7 PA |
1232 | /* New image, it may or may not end up using thread_db. Assume |
1233 | not unless we find otherwise. */ | |
1234 | delete_thread_db_info (GET_PID (ptid)); | |
1235 | if (!thread_db_list) | |
1236 | unpush_target (&thread_db_ops); | |
3f64f7b1 | 1237 | |
6c95b8df PA |
1238 | /* Thread event breakpoints are deleted by |
1239 | update_breakpoints_after_exec. */ | |
1240 | ||
49fd4a42 | 1241 | return ptid; |
3f64f7b1 DJ |
1242 | } |
1243 | ||
4105de34 DJ |
1244 | /* If we do not know about the main thread yet, this would be a good time to |
1245 | find it. */ | |
d90e17a7 PA |
1246 | if (ourstatus->kind == TARGET_WAITKIND_STOPPED && !have_threads (ptid)) |
1247 | thread_db_find_new_threads_1 (ptid); | |
4105de34 | 1248 | |
fb0e1ba7 MK |
1249 | if (ourstatus->kind == TARGET_WAITKIND_STOPPED |
1250 | && ourstatus->value.sig == TARGET_SIGNAL_TRAP) | |
1251 | /* Check for a thread event. */ | |
39f77062 | 1252 | check_event (ptid); |
fb0e1ba7 | 1253 | |
d90e17a7 | 1254 | if (have_threads (ptid)) |
4105de34 DJ |
1255 | { |
1256 | /* Change ptids back into the higher level PID + TID format. If | |
1257 | the thread is dead and no longer on the thread list, we will | |
1258 | get back a dead ptid. This can occur if the thread death | |
1259 | event gets postponed by other simultaneous events. In such a | |
1260 | case, we want to just ignore the event and continue on. */ | |
1261 | ||
4105de34 DJ |
1262 | ptid = thread_from_lwp (ptid); |
1263 | if (GET_PID (ptid) == -1) | |
1264 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
1265 | } | |
fb0e1ba7 | 1266 | |
b9b5d7ea | 1267 | return ptid; |
fb0e1ba7 MK |
1268 | } |
1269 | ||
fb0e1ba7 | 1270 | static void |
136d6dae | 1271 | thread_db_mourn_inferior (struct target_ops *ops) |
fb0e1ba7 | 1272 | { |
117de6a9 PA |
1273 | struct target_ops *target_beneath = find_target_beneath (ops); |
1274 | ||
d90e17a7 | 1275 | delete_thread_db_info (GET_PID (inferior_ptid)); |
fb0e1ba7 | 1276 | |
d90e17a7 PA |
1277 | target_beneath->to_mourn_inferior (target_beneath); |
1278 | ||
6c95b8df PA |
1279 | /* Delete the old thread event breakpoints. Do this after mourning |
1280 | the inferior, so that we don't try to uninsert them. */ | |
1281 | remove_thread_event_breakpoints (); | |
1282 | ||
b26a6851 | 1283 | /* Detach thread_db target ops. */ |
d90e17a7 PA |
1284 | if (!thread_db_list) |
1285 | unpush_target (ops); | |
fb0e1ba7 MK |
1286 | } |
1287 | ||
02c6c942 PP |
1288 | struct callback_data |
1289 | { | |
1290 | struct thread_db_info *info; | |
1291 | int new_threads; | |
1292 | }; | |
1293 | ||
fb0e1ba7 MK |
1294 | static int |
1295 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) | |
1296 | { | |
1297 | td_thrinfo_t ti; | |
1298 | td_err_e err; | |
39f77062 | 1299 | ptid_t ptid; |
403fe197 | 1300 | struct thread_info *tp; |
02c6c942 PP |
1301 | struct callback_data *cb_data = data; |
1302 | struct thread_db_info *info = cb_data->info; | |
fb0e1ba7 | 1303 | |
d90e17a7 | 1304 | err = info->td_thr_get_info_p (th_p, &ti); |
fb0e1ba7 | 1305 | if (err != TD_OK) |
8a3fe4f8 | 1306 | error (_("find_new_threads_callback: cannot get thread info: %s"), |
3197744f | 1307 | thread_db_err_str (err)); |
fb0e1ba7 | 1308 | |
21bf60fe MK |
1309 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) |
1310 | return 0; /* A zombie -- ignore. */ | |
5fd913cc | 1311 | |
4105de34 DJ |
1312 | if (ti.ti_tid == 0) |
1313 | { | |
1314 | /* A thread ID of zero means that this is the main thread, but | |
1315 | glibc has not yet initialized thread-local storage and the | |
1316 | pthread library. We do not know what the thread's TID will | |
1317 | be yet. Just enable event reporting and otherwise ignore | |
1318 | it. */ | |
1319 | ||
4d062f1a PA |
1320 | /* In that case, we're not stopped in a fork syscall and don't |
1321 | need this glibc bug workaround. */ | |
1322 | info->need_stale_parent_threads_check = 0; | |
1323 | ||
d90e17a7 | 1324 | err = info->td_thr_event_enable_p (th_p, 1); |
4105de34 | 1325 | if (err != TD_OK) |
4d062f1a PA |
1326 | error (_("Cannot enable thread event reporting for LWP %d: %s"), |
1327 | (int) ti.ti_lid, thread_db_err_str (err)); | |
4105de34 DJ |
1328 | |
1329 | return 0; | |
1330 | } | |
1331 | ||
4d062f1a PA |
1332 | /* Ignore stale parent threads, caused by glibc/BZ5983. This is a |
1333 | bit expensive, as it needs to open /proc/pid/status, so try to | |
1334 | avoid doing the work if we know we don't have to. */ | |
1335 | if (info->need_stale_parent_threads_check) | |
1336 | { | |
1337 | int tgid = linux_proc_get_tgid (ti.ti_lid); | |
1338 | if (tgid != -1 && tgid != info->pid) | |
1339 | return 0; | |
1340 | } | |
1341 | ||
1342 | ptid = ptid_build (info->pid, ti.ti_lid, 0); | |
e09875d4 | 1343 | tp = find_thread_ptid (ptid); |
403fe197 | 1344 | if (tp == NULL || tp->private == NULL) |
02c6c942 PP |
1345 | { |
1346 | if (attach_thread (ptid, th_p, &ti)) | |
1347 | cb_data->new_threads += 1; | |
1348 | else | |
1349 | /* Problem attaching this thread; perhaps it exited before we | |
1350 | could attach it? | |
1351 | This could mean that the thread list inside glibc itself is in | |
1352 | inconsistent state, and libthread_db could go on looping forever | |
1353 | (observed with glibc-2.3.6). To prevent that, terminate | |
1354 | iteration: thread_db_find_new_threads_2 will retry. */ | |
1355 | return 1; | |
1356 | } | |
fb0e1ba7 MK |
1357 | |
1358 | return 0; | |
1359 | } | |
1360 | ||
02c6c942 PP |
1361 | /* Helper for thread_db_find_new_threads_2. |
1362 | Returns number of new threads found. */ | |
1363 | ||
1364 | static int | |
1365 | find_new_threads_once (struct thread_db_info *info, int iteration, | |
1366 | int *errp) | |
1367 | { | |
1368 | volatile struct gdb_exception except; | |
1369 | struct callback_data data; | |
1370 | int err = TD_ERR; | |
1371 | ||
1372 | data.info = info; | |
1373 | data.new_threads = 0; | |
1374 | ||
1375 | TRY_CATCH (except, RETURN_MASK_ERROR) | |
1376 | { | |
1377 | /* Iterate over all user-space threads to discover new threads. */ | |
1378 | err = info->td_ta_thr_iter_p (info->thread_agent, | |
1379 | find_new_threads_callback, | |
1380 | &data, | |
1381 | TD_THR_ANY_STATE, | |
1382 | TD_THR_LOWEST_PRIORITY, | |
1383 | TD_SIGNO_MASK, | |
1384 | TD_THR_ANY_USER_FLAGS); | |
1385 | } | |
1386 | ||
1387 | if (info_verbose) | |
1388 | { | |
1389 | if (except.reason < 0) | |
1390 | exception_fprintf (gdb_stderr, except, | |
1391 | "Warning: find_new_threads_once: "); | |
1392 | ||
1393 | printf_filtered (_("Found %d new threads in iteration %d.\n"), | |
1394 | data.new_threads, iteration); | |
1395 | } | |
1396 | ||
1397 | if (errp != NULL) | |
1398 | *errp = err; | |
1399 | ||
1400 | return data.new_threads; | |
1401 | } | |
1402 | ||
4c28f408 | 1403 | /* Search for new threads, accessing memory through stopped thread |
02c6c942 PP |
1404 | PTID. If UNTIL_NO_NEW is true, repeat searching until several |
1405 | searches in a row do not discover any new threads. */ | |
4c28f408 | 1406 | |
fb0e1ba7 | 1407 | static void |
02c6c942 | 1408 | thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new) |
fb0e1ba7 MK |
1409 | { |
1410 | td_err_e err; | |
4c28f408 | 1411 | struct lwp_info *lp; |
d90e17a7 PA |
1412 | struct thread_db_info *info; |
1413 | int pid = ptid_get_pid (ptid); | |
02c6c942 | 1414 | int i, loop; |
4c28f408 PA |
1415 | |
1416 | /* In linux, we can only read memory through a stopped lwp. */ | |
1417 | ALL_LWPS (lp, ptid) | |
d90e17a7 | 1418 | if (lp->stopped && ptid_get_pid (lp->ptid) == pid) |
4c28f408 PA |
1419 | break; |
1420 | ||
1421 | if (!lp) | |
1422 | /* There is no stopped thread. Bail out. */ | |
1423 | return; | |
fb0e1ba7 | 1424 | |
d90e17a7 PA |
1425 | info = get_thread_db_info (GET_PID (ptid)); |
1426 | ||
4c28f408 | 1427 | /* Access an lwp we know is stopped. */ |
d90e17a7 | 1428 | info->proc_handle.ptid = ptid; |
02c6c942 PP |
1429 | |
1430 | if (until_no_new) | |
1431 | { | |
1432 | /* Require 4 successive iterations which do not find any new threads. | |
1433 | The 4 is a heuristic: there is an inherent race here, and I have | |
1434 | seen that 2 iterations in a row are not always sufficient to | |
1435 | "capture" all threads. */ | |
1436 | for (i = 0, loop = 0; loop < 4; ++i, ++loop) | |
1437 | if (find_new_threads_once (info, i, NULL) != 0) | |
1438 | /* Found some new threads. Restart the loop from beginning. */ | |
1439 | loop = -1; | |
1440 | } | |
1441 | else | |
1442 | { | |
1443 | int err; | |
1444 | ||
1445 | find_new_threads_once (info, 0, &err); | |
1446 | if (err != TD_OK) | |
1447 | error (_("Cannot find new threads: %s"), thread_db_err_str (err)); | |
1448 | } | |
fb0e1ba7 MK |
1449 | } |
1450 | ||
02c6c942 PP |
1451 | static void |
1452 | thread_db_find_new_threads_1 (ptid_t ptid) | |
1453 | { | |
1454 | thread_db_find_new_threads_2 (ptid, 0); | |
1455 | } | |
1456 | ||
1457 | ||
28439f5e PA |
1458 | static void |
1459 | thread_db_find_new_threads (struct target_ops *ops) | |
1460 | { | |
d90e17a7 PA |
1461 | struct thread_db_info *info; |
1462 | ||
1463 | info = get_thread_db_info (GET_PID (inferior_ptid)); | |
1464 | ||
1465 | if (info == NULL) | |
1466 | return; | |
1467 | ||
1468 | thread_db_find_new_threads_1 (inferior_ptid); | |
28439f5e PA |
1469 | } |
1470 | ||
fb0e1ba7 | 1471 | static char * |
117de6a9 | 1472 | thread_db_pid_to_str (struct target_ops *ops, ptid_t ptid) |
fb0e1ba7 | 1473 | { |
e09875d4 | 1474 | struct thread_info *thread_info = find_thread_ptid (ptid); |
117de6a9 | 1475 | struct target_ops *beneath; |
17faa917 DJ |
1476 | |
1477 | if (thread_info != NULL && thread_info->private != NULL) | |
fb0e1ba7 MK |
1478 | { |
1479 | static char buf[64]; | |
17faa917 | 1480 | thread_t tid; |
fb0e1ba7 | 1481 | |
17faa917 | 1482 | tid = thread_info->private->tid; |
17faa917 DJ |
1483 | snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld)", |
1484 | tid, GET_LWP (ptid)); | |
fb0e1ba7 MK |
1485 | |
1486 | return buf; | |
1487 | } | |
1488 | ||
117de6a9 PA |
1489 | beneath = find_target_beneath (ops); |
1490 | if (beneath->to_pid_to_str (beneath, ptid)) | |
1491 | return beneath->to_pid_to_str (beneath, ptid); | |
fb0e1ba7 | 1492 | |
39f77062 | 1493 | return normal_pid_to_str (ptid); |
fb0e1ba7 MK |
1494 | } |
1495 | ||
28b17333 DJ |
1496 | /* Return a string describing the state of the thread specified by |
1497 | INFO. */ | |
1498 | ||
1499 | static char * | |
1500 | thread_db_extra_thread_info (struct thread_info *info) | |
1501 | { | |
17faa917 DJ |
1502 | if (info->private == NULL) |
1503 | return NULL; | |
1504 | ||
28b17333 DJ |
1505 | if (info->private->dying) |
1506 | return "Exiting"; | |
1507 | ||
1508 | return NULL; | |
1509 | } | |
1510 | ||
b2756930 KB |
1511 | /* Get the address of the thread local variable in load module LM which |
1512 | is stored at OFFSET within the thread local storage for thread PTID. */ | |
3f47be5c EZ |
1513 | |
1514 | static CORE_ADDR | |
117de6a9 PA |
1515 | thread_db_get_thread_local_address (struct target_ops *ops, |
1516 | ptid_t ptid, | |
b2756930 | 1517 | CORE_ADDR lm, |
b4acd559 | 1518 | CORE_ADDR offset) |
3f47be5c | 1519 | { |
17faa917 | 1520 | struct thread_info *thread_info; |
117de6a9 | 1521 | struct target_ops *beneath; |
17faa917 | 1522 | |
4105de34 | 1523 | /* If we have not discovered any threads yet, check now. */ |
d90e17a7 PA |
1524 | if (!have_threads (ptid)) |
1525 | thread_db_find_new_threads_1 (ptid); | |
4105de34 | 1526 | |
17faa917 | 1527 | /* Find the matching thread. */ |
e09875d4 | 1528 | thread_info = find_thread_ptid (ptid); |
4105de34 | 1529 | |
17faa917 | 1530 | if (thread_info != NULL && thread_info->private != NULL) |
3f47be5c | 1531 | { |
3f47be5c | 1532 | td_err_e err; |
3f47be5c | 1533 | void *address; |
d90e17a7 PA |
1534 | struct thread_db_info *info; |
1535 | ||
1536 | info = get_thread_db_info (GET_PID (ptid)); | |
3f47be5c EZ |
1537 | |
1538 | /* glibc doesn't provide the needed interface. */ | |
d90e17a7 | 1539 | if (!info->td_thr_tls_get_addr_p) |
109c3e39 AC |
1540 | throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR, |
1541 | _("No TLS library support")); | |
3f47be5c | 1542 | |
b2756930 KB |
1543 | /* Caller should have verified that lm != 0. */ |
1544 | gdb_assert (lm != 0); | |
3f47be5c | 1545 | |
3f47be5c | 1546 | /* Finally, get the address of the variable. */ |
d90e17a7 PA |
1547 | err = info->td_thr_tls_get_addr_p (&thread_info->private->th, |
1548 | (void *)(size_t) lm, | |
1549 | offset, &address); | |
3f47be5c EZ |
1550 | |
1551 | #ifdef THREAD_DB_HAS_TD_NOTALLOC | |
1552 | /* The memory hasn't been allocated, yet. */ | |
1553 | if (err == TD_NOTALLOC) | |
b4acd559 JJ |
1554 | /* Now, if libthread_db provided the initialization image's |
1555 | address, we *could* try to build a non-lvalue value from | |
1556 | the initialization image. */ | |
109c3e39 AC |
1557 | throw_error (TLS_NOT_ALLOCATED_YET_ERROR, |
1558 | _("TLS not allocated yet")); | |
3f47be5c EZ |
1559 | #endif |
1560 | ||
1561 | /* Something else went wrong. */ | |
1562 | if (err != TD_OK) | |
109c3e39 AC |
1563 | throw_error (TLS_GENERIC_ERROR, |
1564 | (("%s")), thread_db_err_str (err)); | |
3f47be5c EZ |
1565 | |
1566 | /* Cast assuming host == target. Joy. */ | |
16451949 AS |
1567 | /* Do proper sign extension for the target. */ |
1568 | gdb_assert (exec_bfd); | |
1569 | return (bfd_get_sign_extend_vma (exec_bfd) > 0 | |
1570 | ? (CORE_ADDR) (intptr_t) address | |
1571 | : (CORE_ADDR) (uintptr_t) address); | |
3f47be5c EZ |
1572 | } |
1573 | ||
117de6a9 PA |
1574 | beneath = find_target_beneath (ops); |
1575 | if (beneath->to_get_thread_local_address) | |
1576 | return beneath->to_get_thread_local_address (beneath, ptid, lm, offset); | |
93ad78a7 | 1577 | else |
109c3e39 AC |
1578 | throw_error (TLS_GENERIC_ERROR, |
1579 | _("TLS not supported on this target")); | |
3f47be5c EZ |
1580 | } |
1581 | ||
0ef643c8 JB |
1582 | /* Callback routine used to find a thread based on the TID part of |
1583 | its PTID. */ | |
1584 | ||
1585 | static int | |
1586 | thread_db_find_thread_from_tid (struct thread_info *thread, void *data) | |
1587 | { | |
1588 | long *tid = (long *) data; | |
1589 | ||
1590 | if (thread->private->tid == *tid) | |
1591 | return 1; | |
1592 | ||
1593 | return 0; | |
1594 | } | |
1595 | ||
1596 | /* Implement the to_get_ada_task_ptid target method for this target. */ | |
1597 | ||
1598 | static ptid_t | |
1599 | thread_db_get_ada_task_ptid (long lwp, long thread) | |
1600 | { | |
1601 | struct thread_info *thread_info; | |
1602 | ||
d90e17a7 | 1603 | thread_db_find_new_threads_1 (inferior_ptid); |
0ef643c8 JB |
1604 | thread_info = iterate_over_threads (thread_db_find_thread_from_tid, &thread); |
1605 | ||
1606 | gdb_assert (thread_info != NULL); | |
1607 | ||
1608 | return (thread_info->ptid); | |
1609 | } | |
1610 | ||
4d062f1a PA |
1611 | static void |
1612 | thread_db_resume (struct target_ops *ops, | |
1613 | ptid_t ptid, int step, enum target_signal signo) | |
1614 | { | |
1615 | struct target_ops *beneath = find_target_beneath (ops); | |
1616 | struct thread_db_info *info; | |
1617 | ||
1618 | if (ptid_equal (ptid, minus_one_ptid)) | |
1619 | info = get_thread_db_info (GET_PID (inferior_ptid)); | |
1620 | else | |
1621 | info = get_thread_db_info (GET_PID (ptid)); | |
1622 | ||
1623 | /* This workaround is only needed for child fork lwps stopped in a | |
1624 | PTRACE_O_TRACEFORK event. When the inferior is resumed, the | |
1625 | workaround can be disabled. */ | |
1626 | if (info) | |
1627 | info->need_stale_parent_threads_check = 0; | |
1628 | ||
1629 | beneath->to_resume (beneath, ptid, step, signo); | |
1630 | } | |
1631 | ||
fb0e1ba7 MK |
1632 | static void |
1633 | init_thread_db_ops (void) | |
1634 | { | |
1635 | thread_db_ops.to_shortname = "multi-thread"; | |
1636 | thread_db_ops.to_longname = "multi-threaded child process."; | |
1637 | thread_db_ops.to_doc = "Threads and pthreads support."; | |
1638 | thread_db_ops.to_detach = thread_db_detach; | |
fb0e1ba7 | 1639 | thread_db_ops.to_wait = thread_db_wait; |
4d062f1a | 1640 | thread_db_ops.to_resume = thread_db_resume; |
fb0e1ba7 | 1641 | thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior; |
fb0e1ba7 MK |
1642 | thread_db_ops.to_find_new_threads = thread_db_find_new_threads; |
1643 | thread_db_ops.to_pid_to_str = thread_db_pid_to_str; | |
1644 | thread_db_ops.to_stratum = thread_stratum; | |
1645 | thread_db_ops.to_has_thread_control = tc_schedlock; | |
3f47be5c EZ |
1646 | thread_db_ops.to_get_thread_local_address |
1647 | = thread_db_get_thread_local_address; | |
28b17333 | 1648 | thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info; |
0ef643c8 | 1649 | thread_db_ops.to_get_ada_task_ptid = thread_db_get_ada_task_ptid; |
fb0e1ba7 MK |
1650 | thread_db_ops.to_magic = OPS_MAGIC; |
1651 | } | |
1652 | ||
2c0b251b PA |
1653 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1654 | extern initialize_file_ftype _initialize_thread_db; | |
1655 | ||
fb0e1ba7 MK |
1656 | void |
1657 | _initialize_thread_db (void) | |
1658 | { | |
17a37d48 PP |
1659 | init_thread_db_ops (); |
1660 | add_target (&thread_db_ops); | |
1661 | ||
1662 | /* Defer loading of libthread_db.so until inferior is running. | |
1663 | This allows gdb to load correct libthread_db for a given | |
1664 | executable -- there could be mutiple versions of glibc, | |
1665 | compiled with LinuxThreads or NPTL, and until there is | |
1666 | a running inferior, we can't tell which libthread_db is | |
1667 | the correct one to load. */ | |
1668 | ||
1669 | libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH); | |
1670 | ||
1671 | add_setshow_optional_filename_cmd ("libthread-db-search-path", | |
1672 | class_support, | |
1673 | &libthread_db_search_path, _("\ | |
1674 | Set search path for libthread_db."), _("\ | |
1675 | Show the current search path or libthread_db."), _("\ | |
1676 | This path is used to search for libthread_db to be loaded into \ | |
1677 | gdb itself."), | |
1678 | NULL, | |
1679 | NULL, | |
1680 | &setlist, &showlist); | |
1681 | /* Add ourselves to objfile event chain. */ | |
1682 | observer_attach_new_objfile (thread_db_new_objfile); | |
fb0e1ba7 | 1683 | } |