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