perf tools: Introduce binary__fprintf()

Out of print_binary() but receiving a fp pointer and expecting that the
printer be a fprintf like function, i.e. receive a FILE pointer and
return the number of characters printed.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: yuzhoujian <yuzhoujian@didichuxing.com>
Link: http://lkml.kernel.org/n/tip-6oqnxr6lmgqe6q6p3iugnscx@git.kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 7958e541
...@@ -1164,40 +1164,40 @@ struct printer_data { ...@@ -1164,40 +1164,40 @@ struct printer_data {
bool is_printable; bool is_printable;
}; };
static void static int sample__fprintf_bpf_output(enum binary_printer_ops op,
print_sample_bpf_output_printer(enum binary_printer_ops op,
unsigned int val, unsigned int val,
void *extra) void *extra, FILE *fp)
{ {
unsigned char ch = (unsigned char)val; unsigned char ch = (unsigned char)val;
struct printer_data *printer_data = extra; struct printer_data *printer_data = extra;
int printed = 0;
switch (op) { switch (op) {
case BINARY_PRINT_DATA_BEGIN: case BINARY_PRINT_DATA_BEGIN:
printf("\n"); printed += fprintf(fp, "\n");
break; break;
case BINARY_PRINT_LINE_BEGIN: case BINARY_PRINT_LINE_BEGIN:
printf("%17s", !printer_data->line_no ? "BPF output:" : printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
" "); " ");
break; break;
case BINARY_PRINT_ADDR: case BINARY_PRINT_ADDR:
printf(" %04x:", val); printed += fprintf(fp, " %04x:", val);
break; break;
case BINARY_PRINT_NUM_DATA: case BINARY_PRINT_NUM_DATA:
printf(" %02x", val); printed += fprintf(fp, " %02x", val);
break; break;
case BINARY_PRINT_NUM_PAD: case BINARY_PRINT_NUM_PAD:
printf(" "); printed += fprintf(fp, " ");
break; break;
case BINARY_PRINT_SEP: case BINARY_PRINT_SEP:
printf(" "); printed += fprintf(fp, " ");
break; break;
case BINARY_PRINT_CHAR_DATA: case BINARY_PRINT_CHAR_DATA:
if (printer_data->hit_nul && ch) if (printer_data->hit_nul && ch)
printer_data->is_printable = false; printer_data->is_printable = false;
if (!isprint(ch)) { if (!isprint(ch)) {
printf("%c", '.'); printed += fprintf(fp, "%c", '.');
if (!printer_data->is_printable) if (!printer_data->is_printable)
break; break;
...@@ -1207,20 +1207,22 @@ print_sample_bpf_output_printer(enum binary_printer_ops op, ...@@ -1207,20 +1207,22 @@ print_sample_bpf_output_printer(enum binary_printer_ops op,
else else
printer_data->is_printable = false; printer_data->is_printable = false;
} else { } else {
printf("%c", ch); printed += fprintf(fp, "%c", ch);
} }
break; break;
case BINARY_PRINT_CHAR_PAD: case BINARY_PRINT_CHAR_PAD:
printf(" "); printed += fprintf(fp, " ");
break; break;
case BINARY_PRINT_LINE_END: case BINARY_PRINT_LINE_END:
printf("\n"); printed += fprintf(fp, "\n");
printer_data->line_no++; printer_data->line_no++;
break; break;
case BINARY_PRINT_DATA_END: case BINARY_PRINT_DATA_END:
default: default:
break; break;
} }
return printed;
} }
static void print_sample_bpf_output(struct perf_sample *sample) static void print_sample_bpf_output(struct perf_sample *sample)
...@@ -1229,7 +1231,7 @@ static void print_sample_bpf_output(struct perf_sample *sample) ...@@ -1229,7 +1231,7 @@ static void print_sample_bpf_output(struct perf_sample *sample)
struct printer_data printer_data = {0, false, true}; struct printer_data printer_data = {0, false, true};
print_binary(sample->raw_data, nr_bytes, 8, print_binary(sample->raw_data, nr_bytes, 8,
print_sample_bpf_output_printer, &printer_data); sample__fprintf_bpf_output, &printer_data);
if (printer_data.is_printable && printer_data.hit_nul) if (printer_data.is_printable && printer_data.hit_nul)
printf("%17s \"%s\"\n", "BPF string:", printf("%17s \"%s\"\n", "BPF string:",
......
...@@ -1828,16 +1828,14 @@ static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evs ...@@ -1828,16 +1828,14 @@ static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evs
goto out_put; goto out_put;
} }
static void bpf_output__printer(enum binary_printer_ops op, static int bpf_output__printer(enum binary_printer_ops op,
unsigned int val, void *extra) unsigned int val, void *extra __maybe_unused, FILE *fp)
{ {
FILE *output = extra;
unsigned char ch = (unsigned char)val; unsigned char ch = (unsigned char)val;
switch (op) { switch (op) {
case BINARY_PRINT_CHAR_DATA: case BINARY_PRINT_CHAR_DATA:
fprintf(output, "%c", isprint(ch) ? ch : '.'); return fprintf(fp, "%c", isprint(ch) ? ch : '.');
break;
case BINARY_PRINT_DATA_BEGIN: case BINARY_PRINT_DATA_BEGIN:
case BINARY_PRINT_LINE_BEGIN: case BINARY_PRINT_LINE_BEGIN:
case BINARY_PRINT_ADDR: case BINARY_PRINT_ADDR:
...@@ -1850,13 +1848,15 @@ static void bpf_output__printer(enum binary_printer_ops op, ...@@ -1850,13 +1848,15 @@ static void bpf_output__printer(enum binary_printer_ops op,
default: default:
break; break;
} }
return 0;
} }
static void bpf_output__fprintf(struct trace *trace, static void bpf_output__fprintf(struct trace *trace,
struct perf_sample *sample) struct perf_sample *sample)
{ {
print_binary(sample->raw_data, sample->raw_size, 8, binary__fprintf(sample->raw_data, sample->raw_size, 8,
bpf_output__printer, trace->output); bpf_output__printer, NULL, trace->output);
} }
static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel, static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
......
...@@ -111,50 +111,53 @@ int dump_printf(const char *fmt, ...) ...@@ -111,50 +111,53 @@ int dump_printf(const char *fmt, ...)
return ret; return ret;
} }
static void trace_event_printer(enum binary_printer_ops op, static int trace_event_printer(enum binary_printer_ops op,
unsigned int val, void *extra) unsigned int val, void *extra, FILE *fp)
{ {
const char *color = PERF_COLOR_BLUE; const char *color = PERF_COLOR_BLUE;
union perf_event *event = (union perf_event *)extra; union perf_event *event = (union perf_event *)extra;
unsigned char ch = (unsigned char)val; unsigned char ch = (unsigned char)val;
int printed = 0;
switch (op) { switch (op) {
case BINARY_PRINT_DATA_BEGIN: case BINARY_PRINT_DATA_BEGIN:
printf("."); printed += fprintf(fp, ".");
color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n", printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
event->header.size); event->header.size);
break; break;
case BINARY_PRINT_LINE_BEGIN: case BINARY_PRINT_LINE_BEGIN:
printf("."); printed += fprintf(fp, ".");
break; break;
case BINARY_PRINT_ADDR: case BINARY_PRINT_ADDR:
color_fprintf(stdout, color, " %04x: ", val); printed += color_fprintf(fp, color, " %04x: ", val);
break; break;
case BINARY_PRINT_NUM_DATA: case BINARY_PRINT_NUM_DATA:
color_fprintf(stdout, color, " %02x", val); printed += color_fprintf(fp, color, " %02x", val);
break; break;
case BINARY_PRINT_NUM_PAD: case BINARY_PRINT_NUM_PAD:
color_fprintf(stdout, color, " "); printed += color_fprintf(fp, color, " ");
break; break;
case BINARY_PRINT_SEP: case BINARY_PRINT_SEP:
color_fprintf(stdout, color, " "); printed += color_fprintf(fp, color, " ");
break; break;
case BINARY_PRINT_CHAR_DATA: case BINARY_PRINT_CHAR_DATA:
color_fprintf(stdout, color, "%c", printed += color_fprintf(fp, color, "%c",
isprint(ch) ? ch : '.'); isprint(ch) ? ch : '.');
break; break;
case BINARY_PRINT_CHAR_PAD: case BINARY_PRINT_CHAR_PAD:
color_fprintf(stdout, color, " "); printed += color_fprintf(fp, color, " ");
break; break;
case BINARY_PRINT_LINE_END: case BINARY_PRINT_LINE_END:
color_fprintf(stdout, color, "\n"); printed += color_fprintf(fp, color, "\n");
break; break;
case BINARY_PRINT_DATA_END: case BINARY_PRINT_DATA_END:
printf("\n"); printed += fprintf(fp, "\n");
break; break;
default: default:
break; break;
} }
return printed;
} }
void trace_event(union perf_event *event) void trace_event(union perf_event *event)
......
...@@ -2,40 +2,42 @@ ...@@ -2,40 +2,42 @@
#include <linux/log2.h> #include <linux/log2.h>
#include "sane_ctype.h" #include "sane_ctype.h"
void print_binary(unsigned char *data, size_t len, int binary__fprintf(unsigned char *data, size_t len,
size_t bytes_per_line, print_binary_t printer, size_t bytes_per_line, binary__fprintf_t printer,
void *extra) void *extra, FILE *fp)
{ {
size_t i, j, mask; size_t i, j, mask;
int printed = 0;
if (!printer) if (!printer)
return; return 0;
bytes_per_line = roundup_pow_of_two(bytes_per_line); bytes_per_line = roundup_pow_of_two(bytes_per_line);
mask = bytes_per_line - 1; mask = bytes_per_line - 1;
printer(BINARY_PRINT_DATA_BEGIN, 0, extra); printed += printer(BINARY_PRINT_DATA_BEGIN, 0, extra, fp);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if ((i & mask) == 0) { if ((i & mask) == 0) {
printer(BINARY_PRINT_LINE_BEGIN, -1, extra); printed += printer(BINARY_PRINT_LINE_BEGIN, -1, extra, fp);
printer(BINARY_PRINT_ADDR, i, extra); printed += printer(BINARY_PRINT_ADDR, i, extra, fp);
} }
printer(BINARY_PRINT_NUM_DATA, data[i], extra); printed += printer(BINARY_PRINT_NUM_DATA, data[i], extra, fp);
if (((i & mask) == mask) || i == len - 1) { if (((i & mask) == mask) || i == len - 1) {
for (j = 0; j < mask-(i & mask); j++) for (j = 0; j < mask-(i & mask); j++)
printer(BINARY_PRINT_NUM_PAD, -1, extra); printed += printer(BINARY_PRINT_NUM_PAD, -1, extra, fp);
printer(BINARY_PRINT_SEP, i, extra); printer(BINARY_PRINT_SEP, i, extra, fp);
for (j = i & ~mask; j <= i; j++) for (j = i & ~mask; j <= i; j++)
printer(BINARY_PRINT_CHAR_DATA, data[j], extra); printed += printer(BINARY_PRINT_CHAR_DATA, data[j], extra, fp);
for (j = 0; j < mask-(i & mask); j++) for (j = 0; j < mask-(i & mask); j++)
printer(BINARY_PRINT_CHAR_PAD, i, extra); printed += printer(BINARY_PRINT_CHAR_PAD, i, extra, fp);
printer(BINARY_PRINT_LINE_END, -1, extra); printed += printer(BINARY_PRINT_LINE_END, -1, extra, fp);
} }
} }
printer(BINARY_PRINT_DATA_END, -1, extra); printed += printer(BINARY_PRINT_DATA_END, -1, extra, fp);
return printed;
} }
int is_printable_array(char *p, unsigned int len) int is_printable_array(char *p, unsigned int len)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define PERF_PRINT_BINARY_H #define PERF_PRINT_BINARY_H
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
enum binary_printer_ops { enum binary_printer_ops {
BINARY_PRINT_DATA_BEGIN, BINARY_PRINT_DATA_BEGIN,
...@@ -16,12 +17,19 @@ enum binary_printer_ops { ...@@ -16,12 +17,19 @@ enum binary_printer_ops {
BINARY_PRINT_DATA_END, BINARY_PRINT_DATA_END,
}; };
typedef void (*print_binary_t)(enum binary_printer_ops op, typedef int (*binary__fprintf_t)(enum binary_printer_ops op,
unsigned int val, void *extra); unsigned int val, void *extra, FILE *fp);
void print_binary(unsigned char *data, size_t len, int binary__fprintf(unsigned char *data, size_t len,
size_t bytes_per_line, print_binary_t printer, size_t bytes_per_line, binary__fprintf_t printer,
void *extra); void *extra, FILE *fp);
static inline void print_binary(unsigned char *data, size_t len,
size_t bytes_per_line, binary__fprintf_t printer,
void *extra)
{
binary__fprintf(data, len, bytes_per_line, printer, extra, stdout);
}
int is_printable_array(char *p, unsigned int len); int is_printable_array(char *p, unsigned int len);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment