3 #include "json-writer.h"
4 #include "run-command.h"
6 #include "trace2/tr2_dst.h"
7 #include "trace2/tr2_tbuf.h"
8 #include "trace2/tr2_sid.h"
9 #include "trace2/tr2_sysenv.h"
10 #include "trace2/tr2_tgt.h"
11 #include "trace2/tr2_tls.h"
13 static struct tr2_dst tr2dst_event = { TR2_SYSENV_EVENT, 0, 0, 0 };
16 * The version number of the JSON data generated by the EVENT target
17 * in this source file. Update this if you make a significant change
18 * to the JSON fields or message structure. You probably do not need
19 * to update this if you just add another call to one of the existing
22 #define TR2_EVENT_VERSION "1"
25 * Region nesting limit for messages written to the event target.
27 * The "region_enter" and "region_leave" messages (especially recursive
28 * messages such as those produced while diving the worktree or index)
29 * are primarily intended for the performance target during debugging.
31 * Some of the outer-most messages, however, may be of interest to the
32 * event target. Use the TR2_SYSENV_EVENT_NESTING setting to increase
33 * region details in the event target.
35 static int tr2env_event_max_nesting_levels = 2;
38 * Use the TR2_SYSENV_EVENT_BRIEF to omit the <time>, <file>, and
39 * <line> fields from most events.
41 static int tr2env_event_be_brief;
43 static int fn_init(void)
45 int want = tr2_dst_trace_want(&tr2dst_event);
54 nesting = tr2_sysenv_get(TR2_SYSENV_EVENT_NESTING);
55 if (nesting && *nesting && ((max_nesting = atoi(nesting)) > 0))
56 tr2env_event_max_nesting_levels = max_nesting;
58 brief = tr2_sysenv_get(TR2_SYSENV_EVENT_BRIEF);
59 if (brief && *brief &&
60 ((want_brief = git_parse_maybe_bool(brief)) != -1))
61 tr2env_event_be_brief = want_brief;
66 static void fn_term(void)
68 tr2_dst_trace_disable(&tr2dst_event);
72 * Append common key-value pairs to the currently open JSON object.
73 * "event:"<event_name>"
75 * "thread":"<thread_name>"
78 * "line":<line_number>
81 static void event_fmt_prepare(const char *event_name, const char *file,
82 int line, const struct repository *repo,
83 struct json_writer *jw)
85 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
86 struct tr2_tbuf tb_now;
88 jw_object_string(jw, "event", event_name);
89 jw_object_string(jw, "sid", tr2_sid_get());
90 jw_object_string(jw, "thread", ctx->thread_name.buf);
93 * In brief mode, only emit <time> on these 2 event types.
95 if (!tr2env_event_be_brief || !strcmp(event_name, "version") ||
96 !strcmp(event_name, "atexit")) {
97 tr2_tbuf_utc_datetime_extended(&tb_now);
98 jw_object_string(jw, "time", tb_now.buf);
101 if (!tr2env_event_be_brief && file && *file) {
102 jw_object_string(jw, "file", file);
103 jw_object_intmax(jw, "line", line);
107 jw_object_intmax(jw, "repo", repo->trace2_repo_id);
110 static void fn_version_fl(const char *file, int line)
112 const char *event_name = "version";
113 struct json_writer jw = JSON_WRITER_INIT;
115 jw_object_begin(&jw, 0);
116 event_fmt_prepare(event_name, file, line, NULL, &jw);
117 jw_object_string(&jw, "evt", TR2_EVENT_VERSION);
118 jw_object_string(&jw, "exe", git_version_string);
121 tr2_dst_write_line(&tr2dst_event, &jw.json);
125 static void fn_start_fl(const char *file, int line,
126 uint64_t us_elapsed_absolute, const char **argv)
128 const char *event_name = "start";
129 struct json_writer jw = JSON_WRITER_INIT;
130 double t_abs = (double)us_elapsed_absolute / 1000000.0;
132 jw_object_begin(&jw, 0);
133 event_fmt_prepare(event_name, file, line, NULL, &jw);
134 jw_object_double(&jw, "t_abs", 6, t_abs);
135 jw_object_inline_begin_array(&jw, "argv");
136 jw_array_argv(&jw, argv);
140 tr2_dst_write_line(&tr2dst_event, &jw.json);
144 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
147 const char *event_name = "exit";
148 struct json_writer jw = JSON_WRITER_INIT;
149 double t_abs = (double)us_elapsed_absolute / 1000000.0;
151 jw_object_begin(&jw, 0);
152 event_fmt_prepare(event_name, file, line, NULL, &jw);
153 jw_object_double(&jw, "t_abs", 6, t_abs);
154 jw_object_intmax(&jw, "code", code);
157 tr2_dst_write_line(&tr2dst_event, &jw.json);
161 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
163 const char *event_name = "signal";
164 struct json_writer jw = JSON_WRITER_INIT;
165 double t_abs = (double)us_elapsed_absolute / 1000000.0;
167 jw_object_begin(&jw, 0);
168 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
169 jw_object_double(&jw, "t_abs", 6, t_abs);
170 jw_object_intmax(&jw, "signo", signo);
173 tr2_dst_write_line(&tr2dst_event, &jw.json);
177 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
179 const char *event_name = "atexit";
180 struct json_writer jw = JSON_WRITER_INIT;
181 double t_abs = (double)us_elapsed_absolute / 1000000.0;
183 jw_object_begin(&jw, 0);
184 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
185 jw_object_double(&jw, "t_abs", 6, t_abs);
186 jw_object_intmax(&jw, "code", code);
189 tr2_dst_write_line(&tr2dst_event, &jw.json);
193 static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
194 const char *fmt, va_list ap)
198 struct strbuf buf = STRBUF_INIT;
200 va_copy(copy_ap, ap);
201 strbuf_vaddf(&buf, fmt, copy_ap);
204 jw_object_string(jw, field_name, buf.buf);
205 strbuf_release(&buf);
210 static void fn_error_va_fl(const char *file, int line, const char *fmt,
213 const char *event_name = "error";
214 struct json_writer jw = JSON_WRITER_INIT;
216 jw_object_begin(&jw, 0);
217 event_fmt_prepare(event_name, file, line, NULL, &jw);
218 maybe_add_string_va(&jw, "msg", fmt, ap);
220 * Also emit the format string as a field in case
221 * post-processors want to aggregate common error
222 * messages by type without argument fields (such
223 * as pathnames or branch names) cluttering it up.
226 jw_object_string(&jw, "fmt", fmt);
229 tr2_dst_write_line(&tr2dst_event, &jw.json);
233 static void fn_command_path_fl(const char *file, int line, const char *pathname)
235 const char *event_name = "cmd_path";
236 struct json_writer jw = JSON_WRITER_INIT;
238 jw_object_begin(&jw, 0);
239 event_fmt_prepare(event_name, file, line, NULL, &jw);
240 jw_object_string(&jw, "path", pathname);
243 tr2_dst_write_line(&tr2dst_event, &jw.json);
247 static void fn_command_name_fl(const char *file, int line, const char *name,
248 const char *hierarchy)
250 const char *event_name = "cmd_name";
251 struct json_writer jw = JSON_WRITER_INIT;
253 jw_object_begin(&jw, 0);
254 event_fmt_prepare(event_name, file, line, NULL, &jw);
255 jw_object_string(&jw, "name", name);
256 if (hierarchy && *hierarchy)
257 jw_object_string(&jw, "hierarchy", hierarchy);
260 tr2_dst_write_line(&tr2dst_event, &jw.json);
264 static void fn_command_mode_fl(const char *file, int line, const char *mode)
266 const char *event_name = "cmd_mode";
267 struct json_writer jw = JSON_WRITER_INIT;
269 jw_object_begin(&jw, 0);
270 event_fmt_prepare(event_name, file, line, NULL, &jw);
271 jw_object_string(&jw, "name", mode);
274 tr2_dst_write_line(&tr2dst_event, &jw.json);
278 static void fn_alias_fl(const char *file, int line, const char *alias,
281 const char *event_name = "alias";
282 struct json_writer jw = JSON_WRITER_INIT;
284 jw_object_begin(&jw, 0);
285 event_fmt_prepare(event_name, file, line, NULL, &jw);
286 jw_object_string(&jw, "alias", alias);
287 jw_object_inline_begin_array(&jw, "argv");
288 jw_array_argv(&jw, argv);
292 tr2_dst_write_line(&tr2dst_event, &jw.json);
296 static void fn_child_start_fl(const char *file, int line,
297 uint64_t us_elapsed_absolute,
298 const struct child_process *cmd)
300 const char *event_name = "child_start";
301 struct json_writer jw = JSON_WRITER_INIT;
303 jw_object_begin(&jw, 0);
304 event_fmt_prepare(event_name, file, line, NULL, &jw);
305 jw_object_intmax(&jw, "child_id", cmd->trace2_child_id);
306 if (cmd->trace2_hook_name) {
307 jw_object_string(&jw, "child_class", "hook");
308 jw_object_string(&jw, "hook_name", cmd->trace2_hook_name);
310 const char *child_class =
311 cmd->trace2_child_class ? cmd->trace2_child_class : "?";
312 jw_object_string(&jw, "child_class", child_class);
315 jw_object_string(&jw, "cd", cmd->dir);
316 jw_object_bool(&jw, "use_shell", cmd->use_shell);
317 jw_object_inline_begin_array(&jw, "argv");
319 jw_array_string(&jw, "git");
320 jw_array_argv(&jw, cmd->argv);
324 tr2_dst_write_line(&tr2dst_event, &jw.json);
328 static void fn_child_exit_fl(const char *file, int line,
329 uint64_t us_elapsed_absolute, int cid, int pid,
330 int code, uint64_t us_elapsed_child)
332 const char *event_name = "child_exit";
333 struct json_writer jw = JSON_WRITER_INIT;
334 double t_rel = (double)us_elapsed_child / 1000000.0;
336 jw_object_begin(&jw, 0);
337 event_fmt_prepare(event_name, file, line, NULL, &jw);
338 jw_object_intmax(&jw, "child_id", cid);
339 jw_object_intmax(&jw, "pid", pid);
340 jw_object_intmax(&jw, "code", code);
341 jw_object_double(&jw, "t_rel", 6, t_rel);
344 tr2_dst_write_line(&tr2dst_event, &jw.json);
349 static void fn_thread_start_fl(const char *file, int line,
350 uint64_t us_elapsed_absolute)
352 const char *event_name = "thread_start";
353 struct json_writer jw = JSON_WRITER_INIT;
355 jw_object_begin(&jw, 0);
356 event_fmt_prepare(event_name, file, line, NULL, &jw);
359 tr2_dst_write_line(&tr2dst_event, &jw.json);
363 static void fn_thread_exit_fl(const char *file, int line,
364 uint64_t us_elapsed_absolute,
365 uint64_t us_elapsed_thread)
367 const char *event_name = "thread_exit";
368 struct json_writer jw = JSON_WRITER_INIT;
369 double t_rel = (double)us_elapsed_thread / 1000000.0;
371 jw_object_begin(&jw, 0);
372 event_fmt_prepare(event_name, file, line, NULL, &jw);
373 jw_object_double(&jw, "t_rel", 6, t_rel);
376 tr2_dst_write_line(&tr2dst_event, &jw.json);
380 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
381 int exec_id, const char *exe, const char **argv)
383 const char *event_name = "exec";
384 struct json_writer jw = JSON_WRITER_INIT;
386 jw_object_begin(&jw, 0);
387 event_fmt_prepare(event_name, file, line, NULL, &jw);
388 jw_object_intmax(&jw, "exec_id", exec_id);
390 jw_object_string(&jw, "exe", exe);
391 jw_object_inline_begin_array(&jw, "argv");
392 jw_array_argv(&jw, argv);
396 tr2_dst_write_line(&tr2dst_event, &jw.json);
400 static void fn_exec_result_fl(const char *file, int line,
401 uint64_t us_elapsed_absolute, int exec_id,
404 const char *event_name = "exec_result";
405 struct json_writer jw = JSON_WRITER_INIT;
407 jw_object_begin(&jw, 0);
408 event_fmt_prepare(event_name, file, line, NULL, &jw);
409 jw_object_intmax(&jw, "exec_id", exec_id);
410 jw_object_intmax(&jw, "code", code);
413 tr2_dst_write_line(&tr2dst_event, &jw.json);
417 static void fn_param_fl(const char *file, int line, const char *param,
420 const char *event_name = "def_param";
421 struct json_writer jw = JSON_WRITER_INIT;
423 jw_object_begin(&jw, 0);
424 event_fmt_prepare(event_name, file, line, NULL, &jw);
425 jw_object_string(&jw, "param", param);
426 jw_object_string(&jw, "value", value);
429 tr2_dst_write_line(&tr2dst_event, &jw.json);
433 static void fn_repo_fl(const char *file, int line,
434 const struct repository *repo)
436 const char *event_name = "def_repo";
437 struct json_writer jw = JSON_WRITER_INIT;
439 jw_object_begin(&jw, 0);
440 event_fmt_prepare(event_name, file, line, repo, &jw);
441 jw_object_string(&jw, "worktree", repo->worktree);
444 tr2_dst_write_line(&tr2dst_event, &jw.json);
448 static void fn_region_enter_printf_va_fl(const char *file, int line,
449 uint64_t us_elapsed_absolute,
450 const char *category,
452 const struct repository *repo,
453 const char *fmt, va_list ap)
455 const char *event_name = "region_enter";
456 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
457 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
458 struct json_writer jw = JSON_WRITER_INIT;
460 jw_object_begin(&jw, 0);
461 event_fmt_prepare(event_name, file, line, repo, &jw);
462 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
464 jw_object_string(&jw, "category", category);
466 jw_object_string(&jw, "label", label);
467 maybe_add_string_va(&jw, "msg", fmt, ap);
470 tr2_dst_write_line(&tr2dst_event, &jw.json);
475 static void fn_region_leave_printf_va_fl(
476 const char *file, int line, uint64_t us_elapsed_absolute,
477 uint64_t us_elapsed_region, const char *category, const char *label,
478 const struct repository *repo, const char *fmt, va_list ap)
480 const char *event_name = "region_leave";
481 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
482 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
483 struct json_writer jw = JSON_WRITER_INIT;
484 double t_rel = (double)us_elapsed_region / 1000000.0;
486 jw_object_begin(&jw, 0);
487 event_fmt_prepare(event_name, file, line, repo, &jw);
488 jw_object_double(&jw, "t_rel", 6, t_rel);
489 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
491 jw_object_string(&jw, "category", category);
493 jw_object_string(&jw, "label", label);
494 maybe_add_string_va(&jw, "msg", fmt, ap);
497 tr2_dst_write_line(&tr2dst_event, &jw.json);
502 static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
503 uint64_t us_elapsed_region, const char *category,
504 const struct repository *repo, const char *key,
507 const char *event_name = "data";
508 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
509 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
510 struct json_writer jw = JSON_WRITER_INIT;
511 double t_abs = (double)us_elapsed_absolute / 1000000.0;
512 double t_rel = (double)us_elapsed_region / 1000000.0;
514 jw_object_begin(&jw, 0);
515 event_fmt_prepare(event_name, file, line, repo, &jw);
516 jw_object_double(&jw, "t_abs", 6, t_abs);
517 jw_object_double(&jw, "t_rel", 6, t_rel);
518 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
519 jw_object_string(&jw, "category", category);
520 jw_object_string(&jw, "key", key);
521 jw_object_string(&jw, "value", value);
524 tr2_dst_write_line(&tr2dst_event, &jw.json);
529 static void fn_data_json_fl(const char *file, int line,
530 uint64_t us_elapsed_absolute,
531 uint64_t us_elapsed_region, const char *category,
532 const struct repository *repo, const char *key,
533 const struct json_writer *value)
535 const char *event_name = "data_json";
536 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
537 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
538 struct json_writer jw = JSON_WRITER_INIT;
539 double t_abs = (double)us_elapsed_absolute / 1000000.0;
540 double t_rel = (double)us_elapsed_region / 1000000.0;
542 jw_object_begin(&jw, 0);
543 event_fmt_prepare(event_name, file, line, repo, &jw);
544 jw_object_double(&jw, "t_abs", 6, t_abs);
545 jw_object_double(&jw, "t_rel", 6, t_rel);
546 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
547 jw_object_string(&jw, "category", category);
548 jw_object_string(&jw, "key", key);
549 jw_object_sub_jw(&jw, "value", value);
552 tr2_dst_write_line(&tr2dst_event, &jw.json);
557 struct tr2_tgt tr2_tgt_event = {
581 fn_region_enter_printf_va_fl,
582 fn_region_leave_printf_va_fl,