]>
Commit | Line | Data |
---|---|---|
0d62e5e8 | 1 | /* Thread management interface, for the remote server for GDB. |
61baf725 | 2 | Copyright (C) 2002-2017 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; | |
f6327dcb | 203 | lwp->thread_handle = ti.ti_tid; |
ae13219e | 204 | |
ae13219e DJ |
205 | return 1; |
206 | } | |
207 | ||
5f7d1694 PP |
208 | /* Attach a thread. Return true on success. */ |
209 | ||
210 | static int | |
211 | attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p) | |
ae13219e | 212 | { |
7ae1a6a6 PA |
213 | struct process_info *proc = current_process (); |
214 | int pid = pid_of (proc); | |
215 | ptid_t ptid = ptid_build (pid, ti_p->ti_lid, 0); | |
54a0b537 | 216 | struct lwp_info *lwp; |
7ae1a6a6 | 217 | int err; |
ae13219e | 218 | |
0d62e5e8 | 219 | if (debug_threads) |
87ce2a04 | 220 | debug_printf ("Attaching to thread %ld (LWP %d)\n", |
d41401ac | 221 | (unsigned long) ti_p->ti_tid, ti_p->ti_lid); |
7ae1a6a6 PA |
222 | err = linux_attach_lwp (ptid); |
223 | if (err != 0) | |
0d62e5e8 | 224 | { |
7ae1a6a6 | 225 | warning ("Could not attach to thread %ld (LWP %d): %s\n", |
d41401ac | 226 | (unsigned long) ti_p->ti_tid, ti_p->ti_lid, |
8784d563 | 227 | linux_ptrace_attach_fail_reason_string (ptid, err)); |
5f7d1694 | 228 | return 0; |
0d62e5e8 DJ |
229 | } |
230 | ||
7ae1a6a6 PA |
231 | lwp = find_lwp_pid (ptid); |
232 | gdb_assert (lwp != NULL); | |
54a0b537 PA |
233 | lwp->thread_known = 1; |
234 | lwp->th = *th_p; | |
f6327dcb | 235 | lwp->thread_handle = ti_p->ti_tid; |
24a09b5f | 236 | |
5f7d1694 PP |
237 | return 1; |
238 | } | |
239 | ||
240 | /* Attach thread if we haven't seen it yet. | |
241 | Increment *COUNTER if we have attached a new thread. | |
242 | Return false on failure. */ | |
243 | ||
244 | static int | |
245 | maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p, | |
246 | int *counter) | |
247 | { | |
248 | struct lwp_info *lwp; | |
249 | ||
250 | lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid)); | |
251 | if (lwp != NULL) | |
252 | return 1; | |
253 | ||
254 | if (!attach_thread (th_p, ti_p)) | |
255 | return 0; | |
256 | ||
257 | if (counter != NULL) | |
258 | *counter += 1; | |
259 | ||
260 | return 1; | |
0d62e5e8 DJ |
261 | } |
262 | ||
263 | static int | |
264 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) | |
265 | { | |
266 | td_thrinfo_t ti; | |
267 | td_err_e err; | |
fe978cb0 | 268 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
0d62e5e8 | 269 | |
cdbfd419 | 270 | err = thread_db->td_thr_get_info_p (th_p, &ti); |
0d62e5e8 DJ |
271 | if (err != TD_OK) |
272 | error ("Cannot get thread info: %s", thread_db_err_str (err)); | |
273 | ||
a33e3959 PA |
274 | if (ti.ti_lid == -1) |
275 | { | |
276 | /* A thread with kernel thread ID -1 is either a thread that | |
277 | exited and was joined, or a thread that is being created but | |
278 | hasn't started yet, and that is reusing the tcb/stack of a | |
279 | thread that previously exited and was joined. (glibc marks | |
280 | terminated and joined threads with kernel thread ID -1. See | |
281 | glibc PR17707. */ | |
d6c146e9 PA |
282 | if (debug_threads) |
283 | debug_printf ("thread_db: skipping exited and " | |
d41401ac DE |
284 | "joined thread (0x%lx)\n", |
285 | (unsigned long) ti.ti_tid); | |
a33e3959 PA |
286 | return 0; |
287 | } | |
288 | ||
0d62e5e8 DJ |
289 | /* Check for zombies. */ |
290 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) | |
291 | return 0; | |
292 | ||
5f7d1694 PP |
293 | if (!maybe_attach_thread (th_p, &ti, (int *) data)) |
294 | { | |
295 | /* Terminate iteration early: we might be looking at stale data in | |
296 | the inferior. The thread_db_find_new_threads will retry. */ | |
297 | return 1; | |
298 | } | |
0d62e5e8 DJ |
299 | |
300 | return 0; | |
301 | } | |
302 | ||
303 | static void | |
304 | thread_db_find_new_threads (void) | |
305 | { | |
306 | td_err_e err; | |
fbd5db48 | 307 | ptid_t ptid = current_ptid; |
fe978cb0 | 308 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
5f7d1694 | 309 | int loop, iteration; |
0d62e5e8 | 310 | |
ae13219e DJ |
311 | /* This function is only called when we first initialize thread_db. |
312 | First locate the initial thread. If it is not ready for | |
313 | debugging yet, then stop. */ | |
95954743 | 314 | if (find_one_thread (ptid) == 0) |
ae13219e DJ |
315 | return; |
316 | ||
5f7d1694 PP |
317 | /* Require 4 successive iterations which do not find any new threads. |
318 | The 4 is a heuristic: there is an inherent race here, and I have | |
319 | seen that 2 iterations in a row are not always sufficient to | |
320 | "capture" all threads. */ | |
321 | for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration) | |
322 | { | |
323 | int new_thread_count = 0; | |
324 | ||
325 | /* Iterate over all user-space threads to discover new threads. */ | |
326 | err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent, | |
327 | find_new_threads_callback, | |
328 | &new_thread_count, | |
493e2a69 MS |
329 | TD_THR_ANY_STATE, |
330 | TD_THR_LOWEST_PRIORITY, | |
5f7d1694 PP |
331 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); |
332 | if (debug_threads) | |
87ce2a04 DE |
333 | debug_printf ("Found %d threads in iteration %d.\n", |
334 | new_thread_count, iteration); | |
5f7d1694 PP |
335 | |
336 | if (new_thread_count != 0) | |
337 | { | |
338 | /* Found new threads. Restart iteration from beginning. */ | |
339 | loop = -1; | |
340 | } | |
341 | } | |
0d62e5e8 DJ |
342 | if (err != TD_OK) |
343 | error ("Cannot find new threads: %s", thread_db_err_str (err)); | |
344 | } | |
345 | ||
fd500816 DJ |
346 | /* Cache all future symbols that thread_db might request. We can not |
347 | request symbols at arbitrary states in the remote protocol, only | |
348 | when the client tells us that new symbols are available. So when | |
349 | we load the thread library, make sure to check the entire list. */ | |
350 | ||
351 | static void | |
352 | thread_db_look_up_symbols (void) | |
353 | { | |
fe978cb0 | 354 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
cdbfd419 | 355 | const char **sym_list; |
fd500816 DJ |
356 | CORE_ADDR unused; |
357 | ||
cdbfd419 | 358 | for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++) |
9836d6ea PA |
359 | look_up_one_symbol (*sym_list, &unused, 1); |
360 | ||
361 | /* We're not interested in any other libraries loaded after this | |
362 | point, only in symbols in libpthread.so. */ | |
363 | thread_db->all_symbols_looked_up = 1; | |
364 | } | |
365 | ||
366 | int | |
367 | thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp) | |
368 | { | |
fe978cb0 | 369 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
9836d6ea PA |
370 | int may_ask_gdb = !thread_db->all_symbols_looked_up; |
371 | ||
372 | /* If we've passed the call to thread_db_look_up_symbols, then | |
373 | anything not in the cache must not exist; we're not interested | |
374 | in any libraries loaded after that point, only in symbols in | |
375 | libpthread.so. It might not be an appropriate time to look | |
376 | up a symbol, e.g. while we're trying to fetch registers. */ | |
377 | return look_up_one_symbol (name, addrp, may_ask_gdb); | |
fd500816 DJ |
378 | } |
379 | ||
dae5f5cf DJ |
380 | int |
381 | thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset, | |
382 | CORE_ADDR load_module, CORE_ADDR *address) | |
383 | { | |
dae5f5cf DJ |
384 | psaddr_t addr; |
385 | td_err_e err; | |
54a0b537 | 386 | struct lwp_info *lwp; |
0bfdf32f | 387 | struct thread_info *saved_thread; |
cdbfd419 PP |
388 | struct process_info *proc; |
389 | struct thread_db *thread_db; | |
390 | ||
391 | proc = get_thread_process (thread); | |
fe978cb0 | 392 | thread_db = proc->priv->thread_db; |
dae5f5cf | 393 | |
7fe519cb | 394 | /* If the thread layer is not (yet) initialized, fail. */ |
8a4ac37e | 395 | if (thread_db == NULL || !thread_db->all_symbols_looked_up) |
7fe519cb UW |
396 | return TD_ERR; |
397 | ||
5876f503 JK |
398 | /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase |
399 | could work. */ | |
400 | if (thread_db->td_thr_tls_get_addr_p == NULL | |
401 | || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL)) | |
cdbfd419 PP |
402 | return -1; |
403 | ||
54a0b537 PA |
404 | lwp = get_thread_lwp (thread); |
405 | if (!lwp->thread_known) | |
80894984 | 406 | find_one_thread (thread->entry.id); |
54a0b537 | 407 | if (!lwp->thread_known) |
dae5f5cf DJ |
408 | return TD_NOTHR; |
409 | ||
0bfdf32f GB |
410 | saved_thread = current_thread; |
411 | current_thread = thread; | |
5876f503 JK |
412 | |
413 | if (load_module != 0) | |
414 | { | |
415 | /* Note the cast through uintptr_t: this interface only works if | |
416 | a target address fits in a psaddr_t, which is a host pointer. | |
417 | So a 32-bit debugger can not access 64-bit TLS through this. */ | |
418 | err = thread_db->td_thr_tls_get_addr_p (&lwp->th, | |
419 | (psaddr_t) (uintptr_t) load_module, | |
420 | offset, &addr); | |
421 | } | |
422 | else | |
423 | { | |
424 | /* This code path handles the case of -static -pthread executables: | |
425 | https://sourceware.org/ml/libc-help/2014-03/msg00024.html | |
426 | For older GNU libc r_debug.r_map is NULL. For GNU libc after | |
427 | PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL. | |
428 | The constant number 1 depends on GNU __libc_setup_tls | |
429 | initialization of l_tls_modid to 1. */ | |
430 | err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr); | |
431 | addr = (char *) addr + offset; | |
432 | } | |
433 | ||
0bfdf32f | 434 | current_thread = saved_thread; |
dae5f5cf DJ |
435 | if (err == TD_OK) |
436 | { | |
186947f7 | 437 | *address = (CORE_ADDR) (uintptr_t) addr; |
dae5f5cf DJ |
438 | return 0; |
439 | } | |
440 | else | |
441 | return err; | |
cdbfd419 PP |
442 | } |
443 | ||
f6327dcb KB |
444 | /* See linux-low.h. */ |
445 | ||
446 | bool | |
447 | thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len) | |
448 | { | |
449 | struct thread_db *thread_db; | |
450 | struct lwp_info *lwp; | |
451 | struct thread_info *thread | |
452 | = (struct thread_info *) find_inferior_id (&all_threads, ptid); | |
453 | ||
454 | if (thread == NULL) | |
455 | return false; | |
456 | ||
457 | thread_db = get_thread_process (thread)->priv->thread_db; | |
458 | ||
459 | if (thread_db == NULL) | |
460 | return false; | |
461 | ||
462 | lwp = get_thread_lwp (thread); | |
463 | ||
464 | if (!lwp->thread_known && !find_one_thread (thread->entry.id)) | |
465 | return false; | |
466 | ||
467 | gdb_assert (lwp->thread_known); | |
468 | ||
469 | *handle = (gdb_byte *) &lwp->thread_handle; | |
470 | *handle_len = sizeof (lwp->thread_handle); | |
471 | return true; | |
472 | } | |
473 | ||
96f15937 PP |
474 | #ifdef USE_LIBTHREAD_DB_DIRECTLY |
475 | ||
476 | static int | |
477 | thread_db_load_search (void) | |
478 | { | |
479 | td_err_e err; | |
9836d6ea | 480 | struct thread_db *tdb; |
96f15937 PP |
481 | struct process_info *proc = current_process (); |
482 | ||
fe978cb0 | 483 | gdb_assert (proc->priv->thread_db == NULL); |
96f15937 | 484 | |
8d749320 | 485 | tdb = XCNEW (struct thread_db); |
fe978cb0 | 486 | proc->priv->thread_db = tdb; |
f9e39928 | 487 | |
9836d6ea | 488 | tdb->td_ta_new_p = &td_ta_new; |
96f15937 PP |
489 | |
490 | /* Attempt to open a connection to the thread library. */ | |
9836d6ea | 491 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
96f15937 PP |
492 | if (err != TD_OK) |
493 | { | |
494 | if (debug_threads) | |
87ce2a04 | 495 | debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err)); |
9836d6ea | 496 | free (tdb); |
fe978cb0 | 497 | proc->priv->thread_db = NULL; |
96f15937 PP |
498 | return 0; |
499 | } | |
500 | ||
9836d6ea PA |
501 | tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr; |
502 | tdb->td_thr_get_info_p = &td_thr_get_info; | |
503 | tdb->td_ta_thr_iter_p = &td_ta_thr_iter; | |
504 | tdb->td_symbol_list_p = &td_symbol_list; | |
96f15937 | 505 | |
96f15937 | 506 | /* These are not essential. */ |
9836d6ea | 507 | tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr; |
5876f503 | 508 | tdb->td_thr_tlsbase_p = &td_thr_tlsbase; |
96f15937 PP |
509 | |
510 | return 1; | |
511 | } | |
512 | ||
513 | #else | |
514 | ||
cdbfd419 PP |
515 | static int |
516 | try_thread_db_load_1 (void *handle) | |
517 | { | |
518 | td_err_e err; | |
9836d6ea | 519 | struct thread_db *tdb; |
cdbfd419 PP |
520 | struct process_info *proc = current_process (); |
521 | ||
fe978cb0 | 522 | gdb_assert (proc->priv->thread_db == NULL); |
cdbfd419 | 523 | |
8d749320 | 524 | tdb = XCNEW (struct thread_db); |
fe978cb0 | 525 | proc->priv->thread_db = tdb; |
f9e39928 | 526 | |
9836d6ea | 527 | tdb->handle = handle; |
cdbfd419 PP |
528 | |
529 | /* Initialize pointers to the dynamic library functions we will use. | |
530 | Essential functions first. */ | |
531 | ||
532 | #define CHK(required, a) \ | |
533 | do \ | |
534 | { \ | |
535 | if ((a) == NULL) \ | |
536 | { \ | |
537 | if (debug_threads) \ | |
87ce2a04 | 538 | debug_printf ("dlsym: %s\n", dlerror ()); \ |
cdbfd419 | 539 | if (required) \ |
9836d6ea PA |
540 | { \ |
541 | free (tdb); \ | |
fe978cb0 | 542 | proc->priv->thread_db = NULL; \ |
9836d6ea PA |
543 | return 0; \ |
544 | } \ | |
cdbfd419 PP |
545 | } \ |
546 | } \ | |
547 | while (0) | |
548 | ||
96e9210f PA |
549 | #define TDB_DLSYM(tdb, func) \ |
550 | tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func) | |
551 | ||
552 | CHK (1, TDB_DLSYM (tdb, td_ta_new)); | |
cdbfd419 PP |
553 | |
554 | /* Attempt to open a connection to the thread library. */ | |
9836d6ea | 555 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
cdbfd419 PP |
556 | if (err != TD_OK) |
557 | { | |
558 | if (debug_threads) | |
87ce2a04 | 559 | debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err)); |
9836d6ea | 560 | free (tdb); |
fe978cb0 | 561 | proc->priv->thread_db = NULL; |
cdbfd419 PP |
562 | return 0; |
563 | } | |
564 | ||
96e9210f PA |
565 | CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr)); |
566 | CHK (1, TDB_DLSYM (tdb, td_thr_get_info)); | |
567 | CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter)); | |
568 | CHK (1, TDB_DLSYM (tdb, td_symbol_list)); | |
cdbfd419 | 569 | |
cdbfd419 | 570 | /* These are not essential. */ |
96e9210f PA |
571 | CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr)); |
572 | CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase)); | |
cdbfd419 PP |
573 | |
574 | #undef CHK | |
96e9210f | 575 | #undef TDB_DLSYM |
cdbfd419 | 576 | |
cdbfd419 PP |
577 | return 1; |
578 | } | |
579 | ||
10e86dd7 DE |
580 | #ifdef HAVE_DLADDR |
581 | ||
cdbfd419 PP |
582 | /* Lookup a library in which given symbol resides. |
583 | Note: this is looking in the GDBSERVER process, not in the inferior. | |
584 | Returns library name, or NULL. */ | |
585 | ||
586 | static const char * | |
587 | dladdr_to_soname (const void *addr) | |
588 | { | |
589 | Dl_info info; | |
590 | ||
591 | if (dladdr (addr, &info) != 0) | |
592 | return info.dli_fname; | |
593 | return NULL; | |
594 | } | |
595 | ||
10e86dd7 DE |
596 | #endif |
597 | ||
cdbfd419 PP |
598 | static int |
599 | try_thread_db_load (const char *library) | |
600 | { | |
601 | void *handle; | |
602 | ||
603 | if (debug_threads) | |
87ce2a04 DE |
604 | debug_printf ("Trying host libthread_db library: %s.\n", |
605 | library); | |
cdbfd419 PP |
606 | handle = dlopen (library, RTLD_NOW); |
607 | if (handle == NULL) | |
608 | { | |
609 | if (debug_threads) | |
87ce2a04 | 610 | debug_printf ("dlopen failed: %s.\n", dlerror ()); |
cdbfd419 PP |
611 | return 0; |
612 | } | |
613 | ||
10e86dd7 | 614 | #ifdef HAVE_DLADDR |
cdbfd419 PP |
615 | if (debug_threads && strchr (library, '/') == NULL) |
616 | { | |
617 | void *td_init; | |
618 | ||
619 | td_init = dlsym (handle, "td_init"); | |
620 | if (td_init != NULL) | |
621 | { | |
622 | const char *const libpath = dladdr_to_soname (td_init); | |
623 | ||
624 | if (libpath != NULL) | |
4eefa7bc | 625 | debug_printf ("Host %s resolved to: %s.\n", library, libpath); |
cdbfd419 PP |
626 | } |
627 | } | |
10e86dd7 | 628 | #endif |
cdbfd419 PP |
629 | |
630 | if (try_thread_db_load_1 (handle)) | |
631 | return 1; | |
632 | ||
633 | /* This library "refused" to work on current inferior. */ | |
634 | dlclose (handle); | |
635 | return 0; | |
636 | } | |
637 | ||
98a5dd13 DE |
638 | /* Handle $sdir in libthread-db-search-path. |
639 | Look for libthread_db in the system dirs, or wherever a plain | |
640 | dlopen(file_without_path) will look. | |
641 | The result is true for success. */ | |
642 | ||
cdbfd419 | 643 | static int |
98a5dd13 DE |
644 | try_thread_db_load_from_sdir (void) |
645 | { | |
646 | return try_thread_db_load (LIBTHREAD_DB_SO); | |
647 | } | |
648 | ||
649 | /* Try to load libthread_db from directory DIR of length DIR_LEN. | |
650 | The result is true for success. */ | |
651 | ||
652 | static int | |
653 | try_thread_db_load_from_dir (const char *dir, size_t dir_len) | |
cdbfd419 PP |
654 | { |
655 | char path[PATH_MAX]; | |
98a5dd13 DE |
656 | |
657 | if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path)) | |
658 | { | |
224c3ddb | 659 | char *cp = (char *) xmalloc (dir_len + 1); |
98a5dd13 DE |
660 | |
661 | memcpy (cp, dir, dir_len); | |
662 | cp[dir_len] = '\0'; | |
663 | warning (_("libthread-db-search-path component too long," | |
664 | " ignored: %s."), cp); | |
665 | free (cp); | |
666 | return 0; | |
667 | } | |
668 | ||
669 | memcpy (path, dir, dir_len); | |
670 | path[dir_len] = '/'; | |
671 | strcpy (path + dir_len + 1, LIBTHREAD_DB_SO); | |
672 | return try_thread_db_load (path); | |
673 | } | |
674 | ||
675 | /* Search libthread_db_search_path for libthread_db which "agrees" | |
676 | to work on current inferior. | |
677 | The result is true for success. */ | |
678 | ||
679 | static int | |
680 | thread_db_load_search (void) | |
681 | { | |
e6712ff1 DE |
682 | VEC (char_ptr) *dir_vec; |
683 | char *this_dir; | |
684 | int i, rc = 0; | |
cdbfd419 PP |
685 | |
686 | if (libthread_db_search_path == NULL) | |
687 | libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH); | |
688 | ||
e6712ff1 DE |
689 | dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path); |
690 | ||
691 | for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i) | |
cdbfd419 | 692 | { |
e6712ff1 | 693 | const int pdir_len = sizeof ("$pdir") - 1; |
98a5dd13 DE |
694 | size_t this_dir_len; |
695 | ||
e6712ff1 | 696 | this_dir_len = strlen (this_dir); |
cdbfd419 | 697 | |
e6712ff1 DE |
698 | if (strncmp (this_dir, "$pdir", pdir_len) == 0 |
699 | && (this_dir[pdir_len] == '\0' | |
700 | || this_dir[pdir_len] == '/')) | |
98a5dd13 DE |
701 | { |
702 | /* We don't maintain a list of loaded libraries so we don't know | |
703 | where libpthread lives. We *could* fetch the info, but we don't | |
704 | do that yet. Ignore it. */ | |
705 | } | |
e6712ff1 | 706 | else if (strcmp (this_dir, "$sdir") == 0) |
98a5dd13 DE |
707 | { |
708 | if (try_thread_db_load_from_sdir ()) | |
cdbfd419 | 709 | { |
98a5dd13 | 710 | rc = 1; |
cdbfd419 PP |
711 | break; |
712 | } | |
cdbfd419 | 713 | } |
98a5dd13 | 714 | else |
cdbfd419 | 715 | { |
98a5dd13 DE |
716 | if (try_thread_db_load_from_dir (this_dir, this_dir_len)) |
717 | { | |
718 | rc = 1; | |
719 | break; | |
720 | } | |
cdbfd419 PP |
721 | } |
722 | } | |
cdbfd419 | 723 | |
e6712ff1 | 724 | free_char_ptr_vec (dir_vec); |
cdbfd419 | 725 | if (debug_threads) |
87ce2a04 | 726 | debug_printf ("thread_db_load_search returning %d\n", rc); |
cdbfd419 | 727 | return rc; |
dae5f5cf DJ |
728 | } |
729 | ||
96f15937 PP |
730 | #endif /* USE_LIBTHREAD_DB_DIRECTLY */ |
731 | ||
0d62e5e8 | 732 | int |
9b4c5f87 | 733 | thread_db_init (void) |
0d62e5e8 | 734 | { |
95954743 | 735 | struct process_info *proc = current_process (); |
0d62e5e8 | 736 | |
fd500816 DJ |
737 | /* FIXME drow/2004-10-16: This is the "overall process ID", which |
738 | GNU/Linux calls tgid, "thread group ID". When we support | |
739 | attaching to threads, the original thread may not be the correct | |
740 | thread. We would have to get the process ID from /proc for NPTL. | |
fd500816 DJ |
741 | |
742 | This isn't the only place in gdbserver that assumes that the first | |
743 | process in the list is the thread group leader. */ | |
ea025f5f | 744 | |
cdbfd419 | 745 | if (thread_db_load_search ()) |
0d62e5e8 | 746 | { |
2db9a427 PA |
747 | /* It's best to avoid td_ta_thr_iter if possible. That walks |
748 | data structures in the inferior's address space that may be | |
749 | corrupted, or, if the target is running, the list may change | |
750 | while we walk it. In the latter case, it's possible that a | |
751 | thread exits just at the exact time that causes GDBserver to | |
9b4c5f87 AT |
752 | get stuck in an infinite loop. As the kernel supports clone |
753 | events and /proc/PID/task/ exists, then we already know about | |
2db9a427 PA |
754 | all threads in the process. When we need info out of |
755 | thread_db on a given thread (e.g., for TLS), we'll use | |
756 | find_one_thread then. That uses thread_db entry points that | |
757 | do not walk libpthread's thread list, so should be safe, as | |
758 | well as more efficient. */ | |
9b4c5f87 | 759 | if (!linux_proc_task_list_dir_exists (pid_of (proc))) |
2db9a427 | 760 | thread_db_find_new_threads (); |
fd500816 | 761 | thread_db_look_up_symbols (); |
0d62e5e8 | 762 | return 1; |
cdbfd419 | 763 | } |
0d62e5e8 | 764 | |
cdbfd419 PP |
765 | return 0; |
766 | } | |
767 | ||
f9e39928 PA |
768 | static void |
769 | switch_to_process (struct process_info *proc) | |
770 | { | |
771 | int pid = pid_of (proc); | |
772 | ||
785922a5 | 773 | current_thread = find_any_thread_of_pid (pid); |
f9e39928 PA |
774 | } |
775 | ||
cdbfd419 PP |
776 | /* Disconnect from libthread_db and free resources. */ |
777 | ||
8336d594 PA |
778 | static void |
779 | disable_thread_event_reporting (struct process_info *proc) | |
cdbfd419 | 780 | { |
fe978cb0 | 781 | struct thread_db *thread_db = proc->priv->thread_db; |
cdbfd419 PP |
782 | if (thread_db) |
783 | { | |
21e1bee4 PP |
784 | td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta, |
785 | td_thr_events_t *event); | |
786 | ||
fd7dd3e6 | 787 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
96e9210f PA |
788 | td_ta_clear_event_p |
789 | = (td_ta_clear_event_ftype *) dlsym (thread_db->handle, | |
790 | "td_ta_clear_event"); | |
fd7dd3e6 | 791 | #else |
fd7dd3e6 PA |
792 | td_ta_clear_event_p = &td_ta_clear_event; |
793 | #endif | |
794 | ||
8336d594 | 795 | if (td_ta_clear_event_p != NULL) |
21e1bee4 | 796 | { |
0bfdf32f | 797 | struct thread_info *saved_thread = current_thread; |
21e1bee4 | 798 | td_thr_events_t events; |
8336d594 | 799 | |
f9e39928 | 800 | switch_to_process (proc); |
21e1bee4 | 801 | |
fd7dd3e6 PA |
802 | /* Set the process wide mask saying we aren't interested |
803 | in any events anymore. */ | |
21e1bee4 PP |
804 | td_event_fillset (&events); |
805 | (*td_ta_clear_event_p) (thread_db->thread_agent, &events); | |
8336d594 | 806 | |
0bfdf32f | 807 | current_thread = saved_thread; |
21e1bee4 | 808 | } |
8336d594 PA |
809 | } |
810 | } | |
811 | ||
812 | void | |
813 | thread_db_detach (struct process_info *proc) | |
814 | { | |
fe978cb0 | 815 | struct thread_db *thread_db = proc->priv->thread_db; |
f9e39928 PA |
816 | |
817 | if (thread_db) | |
818 | { | |
819 | disable_thread_event_reporting (proc); | |
f9e39928 | 820 | } |
8336d594 PA |
821 | } |
822 | ||
823 | /* Disconnect from libthread_db and free resources. */ | |
824 | ||
825 | void | |
826 | thread_db_mourn (struct process_info *proc) | |
827 | { | |
fe978cb0 | 828 | struct thread_db *thread_db = proc->priv->thread_db; |
8336d594 PA |
829 | if (thread_db) |
830 | { | |
96e9210f | 831 | td_ta_delete_ftype *td_ta_delete_p; |
8336d594 PA |
832 | |
833 | #ifndef USE_LIBTHREAD_DB_DIRECTLY | |
96e9210f | 834 | td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete"); |
8336d594 PA |
835 | #else |
836 | td_ta_delete_p = &td_ta_delete; | |
837 | #endif | |
cdbfd419 | 838 | |
cdbfd419 PP |
839 | if (td_ta_delete_p != NULL) |
840 | (*td_ta_delete_p) (thread_db->thread_agent); | |
841 | ||
fd7dd3e6 | 842 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
cdbfd419 | 843 | dlclose (thread_db->handle); |
96f15937 PP |
844 | #endif /* USE_LIBTHREAD_DB_DIRECTLY */ |
845 | ||
cdbfd419 | 846 | free (thread_db); |
fe978cb0 | 847 | proc->priv->thread_db = NULL; |
cdbfd419 PP |
848 | } |
849 | } | |
850 | ||
851 | /* Handle "set libthread-db-search-path" monitor command and return 1. | |
852 | For any other command, return 0. */ | |
853 | ||
854 | int | |
855 | thread_db_handle_monitor_command (char *mon) | |
856 | { | |
84e578fb DE |
857 | const char *cmd = "set libthread-db-search-path"; |
858 | size_t cmd_len = strlen (cmd); | |
859 | ||
860 | if (strncmp (mon, cmd, cmd_len) == 0 | |
861 | && (mon[cmd_len] == '\0' | |
862 | || mon[cmd_len] == ' ')) | |
cdbfd419 | 863 | { |
84e578fb | 864 | const char *cp = mon + cmd_len; |
cdbfd419 PP |
865 | |
866 | if (libthread_db_search_path != NULL) | |
867 | free (libthread_db_search_path); | |
868 | ||
869 | /* Skip leading space (if any). */ | |
870 | while (isspace (*cp)) | |
871 | ++cp; | |
872 | ||
84e578fb DE |
873 | if (*cp == '\0') |
874 | cp = LIBTHREAD_DB_SEARCH_PATH; | |
cdbfd419 PP |
875 | libthread_db_search_path = xstrdup (cp); |
876 | ||
877 | monitor_output ("libthread-db-search-path set to `"); | |
878 | monitor_output (libthread_db_search_path); | |
879 | monitor_output ("'\n"); | |
880 | return 1; | |
0d62e5e8 DJ |
881 | } |
882 | ||
cdbfd419 | 883 | /* Tell server.c to perform default processing. */ |
0d62e5e8 DJ |
884 | return 0; |
885 | } | |
86299109 KB |
886 | |
887 | /* See linux-low.h. */ | |
888 | ||
889 | void | |
890 | thread_db_notice_clone (struct process_info *proc, ptid_t ptid) | |
891 | { | |
892 | struct thread_db *thread_db = proc->priv->thread_db; | |
893 | ||
894 | /* If the thread layer isn't initialized, return. It may just | |
895 | be that the program uses clone, but does not use libthread_db. */ | |
896 | if (thread_db == NULL || !thread_db->all_symbols_looked_up) | |
897 | return; | |
898 | ||
899 | if (!find_one_thread (ptid)) | |
900 | warning ("Cannot find thread after clone.\n"); | |
901 | } |