-
Notifications
You must be signed in to change notification settings - Fork 12
/
trace_helpers.c
1599 lines (1343 loc) · 38.3 KB
/
trace_helpers.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
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
// Copyright (c) 2020 Wenbo Zhang
//
// Based on ksyms improvements from Andrii Nakryiko, add more helpers.
// 28-Feb-2020 Wenbo Zhang Created this.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <time.h>
#include <linux/refcount.h>
#include <linux/rblist.h>
#include <linux/time64.h>
#include <bpf/btf.h>
#include <bpf/libbpf.h>
#include <limits.h>
#include <lzma.h>
#include <monitor.h>
#include "trace_helpers.h"
#include "uprobe_helpers.h"
#define min(x, y) ({ \
typeof(x) _min1 = (x); \
typeof(y) _min2 = (y); \
(void) (&_min1 == &_min2); \
_min1 < _min2 ? _min1 : _min2; })
#define DISK_NAME_LEN 32
#define MINORBITS 20
#define MINORMASK ((1U << MINORBITS) - 1)
#define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
struct ksyms {
struct ksym *syms;
int syms_sz;
int syms_cap;
char *strs;
int strs_sz;
int strs_cap;
};
static int ksyms__add_symbol(struct ksyms *ksyms, const char *name, unsigned long addr)
{
size_t new_cap, name_len = strlen(name) + 1;
struct ksym *ksym;
void *tmp;
if (ksyms->strs_sz + name_len > ksyms->strs_cap) {
new_cap = ksyms->strs_cap * 4 / 3;
if (new_cap < ksyms->strs_sz + name_len)
new_cap = ksyms->strs_sz + name_len;
if (new_cap < 1024)
new_cap = 1024;
tmp = realloc(ksyms->strs, new_cap);
if (!tmp)
return -1;
ksyms->strs = tmp;
ksyms->strs_cap = new_cap;
}
if (ksyms->syms_sz + 1 > ksyms->syms_cap) {
new_cap = ksyms->syms_cap * 4 / 3;
if (new_cap < 1024)
new_cap = 1024;
tmp = realloc(ksyms->syms, sizeof(*ksyms->syms) * new_cap);
if (!tmp)
return -1;
ksyms->syms = tmp;
ksyms->syms_cap = new_cap;
}
ksym = &ksyms->syms[ksyms->syms_sz];
/* while constructing, re-use pointer as just a plain offset */
ksym->name = (void *)(unsigned long)ksyms->strs_sz;
ksym->addr = addr;
memcpy(ksyms->strs + ksyms->strs_sz, name, name_len);
ksyms->strs_sz += name_len;
ksyms->syms_sz++;
return 0;
}
static int ksym_cmp(const void *p1, const void *p2)
{
const struct ksym *s1 = p1, *s2 = p2;
if (s1->addr == s2->addr)
return strcmp(s1->name, s2->name);
return s1->addr < s2->addr ? -1 : 1;
}
struct ksyms *ksyms__load(void)
{
char sym_type, sym_name[256];
struct ksyms *ksyms;
unsigned long sym_addr;
int i, ret;
FILE *f;
f = fopen("/proc/kallsyms", "r");
if (!f)
return NULL;
ksyms = calloc(1, sizeof(*ksyms));
if (!ksyms)
goto err_out;
while (true) {
ret = fscanf(f, "%lx %c %s%*[^\n]\n",
&sym_addr, &sym_type, sym_name);
if (ret == EOF && feof(f))
break;
if (ret != 3)
goto err_out;
if (ksyms__add_symbol(ksyms, sym_name, sym_addr))
goto err_out;
}
/* now when strings are finalized, adjust pointers properly */
for (i = 0; i < ksyms->syms_sz; i++)
ksyms->syms[i].name += (unsigned long)ksyms->strs;
qsort(ksyms->syms, ksyms->syms_sz, sizeof(*ksyms->syms), ksym_cmp);
fclose(f);
return ksyms;
err_out:
ksyms__free(ksyms);
fclose(f);
return NULL;
}
void ksyms__free(struct ksyms *ksyms)
{
if (!ksyms)
return;
free(ksyms->syms);
free(ksyms->strs);
free(ksyms);
}
const struct ksym *ksyms__map_addr(const struct ksyms *ksyms,
unsigned long addr)
{
int start = 0, end = ksyms->syms_sz - 1, mid;
unsigned long sym_addr;
/* find largest sym_addr <= addr using binary search */
while (start < end) {
mid = start + (end - start + 1) / 2;
sym_addr = ksyms->syms[mid].addr;
if (sym_addr <= addr)
start = mid;
else
end = mid - 1;
}
if (start == end && ksyms->syms[start].addr <= addr)
return &ksyms->syms[start];
return NULL;
}
const struct ksym *ksyms__get_symbol(const struct ksyms *ksyms,
const char *name)
{
int i;
for (i = 0; i < ksyms->syms_sz; i++) {
if (strcmp(ksyms->syms[i].name, name) == 0)
return &ksyms->syms[i];
}
return NULL;
}
/*
* syms_cache --> syms --> dso --> object --> sym
* pid maps file sym
**/
struct load_range {
uint64_t start;
uint64_t end;
uint64_t file_off;
};
enum elf_type {
EXEC,
DYN,
PERF_MAP,
VDSO,
UNKNOWN,
};
struct object {
struct rb_node rbnode;
refcount_t refcnt;
char *name;
/* Dyn's first text section virtual addr at execution */
uint64_t sh_addr;
/* Dyn's first text section file offset */
uint64_t sh_offset;
enum elf_type type;
struct sym *syms;
int syms_sz;
int syms_cap;
char *strs;
int strs_sz;
int strs_cap;
};
struct dso {
struct load_range *ranges;
int range_sz;
struct object *obj;
};
struct map {
uint64_t start_addr;
uint64_t end_addr;
uint64_t file_off;
uint64_t dev_major;
uint64_t dev_minor;
uint64_t inode;
};
struct syms {
struct dso *dsos;
int dso_sz;
};
static bool is_file_backed(const char *mapname)
{
#define STARTS_WITH(mapname, prefix) \
(!strncmp(mapname, prefix, sizeof(prefix) - 1))
return mapname[0] && !(
STARTS_WITH(mapname, "//anon") ||
STARTS_WITH(mapname, "/dev/zero") ||
STARTS_WITH(mapname, "/anon_hugepage") ||
STARTS_WITH(mapname, "socket:") ||
STARTS_WITH(mapname, "[stack") ||
STARTS_WITH(mapname, "/SYSV") ||
STARTS_WITH(mapname, "[heap]") ||
STARTS_WITH(mapname, "[vsyscall]"));
}
static bool is_perf_map(const char *path)
{
return false;
}
static bool is_vdso(const char *path)
{
return !strcmp(path, "[vdso]");
}
static int get_elf_type(const char *path)
{
GElf_Ehdr hdr;
void *res;
Elf *e;
int fd;
if (is_vdso(path))
return -1;
e = open_elf(path, &fd);
if (!e)
return -1;
res = gelf_getehdr(e, &hdr);
close_elf(e, fd);
if (!res)
return -1;
return hdr.e_type;
}
static int get_elf_text_scn_info(const char *path, uint64_t *addr,
uint64_t *offset)
{
Elf_Scn *section = NULL;
int fd = -1, err = -1;
GElf_Shdr header;
size_t stridx;
Elf *e = NULL;
char *name;
e = open_elf(path, &fd);
if (!e)
goto err_out;
err = elf_getshdrstrndx(e, &stridx);
if (err < 0)
goto err_out;
err = -1;
while ((section = elf_nextscn(e, section)) != 0) {
if (!gelf_getshdr(section, &header))
continue;
name = elf_strptr(e, stridx, header.sh_name);
if (name && !strcmp(name, ".text")) {
*addr = (uint64_t)header.sh_addr;
*offset = (uint64_t)header.sh_offset;
err = 0;
break;
}
}
err_out:
close_elf(e, fd);
return err;
}
static int object_node_cmp(struct rb_node *rbn, const void *entry)
{
struct object *obj = container_of(rbn, struct object, rbnode);
const char *name = entry;
return strcmp(obj->name, name);
}
static struct rb_node *object_node_new(struct rblist *rlist, const void *new_entry)
{
const char *name = new_entry;
struct object *obj = malloc(sizeof(*obj));
int type;
if (obj) {
memset(obj, 0, sizeof(*obj));
RB_CLEAR_NODE(&obj->rbnode);
obj->name = strdup(name);
refcount_set(&obj->refcnt, 0);
type = get_elf_type(name);
if (type == ET_EXEC) {
obj->type = EXEC;
if (get_elf_text_scn_info(name, &obj->sh_addr, &obj->sh_offset) < 0)
return NULL;
} else if (type == ET_DYN) {
obj->type = DYN;
if (get_elf_text_scn_info(name, &obj->sh_addr, &obj->sh_offset) < 0)
return NULL;
} else if (is_perf_map(name)) {
obj->type = PERF_MAP;
} else if (is_vdso(name)) {
obj->type = VDSO;
} else {
obj->type = UNKNOWN;
}
return &obj->rbnode;
} else
return NULL;
}
static void object_node_delete(struct rblist *rblist, struct rb_node *rbn)
{
struct object *obj = container_of(rbn, struct object, rbnode);
free(obj->name);
free(obj->syms);
free(obj->strs);
free(obj);
}
static struct rblist objects = {
.entries = RB_ROOT_CACHED,
.nr_entries = 0,
.node_cmp = object_node_cmp,
.node_new = object_node_new,
.node_delete = object_node_delete,
};
static struct object *obj__get(const char *name)
{
struct rb_node *rbnode;
struct object *obj = NULL;
rbnode = rblist__findnew(&objects, name);
if (rbnode) {
obj = container_of(rbnode, struct object, rbnode);
if (refcount_read(&obj->refcnt) == 0)
refcount_set(&obj->refcnt, 1);
else
refcount_inc(&obj->refcnt);
}
return obj;
}
static void obj__put(struct object *obj)
{
if (obj && refcount_dec_and_test(&obj->refcnt))
rblist__remove_node(&objects, &obj->rbnode);
}
void obj__stat(FILE *fp)
{
static const char *str_type[] = {"EXEC", "DYN", "PERF_MAP", "VDSO", "UNKNOWN"};
struct rb_node *node;
struct object *obj;
long used, size;
if (rblist__nr_entries(&objects) == 0)
return;
fprintf(fp, "OBJECTS %u\n", rblist__nr_entries(&objects));
fprintf(fp, "%-4s %-8s %-8s %-12s %-12s %s\n", "REF", "TYPE", "SYMS", "USED", "MEMS", "OBJECT");
for (node = rb_first_cached(&objects.entries); node;
node = rb_next(node)) {
obj = container_of(node, struct object, rbnode);
used = obj->syms_sz * sizeof(*obj->syms) + obj->strs_sz;
size = obj->syms_cap * sizeof(*obj->syms) + obj->strs_cap;
fprintf(fp, "%-4u %-8s %-8d %-12ld %-12ld %s\n", refcount_read(&obj->refcnt),
str_type[obj->type], obj->syms_sz, used, size, obj->name);
}
}
static int syms__add_dso(struct syms *syms, struct map *map, const char *name)
{
struct dso *dso = NULL;
int i;
void *tmp;
for (i = 0; i < syms->dso_sz; i++) {
if (!strcmp(syms->dsos[i].obj->name, name)) {
dso = &syms->dsos[i];
break;
}
}
if (!dso) {
tmp = realloc(syms->dsos, (syms->dso_sz + 1) *
sizeof(*syms->dsos));
if (!tmp)
return -1;
syms->dsos = tmp;
dso = &syms->dsos[syms->dso_sz++];
memset(dso, 0, sizeof(*dso));
dso->obj = obj__get(name);
if (!dso->obj)
return -1;
}
tmp = realloc(dso->ranges, (dso->range_sz + 1) * sizeof(*dso->ranges));
if (!tmp)
return -1;
dso->ranges = tmp;
dso->ranges[dso->range_sz].start = map->start_addr;
dso->ranges[dso->range_sz].end = map->end_addr;
dso->ranges[dso->range_sz].file_off = map->file_off;
dso->range_sz++;
return 0;
}
static void dso__free_fields(struct dso *dso)
{
if (!dso)
return;
obj__put(dso->obj);
free(dso->ranges);
}
struct dso *syms__find_dso(const struct syms *syms, unsigned long addr,
uint64_t *offset)
{
struct load_range *range;
struct dso *dso;
int i, j;
for (i = 0; i < syms->dso_sz; i++) {
dso = &syms->dsos[i];
for (j = 0; j < dso->range_sz; j++) {
range = &dso->ranges[j];
if (addr <= range->start || addr >= range->end)
continue;
if (dso->obj->type == DYN || dso->obj->type == VDSO) {
/* Offset within the mmap */
*offset = addr - range->start + range->file_off;
/* Offset within the ELF for dyn symbol lookup */
*offset += dso->obj->sh_addr - dso->obj->sh_offset;
} else {
*offset = addr;
}
return dso;
}
}
return NULL;
}
static int obj__load_sym_table_from_perf_map(struct object *obj)
{
return -1;
}
static int obj__add_sym(struct object *obj, const char *name, uint64_t start,
uint64_t size)
{
struct sym *sym;
size_t new_cap, name_len = strlen(name) + 1;
void *tmp;
if (obj->strs_sz + name_len > obj->strs_cap) {
new_cap = obj->strs_cap * 4 / 3;
if (new_cap < obj->strs_sz + name_len)
new_cap = obj->strs_sz + name_len;
if (new_cap < 1024)
new_cap = 1024;
tmp = realloc(obj->strs, new_cap);
if (!tmp)
return -1;
obj->strs = tmp;
obj->strs_cap = new_cap;
}
if (obj->syms_sz + 1 > obj->syms_cap) {
new_cap = obj->syms_cap * 4 / 3;
if (new_cap < 1024)
new_cap = 1024;
tmp = realloc(obj->syms, sizeof(*obj->syms) * new_cap);
if (!tmp)
return -1;
obj->syms = tmp;
obj->syms_cap = new_cap;
}
sym = &obj->syms[obj->syms_sz++];
/* while constructing, re-use pointer as just a plain offset */
sym->name = (void *)(unsigned long)obj->strs_sz;
sym->start = start;
sym->size = size;
memcpy(obj->strs + obj->strs_sz, name, name_len);
obj->strs_sz += name_len;
return 0;
}
static int sym_cmp(const void *p1, const void *p2)
{
const struct sym *s1 = p1, *s2 = p2;
if (s1->start == s2->start)
return strcmp(s1->name, s2->name);
return s1->start < s2->start ? -1 : 1;
}
static int obj__add_syms(struct object *obj, Elf *e, Elf_Scn *section,
size_t stridx, size_t symsize)
{
Elf_Data *data = NULL;
while ((data = elf_getdata(section, data)) != 0) {
size_t i, symcount = data->d_size / symsize;
if (data->d_size % symsize)
return -1;
for (i = 0; i < symcount; ++i) {
const char *name;
GElf_Sym sym;
if (!gelf_getsym(data, (int)i, &sym))
continue;
if (!(name = elf_strptr(e, stridx, sym.st_name)))
continue;
if (name[0] == '\0')
continue;
if (sym.st_value == 0)
continue;
if (obj__add_sym(obj, name, sym.st_value, sym.st_size))
goto err_out;
}
}
return 0;
err_out:
return -1;
}
static void obj__free_fields(struct object *obj)
{
free(obj->syms);
free(obj->strs);
obj->syms_sz = 0;
obj->syms_cap = 0;
obj->strs_sz = 0;
obj->syms_cap = 0;
}
#define MAGIC "\xFD" "7zXZ\0" /* XZ file format. */
#define MAGIC2 "\x5d\0" /* Raw LZMA format. */
static int unlzma(void *input, size_t input_size, void **output, size_t *output_size)
{
lzma_stream strm = LZMA_STREAM_INIT;
lzma_action action = LZMA_RUN;
lzma_ret ret;
void *buffer = NULL;
size_t size = 0;
*output = NULL;
*output_size = 0;
#define NOMAGIC(magic) \
(input_size <= sizeof magic || \
memcmp (input, magic, sizeof magic - 1))
if (NOMAGIC (MAGIC) && NOMAGIC (MAGIC2))
return -1;
ret = lzma_stream_decoder(&strm, 1UL << 30, 0);
if (ret != LZMA_OK)
return -1;
strm.next_in = input;
strm.avail_in = input_size;
do {
if (strm.avail_out == 0) {
ptrdiff_t pos = (void *) strm.next_out - buffer;
size_t more = size ? size * 2 : input_size;
char *b = realloc (buffer, more);
while (unlikely (b == NULL) && more >= size + 1024)
b = realloc (buffer, more -= 1024);
if (unlikely (b == NULL)) {
ret = LZMA_MEM_ERROR;
break;
}
buffer = b;
size = more;
strm.next_out = buffer + pos;
strm.avail_out = size - pos;
}
} while ((ret = lzma_code(&strm, action)) == LZMA_OK);
size = strm.total_out;
buffer = realloc (buffer, size) ?: size == 0 ? NULL : buffer;
lzma_end(&strm);
if (ret == LZMA_STREAM_END) {
*output = buffer;
*output_size = size;
return 0;
}
free(buffer);
return -1;
}
static int elf__load_sym_table(struct object *obj, Elf *e)
{
Elf_Scn *section = NULL;
size_t shstrndx;
void *buffer = NULL;
size_t size = 0;
int added = 0;
if (elf_getshdrstrndx(e, &shstrndx) < 0)
return -1;
while ((section = elf_nextscn(e, section)) != 0) {
GElf_Shdr header;
const char *name;
if (!gelf_getshdr(section, &header))
continue;
name = elf_strptr(e, shstrndx, header.sh_name);
if (name == NULL)
continue;
if (header.sh_type == SHT_SYMTAB ||
header.sh_type == SHT_DYNSYM) {
if (obj__add_syms(obj, e, section, header.sh_link,
header.sh_entsize))
goto err_out;
added ++;
continue;
}
if (!strcmp (name, ".gnu_debugdata")) {
/* Uncompress LZMA data found in a minidebug file. The minidebug
* format is described at
* https://sourceware.org/gdb/current/onlinedocs/gdb/MiniDebugInfo.html.
*/
Elf_Data *rawdata = elf_rawdata(section, NULL);
if (rawdata != NULL &&
unlzma(rawdata->d_buf, rawdata->d_size, &buffer, &size) == 0) {
Elf *debuginfo = open_elf_memory(buffer, size);
if (debuginfo &&
elf__load_sym_table(obj, debuginfo) == 0) {
close_elf(debuginfo, 0);
}
free(buffer);
}
}
}
return added ? 0 : -1;
err_out:
return -1;
}
typedef struct
{
uint32_t namesz;
uint32_t descsz;
uint32_t type;
char name[1];
} b_elf_note;
#define NT_GNU_BUILD_ID 3
#define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
/*
* Open a separate debug info file, using the build ID to find it.
* The GDB manual says that the only place gdb looks for a debug file
* when the build ID is known is in /usr/lib/debug/.build-id.
* https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
*/
static Elf *open_elf_debugfile_by_buildid(Elf *e, int *debug_fd)
{
const char * const prefix = SYSTEM_BUILD_ID_DIR;
const size_t prefix_len = strlen (prefix);
const char * const suffix = ".debug";
const size_t suffix_len = strlen (suffix);
size_t len;
char *bd_filename, *t;
size_t i;
const char *buildid_data = NULL;
uint32_t buildid_size;
Elf_Scn *section = NULL;
size_t shstrndx;
Elf *debug = NULL;
if (elf_getshdrstrndx(e, &shstrndx) < 0)
return NULL;
while ((section = elf_nextscn(e, section)) != 0) {
GElf_Shdr header;
const char *name;
if (!gelf_getshdr(section, &header))
continue;
name = elf_strptr(e, shstrndx, header.sh_name);
if (name == NULL)
continue;
if (!strcmp (name, ".note.gnu.build-id")) {
const b_elf_note *note;
Elf_Data *rawdata = elf_rawdata(section, NULL);
note = (const b_elf_note *)rawdata->d_buf;
if (note->type == NT_GNU_BUILD_ID &&
note->namesz == 4 &&
strncmp (note->name, "GNU", 4) == 0 &&
header.sh_size <= 12 + ((note->namesz + 3) & ~3) + note->descsz)
{
buildid_data = ¬e->name[0] + ((note->namesz + 3) & ~3);
buildid_size = note->descsz;
goto found;
}
}
}
return NULL;
found:
len = prefix_len + buildid_size * 2 + suffix_len + 2;
bd_filename = malloc(len);
if (bd_filename == NULL)
return NULL;
t = bd_filename;
memcpy(t, prefix, prefix_len);
t += prefix_len;
for (i = 0; i < buildid_size; i++) {
unsigned char b;
unsigned char nib;
b = (unsigned char) buildid_data[i];
nib = (b & 0xf0) >> 4;
*t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
nib = b & 0x0f;
*t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
if (i == 0)
*t++ = '/';
}
memcpy (t, suffix, suffix_len);
t[suffix_len] = '\0';
debug = open_elf(bd_filename, debug_fd);
free(bd_filename);
return debug;
}
static int obj__load_sym_table_from_elf(struct object *obj, int fd)
{
Elf *e, *debug = NULL;
int debug_fd = 0;
int i, err = -1;
void *tmp;
e = fd > 0 ? open_elf_by_fd(fd) : open_elf(obj->name, &fd);
if (!e)
return err;
debug = open_elf_debugfile_by_buildid(e, &debug_fd);
if (!debug || elf__load_sym_table(obj, debug) < 0) {
if (elf__load_sym_table(obj, e) < 0)
goto err_out;
}
tmp = realloc(obj->strs, obj->strs_sz);
if (!tmp)
goto err_out;
obj->strs = tmp;
obj->strs_cap = obj->strs_sz;
tmp = realloc(obj->syms, sizeof(*obj->syms) * obj->syms_sz);
if (!tmp)
goto err_out;
obj->syms = tmp;
obj->syms_cap = obj->syms_sz;
/* now when strings are finalized, adjust pointers properly */
for (i = 0; i < obj->syms_sz; i++)
obj->syms[i].name += (unsigned long)obj->strs;
qsort(obj->syms, obj->syms_sz, sizeof(*obj->syms), sym_cmp);
err = 0;
out:
close_elf(e, fd);
if (debug)
close_elf(debug, debug_fd);
return err;
err_out:
obj__free_fields(obj);
goto out;
}
static int create_tmp_vdso_image(struct object *obj)
{
uint64_t start_addr, end_addr;
long pid = getpid();
char buf[PATH_MAX];
void *image = NULL;
char tmpfile[128];
int ret, fd = -1;
uint64_t sz;
char *name;
FILE *f;
snprintf(tmpfile, sizeof(tmpfile), "/proc/%ld/maps", pid);
f = fopen(tmpfile, "r");
if (!f)
return -1;
while (true) {
ret = fscanf(f, "%lx-%lx %*s %*x %*x:%*x %*u%[^\n]",
&start_addr, &end_addr, buf);
if (ret == EOF && feof(f))
break;
if (ret != 3)
goto err_out;
name = buf;
while (isspace(*name))
name++;
if (!is_file_backed(name))
continue;
if (is_vdso(name))
break;
}
sz = end_addr - start_addr;
image = malloc(sz);
if (!image)
goto err_out;
memcpy(image, (void *)start_addr, sz);
snprintf(tmpfile, sizeof(tmpfile),
"/tmp/libbpf_%ld_vdso_image_XXXXXX", pid);
fd = mkostemp(tmpfile, O_CLOEXEC);
if (fd < 0) {
fprintf(stderr, "failed to create temp file: %s\n",
strerror(errno));
goto err_out;
}
/* Unlink the file to avoid leaking */
if (unlink(tmpfile) == -1)
fprintf(stderr, "failed to unlink %s: %s\n", tmpfile,
strerror(errno));
if (write(fd, image, sz) == -1) {
fprintf(stderr, "failed to write to vDSO image: %s\n",
strerror(errno));
close(fd);
fd = -1;
goto err_out;
}
err_out:
fclose(f);
free(image);
return fd;
}
static int obj__load_sym_table_from_vdso_image(struct object *obj)
{
int fd = create_tmp_vdso_image(obj);
if (fd < 0)
return -1;
return obj__load_sym_table_from_elf(obj, fd);
}
static int obj__load_sym_table(struct object *obj)
{
if (obj->type == UNKNOWN)
return -1;
if (obj->type == PERF_MAP)
return obj__load_sym_table_from_perf_map(obj);
if (obj->type == EXEC || obj->type == DYN)
return obj__load_sym_table_from_elf(obj, 0);
if (obj->type == VDSO)
return obj__load_sym_table_from_vdso_image(obj);
return -1;
}
static const struct sym *obj__find_name(struct object *obj, const char *name)
{
int i;
if (!obj)
return NULL;
if (!obj->syms && obj__load_sym_table(obj))
return NULL;
for (i = 0; i < obj->syms_sz; i++) {
if (strcmp(obj->syms[i].name, name) == 0)
return &obj->syms[i];
}
return NULL;
}
static const struct sym *obj__find_offset(struct object *obj, uint64_t offset)
{
unsigned long sym_addr;
int start, end, mid;
if (!obj)
return NULL;
if (!obj->syms && obj__load_sym_table(obj))
return NULL;
start = 0;
end = obj->syms_sz - 1;
/* find largest sym_addr <= addr using binary search */
while (start < end) {
mid = start + (end - start + 1) / 2;
sym_addr = obj->syms[mid].start;
if (sym_addr <= offset)
start = mid;
else
end = mid - 1;
}
if (start == end &&
obj->syms[start].start <= offset &&
obj->syms[start].start + obj->syms[start].size >= offset)
return &obj->syms[start];
return NULL;
}