Skip to content

Commit b2d0dbf

Browse files
jstancekacmel
authored andcommitted
perf tests: objdump output can contain multi byte chunks
objdump's raw insn output can vary across architectures on the number of bytes per chunk (bpc) displayed and their endianness. The code-reading test relied on reading objdump output as 1 bpc. Kaixu Xia reported test failure on ARM64, where objdump displays 4 bpc: 70c48: f90027bf str xzr, [x29,hardkernel#72] 70c4c: 91224000 add x0, x0, #0x890 70c50: f90023a0 str x0, [x29,hardkernel#64] This patch adds support to read raw insn output for any bpc length. In case of 2+ bpc it also guesses objdump's display endian. Reported-and-Tested-by: Kaixu Xia <[email protected]> Signed-off-by: Jan Stancek <[email protected]> Acked-by: Adrian Hunter <[email protected]> Cc: Corey Ashford <[email protected]> Cc: David Ahern <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/07f0f7bcbda78deb423298708ef9b6a54d6b92bd.1452592712.git.jstancek@redhat.com [ Fix up pr_fmt() call to use %zd for size_t variables, fixing the build on Ubuntu cross-compiling to armhf and ppc64 ] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent b6f35ed commit b2d0dbf

File tree

1 file changed

+71
-29
lines changed

1 file changed

+71
-29
lines changed

tools/perf/tests/code-reading.c

+71-29
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,86 @@ static unsigned int hex(char c)
3333
return c - 'A' + 10;
3434
}
3535

36-
static size_t read_objdump_line(const char *line, size_t line_len, void *buf,
37-
size_t len)
36+
static size_t read_objdump_chunk(const char **line, unsigned char **buf,
37+
size_t *buf_len)
3838
{
39-
const char *p;
40-
size_t i, j = 0;
41-
42-
/* Skip to a colon */
43-
p = strchr(line, ':');
44-
if (!p)
45-
return 0;
46-
i = p + 1 - line;
39+
size_t bytes_read = 0;
40+
unsigned char *chunk_start = *buf;
4741

4842
/* Read bytes */
49-
while (j < len) {
43+
while (*buf_len > 0) {
5044
char c1, c2;
5145

52-
/* Skip spaces */
53-
for (; i < line_len; i++) {
54-
if (!isspace(line[i]))
55-
break;
56-
}
5746
/* Get 2 hex digits */
58-
if (i >= line_len || !isxdigit(line[i]))
47+
c1 = *(*line)++;
48+
if (!isxdigit(c1))
5949
break;
60-
c1 = line[i++];
61-
if (i >= line_len || !isxdigit(line[i]))
50+
c2 = *(*line)++;
51+
if (!isxdigit(c2))
6252
break;
63-
c2 = line[i++];
64-
/* Followed by a space */
65-
if (i < line_len && line[i] && !isspace(line[i]))
53+
54+
/* Store byte and advance buf */
55+
**buf = (hex(c1) << 4) | hex(c2);
56+
(*buf)++;
57+
(*buf_len)--;
58+
bytes_read++;
59+
60+
/* End of chunk? */
61+
if (isspace(**line))
6662
break;
67-
/* Store byte */
68-
*(unsigned char *)buf = (hex(c1) << 4) | hex(c2);
69-
buf += 1;
70-
j++;
7163
}
64+
65+
/*
66+
* objdump will display raw insn as LE if code endian
67+
* is LE and bytes_per_chunk > 1. In that case reverse
68+
* the chunk we just read.
69+
*
70+
* see disassemble_bytes() at binutils/objdump.c for details
71+
* how objdump chooses display endian)
72+
*/
73+
if (bytes_read > 1 && !bigendian()) {
74+
unsigned char *chunk_end = chunk_start + bytes_read - 1;
75+
unsigned char tmp;
76+
77+
while (chunk_start < chunk_end) {
78+
tmp = *chunk_start;
79+
*chunk_start = *chunk_end;
80+
*chunk_end = tmp;
81+
chunk_start++;
82+
chunk_end--;
83+
}
84+
}
85+
86+
return bytes_read;
87+
}
88+
89+
static size_t read_objdump_line(const char *line, unsigned char *buf,
90+
size_t buf_len)
91+
{
92+
const char *p;
93+
size_t ret, bytes_read = 0;
94+
95+
/* Skip to a colon */
96+
p = strchr(line, ':');
97+
if (!p)
98+
return 0;
99+
p++;
100+
101+
/* Skip initial spaces */
102+
while (*p) {
103+
if (!isspace(*p))
104+
break;
105+
p++;
106+
}
107+
108+
do {
109+
ret = read_objdump_chunk(&p, &buf, &buf_len);
110+
bytes_read += ret;
111+
p++;
112+
} while (ret > 0);
113+
72114
/* return number of successfully read bytes */
73-
return j;
115+
return bytes_read;
74116
}
75117

76118
static int read_objdump_output(FILE *f, void *buf, size_t *len, u64 start_addr)
@@ -95,7 +137,7 @@ static int read_objdump_output(FILE *f, void *buf, size_t *len, u64 start_addr)
95137
}
96138

97139
/* read objdump data into temporary buffer */
98-
read_bytes = read_objdump_line(line, ret, tmp, sizeof(tmp));
140+
read_bytes = read_objdump_line(line, tmp, sizeof(tmp));
99141
if (!read_bytes)
100142
continue;
101143

@@ -152,7 +194,7 @@ static int read_via_objdump(const char *filename, u64 addr, void *buf,
152194

153195
ret = read_objdump_output(f, buf, &len, addr);
154196
if (len) {
155-
pr_debug("objdump read too few bytes\n");
197+
pr_debug("objdump read too few bytes: %zd\n", len);
156198
if (!ret)
157199
ret = len;
158200
}

0 commit comments

Comments
 (0)