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