-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebuginfo.c
More file actions
5214 lines (4602 loc) · 179 KB
/
debuginfo.c
File metadata and controls
5214 lines (4602 loc) · 179 KB
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
/* -*- mode: C; c-basic-offset: 3; -*- */
/*--------------------------------------------------------------------*/
/*--- Top level management of symbols and debugging information. ---*/
/*--- debuginfo.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Valgrind, a dynamic binary instrumentation
framework.
Copyright (C) 2000-2017 Julian Seward
jseward@acm.org
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 2 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/>.
The GNU General Public License is contained in the file COPYING.
*/
#include "pub_core_basics.h"
#include "pub_core_vki.h"
#include "pub_core_threadstate.h"
#include "pub_core_debuginfo.h" /* self */
#include "pub_core_debuglog.h"
#include "pub_core_demangle.h"
#include "pub_core_libcbase.h"
#include "pub_core_libcassert.h"
#include "pub_core_libcprint.h"
#include "pub_core_libcfile.h"
#include "pub_core_libcproc.h" // VG_(getenv)
#include "pub_core_rangemap.h"
#include "pub_core_seqmatch.h"
#include "pub_core_options.h"
#include "pub_core_redir.h" // VG_(redir_notify_{new,delete}_SegInfo)
#include "pub_core_aspacemgr.h"
#include "pub_core_machine.h" // VG_PLAT_USES_PPCTOC
#include "pub_core_xarray.h"
#include "pub_core_oset.h"
#include "pub_core_execontext.h"
#include "pub_core_stacktrace.h" // VG_(get_StackTrace) XXX: circular dependency
#include "pub_core_ume.h"
#include "priv_misc.h" /* dinfo_zalloc/free */
#include "priv_image.h"
#include "priv_d3basics.h" /* ML_(pp_GX) */
#include "priv_tytypes.h"
#include "priv_storage.h"
#include "priv_readdwarf.h"
#if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
# include "priv_readelf.h"
# include "priv_readdwarf3.h"
# include "priv_readpdb.h"
#elif defined(VGO_darwin)
# include "priv_readmacho.h"
# include "priv_readpdb.h"
#endif
#if defined(VGO_freebsd)
#include "pub_core_clientstate.h"
#endif
/* Set this to 1 to enable somewhat minimal debug printing for the
debuginfo-epoch machinery. */
#define DEBUG_EPOCHS 0
/*------------------------------------------------------------*/
/*--- The _svma / _avma / _image / _bias naming scheme ---*/
/*------------------------------------------------------------*/
/* JRS 11 Jan 07: I find the different kinds of addresses involved in
debuginfo reading confusing. Recently I arrived at some
terminology which makes it clearer (to me, at least). There are 3
kinds of address used in the debuginfo reading process:
stated VMAs - the address where (eg) a .so says a symbol is, that
is, what it tells you if you consider the .so in
isolation
actual VMAs - the address where (eg) said symbol really wound up
after the .so was mapped into memory
image addresses - pointers into the copy of the .so (etc)
transiently mmaped aboard whilst we read its info
Additionally I use the term 'bias' to denote the difference
between stated and actual VMAs for a given entity.
This terminology is not used consistently, but a start has been
made. readelf.c and the call-frame info reader in readdwarf.c now
use it. Specifically, various variables and structure fields have
been annotated with _avma / _svma / _image / _bias. In places _img
is used instead of _image for the sake of brevity.
*/
/*------------------------------------------------------------*/
/*--- fwdses ---*/
/*------------------------------------------------------------*/
static void caches__invalidate (void);
/*------------------------------------------------------------*/
/*--- Epochs ---*/
/*------------------------------------------------------------*/
/* The DebugInfo epoch is incremented every time we either load debuginfo in
response to an object mapping, or an existing DebugInfo becomes
non-current (or will be discarded) due to an object unmap. By storing,
in each DebugInfo, the first and last epoch for which it is valid, we can
unambiguously identify the set of DebugInfos which should be used to
provide metadata for a code or data address, provided we know the epoch
to which that address pertains.
Note, this isn't the same as the "handle_counter" below. That only
advances when new DebugInfos are created. "current_epoch" advances both
at DebugInfo created and destruction-or-making-non-current.
*/
// The value zero is reserved for indicating an invalid epoch number.
static UInt current_epoch = 1;
inline DiEpoch VG_(current_DiEpoch) ( void ) {
DiEpoch dep; dep.n = current_epoch; return dep;
}
static void advance_current_DiEpoch ( const HChar* msg ) {
current_epoch++;
if (DEBUG_EPOCHS)
VG_(printf)("Advancing current epoch to %u due to %s\n",
current_epoch, msg);
}
static inline Bool eq_DiEpoch ( DiEpoch dep1, DiEpoch dep2 ) {
return dep1.n == dep2.n && /*neither is invalid*/dep1.n != 0;
}
// Is this DebugInfo currently "allocated" (pre-use state, only FSM active) ?
static inline Bool is_DebugInfo_allocated ( const DebugInfo* di )
{
if (is_DiEpoch_INVALID(di->first_epoch)
&& is_DiEpoch_INVALID(di->last_epoch)) {
return True;
} else {
return False;
}
}
// Is this DebugInfo currently "active" (valid for the current epoch) ?
static inline Bool is_DebugInfo_active ( const DebugInfo* di )
{
if (!is_DiEpoch_INVALID(di->first_epoch)
&& is_DiEpoch_INVALID(di->last_epoch)) {
// Yes it is active. Sanity check ..
vg_assert(di->first_epoch.n <= current_epoch);
return True;
} else {
return False;
}
}
// Is this DebugInfo currently "archived" ?
static inline Bool is_DebugInfo_archived ( const DebugInfo* di )
{
if (!is_DiEpoch_INVALID(di->first_epoch)
&& !is_DiEpoch_INVALID(di->last_epoch)) {
// Yes it is archived. Sanity checks ..
vg_assert(di->first_epoch.n <= di->last_epoch.n);
vg_assert(di->last_epoch.n <= current_epoch);
return True;
} else {
return False;
}
}
// Is this DebugInfo valid for the specified epoch?
static inline Bool is_DI_valid_for_epoch ( const DebugInfo* di, DiEpoch ep )
{
// Stay sane
vg_assert(ep.n > 0 && ep.n <= current_epoch);
Bool first_valid = !is_DiEpoch_INVALID(di->first_epoch);
Bool last_valid = !is_DiEpoch_INVALID(di->last_epoch);
if (first_valid) {
if (last_valid) {
// Both valid. di is in Archived state.
return di->first_epoch.n <= ep.n && ep.n <= di->last_epoch.n;
} else {
// First is valid, last is invalid. di is in Active state.
return di->first_epoch.n <= ep.n;
}
} else {
vg_assert (!last_valid); // First invalid, last valid is a bad state.
// Neither is valid. di is in Allocated state.
return False;
}
}
static inline UInt ROL32 ( UInt x, UInt n )
{
return (x << n) | (x >> (32-n));
}
/*------------------------------------------------------------*/
/*--- Root structure ---*/
/*------------------------------------------------------------*/
/* The root structure for the entire debug info system. It is a
linked list of DebugInfos. */
static DebugInfo* debugInfo_list = NULL;
/* Find 'di' in the debugInfo_list and move it one step closer to the
front of the list, so as to make subsequent searches for it
cheaper. When used in a controlled way, makes a major improvement
in some DebugInfo-search-intensive situations, most notably stack
unwinding on amd64-linux. */
static void move_DebugInfo_one_step_forward ( DebugInfo* di )
{
DebugInfo *di0, *di1, *di2;
if (di == debugInfo_list)
return; /* already at head of list */
vg_assert(di != NULL);
di0 = debugInfo_list;
di1 = NULL;
di2 = NULL;
while (True) {
if (di0 == NULL || di0 == di) break;
di2 = di1;
di1 = di0;
di0 = di0->next;
}
vg_assert(di0 == di);
if (di0 != NULL && di1 != NULL && di2 != NULL) {
DebugInfo* tmp;
/* di0 points to di, di1 to its predecessor, and di2 to di1's
predecessor. Swap di0 and di1, that is, move di0 one step
closer to the start of the list. */
vg_assert(di2->next == di1);
vg_assert(di1->next == di0);
tmp = di0->next;
di2->next = di0;
di0->next = di1;
di1->next = tmp;
}
else
if (di0 != NULL && di1 != NULL && di2 == NULL) {
/* it's second in the list. */
vg_assert(debugInfo_list == di1);
vg_assert(di1->next == di0);
di1->next = di0->next;
di0->next = di1;
debugInfo_list = di0;
}
}
// Debugging helper for epochs
static void show_epochs ( const HChar* msg )
{
if (DEBUG_EPOCHS) {
DebugInfo* di;
VG_(printf)("\nDebugInfo epoch display, requested by \"%s\"\n", msg);
VG_(printf)(" Current epoch (note: 0 means \"invalid epoch\") = %u\n",
current_epoch);
for (di = debugInfo_list; di; di = di->next) {
VG_(printf)(" [di=%p] first %u last %u %s\n",
di, di->first_epoch.n, di->last_epoch.n, di->fsm.filename);
}
VG_(printf)("\n");
}
}
/*------------------------------------------------------------*/
/*--- Notification (acquire/discard) helpers ---*/
/*------------------------------------------------------------*/
/* Gives out unique abstract handles for allocated DebugInfos. See
comment in priv_storage.h, declaration of struct _DebugInfo, for
details. */
static ULong handle_counter = 1;
/* Allocate and zero out a new DebugInfo record. */
static
DebugInfo* alloc_DebugInfo( const HChar* filename )
{
Bool traceme;
DebugInfo* di;
vg_assert(filename);
di = ML_(dinfo_zalloc)("di.debuginfo.aDI.1", sizeof(DebugInfo));
di->handle = handle_counter++;
di->first_epoch = DiEpoch_INVALID();
di->last_epoch = DiEpoch_INVALID();
di->fsm.filename = ML_(dinfo_strdup)("di.debuginfo.aDI.2", filename);
di->fsm.maps = VG_(newXA)(
ML_(dinfo_zalloc), "di.debuginfo.aDI.3",
ML_(dinfo_free), sizeof(DebugInfoMapping));
/* Everything else -- pointers, sizes, arrays -- is zeroed by
ML_(dinfo_zalloc). Now set up the debugging-output flags. */
traceme
= VG_(string_match)( VG_(clo_trace_symtab_patt), filename );
if (traceme) {
di->trace_symtab = VG_(clo_trace_symtab);
di->trace_cfi = VG_(clo_trace_cfi);
di->ddump_syms = VG_(clo_debug_dump_syms);
di->ddump_line = VG_(clo_debug_dump_line);
di->ddump_frames = VG_(clo_debug_dump_frames);
}
return di;
}
/* Free a DebugInfo, and also all the stuff hanging off it. */
static void free_DebugInfo ( DebugInfo* di )
{
Word i, j, n;
TyEnt* ent;
GExpr* gexpr;
vg_assert(di != NULL);
if (di->fsm.maps) VG_(deleteXA)(di->fsm.maps);
if (di->fsm.filename) ML_(dinfo_free)(di->fsm.filename);
if (di->fsm.dbgname) ML_(dinfo_free)(di->fsm.dbgname);
if (di->soname) ML_(dinfo_free)(di->soname);
if (di->loctab) ML_(dinfo_free)(di->loctab);
if (di->loctab_fndn_ix) ML_(dinfo_free)(di->loctab_fndn_ix);
if (di->inltab) ML_(dinfo_free)(di->inltab);
if (di->cfsi_base) ML_(dinfo_free)(di->cfsi_base);
if (di->cfsi_m_ix) ML_(dinfo_free)(di->cfsi_m_ix);
if (di->cfsi_rd) ML_(dinfo_free)(di->cfsi_rd);
if (di->cfsi_m_pool) VG_(deleteDedupPA)(di->cfsi_m_pool);
if (di->cfsi_exprs) VG_(deleteXA)(di->cfsi_exprs);
if (di->fpo) ML_(dinfo_free)(di->fpo);
if (di->symtab) {
/* We have to visit all the entries so as to free up any
sec_names arrays that might exist. */
n = di->symtab_used;
for (i = 0; i < n; i++) {
DiSym* sym = &di->symtab[i];
if (sym->sec_names)
ML_(dinfo_free)(sym->sec_names);
}
/* and finally .. */
ML_(dinfo_free)(di->symtab);
}
if (di->strpool)
VG_(deleteDedupPA) (di->strpool);
if (di->fndnpool)
VG_(deleteDedupPA) (di->fndnpool);
/* Delete the two admin arrays. These lists exist primarily so
that we can visit each object exactly once when we need to
delete them. */
if (di->admin_tyents) {
n = VG_(sizeXA)(di->admin_tyents);
for (i = 0; i < n; i++) {
ent = (TyEnt*)VG_(indexXA)(di->admin_tyents, i);
/* Dump anything hanging off this ent */
ML_(TyEnt__make_EMPTY)(ent);
}
VG_(deleteXA)(di->admin_tyents);
di->admin_tyents = NULL;
}
if (di->admin_gexprs) {
n = VG_(sizeXA)(di->admin_gexprs);
for (i = 0; i < n; i++) {
gexpr = *(GExpr**)VG_(indexXA)(di->admin_gexprs, i);
ML_(dinfo_free)(gexpr);
}
VG_(deleteXA)(di->admin_gexprs);
di->admin_gexprs = NULL;
}
/* Dump the variable info. This is kinda complex: we must take
care not to free items which reside in either the admin lists
(as we have just freed them) or which reside in the DebugInfo's
string table. */
if (di->varinfo) {
for (i = 0; i < VG_(sizeXA)(di->varinfo); i++) {
OSet* scope = *(OSet**)VG_(indexXA)(di->varinfo, i);
if (!scope) continue;
/* iterate over all entries in 'scope' */
VG_(OSetGen_ResetIter)(scope);
while (True) {
DiAddrRange* arange = VG_(OSetGen_Next)(scope);
if (!arange) break;
/* for each var in 'arange' */
vg_assert(arange->vars);
for (j = 0; j < VG_(sizeXA)( arange->vars ); j++) {
DiVariable* var = (DiVariable*)VG_(indexXA)(arange->vars,j);
vg_assert(var);
/* Nothing to free in var: all the pointer fields refer
to stuff either on an admin list, or in
.strpool */
}
VG_(deleteXA)(arange->vars);
/* Don't free arange itself, as OSetGen_Destroy does
that */
}
VG_(OSetGen_Destroy)(scope);
}
VG_(deleteXA)(di->varinfo);
}
ML_(dinfo_free)(di);
}
/* 'di' is a member of debugInfo_list. Find it, and either (remove it from
the list and free all storage reachable from it) or archive it.
Notify m_redir that this removal/archiving has happened.
Note that 'di' can't be archived. Is a DebugInfo is archived then we
want to hold on to it forever. This is asserted for.
Note also, we don't advance the current epoch here. That's the
responsibility of some (non-immediate) caller.
*/
static void discard_or_archive_DebugInfo ( DebugInfo* di )
{
/* di->have_dinfo can be False when an object is mapped "ro"
and then unmapped before the debug info is loaded.
In other words, debugInfo_list might contain many di that have
no OS mappings, even if their fsm.maps still contain mappings.
Such (left over) mappings can overlap with real mappings.
Search for FSMMAPSNOTCLEANEDUP: below for more details. */
/* If a di has no dinfo, we can discard even if VG_(clo_keep_debuginfo). */
const Bool archive = VG_(clo_keep_debuginfo) && di->have_dinfo;
DebugInfo** prev_next_ptr = &debugInfo_list;
DebugInfo* curr = debugInfo_list;
/* If di->have_dinfo, then it must be active! */
vg_assert(!di->have_dinfo || is_DebugInfo_active(di));
while (curr) {
if (curr == di) {
/* Found it; (remove from list and free it), or archive it. */
if (VG_(clo_verbosity) > 1 || VG_(clo_trace_redir))
VG_(dmsg)("%s syms at %#lx-%#lx in %s (have_dinfo %d)\n",
archive ? "Archiving" : "Discarding",
di->text_avma,
di->text_avma + di->text_size,
curr->fsm.filename ? curr->fsm.filename
: "???",
curr->have_dinfo);
vg_assert(*prev_next_ptr == curr);
if (!archive) {
*prev_next_ptr = curr->next;
}
if (curr->have_dinfo) {
VG_(redir_notify_delete_DebugInfo)( curr );
}
if (archive) {
/* Adjust the epoch markers appropriately. */
di->last_epoch = VG_(current_DiEpoch)();
VG_(archive_ExeContext_in_range) (di->last_epoch,
di->text_avma, di->text_size);
vg_assert(is_DebugInfo_archived(di));
} else {
free_DebugInfo(curr);
}
return;
}
prev_next_ptr = &curr->next;
curr = curr->next;
}
/* Not found. */
}
/* Repeatedly scan debugInfo_list, looking for DebugInfos with text
AVMAs intersecting [start,start+length), and call discard_DebugInfo
to get rid of them. This modifies the list, hence the multiple
iterations. Returns True iff any such DebugInfos were found.
*/
static Bool discard_syms_in_range ( Addr start, SizeT length )
{
Bool anyFound = False;
Bool found;
DebugInfo* curr;
while (True) {
found = False;
curr = debugInfo_list;
while (True) {
if (curr == NULL)
break;
if (is_DebugInfo_archived(curr)
|| !curr->text_present
|| (curr->text_present
&& curr->text_size > 0
&& (start+length - 1 < curr->text_avma
|| curr->text_avma + curr->text_size - 1 < start))) {
/* no overlap */
} else {
found = True;
break;
}
curr = curr->next;
}
if (!found) break;
anyFound = True;
discard_or_archive_DebugInfo( curr );
}
return anyFound;
}
/* Does [s1,+len1) overlap [s2,+len2) ? Note: does not handle
wraparound at the end of the address space -- just asserts in that
case. */
static Bool ranges_overlap (Addr s1, SizeT len1, Addr s2, SizeT len2 )
{
Addr e1, e2;
if (len1 == 0 || len2 == 0)
return False;
e1 = s1 + len1 - 1;
e2 = s2 + len2 - 1;
/* Assert that we don't have wraparound. If we do it would imply
that file sections are getting mapped around the end of the
address space, which sounds unlikely. */
vg_assert(s1 <= e1);
vg_assert(s2 <= e2);
if (e1 < s2 || e2 < s1) return False;
return True;
}
/* Do the basic mappings of the two DebugInfos overlap in any way? */
static Bool do_DebugInfos_overlap ( const DebugInfo* di1, const DebugInfo* di2 )
{
Word i, j;
vg_assert(di1);
vg_assert(di2);
for (i = 0; i < VG_(sizeXA)(di1->fsm.maps); i++) {
const DebugInfoMapping* map1 = VG_(indexXA)(di1->fsm.maps, i);
for (j = 0; j < VG_(sizeXA)(di2->fsm.maps); j++) {
const DebugInfoMapping* map2 = VG_(indexXA)(di2->fsm.maps, j);
if (ranges_overlap(map1->avma, map1->size, map2->avma, map2->size)) {
return True;
}
}
}
return False;
}
/* Discard or archive all elements of debugInfo_list whose .mark bit is set.
*/
static void discard_or_archive_marked_DebugInfos ( void )
{
DebugInfo* curr;
while (True) {
curr = debugInfo_list;
while (True) {
if (!curr)
break;
if (curr->mark)
break;
curr = curr->next;
}
if (!curr) break;
// If |curr| is going to remain in the debugInfo_list, and merely change
// state, then we need to clear its mark bit so we don't subsequently
// try to archive it again later. Possibly related to #393146.
if (VG_(clo_keep_debuginfo))
curr->mark = False;
discard_or_archive_DebugInfo( curr );
}
}
/* Discard any elements of debugInfo_list which overlap with diRef.
Clearly diRef must have its mapping information set to something sane. */
static void discard_DebugInfos_which_overlap_with ( DebugInfo* diRef )
{
vg_assert(is_DebugInfo_allocated(diRef));
DebugInfo* di;
/* Mark all the DebugInfos in debugInfo_list that need to be
deleted. First, clear all the mark bits; then set them if they
overlap with siRef. Since siRef itself is in this list we at
least expect its own mark bit to be set. */
for (di = debugInfo_list; di; di = di->next) {
di->mark = False;
if (is_DebugInfo_archived(di))
continue;
di->mark = do_DebugInfos_overlap( di, diRef );
if (di == diRef) {
vg_assert(di->mark);
di->mark = False;
}
}
discard_or_archive_marked_DebugInfos();
}
/* Find the existing DebugInfo for |filename| or if not found, create
one. In the latter case |filename| is strdup'd into VG_AR_DINFO,
and the new DebugInfo is added to debugInfo_list. */
static DebugInfo* find_or_create_DebugInfo_for ( const HChar* filename )
{
DebugInfo* di;
vg_assert(filename);
for (di = debugInfo_list; di; di = di->next) {
if (is_DebugInfo_archived(di))
continue;
vg_assert(di->fsm.filename);
if (0==VG_(strcmp)(di->fsm.filename, filename))
break;
}
if (!di) {
di = alloc_DebugInfo(filename);
vg_assert(di);
di->next = debugInfo_list;
debugInfo_list = di;
}
vg_assert(!is_DebugInfo_archived(di));
return di;
}
/* Debuginfo reading for 'di' has just been successfully completed.
Check that the invariants stated in
"Comment_on_IMPORTANT_CFSI_REPRESENTATIONAL_INVARIANTS" in
priv_storage.h are observed. */
static void check_CFSI_related_invariants ( const DebugInfo* di )
{
DebugInfo* di2 = NULL;
Bool has_nonempty_rx = False;
Word i, j;
const Bool debug = VG_(debugLog_getLevel)() >= 3;
vg_assert(di);
/* This fn isn't called until after debuginfo for this object has
been successfully read. And that shouldn't happen until we have
both a r-x and rw- mapping for the object. Hence: */
vg_assert(di->fsm.have_rx_map);
for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
const DebugInfoMapping* map = VG_(indexXA)(di->fsm.maps, i);
/* We are interested in r-x mappings only */
if (!map->rx)
continue;
/* degenerate case: r-x section is empty */
if (map->size == 0)
continue;
has_nonempty_rx = True;
/* normal case: r-x section is nonempty */
/* invariant (0) */
vg_assert(map->size > 0);
/* invariant (1) */
for (di2 = debugInfo_list; di2; di2 = di2->next) {
if (di2 == di || is_DebugInfo_archived(di2))
continue;
for (j = 0; j < VG_(sizeXA)(di2->fsm.maps); j++) {
const DebugInfoMapping* map2 = VG_(indexXA)(di2->fsm.maps, j);
if (!map2->rx || map2->size == 0)
continue;
vg_assert2(!ranges_overlap(map->avma, map->size,
map2->avma, map2->size),
"DiCfsi invariant (1) verification failed");
}
}
}
/* degenerate case: all r-x sections are empty */
if (!has_nonempty_rx) {
vg_assert(di->cfsi_rd == NULL);
return;
}
/* invariant (2) */
if (di->cfsi_rd) {
vg_assert(di->cfsi_minavma <= di->cfsi_maxavma); /* duh! */
/* It may be that the cfsi range doesn't fit into any one individual
mapping, but it is covered by the combination of all the mappings.
That's a bit tricky to establish. To do so, create a RangeMap with
the cfsi range as the single only non-zero mapping, then zero out all
the parts described by di->fsm.maps, and check that there's nothing
left. */
RangeMap* rm = VG_(newRangeMap)( ML_(dinfo_zalloc),
"di.debuginfo. cCri.1", ML_(dinfo_free),
/*initialVal*/0 );
VG_(bindRangeMap)(rm, di->cfsi_minavma, di->cfsi_maxavma, 1);
for (i = 0; i < VG_(sizeXA)(di->fsm.maps); i++) {
const DebugInfoMapping* map = VG_(indexXA)(di->fsm.maps, i);
/* We are interested in r-x mappings only */
if (!map->rx)
continue;
if (map->size > 0)
VG_(bindRangeMap)(rm, map->avma, map->avma + map->size - 1, 0);
}
/* Typically, the range map contains one single range with value 0,
meaning that the cfsi range is entirely covered by the rx mappings.
However, in some cases, there are holes in the rx mappings
(see BZ #398028).
In such a case, check that no cfsi refers to these holes. */
Bool cfsi_fits = VG_(sizeRangeMap)(rm) >= 1;
// Check the ranges in the map.
for (Word ix = 0; ix < VG_(sizeRangeMap)(rm); ix++) {
UWord key_min = 0x55, key_max = 0x56, val = 0x57;
VG_(indexRangeMap)(&key_min, &key_max, &val, rm, ix);
if (debug)
VG_(dmsg)("cfsi range rx-mappings coverage check: %s %#lx-%#lx\n",
val == 1 ? "Uncovered" : "Covered",
key_min, key_max);
{
// Sanity-check the range-map operation
UWord check_key_min = 0x55, check_key_max = 0x56, check_val = 0x57;
VG_(lookupRangeMap)(&check_key_min, &check_key_max, &check_val, rm,
key_min + (key_max - key_min) / 2);
if (ix == 0)
vg_assert(key_min == (UWord)0);
if (ix == VG_(sizeRangeMap)(rm) - 1)
vg_assert(key_max == ~(UWord)0);
vg_assert(key_min == check_key_min);
vg_assert(key_max == check_key_max);
vg_assert(val == 0 || val == 1);
vg_assert(val == check_val);
}
if (val == 1) {
/* This is a part of cfsi_minavma .. cfsi_maxavma not covered.
Check no cfsi overlaps with this range. */
for (i = 0; i < di->cfsi_used; i++) {
DiCfSI* cfsi = &di->cfsi_rd[i];
vg_assert2(cfsi->base > key_max
|| cfsi->base + cfsi->len - 1 < key_min,
"DiCfsi invariant (2) verification failed");
}
}
}
vg_assert(cfsi_fits);
VG_(deleteRangeMap)(rm);
}
/* invariants (3) and (4) */
if (di->cfsi_rd) {
vg_assert(di->cfsi_used > 0);
vg_assert(di->cfsi_size > 0);
for (i = 0; i < di->cfsi_used; i++) {
DiCfSI* cfsi = &di->cfsi_rd[i];
vg_assert(cfsi->len > 0);
vg_assert(cfsi->base >= di->cfsi_minavma);
vg_assert(cfsi->base + cfsi->len - 1 <= di->cfsi_maxavma);
if (i > 0) {
DiCfSI* cfsip = &di->cfsi_rd[i-1];
vg_assert(cfsip->base + cfsip->len <= cfsi->base);
}
}
} else {
vg_assert(di->cfsi_used == 0);
vg_assert(di->cfsi_size == 0);
}
}
/*--------------------------------------------------------------*/
/*--- ---*/
/*--- TOP LEVEL: INITIALISE THE DEBUGINFO SYSTEM ---*/
/*--- ---*/
/*--------------------------------------------------------------*/
void VG_(di_initialise) ( void )
{
/* There's actually very little to do here, since everything
centers around the DebugInfos in debugInfo_list, they are
created and destroyed on demand, and each one is treated more or
less independently. */
vg_assert(debugInfo_list == NULL);
/* flush the debug info caches. */
caches__invalidate();
}
/*--------------------------------------------------------------*/
/*--- ---*/
/*--- TOP LEVEL: NOTIFICATION (ACQUIRE/DISCARD INFO) (LINUX) ---*/
/*--- ---*/
/*--------------------------------------------------------------*/
#if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) || defined(VGO_freebsd)
/* Helper (indirect) for di_notify_ACHIEVE_ACCEPT_STATE */
static Bool overlaps_DebugInfoMappings ( const DebugInfoMapping* map1,
const DebugInfoMapping* map2 )
{
vg_assert(map1 && map2 && map1 != map2);
vg_assert(map1->size != 0 && map2->size != 0);
if (map1->avma + map1->size <= map2->avma) return False;
if (map2->avma + map2->size <= map1->avma) return False;
return True;
}
/* Helper (indirect) for di_notify_ACHIEVE_ACCEPT_STATE */
static void show_DebugInfoMappings
( const DebugInfo* di,
/*MOD*/XArray* maps /* XArray<DebugInfoMapping> */ )
{
Word i, n;
vg_assert(maps);
n = VG_(sizeXA)(maps);
for (i = 0; i < n; i++) {
const DebugInfoMapping* map = VG_(indexXA)(maps, i);
TRACE_SYMTAB(" [%ld] avma 0x%-16lx size %-8lu "
"foff %-8lld %s %s %s\n",
i, map->avma, map->size, (Long)map->foff,
map->rx ? "rx" : "--",
map->rw ? "rw" : "--",
map->ro ? "ro" : "--");
}
}
/* Helper for di_notify_ACHIEVE_ACCEPT_STATE. This removes overlaps
in |maps|, in a fairly weak way, by truncating overlapping ends.
This may need to be strengthened in future. Currently it performs
a post-fixup check, so as least we can be sure that if this
function returns (rather than asserts) that |maps| is overlap
free. */
static void truncate_DebugInfoMapping_overlaps
( const DebugInfo* di,
/*MOD*/XArray* maps /* XArray<DebugInfoMapping> */ )
{
TRACE_SYMTAB("Un-de-overlapped _DebugInfoMappings:\n");
show_DebugInfoMappings(di, maps);
TRACE_SYMTAB("\n");
Word i, j, n;
DebugInfoMapping *map_i, *map_j;
n = VG_(sizeXA)(maps);
for (i = 0; i < n; i++) {
map_i = VG_(indexXA)(maps, i);
if (map_i->size == 0)
continue; // Hmm, mutancy. Shouldn't happen.
for (j = i+1; j < n; j++) {
map_j = VG_(indexXA)(maps, j);
if (map_j->size == 0)
continue; // Hmm, mutancy. Shouldn't happen.
/* map_j was observed later than map_i, since the entries are
in the XArray in the order in which they were observed.
If map_j starts inside map_i, trim map_i's end so it does
not overlap map_j. This reflects the reality that when
two mmaped areas overlap, the later mmap silently
overwrites the earlier mmap's mapping. */
if (map_j->avma >= map_i->avma
&& map_j->avma < map_i->avma + map_i->size) {
SizeT map_i_newsize = map_j->avma - map_i->avma;
vg_assert(map_i_newsize < map_i->size);
map_i->size = map_i_newsize;
}
}
}
TRACE_SYMTAB("De-overlapped DebugInfoMappings:\n");
show_DebugInfoMappings(di, maps);
TRACE_SYMTAB("\n");
TRACE_SYMTAB("Checking that there are no remaining overlaps.\n");
for (i = 0; i < n; i++) {
map_i = VG_(indexXA)(maps, i);
if (map_i->size == 0)
continue;
for (j = i+1; j < n; j++) {
map_j = VG_(indexXA)(maps, j);
if (map_j->size == 0)
continue;
Bool overlap
= overlaps_DebugInfoMappings( map_i, map_j );
/* If the following assert ever fails, it means the de-overlapping
scheme above is too weak, and needs improvement. */
vg_assert(!overlap);
}
}
TRACE_SYMTAB("Check successful.\n");
}
/* The debug info system is driven by notifications that a text
segment has been mapped in, or unmapped, or when sections change
permission. It's all a bit kludgey and basically means watching
syscalls, trying to second-guess when the system's dynamic linker
is done with mapping in a new object for execution. This is all
tracked using the DebugInfoFSM struct for the object. Anyway, once
we finally decide we've got to an accept state, this section then
will acquire whatever info is available for the corresponding
object. This section contains the notification handlers, which
update the FSM and determine when an accept state has been reached.
*/
/* When the sequence of observations causes a DebugInfoFSM to move
into the accept state, call here to actually get the debuginfo read
in. Returns a ULong whose purpose is described in comments
preceding VG_(di_notify_mmap) just below.
*/
static ULong di_notify_ACHIEVE_ACCEPT_STATE ( struct _DebugInfo* di )
{
ULong di_handle;
Bool ok;
advance_current_DiEpoch("di_notify_ACHIEVE_ACCEPT_STATE");
vg_assert(di->fsm.filename);
TRACE_SYMTAB("\n");
TRACE_SYMTAB("------ start ELF OBJECT "
"-------------------------"
"------------------------------\n");
TRACE_SYMTAB("------ name = %s\n", di->fsm.filename);
TRACE_SYMTAB("\n");
/* We're going to read symbols and debug info for the avma
ranges specified in the _DebugInfoFsm mapping array. First
get rid of any other DebugInfos which overlap any of those
ranges (to avoid total confusion). But only those valid in
the current epoch. We don't want to discard archived DebugInfos. */
discard_DebugInfos_which_overlap_with( di );
/* The DebugInfoMappings that now exist in the FSM may involve
overlaps. This confuses ML_(read_elf_*), and may cause
it to compute wrong biases. So de-overlap them now.
See http://bugzilla.mozilla.org/show_bug.cgi?id=788974 */
truncate_DebugInfoMapping_overlaps( di, di->fsm.maps );
/* And acquire new info. */
# if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
ok = ML_(read_elf_object)( di );
if (ok)
di->deferred = True;
# elif defined(VGO_darwin)
ok = ML_(read_macho_debug_info)( di );
# else
# error "unknown OS"
# endif
if (ok) {
TRACE_SYMTAB("\n------ Canonicalising the "
"acquired info ------\n");
/* invalidate the debug info caches. */
caches__invalidate();
/* prepare read data for use */
ML_(canonicaliseTables)( di );
/* Check invariants listed in
Comment_on_IMPORTANT_REPRESENTATIONAL_INVARIANTS in
priv_storage.h. */
check_CFSI_related_invariants(di);
ML_(finish_CFSI_arrays)(di);
// Mark di's first epoch point as a valid epoch. Because its
// last_epoch value is still invalid, this changes di's state from
// "allocated" to "active".
vg_assert(is_DebugInfo_allocated(di));
di->first_epoch = VG_(current_DiEpoch)();
vg_assert(is_DebugInfo_active(di));
show_epochs("di_notify_ACHIEVE_ACCEPT_STATE success");