Commit | Line | Data |
---|---|---|
0d62e5e8 | 1 | /* Thread management interface, for the remote server for GDB. |
b811d2c2 | 2 | Copyright (C) 2002-2020 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 | ||
f9d949fb | 25 | #include "debug.h" |
0050a760 | 26 | #include "gdb_proc_service.h" |
125f8a3d | 27 | #include "nat/gdb_thread_db.h" |
268a13a5 | 28 | #include "gdbsupport/gdb_vecs.h" |
2db9a427 | 29 | #include "nat/linux-procfs.h" |
268a13a5 | 30 | #include "gdbsupport/scoped_restore.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 | ||
94c207e0 PA |
158 | /* Get thread info about PTID, accessing memory via the current |
159 | thread. */ | |
160 | ||
ae13219e | 161 | static int |
95954743 | 162 | find_one_thread (ptid_t ptid) |
0d62e5e8 | 163 | { |
ae13219e DJ |
164 | td_thrhandle_t th; |
165 | td_thrinfo_t ti; | |
0d62e5e8 | 166 | td_err_e err; |
54a0b537 | 167 | struct lwp_info *lwp; |
fe978cb0 | 168 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
e38504b3 | 169 | int lwpid = ptid.lwp (); |
0d62e5e8 | 170 | |
8dc7b443 SM |
171 | thread_info *thread = find_thread_ptid (ptid); |
172 | lwp = get_thread_lwp (thread); | |
54a0b537 | 173 | if (lwp->thread_known) |
ae13219e DJ |
174 | return 1; |
175 | ||
24a09b5f | 176 | /* Get information about this thread. */ |
cdbfd419 | 177 | err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th); |
ae13219e | 178 | if (err != TD_OK) |
24a09b5f DJ |
179 | error ("Cannot get thread handle for LWP %d: %s", |
180 | lwpid, thread_db_err_str (err)); | |
ae13219e | 181 | |
cdbfd419 | 182 | err = thread_db->td_thr_get_info_p (&th, &ti); |
ae13219e | 183 | if (err != TD_OK) |
24a09b5f DJ |
184 | error ("Cannot get thread info for LWP %d: %s", |
185 | lwpid, thread_db_err_str (err)); | |
ae13219e DJ |
186 | |
187 | if (debug_threads) | |
87ce2a04 | 188 | debug_printf ("Found thread %ld (LWP %d)\n", |
d41401ac | 189 | (unsigned long) ti.ti_tid, ti.ti_lid); |
ae13219e | 190 | |
95954743 | 191 | if (lwpid != ti.ti_lid) |
24a09b5f DJ |
192 | { |
193 | warning ("PID mismatch! Expected %ld, got %ld", | |
95954743 | 194 | (long) lwpid, (long) ti.ti_lid); |
24a09b5f DJ |
195 | return 0; |
196 | } | |
ae13219e | 197 | |
24a09b5f DJ |
198 | /* If the new thread ID is zero, a final thread ID will be available |
199 | later. Do not enable thread debugging yet. */ | |
200 | if (ti.ti_tid == 0) | |
201 | return 0; | |
ae13219e | 202 | |
54a0b537 PA |
203 | lwp->thread_known = 1; |
204 | lwp->th = th; | |
f6327dcb | 205 | lwp->thread_handle = ti.ti_tid; |
ae13219e | 206 | |
ae13219e DJ |
207 | return 1; |
208 | } | |
209 | ||
5f7d1694 PP |
210 | /* Attach a thread. Return true on success. */ |
211 | ||
212 | static int | |
213 | attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p) | |
ae13219e | 214 | { |
7ae1a6a6 PA |
215 | struct process_info *proc = current_process (); |
216 | int pid = pid_of (proc); | |
fd79271b | 217 | ptid_t ptid = ptid_t (pid, ti_p->ti_lid, 0); |
54a0b537 | 218 | struct lwp_info *lwp; |
7ae1a6a6 | 219 | int err; |
ae13219e | 220 | |
0d62e5e8 | 221 | if (debug_threads) |
87ce2a04 | 222 | debug_printf ("Attaching to thread %ld (LWP %d)\n", |
d41401ac | 223 | (unsigned long) ti_p->ti_tid, ti_p->ti_lid); |
fd000fb3 | 224 | err = the_linux_target->attach_lwp (ptid); |
7ae1a6a6 | 225 | if (err != 0) |
0d62e5e8 | 226 | { |
50fa3001 | 227 | std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err); |
4d9b86e1 | 228 | |
422186a9 | 229 | warning ("Could not attach to thread %ld (LWP %d): %s", |
4d9b86e1 SM |
230 | (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ()); |
231 | ||
5f7d1694 | 232 | return 0; |
0d62e5e8 DJ |
233 | } |
234 | ||
7ae1a6a6 PA |
235 | lwp = find_lwp_pid (ptid); |
236 | gdb_assert (lwp != NULL); | |
54a0b537 PA |
237 | lwp->thread_known = 1; |
238 | lwp->th = *th_p; | |
f6327dcb | 239 | lwp->thread_handle = ti_p->ti_tid; |
24a09b5f | 240 | |
5f7d1694 PP |
241 | return 1; |
242 | } | |
243 | ||
244 | /* Attach thread if we haven't seen it yet. | |
245 | Increment *COUNTER if we have attached a new thread. | |
246 | Return false on failure. */ | |
247 | ||
248 | static int | |
249 | maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p, | |
250 | int *counter) | |
251 | { | |
252 | struct lwp_info *lwp; | |
253 | ||
f2907e49 | 254 | lwp = find_lwp_pid (ptid_t (ti_p->ti_lid)); |
5f7d1694 PP |
255 | if (lwp != NULL) |
256 | return 1; | |
257 | ||
258 | if (!attach_thread (th_p, ti_p)) | |
259 | return 0; | |
260 | ||
261 | if (counter != NULL) | |
262 | *counter += 1; | |
263 | ||
264 | return 1; | |
0d62e5e8 DJ |
265 | } |
266 | ||
267 | static int | |
268 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) | |
269 | { | |
270 | td_thrinfo_t ti; | |
271 | td_err_e err; | |
fe978cb0 | 272 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
0d62e5e8 | 273 | |
cdbfd419 | 274 | err = thread_db->td_thr_get_info_p (th_p, &ti); |
0d62e5e8 DJ |
275 | if (err != TD_OK) |
276 | error ("Cannot get thread info: %s", thread_db_err_str (err)); | |
277 | ||
a33e3959 PA |
278 | if (ti.ti_lid == -1) |
279 | { | |
280 | /* A thread with kernel thread ID -1 is either a thread that | |
281 | exited and was joined, or a thread that is being created but | |
282 | hasn't started yet, and that is reusing the tcb/stack of a | |
283 | thread that previously exited and was joined. (glibc marks | |
284 | terminated and joined threads with kernel thread ID -1. See | |
285 | glibc PR17707. */ | |
d6c146e9 PA |
286 | if (debug_threads) |
287 | debug_printf ("thread_db: skipping exited and " | |
d41401ac DE |
288 | "joined thread (0x%lx)\n", |
289 | (unsigned long) ti.ti_tid); | |
a33e3959 PA |
290 | return 0; |
291 | } | |
292 | ||
0d62e5e8 DJ |
293 | /* Check for zombies. */ |
294 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) | |
295 | return 0; | |
296 | ||
5f7d1694 PP |
297 | if (!maybe_attach_thread (th_p, &ti, (int *) data)) |
298 | { | |
299 | /* Terminate iteration early: we might be looking at stale data in | |
300 | the inferior. The thread_db_find_new_threads will retry. */ | |
301 | return 1; | |
302 | } | |
0d62e5e8 DJ |
303 | |
304 | return 0; | |
305 | } | |
306 | ||
307 | static void | |
308 | thread_db_find_new_threads (void) | |
309 | { | |
310 | td_err_e err; | |
fbd5db48 | 311 | ptid_t ptid = current_ptid; |
fe978cb0 | 312 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
5f7d1694 | 313 | int loop, iteration; |
0d62e5e8 | 314 | |
ae13219e DJ |
315 | /* This function is only called when we first initialize thread_db. |
316 | First locate the initial thread. If it is not ready for | |
317 | debugging yet, then stop. */ | |
95954743 | 318 | if (find_one_thread (ptid) == 0) |
ae13219e DJ |
319 | return; |
320 | ||
5f7d1694 PP |
321 | /* Require 4 successive iterations which do not find any new threads. |
322 | The 4 is a heuristic: there is an inherent race here, and I have | |
323 | seen that 2 iterations in a row are not always sufficient to | |
324 | "capture" all threads. */ | |
325 | for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration) | |
326 | { | |
327 | int new_thread_count = 0; | |
328 | ||
329 | /* Iterate over all user-space threads to discover new threads. */ | |
330 | err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent, | |
331 | find_new_threads_callback, | |
332 | &new_thread_count, | |
493e2a69 MS |
333 | TD_THR_ANY_STATE, |
334 | TD_THR_LOWEST_PRIORITY, | |
5f7d1694 PP |
335 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); |
336 | if (debug_threads) | |
87ce2a04 DE |
337 | debug_printf ("Found %d threads in iteration %d.\n", |
338 | new_thread_count, iteration); | |
5f7d1694 PP |
339 | |
340 | if (new_thread_count != 0) | |
341 | { | |
342 | /* Found new threads. Restart iteration from beginning. */ | |
343 | loop = -1; | |
344 | } | |
345 | } | |
0d62e5e8 DJ |
346 | if (err != TD_OK) |
347 | error ("Cannot find new threads: %s", thread_db_err_str (err)); | |
348 | } | |
349 | ||
fd500816 DJ |
350 | /* Cache all future symbols that thread_db might request. We can not |
351 | request symbols at arbitrary states in the remote protocol, only | |
352 | when the client tells us that new symbols are available. So when | |
353 | we load the thread library, make sure to check the entire list. */ | |
354 | ||
355 | static void | |
356 | thread_db_look_up_symbols (void) | |
357 | { | |
fe978cb0 | 358 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
cdbfd419 | 359 | const char **sym_list; |
fd500816 DJ |
360 | CORE_ADDR unused; |
361 | ||
cdbfd419 | 362 | for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++) |
9836d6ea PA |
363 | look_up_one_symbol (*sym_list, &unused, 1); |
364 | ||
365 | /* We're not interested in any other libraries loaded after this | |
366 | point, only in symbols in libpthread.so. */ | |
367 | thread_db->all_symbols_looked_up = 1; | |
368 | } | |
369 | ||
370 | int | |
371 | thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp) | |
372 | { | |
fe978cb0 | 373 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
9836d6ea PA |
374 | int may_ask_gdb = !thread_db->all_symbols_looked_up; |
375 | ||
376 | /* If we've passed the call to thread_db_look_up_symbols, then | |
377 | anything not in the cache must not exist; we're not interested | |
378 | in any libraries loaded after that point, only in symbols in | |
379 | libpthread.so. It might not be an appropriate time to look | |
380 | up a symbol, e.g. while we're trying to fetch registers. */ | |
381 | return look_up_one_symbol (name, addrp, may_ask_gdb); | |
fd500816 DJ |
382 | } |
383 | ||
dae5f5cf DJ |
384 | int |
385 | thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset, | |
386 | CORE_ADDR load_module, CORE_ADDR *address) | |
387 | { | |
dae5f5cf DJ |
388 | psaddr_t addr; |
389 | td_err_e err; | |
54a0b537 | 390 | struct lwp_info *lwp; |
0bfdf32f | 391 | struct thread_info *saved_thread; |
cdbfd419 PP |
392 | struct process_info *proc; |
393 | struct thread_db *thread_db; | |
394 | ||
395 | proc = get_thread_process (thread); | |
fe978cb0 | 396 | thread_db = proc->priv->thread_db; |
dae5f5cf | 397 | |
7fe519cb | 398 | /* If the thread layer is not (yet) initialized, fail. */ |
8a4ac37e | 399 | if (thread_db == NULL || !thread_db->all_symbols_looked_up) |
7fe519cb UW |
400 | return TD_ERR; |
401 | ||
5876f503 JK |
402 | /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase |
403 | could work. */ | |
404 | if (thread_db->td_thr_tls_get_addr_p == NULL | |
405 | || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL)) | |
cdbfd419 PP |
406 | return -1; |
407 | ||
54a0b537 PA |
408 | lwp = get_thread_lwp (thread); |
409 | if (!lwp->thread_known) | |
9c80ecd6 | 410 | find_one_thread (thread->id); |
54a0b537 | 411 | if (!lwp->thread_known) |
dae5f5cf DJ |
412 | return TD_NOTHR; |
413 | ||
0bfdf32f GB |
414 | saved_thread = current_thread; |
415 | current_thread = thread; | |
5876f503 JK |
416 | |
417 | if (load_module != 0) | |
418 | { | |
419 | /* Note the cast through uintptr_t: this interface only works if | |
420 | a target address fits in a psaddr_t, which is a host pointer. | |
421 | So a 32-bit debugger can not access 64-bit TLS through this. */ | |
422 | err = thread_db->td_thr_tls_get_addr_p (&lwp->th, | |
423 | (psaddr_t) (uintptr_t) load_module, | |
424 | offset, &addr); | |
425 | } | |
426 | else | |
427 | { | |
428 | /* This code path handles the case of -static -pthread executables: | |
429 | https://sourceware.org/ml/libc-help/2014-03/msg00024.html | |
430 | For older GNU libc r_debug.r_map is NULL. For GNU libc after | |
431 | PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL. | |
432 | The constant number 1 depends on GNU __libc_setup_tls | |
433 | initialization of l_tls_modid to 1. */ | |
434 | err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr); | |
435 | addr = (char *) addr + offset; | |
436 | } | |
437 | ||
0bfdf32f | 438 | current_thread = saved_thread; |
dae5f5cf DJ |
439 | if (err == TD_OK) |
440 | { | |
186947f7 | 441 | *address = (CORE_ADDR) (uintptr_t) addr; |
dae5f5cf DJ |
442 | return 0; |
443 | } | |
444 | else | |
445 | return err; | |
cdbfd419 PP |
446 | } |
447 | ||
f6327dcb KB |
448 | /* See linux-low.h. */ |
449 | ||
450 | bool | |
451 | thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len) | |
452 | { | |
453 | struct thread_db *thread_db; | |
454 | struct lwp_info *lwp; | |
8dc7b443 | 455 | thread_info *thread = find_thread_ptid (ptid); |
f6327dcb KB |
456 | |
457 | if (thread == NULL) | |
458 | return false; | |
459 | ||
460 | thread_db = get_thread_process (thread)->priv->thread_db; | |
461 | ||
462 | if (thread_db == NULL) | |
463 | return false; | |
464 | ||
465 | lwp = get_thread_lwp (thread); | |
466 | ||
9c80ecd6 | 467 | if (!lwp->thread_known && !find_one_thread (thread->id)) |
f6327dcb KB |
468 | return false; |
469 | ||
470 | gdb_assert (lwp->thread_known); | |
471 | ||
472 | *handle = (gdb_byte *) &lwp->thread_handle; | |
473 | *handle_len = sizeof (lwp->thread_handle); | |
474 | return true; | |
475 | } | |
476 | ||
96f15937 PP |
477 | #ifdef USE_LIBTHREAD_DB_DIRECTLY |
478 | ||
479 | static int | |
480 | thread_db_load_search (void) | |
481 | { | |
482 | td_err_e err; | |
9836d6ea | 483 | struct thread_db *tdb; |
96f15937 PP |
484 | struct process_info *proc = current_process (); |
485 | ||
fe978cb0 | 486 | gdb_assert (proc->priv->thread_db == NULL); |
96f15937 | 487 | |
8d749320 | 488 | tdb = XCNEW (struct thread_db); |
fe978cb0 | 489 | proc->priv->thread_db = tdb; |
f9e39928 | 490 | |
9836d6ea | 491 | tdb->td_ta_new_p = &td_ta_new; |
96f15937 PP |
492 | |
493 | /* Attempt to open a connection to the thread library. */ | |
9836d6ea | 494 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
96f15937 PP |
495 | if (err != TD_OK) |
496 | { | |
497 | if (debug_threads) | |
87ce2a04 | 498 | debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err)); |
9836d6ea | 499 | free (tdb); |
fe978cb0 | 500 | proc->priv->thread_db = NULL; |
96f15937 PP |
501 | return 0; |
502 | } | |
503 | ||
9836d6ea PA |
504 | tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr; |
505 | tdb->td_thr_get_info_p = &td_thr_get_info; | |
506 | tdb->td_ta_thr_iter_p = &td_ta_thr_iter; | |
507 | tdb->td_symbol_list_p = &td_symbol_list; | |
96f15937 | 508 | |
96f15937 | 509 | /* These are not essential. */ |
9836d6ea | 510 | tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr; |
5876f503 | 511 | tdb->td_thr_tlsbase_p = &td_thr_tlsbase; |
96f15937 PP |
512 | |
513 | return 1; | |
514 | } | |
515 | ||
516 | #else | |
517 | ||
cdbfd419 PP |
518 | static int |
519 | try_thread_db_load_1 (void *handle) | |
520 | { | |
521 | td_err_e err; | |
9836d6ea | 522 | struct thread_db *tdb; |
cdbfd419 PP |
523 | struct process_info *proc = current_process (); |
524 | ||
fe978cb0 | 525 | gdb_assert (proc->priv->thread_db == NULL); |
cdbfd419 | 526 | |
8d749320 | 527 | tdb = XCNEW (struct thread_db); |
fe978cb0 | 528 | proc->priv->thread_db = tdb; |
f9e39928 | 529 | |
9836d6ea | 530 | tdb->handle = handle; |
cdbfd419 PP |
531 | |
532 | /* Initialize pointers to the dynamic library functions we will use. | |
533 | Essential functions first. */ | |
534 | ||
535 | #define CHK(required, a) \ | |
536 | do \ | |
537 | { \ | |
538 | if ((a) == NULL) \ | |
539 | { \ | |
540 | if (debug_threads) \ | |
87ce2a04 | 541 | debug_printf ("dlsym: %s\n", dlerror ()); \ |
cdbfd419 | 542 | if (required) \ |
9836d6ea PA |
543 | { \ |
544 | free (tdb); \ | |
fe978cb0 | 545 | proc->priv->thread_db = NULL; \ |
9836d6ea PA |
546 | return 0; \ |
547 | } \ | |
cdbfd419 PP |
548 | } \ |
549 | } \ | |
550 | while (0) | |
551 | ||
96e9210f PA |
552 | #define TDB_DLSYM(tdb, func) \ |
553 | tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func) | |
554 | ||
555 | CHK (1, TDB_DLSYM (tdb, td_ta_new)); | |
cdbfd419 PP |
556 | |
557 | /* Attempt to open a connection to the thread library. */ | |
9836d6ea | 558 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
cdbfd419 PP |
559 | if (err != TD_OK) |
560 | { | |
561 | if (debug_threads) | |
87ce2a04 | 562 | debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err)); |
9836d6ea | 563 | free (tdb); |
fe978cb0 | 564 | proc->priv->thread_db = NULL; |
cdbfd419 PP |
565 | return 0; |
566 | } | |
567 | ||
96e9210f PA |
568 | CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr)); |
569 | CHK (1, TDB_DLSYM (tdb, td_thr_get_info)); | |
570 | CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter)); | |
571 | CHK (1, TDB_DLSYM (tdb, td_symbol_list)); | |
cdbfd419 | 572 | |
cdbfd419 | 573 | /* These are not essential. */ |
96e9210f PA |
574 | CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr)); |
575 | CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase)); | |
cdbfd419 PP |
576 | |
577 | #undef CHK | |
96e9210f | 578 | #undef TDB_DLSYM |
cdbfd419 | 579 | |
cdbfd419 PP |
580 | return 1; |
581 | } | |
582 | ||
10e86dd7 DE |
583 | #ifdef HAVE_DLADDR |
584 | ||
cdbfd419 PP |
585 | /* Lookup a library in which given symbol resides. |
586 | Note: this is looking in the GDBSERVER process, not in the inferior. | |
587 | Returns library name, or NULL. */ | |
588 | ||
589 | static const char * | |
590 | dladdr_to_soname (const void *addr) | |
591 | { | |
592 | Dl_info info; | |
593 | ||
594 | if (dladdr (addr, &info) != 0) | |
595 | return info.dli_fname; | |
596 | return NULL; | |
597 | } | |
598 | ||
10e86dd7 DE |
599 | #endif |
600 | ||
cdbfd419 PP |
601 | static int |
602 | try_thread_db_load (const char *library) | |
603 | { | |
604 | void *handle; | |
605 | ||
606 | if (debug_threads) | |
87ce2a04 DE |
607 | debug_printf ("Trying host libthread_db library: %s.\n", |
608 | library); | |
cdbfd419 PP |
609 | handle = dlopen (library, RTLD_NOW); |
610 | if (handle == NULL) | |
611 | { | |
612 | if (debug_threads) | |
87ce2a04 | 613 | debug_printf ("dlopen failed: %s.\n", dlerror ()); |
cdbfd419 PP |
614 | return 0; |
615 | } | |
616 | ||
10e86dd7 | 617 | #ifdef HAVE_DLADDR |
cdbfd419 PP |
618 | if (debug_threads && strchr (library, '/') == NULL) |
619 | { | |
620 | void *td_init; | |
621 | ||
622 | td_init = dlsym (handle, "td_init"); | |
623 | if (td_init != NULL) | |
624 | { | |
625 | const char *const libpath = dladdr_to_soname (td_init); | |
626 | ||
627 | if (libpath != NULL) | |
4eefa7bc | 628 | debug_printf ("Host %s resolved to: %s.\n", library, libpath); |
cdbfd419 PP |
629 | } |
630 | } | |
10e86dd7 | 631 | #endif |
cdbfd419 PP |
632 | |
633 | if (try_thread_db_load_1 (handle)) | |
634 | return 1; | |
635 | ||
636 | /* This library "refused" to work on current inferior. */ | |
637 | dlclose (handle); | |
638 | return 0; | |
639 | } | |
640 | ||
98a5dd13 DE |
641 | /* Handle $sdir in libthread-db-search-path. |
642 | Look for libthread_db in the system dirs, or wherever a plain | |
643 | dlopen(file_without_path) will look. | |
644 | The result is true for success. */ | |
645 | ||
cdbfd419 | 646 | static int |
98a5dd13 DE |
647 | try_thread_db_load_from_sdir (void) |
648 | { | |
649 | return try_thread_db_load (LIBTHREAD_DB_SO); | |
650 | } | |
651 | ||
652 | /* Try to load libthread_db from directory DIR of length DIR_LEN. | |
653 | The result is true for success. */ | |
654 | ||
655 | static int | |
656 | try_thread_db_load_from_dir (const char *dir, size_t dir_len) | |
cdbfd419 PP |
657 | { |
658 | char path[PATH_MAX]; | |
98a5dd13 DE |
659 | |
660 | if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path)) | |
661 | { | |
224c3ddb | 662 | char *cp = (char *) xmalloc (dir_len + 1); |
98a5dd13 DE |
663 | |
664 | memcpy (cp, dir, dir_len); | |
665 | cp[dir_len] = '\0'; | |
666 | warning (_("libthread-db-search-path component too long," | |
667 | " ignored: %s."), cp); | |
668 | free (cp); | |
669 | return 0; | |
670 | } | |
671 | ||
672 | memcpy (path, dir, dir_len); | |
673 | path[dir_len] = '/'; | |
674 | strcpy (path + dir_len + 1, LIBTHREAD_DB_SO); | |
675 | return try_thread_db_load (path); | |
676 | } | |
677 | ||
678 | /* Search libthread_db_search_path for libthread_db which "agrees" | |
679 | to work on current inferior. | |
680 | The result is true for success. */ | |
681 | ||
682 | static int | |
683 | thread_db_load_search (void) | |
684 | { | |
e80aaf61 | 685 | int rc = 0; |
cdbfd419 PP |
686 | |
687 | if (libthread_db_search_path == NULL) | |
688 | libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH); | |
689 | ||
e80aaf61 SM |
690 | std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec |
691 | = dirnames_to_char_ptr_vec (libthread_db_search_path); | |
e6712ff1 | 692 | |
e80aaf61 | 693 | for (const gdb::unique_xmalloc_ptr<char> &this_dir_up : dir_vec) |
cdbfd419 | 694 | { |
e80aaf61 | 695 | char *this_dir = this_dir_up.get (); |
e6712ff1 | 696 | const int pdir_len = sizeof ("$pdir") - 1; |
98a5dd13 DE |
697 | size_t this_dir_len; |
698 | ||
e6712ff1 | 699 | this_dir_len = strlen (this_dir); |
cdbfd419 | 700 | |
e6712ff1 DE |
701 | if (strncmp (this_dir, "$pdir", pdir_len) == 0 |
702 | && (this_dir[pdir_len] == '\0' | |
703 | || this_dir[pdir_len] == '/')) | |
98a5dd13 DE |
704 | { |
705 | /* We don't maintain a list of loaded libraries so we don't know | |
706 | where libpthread lives. We *could* fetch the info, but we don't | |
707 | do that yet. Ignore it. */ | |
708 | } | |
e6712ff1 | 709 | else if (strcmp (this_dir, "$sdir") == 0) |
98a5dd13 DE |
710 | { |
711 | if (try_thread_db_load_from_sdir ()) | |
cdbfd419 | 712 | { |
98a5dd13 | 713 | rc = 1; |
cdbfd419 PP |
714 | break; |
715 | } | |
cdbfd419 | 716 | } |
98a5dd13 | 717 | else |
cdbfd419 | 718 | { |
98a5dd13 DE |
719 | if (try_thread_db_load_from_dir (this_dir, this_dir_len)) |
720 | { | |
721 | rc = 1; | |
722 | break; | |
723 | } | |
cdbfd419 PP |
724 | } |
725 | } | |
cdbfd419 PP |
726 | |
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)) | |
422186a9 | 909 | warning ("Cannot find thread after clone."); |
86299109 | 910 | } |