Skip to content

Commit 3b01a41

Browse files
committed
perf symbols: Improve kallsyms symbol end addr calculation
For kallsyms we don't have the symbol address end, so we do an extra pass and set the symbol end addr as being the start of the next minus one. But this was being done just after we filtered the symbols of a particular type (functions, variables), so the symbol end was sometimes after what it really is. Fixing up symbol end also was falling apart when we have symbol aliases, then the end address of all but the last alias was being set to be before its start. Fix it up by checking for symbol aliases and making the kallsyms__parse routine use the next symbol, whatever its type, as the limit for the previous symbol, passing that end address to the callback. This was detected by the 'perf test' synthetic paranoid regression tests, fix it up so that even that case doesn't mislead us. Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Paul Mackerras <paulus@samba.org> Cc: Stephane Eranian <eranian@google.com> LKML-Reference: <new-submission> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 9fb6720 commit 3b01a41

3 files changed

Lines changed: 45 additions & 16 deletions

File tree

‎tools/perf/util/event.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ struct process_symbol_args {
332332
u64 start;
333333
};
334334

335-
static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
335+
static int find_symbol_cb(void *arg, const char *name, char type,
336+
u64 start, u64 end __used)
336337
{
337338
struct process_symbol_args *args = arg;
338339

‎tools/perf/util/symbol.c‎

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <limits.h>
2323
#include <sys/utsname.h>
2424

25+
#ifndef KSYM_NAME_LEN
26+
#define KSYM_NAME_LEN 128
27+
#endif
28+
2529
#ifndef NT_GNU_BUILD_ID
2630
#define NT_GNU_BUILD_ID 3
2731
#endif
@@ -93,7 +97,7 @@ static void symbols__fixup_end(struct rb_root *self)
9397
prev = curr;
9498
curr = rb_entry(nd, struct symbol, rb_node);
9599

96-
if (prev->end == prev->start)
100+
if (prev->end == prev->start && prev->end != curr->start)
97101
prev->end = curr->start - 1;
98102
}
99103

@@ -426,16 +430,25 @@ size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
426430

427431
int kallsyms__parse(const char *filename, void *arg,
428432
int (*process_symbol)(void *arg, const char *name,
429-
char type, u64 start))
433+
char type, u64 start, u64 end))
430434
{
431435
char *line = NULL;
432436
size_t n;
433-
int err = 0;
437+
int err = -1;
438+
u64 prev_start = 0;
439+
char prev_symbol_type = 0;
440+
char *prev_symbol_name;
434441
FILE *file = fopen(filename, "r");
435442

436443
if (file == NULL)
437444
goto out_failure;
438445

446+
prev_symbol_name = malloc(KSYM_NAME_LEN);
447+
if (prev_symbol_name == NULL)
448+
goto out_close;
449+
450+
err = 0;
451+
439452
while (!feof(file)) {
440453
u64 start;
441454
int line_len, len;
@@ -455,14 +468,33 @@ int kallsyms__parse(const char *filename, void *arg,
455468
continue;
456469

457470
symbol_type = toupper(line[len]);
458-
symbol_name = line + len + 2;
471+
len += 2;
472+
symbol_name = line + len;
473+
len = line_len - len;
459474

460-
err = process_symbol(arg, symbol_name, symbol_type, start);
461-
if (err)
475+
if (len >= KSYM_NAME_LEN) {
476+
err = -1;
462477
break;
478+
}
479+
480+
if (prev_symbol_type) {
481+
u64 end = start;
482+
if (end != prev_start)
483+
--end;
484+
err = process_symbol(arg, prev_symbol_name,
485+
prev_symbol_type, prev_start, end);
486+
if (err)
487+
break;
488+
}
489+
490+
memcpy(prev_symbol_name, symbol_name, len + 1);
491+
prev_symbol_type = symbol_type;
492+
prev_start = start;
463493
}
464494

495+
free(prev_symbol_name);
465496
free(line);
497+
out_close:
466498
fclose(file);
467499
return err;
468500

@@ -484,7 +516,7 @@ static u8 kallsyms2elf_type(char type)
484516
}
485517

486518
static int map__process_kallsym_symbol(void *arg, const char *name,
487-
char type, u64 start)
519+
char type, u64 start, u64 end)
488520
{
489521
struct symbol *sym;
490522
struct process_kallsyms_args *a = arg;
@@ -493,11 +525,8 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
493525
if (!symbol_type__is_a(type, a->map->type))
494526
return 0;
495527

496-
/*
497-
* Will fix up the end later, when we have all symbols sorted.
498-
*/
499-
sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
500-
528+
sym = symbol__new(start, end - start + 1,
529+
kallsyms2elf_type(type), name);
501530
if (sym == NULL)
502531
return -ENOMEM;
503532
/*
@@ -650,7 +679,6 @@ int dso__load_kallsyms(struct dso *self, const char *filename,
650679
if (dso__load_all_kallsyms(self, filename, map) < 0)
651680
return -1;
652681

653-
symbols__fixup_end(&self->symbols[map->type]);
654682
if (self->kernel == DSO_TYPE_GUEST_KERNEL)
655683
self->origin = DSO__ORIG_GUEST_KERNEL;
656684
else
@@ -2162,7 +2190,7 @@ struct process_args {
21622190
};
21632191

21642192
static int symbol__in_kernel(void *arg, const char *name,
2165-
char type __used, u64 start)
2193+
char type __used, u64 start, u64 end __used)
21662194
{
21672195
struct process_args *args = arg;
21682196

‎tools/perf/util/symbol.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
215215
int build_id__sprintf(const u8 *self, int len, char *bf);
216216
int kallsyms__parse(const char *filename, void *arg,
217217
int (*process_symbol)(void *arg, const char *name,
218-
char type, u64 start));
218+
char type, u64 start, u64 end));
219219

220220
void machine__destroy_kernel_maps(struct machine *self);
221221
int __machine__create_kernel_maps(struct machine *self, struct dso *kernel);

0 commit comments

Comments
 (0)