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_tgt.h"
10 #include "trace2/tr2_tls.h"
12 static struct tr2_dst tr2dst_event = { "GIT_TR2_EVENT", 0, 0, 0 };
15 * The version number of the JSON data generated by the EVENT target
16 * in this source file. Update this if you make a significant change
17 * to the JSON fields or message structure. You probably do not need
18 * to update this if you just add another call to one of the existing
21 #define TR2_EVENT_VERSION "1"
24 * Region nesting limit for messages written to the event target.
26 * The "region_enter" and "region_leave" messages (especially recursive
27 * messages such as those produced while diving the worktree or index)
28 * are primarily intended for the performance target during debugging.
30 * Some of the outer-most messages, however, may be of interest to the
31 * event target. Set this environment variable to a larger integer for
32 * more detail in the event target.
34 #define TR2_ENVVAR_EVENT_NESTING "GIT_TR2_EVENT_NESTING"
35 static int tr2env_event_nesting_wanted = 2;
38 * Set this environment variable to true to omit the <time>, <file>, and
39 * <line> fields from most events.
41 #define TR2_ENVVAR_EVENT_BRIEF "GIT_TR2_EVENT_BRIEF"
42 static int tr2env_event_brief;
44 static int fn_init(void)
46 int want = tr2_dst_trace_want(&tr2dst_event);
55 nesting = getenv(TR2_ENVVAR_EVENT_NESTING);
56 if (nesting && ((want_nesting = atoi(nesting)) > 0))
57 tr2env_event_nesting_wanted = want_nesting;
59 brief = getenv(TR2_ENVVAR_EVENT_BRIEF);
60 if (brief && ((want_brief = atoi(brief)) > 0))
61 tr2env_event_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_brief || !strcmp(event_name, "version") ||
96 !strcmp(event_name, "atexit")) {
97 tr2_tbuf_utc_time(&tb_now);
98 jw_object_string(jw, "time", tb_now.buf);
101 if (!tr2env_event_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, const char **argv)
127 const char *event_name = "start";
128 struct json_writer jw = JSON_WRITER_INIT;
130 jw_object_begin(&jw, 0);
131 event_fmt_prepare(event_name, file, line, NULL, &jw);
132 jw_object_inline_begin_array(&jw, "argv");
133 jw_array_argv(&jw, argv);
137 tr2_dst_write_line(&tr2dst_event, &jw.json);
141 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
144 const char *event_name = "exit";
145 struct json_writer jw = JSON_WRITER_INIT;
146 double t_abs = (double)us_elapsed_absolute / 1000000.0;
148 jw_object_begin(&jw, 0);
149 event_fmt_prepare(event_name, file, line, NULL, &jw);
150 jw_object_double(&jw, "t_abs", 6, t_abs);
151 jw_object_intmax(&jw, "code", code);
154 tr2_dst_write_line(&tr2dst_event, &jw.json);
158 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
160 const char *event_name = "signal";
161 struct json_writer jw = JSON_WRITER_INIT;
162 double t_abs = (double)us_elapsed_absolute / 1000000.0;
164 jw_object_begin(&jw, 0);
165 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
166 jw_object_double(&jw, "t_abs", 6, t_abs);
167 jw_object_intmax(&jw, "signo", signo);
170 tr2_dst_write_line(&tr2dst_event, &jw.json);
174 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
176 const char *event_name = "atexit";
177 struct json_writer jw = JSON_WRITER_INIT;
178 double t_abs = (double)us_elapsed_absolute / 1000000.0;
180 jw_object_begin(&jw, 0);
181 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
182 jw_object_double(&jw, "t_abs", 6, t_abs);
183 jw_object_intmax(&jw, "code", code);
186 tr2_dst_write_line(&tr2dst_event, &jw.json);
190 static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
191 const char *fmt, va_list ap)
195 struct strbuf buf = STRBUF_INIT;
197 va_copy(copy_ap, ap);
198 strbuf_vaddf(&buf, fmt, copy_ap);
201 jw_object_string(jw, field_name, buf.buf);
202 strbuf_release(&buf);
207 jw_object_string(jw, field_name, fmt);
212 static void fn_error_va_fl(const char *file, int line, const char *fmt,
215 const char *event_name = "error";
216 struct json_writer jw = JSON_WRITER_INIT;
218 jw_object_begin(&jw, 0);
219 event_fmt_prepare(event_name, file, line, NULL, &jw);
220 maybe_add_string_va(&jw, "msg", fmt, ap);
222 * Also emit the format string as a field in case
223 * post-processors want to aggregate common error
224 * messages by type without argument fields (such
225 * as pathnames or branch names) cluttering it up.
228 jw_object_string(&jw, "fmt", fmt);
231 tr2_dst_write_line(&tr2dst_event, &jw.json);
235 static void fn_command_path_fl(const char *file, int line, const char *pathname)
237 const char *event_name = "cmd_path";
238 struct json_writer jw = JSON_WRITER_INIT;
240 jw_object_begin(&jw, 0);
241 event_fmt_prepare(event_name, file, line, NULL, &jw);
242 jw_object_string(&jw, "path", pathname);
245 tr2_dst_write_line(&tr2dst_event, &jw.json);
249 static void fn_command_name_fl(const char *file, int line, const char *name,
250 const char *hierarchy)
252 const char *event_name = "cmd_name";
253 struct json_writer jw = JSON_WRITER_INIT;
255 jw_object_begin(&jw, 0);
256 event_fmt_prepare(event_name, file, line, NULL, &jw);
257 jw_object_string(&jw, "name", name);
258 if (hierarchy && *hierarchy)
259 jw_object_string(&jw, "hierarchy", hierarchy);
262 tr2_dst_write_line(&tr2dst_event, &jw.json);
266 static void fn_command_mode_fl(const char *file, int line, const char *mode)
268 const char *event_name = "cmd_mode";
269 struct json_writer jw = JSON_WRITER_INIT;
271 jw_object_begin(&jw, 0);
272 event_fmt_prepare(event_name, file, line, NULL, &jw);
273 jw_object_string(&jw, "name", mode);
276 tr2_dst_write_line(&tr2dst_event, &jw.json);
280 static void fn_alias_fl(const char *file, int line, const char *alias,
283 const char *event_name = "alias";
284 struct json_writer jw = JSON_WRITER_INIT;
286 jw_object_begin(&jw, 0);
287 event_fmt_prepare(event_name, file, line, NULL, &jw);
288 jw_object_string(&jw, "alias", alias);
289 jw_object_inline_begin_array(&jw, "argv");
290 jw_array_argv(&jw, argv);
294 tr2_dst_write_line(&tr2dst_event, &jw.json);
298 static void fn_child_start_fl(const char *file, int line,
299 uint64_t us_elapsed_absolute,
300 const struct child_process *cmd)
302 const char *event_name = "child_start";
303 struct json_writer jw = JSON_WRITER_INIT;
305 jw_object_begin(&jw, 0);
306 event_fmt_prepare(event_name, file, line, NULL, &jw);
307 jw_object_intmax(&jw, "child_id", cmd->trace2_child_id);
308 if (cmd->trace2_hook_name) {
309 jw_object_string(&jw, "child_class", "hook");
310 jw_object_string(&jw, "hook_name", cmd->trace2_hook_name);
312 const char *child_class =
313 cmd->trace2_child_class ? cmd->trace2_child_class : "?";
314 jw_object_string(&jw, "child_class", child_class);
317 jw_object_string(&jw, "cd", cmd->dir);
318 jw_object_bool(&jw, "use_shell", cmd->use_shell);
319 jw_object_inline_begin_array(&jw, "argv");
321 jw_array_string(&jw, "git");
322 jw_array_argv(&jw, cmd->argv);
326 tr2_dst_write_line(&tr2dst_event, &jw.json);
330 static void fn_child_exit_fl(const char *file, int line,
331 uint64_t us_elapsed_absolute, int cid, int pid,
332 int code, uint64_t us_elapsed_child)
334 const char *event_name = "child_exit";
335 struct json_writer jw = JSON_WRITER_INIT;
336 double t_rel = (double)us_elapsed_child / 1000000.0;
338 jw_object_begin(&jw, 0);
339 event_fmt_prepare(event_name, file, line, NULL, &jw);
340 jw_object_intmax(&jw, "child_id", cid);
341 jw_object_intmax(&jw, "pid", pid);
342 jw_object_intmax(&jw, "code", code);
343 jw_object_double(&jw, "t_rel", 6, t_rel);
346 tr2_dst_write_line(&tr2dst_event, &jw.json);
351 static void fn_thread_start_fl(const char *file, int line,
352 uint64_t us_elapsed_absolute)
354 const char *event_name = "thread_start";
355 struct json_writer jw = JSON_WRITER_INIT;
357 jw_object_begin(&jw, 0);
358 event_fmt_prepare(event_name, file, line, NULL, &jw);
361 tr2_dst_write_line(&tr2dst_event, &jw.json);
365 static void fn_thread_exit_fl(const char *file, int line,
366 uint64_t us_elapsed_absolute,
367 uint64_t us_elapsed_thread)
369 const char *event_name = "thread_exit";
370 struct json_writer jw = JSON_WRITER_INIT;
371 double t_rel = (double)us_elapsed_thread / 1000000.0;
373 jw_object_begin(&jw, 0);
374 event_fmt_prepare(event_name, file, line, NULL, &jw);
375 jw_object_double(&jw, "t_rel", 6, t_rel);
378 tr2_dst_write_line(&tr2dst_event, &jw.json);
382 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
383 int exec_id, const char *exe, const char **argv)
385 const char *event_name = "exec";
386 struct json_writer jw = JSON_WRITER_INIT;
388 jw_object_begin(&jw, 0);
389 event_fmt_prepare(event_name, file, line, NULL, &jw);
390 jw_object_intmax(&jw, "exec_id", exec_id);
392 jw_object_string(&jw, "exe", exe);
393 jw_object_inline_begin_array(&jw, "argv");
394 jw_array_argv(&jw, argv);
398 tr2_dst_write_line(&tr2dst_event, &jw.json);
402 static void fn_exec_result_fl(const char *file, int line,
403 uint64_t us_elapsed_absolute, int exec_id,
406 const char *event_name = "exec_result";
407 struct json_writer jw = JSON_WRITER_INIT;
409 jw_object_begin(&jw, 0);
410 event_fmt_prepare(event_name, file, line, NULL, &jw);
411 jw_object_intmax(&jw, "exec_id", exec_id);
412 jw_object_intmax(&jw, "code", code);
415 tr2_dst_write_line(&tr2dst_event, &jw.json);
419 static void fn_param_fl(const char *file, int line, const char *param,
422 const char *event_name = "def_param";
423 struct json_writer jw = JSON_WRITER_INIT;
425 jw_object_begin(&jw, 0);
426 event_fmt_prepare(event_name, file, line, NULL, &jw);
427 jw_object_string(&jw, "param", param);
428 jw_object_string(&jw, "value", value);
431 tr2_dst_write_line(&tr2dst_event, &jw.json);
435 static void fn_repo_fl(const char *file, int line,
436 const struct repository *repo)
438 const char *event_name = "def_repo";
439 struct json_writer jw = JSON_WRITER_INIT;
441 jw_object_begin(&jw, 0);
442 event_fmt_prepare(event_name, file, line, repo, &jw);
443 jw_object_string(&jw, "worktree", repo->worktree);
446 tr2_dst_write_line(&tr2dst_event, &jw.json);
450 static void fn_region_enter_printf_va_fl(const char *file, int line,
451 uint64_t us_elapsed_absolute,
452 const char *category,
454 const struct repository *repo,
455 const char *fmt, va_list ap)
457 const char *event_name = "region_enter";
458 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
459 if (ctx->nr_open_regions <= tr2env_event_nesting_wanted) {
460 struct json_writer jw = JSON_WRITER_INIT;
462 jw_object_begin(&jw, 0);
463 event_fmt_prepare(event_name, file, line, repo, &jw);
464 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
466 jw_object_string(&jw, "category", category);
468 jw_object_string(&jw, "label", label);
469 maybe_add_string_va(&jw, "msg", fmt, ap);
472 tr2_dst_write_line(&tr2dst_event, &jw.json);
477 static void fn_region_leave_printf_va_fl(
478 const char *file, int line, uint64_t us_elapsed_absolute,
479 uint64_t us_elapsed_region, const char *category, const char *label,
480 const struct repository *repo, const char *fmt, va_list ap)
482 const char *event_name = "region_leave";
483 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
484 if (ctx->nr_open_regions <= tr2env_event_nesting_wanted) {
485 struct json_writer jw = JSON_WRITER_INIT;
486 double t_rel = (double)us_elapsed_region / 1000000.0;
488 jw_object_begin(&jw, 0);
489 event_fmt_prepare(event_name, file, line, repo, &jw);
490 jw_object_double(&jw, "t_rel", 6, t_rel);
491 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
493 jw_object_string(&jw, "category", category);
495 jw_object_string(&jw, "label", label);
496 maybe_add_string_va(&jw, "msg", fmt, ap);
499 tr2_dst_write_line(&tr2dst_event, &jw.json);
504 static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
505 uint64_t us_elapsed_region, const char *category,
506 const struct repository *repo, const char *key,
509 const char *event_name = "data";
510 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
511 if (ctx->nr_open_regions <= tr2env_event_nesting_wanted) {
512 struct json_writer jw = JSON_WRITER_INIT;
513 double t_abs = (double)us_elapsed_absolute / 1000000.0;
514 double t_rel = (double)us_elapsed_region / 1000000.0;
516 jw_object_begin(&jw, 0);
517 event_fmt_prepare(event_name, file, line, repo, &jw);
518 jw_object_double(&jw, "t_abs", 6, t_abs);
519 jw_object_double(&jw, "t_rel", 6, t_rel);
520 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
521 jw_object_string(&jw, "category", category);
522 jw_object_string(&jw, "key", key);
523 jw_object_string(&jw, "value", value);
526 tr2_dst_write_line(&tr2dst_event, &jw.json);
531 static void fn_data_json_fl(const char *file, int line,
532 uint64_t us_elapsed_absolute,
533 uint64_t us_elapsed_region, const char *category,
534 const struct repository *repo, const char *key,
535 const struct json_writer *value)
537 const char *event_name = "data_json";
538 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
539 if (ctx->nr_open_regions <= tr2env_event_nesting_wanted) {
540 struct json_writer jw = JSON_WRITER_INIT;
541 double t_abs = (double)us_elapsed_absolute / 1000000.0;
542 double t_rel = (double)us_elapsed_region / 1000000.0;
544 jw_object_begin(&jw, 0);
545 event_fmt_prepare(event_name, file, line, repo, &jw);
546 jw_object_double(&jw, "t_abs", 6, t_abs);
547 jw_object_double(&jw, "t_rel", 6, t_rel);
548 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
549 jw_object_string(&jw, "category", category);
550 jw_object_string(&jw, "key", key);
551 jw_object_sub_jw(&jw, "value", value);
554 tr2_dst_write_line(&tr2dst_event, &jw.json);
559 struct tr2_tgt tr2_tgt_event = {
583 fn_region_enter_printf_va_fl,
584 fn_region_leave_printf_va_fl,