1d0713d4cSNicholas Fraser // SPDX-License-Identifier: GPL-2.0-only
2d0713d4cSNicholas Fraser /*
3d0713d4cSNicholas Fraser * JSON export.
4d0713d4cSNicholas Fraser *
5d0713d4cSNicholas Fraser * Copyright (C) 2021, CodeWeavers Inc. <nfraser@codeweavers.com>
6d0713d4cSNicholas Fraser */
7d0713d4cSNicholas Fraser
8d0713d4cSNicholas Fraser #include "data-convert.h"
9d0713d4cSNicholas Fraser
10d0713d4cSNicholas Fraser #include <fcntl.h>
11d0713d4cSNicholas Fraser #include <inttypes.h>
12d0713d4cSNicholas Fraser #include <sys/stat.h>
13d0713d4cSNicholas Fraser #include <unistd.h>
14d0713d4cSNicholas Fraser
15d0713d4cSNicholas Fraser #include "linux/compiler.h"
16d0713d4cSNicholas Fraser #include "linux/err.h"
17d0713d4cSNicholas Fraser #include "util/auxtrace.h"
18d0713d4cSNicholas Fraser #include "util/debug.h"
19d0713d4cSNicholas Fraser #include "util/dso.h"
20d0713d4cSNicholas Fraser #include "util/event.h"
21d0713d4cSNicholas Fraser #include "util/evsel.h"
22d0713d4cSNicholas Fraser #include "util/evlist.h"
23d0713d4cSNicholas Fraser #include "util/header.h"
24d0713d4cSNicholas Fraser #include "util/map.h"
25d0713d4cSNicholas Fraser #include "util/session.h"
26d0713d4cSNicholas Fraser #include "util/symbol.h"
27d0713d4cSNicholas Fraser #include "util/thread.h"
28d0713d4cSNicholas Fraser #include "util/tool.h"
29d0713d4cSNicholas Fraser
30378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
31378ef0f5SIan Rogers #include <traceevent/event-parse.h>
32378ef0f5SIan Rogers #endif
33378ef0f5SIan Rogers
34d0713d4cSNicholas Fraser struct convert_json {
35d0713d4cSNicholas Fraser struct perf_tool tool;
36d0713d4cSNicholas Fraser FILE *out;
37d0713d4cSNicholas Fraser bool first;
38d0713d4cSNicholas Fraser u64 events_count;
39d0713d4cSNicholas Fraser };
40d0713d4cSNicholas Fraser
41d0713d4cSNicholas Fraser // Outputs a JSON-encoded string surrounded by quotes with characters escaped.
output_json_string(FILE * out,const char * s)42d0713d4cSNicholas Fraser static void output_json_string(FILE *out, const char *s)
43d0713d4cSNicholas Fraser {
44d0713d4cSNicholas Fraser fputc('"', out);
45d0713d4cSNicholas Fraser while (*s) {
46d0713d4cSNicholas Fraser switch (*s) {
47d0713d4cSNicholas Fraser
48d0713d4cSNicholas Fraser // required escapes with special forms as per RFC 8259
49d0713d4cSNicholas Fraser case '"': fputs("\\\"", out); break;
50d0713d4cSNicholas Fraser case '\\': fputs("\\\\", out); break;
51d0713d4cSNicholas Fraser case '\b': fputs("\\b", out); break;
52d0713d4cSNicholas Fraser case '\f': fputs("\\f", out); break;
53d0713d4cSNicholas Fraser case '\n': fputs("\\n", out); break;
54d0713d4cSNicholas Fraser case '\r': fputs("\\r", out); break;
55d0713d4cSNicholas Fraser case '\t': fputs("\\t", out); break;
56d0713d4cSNicholas Fraser
57d0713d4cSNicholas Fraser default:
58d0713d4cSNicholas Fraser // all other control characters must be escaped by hex code
59d0713d4cSNicholas Fraser if (*s <= 0x1f)
60d0713d4cSNicholas Fraser fprintf(out, "\\u%04x", *s);
61d0713d4cSNicholas Fraser else
62d0713d4cSNicholas Fraser fputc(*s, out);
63d0713d4cSNicholas Fraser break;
64d0713d4cSNicholas Fraser }
65d0713d4cSNicholas Fraser
66d0713d4cSNicholas Fraser ++s;
67d0713d4cSNicholas Fraser }
68d0713d4cSNicholas Fraser fputc('"', out);
69d0713d4cSNicholas Fraser }
70d0713d4cSNicholas Fraser
71d0713d4cSNicholas Fraser // Outputs an optional comma, newline and indentation to delimit a new value
72d0713d4cSNicholas Fraser // from the previous one in a JSON object or array.
output_json_delimiters(FILE * out,bool comma,int depth)73d0713d4cSNicholas Fraser static void output_json_delimiters(FILE *out, bool comma, int depth)
74d0713d4cSNicholas Fraser {
75d0713d4cSNicholas Fraser int i;
76d0713d4cSNicholas Fraser
77d0713d4cSNicholas Fraser if (comma)
78d0713d4cSNicholas Fraser fputc(',', out);
79d0713d4cSNicholas Fraser fputc('\n', out);
80d0713d4cSNicholas Fraser for (i = 0; i < depth; ++i)
81d0713d4cSNicholas Fraser fputc('\t', out);
82d0713d4cSNicholas Fraser }
83d0713d4cSNicholas Fraser
84d0713d4cSNicholas Fraser // Outputs a printf format string (with delimiter) as a JSON value.
85d0713d4cSNicholas Fraser __printf(4, 5)
output_json_format(FILE * out,bool comma,int depth,const char * format,...)86d0713d4cSNicholas Fraser static void output_json_format(FILE *out, bool comma, int depth, const char *format, ...)
87d0713d4cSNicholas Fraser {
88d0713d4cSNicholas Fraser va_list args;
89d0713d4cSNicholas Fraser
90d0713d4cSNicholas Fraser output_json_delimiters(out, comma, depth);
91d0713d4cSNicholas Fraser va_start(args, format);
92d0713d4cSNicholas Fraser vfprintf(out, format, args);
93d0713d4cSNicholas Fraser va_end(args);
94d0713d4cSNicholas Fraser }
95d0713d4cSNicholas Fraser
96d0713d4cSNicholas Fraser // Outputs a JSON key-value pair where the value is a string.
output_json_key_string(FILE * out,bool comma,int depth,const char * key,const char * value)97d0713d4cSNicholas Fraser static void output_json_key_string(FILE *out, bool comma, int depth,
98d0713d4cSNicholas Fraser const char *key, const char *value)
99d0713d4cSNicholas Fraser {
100d0713d4cSNicholas Fraser output_json_delimiters(out, comma, depth);
101d0713d4cSNicholas Fraser output_json_string(out, key);
102d0713d4cSNicholas Fraser fputs(": ", out);
103d0713d4cSNicholas Fraser output_json_string(out, value);
104d0713d4cSNicholas Fraser }
105d0713d4cSNicholas Fraser
106d0713d4cSNicholas Fraser // Outputs a JSON key-value pair where the value is a printf format string.
107d0713d4cSNicholas Fraser __printf(5, 6)
output_json_key_format(FILE * out,bool comma,int depth,const char * key,const char * format,...)108d0713d4cSNicholas Fraser static void output_json_key_format(FILE *out, bool comma, int depth,
109d0713d4cSNicholas Fraser const char *key, const char *format, ...)
110d0713d4cSNicholas Fraser {
111d0713d4cSNicholas Fraser va_list args;
112d0713d4cSNicholas Fraser
113d0713d4cSNicholas Fraser output_json_delimiters(out, comma, depth);
114d0713d4cSNicholas Fraser output_json_string(out, key);
115d0713d4cSNicholas Fraser fputs(": ", out);
116d0713d4cSNicholas Fraser va_start(args, format);
117d0713d4cSNicholas Fraser vfprintf(out, format, args);
118d0713d4cSNicholas Fraser va_end(args);
119d0713d4cSNicholas Fraser }
120d0713d4cSNicholas Fraser
output_sample_callchain_entry(struct perf_tool * tool,u64 ip,struct addr_location * al)121d0713d4cSNicholas Fraser static void output_sample_callchain_entry(struct perf_tool *tool,
122d0713d4cSNicholas Fraser u64 ip, struct addr_location *al)
123d0713d4cSNicholas Fraser {
124d0713d4cSNicholas Fraser struct convert_json *c = container_of(tool, struct convert_json, tool);
125d0713d4cSNicholas Fraser FILE *out = c->out;
126d0713d4cSNicholas Fraser
127d0713d4cSNicholas Fraser output_json_format(out, false, 4, "{");
128d0713d4cSNicholas Fraser output_json_key_format(out, false, 5, "ip", "\"0x%" PRIx64 "\"", ip);
129d0713d4cSNicholas Fraser
130d0713d4cSNicholas Fraser if (al && al->sym && al->sym->namelen) {
131*63df0e4bSIan Rogers struct dso *dso = al->map ? map__dso(al->map) : NULL;
132*63df0e4bSIan Rogers
133d0713d4cSNicholas Fraser fputc(',', out);
134d0713d4cSNicholas Fraser output_json_key_string(out, false, 5, "symbol", al->sym->name);
135d0713d4cSNicholas Fraser
136*63df0e4bSIan Rogers if (dso) {
137*63df0e4bSIan Rogers const char *dso_name = dso->short_name;
138d0713d4cSNicholas Fraser
139*63df0e4bSIan Rogers if (dso_name && strlen(dso_name) > 0) {
140d0713d4cSNicholas Fraser fputc(',', out);
141*63df0e4bSIan Rogers output_json_key_string(out, false, 5, "dso", dso_name);
142d0713d4cSNicholas Fraser }
143d0713d4cSNicholas Fraser }
144d0713d4cSNicholas Fraser }
145d0713d4cSNicholas Fraser
146d0713d4cSNicholas Fraser output_json_format(out, false, 4, "}");
147d0713d4cSNicholas Fraser }
148d0713d4cSNicholas Fraser
process_sample_event(struct perf_tool * tool,union perf_event * event __maybe_unused,struct perf_sample * sample,struct evsel * evsel __maybe_unused,struct machine * machine)149d0713d4cSNicholas Fraser static int process_sample_event(struct perf_tool *tool,
150d0713d4cSNicholas Fraser union perf_event *event __maybe_unused,
151d0713d4cSNicholas Fraser struct perf_sample *sample,
152d0713d4cSNicholas Fraser struct evsel *evsel __maybe_unused,
153d0713d4cSNicholas Fraser struct machine *machine)
154d0713d4cSNicholas Fraser {
155d0713d4cSNicholas Fraser struct convert_json *c = container_of(tool, struct convert_json, tool);
156d0713d4cSNicholas Fraser FILE *out = c->out;
157d0713d4cSNicholas Fraser struct addr_location al;
158924a2215SShawn M. Chapla u64 sample_type = __evlist__combined_sample_type(evsel->evlist);
159d0713d4cSNicholas Fraser u8 cpumode = PERF_RECORD_MISC_USER;
160d0713d4cSNicholas Fraser
161d0713d4cSNicholas Fraser addr_location__init(&al);
162d0713d4cSNicholas Fraser if (machine__resolve(machine, &al, sample) < 0) {
163d0713d4cSNicholas Fraser pr_err("Sample resolution failed!\n");
164d0713d4cSNicholas Fraser addr_location__exit(&al);
165d0713d4cSNicholas Fraser return -1;
166d0713d4cSNicholas Fraser }
167d0713d4cSNicholas Fraser
168d0713d4cSNicholas Fraser ++c->events_count;
169d0713d4cSNicholas Fraser
170d0713d4cSNicholas Fraser if (c->first)
171d0713d4cSNicholas Fraser c->first = false;
172d0713d4cSNicholas Fraser else
173d0713d4cSNicholas Fraser fputc(',', out);
174d0713d4cSNicholas Fraser output_json_format(out, false, 2, "{");
175d0713d4cSNicholas Fraser
176d0713d4cSNicholas Fraser output_json_key_format(out, false, 3, "timestamp", "%" PRIi64, sample->time);
177d0713d4cSNicholas Fraser output_json_key_format(out, true, 3, "pid", "%i", thread__pid(al.thread));
178924a2215SShawn M. Chapla output_json_key_format(out, true, 3, "tid", "%i", thread__tid(al.thread));
179924a2215SShawn M. Chapla
180924a2215SShawn M. Chapla if ((sample_type & PERF_SAMPLE_CPU))
181d0713d4cSNicholas Fraser output_json_key_format(out, true, 3, "cpu", "%i", sample->cpu);
182d0713d4cSNicholas Fraser else if (thread__cpu(al.thread) >= 0)
183d0713d4cSNicholas Fraser output_json_key_format(out, true, 3, "cpu", "%i", thread__cpu(al.thread));
184d0713d4cSNicholas Fraser
185d0713d4cSNicholas Fraser output_json_key_string(out, true, 3, "comm", thread__comm_str(al.thread));
186d0713d4cSNicholas Fraser
187d0713d4cSNicholas Fraser output_json_key_format(out, true, 3, "callchain", "[");
188d0713d4cSNicholas Fraser if (sample->callchain) {
189d0713d4cSNicholas Fraser unsigned int i;
190d0713d4cSNicholas Fraser bool ok;
191d0713d4cSNicholas Fraser bool first_callchain = true;
192d0713d4cSNicholas Fraser
193d0713d4cSNicholas Fraser for (i = 0; i < sample->callchain->nr; ++i) {
194d0713d4cSNicholas Fraser u64 ip = sample->callchain->ips[i];
195d0713d4cSNicholas Fraser struct addr_location tal;
196d0713d4cSNicholas Fraser
197d0713d4cSNicholas Fraser if (ip >= PERF_CONTEXT_MAX) {
198d0713d4cSNicholas Fraser switch (ip) {
199d0713d4cSNicholas Fraser case PERF_CONTEXT_HV:
200d0713d4cSNicholas Fraser cpumode = PERF_RECORD_MISC_HYPERVISOR;
201d0713d4cSNicholas Fraser break;
202d0713d4cSNicholas Fraser case PERF_CONTEXT_KERNEL:
203d0713d4cSNicholas Fraser cpumode = PERF_RECORD_MISC_KERNEL;
204d0713d4cSNicholas Fraser break;
205d0713d4cSNicholas Fraser case PERF_CONTEXT_USER:
206d0713d4cSNicholas Fraser cpumode = PERF_RECORD_MISC_USER;
207d0713d4cSNicholas Fraser break;
208d0713d4cSNicholas Fraser default:
209d0713d4cSNicholas Fraser pr_debug("invalid callchain context: %"
210d0713d4cSNicholas Fraser PRId64 "\n", (s64) ip);
211d0713d4cSNicholas Fraser break;
212d0713d4cSNicholas Fraser }
213d0713d4cSNicholas Fraser continue;
214d0713d4cSNicholas Fraser }
215d0713d4cSNicholas Fraser
216d0713d4cSNicholas Fraser if (first_callchain)
217d0713d4cSNicholas Fraser first_callchain = false;
218d0713d4cSNicholas Fraser else
219d0713d4cSNicholas Fraser fputc(',', out);
220d0713d4cSNicholas Fraser
221d0713d4cSNicholas Fraser addr_location__init(&tal);
222d0713d4cSNicholas Fraser ok = thread__find_symbol(al.thread, cpumode, ip, &tal);
223d0713d4cSNicholas Fraser output_sample_callchain_entry(tool, ip, ok ? &tal : NULL);
224d0713d4cSNicholas Fraser addr_location__exit(&tal);
225d0713d4cSNicholas Fraser }
226378ef0f5SIan Rogers } else {
2279d895e46SDmitrii Dolgov output_sample_callchain_entry(tool, sample->ip, &al);
2289d895e46SDmitrii Dolgov }
2299d895e46SDmitrii Dolgov output_json_format(out, false, 3, "]");
2309d895e46SDmitrii Dolgov
2319d895e46SDmitrii Dolgov #ifdef HAVE_LIBTRACEEVENT
2329d895e46SDmitrii Dolgov if (sample->raw_data) {
2339d895e46SDmitrii Dolgov int i;
2349d895e46SDmitrii Dolgov struct tep_format_field **fields;
2359d895e46SDmitrii Dolgov
2369d895e46SDmitrii Dolgov fields = tep_event_fields(evsel->tp_format);
2379d895e46SDmitrii Dolgov if (fields) {
2389d895e46SDmitrii Dolgov i = 0;
2399d895e46SDmitrii Dolgov while (fields[i]) {
2409d895e46SDmitrii Dolgov struct trace_seq s;
2419d895e46SDmitrii Dolgov
2429d895e46SDmitrii Dolgov trace_seq_init(&s);
2439d895e46SDmitrii Dolgov tep_print_field(&s, sample->raw_data, fields[i]);
2449d895e46SDmitrii Dolgov output_json_key_string(out, true, 3, fields[i]->name, s.buffer);
2459d895e46SDmitrii Dolgov
246378ef0f5SIan Rogers i++;
247d0713d4cSNicholas Fraser }
248d0713d4cSNicholas Fraser free(fields);
249d0713d4cSNicholas Fraser }
250d0713d4cSNicholas Fraser }
251d0713d4cSNicholas Fraser #endif
252d0713d4cSNicholas Fraser output_json_format(out, false, 2, "}");
253d0713d4cSNicholas Fraser addr_location__exit(&al);
254d0713d4cSNicholas Fraser return 0;
255d0713d4cSNicholas Fraser }
256d0713d4cSNicholas Fraser
output_headers(struct perf_session * session,struct convert_json * c)257d0713d4cSNicholas Fraser static void output_headers(struct perf_session *session, struct convert_json *c)
258d0713d4cSNicholas Fraser {
259d0713d4cSNicholas Fraser struct stat st;
260d0713d4cSNicholas Fraser struct perf_header *header = &session->header;
261d0713d4cSNicholas Fraser int ret;
262d0713d4cSNicholas Fraser int fd = perf_data__fd(session->data);
263d0713d4cSNicholas Fraser int i;
264d0713d4cSNicholas Fraser FILE *out = c->out;
265d0713d4cSNicholas Fraser
266d0713d4cSNicholas Fraser output_json_key_format(out, false, 2, "header-version", "%u", header->version);
267d0713d4cSNicholas Fraser
268d0713d4cSNicholas Fraser ret = fstat(fd, &st);
269d0713d4cSNicholas Fraser if (ret >= 0) {
270d0713d4cSNicholas Fraser time_t stctime = st.st_mtime;
271d0713d4cSNicholas Fraser char buf[256];
272d0713d4cSNicholas Fraser
273d0713d4cSNicholas Fraser strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&stctime));
274d0713d4cSNicholas Fraser output_json_key_string(out, true, 2, "captured-on", buf);
275d0713d4cSNicholas Fraser } else {
276d0713d4cSNicholas Fraser pr_debug("Failed to get mtime of source file, not writing captured-on");
277d0713d4cSNicholas Fraser }
278d0713d4cSNicholas Fraser
279d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "data-offset", "%" PRIu64, header->data_offset);
280d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "data-size", "%" PRIu64, header->data_size);
281d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "feat-offset", "%" PRIu64, header->feat_offset);
282d0713d4cSNicholas Fraser
283d0713d4cSNicholas Fraser output_json_key_string(out, true, 2, "hostname", header->env.hostname);
284d0713d4cSNicholas Fraser output_json_key_string(out, true, 2, "os-release", header->env.os_release);
285d0713d4cSNicholas Fraser output_json_key_string(out, true, 2, "arch", header->env.arch);
286d0713d4cSNicholas Fraser
287d0713d4cSNicholas Fraser output_json_key_string(out, true, 2, "cpu-desc", header->env.cpu_desc);
288d0713d4cSNicholas Fraser output_json_key_string(out, true, 2, "cpuid", header->env.cpuid);
289d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "nrcpus-online", "%u", header->env.nr_cpus_online);
290d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "nrcpus-avail", "%u", header->env.nr_cpus_avail);
291d0713d4cSNicholas Fraser
292d0713d4cSNicholas Fraser if (header->env.clock.enabled) {
293d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "clockid",
294d0713d4cSNicholas Fraser "%u", header->env.clock.clockid);
295d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "clock-time",
296d0713d4cSNicholas Fraser "%" PRIu64, header->env.clock.clockid_ns);
297d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "real-time",
298d0713d4cSNicholas Fraser "%" PRIu64, header->env.clock.tod_ns);
299d0713d4cSNicholas Fraser }
300d0713d4cSNicholas Fraser
301d0713d4cSNicholas Fraser output_json_key_string(out, true, 2, "perf-version", header->env.version);
302d0713d4cSNicholas Fraser
303d0713d4cSNicholas Fraser output_json_key_format(out, true, 2, "cmdline", "[");
304d0713d4cSNicholas Fraser for (i = 0; i < header->env.nr_cmdline; i++) {
305d0713d4cSNicholas Fraser output_json_delimiters(out, i != 0, 3);
306d0713d4cSNicholas Fraser output_json_string(c->out, header->env.cmdline_argv[i]);
307d0713d4cSNicholas Fraser }
308d0713d4cSNicholas Fraser output_json_format(out, false, 2, "]");
309d0713d4cSNicholas Fraser }
310d0713d4cSNicholas Fraser
bt_convert__perf2json(const char * input_name,const char * output_name,struct perf_data_convert_opts * opts __maybe_unused)311d0713d4cSNicholas Fraser int bt_convert__perf2json(const char *input_name, const char *output_name,
312d0713d4cSNicholas Fraser struct perf_data_convert_opts *opts __maybe_unused)
313d0713d4cSNicholas Fraser {
314d0713d4cSNicholas Fraser struct perf_session *session;
315d0713d4cSNicholas Fraser int fd;
316d0713d4cSNicholas Fraser int ret = -1;
317d0713d4cSNicholas Fraser
318d0713d4cSNicholas Fraser struct convert_json c = {
319d0713d4cSNicholas Fraser .tool = {
320d0713d4cSNicholas Fraser .sample = process_sample_event,
321d0713d4cSNicholas Fraser .mmap = perf_event__process_mmap,
322d0713d4cSNicholas Fraser .mmap2 = perf_event__process_mmap2,
323378ef0f5SIan Rogers .comm = perf_event__process_comm,
324d0713d4cSNicholas Fraser .namespaces = perf_event__process_namespaces,
325378ef0f5SIan Rogers .cgroup = perf_event__process_cgroup,
326d0713d4cSNicholas Fraser .exit = perf_event__process_exit,
327d0713d4cSNicholas Fraser .fork = perf_event__process_fork,
328d0713d4cSNicholas Fraser .lost = perf_event__process_lost,
329d0713d4cSNicholas Fraser #ifdef HAVE_LIBTRACEEVENT
330d0713d4cSNicholas Fraser .tracing_data = perf_event__process_tracing_data,
331d0713d4cSNicholas Fraser #endif
332d0713d4cSNicholas Fraser .build_id = perf_event__process_build_id,
333d0713d4cSNicholas Fraser .id_index = perf_event__process_id_index,
334d0713d4cSNicholas Fraser .auxtrace_info = perf_event__process_auxtrace_info,
335d0713d4cSNicholas Fraser .auxtrace = perf_event__process_auxtrace,
336d0713d4cSNicholas Fraser .event_update = perf_event__process_event_update,
337d0713d4cSNicholas Fraser .ordered_events = true,
338d0713d4cSNicholas Fraser .ordering_requires_timestamps = true,
339d0713d4cSNicholas Fraser },
340d0713d4cSNicholas Fraser .first = true,
341d0713d4cSNicholas Fraser .events_count = 0,
342d0713d4cSNicholas Fraser };
343d0713d4cSNicholas Fraser
344d0713d4cSNicholas Fraser struct perf_data data = {
345d0713d4cSNicholas Fraser .mode = PERF_DATA_MODE_READ,
346d0713d4cSNicholas Fraser .path = input_name,
347d0713d4cSNicholas Fraser .force = opts->force,
348d0713d4cSNicholas Fraser };
349d0713d4cSNicholas Fraser
350d0713d4cSNicholas Fraser if (opts->all) {
351d0713d4cSNicholas Fraser pr_err("--all is currently unsupported for JSON output.\n");
352d0713d4cSNicholas Fraser goto err;
353d0713d4cSNicholas Fraser }
354d0713d4cSNicholas Fraser if (opts->tod) {
355d0713d4cSNicholas Fraser pr_err("--tod is currently unsupported for JSON output.\n");
356d0713d4cSNicholas Fraser goto err;
357d0713d4cSNicholas Fraser }
358d0713d4cSNicholas Fraser
359d0713d4cSNicholas Fraser fd = open(output_name, O_CREAT | O_WRONLY | (opts->force ? O_TRUNC : O_EXCL), 0666);
360d0713d4cSNicholas Fraser if (fd == -1) {
361d0713d4cSNicholas Fraser if (errno == EEXIST)
362d0713d4cSNicholas Fraser pr_err("Output file exists. Use --force to overwrite it.\n");
363d0713d4cSNicholas Fraser else
364d0713d4cSNicholas Fraser pr_err("Error opening output file!\n");
365d0713d4cSNicholas Fraser goto err;
366d0713d4cSNicholas Fraser }
367d0713d4cSNicholas Fraser
368d0713d4cSNicholas Fraser c.out = fdopen(fd, "w");
3692681bd85SNamhyung Kim if (!c.out) {
370d0713d4cSNicholas Fraser fprintf(stderr, "Error opening output file!\n");
371d0713d4cSNicholas Fraser close(fd);
372d0713d4cSNicholas Fraser goto err;
373d0713d4cSNicholas Fraser }
374d0713d4cSNicholas Fraser
375d0713d4cSNicholas Fraser session = perf_session__new(&data, &c.tool);
376d0713d4cSNicholas Fraser if (IS_ERR(session)) {
377d0713d4cSNicholas Fraser fprintf(stderr, "Error creating perf session!\n");
378d0713d4cSNicholas Fraser goto err_fclose;
379d0713d4cSNicholas Fraser }
380d0713d4cSNicholas Fraser
381d0713d4cSNicholas Fraser if (symbol__init(&session->header.env) < 0) {
382d0713d4cSNicholas Fraser fprintf(stderr, "Symbol init error!\n");
383d0713d4cSNicholas Fraser goto err_session_delete;
384d0713d4cSNicholas Fraser }
385d0713d4cSNicholas Fraser
386d0713d4cSNicholas Fraser // The opening brace is printed manually because it isn't delimited from a
387d0713d4cSNicholas Fraser // previous value (i.e. we don't want a leading newline)
388d0713d4cSNicholas Fraser fputc('{', c.out);
389d0713d4cSNicholas Fraser
390d0713d4cSNicholas Fraser // Version number for future-proofing. Most additions should be able to be
391d0713d4cSNicholas Fraser // done in a backwards-compatible way so this should only need to be bumped
392d0713d4cSNicholas Fraser // if some major breaking change must be made.
393d0713d4cSNicholas Fraser output_json_format(c.out, false, 1, "\"linux-perf-json-version\": 1");
394d0713d4cSNicholas Fraser
395d0713d4cSNicholas Fraser // Output headers
396d0713d4cSNicholas Fraser output_json_format(c.out, true, 1, "\"headers\": {");
397d0713d4cSNicholas Fraser output_headers(session, &c);
398d0713d4cSNicholas Fraser output_json_format(c.out, false, 1, "}");
399d0713d4cSNicholas Fraser
400d0713d4cSNicholas Fraser // Output samples
401d0713d4cSNicholas Fraser output_json_format(c.out, true, 1, "\"samples\": [");
402d0713d4cSNicholas Fraser perf_session__process_events(session);
403d0713d4cSNicholas Fraser output_json_format(c.out, false, 1, "]");
404d0713d4cSNicholas Fraser output_json_format(c.out, false, 0, "}");
405d0713d4cSNicholas Fraser fputc('\n', c.out);
406d0713d4cSNicholas Fraser
407d0713d4cSNicholas Fraser fprintf(stderr,
408d0713d4cSNicholas Fraser "[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
409d0713d4cSNicholas Fraser data.path, output_name);
410d0713d4cSNicholas Fraser
411d0713d4cSNicholas Fraser fprintf(stderr,
412d0713d4cSNicholas Fraser "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
413d0713d4cSNicholas Fraser (ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
414d0713d4cSNicholas Fraser
415d0713d4cSNicholas Fraser ret = 0;
416d0713d4cSNicholas Fraser err_session_delete:
417 perf_session__delete(session);
418 err_fclose:
419 fclose(c.out);
420 err:
421 return ret;
422 }
423