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 	if (sample->raw_data) {
221 		int i;
222 		struct tep_format_field **fields;
223 
224 		fields = tep_event_fields(evsel->tp_format);
225 		if (fields) {
226 			i = 0;
227 			while (fields[i]) {
228 				struct trace_seq s;
229 
230 				trace_seq_init(&s);
231 				tep_print_field(&s, sample->raw_data, fields[i]);
232 				output_json_key_string(out, true, 3, fields[i]->name, s.buffer);
233 
234 				i++;
235 			}
236 			free(fields);
237 		}
238 	}
239 
240 	output_json_format(out, false, 2, "}");
241 	return 0;
242 }
243 
244 static void output_headers(struct perf_session *session, struct convert_json *c)
245 {
246 	struct stat st;
247 	struct perf_header *header = &session->header;
248 	int ret;
249 	int fd = perf_data__fd(session->data);
250 	int i;
251 	FILE *out = c->out;
252 
253 	output_json_key_format(out, false, 2, "header-version", "%u", header->version);
254 
255 	ret = fstat(fd, &st);
256 	if (ret >= 0) {
257 		time_t stctime = st.st_mtime;
258 		char buf[256];
259 
260 		strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&stctime));
261 		output_json_key_string(out, true, 2, "captured-on", buf);
262 	} else {
263 		pr_debug("Failed to get mtime of source file, not writing captured-on");
264 	}
265 
266 	output_json_key_format(out, true, 2, "data-offset", "%" PRIu64, header->data_offset);
267 	output_json_key_format(out, true, 2, "data-size", "%" PRIu64, header->data_size);
268 	output_json_key_format(out, true, 2, "feat-offset", "%" PRIu64, header->feat_offset);
269 
270 	output_json_key_string(out, true, 2, "hostname", header->env.hostname);
271 	output_json_key_string(out, true, 2, "os-release", header->env.os_release);
272 	output_json_key_string(out, true, 2, "arch", header->env.arch);
273 
274 	output_json_key_string(out, true, 2, "cpu-desc", header->env.cpu_desc);
275 	output_json_key_string(out, true, 2, "cpuid", header->env.cpuid);
276 	output_json_key_format(out, true, 2, "nrcpus-online", "%u", header->env.nr_cpus_online);
277 	output_json_key_format(out, true, 2, "nrcpus-avail", "%u", header->env.nr_cpus_avail);
278 
279 	if (header->env.clock.enabled) {
280 		output_json_key_format(out, true, 2, "clockid",
281 				"%u", header->env.clock.clockid);
282 		output_json_key_format(out, true, 2, "clock-time",
283 				"%" PRIu64, header->env.clock.clockid_ns);
284 		output_json_key_format(out, true, 2, "real-time",
285 				"%" PRIu64, header->env.clock.tod_ns);
286 	}
287 
288 	output_json_key_string(out, true, 2, "perf-version", header->env.version);
289 
290 	output_json_key_format(out, true, 2, "cmdline", "[");
291 	for (i = 0; i < header->env.nr_cmdline; i++) {
292 		output_json_delimiters(out, i != 0, 3);
293 		output_json_string(c->out, header->env.cmdline_argv[i]);
294 	}
295 	output_json_format(out, false, 2, "]");
296 }
297 
298 int bt_convert__perf2json(const char *input_name, const char *output_name,
299 		struct perf_data_convert_opts *opts __maybe_unused)
300 {
301 	struct perf_session *session;
302 	int fd;
303 	int ret = -1;
304 
305 	struct convert_json c = {
306 		.tool = {
307 			.sample         = process_sample_event,
308 			.mmap           = perf_event__process_mmap,
309 			.mmap2          = perf_event__process_mmap2,
310 			.comm           = perf_event__process_comm,
311 			.namespaces     = perf_event__process_namespaces,
312 			.cgroup         = perf_event__process_cgroup,
313 			.exit           = perf_event__process_exit,
314 			.fork           = perf_event__process_fork,
315 			.lost           = perf_event__process_lost,
316 			.tracing_data   = perf_event__process_tracing_data,
317 			.build_id       = perf_event__process_build_id,
318 			.id_index       = perf_event__process_id_index,
319 			.auxtrace_info  = perf_event__process_auxtrace_info,
320 			.auxtrace       = perf_event__process_auxtrace,
321 			.event_update   = perf_event__process_event_update,
322 			.ordered_events = true,
323 			.ordering_requires_timestamps = true,
324 		},
325 		.first = true,
326 		.events_count = 0,
327 	};
328 
329 	struct perf_data data = {
330 		.mode = PERF_DATA_MODE_READ,
331 		.path = input_name,
332 		.force = opts->force,
333 	};
334 
335 	if (opts->all) {
336 		pr_err("--all is currently unsupported for JSON output.\n");
337 		goto err;
338 	}
339 	if (opts->tod) {
340 		pr_err("--tod is currently unsupported for JSON output.\n");
341 		goto err;
342 	}
343 
344 	fd = open(output_name, O_CREAT | O_WRONLY | (opts->force ? O_TRUNC : O_EXCL), 0666);
345 	if (fd == -1) {
346 		if (errno == EEXIST)
347 			pr_err("Output file exists. Use --force to overwrite it.\n");
348 		else
349 			pr_err("Error opening output file!\n");
350 		goto err;
351 	}
352 
353 	c.out = fdopen(fd, "w");
354 	if (!c.out) {
355 		fprintf(stderr, "Error opening output file!\n");
356 		close(fd);
357 		goto err;
358 	}
359 
360 	session = perf_session__new(&data, &c.tool);
361 	if (IS_ERR(session)) {
362 		fprintf(stderr, "Error creating perf session!\n");
363 		goto err_fclose;
364 	}
365 
366 	if (symbol__init(&session->header.env) < 0) {
367 		fprintf(stderr, "Symbol init error!\n");
368 		goto err_session_delete;
369 	}
370 
371 	// The opening brace is printed manually because it isn't delimited from a
372 	// previous value (i.e. we don't want a leading newline)
373 	fputc('{', c.out);
374 
375 	// Version number for future-proofing. Most additions should be able to be
376 	// done in a backwards-compatible way so this should only need to be bumped
377 	// if some major breaking change must be made.
378 	output_json_format(c.out, false, 1, "\"linux-perf-json-version\": 1");
379 
380 	// Output headers
381 	output_json_format(c.out, true, 1, "\"headers\": {");
382 	output_headers(session, &c);
383 	output_json_format(c.out, false, 1, "}");
384 
385 	// Output samples
386 	output_json_format(c.out, true, 1, "\"samples\": [");
387 	perf_session__process_events(session);
388 	output_json_format(c.out, false, 1, "]");
389 	output_json_format(c.out, false, 0, "}");
390 	fputc('\n', c.out);
391 
392 	fprintf(stderr,
393 			"[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
394 			data.path, output_name);
395 
396 	fprintf(stderr,
397 			"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
398 			(ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
399 
400 	ret = 0;
401 err_session_delete:
402 	perf_session__delete(session);
403 err_fclose:
404 	fclose(c.out);
405 err:
406 	return ret;
407 }
408