1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2021, CodeWeavers Inc. <nfraser@codeweavers.com>
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"
31 struct perf_tool tool;
37 // Outputs a JSON-encoded string surrounded by quotes with characters escaped.
38 static void output_json_string(FILE *out, const char *s)
44 // required escapes with special forms as per RFC 8259
45 case '"': fputs("\\\"", out); break;
46 case '\\': fputs("\\\\", out); break;
47 case '\b': fputs("\\b", out); break;
48 case '\f': fputs("\\f", out); break;
49 case '\n': fputs("\\n", out); break;
50 case '\r': fputs("\\r", out); break;
51 case '\t': fputs("\\t", out); break;
54 // all other control characters must be escaped by hex code
56 fprintf(out, "\\u%04x", *s);
67 // Outputs an optional comma, newline and indentation to delimit a new value
68 // from the previous one in a JSON object or array.
69 static void output_json_delimiters(FILE *out, bool comma, int depth)
76 for (i = 0; i < depth; ++i)
80 // Outputs a printf format string (with delimiter) as a JSON value.
82 static void output_json_format(FILE *out, bool comma, int depth, const char *format, ...)
86 output_json_delimiters(out, comma, depth);
87 va_start(args, format);
88 vfprintf(out, format, args);
92 // Outputs a JSON key-value pair where the value is a string.
93 static void output_json_key_string(FILE *out, bool comma, int depth,
94 const char *key, const char *value)
96 output_json_delimiters(out, comma, depth);
97 output_json_string(out, key);
99 output_json_string(out, value);
102 // Outputs a JSON key-value pair where the value is a printf format string.
104 static void output_json_key_format(FILE *out, bool comma, int depth,
105 const char *key, const char *format, ...)
109 output_json_delimiters(out, comma, depth);
110 output_json_string(out, key);
112 va_start(args, format);
113 vfprintf(out, format, args);
117 static void output_sample_callchain_entry(struct perf_tool *tool,
118 u64 ip, struct addr_location *al)
120 struct convert_json *c = container_of(tool, struct convert_json, tool);
123 output_json_format(out, false, 4, "{");
124 output_json_key_format(out, false, 5, "ip", "\"0x%" PRIx64 "\"", ip);
126 if (al && al->sym && al->sym->namelen) {
128 output_json_key_string(out, false, 5, "symbol", al->sym->name);
130 if (al->map && al->map->dso) {
131 const char *dso = al->map->dso->short_name;
133 if (dso && strlen(dso) > 0) {
135 output_json_key_string(out, false, 5, "dso", dso);
140 output_json_format(out, false, 4, "}");
143 static int process_sample_event(struct perf_tool *tool,
144 union perf_event *event __maybe_unused,
145 struct perf_sample *sample,
146 struct evsel *evsel __maybe_unused,
147 struct machine *machine)
149 struct convert_json *c = container_of(tool, struct convert_json, tool);
151 struct addr_location al, tal;
152 u8 cpumode = PERF_RECORD_MISC_USER;
154 if (machine__resolve(machine, &al, sample) < 0) {
155 pr_err("Sample resolution failed!\n");
165 output_json_format(out, false, 2, "{");
167 output_json_key_format(out, false, 3, "timestamp", "%" PRIi64, sample->time);
168 output_json_key_format(out, true, 3, "pid", "%i", al.thread->pid_);
169 output_json_key_format(out, true, 3, "tid", "%i", al.thread->tid);
171 if (al.thread->cpu >= 0)
172 output_json_key_format(out, true, 3, "cpu", "%i", al.thread->cpu);
174 output_json_key_string(out, true, 3, "comm", thread__comm_str(al.thread));
176 output_json_key_format(out, true, 3, "callchain", "[");
177 if (sample->callchain) {
180 bool first_callchain = true;
182 for (i = 0; i < sample->callchain->nr; ++i) {
183 u64 ip = sample->callchain->ips[i];
185 if (ip >= PERF_CONTEXT_MAX) {
187 case PERF_CONTEXT_HV:
188 cpumode = PERF_RECORD_MISC_HYPERVISOR;
190 case PERF_CONTEXT_KERNEL:
191 cpumode = PERF_RECORD_MISC_KERNEL;
193 case PERF_CONTEXT_USER:
194 cpumode = PERF_RECORD_MISC_USER;
197 pr_debug("invalid callchain context: %"
198 PRId64 "\n", (s64) ip);
205 first_callchain = false;
209 ok = thread__find_symbol(al.thread, cpumode, ip, &tal);
210 output_sample_callchain_entry(tool, ip, ok ? &tal : NULL);
213 output_sample_callchain_entry(tool, sample->ip, &al);
215 output_json_format(out, false, 3, "]");
217 output_json_format(out, false, 2, "}");
221 static void output_headers(struct perf_session *session, struct convert_json *c)
224 struct perf_header *header = &session->header;
226 int fd = perf_data__fd(session->data);
230 output_json_key_format(out, false, 2, "header-version", "%u", header->version);
232 ret = fstat(fd, &st);
234 time_t stctime = st.st_mtime;
237 strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&stctime));
238 output_json_key_string(out, true, 2, "captured-on", buf);
240 pr_debug("Failed to get mtime of source file, not writing captured-on");
243 output_json_key_format(out, true, 2, "data-offset", "%" PRIu64, header->data_offset);
244 output_json_key_format(out, true, 2, "data-size", "%" PRIu64, header->data_size);
245 output_json_key_format(out, true, 2, "feat-offset", "%" PRIu64, header->feat_offset);
247 output_json_key_string(out, true, 2, "hostname", header->env.hostname);
248 output_json_key_string(out, true, 2, "os-release", header->env.os_release);
249 output_json_key_string(out, true, 2, "arch", header->env.arch);
251 output_json_key_string(out, true, 2, "cpu-desc", header->env.cpu_desc);
252 output_json_key_string(out, true, 2, "cpuid", header->env.cpuid);
253 output_json_key_format(out, true, 2, "nrcpus-online", "%u", header->env.nr_cpus_online);
254 output_json_key_format(out, true, 2, "nrcpus-avail", "%u", header->env.nr_cpus_avail);
256 if (header->env.clock.enabled) {
257 output_json_key_format(out, true, 2, "clockid",
258 "%u", header->env.clock.clockid);
259 output_json_key_format(out, true, 2, "clock-time",
260 "%" PRIu64, header->env.clock.clockid_ns);
261 output_json_key_format(out, true, 2, "real-time",
262 "%" PRIu64, header->env.clock.tod_ns);
265 output_json_key_string(out, true, 2, "perf-version", header->env.version);
267 output_json_key_format(out, true, 2, "cmdline", "[");
268 for (i = 0; i < header->env.nr_cmdline; i++) {
269 output_json_delimiters(out, i != 0, 3);
270 output_json_string(c->out, header->env.cmdline_argv[i]);
272 output_json_format(out, false, 2, "]");
275 int bt_convert__perf2json(const char *input_name, const char *output_name,
276 struct perf_data_convert_opts *opts __maybe_unused)
278 struct perf_session *session;
282 struct convert_json c = {
284 .sample = process_sample_event,
285 .mmap = perf_event__process_mmap,
286 .mmap2 = perf_event__process_mmap2,
287 .comm = perf_event__process_comm,
288 .namespaces = perf_event__process_namespaces,
289 .cgroup = perf_event__process_cgroup,
290 .exit = perf_event__process_exit,
291 .fork = perf_event__process_fork,
292 .lost = perf_event__process_lost,
293 .tracing_data = perf_event__process_tracing_data,
294 .build_id = perf_event__process_build_id,
295 .id_index = perf_event__process_id_index,
296 .auxtrace_info = perf_event__process_auxtrace_info,
297 .auxtrace = perf_event__process_auxtrace,
298 .event_update = perf_event__process_event_update,
299 .ordered_events = true,
300 .ordering_requires_timestamps = true,
306 struct perf_data data = {
307 .mode = PERF_DATA_MODE_READ,
309 .force = opts->force,
313 pr_err("--all is currently unsupported for JSON output.\n");
317 pr_err("--tod is currently unsupported for JSON output.\n");
321 fd = open(output_name, O_CREAT | O_WRONLY | (opts->force ? O_TRUNC : O_EXCL), 0666);
324 pr_err("Output file exists. Use --force to overwrite it.\n");
326 pr_err("Error opening output file!\n");
330 c.out = fdopen(fd, "w");
332 fprintf(stderr, "Error opening output file!\n");
337 session = perf_session__new(&data, &c.tool);
338 if (IS_ERR(session)) {
339 fprintf(stderr, "Error creating perf session!\n");
343 if (symbol__init(&session->header.env) < 0) {
344 fprintf(stderr, "Symbol init error!\n");
345 goto err_session_delete;
348 // The opening brace is printed manually because it isn't delimited from a
349 // previous value (i.e. we don't want a leading newline)
352 // Version number for future-proofing. Most additions should be able to be
353 // done in a backwards-compatible way so this should only need to be bumped
354 // if some major breaking change must be made.
355 output_json_format(c.out, false, 1, "\"linux-perf-json-version\": 1");
358 output_json_format(c.out, true, 1, "\"headers\": {");
359 output_headers(session, &c);
360 output_json_format(c.out, false, 1, "}");
363 output_json_format(c.out, true, 1, "\"samples\": [");
364 perf_session__process_events(session);
365 output_json_format(c.out, false, 1, "]");
366 output_json_format(c.out, false, 0, "}");
370 "[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
371 data.path, output_name);
374 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
375 (ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
379 perf_session__delete(session);