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 jw_object_string(jw
, field_name
, fmt
);
215 static void fn_error_va_fl(const char *file
, int line
, const char *fmt
,
218 const char *event_name
= "error";
219 struct json_writer jw
= JSON_WRITER_INIT
;
221 jw_object_begin(&jw
, 0);
222 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
223 maybe_add_string_va(&jw
, "msg", fmt
, ap
);
225 * Also emit the format string as a field in case
226 * post-processors want to aggregate common error
227 * messages by type without argument fields (such
228 * as pathnames or branch names) cluttering it up.
231 jw_object_string(&jw
, "fmt", fmt
);
234 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
238 static void fn_command_path_fl(const char *file
, int line
, const char *pathname
)
240 const char *event_name
= "cmd_path";
241 struct json_writer jw
= JSON_WRITER_INIT
;
243 jw_object_begin(&jw
, 0);
244 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
245 jw_object_string(&jw
, "path", pathname
);
248 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
252 static void fn_command_name_fl(const char *file
, int line
, const char *name
,
253 const char *hierarchy
)
255 const char *event_name
= "cmd_name";
256 struct json_writer jw
= JSON_WRITER_INIT
;
258 jw_object_begin(&jw
, 0);
259 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
260 jw_object_string(&jw
, "name", name
);
261 if (hierarchy
&& *hierarchy
)
262 jw_object_string(&jw
, "hierarchy", hierarchy
);
265 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
269 static void fn_command_mode_fl(const char *file
, int line
, const char *mode
)
271 const char *event_name
= "cmd_mode";
272 struct json_writer jw
= JSON_WRITER_INIT
;
274 jw_object_begin(&jw
, 0);
275 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
276 jw_object_string(&jw
, "name", mode
);
279 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
283 static void fn_alias_fl(const char *file
, int line
, const char *alias
,
286 const char *event_name
= "alias";
287 struct json_writer jw
= JSON_WRITER_INIT
;
289 jw_object_begin(&jw
, 0);
290 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
291 jw_object_string(&jw
, "alias", alias
);
292 jw_object_inline_begin_array(&jw
, "argv");
293 jw_array_argv(&jw
, argv
);
297 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
301 static void fn_child_start_fl(const char *file
, int line
,
302 uint64_t us_elapsed_absolute
,
303 const struct child_process
*cmd
)
305 const char *event_name
= "child_start";
306 struct json_writer jw
= JSON_WRITER_INIT
;
308 jw_object_begin(&jw
, 0);
309 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
310 jw_object_intmax(&jw
, "child_id", cmd
->trace2_child_id
);
311 if (cmd
->trace2_hook_name
) {
312 jw_object_string(&jw
, "child_class", "hook");
313 jw_object_string(&jw
, "hook_name", cmd
->trace2_hook_name
);
315 const char *child_class
=
316 cmd
->trace2_child_class
? cmd
->trace2_child_class
: "?";
317 jw_object_string(&jw
, "child_class", child_class
);
320 jw_object_string(&jw
, "cd", cmd
->dir
);
321 jw_object_bool(&jw
, "use_shell", cmd
->use_shell
);
322 jw_object_inline_begin_array(&jw
, "argv");
324 jw_array_string(&jw
, "git");
325 jw_array_argv(&jw
, cmd
->argv
);
329 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
333 static void fn_child_exit_fl(const char *file
, int line
,
334 uint64_t us_elapsed_absolute
, int cid
, int pid
,
335 int code
, uint64_t us_elapsed_child
)
337 const char *event_name
= "child_exit";
338 struct json_writer jw
= JSON_WRITER_INIT
;
339 double t_rel
= (double)us_elapsed_child
/ 1000000.0;
341 jw_object_begin(&jw
, 0);
342 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
343 jw_object_intmax(&jw
, "child_id", cid
);
344 jw_object_intmax(&jw
, "pid", pid
);
345 jw_object_intmax(&jw
, "code", code
);
346 jw_object_double(&jw
, "t_rel", 6, t_rel
);
349 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
354 static void fn_thread_start_fl(const char *file
, int line
,
355 uint64_t us_elapsed_absolute
)
357 const char *event_name
= "thread_start";
358 struct json_writer jw
= JSON_WRITER_INIT
;
360 jw_object_begin(&jw
, 0);
361 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
364 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
368 static void fn_thread_exit_fl(const char *file
, int line
,
369 uint64_t us_elapsed_absolute
,
370 uint64_t us_elapsed_thread
)
372 const char *event_name
= "thread_exit";
373 struct json_writer jw
= JSON_WRITER_INIT
;
374 double t_rel
= (double)us_elapsed_thread
/ 1000000.0;
376 jw_object_begin(&jw
, 0);
377 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
378 jw_object_double(&jw
, "t_rel", 6, t_rel
);
381 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
385 static void fn_exec_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
386 int exec_id
, const char *exe
, const char **argv
)
388 const char *event_name
= "exec";
389 struct json_writer jw
= JSON_WRITER_INIT
;
391 jw_object_begin(&jw
, 0);
392 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
393 jw_object_intmax(&jw
, "exec_id", exec_id
);
395 jw_object_string(&jw
, "exe", exe
);
396 jw_object_inline_begin_array(&jw
, "argv");
397 jw_array_argv(&jw
, argv
);
401 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
405 static void fn_exec_result_fl(const char *file
, int line
,
406 uint64_t us_elapsed_absolute
, int exec_id
,
409 const char *event_name
= "exec_result";
410 struct json_writer jw
= JSON_WRITER_INIT
;
412 jw_object_begin(&jw
, 0);
413 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
414 jw_object_intmax(&jw
, "exec_id", exec_id
);
415 jw_object_intmax(&jw
, "code", code
);
418 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
422 static void fn_param_fl(const char *file
, int line
, const char *param
,
425 const char *event_name
= "def_param";
426 struct json_writer jw
= JSON_WRITER_INIT
;
428 jw_object_begin(&jw
, 0);
429 event_fmt_prepare(event_name
, file
, line
, NULL
, &jw
);
430 jw_object_string(&jw
, "param", param
);
431 jw_object_string(&jw
, "value", value
);
434 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
438 static void fn_repo_fl(const char *file
, int line
,
439 const struct repository
*repo
)
441 const char *event_name
= "def_repo";
442 struct json_writer jw
= JSON_WRITER_INIT
;
444 jw_object_begin(&jw
, 0);
445 event_fmt_prepare(event_name
, file
, line
, repo
, &jw
);
446 jw_object_string(&jw
, "worktree", repo
->worktree
);
449 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
453 static void fn_region_enter_printf_va_fl(const char *file
, int line
,
454 uint64_t us_elapsed_absolute
,
455 const char *category
,
457 const struct repository
*repo
,
458 const char *fmt
, va_list ap
)
460 const char *event_name
= "region_enter";
461 struct tr2tls_thread_ctx
*ctx
= tr2tls_get_self();
462 if (ctx
->nr_open_regions
<= tr2env_event_max_nesting_levels
) {
463 struct json_writer jw
= JSON_WRITER_INIT
;
465 jw_object_begin(&jw
, 0);
466 event_fmt_prepare(event_name
, file
, line
, repo
, &jw
);
467 jw_object_intmax(&jw
, "nesting", ctx
->nr_open_regions
);
469 jw_object_string(&jw
, "category", category
);
471 jw_object_string(&jw
, "label", label
);
472 maybe_add_string_va(&jw
, "msg", fmt
, ap
);
475 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
480 static void fn_region_leave_printf_va_fl(
481 const char *file
, int line
, uint64_t us_elapsed_absolute
,
482 uint64_t us_elapsed_region
, const char *category
, const char *label
,
483 const struct repository
*repo
, const char *fmt
, va_list ap
)
485 const char *event_name
= "region_leave";
486 struct tr2tls_thread_ctx
*ctx
= tr2tls_get_self();
487 if (ctx
->nr_open_regions
<= tr2env_event_max_nesting_levels
) {
488 struct json_writer jw
= JSON_WRITER_INIT
;
489 double t_rel
= (double)us_elapsed_region
/ 1000000.0;
491 jw_object_begin(&jw
, 0);
492 event_fmt_prepare(event_name
, file
, line
, repo
, &jw
);
493 jw_object_double(&jw
, "t_rel", 6, t_rel
);
494 jw_object_intmax(&jw
, "nesting", ctx
->nr_open_regions
);
496 jw_object_string(&jw
, "category", category
);
498 jw_object_string(&jw
, "label", label
);
499 maybe_add_string_va(&jw
, "msg", fmt
, ap
);
502 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
507 static void fn_data_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
508 uint64_t us_elapsed_region
, const char *category
,
509 const struct repository
*repo
, const char *key
,
512 const char *event_name
= "data";
513 struct tr2tls_thread_ctx
*ctx
= tr2tls_get_self();
514 if (ctx
->nr_open_regions
<= tr2env_event_max_nesting_levels
) {
515 struct json_writer jw
= JSON_WRITER_INIT
;
516 double t_abs
= (double)us_elapsed_absolute
/ 1000000.0;
517 double t_rel
= (double)us_elapsed_region
/ 1000000.0;
519 jw_object_begin(&jw
, 0);
520 event_fmt_prepare(event_name
, file
, line
, repo
, &jw
);
521 jw_object_double(&jw
, "t_abs", 6, t_abs
);
522 jw_object_double(&jw
, "t_rel", 6, t_rel
);
523 jw_object_intmax(&jw
, "nesting", ctx
->nr_open_regions
);
524 jw_object_string(&jw
, "category", category
);
525 jw_object_string(&jw
, "key", key
);
526 jw_object_string(&jw
, "value", value
);
529 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
534 static void fn_data_json_fl(const char *file
, int line
,
535 uint64_t us_elapsed_absolute
,
536 uint64_t us_elapsed_region
, const char *category
,
537 const struct repository
*repo
, const char *key
,
538 const struct json_writer
*value
)
540 const char *event_name
= "data_json";
541 struct tr2tls_thread_ctx
*ctx
= tr2tls_get_self();
542 if (ctx
->nr_open_regions
<= tr2env_event_max_nesting_levels
) {
543 struct json_writer jw
= JSON_WRITER_INIT
;
544 double t_abs
= (double)us_elapsed_absolute
/ 1000000.0;
545 double t_rel
= (double)us_elapsed_region
/ 1000000.0;
547 jw_object_begin(&jw
, 0);
548 event_fmt_prepare(event_name
, file
, line
, repo
, &jw
);
549 jw_object_double(&jw
, "t_abs", 6, t_abs
);
550 jw_object_double(&jw
, "t_rel", 6, t_rel
);
551 jw_object_intmax(&jw
, "nesting", ctx
->nr_open_regions
);
552 jw_object_string(&jw
, "category", category
);
553 jw_object_string(&jw
, "key", key
);
554 jw_object_sub_jw(&jw
, "value", value
);
557 tr2_dst_write_line(&tr2dst_event
, &jw
.json
);
562 struct tr2_tgt tr2_tgt_event
= {
586 fn_region_enter_printf_va_fl
,
587 fn_region_leave_printf_va_fl
,