Commit | Line | Data |
---|---|---|
0d62e5e8 | 1 | /* Thread management interface, for the remote server for GDB. |
618f726f | 2 | Copyright (C) 2002-2016 Free Software Foundation, Inc. |
0d62e5e8 DJ |
3 | |
4 | Contributed by MontaVista Software. | |
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 |
0d62e5e8 DJ |
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/>. */ |
0d62e5e8 DJ |
20 | |
21 | #include "server.h" | |
22 | ||
23 | #include "linux-low.h" | |
24 | ||
25 | extern int debug_threads; | |
26 | ||
0050a760 | 27 | #include "gdb_proc_service.h" |
125f8a3d | 28 | #include "nat/gdb_thread_db.h" |
e6712ff1 | 29 | #include "gdb_vecs.h" |
2db9a427 | 30 | #include "nat/linux-procfs.h" |
0d62e5e8 | 31 | |
96f15937 | 32 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
cdbfd419 | 33 | #include <dlfcn.h> |
96f15937 | 34 | #endif |
cdbfd419 PP |
35 | #include <limits.h> |
36 | #include <ctype.h> | |
37 | ||
38 | struct thread_db | |
39 | { | |
40 | /* Structure that identifies the child process for the | |
41 | <proc_service.h> interface. */ | |
42 | struct ps_prochandle proc_handle; | |
43 | ||
44 | /* Connection to the libthread_db library. */ | |
45 | td_thragent_t *thread_agent; | |
46 | ||
9836d6ea PA |
47 | /* If this flag has been set, we've already asked GDB for all |
48 | symbols we might need; assume symbol cache misses are | |
49 | failures. */ | |
50 | int all_symbols_looked_up; | |
51 | ||
96f15937 | 52 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
cdbfd419 PP |
53 | /* Handle of the libthread_db from dlopen. */ |
54 | void *handle; | |
96f15937 | 55 | #endif |
cdbfd419 PP |
56 | |
57 | /* Addresses of libthread_db functions. */ | |
96e9210f | 58 | td_ta_new_ftype *td_ta_new_p; |
96e9210f PA |
59 | td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p; |
60 | td_thr_get_info_ftype *td_thr_get_info_p; | |
96e9210f PA |
61 | td_ta_thr_iter_ftype *td_ta_thr_iter_p; |
62 | td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p; | |
63 | td_thr_tlsbase_ftype *td_thr_tlsbase_p; | |
64 | td_symbol_list_ftype *td_symbol_list_p; | |
cdbfd419 PP |
65 | }; |
66 | ||
67 | static char *libthread_db_search_path; | |
186947f7 | 68 | |
95954743 | 69 | static int find_one_thread (ptid_t); |
0d62e5e8 DJ |
70 | static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data); |
71 | ||
54363045 | 72 | static const char * |
0d62e5e8 DJ |
73 | thread_db_err_str (td_err_e err) |
74 | { | |
75 | static char buf[64]; | |
76 | ||
77 | switch (err) | |
78 | { | |
79 | case TD_OK: | |
80 | return "generic 'call succeeded'"; | |
81 | case TD_ERR: | |
82 | return "generic error"; | |
83 | case TD_NOTHR: | |
84 | return "no thread to satisfy query"; | |
85 | case TD_NOSV: | |
86 | return "no sync handle to satisfy query"; | |
87 | case TD_NOLWP: | |
88 | return "no LWP to satisfy query"; | |
89 | case TD_BADPH: | |
90 | return "invalid process handle"; | |
91 | case TD_BADTH: | |
92 | return "invalid thread handle"; | |
93 | case TD_BADSH: | |
94 | return "invalid synchronization handle"; | |
95 | case TD_BADTA: | |
96 | return "invalid thread agent"; | |
97 | case TD_BADKEY: | |
98 | return "invalid key"; | |
99 | case TD_NOMSG: | |
100 | return "no event message for getmsg"; | |
101 | case TD_NOFPREGS: | |
102 | return "FPU register set not available"; | |
103 | case TD_NOLIBTHREAD: | |
104 | return "application not linked with libthread"; | |
105 | case TD_NOEVENT: | |
106 | return "requested event is not supported"; | |
107 | case TD_NOCAPAB: | |
108 | return "capability not available"; | |
109 | case TD_DBERR: | |
110 | return "debugger service failed"; | |
111 | case TD_NOAPLIC: | |
112 | return "operation not applicable to"; | |
113 | case TD_NOTSD: | |
114 | return "no thread-specific data for this thread"; | |
115 | case TD_MALLOC: | |
116 | return "malloc failed"; | |
117 | case TD_PARTIALREG: | |
118 | return "only part of register set was written/read"; | |
119 | case TD_NOXREGS: | |
120 | return "X register set not available for this thread"; | |
3db0444b DJ |
121 | #ifdef HAVE_TD_VERSION |
122 | case TD_VERSION: | |
123 | return "version mismatch between libthread_db and libpthread"; | |
124 | #endif | |
0d62e5e8 | 125 | default: |
6cebaf6e | 126 | xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err); |
0d62e5e8 DJ |
127 | return buf; |
128 | } | |
129 | } | |
130 | ||
131 | #if 0 | |
132 | static char * | |
133 | thread_db_state_str (td_thr_state_e state) | |
134 | { | |
135 | static char buf[64]; | |
136 | ||
137 | switch (state) | |
138 | { | |
139 | case TD_THR_STOPPED: | |
140 | return "stopped by debugger"; | |
141 | case TD_THR_RUN: | |
142 | return "runnable"; | |
143 | case TD_THR_ACTIVE: | |
144 | return "active"; | |
145 | case TD_THR_ZOMBIE: | |
146 | return "zombie"; | |
147 | case TD_THR_SLEEP: | |
148 | return "sleeping"; | |
149 | case TD_THR_STOPPED_ASLEEP: | |
150 | return "stopped by debugger AND blocked"; | |
151 | default: | |
6cebaf6e | 152 | xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state); |
0d62e5e8 DJ |
153 | return buf; |
154 | } | |
155 | } | |
156 | #endif | |
157 | ||
ae13219e | 158 | static int |
95954743 | 159 | find_one_thread (ptid_t ptid) |
0d62e5e8 | 160 | { |
ae13219e DJ |
161 | td_thrhandle_t th; |
162 | td_thrinfo_t ti; | |
0d62e5e8 DJ |
163 | td_err_e err; |
164 | struct thread_info *inferior; | |
54a0b537 | 165 | struct lwp_info *lwp; |
fe978cb0 | 166 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
95954743 | 167 | int lwpid = ptid_get_lwp (ptid); |
0d62e5e8 | 168 | |
95954743 | 169 | inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid); |
54a0b537 PA |
170 | lwp = get_thread_lwp (inferior); |
171 | if (lwp->thread_known) | |
ae13219e DJ |
172 | return 1; |
173 | ||
24a09b5f | 174 | /* Get information about this thread. */ |
cdbfd419 | 175 | err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th); |
ae13219e | 176 | if (err != TD_OK) |
24a09b5f DJ |
177 | error ("Cannot get thread handle for LWP %d: %s", |
178 | lwpid, thread_db_err_str (err)); | |
ae13219e | 179 | |
cdbfd419 | 180 | err = thread_db->td_thr_get_info_p (&th, &ti); |
ae13219e | 181 | if (err != TD_OK) |
24a09b5f DJ |
182 | error ("Cannot get thread info for LWP %d: %s", |
183 | lwpid, thread_db_err_str (err)); | |
ae13219e DJ |
184 | |
185 | if (debug_threads) | |
87ce2a04 | 186 | debug_printf ("Found thread %ld (LWP %d)\n", |
d41401ac | 187 | (unsigned long) ti.ti_tid, ti.ti_lid); |
ae13219e | 188 | |
95954743 | 189 | if (lwpid != ti.ti_lid) |
24a09b5f DJ |
190 | { |
191 | warning ("PID mismatch! Expected %ld, got %ld", | |
95954743 | 192 | (long) lwpid, (long) ti.ti_lid); |
24a09b5f DJ |
193 | return 0; |
194 | } | |
ae13219e | 195 | |
24a09b5f DJ |
196 | /* If the new thread ID is zero, a final thread ID will be available |
197 | later. Do not enable thread debugging yet. */ | |
198 | if (ti.ti_tid == 0) | |
199 | return 0; | |
ae13219e | 200 | |
54a0b537 PA |
201 | lwp->thread_known = 1; |
202 | lwp->th = th; | |
ae13219e | 203 | |
ae13219e DJ |
204 | return 1; |
205 | } | |
206 | ||
5f7d1694 PP |
207 | /* Attach a thread. Return true on success. */ |
208 | ||
209 | static int | |
210 | attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p) | |
ae13219e | 211 | { |
7ae1a6a6 PA |
212 | struct process_info *proc = current_process (); |
213 | int pid = pid_of (proc); | |
214 | ptid_t ptid = ptid_build (pid, ti_p->ti_lid, 0); | |
54a0b537 | 215 | struct lwp_info *lwp; |
7ae1a6a6 | 216 | int err; |
ae13219e | 217 | |
0d62e5e8 | 218 | if (debug_threads) |
87ce2a04 | 219 | debug_printf ("Attaching to thread %ld (LWP %d)\n", |
d41401ac | 220 | (unsigned long) ti_p->ti_tid, ti_p->ti_lid); |
7ae1a6a6 PA |
221 | err = linux_attach_lwp (ptid); |
222 | if (err != 0) | |
0d62e5e8 | 223 | { |
7ae1a6a6 | 224 | warning ("Could not attach to thread %ld (LWP %d): %s\n", |
d41401ac | 225 | (unsigned long) ti_p->ti_tid, ti_p->ti_lid, |
8784d563 | 226 | linux_ptrace_attach_fail_reason_string (ptid, err)); |
5f7d1694 | 227 | return 0; |
0d62e5e8 DJ |
228 | } |
229 | ||
7ae1a6a6 PA |
230 | lwp = find_lwp_pid (ptid); |
231 | gdb_assert (lwp != NULL); | |
54a0b537 PA |
232 | lwp->thread_known = 1; |
233 | lwp->th = *th_p; | |
24a09b5f | 234 | |
5f7d1694 PP |
235 | return 1; |
236 | } | |
237 | ||
238 | /* Attach thread if we haven't seen it yet. | |
239 | Increment *COUNTER if we have attached a new thread. | |
240 | Return false on failure. */ | |
241 | ||
242 | static int | |
243 | maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p, | |
244 | int *counter) | |
245 | { | |
246 | struct lwp_info *lwp; | |
247 | ||
248 | lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid)); | |
249 | if (lwp != NULL) | |
250 | return 1; | |
251 | ||
252 | if (!attach_thread (th_p, ti_p)) | |
253 | return 0; | |
254 | ||
255 | if (counter != NULL) | |
256 | *counter += 1; | |
257 | ||
258 | return 1; | |
0d62e5e8 DJ |
259 | } |
260 | ||
261 | static int | |
262 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) | |
263 | { | |
264 | td_thrinfo_t ti; | |
265 | td_err_e err; | |
fe978cb0 | 266 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
0d62e5e8 | 267 | |
cdbfd419 | 268 | err = thread_db->td_thr_get_info_p (th_p, &ti); |
0d62e5e8 DJ |
269 | if (err != TD_OK) |
270 | error ("Cannot get thread info: %s", thread_db_err_str (err)); | |
271 | ||
a33e3959 PA |
272 | if (ti.ti_lid == -1) |
273 | { | |
274 | /* A thread with kernel thread ID -1 is either a thread that | |
275 | exited and was joined, or a thread that is being created but | |
276 | hasn't started yet, and that is reusing the tcb/stack of a | |
277 | thread that previously exited and was joined. (glibc marks | |
278 | terminated and joined threads with kernel thread ID -1. See | |
279 | glibc PR17707. */ | |
d6c146e9 PA |
280 | if (debug_threads) |
281 | debug_printf ("thread_db: skipping exited and " | |
d41401ac DE |
282 | "joined thread (0x%lx)\n", |
283 | (unsigned long) ti.ti_tid); | |
a33e3959 PA |
284 | return 0; |
285 | } | |
286 | ||
0d62e5e8 DJ |
287 | /* Check for zombies. */ |
288 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) | |
289 | return 0; | |
290 | ||
5f7d1694 PP |
291 | if (!maybe_attach_thread (th_p, &ti, (int *) data)) |
292 | { | |
293 | /* Terminate iteration early: we might be looking at stale data in | |
294 | the inferior. The thread_db_find_new_threads will retry. */ | |
295 | return 1; | |
296 | } | |
0d62e5e8 DJ |
297 | |
298 | return 0; | |
299 | } | |
300 | ||
301 | static void | |
302 | thread_db_find_new_threads (void) | |
303 | { | |
304 | td_err_e err; | |
fbd5db48 | 305 | ptid_t ptid = current_ptid; |
fe978cb0 | 306 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
5f7d1694 | 307 | int loop, iteration; |
0d62e5e8 | 308 | |
ae13219e DJ |
309 | /* This function is only called when we first initialize thread_db. |
310 | First locate the initial thread. If it is not ready for | |
311 | debugging yet, then stop. */ | |
95954743 | 312 | if (find_one_thread (ptid) == 0) |
ae13219e DJ |
313 | return; |
314 | ||
5f7d1694 PP |
315 | /* Require 4 successive iterations which do not find any new threads. |
316 | The 4 is a heuristic: there is an inherent race here, and I have | |
317 | seen that 2 iterations in a row are not always sufficient to | |
318 | "capture" all threads. */ | |
319 | for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration) | |
320 | { | |
321 | int new_thread_count = 0; | |
322 | ||
323 | /* Iterate over all user-space threads to discover new threads. */ | |
324 | err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent, | |
325 | find_new_threads_callback, | |
326 | &new_thread_count, | |
493e2a69 MS |
327 | TD_THR_ANY_STATE, |
328 | TD_THR_LOWEST_PRIORITY, | |
5f7d1694 PP |
329 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); |
330 | if (debug_threads) | |
87ce2a04 DE |
331 | debug_printf ("Found %d threads in iteration %d.\n", |
332 | new_thread_count, iteration); | |
5f7d1694 PP |
333 | |
334 | if (new_thread_count != 0) | |
335 | { | |
336 | /* Found new threads. Restart iteration from beginning. */ | |
337 | loop = -1; | |
338 | } | |
339 | } | |
0d62e5e8 DJ |
340 | if (err != TD_OK) |
341 | error ("Cannot find new threads: %s", thread_db_err_str (err)); | |
342 | } | |
343 | ||
fd500816 DJ |
344 | /* Cache all future symbols that thread_db might request. We can not |
345 | request symbols at arbitrary states in the remote protocol, only | |
346 | when the client tells us that new symbols are available. So when | |
347 | we load the thread library, make sure to check the entire list. */ | |
348 | ||
349 | static void | |
350 | thread_db_look_up_symbols (void) | |
351 | { | |
fe978cb0 | 352 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
cdbfd419 | 353 | const char **sym_list; |
fd500816 DJ |
354 | CORE_ADDR unused; |
355 | ||
cdbfd419 | 356 | for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++) |
9836d6ea PA |
357 | look_up_one_symbol (*sym_list, &unused, 1); |
358 | ||
359 | /* We're not interested in any other libraries loaded after this | |
360 | point, only in symbols in libpthread.so. */ | |
361 | thread_db->all_symbols_looked_up = 1; | |
362 | } | |
363 | ||
364 | int | |
365 | thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp) | |
366 | { | |
fe978cb0 | 367 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
9836d6ea PA |
368 | int may_ask_gdb = !thread_db->all_symbols_looked_up; |
369 | ||
370 | /* If we've passed the call to thread_db_look_up_symbols, then | |
371 | anything not in the cache must not exist; we're not interested | |
372 | in any libraries loaded after that point, only in symbols in | |
373 | libpthread.so. It might not be an appropriate time to look | |
374 | up a symbol, e.g. while we're trying to fetch registers. */ | |
375 | return look_up_one_symbol (name, addrp, may_ask_gdb); | |
fd500816 DJ |
376 | } |
377 | ||
dae5f5cf DJ |
378 | int |
379 | thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset, | |
380 | CORE_ADDR load_module, CORE_ADDR *address) | |
381 | { | |
dae5f5cf DJ |
382 | psaddr_t addr; |
383 | td_err_e err; | |
54a0b537 | 384 | struct lwp_info *lwp; |
0bfdf32f | 385 | struct thread_info *saved_thread; |
cdbfd419 PP |
386 | struct process_info *proc; |
387 | struct thread_db *thread_db; | |
388 | ||
389 | proc = get_thread_process (thread); | |
fe978cb0 | 390 | thread_db = proc->priv->thread_db; |
dae5f5cf | 391 | |
7fe519cb | 392 | /* If the thread layer is not (yet) initialized, fail. */ |
8a4ac37e | 393 | if (thread_db == NULL || !thread_db->all_symbols_looked_up) |
7fe519cb UW |
394 | return TD_ERR; |
395 | ||
5876f503 JK |
396 | /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase |
397 | could work. */ | |
398 | if (thread_db->td_thr_tls_get_addr_p == NULL | |
399 | || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL)) | |
cdbfd419 PP |
400 | return -1; |
401 | ||
54a0b537 PA |
402 | lwp = get_thread_lwp (thread); |
403 | if (!lwp->thread_known) | |
80894984 | 404 | find_one_thread (thread->entry.id); |
54a0b537 | 405 | if (!lwp->thread_known) |
dae5f5cf DJ |
406 | return TD_NOTHR; |
407 | ||
0bfdf32f GB |
408 | saved_thread = current_thread; |
409 | current_thread = thread; | |
5876f503 JK |
410 | |
411 | if (load_module != 0) | |
412 | { | |
413 | /* Note the cast through uintptr_t: this interface only works if | |
414 | a target address fits in a psaddr_t, which is a host pointer. | |
415 | So a 32-bit debugger can not access 64-bit TLS through this. */ | |
416 | err = thread_db->td_thr_tls_get_addr_p (&lwp->th, | |
417 | (psaddr_t) (uintptr_t) load_module, | |
418 | offset, &addr); | |
419 | } | |
420 | else | |
421 | { | |
422 | /* This code path handles the case of -static -pthread executables: | |
423 | https://sourceware.org/ml/libc-help/2014-03/msg00024.html | |
424 | For older GNU libc r_debug.r_map is NULL. For GNU libc after | |
425 | PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL. | |
426 | The constant number 1 depends on GNU __libc_setup_tls | |
427 | initialization of l_tls_modid to 1. */ | |
428 | err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr); | |
429 | addr = (char *) addr + offset; | |
430 | } | |
431 | ||
0bfdf32f | 432 | current_thread = saved_thread; |
dae5f5cf DJ |
433 | if (err == TD_OK) |
434 | { | |
186947f7 | 435 | *address = (CORE_ADDR) (uintptr_t) addr; |
dae5f5cf DJ |
436 | return 0; |
437 | } | |
438 | else | |
439 | return err; | |
cdbfd419 PP |
440 | } |
441 | ||
96f15937 PP |
442 | #ifdef USE_LIBTHREAD_DB_DIRECTLY |
443 | ||
444 | static int | |
445 | thread_db_load_search (void) | |
446 | { | |
447 | td_err_e err; | |
9836d6ea | 448 | struct thread_db *tdb; |
96f15937 PP |
449 | struct process_info *proc = current_process (); |
450 | ||
fe978cb0 | 451 | gdb_assert (proc->priv->thread_db == NULL); |
96f15937 | 452 | |
8d749320 | 453 | tdb = XCNEW (struct thread_db); |
fe978cb0 | 454 | proc->priv->thread_db = tdb; |
f9e39928 | 455 | |
9836d6ea | 456 | tdb->td_ta_new_p = &td_ta_new; |
96f15937 PP |
457 | |
458 | /* Attempt to open a connection to the thread library. */ | |
9836d6ea | 459 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
96f15937 PP |
460 | if (err != TD_OK) |
461 | { | |
462 | if (debug_threads) | |
87ce2a04 | 463 | debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err)); |
9836d6ea | 464 | free (tdb); |
fe978cb0 | 465 | proc->priv->thread_db = NULL; |
96f15937 PP |
466 | return 0; |
467 | } | |
468 | ||
9836d6ea PA |
469 | tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr; |
470 | tdb->td_thr_get_info_p = &td_thr_get_info; | |
471 | tdb->td_ta_thr_iter_p = &td_ta_thr_iter; | |
472 | tdb->td_symbol_list_p = &td_symbol_list; | |
96f15937 | 473 | |
96f15937 | 474 | /* These are not essential. */ |
9836d6ea | 475 | tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr; |
5876f503 | 476 | tdb->td_thr_tlsbase_p = &td_thr_tlsbase; |
96f15937 PP |
477 | |
478 | return 1; | |
479 | } | |
480 | ||
481 | #else | |
482 | ||
cdbfd419 PP |
483 | static int |
484 | try_thread_db_load_1 (void *handle) | |
485 | { | |
486 | td_err_e err; | |
9836d6ea | 487 | struct thread_db *tdb; |
cdbfd419 PP |
488 | struct process_info *proc = current_process (); |
489 | ||
fe978cb0 | 490 | gdb_assert (proc->priv->thread_db == NULL); |
cdbfd419 | 491 | |
8d749320 | 492 | tdb = XCNEW (struct thread_db); |
fe978cb0 | 493 | proc->priv->thread_db = tdb; |
f9e39928 | 494 | |
9836d6ea | 495 | tdb->handle = handle; |
cdbfd419 PP |
496 | |
497 | /* Initialize pointers to the dynamic library functions we will use. | |
498 | Essential functions first. */ | |
499 | ||
500 | #define CHK(required, a) \ | |
501 | do \ | |
502 | { \ | |
503 | if ((a) == NULL) \ | |
504 | { \ | |
505 | if (debug_threads) \ | |
87ce2a04 | 506 | debug_printf ("dlsym: %s\n", dlerror ()); \ |
cdbfd419 | 507 | if (required) \ |
9836d6ea PA |
508 | { \ |
509 | free (tdb); \ | |
fe978cb0 | 510 | proc->priv->thread_db = NULL; \ |
9836d6ea PA |
511 | return 0; \ |
512 | } \ | |
cdbfd419 PP |
513 | } \ |
514 | } \ | |
515 | while (0) | |
516 | ||
96e9210f PA |
517 | #define TDB_DLSYM(tdb, func) \ |
518 | tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func) | |
519 | ||
520 | CHK (1, TDB_DLSYM (tdb, td_ta_new)); | |
cdbfd419 PP |
521 | |
522 | /* Attempt to open a connection to the thread library. */ | |
9836d6ea | 523 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
cdbfd419 PP |
524 | if (err != TD_OK) |
525 | { | |
526 | if (debug_threads) | |
87ce2a04 | 527 | debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err)); |
9836d6ea | 528 | free (tdb); |
fe978cb0 | 529 | proc->priv->thread_db = NULL; |
cdbfd419 PP |
530 | return 0; |
531 | } | |
532 | ||
96e9210f PA |
533 | CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr)); |
534 | CHK (1, TDB_DLSYM (tdb, td_thr_get_info)); | |
535 | CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter)); | |
536 | CHK (1, TDB_DLSYM (tdb, td_symbol_list)); | |
cdbfd419 | 537 | |
cdbfd419 | 538 | /* These are not essential. */ |
96e9210f PA |
539 | CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr)); |
540 | CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase)); | |
cdbfd419 PP |
541 | |
542 | #undef CHK | |
96e9210f | 543 | #undef TDB_DLSYM |
cdbfd419 | 544 | |
cdbfd419 PP |
545 | return 1; |
546 | } | |
547 | ||
10e86dd7 DE |
548 | #ifdef HAVE_DLADDR |
549 | ||
cdbfd419 PP |
550 | /* Lookup a library in which given symbol resides. |
551 | Note: this is looking in the GDBSERVER process, not in the inferior. | |
552 | Returns library name, or NULL. */ | |
553 | ||
554 | static const char * | |
555 | dladdr_to_soname (const void *addr) | |
556 | { | |
557 | Dl_info info; | |
558 | ||
559 | if (dladdr (addr, &info) != 0) | |
560 | return info.dli_fname; | |
561 | return NULL; | |
562 | } | |
563 | ||
10e86dd7 DE |
564 | #endif |
565 | ||
cdbfd419 PP |
566 | static int |
567 | try_thread_db_load (const char *library) | |
568 | { | |
569 | void *handle; | |
570 | ||
571 | if (debug_threads) | |
87ce2a04 DE |
572 | debug_printf ("Trying host libthread_db library: %s.\n", |
573 | library); | |
cdbfd419 PP |
574 | handle = dlopen (library, RTLD_NOW); |
575 | if (handle == NULL) | |
576 | { | |
577 | if (debug_threads) | |
87ce2a04 | 578 | debug_printf ("dlopen failed: %s.\n", dlerror ()); |
cdbfd419 PP |
579 | return 0; |
580 | } | |
581 | ||
10e86dd7 | 582 | #ifdef HAVE_DLADDR |
cdbfd419 PP |
583 | if (debug_threads && strchr (library, '/') == NULL) |
584 | { | |
585 | void *td_init; | |
586 | ||
587 | td_init = dlsym (handle, "td_init"); | |
588 | if (td_init != NULL) | |
589 | { | |
590 | const char *const libpath = dladdr_to_soname (td_init); | |
591 | ||
592 | if (libpath != NULL) | |
4eefa7bc | 593 | debug_printf ("Host %s resolved to: %s.\n", library, libpath); |
cdbfd419 PP |
594 | } |
595 | } | |
10e86dd7 | 596 | #endif |
cdbfd419 PP |
597 | |
598 | if (try_thread_db_load_1 (handle)) | |
599 | return 1; | |
600 | ||
601 | /* This library "refused" to work on current inferior. */ | |
602 | dlclose (handle); | |
603 | return 0; | |
604 | } | |
605 | ||
98a5dd13 DE |
606 | /* Handle $sdir in libthread-db-search-path. |
607 | Look for libthread_db in the system dirs, or wherever a plain | |
608 | dlopen(file_without_path) will look. | |
609 | The result is true for success. */ | |
610 | ||
cdbfd419 | 611 | static int |
98a5dd13 DE |
612 | try_thread_db_load_from_sdir (void) |
613 | { | |
614 | return try_thread_db_load (LIBTHREAD_DB_SO); | |
615 | } | |
616 | ||
617 | /* Try to load libthread_db from directory DIR of length DIR_LEN. | |
618 | The result is true for success. */ | |
619 | ||
620 | static int | |
621 | try_thread_db_load_from_dir (const char *dir, size_t dir_len) | |
cdbfd419 PP |
622 | { |
623 | char path[PATH_MAX]; | |
98a5dd13 DE |
624 | |
625 | if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path)) | |
626 | { | |
224c3ddb | 627 | char *cp = (char *) xmalloc (dir_len + 1); |
98a5dd13 DE |
628 | |
629 | memcpy (cp, dir, dir_len); | |
630 | cp[dir_len] = '\0'; | |
631 | warning (_("libthread-db-search-path component too long," | |
632 | " ignored: %s."), cp); | |
633 | free (cp); | |
634 | return 0; | |
635 | } | |
636 | ||
637 | memcpy (path, dir, dir_len); | |
638 | path[dir_len] = '/'; | |
639 | strcpy (path + dir_len + 1, LIBTHREAD_DB_SO); | |
640 | return try_thread_db_load (path); | |
641 | } | |
642 | ||
643 | /* Search libthread_db_search_path for libthread_db which "agrees" | |
644 | to work on current inferior. | |
645 | The result is true for success. */ | |
646 | ||
647 | static int | |
648 | thread_db_load_search (void) | |
649 | { | |
e6712ff1 DE |
650 | VEC (char_ptr) *dir_vec; |
651 | char *this_dir; | |
652 | int i, rc = 0; | |
cdbfd419 PP |
653 | |
654 | if (libthread_db_search_path == NULL) | |
655 | libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH); | |
656 | ||
e6712ff1 DE |
657 | dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path); |
658 | ||
659 | for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i) | |
cdbfd419 | 660 | { |
e6712ff1 | 661 | const int pdir_len = sizeof ("$pdir") - 1; |
98a5dd13 DE |
662 | size_t this_dir_len; |
663 | ||
e6712ff1 | 664 | this_dir_len = strlen (this_dir); |
cdbfd419 | 665 | |
e6712ff1 DE |
666 | if (strncmp (this_dir, "$pdir", pdir_len) == 0 |
667 | && (this_dir[pdir_len] == '\0' | |
668 | || this_dir[pdir_len] == '/')) | |
98a5dd13 DE |
669 | { |
670 | /* We don't maintain a list of loaded libraries so we don't know | |
671 | where libpthread lives. We *could* fetch the info, but we don't | |
672 | do that yet. Ignore it. */ | |
673 | } | |
e6712ff1 | 674 | else if (strcmp (this_dir, "$sdir") == 0) |
98a5dd13 DE |
675 | { |
676 | if (try_thread_db_load_from_sdir ()) | |
cdbfd419 | 677 | { |
98a5dd13 | 678 | rc = 1; |
cdbfd419 PP |
679 | break; |
680 | } | |
cdbfd419 | 681 | } |
98a5dd13 | 682 | else |
cdbfd419 | 683 | { |
98a5dd13 DE |
684 | if (try_thread_db_load_from_dir (this_dir, this_dir_len)) |
685 | { | |
686 | rc = 1; | |
687 | break; | |
688 | } | |
cdbfd419 PP |
689 | } |
690 | } | |
cdbfd419 | 691 | |
e6712ff1 | 692 | free_char_ptr_vec (dir_vec); |
cdbfd419 | 693 | if (debug_threads) |
87ce2a04 | 694 | debug_printf ("thread_db_load_search returning %d\n", rc); |
cdbfd419 | 695 | return rc; |
dae5f5cf DJ |
696 | } |
697 | ||
96f15937 PP |
698 | #endif /* USE_LIBTHREAD_DB_DIRECTLY */ |
699 | ||
0d62e5e8 | 700 | int |
9b4c5f87 | 701 | thread_db_init (void) |
0d62e5e8 | 702 | { |
95954743 | 703 | struct process_info *proc = current_process (); |
0d62e5e8 | 704 | |
fd500816 DJ |
705 | /* FIXME drow/2004-10-16: This is the "overall process ID", which |
706 | GNU/Linux calls tgid, "thread group ID". When we support | |
707 | attaching to threads, the original thread may not be the correct | |
708 | thread. We would have to get the process ID from /proc for NPTL. | |
fd500816 DJ |
709 | |
710 | This isn't the only place in gdbserver that assumes that the first | |
711 | process in the list is the thread group leader. */ | |
ea025f5f | 712 | |
cdbfd419 | 713 | if (thread_db_load_search ()) |
0d62e5e8 | 714 | { |
2db9a427 PA |
715 | /* It's best to avoid td_ta_thr_iter if possible. That walks |
716 | data structures in the inferior's address space that may be | |
717 | corrupted, or, if the target is running, the list may change | |
718 | while we walk it. In the latter case, it's possible that a | |
719 | thread exits just at the exact time that causes GDBserver to | |
9b4c5f87 AT |
720 | get stuck in an infinite loop. As the kernel supports clone |
721 | events and /proc/PID/task/ exists, then we already know about | |
2db9a427 PA |
722 | all threads in the process. When we need info out of |
723 | thread_db on a given thread (e.g., for TLS), we'll use | |
724 | find_one_thread then. That uses thread_db entry points that | |
725 | do not walk libpthread's thread list, so should be safe, as | |
726 | well as more efficient. */ | |
9b4c5f87 | 727 | if (!linux_proc_task_list_dir_exists (pid_of (proc))) |
2db9a427 | 728 | thread_db_find_new_threads (); |
fd500816 | 729 | thread_db_look_up_symbols (); |
0d62e5e8 | 730 | return 1; |
cdbfd419 | 731 | } |
0d62e5e8 | 732 | |
cdbfd419 PP |
733 | return 0; |
734 | } | |
735 | ||
ca5c370d PA |
736 | static int |
737 | any_thread_of (struct inferior_list_entry *entry, void *args) | |
738 | { | |
9a3c8263 | 739 | int *pid_p = (int *) args; |
ca5c370d PA |
740 | |
741 | if (ptid_get_pid (entry->id) == *pid_p) | |
742 | return 1; | |
743 | ||
744 | return 0; | |
745 | } | |
746 | ||
f9e39928 PA |
747 | static void |
748 | switch_to_process (struct process_info *proc) | |
749 | { | |
750 | int pid = pid_of (proc); | |
751 | ||
0bfdf32f | 752 | current_thread = |
f9e39928 PA |
753 | (struct thread_info *) find_inferior (&all_threads, |
754 | any_thread_of, &pid); | |
755 | } | |
756 | ||
cdbfd419 PP |
757 | /* Disconnect from libthread_db and free resources. */ |
758 | ||
8336d594 PA |
759 | static void |
760 | disable_thread_event_reporting (struct process_info *proc) | |
cdbfd419 | 761 | { |
fe978cb0 | 762 | struct thread_db *thread_db = proc->priv->thread_db; |
cdbfd419 PP |
763 | if (thread_db) |
764 | { | |
21e1bee4 PP |
765 | td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta, |
766 | td_thr_events_t *event); | |
767 | ||
fd7dd3e6 | 768 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
96e9210f PA |
769 | td_ta_clear_event_p |
770 | = (td_ta_clear_event_ftype *) dlsym (thread_db->handle, | |
771 | "td_ta_clear_event"); | |
fd7dd3e6 | 772 | #else |
fd7dd3e6 PA |
773 | td_ta_clear_event_p = &td_ta_clear_event; |
774 | #endif | |
775 | ||
8336d594 | 776 | if (td_ta_clear_event_p != NULL) |
21e1bee4 | 777 | { |
0bfdf32f | 778 | struct thread_info *saved_thread = current_thread; |
21e1bee4 | 779 | td_thr_events_t events; |
8336d594 | 780 | |
f9e39928 | 781 | switch_to_process (proc); |
21e1bee4 | 782 | |
fd7dd3e6 PA |
783 | /* Set the process wide mask saying we aren't interested |
784 | in any events anymore. */ | |
21e1bee4 PP |
785 | td_event_fillset (&events); |
786 | (*td_ta_clear_event_p) (thread_db->thread_agent, &events); | |
8336d594 | 787 | |
0bfdf32f | 788 | current_thread = saved_thread; |
21e1bee4 | 789 | } |
8336d594 PA |
790 | } |
791 | } | |
792 | ||
793 | void | |
794 | thread_db_detach (struct process_info *proc) | |
795 | { | |
fe978cb0 | 796 | struct thread_db *thread_db = proc->priv->thread_db; |
f9e39928 PA |
797 | |
798 | if (thread_db) | |
799 | { | |
800 | disable_thread_event_reporting (proc); | |
f9e39928 | 801 | } |
8336d594 PA |
802 | } |
803 | ||
804 | /* Disconnect from libthread_db and free resources. */ | |
805 | ||
806 | void | |
807 | thread_db_mourn (struct process_info *proc) | |
808 | { | |
fe978cb0 | 809 | struct thread_db *thread_db = proc->priv->thread_db; |
8336d594 PA |
810 | if (thread_db) |
811 | { | |
96e9210f | 812 | td_ta_delete_ftype *td_ta_delete_p; |
8336d594 PA |
813 | |
814 | #ifndef USE_LIBTHREAD_DB_DIRECTLY | |
96e9210f | 815 | td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete"); |
8336d594 PA |
816 | #else |
817 | td_ta_delete_p = &td_ta_delete; | |
818 | #endif | |
cdbfd419 | 819 | |
cdbfd419 PP |
820 | if (td_ta_delete_p != NULL) |
821 | (*td_ta_delete_p) (thread_db->thread_agent); | |
822 | ||
fd7dd3e6 | 823 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
cdbfd419 | 824 | dlclose (thread_db->handle); |
96f15937 PP |
825 | #endif /* USE_LIBTHREAD_DB_DIRECTLY */ |
826 | ||
cdbfd419 | 827 | free (thread_db); |
fe978cb0 | 828 | proc->priv->thread_db = NULL; |
cdbfd419 PP |
829 | } |
830 | } | |
831 | ||
832 | /* Handle "set libthread-db-search-path" monitor command and return 1. | |
833 | For any other command, return 0. */ | |
834 | ||
835 | int | |
836 | thread_db_handle_monitor_command (char *mon) | |
837 | { | |
84e578fb DE |
838 | const char *cmd = "set libthread-db-search-path"; |
839 | size_t cmd_len = strlen (cmd); | |
840 | ||
841 | if (strncmp (mon, cmd, cmd_len) == 0 | |
842 | && (mon[cmd_len] == '\0' | |
843 | || mon[cmd_len] == ' ')) | |
cdbfd419 | 844 | { |
84e578fb | 845 | const char *cp = mon + cmd_len; |
cdbfd419 PP |
846 | |
847 | if (libthread_db_search_path != NULL) | |
848 | free (libthread_db_search_path); | |
849 | ||
850 | /* Skip leading space (if any). */ | |
851 | while (isspace (*cp)) | |
852 | ++cp; | |
853 | ||
84e578fb DE |
854 | if (*cp == '\0') |
855 | cp = LIBTHREAD_DB_SEARCH_PATH; | |
cdbfd419 PP |
856 | libthread_db_search_path = xstrdup (cp); |
857 | ||
858 | monitor_output ("libthread-db-search-path set to `"); | |
859 | monitor_output (libthread_db_search_path); | |
860 | monitor_output ("'\n"); | |
861 | return 1; | |
0d62e5e8 DJ |
862 | } |
863 | ||
cdbfd419 | 864 | /* Tell server.c to perform default processing. */ |
0d62e5e8 DJ |
865 | return 0; |
866 | } |