Skip to content

Commit 786c1b5

Browse files
committed
perf annotate: Start supporting cross arch annotation
Introduce a 'struct arch', where arch specific stuff will live, starting with objdump's choice of comment delimitation character, that is '#' in x86 while a ';' in arm. This has some bits and pieces from a patch submitted by Ravi. Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Chris Riyder <[email protected]> Cc: David Ahern <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kim Phillips <[email protected]> Cc: Markus Trippelsdorf <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Naveen N. Rao <[email protected]> Cc: Pawel Moll <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ravi Bangoria <[email protected]> Cc: Taeung Song <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 6a6b12e commit 786c1b5

File tree

5 files changed

+103
-23
lines changed

5 files changed

+103
-23
lines changed

tools/perf/builtin-top.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
130130
return err;
131131
}
132132

133-
err = symbol__disassemble(sym, map, 0);
133+
err = symbol__disassemble(sym, map, NULL, 0);
134134
if (err == 0) {
135135
out_assign:
136136
top->sym_filter_entry = he;

tools/perf/ui/browsers/annotate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ int symbol__tui_annotate(struct symbol *sym, struct map *map,
10501050
(nr_pcnt - 1);
10511051
}
10521052

1053-
err = symbol__disassemble(sym, map, sizeof_bdl);
1053+
err = symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), sizeof_bdl);
10541054
if (err) {
10551055
char msg[BUFSIZ];
10561056
symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));

tools/perf/ui/gtk/annotate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static int symbol__gtk_annotate(struct symbol *sym, struct map *map,
167167
if (map->dso->annotate_warned)
168168
return -1;
169169

170-
err = symbol__disassemble(sym, map, 0);
170+
err = symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 0);
171171
if (err) {
172172
char msg[BUFSIZ];
173173
symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));

tools/perf/util/annotate.c

Lines changed: 96 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
#include "annotate.h"
1919
#include "evsel.h"
2020
#include "block-range.h"
21+
#include "arch/common.h"
2122
#include <regex.h>
2223
#include <pthread.h>
2324
#include <linux/bitops.h>
25+
#include <sys/utsname.h>
2426

2527
const char *disassembler_style;
2628
const char *objdump_path;
@@ -29,6 +31,28 @@ static regex_t file_lineno;
2931
static struct ins *ins__find(const char *name);
3032
static int disasm_line__parse(char *line, char **namep, char **rawp);
3133

34+
struct arch {
35+
const char *name;
36+
struct {
37+
char comment_char;
38+
} objdump;
39+
};
40+
41+
static struct arch architectures[] = {
42+
{
43+
.name = "arm",
44+
.objdump = {
45+
.comment_char = ';',
46+
},
47+
},
48+
{
49+
.name = "x86",
50+
.objdump = {
51+
.comment_char = '#',
52+
},
53+
},
54+
};
55+
3256
static void ins__delete(struct ins_operands *ops)
3357
{
3458
if (ops == NULL)
@@ -54,7 +78,7 @@ int ins__scnprintf(struct ins *ins, char *bf, size_t size,
5478
return ins__raw_scnprintf(ins, bf, size, ops);
5579
}
5680

57-
static int call__parse(struct ins_operands *ops, struct map *map)
81+
static int call__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map)
5882
{
5983
char *endptr, *tok, *name;
6084

@@ -118,7 +142,7 @@ bool ins__is_call(const struct ins *ins)
118142
return ins->ops == &call_ops;
119143
}
120144

121-
static int jump__parse(struct ins_operands *ops, struct map *map __maybe_unused)
145+
static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
122146
{
123147
const char *s = strchr(ops->raw, '+');
124148

@@ -173,7 +197,7 @@ static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
173197
return 0;
174198
}
175199

176-
static int lock__parse(struct ins_operands *ops, struct map *map)
200+
static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
177201
{
178202
char *name;
179203

@@ -194,7 +218,7 @@ static int lock__parse(struct ins_operands *ops, struct map *map)
194218
return 0;
195219

196220
if (ops->locked.ins->ops->parse &&
197-
ops->locked.ins->ops->parse(ops->locked.ops, map) < 0)
221+
ops->locked.ins->ops->parse(arch, ops->locked.ops, map) < 0)
198222
goto out_free_ops;
199223

200224
return 0;
@@ -237,7 +261,7 @@ static struct ins_ops lock_ops = {
237261
.scnprintf = lock__scnprintf,
238262
};
239263

240-
static int mov__parse(struct ins_operands *ops, struct map *map __maybe_unused)
264+
static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
241265
{
242266
char *s = strchr(ops->raw, ','), *target, *comment, prev;
243267

@@ -252,11 +276,7 @@ static int mov__parse(struct ins_operands *ops, struct map *map __maybe_unused)
252276
return -1;
253277

254278
target = ++s;
255-
#ifdef __arm__
256-
comment = strchr(s, ';');
257-
#else
258-
comment = strchr(s, '#');
259-
#endif
279+
comment = strchr(s, arch->objdump.comment_char);
260280

261281
if (comment != NULL)
262282
s = comment - 1;
@@ -304,7 +324,7 @@ static struct ins_ops mov_ops = {
304324
.scnprintf = mov__scnprintf,
305325
};
306326

307-
static int dec__parse(struct ins_operands *ops, struct map *map __maybe_unused)
327+
static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
308328
{
309329
char *target, *comment, *s, prev;
310330

@@ -492,6 +512,41 @@ static struct ins *ins__find(const char *name)
492512
return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__key_cmp);
493513
}
494514

515+
static int arch__key_cmp(const void *name, const void *archp)
516+
{
517+
const struct arch *arch = archp;
518+
519+
return strcmp(name, arch->name);
520+
}
521+
522+
static int arch__cmp(const void *a, const void *b)
523+
{
524+
const struct arch *aa = a;
525+
const struct arch *ab = b;
526+
527+
return strcmp(aa->name, ab->name);
528+
}
529+
530+
static void arch__sort(void)
531+
{
532+
const int nmemb = ARRAY_SIZE(architectures);
533+
534+
qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
535+
}
536+
537+
static struct arch *arch__find(const char *name)
538+
{
539+
const int nmemb = ARRAY_SIZE(architectures);
540+
static bool sorted;
541+
542+
if (!sorted) {
543+
arch__sort();
544+
sorted = true;
545+
}
546+
547+
return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
548+
}
549+
495550
int symbol__alloc_hist(struct symbol *sym)
496551
{
497552
struct annotation *notes = symbol__annotation(sym);
@@ -709,7 +764,7 @@ int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
709764
return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
710765
}
711766

712-
static void disasm_line__init_ins(struct disasm_line *dl, struct map *map)
767+
static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
713768
{
714769
dl->ins = ins__find(dl->name);
715770

@@ -719,7 +774,7 @@ static void disasm_line__init_ins(struct disasm_line *dl, struct map *map)
719774
if (!dl->ins->ops)
720775
return;
721776

722-
if (dl->ins->ops->parse && dl->ins->ops->parse(&dl->ops, map) < 0)
777+
if (dl->ins->ops->parse && dl->ins->ops->parse(arch, &dl->ops, map) < 0)
723778
dl->ins = NULL;
724779
}
725780

@@ -762,6 +817,7 @@ static int disasm_line__parse(char *line, char **namep, char **rawp)
762817

763818
static struct disasm_line *disasm_line__new(s64 offset, char *line,
764819
size_t privsize, int line_nr,
820+
struct arch *arch,
765821
struct map *map)
766822
{
767823
struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
@@ -777,7 +833,7 @@ static struct disasm_line *disasm_line__new(s64 offset, char *line,
777833
if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
778834
goto out_free_line;
779835

780-
disasm_line__init_ins(dl, map);
836+
disasm_line__init_ins(dl, arch, map);
781837
}
782838
}
783839

@@ -1087,6 +1143,7 @@ static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 st
10871143
* The ops.raw part will be parsed further according to type of the instruction.
10881144
*/
10891145
static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
1146+
struct arch *arch,
10901147
FILE *file, size_t privsize,
10911148
int *line_nr)
10921149
{
@@ -1149,7 +1206,7 @@ static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
11491206
parsed_line = tmp2 + 1;
11501207
}
11511208

1152-
dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, map);
1209+
dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
11531210
free(line);
11541211
(*line_nr)++;
11551212

@@ -1280,10 +1337,23 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
12801337
return 0;
12811338
}
12821339

1283-
int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)
1340+
static const char *annotate__norm_arch(const char *arch_name)
1341+
{
1342+
struct utsname uts;
1343+
1344+
if (!arch_name) { /* Assume we are annotating locally. */
1345+
if (uname(&uts) < 0)
1346+
return NULL;
1347+
arch_name = uts.machine;
1348+
}
1349+
return normalize_arch((char *)arch_name);
1350+
}
1351+
1352+
int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_name, size_t privsize)
12841353
{
12851354
struct dso *dso = map->dso;
12861355
char command[PATH_MAX * 2];
1356+
struct arch *arch = NULL;
12871357
FILE *file;
12881358
char symfs_filename[PATH_MAX];
12891359
struct kcore_extract kce;
@@ -1297,6 +1367,14 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)
12971367
if (err)
12981368
return err;
12991369

1370+
arch_name = annotate__norm_arch(arch_name);
1371+
if (!arch_name)
1372+
return -1;
1373+
1374+
arch = arch__find(arch_name);
1375+
if (arch == NULL)
1376+
return -ENOTSUP;
1377+
13001378
pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
13011379
symfs_filename, sym->name, map->unmap_ip(map, sym->start),
13021380
map->unmap_ip(map, sym->end));
@@ -1395,7 +1473,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)
13951473

13961474
nline = 0;
13971475
while (!feof(file)) {
1398-
if (symbol__parse_objdump_line(sym, map, file, privsize,
1476+
if (symbol__parse_objdump_line(sym, map, arch, file, privsize,
13991477
&lineno) < 0)
14001478
break;
14011479
nline++;
@@ -1793,7 +1871,7 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
17931871
struct rb_root source_line = RB_ROOT;
17941872
u64 len;
17951873

1796-
if (symbol__disassemble(sym, map, 0) < 0)
1874+
if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 0) < 0)
17971875
return -1;
17981876

17991877
len = symbol__size(sym);

tools/perf/util/annotate.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ struct ins_operands {
3434
};
3535
};
3636

37+
struct arch;
38+
3739
struct ins_ops {
3840
void (*free)(struct ins_operands *ops);
39-
int (*parse)(struct ins_operands *ops, struct map *map);
41+
int (*parse)(struct arch *arch, struct ins_operands *ops, struct map *map);
4042
int (*scnprintf)(struct ins *ins, char *bf, size_t size,
4143
struct ins_operands *ops);
4244
};
@@ -156,7 +158,7 @@ int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 addr);
156158
int symbol__alloc_hist(struct symbol *sym);
157159
void symbol__annotate_zero_histograms(struct symbol *sym);
158160

159-
int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize);
161+
int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_name, size_t privsize);
160162

161163
enum symbol_disassemble_errno {
162164
SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,

0 commit comments

Comments
 (0)