Skip to content

Commit 5ca8315

Browse files
committed
libbpf-tools/profile: Add module info and symbol offset to backtrace
Add additional information and change format of backtrace - add symbol base offset, dso name, dso base offset - symbol and dso info is included if it's available in target binary - changed format: INDEX ADDR [SYMBOL+OFFSET] [(MODULE+OFFSET)] Print backtrace of ip if it failed to get syms. Before: # profile -d psiginfo vscanf __snprintf_chk [unknown] [unknown] [unknown] [unknown] [unknown] sd_event_exit sd_event_dispatch sd_event_run [unknown] __libc_start_main [unknown] - systemd-journal (204) 1 xas_load xas_find filemap_map_pages __handle_mm_fault handle_mm_fault do_page_fault do_translation_fault do_mem_abort do_el0_ia_bp_hardening el0_ia xas_load -- failed to get syms - PmLogCtl (138757) 1 After: # profile -d #0 0xffffffc01018b7e8 __arm64_sys_clock_nanosleep+0x0 #1 0xffffffc01018b7e8 __arm64_sys_clock_nanosleep+0x0 iovisor#2 0xffffffc01009a93c el0_svc_handler+0x34 iovisor#3 0xffffffc010084a08 el0_svc+0x8 -- iovisor#4 0x0000007fa0bffd14 clock_nanosleep+0x94 (/usr/lib/libc-2.31.so+0x9ed14) iovisor#5 0x0000007fa0c0530c nanosleep+0x1c (/usr/lib/libc-2.31.so+0xa430c) iovisor#6 0x0000007fa0c051e4 sleep+0x34 (/usr/lib/libc-2.31.so+0xa41e4) iovisor#7 0x000000558a5a9608 flb_loop+0x28 (/usr/bin/fluent-bit+0x52608) iovisor#8 0x000000558a59f1c4 flb_main+0xa84 (/usr/bin/fluent-bit+0x481c4) iovisor#9 0x0000007fa0b85124 __libc_start_main+0xe4 (/usr/lib/libc-2.31.so+0x24124) iovisor#10 0x000000558a59d828 _start+0x34 (/usr/bin/fluent-bit+0x46828) - fluent-bit (1238) 1 #0 0xffffffc01027daa4 generic_copy_file_checks+0x334 #1 0xffffffc01027daa4 generic_copy_file_checks+0x334 iovisor#2 0xffffffc0102ba634 __handle_mm_fault+0x8dc iovisor#3 0xffffffc0102baa20 handle_mm_fault+0x168 iovisor#4 0xffffffc010ad23c0 do_page_fault+0x148 iovisor#5 0xffffffc010ad27c0 do_translation_fault+0xb0 iovisor#6 0xffffffc0100816b0 do_mem_abort+0x50 iovisor#7 0xffffffc0100843b0 el0_da+0x1c -- iovisor#8 0x0000007f8dc12648 [unknown] iovisor#9 0x0000007f8dc0aef8 [unknown] iovisor#10 0x0000007f8dc1c990 [unknown] iovisor#11 0x0000007f8dc08b0c [unknown] iovisor#12 0x0000007f8dc08e48 [unknown] iovisor#13 0x0000007f8dc081c8 [unknown] - PmLogCtl (2412) 1 Signed-off-by: Eunseon Lee <[email protected]>
1 parent 79e1d22 commit 5ca8315

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

libbpf-tools/profile.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ static void print_map(struct ksyms *ksyms, struct syms_cache *syms_cache,
289289
bool has_collision = false;
290290
unsigned int missing_stacks = 0;
291291
struct key_ext_t counts[MAX_ENTRIES];
292+
int idx = 0;
292293

293294
ip = calloc(env.perf_max_stack_depth, sizeof(*ip));
294295
if (!ip) {
@@ -366,18 +367,26 @@ static void print_map(struct ksyms *ksyms, struct syms_cache *syms_cache,
366367
printf(" %lld\n", v);
367368
} else {
368369
// print default multi-line stack output
370+
idx = 0;
371+
369372
if (!env.user_stacks_only) {
370373
if (stack_id_err(k->kern_stack_id))
371374
printf(" [Missed Kernel Stack]\n");
372375
else if (k->kern_stack_id >= 0 &&
373376
bpf_map_lookup_elem(sfd, &k->kern_stack_id, ip) == 0) {
374377
if (k->kernel_ip) {
375378
ksym = ksyms__map_addr(ksyms, k->kernel_ip);
376-
printf(" %s\n", ksym ? ksym->name : "[unknown]");
379+
if (ksym)
380+
printf(" #%-2d 0x%llx %s+0x%llx\n", idx++, k->kernel_ip, ksym->name, k->kernel_ip - ksym->addr);
381+
else
382+
printf(" #%-2d 0x%llx [unknown]\n", idx++, k->kernel_ip);
377383
}
378384
for (j = 0; j < env.perf_max_stack_depth && ip[j]; j++) {
379385
ksym = ksyms__map_addr(ksyms, ip[j]);
380-
printf(" %s\n", ksym ? ksym->name : "[unknown]");
386+
if (ksym)
387+
printf(" #%-2d 0x%lx %s+0x%lx\n", idx++, ip[j], ksym->name, ip[j] - ksym->addr);
388+
else
389+
printf(" #%-2d 0x%lx [unknown]\n", idx++, ip[j]);
381390
}
382391
}
383392
}
@@ -392,11 +401,20 @@ static void print_map(struct ksyms *ksyms, struct syms_cache *syms_cache,
392401
bpf_map_lookup_elem(sfd, &k->user_stack_id, ip) == 0) {
393402
syms = syms_cache__get_syms(syms_cache, k->pid);
394403
if (!syms) {
395-
fprintf(stderr, "failed to get syms\n");
404+
for (j = 0; j < env.perf_max_stack_depth && ip[j]; j++)
405+
printf(" #%-2d 0x%016lx [unknown]\n", idx++, ip[j]);
396406
} else {
397407
for (j = 0; j < env.perf_max_stack_depth && ip[j]; j++) {
398-
sym = syms__map_addr(syms, ip[j]);
399-
printf(" %s\n", sym ? sym->name : "[unknown]");
408+
char *dso_name;
409+
uint64_t dso_offset;
410+
sym = syms__map_addr_dso(syms, ip[j], &dso_name, &dso_offset);
411+
412+
printf(" #%-2d 0x%016lx", idx++, ip[j]);
413+
if (sym)
414+
printf(" %s+0x%lx", sym->name, sym->offset);
415+
if (dso_name)
416+
printf(" (%s+0x%lx)", dso_name, dso_offset);
417+
printf("\n");
400418
}
401419
}
402420
}

libbpf-tools/trace_helpers.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ static int dso__add_sym(struct dso *dso, const char *name, uint64_t start,
422422
sym->name = (void*)(unsigned long)off;
423423
sym->start = start;
424424
sym->size = size;
425+
sym->offset = 0;
425426

426427
return 0;
427428
}
@@ -634,8 +635,10 @@ static struct sym *dso__find_sym(struct dso *dso, uint64_t offset)
634635
end = mid - 1;
635636
}
636637

637-
if (start == end && dso->syms[start].start <= offset)
638+
if (start == end && dso->syms[start].start <= offset) {
639+
(dso->syms[start]).offset = offset - dso->syms[start].start;
638640
return &dso->syms[start];
641+
}
639642
return NULL;
640643
}
641644

@@ -720,6 +723,22 @@ const struct sym *syms__map_addr(const struct syms *syms, unsigned long addr)
720723
return dso__find_sym(dso, offset);
721724
}
722725

726+
const struct sym *syms__map_addr_dso(const struct syms *syms, unsigned long addr,
727+
char **dso_name, uint64_t *dso_offset)
728+
{
729+
struct dso *dso;
730+
uint64_t offset;
731+
732+
dso = syms__find_dso(syms, addr, &offset);
733+
if (!dso)
734+
return NULL;
735+
736+
*dso_name = dso->name;
737+
*dso_offset = offset;
738+
739+
return dso__find_sym(dso, offset);
740+
}
741+
723742
struct syms_cache {
724743
struct {
725744
struct syms *syms;

libbpf-tools/trace_helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct sym {
2424
const char *name;
2525
unsigned long start;
2626
unsigned long size;
27+
unsigned long offset;
2728
};
2829

2930
struct syms;
@@ -32,6 +33,8 @@ struct syms *syms__load_pid(int tgid);
3233
struct syms *syms__load_file(const char *fname);
3334
void syms__free(struct syms *syms);
3435
const struct sym *syms__map_addr(const struct syms *syms, unsigned long addr);
36+
const struct sym *syms__map_addr_dso(const struct syms *syms, unsigned long addr,
37+
char **dso_name, uint64_t *dso_offset);
3538

3639
struct syms_cache;
3740

0 commit comments

Comments
 (0)