xref: /freebsd/lib/libpmc/libpmc_json.cc (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1f992dd4bSMatt Macy /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3f992dd4bSMatt Macy  *
4f992dd4bSMatt Macy  * Copyright (c) 2018, Matthew Macy
5f992dd4bSMatt Macy  *
6f992dd4bSMatt Macy  * Redistribution and use in source and binary forms, with or without
7f992dd4bSMatt Macy  * modification, are permitted provided that the following conditions
8f992dd4bSMatt Macy  * are met:
9f992dd4bSMatt Macy  * 1. Redistributions of source code must retain the above copyright
10f992dd4bSMatt Macy  *    notice, this list of conditions and the following disclaimer.
11f992dd4bSMatt Macy  * 2. Redistributions in binary form must reproduce the above copyright
12f992dd4bSMatt Macy  *    notice, this list of conditions and the following disclaimer in the
13f992dd4bSMatt Macy  *    documentation and/or other materials provided with the distribution.
14f992dd4bSMatt Macy  *
15f992dd4bSMatt Macy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16f992dd4bSMatt Macy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f992dd4bSMatt Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f992dd4bSMatt Macy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f992dd4bSMatt Macy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f992dd4bSMatt Macy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f992dd4bSMatt Macy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f992dd4bSMatt Macy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f992dd4bSMatt Macy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f992dd4bSMatt Macy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f992dd4bSMatt Macy  * SUCH DAMAGE.
26f992dd4bSMatt Macy  *
27f992dd4bSMatt Macy  * $FreeBSD$
28f992dd4bSMatt Macy  *
29f992dd4bSMatt Macy  */
30f992dd4bSMatt Macy 
31f992dd4bSMatt Macy #include <sys/types.h>
32f992dd4bSMatt Macy #include <sys/sysctl.h>
33f992dd4bSMatt Macy #include <assert.h>
34b3d01a2aSEnji Cooper #include <err.h>
35b3d01a2aSEnji Cooper #include <errno.h>
36b3d01a2aSEnji Cooper #include <limits.h>
37b3d01a2aSEnji Cooper #include <stddef.h>
38b3d01a2aSEnji Cooper #include <stdio.h>
39b3d01a2aSEnji Cooper #include <stdlib.h>
40b3d01a2aSEnji Cooper #include <string.h>
41f992dd4bSMatt Macy #include <string>
42f992dd4bSMatt Macy #include <sysexits.h>
43b3d01a2aSEnji Cooper 
44b3d01a2aSEnji Cooper #include <pmc.h>
45f992dd4bSMatt Macy #include <pmcformat.h>
46b3d01a2aSEnji Cooper #include <pmclog.h>
47f992dd4bSMatt Macy 
48f992dd4bSMatt Macy using std::string;
49f992dd4bSMatt Macy 
50f992dd4bSMatt Macy static const char *typenames[] = {
51f992dd4bSMatt Macy 	"",
52f992dd4bSMatt Macy 	"{\"type\": \"closelog\"}\n",
53f992dd4bSMatt Macy 	"{\"type\": \"dropnotify\"}\n",
54f992dd4bSMatt Macy 	"{\"type\": \"initialize\"",
55f992dd4bSMatt Macy 	"",
56f992dd4bSMatt Macy 	"{\"type\": \"pmcallocate\"",
57f992dd4bSMatt Macy 	"{\"type\": \"pmcattach\"",
58f992dd4bSMatt Macy 	"{\"type\": \"pmcdetach\"",
59f992dd4bSMatt Macy 	"{\"type\": \"proccsw\"",
60f992dd4bSMatt Macy 	"{\"type\": \"procexec\"",
61f992dd4bSMatt Macy 	"{\"type\": \"procexit\"",
62f992dd4bSMatt Macy 	"{\"type\": \"procfork\"",
63f992dd4bSMatt Macy 	"{\"type\": \"sysexit\"",
64f992dd4bSMatt Macy 	"{\"type\": \"userdata\"",
65f992dd4bSMatt Macy 	"{\"type\": \"map_in\"",
66f992dd4bSMatt Macy 	"{\"type\": \"map_out\"",
67f992dd4bSMatt Macy 	"{\"type\": \"callchain\"",
68f992dd4bSMatt Macy 	"{\"type\": \"pmcallocatedyn\"",
69f992dd4bSMatt Macy 	"{\"type\": \"thr_create\"",
70f992dd4bSMatt Macy 	"{\"type\": \"thr_exit\"",
71f992dd4bSMatt Macy 	"{\"type\": \"proc_create\"",
72f992dd4bSMatt Macy };
73f992dd4bSMatt Macy 
74f992dd4bSMatt Macy static string
75f992dd4bSMatt Macy startentry(struct pmclog_ev *ev)
76f992dd4bSMatt Macy {
77f992dd4bSMatt Macy 	char eventbuf[128];
78f992dd4bSMatt Macy 
79f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf), "%s, \"tsc\": \"%jd\"",
80a72a9036SMatt Macy 	    typenames[ev->pl_type], (uintmax_t)ev->pl_ts.tv_sec);
81f992dd4bSMatt Macy 	return (string(eventbuf));
82f992dd4bSMatt Macy }
83f992dd4bSMatt Macy 
84f992dd4bSMatt Macy static string
85f992dd4bSMatt Macy initialize_to_json(struct pmclog_ev *ev)
86f992dd4bSMatt Macy {
87f992dd4bSMatt Macy 	char eventbuf[256];
88f992dd4bSMatt Macy 	string startent;
89f992dd4bSMatt Macy 
90f992dd4bSMatt Macy 	startent = startentry(ev);
91f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
92f992dd4bSMatt Macy 	    "%s, \"version\": \"0x%08x\", \"arch\": \"0x%08x\", \"cpuid\": \"%s\", "
93f992dd4bSMatt Macy 		"\"tsc_freq\": \"%jd\", \"sec\": \"%jd\", \"nsec\": \"%jd\"}\n",
94f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_i.pl_version, ev->pl_u.pl_i.pl_arch,
95f992dd4bSMatt Macy 		ev->pl_u.pl_i.pl_cpuid, (uintmax_t)ev->pl_u.pl_i.pl_tsc_freq,
96f992dd4bSMatt Macy 		(uintmax_t)ev->pl_u.pl_i.pl_ts.tv_sec, (uintmax_t)ev->pl_u.pl_i.pl_ts.tv_nsec);
97f992dd4bSMatt Macy 	return string(eventbuf);
98f992dd4bSMatt Macy }
99f992dd4bSMatt Macy 
100f992dd4bSMatt Macy static string
101f992dd4bSMatt Macy pmcallocate_to_json(struct pmclog_ev *ev)
102f992dd4bSMatt Macy {
103f992dd4bSMatt Macy 	char eventbuf[256];
104f992dd4bSMatt Macy 	string startent;
105f992dd4bSMatt Macy 
106f992dd4bSMatt Macy 	startent = startentry(ev);
107f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
108f992dd4bSMatt Macy 	    "%s, \"pmcid\": \"0x%08x\", \"event\": \"0x%08x\", \"flags\": \"0x%08x\", "
109f992dd4bSMatt Macy 	    "\"rate\": \"%jd\"}\n",
110f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_a.pl_pmcid, ev->pl_u.pl_a.pl_event,
111f992dd4bSMatt Macy 	    ev->pl_u.pl_a.pl_flags, (intmax_t)ev->pl_u.pl_a.pl_rate);
112f992dd4bSMatt Macy 	return string(eventbuf);
113f992dd4bSMatt Macy }
114f992dd4bSMatt Macy 
115f992dd4bSMatt Macy static string
116f992dd4bSMatt Macy pmcattach_to_json(struct pmclog_ev *ev)
117f992dd4bSMatt Macy {
118f992dd4bSMatt Macy 	char eventbuf[2048];
119f992dd4bSMatt Macy 	string startent;
120f992dd4bSMatt Macy 
121f992dd4bSMatt Macy 	startent = startentry(ev);
122f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
123f992dd4bSMatt Macy 	    "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", \"pathname\": \"%s\"}\n",
124f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_t.pl_pmcid, ev->pl_u.pl_t.pl_pid,
125f992dd4bSMatt Macy 	    ev->pl_u.pl_t.pl_pathname);
126f992dd4bSMatt Macy 	return string(eventbuf);
127f992dd4bSMatt Macy }
128f992dd4bSMatt Macy 
129f992dd4bSMatt Macy static string
130f992dd4bSMatt Macy pmcdetach_to_json(struct pmclog_ev *ev)
131f992dd4bSMatt Macy {
132f992dd4bSMatt Macy 	char eventbuf[128];
133f992dd4bSMatt Macy 	string startent;
134f992dd4bSMatt Macy 
135f992dd4bSMatt Macy 	startent = startentry(ev);
136f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
137f992dd4bSMatt Macy 		"%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\"}\n",
138f992dd4bSMatt Macy 			 startent.c_str(), ev->pl_u.pl_d.pl_pmcid, ev->pl_u.pl_d.pl_pid);
139f992dd4bSMatt Macy 	return string(eventbuf);
140f992dd4bSMatt Macy }
141f992dd4bSMatt Macy 
142f992dd4bSMatt Macy 
143f992dd4bSMatt Macy static string
144f992dd4bSMatt Macy proccsw_to_json(struct pmclog_ev *ev)
145f992dd4bSMatt Macy {
146f992dd4bSMatt Macy 	char eventbuf[128];
147f992dd4bSMatt Macy 	string startent;
148f992dd4bSMatt Macy 
149f992dd4bSMatt Macy 	startent = startentry(ev);
150f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf), "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\" "
151f992dd4bSMatt Macy 	    "\"tid\": \"%d\", \"value\": \"0x%016jx\"}\n",
152f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_c.pl_pmcid, ev->pl_u.pl_c.pl_pid,
153f992dd4bSMatt Macy 	    ev->pl_u.pl_c.pl_tid, (uintmax_t)ev->pl_u.pl_c.pl_value);
154f992dd4bSMatt Macy 	return string(eventbuf);
155f992dd4bSMatt Macy }
156f992dd4bSMatt Macy 
157f992dd4bSMatt Macy static string
158f992dd4bSMatt Macy procexec_to_json(struct pmclog_ev *ev)
159f992dd4bSMatt Macy {
160f992dd4bSMatt Macy 	char eventbuf[2048];
161f992dd4bSMatt Macy 	string startent;
162f992dd4bSMatt Macy 
163f992dd4bSMatt Macy 	startent = startentry(ev);
164f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
165f992dd4bSMatt Macy 		"%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", "
166f992dd4bSMatt Macy 	    "\"start\": \"0x%016jx\", \"pathname\": \"%s\"}\n",
167f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_x.pl_pmcid, ev->pl_u.pl_x.pl_pid,
168a72a9036SMatt Macy 		(uintmax_t)ev->pl_u.pl_x.pl_entryaddr, ev->pl_u.pl_x.pl_pathname);
169f992dd4bSMatt Macy 	return string(eventbuf);
170f992dd4bSMatt Macy }
171f992dd4bSMatt Macy 
172f992dd4bSMatt Macy static string
173f992dd4bSMatt Macy procexit_to_json(struct pmclog_ev *ev)
174f992dd4bSMatt Macy {
175f992dd4bSMatt Macy 	char eventbuf[128];
176f992dd4bSMatt Macy 	string startent;
177f992dd4bSMatt Macy 
178f992dd4bSMatt Macy 	startent = startentry(ev);
179f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
180f992dd4bSMatt Macy 		"%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", "
181f992dd4bSMatt Macy 	    "\"value\": \"0x%016jx\"}\n",
182f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_e.pl_pmcid, ev->pl_u.pl_e.pl_pid,
183f992dd4bSMatt Macy 	    (uintmax_t)ev->pl_u.pl_e.pl_value);
184f992dd4bSMatt Macy 	return string(eventbuf);
185f992dd4bSMatt Macy }
186f992dd4bSMatt Macy 
187f992dd4bSMatt Macy static string
188f992dd4bSMatt Macy procfork_to_json(struct pmclog_ev *ev)
189f992dd4bSMatt Macy {
190f992dd4bSMatt Macy 	char eventbuf[128];
191f992dd4bSMatt Macy 	string startent;
192f992dd4bSMatt Macy 
193f992dd4bSMatt Macy 	startent = startentry(ev);
194f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
195f992dd4bSMatt Macy 		"%s, \"oldpid\": \"%d\", \"newpid\": \"%d\"}\n",
196f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_f.pl_oldpid, ev->pl_u.pl_f.pl_newpid);
197f992dd4bSMatt Macy 	return string(eventbuf);
198f992dd4bSMatt Macy }
199f992dd4bSMatt Macy 
200f992dd4bSMatt Macy static string
201f992dd4bSMatt Macy sysexit_to_json(struct pmclog_ev *ev)
202f992dd4bSMatt Macy {
203f992dd4bSMatt Macy 	char eventbuf[128];
204f992dd4bSMatt Macy 	string startent;
205f992dd4bSMatt Macy 
206f992dd4bSMatt Macy 	startent = startentry(ev);
207f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\"}\n",
208f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_se.pl_pid);
209f992dd4bSMatt Macy 	return string(eventbuf);
210f992dd4bSMatt Macy }
211f992dd4bSMatt Macy 
212f992dd4bSMatt Macy static string
213f992dd4bSMatt Macy userdata_to_json(struct pmclog_ev *ev)
214f992dd4bSMatt Macy {
215f992dd4bSMatt Macy 	char eventbuf[128];
216f992dd4bSMatt Macy 	string startent;
217f992dd4bSMatt Macy 
218f992dd4bSMatt Macy 	startent = startentry(ev);
219f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf), "%s, \"userdata\": \"0x%08x\"}\n",
220f992dd4bSMatt Macy 	    startent.c_str(), ev->pl_u.pl_u.pl_userdata);
221f992dd4bSMatt Macy 	return string(eventbuf);
222f992dd4bSMatt Macy }
223f992dd4bSMatt Macy 
224f992dd4bSMatt Macy static string
225f992dd4bSMatt Macy map_in_to_json(struct pmclog_ev *ev)
226f992dd4bSMatt Macy {
227f992dd4bSMatt Macy 	char eventbuf[2048];
228f992dd4bSMatt Macy 	string startent;
229f992dd4bSMatt Macy 
230f992dd4bSMatt Macy 	startent = startentry(ev);
231f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\", "
232f992dd4bSMatt Macy 	    "\"start\": \"0x%016jx\", \"pathname\": \"%s\"}\n",
233f992dd4bSMatt Macy 	    startent.c_str(), ev->pl_u.pl_mi.pl_pid,
234f992dd4bSMatt Macy 	    (uintmax_t)ev->pl_u.pl_mi.pl_start, ev->pl_u.pl_mi.pl_pathname);
235f992dd4bSMatt Macy 	return string(eventbuf);
236f992dd4bSMatt Macy }
237f992dd4bSMatt Macy 
238f992dd4bSMatt Macy static string
239f992dd4bSMatt Macy map_out_to_json(struct pmclog_ev *ev)
240f992dd4bSMatt Macy {
241f992dd4bSMatt Macy 	char eventbuf[256];
242f992dd4bSMatt Macy 	string startent;
243f992dd4bSMatt Macy 
244f992dd4bSMatt Macy 	startent = startentry(ev);
245f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\", "
246f992dd4bSMatt Macy 	    "\"start\": \"0x%016jx\", \"end\": \"0x%016jx\"}\n",
247f992dd4bSMatt Macy 	    startent.c_str(), ev->pl_u.pl_mi.pl_pid,
248f992dd4bSMatt Macy 	    (uintmax_t)ev->pl_u.pl_mi.pl_start,
249f992dd4bSMatt Macy 	    (uintmax_t)ev->pl_u.pl_mo.pl_end);
250f992dd4bSMatt Macy 	return string(eventbuf);
251f992dd4bSMatt Macy }
252f992dd4bSMatt Macy 
253f992dd4bSMatt Macy static string
254f992dd4bSMatt Macy callchain_to_json(struct pmclog_ev *ev)
255f992dd4bSMatt Macy {
256f992dd4bSMatt Macy 	char eventbuf[1024];
257f992dd4bSMatt Macy 	string result;
258f992dd4bSMatt Macy 	uint32_t i;
259f992dd4bSMatt Macy 	string startent;
260f992dd4bSMatt Macy 
261f992dd4bSMatt Macy 	startent = startentry(ev);
262f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
263f992dd4bSMatt Macy 	    "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", \"tid\": \"%d\", "
264f992dd4bSMatt Macy 	    "\"cpuflags\": \"0x%08x\", \"cpuflags2\": \"0x%08x\", \"pc\": [ ",
265f992dd4bSMatt Macy 		startent.c_str(), ev->pl_u.pl_cc.pl_pmcid, ev->pl_u.pl_cc.pl_pid,
266f992dd4bSMatt Macy 	    ev->pl_u.pl_cc.pl_tid, ev->pl_u.pl_cc.pl_cpuflags, ev->pl_u.pl_cc.pl_cpuflags2);
267f992dd4bSMatt Macy 	result = string(eventbuf);
268f992dd4bSMatt Macy 	for (i = 0; i < ev->pl_u.pl_cc.pl_npc - 1; i++) {
269a72a9036SMatt Macy 		snprintf(eventbuf, sizeof(eventbuf), "\"0x%016jx\", ", (uintmax_t)ev->pl_u.pl_cc.pl_pc[i]);
270f992dd4bSMatt Macy 		result += string(eventbuf);
271f992dd4bSMatt Macy 	}
272a72a9036SMatt Macy 	snprintf(eventbuf, sizeof(eventbuf), "\"0x%016jx\"]}\n", (uintmax_t)ev->pl_u.pl_cc.pl_pc[i]);
273f992dd4bSMatt Macy 	result += string(eventbuf);
274f992dd4bSMatt Macy 	return (result);
275f992dd4bSMatt Macy }
276f992dd4bSMatt Macy 
277f992dd4bSMatt Macy static string
278f992dd4bSMatt Macy pmcallocatedyn_to_json(struct pmclog_ev *ev)
279f992dd4bSMatt Macy {
280f992dd4bSMatt Macy 	char eventbuf[2048];
281f992dd4bSMatt Macy 	string startent;
282f992dd4bSMatt Macy 
283f992dd4bSMatt Macy 	startent = startentry(ev);
284f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
285f992dd4bSMatt Macy 	    "%s, \"pmcid\": \"0x%08x\", \"event\": \"%d\", \"flags\": \"0x%08x\", \"evname\": \"%s\"}\n",
286f992dd4bSMatt Macy 	    startent.c_str(), ev->pl_u.pl_ad.pl_pmcid, ev->pl_u.pl_ad.pl_event,
287f992dd4bSMatt Macy 	    ev->pl_u.pl_ad.pl_flags, ev->pl_u.pl_ad.pl_evname);
288f992dd4bSMatt Macy 	return string(eventbuf);
289f992dd4bSMatt Macy }
290f992dd4bSMatt Macy 
291f992dd4bSMatt Macy static string
292f992dd4bSMatt Macy proccreate_to_json(struct pmclog_ev *ev)
293f992dd4bSMatt Macy {
294f992dd4bSMatt Macy 	char eventbuf[2048];
295f992dd4bSMatt Macy 	string startent;
296f992dd4bSMatt Macy 
297f992dd4bSMatt Macy 	startent = startentry(ev);
298f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
299f992dd4bSMatt Macy 	    "%s, \"pid\": \"%d\", \"flags\": \"0x%08x\", \"pcomm\": \"%s\"}\n",
300f992dd4bSMatt Macy 	    startent.c_str(), ev->pl_u.pl_pc.pl_pid,
301f992dd4bSMatt Macy 	    ev->pl_u.pl_pc.pl_flags, ev->pl_u.pl_pc.pl_pcomm);
302f992dd4bSMatt Macy 	return string(eventbuf);
303f992dd4bSMatt Macy }
304f992dd4bSMatt Macy 
305f992dd4bSMatt Macy static string
306f992dd4bSMatt Macy threadcreate_to_json(struct pmclog_ev *ev)
307f992dd4bSMatt Macy {
308f992dd4bSMatt Macy 	char eventbuf[2048];
309f992dd4bSMatt Macy 	string startent;
310f992dd4bSMatt Macy 
311f992dd4bSMatt Macy 	startent = startentry(ev);
312f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf),
313f992dd4bSMatt Macy 	    "%s, \"tid\": \"%d\", \"pid\": \"%d\", \"flags\": \"0x%08x\", \"tdname\": \"%s\"}\n",
314f992dd4bSMatt Macy 	    startent.c_str(), ev->pl_u.pl_tc.pl_tid, ev->pl_u.pl_tc.pl_pid,
315f992dd4bSMatt Macy 	    ev->pl_u.pl_tc.pl_flags, ev->pl_u.pl_tc.pl_tdname);
316f992dd4bSMatt Macy 	return string(eventbuf);
317f992dd4bSMatt Macy }
318f992dd4bSMatt Macy 
319f992dd4bSMatt Macy static string
320f992dd4bSMatt Macy threadexit_to_json(struct pmclog_ev *ev)
321f992dd4bSMatt Macy {
322f992dd4bSMatt Macy 	char eventbuf[256];
323f992dd4bSMatt Macy 	string startent;
324f992dd4bSMatt Macy 
325f992dd4bSMatt Macy 	startent = startentry(ev);
326f992dd4bSMatt Macy 	snprintf(eventbuf, sizeof(eventbuf), "%s, \"tid\": \"%d\"}\n",
327f992dd4bSMatt Macy 	    startent.c_str(), ev->pl_u.pl_te.pl_tid);
328f992dd4bSMatt Macy 	return string(eventbuf);
329f992dd4bSMatt Macy }
330f992dd4bSMatt Macy 
331f992dd4bSMatt Macy static string
332f992dd4bSMatt Macy stub_to_json(struct pmclog_ev *ev)
333f992dd4bSMatt Macy {
334f992dd4bSMatt Macy 	string startent;
335f992dd4bSMatt Macy 
336f992dd4bSMatt Macy 	startent = startentry(ev);
337f992dd4bSMatt Macy 	startent += string("}\n");
338f992dd4bSMatt Macy 	return startent;
339f992dd4bSMatt Macy }
340f992dd4bSMatt Macy 
341f992dd4bSMatt Macy typedef string (*jconv) (struct pmclog_ev*);
342f992dd4bSMatt Macy 
343f992dd4bSMatt Macy static jconv jsonconvert[] = {
344f992dd4bSMatt Macy 	NULL,
345f992dd4bSMatt Macy 	stub_to_json,
346f992dd4bSMatt Macy 	stub_to_json,
347f992dd4bSMatt Macy 	initialize_to_json,
348f992dd4bSMatt Macy 	NULL,
349f992dd4bSMatt Macy 	pmcallocate_to_json,
350f992dd4bSMatt Macy 	pmcattach_to_json,
351f992dd4bSMatt Macy 	pmcdetach_to_json,
352f992dd4bSMatt Macy 	proccsw_to_json,
353f992dd4bSMatt Macy 	procexec_to_json,
354f992dd4bSMatt Macy 	procexit_to_json,
355f992dd4bSMatt Macy 	procfork_to_json,
356f992dd4bSMatt Macy 	sysexit_to_json,
357f992dd4bSMatt Macy 	userdata_to_json,
358f992dd4bSMatt Macy 	map_in_to_json,
359f992dd4bSMatt Macy 	map_out_to_json,
360f992dd4bSMatt Macy 	callchain_to_json,
361f992dd4bSMatt Macy 	pmcallocatedyn_to_json,
362f992dd4bSMatt Macy 	threadcreate_to_json,
363f992dd4bSMatt Macy 	threadexit_to_json,
364f992dd4bSMatt Macy 	proccreate_to_json,
365f992dd4bSMatt Macy };
366f992dd4bSMatt Macy 
367f992dd4bSMatt Macy string
368f992dd4bSMatt Macy event_to_json(struct pmclog_ev *ev){
369f992dd4bSMatt Macy 
370f992dd4bSMatt Macy 	switch (ev->pl_type) {
371f992dd4bSMatt Macy 	case PMCLOG_TYPE_DROPNOTIFY:
372f992dd4bSMatt Macy 	case PMCLOG_TYPE_CLOSELOG:
373f992dd4bSMatt Macy 	case PMCLOG_TYPE_INITIALIZE:
374f992dd4bSMatt Macy 	case PMCLOG_TYPE_PMCALLOCATE:
375f992dd4bSMatt Macy 	case PMCLOG_TYPE_PMCATTACH:
376f992dd4bSMatt Macy 	case PMCLOG_TYPE_PMCDETACH:
377f992dd4bSMatt Macy 	case PMCLOG_TYPE_PROCCSW:
378f992dd4bSMatt Macy 	case PMCLOG_TYPE_PROCEXEC:
379f992dd4bSMatt Macy 	case PMCLOG_TYPE_PROCEXIT:
380f992dd4bSMatt Macy 	case PMCLOG_TYPE_PROCFORK:
381f992dd4bSMatt Macy 	case PMCLOG_TYPE_SYSEXIT:
382f992dd4bSMatt Macy 	case PMCLOG_TYPE_USERDATA:
383f992dd4bSMatt Macy 	case PMCLOG_TYPE_MAP_IN:
384f992dd4bSMatt Macy 	case PMCLOG_TYPE_MAP_OUT:
385f992dd4bSMatt Macy 	case PMCLOG_TYPE_CALLCHAIN:
386f992dd4bSMatt Macy 	case PMCLOG_TYPE_PMCALLOCATEDYN:
387f992dd4bSMatt Macy 	case PMCLOG_TYPE_THR_CREATE:
388f992dd4bSMatt Macy 	case PMCLOG_TYPE_THR_EXIT:
389f992dd4bSMatt Macy 	case PMCLOG_TYPE_PROC_CREATE:
390f992dd4bSMatt Macy 		return jsonconvert[ev->pl_type](ev);
391f992dd4bSMatt Macy 	default:
392f992dd4bSMatt Macy 		errx(EX_USAGE, "ERROR: unrecognized event type: %d\n", ev->pl_type);
393f992dd4bSMatt Macy 	}
394f992dd4bSMatt Macy }
395f992dd4bSMatt Macy 
396