forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux-thread-db.c
2302 lines (1887 loc) · 65.7 KB
/
linux-thread-db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* libthread_db assisted debugging support, generic parts.
Copyright (C) 1999-2015 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include <dlfcn.h>
#include "gdb_proc_service.h"
#include "nat/gdb_thread_db.h"
#include "gdb_vecs.h"
#include "bfd.h"
#include "command.h"
#include "gdbcmd.h"
#include "gdbthread.h"
#include "inferior.h"
#include "infrun.h"
#include "symfile.h"
#include "objfiles.h"
#include "target.h"
#include "regcache.h"
#include "solib.h"
#include "solib-svr4.h"
#include "gdbcore.h"
#include "observer.h"
#include "linux-nat.h"
#include "nat/linux-procfs.h"
#include "nat/linux-ptrace.h"
#include "nat/linux-osdata.h"
#include "auto-load.h"
#include "cli/cli-utils.h"
#include <signal.h>
#include <ctype.h>
#include "nat/linux-namespaces.h"
#include "common-infinity.h"
/* HACKY INFINITY TEST. */
struct infinity_function *i8_map_lwp2thr;
static void
thread_db_i8func_load (struct program_space *pspace,
struct infinity_function *func)
{
i8_map_lwp2thr = func;
}
static void
thread_db_i8func_change (struct program_space *pspace,
struct infinity_function *func)
{
i8_map_lwp2thr = func;
}
static void
thread_db_i8func_unload (struct program_space *pspace,
struct infinity_function *func)
{
i8_map_lwp2thr = NULL;
}
#include "dwarf2.h"
#include "dwarf2expr.h"
static void
i8_print_dwarf_stack (const char *what, struct dwarf_expr_context *ctx)
{
#if 0
int i;
debug_printf ("%s stack:\n", what);
for (i = 0; i < ctx->stack_len; i++)
{
struct value *val = dwarf_expr_fetch (ctx, i);
char *typestr = type_to_string (value_type (val));
debug_printf (" %2d: %12s %15s %s\n", -i,
phex_nz (value_as_address (val), ctx->addr_size),
pulongest (value_as_address (val)), typestr);
xfree (typestr);
}
#endif
}
static void
i8_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t length)
{
read_memory (addr, buf, length);
}
struct dwarf_expr_context_funcs i8_dwarf_expr_funcs = {
NULL, /* read_addr_from_reg */
NULL, /* get_reg_value */
i8_read_mem,
};
static void
i8_map_lwp2thr_test (struct ps_prochandle *ph,
td_err_e lt_err, psaddr_t lt_unique)
{
struct dwarf_expr_context *ctx;
td_err_e i8_err;
psaddr_t i8_unique;
debug_printf ("libthread_db map_lwp2thr(%s) -> %d, %p\n",
pulongest (ptid_get_lwp (ph->ptid)), lt_err, lt_unique);
ctx = new_dwarf_expr_context ();
ctx->gdbarch = target_gdbarch (); /* XXX objfile of the func? */
ctx->addr_size = gdbarch_addr_bit (ctx->gdbarch) / 8;
ctx->baton = ph; /* For DW_OP_GNU_get_thread_area */
ctx->funcs = &i8_dwarf_expr_funcs;
gdb_assert (ctx->stack_len == 0);
dwarf_expr_push_address (ctx,
i8_extract_uint (i8_map_lwp2thr->symbols,
ctx->addr_size), 0);
/* XXX neither of the following are addresses! */
dwarf_expr_push_address (ctx, ptid_get_pid (ph->ptid), 0);
dwarf_expr_push_address (ctx, ptid_get_lwp (ph->ptid), 0);
i8_print_dwarf_stack ("Input", ctx);
dwarf_expr_eval (ctx, i8_map_lwp2thr->code, i8_map_lwp2thr->codesize);
i8_print_dwarf_stack ("Output", ctx);
gdb_assert (ctx->stack_len >= 2);
/* XXX this isn't an address either... */
i8_err = dwarf_expr_fetch_address (ctx, 0);
i8_unique = (psaddr_t) dwarf_expr_fetch_address (ctx, 1);
debug_printf ("and infinity map_lwp2thr(%s) -> \x1B[%dm%d\x1B[0m, "
"\x1B[%dm%p\x1B[0m\n",
pulongest (ptid_get_lwp (ph->ptid)),
i8_err == lt_err ? 32 : 31, i8_err,
i8_unique == lt_unique ? 32 : 31, i8_unique);
}
/* GNU/Linux libthread_db support.
libthread_db is a library, provided along with libpthread.so, which
exposes the internals of the thread library to a debugger. It
allows GDB to find existing threads, new threads as they are
created, thread IDs (usually, the result of pthread_self), and
thread-local variables.
The libthread_db interface originates on Solaris, where it is
both more powerful and more complicated. This implementation
only works for LinuxThreads and NPTL, the two glibc threading
libraries. It assumes that each thread is permanently assigned
to a single light-weight process (LWP).
libthread_db-specific information is stored in the "private" field
of struct thread_info. When the field is NULL we do not yet have
information about the new thread; this could be temporary (created,
but the thread library's data structures do not reflect it yet)
or permanent (created using clone instead of pthread_create).
Process IDs managed by linux-thread-db.c match those used by
linux-nat.c: a common PID for all processes, an LWP ID for each
thread, and no TID. We save the TID in private. Keeping it out
of the ptid_t prevents thread IDs changing when libpthread is
loaded or unloaded. */
static char *libthread_db_search_path;
/* Set to non-zero if thread_db auto-loading is enabled
by the "set auto-load libthread-db" command. */
static int auto_load_thread_db = 1;
/* Returns true if we need to use thread_db thread create/death event
breakpoints to learn about threads. */
static int
thread_db_use_events (void)
{
/* Not necessary if the kernel supports clone events. */
return !linux_supports_traceclone ();
}
/* "show" command for the auto_load_thread_db configuration variable. */
static void
show_auto_load_thread_db (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Auto-loading of inferior specific libthread_db "
"is %s.\n"),
value);
}
static void
set_libthread_db_search_path (char *ignored, int from_tty,
struct cmd_list_element *c)
{
if (*libthread_db_search_path == '\0')
{
xfree (libthread_db_search_path);
libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
}
}
/* If non-zero, print details of libthread_db processing. */
static unsigned int libthread_db_debug;
static void
show_libthread_db_debug (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("libthread-db debugging is %s.\n"), value);
}
/* If we're running on GNU/Linux, we must explicitly attach to any new
threads. */
/* This module's target vector. */
static struct target_ops thread_db_ops;
/* Non-zero if we have determined the signals used by the threads
library. */
static int thread_signals;
static sigset_t thread_stop_set;
static sigset_t thread_print_set;
struct thread_db_info
{
struct thread_db_info *next;
/* Process id this object refers to. */
int pid;
/* Handle from dlopen for libthread_db.so. */
void *handle;
/* Absolute pathname from gdb_realpath to disk file used for dlopen-ing
HANDLE. It may be NULL for system library. */
char *filename;
/* Structure that identifies the child process for the
<proc_service.h> interface. */
struct ps_prochandle proc_handle;
/* Connection to the libthread_db library. */
td_thragent_t *thread_agent;
/* True if we need to apply the workaround for glibc/BZ5983. When
we catch a PTRACE_O_TRACEFORK, and go query the child's thread
list, nptl_db returns the parent's threads in addition to the new
(single) child thread. If this flag is set, we do extra work to
be able to ignore such stale entries. */
int need_stale_parent_threads_check;
/* Location of the thread creation event breakpoint. The code at
this location in the child process will be called by the pthread
library whenever a new thread is created. By setting a special
breakpoint at this location, GDB can detect when a new thread is
created. We obtain this location via the td_ta_event_addr
call. */
CORE_ADDR td_create_bp_addr;
/* Location of the thread death event breakpoint. */
CORE_ADDR td_death_bp_addr;
/* Pointers to the libthread_db functions. */
td_init_ftype *td_init_p;
td_ta_new_ftype *td_ta_new_p;
td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p;
td_ta_thr_iter_ftype *td_ta_thr_iter_p;
td_ta_event_addr_ftype *td_ta_event_addr_p;
td_ta_set_event_ftype *td_ta_set_event_p;
td_ta_clear_event_ftype *td_ta_clear_event_p;
td_ta_event_getmsg_ftype * td_ta_event_getmsg_p;
td_thr_validate_ftype *td_thr_validate_p;
td_thr_get_info_ftype *td_thr_get_info_p;
td_thr_event_enable_ftype *td_thr_event_enable_p;
td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p;
td_thr_tlsbase_ftype *td_thr_tlsbase_p;
};
/* List of known processes using thread_db, and the required
bookkeeping. */
struct thread_db_info *thread_db_list;
static void thread_db_find_new_threads_1 (ptid_t ptid);
static void thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new);
static void check_thread_signals (void);
static void record_thread (struct thread_db_info *info,
struct thread_info *tp,
ptid_t ptid, const td_thrhandle_t *th_p,
const td_thrinfo_t *ti_p);
/* Add the current inferior to the list of processes using libpthread.
Return a pointer to the newly allocated object that was added to
THREAD_DB_LIST. HANDLE is the handle returned by dlopen'ing
LIBTHREAD_DB_SO. */
static struct thread_db_info *
add_thread_db_info (void *handle)
{
struct thread_db_info *info = XCNEW (struct thread_db_info);
info->pid = ptid_get_pid (inferior_ptid);
info->handle = handle;
/* The workaround works by reading from /proc/pid/status, so it is
disabled for core files. */
if (target_has_execution)
info->need_stale_parent_threads_check = 1;
info->next = thread_db_list;
thread_db_list = info;
return info;
}
/* Return the thread_db_info object representing the bookkeeping
related to process PID, if any; NULL otherwise. */
static struct thread_db_info *
get_thread_db_info (int pid)
{
struct thread_db_info *info;
for (info = thread_db_list; info; info = info->next)
if (pid == info->pid)
return info;
return NULL;
}
/* When PID has exited or has been detached, we no longer want to keep
track of it as using libpthread. Call this function to discard
thread_db related info related to PID. Note that this closes
LIBTHREAD_DB_SO's dlopen'ed handle. */
static void
delete_thread_db_info (int pid)
{
struct thread_db_info *info, *info_prev;
info_prev = NULL;
for (info = thread_db_list; info; info_prev = info, info = info->next)
if (pid == info->pid)
break;
if (info == NULL)
return;
if (info->handle != NULL)
dlclose (info->handle);
xfree (info->filename);
if (info_prev)
info_prev->next = info->next;
else
thread_db_list = info->next;
xfree (info);
}
/* Prototypes for local functions. */
static int attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
const td_thrinfo_t *ti_p);
static void detach_thread (ptid_t ptid);
/* Use "struct private_thread_info" to cache thread state. This is
a substantial optimization. */
struct private_thread_info
{
/* Flag set when we see a TD_DEATH event for this thread. */
unsigned int dying:1;
/* Cached thread state. */
td_thrhandle_t th;
thread_t tid;
};
static char *
thread_db_err_str (td_err_e err)
{
static char buf[64];
switch (err)
{
case TD_OK:
return "generic 'call succeeded'";
case TD_ERR:
return "generic error";
case TD_NOTHR:
return "no thread to satisfy query";
case TD_NOSV:
return "no sync handle to satisfy query";
case TD_NOLWP:
return "no LWP to satisfy query";
case TD_BADPH:
return "invalid process handle";
case TD_BADTH:
return "invalid thread handle";
case TD_BADSH:
return "invalid synchronization handle";
case TD_BADTA:
return "invalid thread agent";
case TD_BADKEY:
return "invalid key";
case TD_NOMSG:
return "no event message for getmsg";
case TD_NOFPREGS:
return "FPU register set not available";
case TD_NOLIBTHREAD:
return "application not linked with libthread";
case TD_NOEVENT:
return "requested event is not supported";
case TD_NOCAPAB:
return "capability not available";
case TD_DBERR:
return "debugger service failed";
case TD_NOAPLIC:
return "operation not applicable to";
case TD_NOTSD:
return "no thread-specific data for this thread";
case TD_MALLOC:
return "malloc failed";
case TD_PARTIALREG:
return "only part of register set was written/read";
case TD_NOXREGS:
return "X register set not available for this thread";
#ifdef THREAD_DB_HAS_TD_NOTALLOC
case TD_NOTALLOC:
return "thread has not yet allocated TLS for given module";
#endif
#ifdef THREAD_DB_HAS_TD_VERSION
case TD_VERSION:
return "versions of libpthread and libthread_db do not match";
#endif
#ifdef THREAD_DB_HAS_TD_NOTLS
case TD_NOTLS:
return "there is no TLS segment in the given module";
#endif
default:
snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
return buf;
}
}
/* Return 1 if any threads have been registered. There may be none if
the threading library is not fully initialized yet. */
static int
have_threads_callback (struct thread_info *thread, void *args)
{
int pid = * (int *) args;
if (ptid_get_pid (thread->ptid) != pid)
return 0;
return thread->priv != NULL;
}
static int
have_threads (ptid_t ptid)
{
int pid = ptid_get_pid (ptid);
return iterate_over_threads (have_threads_callback, &pid) != NULL;
}
/* Fetch the user-level thread id of PTID. */
static void
thread_from_lwp (ptid_t ptid)
{
td_thrhandle_t th;
td_thrinfo_t ti;
td_err_e err;
struct thread_db_info *info;
struct thread_info *tp;
/* Just in case td_ta_map_lwp2thr doesn't initialize it completely. */
th.th_unique = 0;
/* This ptid comes from linux-nat.c, which should always fill in the
LWP. */
gdb_assert (ptid_get_lwp (ptid) != 0);
info = get_thread_db_info (ptid_get_pid (ptid));
/* Access an lwp we know is stopped. */
info->proc_handle.ptid = ptid;
err = info->td_ta_map_lwp2thr_p (info->thread_agent, ptid_get_lwp (ptid),
&th);
if (debug_infinity && i8_map_lwp2thr != NULL)
i8_map_lwp2thr_test (&info->proc_handle, err, th.th_unique);
if (err != TD_OK)
error (_("Cannot find user-level thread for LWP %ld: %s"),
ptid_get_lwp (ptid), thread_db_err_str (err));
err = info->td_thr_get_info_p (&th, &ti);
if (err != TD_OK)
error (_("thread_get_info_callback: cannot get thread info: %s"),
thread_db_err_str (err));
/* Fill the cache. */
tp = find_thread_ptid (ptid);
record_thread (info, tp, ptid, &th, &ti);
}
/* See linux-nat.h. */
int
thread_db_notice_clone (ptid_t parent, ptid_t child)
{
td_thrhandle_t th;
td_thrinfo_t ti;
td_err_e err;
struct thread_db_info *info;
info = get_thread_db_info (ptid_get_pid (child));
if (info == NULL)
return 0;
thread_from_lwp (child);
/* If we do not know about the main thread yet, this would be a good
time to find it. */
thread_from_lwp (parent);
return 1;
}
static void *
verbose_dlsym (void *handle, const char *name)
{
void *sym = dlsym (handle, name);
if (sym == NULL)
warning (_("Symbol \"%s\" not found in libthread_db: %s"),
name, dlerror ());
return sym;
}
static td_err_e
enable_thread_event (td_event_e event, CORE_ADDR *bp)
{
td_notify_t notify;
td_err_e err;
struct thread_db_info *info;
info = get_thread_db_info (ptid_get_pid (inferior_ptid));
/* Access an lwp we know is stopped. */
info->proc_handle.ptid = inferior_ptid;
/* Get the breakpoint address for thread EVENT. */
err = info->td_ta_event_addr_p (info->thread_agent, event, ¬ify);
if (err != TD_OK)
return err;
/* Set up the breakpoint. */
gdb_assert (exec_bfd);
(*bp) = (gdbarch_convert_from_func_ptr_addr
(target_gdbarch (),
/* Do proper sign extension for the target. */
(bfd_get_sign_extend_vma (exec_bfd) > 0
? (CORE_ADDR) (intptr_t) notify.u.bptaddr
: (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
¤t_target));
create_thread_event_breakpoint (target_gdbarch (), *bp);
return TD_OK;
}
/* Verify inferior's '\0'-terminated symbol VER_SYMBOL starts with "%d.%d" and
return 1 if this version is lower (and not equal) to
VER_MAJOR_MIN.VER_MINOR_MIN. Return 0 in all other cases. */
static int
inferior_has_bug (const char *ver_symbol, int ver_major_min, int ver_minor_min)
{
struct bound_minimal_symbol version_msym;
CORE_ADDR version_addr;
char *version;
int err, got, retval = 0;
version_msym = lookup_minimal_symbol (ver_symbol, NULL, NULL);
if (version_msym.minsym == NULL)
return 0;
version_addr = BMSYMBOL_VALUE_ADDRESS (version_msym);
got = target_read_string (version_addr, &version, 32, &err);
if (err == 0 && memchr (version, 0, got) == &version[got -1])
{
int major, minor;
retval = (sscanf (version, "%d.%d", &major, &minor) == 2
&& (major < ver_major_min
|| (major == ver_major_min && minor < ver_minor_min)));
}
xfree (version);
return retval;
}
static void
enable_thread_event_reporting (void)
{
td_thr_events_t events;
td_err_e err;
struct thread_db_info *info;
info = get_thread_db_info (ptid_get_pid (inferior_ptid));
/* We cannot use the thread event reporting facility if these
functions aren't available. */
if (info->td_ta_event_addr_p == NULL
|| info->td_ta_set_event_p == NULL
|| info->td_ta_event_getmsg_p == NULL
|| info->td_thr_event_enable_p == NULL)
return;
/* Set the process wide mask saying which events we're interested in. */
td_event_emptyset (&events);
td_event_addset (&events, TD_CREATE);
/* There is a bug fixed between linuxthreads 2.1.3 and 2.2 by
commit 2e4581e4fba917f1779cd0a010a45698586c190a
* manager.c (pthread_exited): Correctly report event as TD_REAP
instead of TD_DEATH. Fix comments.
where event reporting facility is broken for TD_DEATH events,
so don't enable it if we have glibc but a lower version. */
if (!inferior_has_bug ("__linuxthreads_version", 2, 2))
td_event_addset (&events, TD_DEATH);
err = info->td_ta_set_event_p (info->thread_agent, &events);
if (err != TD_OK)
{
warning (_("Unable to set global thread event mask: %s"),
thread_db_err_str (err));
return;
}
/* Delete previous thread event breakpoints, if any. */
remove_thread_event_breakpoints ();
info->td_create_bp_addr = 0;
info->td_death_bp_addr = 0;
/* Set up the thread creation event. */
err = enable_thread_event (TD_CREATE, &info->td_create_bp_addr);
if (err != TD_OK)
{
warning (_("Unable to get location for thread creation breakpoint: %s"),
thread_db_err_str (err));
return;
}
/* Set up the thread death event. */
err = enable_thread_event (TD_DEATH, &info->td_death_bp_addr);
if (err != TD_OK)
{
warning (_("Unable to get location for thread death breakpoint: %s"),
thread_db_err_str (err));
return;
}
}
/* Similar as thread_db_find_new_threads_1, but try to silently ignore errors
if appropriate.
Return 1 if the caller should abort libthread_db initialization. Return 0
otherwise. */
static int
thread_db_find_new_threads_silently (ptid_t ptid)
{
TRY
{
thread_db_find_new_threads_2 (ptid, 1);
}
CATCH (except, RETURN_MASK_ERROR)
{
if (libthread_db_debug)
exception_fprintf (gdb_stdlog, except,
"Warning: thread_db_find_new_threads_silently: ");
/* There is a bug fixed between nptl 2.6.1 and 2.7 by
commit 7d9d8bd18906fdd17364f372b160d7ab896ce909
where calls to td_thr_get_info fail with TD_ERR for statically linked
executables if td_thr_get_info is called before glibc has initialized
itself.
If the nptl bug is NOT present in the inferior and still thread_db
reports an error return 1. It means the inferior has corrupted thread
list and GDB should fall back only to LWPs.
If the nptl bug is present in the inferior return 0 to silently ignore
such errors, and let gdb enumerate threads again later. In such case
GDB cannot properly display LWPs if the inferior thread list is
corrupted. For core files it does not apply, no 'later enumeration'
is possible. */
if (!target_has_execution || !inferior_has_bug ("nptl_version", 2, 7))
{
exception_fprintf (gdb_stderr, except,
_("Warning: couldn't activate thread debugging "
"using libthread_db: "));
return 1;
}
}
END_CATCH
return 0;
}
/* Lookup a library in which given symbol resides.
Note: this is looking in GDB process, not in the inferior.
Returns library name, or NULL. */
static const char *
dladdr_to_soname (const void *addr)
{
Dl_info info;
if (dladdr (addr, &info) != 0)
return info.dli_fname;
return NULL;
}
/* Attempt to initialize dlopen()ed libthread_db, described by INFO.
Return 1 on success.
Failure could happen if libthread_db does not have symbols we expect,
or when it refuses to work with the current inferior (e.g. due to
version mismatch between libthread_db and libpthread). */
static int
try_thread_db_load_1 (struct thread_db_info *info)
{
td_err_e err;
/* Initialize pointers to the dynamic library functions we will use.
Essential functions first. */
#define TDB_VERBOSE_DLSYM(info, func) \
info->func ## _p = (func ## _ftype *) verbose_dlsym (info->handle, #func)
#define TDB_DLSYM(info, func) \
info->func ## _p = (func ## _ftype *) dlsym (info->handle, #func)
#define CHK(a) \
do \
{ \
if ((a) == NULL) \
return 0; \
} while (0)
CHK (TDB_VERBOSE_DLSYM (info, td_init));
err = info->td_init_p ();
if (err != TD_OK)
{
warning (_("Cannot initialize libthread_db: %s"),
thread_db_err_str (err));
return 0;
}
CHK (TDB_VERBOSE_DLSYM (info, td_ta_new));
/* Initialize the structure that identifies the child process. */
info->proc_handle.ptid = inferior_ptid;
/* Now attempt to open a connection to the thread library. */
err = info->td_ta_new_p (&info->proc_handle, &info->thread_agent);
if (err != TD_OK)
{
if (libthread_db_debug)
fprintf_unfiltered (gdb_stdlog, _("td_ta_new failed: %s\n"),
thread_db_err_str (err));
else
switch (err)
{
case TD_NOLIBTHREAD:
#ifdef THREAD_DB_HAS_TD_VERSION
case TD_VERSION:
#endif
/* The errors above are not unexpected and silently ignored:
they just mean we haven't found correct version of
libthread_db yet. */
break;
default:
warning (_("td_ta_new failed: %s"), thread_db_err_str (err));
}
return 0;
}
/* These are essential. */
CHK (TDB_VERBOSE_DLSYM (info, td_ta_map_lwp2thr));
CHK (TDB_VERBOSE_DLSYM (info, td_ta_thr_iter));
CHK (TDB_VERBOSE_DLSYM (info, td_thr_validate));
CHK (TDB_VERBOSE_DLSYM (info, td_thr_get_info));
/* These are not essential. */
TDB_DLSYM (info, td_ta_event_addr);
TDB_DLSYM (info, td_ta_set_event);
TDB_DLSYM (info, td_ta_clear_event);
TDB_DLSYM (info, td_ta_event_getmsg);
TDB_DLSYM (info, td_thr_event_enable);
TDB_DLSYM (info, td_thr_tls_get_addr);
TDB_DLSYM (info, td_thr_tlsbase);
#undef TDB_VERBOSE_DLSYM
#undef TDB_DLSYM
#undef CHK
/* It's best to avoid td_ta_thr_iter if possible. That walks data
structures in the inferior's address space that may be corrupted,
or, if the target is running, may change while we walk them. If
there's execution (and /proc is mounted), then we're already
attached to all LWPs. Use thread_from_lwp, which uses
td_ta_map_lwp2thr instead, which does not walk the thread list.
td_ta_map_lwp2thr uses ps_get_thread_area, but we can't use that
currently on core targets, as it uses ptrace directly. */
if (target_has_execution
&& linux_proc_task_list_dir_exists (ptid_get_pid (inferior_ptid)))
{
struct lwp_info *lp;
int pid = ptid_get_pid (inferior_ptid);
linux_stop_and_wait_all_lwps ();
ALL_LWPS (lp)
if (ptid_get_pid (lp->ptid) == pid)
thread_from_lwp (lp->ptid);
linux_unstop_all_lwps ();
}
else if (thread_db_find_new_threads_silently (inferior_ptid) != 0)
{
/* Even if libthread_db initializes, if the thread list is
corrupted, we'd not manage to list any threads. Better reject this
thread_db, and fall back to at least listing LWPs. */
return 0;
}
printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
if (*libthread_db_search_path || libthread_db_debug)
{
struct ui_file *file;
const char *library;
library = dladdr_to_soname (*info->td_ta_new_p);
if (library == NULL)
library = LIBTHREAD_DB_SO;
/* If we'd print this to gdb_stdout when debug output is
disabled, still print it to gdb_stdout if debug output is
enabled. User visible output should not depend on debug
settings. */
file = *libthread_db_search_path != '\0' ? gdb_stdout : gdb_stdlog;
fprintf_unfiltered (file, _("Using host libthread_db library \"%s\".\n"),
library);
}
/* The thread library was detected. Activate the thread_db target
if this is the first process using it. */
if (thread_db_list->next == NULL)
push_target (&thread_db_ops);
/* Enable event reporting, but not when debugging a core file. */
if (target_has_execution && thread_db_use_events ())
enable_thread_event_reporting ();
return 1;
}
/* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute,
relative, or just LIBTHREAD_DB. */
static int
try_thread_db_load (const char *library, int check_auto_load_safe)
{
void *handle;
struct thread_db_info *info;
if (libthread_db_debug)
fprintf_unfiltered (gdb_stdlog,
_("Trying host libthread_db library: %s.\n"),
library);
if (check_auto_load_safe)
{
if (access (library, R_OK) != 0)
{
/* Do not print warnings by file_is_auto_load_safe if the library does
not exist at this place. */
if (libthread_db_debug)
fprintf_unfiltered (gdb_stdlog, _("open failed: %s.\n"),
safe_strerror (errno));
return 0;
}
if (!file_is_auto_load_safe (library, _("auto-load: Loading libthread-db "
"library \"%s\" from explicit "
"directory.\n"),
library))
return 0;
}
handle = dlopen (library, RTLD_NOW);
if (handle == NULL)
{
if (libthread_db_debug)
fprintf_unfiltered (gdb_stdlog, _("dlopen failed: %s.\n"), dlerror ());
return 0;
}
if (libthread_db_debug && strchr (library, '/') == NULL)
{
void *td_init;
td_init = dlsym (handle, "td_init");
if (td_init != NULL)
{
const char *const libpath = dladdr_to_soname (td_init);
if (libpath != NULL)
fprintf_unfiltered (gdb_stdlog, _("Host %s resolved to: %s.\n"),
library, libpath);
}
}
info = add_thread_db_info (handle);
/* Do not save system library name, that one is always trusted. */
if (strchr (library, '/') != NULL)
info->filename = gdb_realpath (library);
if (try_thread_db_load_1 (info))
return 1;
/* This library "refused" to work on current inferior. */
delete_thread_db_info (ptid_get_pid (inferior_ptid));
return 0;
}
/* Subroutine of try_thread_db_load_from_pdir to simplify it.
Try loading libthread_db in directory(OBJ)/SUBDIR.
SUBDIR may be NULL. It may also be something like "../lib64".
The result is true for success. */
static int
try_thread_db_load_from_pdir_1 (struct objfile *obj, const char *subdir)
{
struct cleanup *cleanup;
char *path, *cp;
int result;
const char *obj_name = objfile_name (obj);
if (obj_name[0] != '/')
{
warning (_("Expected absolute pathname for libpthread in the"
" inferior, but got %s."), obj_name);
return 0;
}
path = xmalloc (strlen (obj_name) + (subdir ? strlen (subdir) + 1 : 0)
+ 1 + strlen (LIBTHREAD_DB_SO) + 1);
cleanup = make_cleanup (xfree, path);
strcpy (path, obj_name);
cp = strrchr (path, '/');
/* This should at minimum hit the first character. */
gdb_assert (cp != NULL);
cp[1] = '\0';
if (subdir != NULL)
{