1f992dd4bSMatt Macy /*-
24d846d26SWarner 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 */
28f992dd4bSMatt Macy
29f992dd4bSMatt Macy #include <sys/types.h>
30f992dd4bSMatt Macy #include <sys/sysctl.h>
31f992dd4bSMatt Macy #include <assert.h>
32b3d01a2aSEnji Cooper #include <err.h>
33b3d01a2aSEnji Cooper #include <errno.h>
34b3d01a2aSEnji Cooper #include <limits.h>
35b3d01a2aSEnji Cooper #include <stddef.h>
36b3d01a2aSEnji Cooper #include <stdio.h>
37b3d01a2aSEnji Cooper #include <stdlib.h>
38b3d01a2aSEnji Cooper #include <string.h>
39f992dd4bSMatt Macy #include <string>
40f992dd4bSMatt Macy #include <sysexits.h>
41b3d01a2aSEnji Cooper
42b3d01a2aSEnji Cooper #include <pmc.h>
43f992dd4bSMatt Macy #include <pmcformat.h>
44b3d01a2aSEnji Cooper #include <pmclog.h>
45f992dd4bSMatt Macy
46f992dd4bSMatt Macy using std::string;
47f992dd4bSMatt Macy
48f992dd4bSMatt Macy static const char *typenames[] = {
49f992dd4bSMatt Macy "",
50f992dd4bSMatt Macy "{\"type\": \"closelog\"}\n",
51f992dd4bSMatt Macy "{\"type\": \"dropnotify\"}\n",
52f992dd4bSMatt Macy "{\"type\": \"initialize\"",
53f992dd4bSMatt Macy "",
54f992dd4bSMatt Macy "{\"type\": \"pmcallocate\"",
55f992dd4bSMatt Macy "{\"type\": \"pmcattach\"",
56f992dd4bSMatt Macy "{\"type\": \"pmcdetach\"",
57f992dd4bSMatt Macy "{\"type\": \"proccsw\"",
58f992dd4bSMatt Macy "{\"type\": \"procexec\"",
59f992dd4bSMatt Macy "{\"type\": \"procexit\"",
60f992dd4bSMatt Macy "{\"type\": \"procfork\"",
61f992dd4bSMatt Macy "{\"type\": \"sysexit\"",
62f992dd4bSMatt Macy "{\"type\": \"userdata\"",
63f992dd4bSMatt Macy "{\"type\": \"map_in\"",
64f992dd4bSMatt Macy "{\"type\": \"map_out\"",
65f992dd4bSMatt Macy "{\"type\": \"callchain\"",
66f992dd4bSMatt Macy "{\"type\": \"pmcallocatedyn\"",
67f992dd4bSMatt Macy "{\"type\": \"thr_create\"",
68f992dd4bSMatt Macy "{\"type\": \"thr_exit\"",
69f992dd4bSMatt Macy "{\"type\": \"proc_create\"",
70f992dd4bSMatt Macy };
71f992dd4bSMatt Macy
72f992dd4bSMatt Macy static string
startentry(struct pmclog_ev * ev)73f992dd4bSMatt Macy startentry(struct pmclog_ev *ev)
74f992dd4bSMatt Macy {
75f992dd4bSMatt Macy char eventbuf[128];
76f992dd4bSMatt Macy
77f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf), "%s, \"tsc\": \"%jd\"",
78a72a9036SMatt Macy typenames[ev->pl_type], (uintmax_t)ev->pl_ts.tv_sec);
79f992dd4bSMatt Macy return (string(eventbuf));
80f992dd4bSMatt Macy }
81f992dd4bSMatt Macy
82f992dd4bSMatt Macy static string
initialize_to_json(struct pmclog_ev * ev)83f992dd4bSMatt Macy initialize_to_json(struct pmclog_ev *ev)
84f992dd4bSMatt Macy {
85f992dd4bSMatt Macy char eventbuf[256];
86f992dd4bSMatt Macy string startent;
87f992dd4bSMatt Macy
88f992dd4bSMatt Macy startent = startentry(ev);
89f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
90f992dd4bSMatt Macy "%s, \"version\": \"0x%08x\", \"arch\": \"0x%08x\", \"cpuid\": \"%s\", "
91f992dd4bSMatt Macy "\"tsc_freq\": \"%jd\", \"sec\": \"%jd\", \"nsec\": \"%jd\"}\n",
92f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_i.pl_version, ev->pl_u.pl_i.pl_arch,
93f992dd4bSMatt Macy ev->pl_u.pl_i.pl_cpuid, (uintmax_t)ev->pl_u.pl_i.pl_tsc_freq,
94f992dd4bSMatt Macy (uintmax_t)ev->pl_u.pl_i.pl_ts.tv_sec, (uintmax_t)ev->pl_u.pl_i.pl_ts.tv_nsec);
95f992dd4bSMatt Macy return string(eventbuf);
96f992dd4bSMatt Macy }
97f992dd4bSMatt Macy
98f992dd4bSMatt Macy static string
pmcallocate_to_json(struct pmclog_ev * ev)99f992dd4bSMatt Macy pmcallocate_to_json(struct pmclog_ev *ev)
100f992dd4bSMatt Macy {
101f992dd4bSMatt Macy char eventbuf[256];
102f992dd4bSMatt Macy string startent;
103f992dd4bSMatt Macy
104f992dd4bSMatt Macy startent = startentry(ev);
105f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
106f992dd4bSMatt Macy "%s, \"pmcid\": \"0x%08x\", \"event\": \"0x%08x\", \"flags\": \"0x%08x\", "
107f992dd4bSMatt Macy "\"rate\": \"%jd\"}\n",
108f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_a.pl_pmcid, ev->pl_u.pl_a.pl_event,
109f992dd4bSMatt Macy ev->pl_u.pl_a.pl_flags, (intmax_t)ev->pl_u.pl_a.pl_rate);
110f992dd4bSMatt Macy return string(eventbuf);
111f992dd4bSMatt Macy }
112f992dd4bSMatt Macy
113f992dd4bSMatt Macy static string
pmcattach_to_json(struct pmclog_ev * ev)114f992dd4bSMatt Macy pmcattach_to_json(struct pmclog_ev *ev)
115f992dd4bSMatt Macy {
116f992dd4bSMatt Macy char eventbuf[2048];
117f992dd4bSMatt Macy string startent;
118f992dd4bSMatt Macy
119f992dd4bSMatt Macy startent = startentry(ev);
120f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
121f992dd4bSMatt Macy "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", \"pathname\": \"%s\"}\n",
122f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_t.pl_pmcid, ev->pl_u.pl_t.pl_pid,
123f992dd4bSMatt Macy ev->pl_u.pl_t.pl_pathname);
124f992dd4bSMatt Macy return string(eventbuf);
125f992dd4bSMatt Macy }
126f992dd4bSMatt Macy
127f992dd4bSMatt Macy static string
pmcdetach_to_json(struct pmclog_ev * ev)128f992dd4bSMatt Macy pmcdetach_to_json(struct pmclog_ev *ev)
129f992dd4bSMatt Macy {
130f992dd4bSMatt Macy char eventbuf[128];
131f992dd4bSMatt Macy string startent;
132f992dd4bSMatt Macy
133f992dd4bSMatt Macy startent = startentry(ev);
134f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
135f992dd4bSMatt Macy "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\"}\n",
136f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_d.pl_pmcid, ev->pl_u.pl_d.pl_pid);
137f992dd4bSMatt Macy return string(eventbuf);
138f992dd4bSMatt Macy }
139f992dd4bSMatt Macy
140f992dd4bSMatt Macy
141f992dd4bSMatt Macy static string
proccsw_to_json(struct pmclog_ev * ev)142f992dd4bSMatt Macy proccsw_to_json(struct pmclog_ev *ev)
143f992dd4bSMatt Macy {
144f992dd4bSMatt Macy char eventbuf[128];
145f992dd4bSMatt Macy string startent;
146f992dd4bSMatt Macy
147f992dd4bSMatt Macy startent = startentry(ev);
148f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf), "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\" "
149f992dd4bSMatt Macy "\"tid\": \"%d\", \"value\": \"0x%016jx\"}\n",
150f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_c.pl_pmcid, ev->pl_u.pl_c.pl_pid,
151f992dd4bSMatt Macy ev->pl_u.pl_c.pl_tid, (uintmax_t)ev->pl_u.pl_c.pl_value);
152f992dd4bSMatt Macy return string(eventbuf);
153f992dd4bSMatt Macy }
154f992dd4bSMatt Macy
155f992dd4bSMatt Macy static string
procexec_to_json(struct pmclog_ev * ev)156f992dd4bSMatt Macy procexec_to_json(struct pmclog_ev *ev)
157f992dd4bSMatt Macy {
158f992dd4bSMatt Macy char eventbuf[2048];
159f992dd4bSMatt Macy string startent;
160f992dd4bSMatt Macy
161f992dd4bSMatt Macy startent = startentry(ev);
162f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
163f992dd4bSMatt Macy "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", "
164*94426d21SJessica Clarke "\"base\": \"0x%016jx\", \"dyn\": \"0x%016jx\", "
165*94426d21SJessica Clarke "\"pathname\": \"%s\"}\n",
166f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_x.pl_pmcid, ev->pl_u.pl_x.pl_pid,
167*94426d21SJessica Clarke (uintmax_t)ev->pl_u.pl_x.pl_baseaddr,
168*94426d21SJessica Clarke (uintmax_t)ev->pl_u.pl_x.pl_dynaddr,
169*94426d21SJessica Clarke ev->pl_u.pl_x.pl_pathname);
170f992dd4bSMatt Macy return string(eventbuf);
171f992dd4bSMatt Macy }
172f992dd4bSMatt Macy
173f992dd4bSMatt Macy static string
procexit_to_json(struct pmclog_ev * ev)174f992dd4bSMatt Macy procexit_to_json(struct pmclog_ev *ev)
175f992dd4bSMatt Macy {
176f992dd4bSMatt Macy char eventbuf[128];
177f992dd4bSMatt Macy string startent;
178f992dd4bSMatt Macy
179f992dd4bSMatt Macy startent = startentry(ev);
180f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
181f992dd4bSMatt Macy "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", "
182f992dd4bSMatt Macy "\"value\": \"0x%016jx\"}\n",
183f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_e.pl_pmcid, ev->pl_u.pl_e.pl_pid,
184f992dd4bSMatt Macy (uintmax_t)ev->pl_u.pl_e.pl_value);
185f992dd4bSMatt Macy return string(eventbuf);
186f992dd4bSMatt Macy }
187f992dd4bSMatt Macy
188f992dd4bSMatt Macy static string
procfork_to_json(struct pmclog_ev * ev)189f992dd4bSMatt Macy procfork_to_json(struct pmclog_ev *ev)
190f992dd4bSMatt Macy {
191f992dd4bSMatt Macy char eventbuf[128];
192f992dd4bSMatt Macy string startent;
193f992dd4bSMatt Macy
194f992dd4bSMatt Macy startent = startentry(ev);
195f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
196f992dd4bSMatt Macy "%s, \"oldpid\": \"%d\", \"newpid\": \"%d\"}\n",
197f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_f.pl_oldpid, ev->pl_u.pl_f.pl_newpid);
198f992dd4bSMatt Macy return string(eventbuf);
199f992dd4bSMatt Macy }
200f992dd4bSMatt Macy
201f992dd4bSMatt Macy static string
sysexit_to_json(struct pmclog_ev * ev)202f992dd4bSMatt Macy sysexit_to_json(struct pmclog_ev *ev)
203f992dd4bSMatt Macy {
204f992dd4bSMatt Macy char eventbuf[128];
205f992dd4bSMatt Macy string startent;
206f992dd4bSMatt Macy
207f992dd4bSMatt Macy startent = startentry(ev);
208f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\"}\n",
209f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_se.pl_pid);
210f992dd4bSMatt Macy return string(eventbuf);
211f992dd4bSMatt Macy }
212f992dd4bSMatt Macy
213f992dd4bSMatt Macy static string
userdata_to_json(struct pmclog_ev * ev)214f992dd4bSMatt Macy userdata_to_json(struct pmclog_ev *ev)
215f992dd4bSMatt Macy {
216f992dd4bSMatt Macy char eventbuf[128];
217f992dd4bSMatt Macy string startent;
218f992dd4bSMatt Macy
219f992dd4bSMatt Macy startent = startentry(ev);
220f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf), "%s, \"userdata\": \"0x%08x\"}\n",
221f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_u.pl_userdata);
222f992dd4bSMatt Macy return string(eventbuf);
223f992dd4bSMatt Macy }
224f992dd4bSMatt Macy
225f992dd4bSMatt Macy static string
map_in_to_json(struct pmclog_ev * ev)226f992dd4bSMatt Macy map_in_to_json(struct pmclog_ev *ev)
227f992dd4bSMatt Macy {
228f992dd4bSMatt Macy char eventbuf[2048];
229f992dd4bSMatt Macy string startent;
230f992dd4bSMatt Macy
231f992dd4bSMatt Macy startent = startentry(ev);
232f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\", "
233f992dd4bSMatt Macy "\"start\": \"0x%016jx\", \"pathname\": \"%s\"}\n",
234f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_mi.pl_pid,
235f992dd4bSMatt Macy (uintmax_t)ev->pl_u.pl_mi.pl_start, ev->pl_u.pl_mi.pl_pathname);
236f992dd4bSMatt Macy return string(eventbuf);
237f992dd4bSMatt Macy }
238f992dd4bSMatt Macy
239f992dd4bSMatt Macy static string
map_out_to_json(struct pmclog_ev * ev)240f992dd4bSMatt Macy map_out_to_json(struct pmclog_ev *ev)
241f992dd4bSMatt Macy {
242f992dd4bSMatt Macy char eventbuf[256];
243f992dd4bSMatt Macy string startent;
244f992dd4bSMatt Macy
245f992dd4bSMatt Macy startent = startentry(ev);
246f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\", "
247f992dd4bSMatt Macy "\"start\": \"0x%016jx\", \"end\": \"0x%016jx\"}\n",
248f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_mi.pl_pid,
249f992dd4bSMatt Macy (uintmax_t)ev->pl_u.pl_mi.pl_start,
250f992dd4bSMatt Macy (uintmax_t)ev->pl_u.pl_mo.pl_end);
251f992dd4bSMatt Macy return string(eventbuf);
252f992dd4bSMatt Macy }
253f992dd4bSMatt Macy
254f992dd4bSMatt Macy static string
callchain_to_json(struct pmclog_ev * ev)255f992dd4bSMatt Macy callchain_to_json(struct pmclog_ev *ev)
256f992dd4bSMatt Macy {
257f992dd4bSMatt Macy char eventbuf[1024];
258f992dd4bSMatt Macy string result;
259f992dd4bSMatt Macy uint32_t i;
260f992dd4bSMatt Macy string startent;
261f992dd4bSMatt Macy
262f992dd4bSMatt Macy startent = startentry(ev);
263f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
264f992dd4bSMatt Macy "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", \"tid\": \"%d\", "
265f992dd4bSMatt Macy "\"cpuflags\": \"0x%08x\", \"cpuflags2\": \"0x%08x\", \"pc\": [ ",
266f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_cc.pl_pmcid, ev->pl_u.pl_cc.pl_pid,
267f992dd4bSMatt Macy ev->pl_u.pl_cc.pl_tid, ev->pl_u.pl_cc.pl_cpuflags, ev->pl_u.pl_cc.pl_cpuflags2);
268f992dd4bSMatt Macy result = string(eventbuf);
269f992dd4bSMatt Macy for (i = 0; i < ev->pl_u.pl_cc.pl_npc - 1; i++) {
270a72a9036SMatt Macy snprintf(eventbuf, sizeof(eventbuf), "\"0x%016jx\", ", (uintmax_t)ev->pl_u.pl_cc.pl_pc[i]);
271f992dd4bSMatt Macy result += string(eventbuf);
272f992dd4bSMatt Macy }
273a72a9036SMatt Macy snprintf(eventbuf, sizeof(eventbuf), "\"0x%016jx\"]}\n", (uintmax_t)ev->pl_u.pl_cc.pl_pc[i]);
274f992dd4bSMatt Macy result += string(eventbuf);
275f992dd4bSMatt Macy return (result);
276f992dd4bSMatt Macy }
277f992dd4bSMatt Macy
278f992dd4bSMatt Macy static string
pmcallocatedyn_to_json(struct pmclog_ev * ev)279f992dd4bSMatt Macy pmcallocatedyn_to_json(struct pmclog_ev *ev)
280f992dd4bSMatt Macy {
281f992dd4bSMatt Macy char eventbuf[2048];
282f992dd4bSMatt Macy string startent;
283f992dd4bSMatt Macy
284f992dd4bSMatt Macy startent = startentry(ev);
285f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
286f992dd4bSMatt Macy "%s, \"pmcid\": \"0x%08x\", \"event\": \"%d\", \"flags\": \"0x%08x\", \"evname\": \"%s\"}\n",
287f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_ad.pl_pmcid, ev->pl_u.pl_ad.pl_event,
288f992dd4bSMatt Macy ev->pl_u.pl_ad.pl_flags, ev->pl_u.pl_ad.pl_evname);
289f992dd4bSMatt Macy return string(eventbuf);
290f992dd4bSMatt Macy }
291f992dd4bSMatt Macy
292f992dd4bSMatt Macy static string
proccreate_to_json(struct pmclog_ev * ev)293f992dd4bSMatt Macy proccreate_to_json(struct pmclog_ev *ev)
294f992dd4bSMatt Macy {
295f992dd4bSMatt Macy char eventbuf[2048];
296f992dd4bSMatt Macy string startent;
297f992dd4bSMatt Macy
298f992dd4bSMatt Macy startent = startentry(ev);
299f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
300f992dd4bSMatt Macy "%s, \"pid\": \"%d\", \"flags\": \"0x%08x\", \"pcomm\": \"%s\"}\n",
301f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_pc.pl_pid,
302f992dd4bSMatt Macy ev->pl_u.pl_pc.pl_flags, ev->pl_u.pl_pc.pl_pcomm);
303f992dd4bSMatt Macy return string(eventbuf);
304f992dd4bSMatt Macy }
305f992dd4bSMatt Macy
306f992dd4bSMatt Macy static string
threadcreate_to_json(struct pmclog_ev * ev)307f992dd4bSMatt Macy threadcreate_to_json(struct pmclog_ev *ev)
308f992dd4bSMatt Macy {
309f992dd4bSMatt Macy char eventbuf[2048];
310f992dd4bSMatt Macy string startent;
311f992dd4bSMatt Macy
312f992dd4bSMatt Macy startent = startentry(ev);
313f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf),
314f992dd4bSMatt Macy "%s, \"tid\": \"%d\", \"pid\": \"%d\", \"flags\": \"0x%08x\", \"tdname\": \"%s\"}\n",
315f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_tc.pl_tid, ev->pl_u.pl_tc.pl_pid,
316f992dd4bSMatt Macy ev->pl_u.pl_tc.pl_flags, ev->pl_u.pl_tc.pl_tdname);
317f992dd4bSMatt Macy return string(eventbuf);
318f992dd4bSMatt Macy }
319f992dd4bSMatt Macy
320f992dd4bSMatt Macy static string
threadexit_to_json(struct pmclog_ev * ev)321f992dd4bSMatt Macy threadexit_to_json(struct pmclog_ev *ev)
322f992dd4bSMatt Macy {
323f992dd4bSMatt Macy char eventbuf[256];
324f992dd4bSMatt Macy string startent;
325f992dd4bSMatt Macy
326f992dd4bSMatt Macy startent = startentry(ev);
327f992dd4bSMatt Macy snprintf(eventbuf, sizeof(eventbuf), "%s, \"tid\": \"%d\"}\n",
328f992dd4bSMatt Macy startent.c_str(), ev->pl_u.pl_te.pl_tid);
329f992dd4bSMatt Macy return string(eventbuf);
330f992dd4bSMatt Macy }
331f992dd4bSMatt Macy
332f992dd4bSMatt Macy static string
stub_to_json(struct pmclog_ev * ev)333f992dd4bSMatt Macy stub_to_json(struct pmclog_ev *ev)
334f992dd4bSMatt Macy {
335f992dd4bSMatt Macy string startent;
336f992dd4bSMatt Macy
337f992dd4bSMatt Macy startent = startentry(ev);
338f992dd4bSMatt Macy startent += string("}\n");
339f992dd4bSMatt Macy return startent;
340f992dd4bSMatt Macy }
341f992dd4bSMatt Macy
342f992dd4bSMatt Macy typedef string (*jconv) (struct pmclog_ev*);
343f992dd4bSMatt Macy
344f992dd4bSMatt Macy static jconv jsonconvert[] = {
345f992dd4bSMatt Macy NULL,
346f992dd4bSMatt Macy stub_to_json,
347f992dd4bSMatt Macy stub_to_json,
348f992dd4bSMatt Macy initialize_to_json,
349f992dd4bSMatt Macy NULL,
350f992dd4bSMatt Macy pmcallocate_to_json,
351f992dd4bSMatt Macy pmcattach_to_json,
352f992dd4bSMatt Macy pmcdetach_to_json,
353f992dd4bSMatt Macy proccsw_to_json,
354f992dd4bSMatt Macy procexec_to_json,
355f992dd4bSMatt Macy procexit_to_json,
356f992dd4bSMatt Macy procfork_to_json,
357f992dd4bSMatt Macy sysexit_to_json,
358f992dd4bSMatt Macy userdata_to_json,
359f992dd4bSMatt Macy map_in_to_json,
360f992dd4bSMatt Macy map_out_to_json,
361f992dd4bSMatt Macy callchain_to_json,
362f992dd4bSMatt Macy pmcallocatedyn_to_json,
363f992dd4bSMatt Macy threadcreate_to_json,
364f992dd4bSMatt Macy threadexit_to_json,
365f992dd4bSMatt Macy proccreate_to_json,
366f992dd4bSMatt Macy };
367f992dd4bSMatt Macy
368f992dd4bSMatt Macy string
event_to_json(struct pmclog_ev * ev)369f992dd4bSMatt Macy event_to_json(struct pmclog_ev *ev){
370f992dd4bSMatt Macy
371f992dd4bSMatt Macy switch (ev->pl_type) {
372f992dd4bSMatt Macy case PMCLOG_TYPE_DROPNOTIFY:
373f992dd4bSMatt Macy case PMCLOG_TYPE_CLOSELOG:
374f992dd4bSMatt Macy case PMCLOG_TYPE_INITIALIZE:
375f992dd4bSMatt Macy case PMCLOG_TYPE_PMCALLOCATE:
376f992dd4bSMatt Macy case PMCLOG_TYPE_PMCATTACH:
377f992dd4bSMatt Macy case PMCLOG_TYPE_PMCDETACH:
378f992dd4bSMatt Macy case PMCLOG_TYPE_PROCCSW:
379f992dd4bSMatt Macy case PMCLOG_TYPE_PROCEXEC:
380f992dd4bSMatt Macy case PMCLOG_TYPE_PROCEXIT:
381f992dd4bSMatt Macy case PMCLOG_TYPE_PROCFORK:
382f992dd4bSMatt Macy case PMCLOG_TYPE_SYSEXIT:
383f992dd4bSMatt Macy case PMCLOG_TYPE_USERDATA:
384f992dd4bSMatt Macy case PMCLOG_TYPE_MAP_IN:
385f992dd4bSMatt Macy case PMCLOG_TYPE_MAP_OUT:
386f992dd4bSMatt Macy case PMCLOG_TYPE_CALLCHAIN:
387f992dd4bSMatt Macy case PMCLOG_TYPE_PMCALLOCATEDYN:
388f992dd4bSMatt Macy case PMCLOG_TYPE_THR_CREATE:
389f992dd4bSMatt Macy case PMCLOG_TYPE_THR_EXIT:
390f992dd4bSMatt Macy case PMCLOG_TYPE_PROC_CREATE:
391f992dd4bSMatt Macy return jsonconvert[ev->pl_type](ev);
392f992dd4bSMatt Macy default:
393f992dd4bSMatt Macy errx(EX_USAGE, "ERROR: unrecognized event type: %d\n", ev->pl_type);
394f992dd4bSMatt Macy }
395f992dd4bSMatt Macy }
396f992dd4bSMatt Macy
397