1 // SPDX-License-Identifier: GPL-2.0-only
8 #include "data-convert.h"
15 #include "linux/compiler.h"
16 #include "linux/err.h"
17 #include "util/auxtrace.h"
18 #include "util/debug.h"
20 #include "util/event.h"
21 #include "util/evsel.h"
22 #include "util/evlist.h"
23 #include "util/header.h"
25 #include "util/session.h"
26 #include "util/symbol.h"
27 #include "util/thread.h"
28 #include "util/tool.h"
30 #ifdef HAVE_LIBTRACEEVENT
31 #include <traceevent/event-parse.h>
35 struct perf_tool tool;
41 // Outputs a JSON-encoded string surrounded by quotes with characters escaped.
42 static void output_json_string(FILE *out, const char *s)
48 // required escapes with special forms as per RFC 8259
49 case '"': fputs("\\\"", out); break;
50 case '\\': fputs("\\\\", out); break;
51 case '\b': fputs("\\b", out); break;
52 case '\f': fputs("\\f", out); break;
53 case '\n': fputs("\\n", out); break;
54 case '\r': fputs("\\r", out); break;
55 case '\t': fputs("\\t", out); break;
58 // all other control characters must be escaped by hex code
60 fprintf(out, "\\u%04x", *s);
71 // Outputs an optional comma, newline and indentation to delimit a new value
72 // from the previous one in a JSON object or array.
73 static void output_json_delimiters(FILE *out, bool comma, int depth)
80 for (i = 0; i < depth; ++i)
84 // Outputs a printf format string (with delimiter) as a JSON value.
86 static void output_json_format(FILE *out, bool comma, int depth, const char *format, ...)
90 output_json_delimiters(out, comma, depth);
91 va_start(args, format);
92 vfprintf(out, format, args);
96 // Outputs a JSON key-value pair where the value is a string.
97 static void output_json_key_string(FILE *out, bool comma, int depth,
98 const char *key, const char *value)
100 output_json_delimiters(out, comma, depth);
101 output_json_string(out, key);
103 output_json_string(out, value);
106 // Outputs a JSON key-value pair where the value is a printf format string.
108 static void output_json_key_format(FILE *out, bool comma, int depth,
109 const char *key, const char *format, ...)
113 output_json_delimiters(out, comma, depth);
114 output_json_string(out, key);
116 va_start(args, format);
117 vfprintf(out, format, args);
121 static void output_sample_callchain_entry(struct perf_tool *tool,
122 u64 ip, struct addr_location *al)
124 struct convert_json *c = container_of(tool, struct convert_json, tool);
127 output_json_format(out, false, 4, "{");
128 output_json_key_format(out, false, 5, "ip", "\"0x%" PRIx64 "\"", ip);
130 if (al && al->sym && al->sym->namelen) {
131 struct dso *dso = al->map ? map__dso(al->map) : NULL;
134 output_json_key_string(out, false, 5, "symbol", al->sym->name);
137 const char *dso_name = dso->short_name;
139 if (dso_name && strlen(dso_name) > 0) {
141 output_json_key_string(out, false, 5, "dso", dso_name);
146 output_json_format(out, false, 4, "}");
149 static int process_sample_event(struct perf_tool *tool,
150 union perf_event *event __maybe_unused,
151 struct perf_sample *sample,
152 struct evsel *evsel __maybe_unused,
153 struct machine *machine)
155 struct convert_json *c = container_of(tool, struct convert_json, tool);
157 struct addr_location al, tal;
158 u64 sample_type = __evlist__combined_sample_type(evsel->evlist);
159 u8 cpumode = PERF_RECORD_MISC_USER;
161 if (machine__resolve(machine, &al, sample) < 0) {
162 pr_err("Sample resolution failed!\n");
172 output_json_format(out, false, 2, "{");
174 output_json_key_format(out, false, 3, "timestamp", "%" PRIi64, sample->time);
175 output_json_key_format(out, true, 3, "pid", "%i", al.thread->pid_);
176 output_json_key_format(out, true, 3, "tid", "%i", al.thread->tid);
178 if ((sample_type & PERF_SAMPLE_CPU))
179 output_json_key_format(out, true, 3, "cpu", "%i", sample->cpu);
180 else if (al.thread->cpu >= 0)
181 output_json_key_format(out, true, 3, "cpu", "%i", al.thread->cpu);
183 output_json_key_string(out, true, 3, "comm", thread__comm_str(al.thread));
185 output_json_key_format(out, true, 3, "callchain", "[");
186 if (sample->callchain) {
189 bool first_callchain = true;
191 for (i = 0; i < sample->callchain->nr; ++i) {
192 u64 ip = sample->callchain->ips[i];
194 if (ip >= PERF_CONTEXT_MAX) {
196 case PERF_CONTEXT_HV:
197 cpumode = PERF_RECORD_MISC_HYPERVISOR;
199 case PERF_CONTEXT_KERNEL:
200 cpumode = PERF_RECORD_MISC_KERNEL;
202 case PERF_CONTEXT_USER:
203 cpumode = PERF_RECORD_MISC_USER;
206 pr_debug("invalid callchain context: %"
207 PRId64 "\n", (s64) ip);
214 first_callchain = false;
218 ok = thread__find_symbol(al.thread, cpumode, ip, &tal);
219 output_sample_callchain_entry(tool, ip, ok ? &tal : NULL);
222 output_sample_callchain_entry(tool, sample->ip, &al);
224 output_json_format(out, false, 3, "]");
226 #ifdef HAVE_LIBTRACEEVENT
227 if (sample->raw_data) {
229 struct tep_format_field **fields;
231 fields = tep_event_fields(evsel->tp_format);
238 tep_print_field(&s, sample->raw_data, fields[i]);
239 output_json_key_string(out, true, 3, fields[i]->name, s.buffer);
247 output_json_format(out, false, 2, "}");
251 static void output_headers(struct perf_session *session, struct convert_json *c)
254 struct perf_header *header = &session->header;
256 int fd = perf_data__fd(session->data);
260 output_json_key_format(out, false, 2, "header-version", "%u", header->version);
262 ret = fstat(fd, &st);
264 time_t stctime = st.st_mtime;
267 strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&stctime));
268 output_json_key_string(out, true, 2, "captured-on", buf);
270 pr_debug("Failed to get mtime of source file, not writing captured-on");
273 output_json_key_format(out, true, 2, "data-offset", "%" PRIu64, header->data_offset);
274 output_json_key_format(out, true, 2, "data-size", "%" PRIu64, header->data_size);
275 output_json_key_format(out, true, 2, "feat-offset", "%" PRIu64, header->feat_offset);
277 output_json_key_string(out, true, 2, "hostname", header->env.hostname);
278 output_json_key_string(out, true, 2, "os-release", header->env.os_release);
279 output_json_key_string(out, true, 2, "arch", header->env.arch);
281 output_json_key_string(out, true, 2, "cpu-desc", header->env.cpu_desc);
282 output_json_key_string(out, true, 2, "cpuid", header->env.cpuid);
283 output_json_key_format(out, true, 2, "nrcpus-online", "%u", header->env.nr_cpus_online);
284 output_json_key_format(out, true, 2, "nrcpus-avail", "%u", header->env.nr_cpus_avail);
286 if (header->env.clock.enabled) {
287 output_json_key_format(out, true, 2, "clockid",
288 "%u", header->env.clock.clockid);
289 output_json_key_format(out, true, 2, "clock-time",
290 "%" PRIu64, header->env.clock.clockid_ns);
291 output_json_key_format(out, true, 2, "real-time",
292 "%" PRIu64, header->env.clock.tod_ns);
295 output_json_key_string(out, true, 2, "perf-version", header->env.version);
297 output_json_key_format(out, true, 2, "cmdline", "[");
298 for (i = 0; i < header->env.nr_cmdline; i++) {
299 output_json_delimiters(out, i != 0, 3);
300 output_json_string(c->out, header->env.cmdline_argv[i]);
302 output_json_format(out, false, 2, "]");
305 int bt_convert__perf2json(const char *input_name, const char *output_name,
306 struct perf_data_convert_opts *opts __maybe_unused)
308 struct perf_session *session;
312 struct convert_json c = {
314 .sample = process_sample_event,
315 .mmap = perf_event__process_mmap,
316 .mmap2 = perf_event__process_mmap2,
317 .comm = perf_event__process_comm,
318 .namespaces = perf_event__process_namespaces,
319 .cgroup = perf_event__process_cgroup,
320 .exit = perf_event__process_exit,
321 .fork = perf_event__process_fork,
322 .lost = perf_event__process_lost,
323 #ifdef HAVE_LIBTRACEEVENT
324 .tracing_data = perf_event__process_tracing_data,
326 .build_id = perf_event__process_build_id,
327 .id_index = perf_event__process_id_index,
328 .auxtrace_info = perf_event__process_auxtrace_info,
329 .auxtrace = perf_event__process_auxtrace,
330 .event_update = perf_event__process_event_update,
331 .ordered_events = true,
332 .ordering_requires_timestamps = true,
338 struct perf_data data = {
339 .mode = PERF_DATA_MODE_READ,
341 .force = opts->force,
345 pr_err("--all is currently unsupported for JSON output.\n");
349 pr_err("--tod is currently unsupported for JSON output.\n");
353 fd = open(output_name, O_CREAT | O_WRONLY | (opts->force ? O_TRUNC : O_EXCL), 0666);
356 pr_err("Output file exists. Use --force to overwrite it.\n");
358 pr_err("Error opening output file!\n");
362 c.out = fdopen(fd, "w");
364 fprintf(stderr, "Error opening output file!\n");
369 session = perf_session__new(&data, &c.tool);
370 if (IS_ERR(session)) {
371 fprintf(stderr, "Error creating perf session!\n");
375 if (symbol__init(&session->header.env) < 0) {
376 fprintf(stderr, "Symbol init error!\n");
377 goto err_session_delete;
380 // The opening brace is printed manually because it isn't delimited from a
381 // previous value (i.e. we don't want a leading newline)
384 // Version number for future-proofing. Most additions should be able to be
385 // done in a backwards-compatible way so this should only need to be bumped
386 // if some major breaking change must be made.
387 output_json_format(c.out, false, 1, "\"linux-perf-json-version\": 1");
390 output_json_format(c.out, true, 1, "\"headers\": {");
391 output_headers(session, &c);
392 output_json_format(c.out, false, 1, "}");
395 output_json_format(c.out, true, 1, "\"samples\": [");
396 perf_session__process_events(session);
397 output_json_format(c.out, false, 1, "]");
398 output_json_format(c.out, false, 0, "}");
402 "[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
403 data.path, output_name);
406 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
407 (ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
411 perf_session__delete(session);