2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018, Matthew Macy
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
32 #include <sys/sysctl.h>
45 #include <pmcformat.h>
50 static const char *typenames[] = {
52 "{\"type\": \"closelog\"}\n",
53 "{\"type\": \"dropnotify\"}\n",
54 "{\"type\": \"initialize\"",
56 "{\"type\": \"pmcallocate\"",
57 "{\"type\": \"pmcattach\"",
58 "{\"type\": \"pmcdetach\"",
59 "{\"type\": \"proccsw\"",
60 "{\"type\": \"procexec\"",
61 "{\"type\": \"procexit\"",
62 "{\"type\": \"procfork\"",
63 "{\"type\": \"sysexit\"",
64 "{\"type\": \"userdata\"",
65 "{\"type\": \"map_in\"",
66 "{\"type\": \"map_out\"",
67 "{\"type\": \"callchain\"",
68 "{\"type\": \"pmcallocatedyn\"",
69 "{\"type\": \"thr_create\"",
70 "{\"type\": \"thr_exit\"",
71 "{\"type\": \"proc_create\"",
75 startentry(struct pmclog_ev *ev)
79 snprintf(eventbuf, sizeof(eventbuf), "%s, \"tsc\": \"%jd\"",
80 typenames[ev->pl_type], (uintmax_t)ev->pl_ts.tv_sec);
81 return (string(eventbuf));
85 initialize_to_json(struct pmclog_ev *ev)
90 startent = startentry(ev);
91 snprintf(eventbuf, sizeof(eventbuf),
92 "%s, \"version\": \"0x%08x\", \"arch\": \"0x%08x\", \"cpuid\": \"%s\", "
93 "\"tsc_freq\": \"%jd\", \"sec\": \"%jd\", \"nsec\": \"%jd\"}\n",
94 startent.c_str(), ev->pl_u.pl_i.pl_version, ev->pl_u.pl_i.pl_arch,
95 ev->pl_u.pl_i.pl_cpuid, (uintmax_t)ev->pl_u.pl_i.pl_tsc_freq,
96 (uintmax_t)ev->pl_u.pl_i.pl_ts.tv_sec, (uintmax_t)ev->pl_u.pl_i.pl_ts.tv_nsec);
97 return string(eventbuf);
101 pmcallocate_to_json(struct pmclog_ev *ev)
106 startent = startentry(ev);
107 snprintf(eventbuf, sizeof(eventbuf),
108 "%s, \"pmcid\": \"0x%08x\", \"event\": \"0x%08x\", \"flags\": \"0x%08x\", "
109 "\"rate\": \"%jd\"}\n",
110 startent.c_str(), ev->pl_u.pl_a.pl_pmcid, ev->pl_u.pl_a.pl_event,
111 ev->pl_u.pl_a.pl_flags, (intmax_t)ev->pl_u.pl_a.pl_rate);
112 return string(eventbuf);
116 pmcattach_to_json(struct pmclog_ev *ev)
121 startent = startentry(ev);
122 snprintf(eventbuf, sizeof(eventbuf),
123 "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", \"pathname\": \"%s\"}\n",
124 startent.c_str(), ev->pl_u.pl_t.pl_pmcid, ev->pl_u.pl_t.pl_pid,
125 ev->pl_u.pl_t.pl_pathname);
126 return string(eventbuf);
130 pmcdetach_to_json(struct pmclog_ev *ev)
135 startent = startentry(ev);
136 snprintf(eventbuf, sizeof(eventbuf),
137 "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\"}\n",
138 startent.c_str(), ev->pl_u.pl_d.pl_pmcid, ev->pl_u.pl_d.pl_pid);
139 return string(eventbuf);
144 proccsw_to_json(struct pmclog_ev *ev)
149 startent = startentry(ev);
150 snprintf(eventbuf, sizeof(eventbuf), "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\" "
151 "\"tid\": \"%d\", \"value\": \"0x%016jx\"}\n",
152 startent.c_str(), ev->pl_u.pl_c.pl_pmcid, ev->pl_u.pl_c.pl_pid,
153 ev->pl_u.pl_c.pl_tid, (uintmax_t)ev->pl_u.pl_c.pl_value);
154 return string(eventbuf);
158 procexec_to_json(struct pmclog_ev *ev)
163 startent = startentry(ev);
164 snprintf(eventbuf, sizeof(eventbuf),
165 "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", "
166 "\"start\": \"0x%016jx\", \"pathname\": \"%s\"}\n",
167 startent.c_str(), ev->pl_u.pl_x.pl_pmcid, ev->pl_u.pl_x.pl_pid,
168 (uintmax_t)ev->pl_u.pl_x.pl_entryaddr, ev->pl_u.pl_x.pl_pathname);
169 return string(eventbuf);
173 procexit_to_json(struct pmclog_ev *ev)
178 startent = startentry(ev);
179 snprintf(eventbuf, sizeof(eventbuf),
180 "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", "
181 "\"value\": \"0x%016jx\"}\n",
182 startent.c_str(), ev->pl_u.pl_e.pl_pmcid, ev->pl_u.pl_e.pl_pid,
183 (uintmax_t)ev->pl_u.pl_e.pl_value);
184 return string(eventbuf);
188 procfork_to_json(struct pmclog_ev *ev)
193 startent = startentry(ev);
194 snprintf(eventbuf, sizeof(eventbuf),
195 "%s, \"oldpid\": \"%d\", \"newpid\": \"%d\"}\n",
196 startent.c_str(), ev->pl_u.pl_f.pl_oldpid, ev->pl_u.pl_f.pl_newpid);
197 return string(eventbuf);
201 sysexit_to_json(struct pmclog_ev *ev)
206 startent = startentry(ev);
207 snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\"}\n",
208 startent.c_str(), ev->pl_u.pl_se.pl_pid);
209 return string(eventbuf);
213 userdata_to_json(struct pmclog_ev *ev)
218 startent = startentry(ev);
219 snprintf(eventbuf, sizeof(eventbuf), "%s, \"userdata\": \"0x%08x\"}\n",
220 startent.c_str(), ev->pl_u.pl_u.pl_userdata);
221 return string(eventbuf);
225 map_in_to_json(struct pmclog_ev *ev)
230 startent = startentry(ev);
231 snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\", "
232 "\"start\": \"0x%016jx\", \"pathname\": \"%s\"}\n",
233 startent.c_str(), ev->pl_u.pl_mi.pl_pid,
234 (uintmax_t)ev->pl_u.pl_mi.pl_start, ev->pl_u.pl_mi.pl_pathname);
235 return string(eventbuf);
239 map_out_to_json(struct pmclog_ev *ev)
244 startent = startentry(ev);
245 snprintf(eventbuf, sizeof(eventbuf), "%s, \"pid\": \"%d\", "
246 "\"start\": \"0x%016jx\", \"end\": \"0x%016jx\"}\n",
247 startent.c_str(), ev->pl_u.pl_mi.pl_pid,
248 (uintmax_t)ev->pl_u.pl_mi.pl_start,
249 (uintmax_t)ev->pl_u.pl_mo.pl_end);
250 return string(eventbuf);
254 callchain_to_json(struct pmclog_ev *ev)
261 startent = startentry(ev);
262 snprintf(eventbuf, sizeof(eventbuf),
263 "%s, \"pmcid\": \"0x%08x\", \"pid\": \"%d\", \"tid\": \"%d\", "
264 "\"cpuflags\": \"0x%08x\", \"cpuflags2\": \"0x%08x\", \"pc\": [ ",
265 startent.c_str(), ev->pl_u.pl_cc.pl_pmcid, ev->pl_u.pl_cc.pl_pid,
266 ev->pl_u.pl_cc.pl_tid, ev->pl_u.pl_cc.pl_cpuflags, ev->pl_u.pl_cc.pl_cpuflags2);
267 result = string(eventbuf);
268 for (i = 0; i < ev->pl_u.pl_cc.pl_npc - 1; i++) {
269 snprintf(eventbuf, sizeof(eventbuf), "\"0x%016jx\", ", (uintmax_t)ev->pl_u.pl_cc.pl_pc[i]);
270 result += string(eventbuf);
272 snprintf(eventbuf, sizeof(eventbuf), "\"0x%016jx\"]}\n", (uintmax_t)ev->pl_u.pl_cc.pl_pc[i]);
273 result += string(eventbuf);
278 pmcallocatedyn_to_json(struct pmclog_ev *ev)
283 startent = startentry(ev);
284 snprintf(eventbuf, sizeof(eventbuf),
285 "%s, \"pmcid\": \"0x%08x\", \"event\": \"%d\", \"flags\": \"0x%08x\", \"evname\": \"%s\"}\n",
286 startent.c_str(), ev->pl_u.pl_ad.pl_pmcid, ev->pl_u.pl_ad.pl_event,
287 ev->pl_u.pl_ad.pl_flags, ev->pl_u.pl_ad.pl_evname);
288 return string(eventbuf);
292 proccreate_to_json(struct pmclog_ev *ev)
297 startent = startentry(ev);
298 snprintf(eventbuf, sizeof(eventbuf),
299 "%s, \"pid\": \"%d\", \"flags\": \"0x%08x\", \"pcomm\": \"%s\"}\n",
300 startent.c_str(), ev->pl_u.pl_pc.pl_pid,
301 ev->pl_u.pl_pc.pl_flags, ev->pl_u.pl_pc.pl_pcomm);
302 return string(eventbuf);
306 threadcreate_to_json(struct pmclog_ev *ev)
311 startent = startentry(ev);
312 snprintf(eventbuf, sizeof(eventbuf),
313 "%s, \"tid\": \"%d\", \"pid\": \"%d\", \"flags\": \"0x%08x\", \"tdname\": \"%s\"}\n",
314 startent.c_str(), ev->pl_u.pl_tc.pl_tid, ev->pl_u.pl_tc.pl_pid,
315 ev->pl_u.pl_tc.pl_flags, ev->pl_u.pl_tc.pl_tdname);
316 return string(eventbuf);
320 threadexit_to_json(struct pmclog_ev *ev)
325 startent = startentry(ev);
326 snprintf(eventbuf, sizeof(eventbuf), "%s, \"tid\": \"%d\"}\n",
327 startent.c_str(), ev->pl_u.pl_te.pl_tid);
328 return string(eventbuf);
332 stub_to_json(struct pmclog_ev *ev)
336 startent = startentry(ev);
337 startent += string("}\n");
341 typedef string (*jconv) (struct pmclog_ev*);
343 static jconv jsonconvert[] = {
361 pmcallocatedyn_to_json,
362 threadcreate_to_json,
368 event_to_json(struct pmclog_ev *ev){
370 switch (ev->pl_type) {
371 case PMCLOG_TYPE_DROPNOTIFY:
372 case PMCLOG_TYPE_CLOSELOG:
373 case PMCLOG_TYPE_INITIALIZE:
374 case PMCLOG_TYPE_PMCALLOCATE:
375 case PMCLOG_TYPE_PMCATTACH:
376 case PMCLOG_TYPE_PMCDETACH:
377 case PMCLOG_TYPE_PROCCSW:
378 case PMCLOG_TYPE_PROCEXEC:
379 case PMCLOG_TYPE_PROCEXIT:
380 case PMCLOG_TYPE_PROCFORK:
381 case PMCLOG_TYPE_SYSEXIT:
382 case PMCLOG_TYPE_USERDATA:
383 case PMCLOG_TYPE_MAP_IN:
384 case PMCLOG_TYPE_MAP_OUT:
385 case PMCLOG_TYPE_CALLCHAIN:
386 case PMCLOG_TYPE_PMCALLOCATEDYN:
387 case PMCLOG_TYPE_THR_CREATE:
388 case PMCLOG_TYPE_THR_EXIT:
389 case PMCLOG_TYPE_PROC_CREATE:
390 return jsonconvert[ev->pl_type](ev);
392 errx(EX_USAGE, "ERROR: unrecognized event type: %d\n", ev->pl_type);