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