Merge https://github.com/prati0100/git-gui
[git] / trace2 / tr2_tgt_event.c
1 #include "cache.h"
2 #include "config.h"
3 #include "json-writer.h"
4 #include "run-command.h"
5 #include "version.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"
12
13 static struct tr2_dst tr2dst_event = { TR2_SYSENV_EVENT, 0, 0, 0, 0 };
14
15 /*
16  * The version number of the JSON data generated by the EVENT target in this
17  * source file. The version should be incremented if new event types are added,
18  * if existing fields are removed, or if there are significant changes in
19  * interpretation of existing events or fields. Smaller changes, such as adding
20  * a new field to an existing event, do not require an increment to the EVENT
21  * format version.
22  */
23 #define TR2_EVENT_VERSION "2"
24
25 /*
26  * Region nesting limit for messages written to the event target.
27  *
28  * The "region_enter" and "region_leave" messages (especially recursive
29  * messages such as those produced while diving the worktree or index)
30  * are primarily intended for the performance target during debugging.
31  *
32  * Some of the outer-most messages, however, may be of interest to the
33  * event target.  Use the TR2_SYSENV_EVENT_NESTING setting to increase
34  * region details in the event target.
35  */
36 static int tr2env_event_max_nesting_levels = 2;
37
38 /*
39  * Use the TR2_SYSENV_EVENT_BRIEF to omit the <time>, <file>, and
40  * <line> fields from most events.
41  */
42 static int tr2env_event_be_brief;
43
44 static int fn_init(void)
45 {
46         int want = tr2_dst_trace_want(&tr2dst_event);
47         int max_nesting;
48         int want_brief;
49         const char *nesting;
50         const char *brief;
51
52         if (!want)
53                 return want;
54
55         nesting = tr2_sysenv_get(TR2_SYSENV_EVENT_NESTING);
56         if (nesting && *nesting && ((max_nesting = atoi(nesting)) > 0))
57                 tr2env_event_max_nesting_levels = max_nesting;
58
59         brief = tr2_sysenv_get(TR2_SYSENV_EVENT_BRIEF);
60         if (brief && *brief &&
61             ((want_brief = git_parse_maybe_bool(brief)) != -1))
62                 tr2env_event_be_brief = want_brief;
63
64         return want;
65 }
66
67 static void fn_term(void)
68 {
69         tr2_dst_trace_disable(&tr2dst_event);
70 }
71
72 /*
73  * Append common key-value pairs to the currently open JSON object.
74  *     "event:"<event_name>"
75  *      "sid":"<sid>"
76  *   "thread":"<thread_name>"
77  *     "time":"<time>"
78  *     "file":"<filename>"
79  *     "line":<line_number>
80  *     "repo":<repo_id>
81  */
82 static void event_fmt_prepare(const char *event_name, const char *file,
83                               int line, const struct repository *repo,
84                               struct json_writer *jw)
85 {
86         struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
87         struct tr2_tbuf tb_now;
88
89         jw_object_string(jw, "event", event_name);
90         jw_object_string(jw, "sid", tr2_sid_get());
91         jw_object_string(jw, "thread", ctx->thread_name.buf);
92
93         /*
94          * In brief mode, only emit <time> on these 2 event types.
95          */
96         if (!tr2env_event_be_brief || !strcmp(event_name, "version") ||
97             !strcmp(event_name, "atexit")) {
98                 tr2_tbuf_utc_datetime_extended(&tb_now);
99                 jw_object_string(jw, "time", tb_now.buf);
100         }
101
102         if (!tr2env_event_be_brief && file && *file) {
103                 jw_object_string(jw, "file", file);
104                 jw_object_intmax(jw, "line", line);
105         }
106
107         if (repo)
108                 jw_object_intmax(jw, "repo", repo->trace2_repo_id);
109 }
110
111 static void fn_too_many_files_fl(const char *file, int line)
112 {
113         const char *event_name = "too_many_files";
114         struct json_writer jw = JSON_WRITER_INIT;
115
116         jw_object_begin(&jw, 0);
117         event_fmt_prepare(event_name, file, line, NULL, &jw);
118         jw_end(&jw);
119
120         tr2_dst_write_line(&tr2dst_event, &jw.json);
121         jw_release(&jw);
122 }
123
124 static void fn_version_fl(const char *file, int line)
125 {
126         const char *event_name = "version";
127         struct json_writer jw = JSON_WRITER_INIT;
128
129         jw_object_begin(&jw, 0);
130         event_fmt_prepare(event_name, file, line, NULL, &jw);
131         jw_object_string(&jw, "evt", TR2_EVENT_VERSION);
132         jw_object_string(&jw, "exe", git_version_string);
133         jw_end(&jw);
134
135         tr2_dst_write_line(&tr2dst_event, &jw.json);
136         jw_release(&jw);
137
138         if (tr2dst_event.too_many_files)
139                 fn_too_many_files_fl(file, line);
140 }
141
142 static void fn_start_fl(const char *file, int line,
143                         uint64_t us_elapsed_absolute, const char **argv)
144 {
145         const char *event_name = "start";
146         struct json_writer jw = JSON_WRITER_INIT;
147         double t_abs = (double)us_elapsed_absolute / 1000000.0;
148
149         jw_object_begin(&jw, 0);
150         event_fmt_prepare(event_name, file, line, NULL, &jw);
151         jw_object_double(&jw, "t_abs", 6, t_abs);
152         jw_object_inline_begin_array(&jw, "argv");
153         jw_array_argv(&jw, argv);
154         jw_end(&jw);
155         jw_end(&jw);
156
157         tr2_dst_write_line(&tr2dst_event, &jw.json);
158         jw_release(&jw);
159 }
160
161 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
162                        int code)
163 {
164         const char *event_name = "exit";
165         struct json_writer jw = JSON_WRITER_INIT;
166         double t_abs = (double)us_elapsed_absolute / 1000000.0;
167
168         jw_object_begin(&jw, 0);
169         event_fmt_prepare(event_name, file, line, NULL, &jw);
170         jw_object_double(&jw, "t_abs", 6, t_abs);
171         jw_object_intmax(&jw, "code", code);
172         jw_end(&jw);
173
174         tr2_dst_write_line(&tr2dst_event, &jw.json);
175         jw_release(&jw);
176 }
177
178 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
179 {
180         const char *event_name = "signal";
181         struct json_writer jw = JSON_WRITER_INIT;
182         double t_abs = (double)us_elapsed_absolute / 1000000.0;
183
184         jw_object_begin(&jw, 0);
185         event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
186         jw_object_double(&jw, "t_abs", 6, t_abs);
187         jw_object_intmax(&jw, "signo", signo);
188         jw_end(&jw);
189
190         tr2_dst_write_line(&tr2dst_event, &jw.json);
191         jw_release(&jw);
192 }
193
194 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
195 {
196         const char *event_name = "atexit";
197         struct json_writer jw = JSON_WRITER_INIT;
198         double t_abs = (double)us_elapsed_absolute / 1000000.0;
199
200         jw_object_begin(&jw, 0);
201         event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
202         jw_object_double(&jw, "t_abs", 6, t_abs);
203         jw_object_intmax(&jw, "code", code);
204         jw_end(&jw);
205
206         tr2_dst_write_line(&tr2dst_event, &jw.json);
207         jw_release(&jw);
208 }
209
210 static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
211                                 const char *fmt, va_list ap)
212 {
213         if (fmt && *fmt) {
214                 va_list copy_ap;
215                 struct strbuf buf = STRBUF_INIT;
216
217                 va_copy(copy_ap, ap);
218                 strbuf_vaddf(&buf, fmt, copy_ap);
219                 va_end(copy_ap);
220
221                 jw_object_string(jw, field_name, buf.buf);
222                 strbuf_release(&buf);
223                 return;
224         }
225 }
226
227 static void fn_error_va_fl(const char *file, int line, const char *fmt,
228                            va_list ap)
229 {
230         const char *event_name = "error";
231         struct json_writer jw = JSON_WRITER_INIT;
232
233         jw_object_begin(&jw, 0);
234         event_fmt_prepare(event_name, file, line, NULL, &jw);
235         maybe_add_string_va(&jw, "msg", fmt, ap);
236         /*
237          * Also emit the format string as a field in case
238          * post-processors want to aggregate common error
239          * messages by type without argument fields (such
240          * as pathnames or branch names) cluttering it up.
241          */
242         if (fmt && *fmt)
243                 jw_object_string(&jw, "fmt", fmt);
244         jw_end(&jw);
245
246         tr2_dst_write_line(&tr2dst_event, &jw.json);
247         jw_release(&jw);
248 }
249
250 static void fn_command_path_fl(const char *file, int line, const char *pathname)
251 {
252         const char *event_name = "cmd_path";
253         struct json_writer jw = JSON_WRITER_INIT;
254
255         jw_object_begin(&jw, 0);
256         event_fmt_prepare(event_name, file, line, NULL, &jw);
257         jw_object_string(&jw, "path", pathname);
258         jw_end(&jw);
259
260         tr2_dst_write_line(&tr2dst_event, &jw.json);
261         jw_release(&jw);
262 }
263
264 static void fn_command_name_fl(const char *file, int line, const char *name,
265                                const char *hierarchy)
266 {
267         const char *event_name = "cmd_name";
268         struct json_writer jw = JSON_WRITER_INIT;
269
270         jw_object_begin(&jw, 0);
271         event_fmt_prepare(event_name, file, line, NULL, &jw);
272         jw_object_string(&jw, "name", name);
273         if (hierarchy && *hierarchy)
274                 jw_object_string(&jw, "hierarchy", hierarchy);
275         jw_end(&jw);
276
277         tr2_dst_write_line(&tr2dst_event, &jw.json);
278         jw_release(&jw);
279 }
280
281 static void fn_command_mode_fl(const char *file, int line, const char *mode)
282 {
283         const char *event_name = "cmd_mode";
284         struct json_writer jw = JSON_WRITER_INIT;
285
286         jw_object_begin(&jw, 0);
287         event_fmt_prepare(event_name, file, line, NULL, &jw);
288         jw_object_string(&jw, "name", mode);
289         jw_end(&jw);
290
291         tr2_dst_write_line(&tr2dst_event, &jw.json);
292         jw_release(&jw);
293 }
294
295 static void fn_alias_fl(const char *file, int line, const char *alias,
296                         const char **argv)
297 {
298         const char *event_name = "alias";
299         struct json_writer jw = JSON_WRITER_INIT;
300
301         jw_object_begin(&jw, 0);
302         event_fmt_prepare(event_name, file, line, NULL, &jw);
303         jw_object_string(&jw, "alias", alias);
304         jw_object_inline_begin_array(&jw, "argv");
305         jw_array_argv(&jw, argv);
306         jw_end(&jw);
307         jw_end(&jw);
308
309         tr2_dst_write_line(&tr2dst_event, &jw.json);
310         jw_release(&jw);
311 }
312
313 static void fn_child_start_fl(const char *file, int line,
314                               uint64_t us_elapsed_absolute,
315                               const struct child_process *cmd)
316 {
317         const char *event_name = "child_start";
318         struct json_writer jw = JSON_WRITER_INIT;
319
320         jw_object_begin(&jw, 0);
321         event_fmt_prepare(event_name, file, line, NULL, &jw);
322         jw_object_intmax(&jw, "child_id", cmd->trace2_child_id);
323         if (cmd->trace2_hook_name) {
324                 jw_object_string(&jw, "child_class", "hook");
325                 jw_object_string(&jw, "hook_name", cmd->trace2_hook_name);
326         } else {
327                 const char *child_class =
328                         cmd->trace2_child_class ? cmd->trace2_child_class : "?";
329                 jw_object_string(&jw, "child_class", child_class);
330         }
331         if (cmd->dir)
332                 jw_object_string(&jw, "cd", cmd->dir);
333         jw_object_bool(&jw, "use_shell", cmd->use_shell);
334         jw_object_inline_begin_array(&jw, "argv");
335         if (cmd->git_cmd)
336                 jw_array_string(&jw, "git");
337         jw_array_argv(&jw, cmd->argv);
338         jw_end(&jw);
339         jw_end(&jw);
340
341         tr2_dst_write_line(&tr2dst_event, &jw.json);
342         jw_release(&jw);
343 }
344
345 static void fn_child_exit_fl(const char *file, int line,
346                              uint64_t us_elapsed_absolute, int cid, int pid,
347                              int code, uint64_t us_elapsed_child)
348 {
349         const char *event_name = "child_exit";
350         struct json_writer jw = JSON_WRITER_INIT;
351         double t_rel = (double)us_elapsed_child / 1000000.0;
352
353         jw_object_begin(&jw, 0);
354         event_fmt_prepare(event_name, file, line, NULL, &jw);
355         jw_object_intmax(&jw, "child_id", cid);
356         jw_object_intmax(&jw, "pid", pid);
357         jw_object_intmax(&jw, "code", code);
358         jw_object_double(&jw, "t_rel", 6, t_rel);
359         jw_end(&jw);
360
361         tr2_dst_write_line(&tr2dst_event, &jw.json);
362
363         jw_release(&jw);
364 }
365
366 static void fn_thread_start_fl(const char *file, int line,
367                                uint64_t us_elapsed_absolute)
368 {
369         const char *event_name = "thread_start";
370         struct json_writer jw = JSON_WRITER_INIT;
371
372         jw_object_begin(&jw, 0);
373         event_fmt_prepare(event_name, file, line, NULL, &jw);
374         jw_end(&jw);
375
376         tr2_dst_write_line(&tr2dst_event, &jw.json);
377         jw_release(&jw);
378 }
379
380 static void fn_thread_exit_fl(const char *file, int line,
381                               uint64_t us_elapsed_absolute,
382                               uint64_t us_elapsed_thread)
383 {
384         const char *event_name = "thread_exit";
385         struct json_writer jw = JSON_WRITER_INIT;
386         double t_rel = (double)us_elapsed_thread / 1000000.0;
387
388         jw_object_begin(&jw, 0);
389         event_fmt_prepare(event_name, file, line, NULL, &jw);
390         jw_object_double(&jw, "t_rel", 6, t_rel);
391         jw_end(&jw);
392
393         tr2_dst_write_line(&tr2dst_event, &jw.json);
394         jw_release(&jw);
395 }
396
397 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
398                        int exec_id, const char *exe, const char **argv)
399 {
400         const char *event_name = "exec";
401         struct json_writer jw = JSON_WRITER_INIT;
402
403         jw_object_begin(&jw, 0);
404         event_fmt_prepare(event_name, file, line, NULL, &jw);
405         jw_object_intmax(&jw, "exec_id", exec_id);
406         if (exe)
407                 jw_object_string(&jw, "exe", exe);
408         jw_object_inline_begin_array(&jw, "argv");
409         jw_array_argv(&jw, argv);
410         jw_end(&jw);
411         jw_end(&jw);
412
413         tr2_dst_write_line(&tr2dst_event, &jw.json);
414         jw_release(&jw);
415 }
416
417 static void fn_exec_result_fl(const char *file, int line,
418                               uint64_t us_elapsed_absolute, int exec_id,
419                               int code)
420 {
421         const char *event_name = "exec_result";
422         struct json_writer jw = JSON_WRITER_INIT;
423
424         jw_object_begin(&jw, 0);
425         event_fmt_prepare(event_name, file, line, NULL, &jw);
426         jw_object_intmax(&jw, "exec_id", exec_id);
427         jw_object_intmax(&jw, "code", code);
428         jw_end(&jw);
429
430         tr2_dst_write_line(&tr2dst_event, &jw.json);
431         jw_release(&jw);
432 }
433
434 static void fn_param_fl(const char *file, int line, const char *param,
435                         const char *value)
436 {
437         const char *event_name = "def_param";
438         struct json_writer jw = JSON_WRITER_INIT;
439
440         jw_object_begin(&jw, 0);
441         event_fmt_prepare(event_name, file, line, NULL, &jw);
442         jw_object_string(&jw, "param", param);
443         jw_object_string(&jw, "value", value);
444         jw_end(&jw);
445
446         tr2_dst_write_line(&tr2dst_event, &jw.json);
447         jw_release(&jw);
448 }
449
450 static void fn_repo_fl(const char *file, int line,
451                        const struct repository *repo)
452 {
453         const char *event_name = "def_repo";
454         struct json_writer jw = JSON_WRITER_INIT;
455
456         jw_object_begin(&jw, 0);
457         event_fmt_prepare(event_name, file, line, repo, &jw);
458         jw_object_string(&jw, "worktree", repo->worktree);
459         jw_end(&jw);
460
461         tr2_dst_write_line(&tr2dst_event, &jw.json);
462         jw_release(&jw);
463 }
464
465 static void fn_region_enter_printf_va_fl(const char *file, int line,
466                                          uint64_t us_elapsed_absolute,
467                                          const char *category,
468                                          const char *label,
469                                          const struct repository *repo,
470                                          const char *fmt, va_list ap)
471 {
472         const char *event_name = "region_enter";
473         struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
474         if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
475                 struct json_writer jw = JSON_WRITER_INIT;
476
477                 jw_object_begin(&jw, 0);
478                 event_fmt_prepare(event_name, file, line, repo, &jw);
479                 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
480                 if (category)
481                         jw_object_string(&jw, "category", category);
482                 if (label)
483                         jw_object_string(&jw, "label", label);
484                 maybe_add_string_va(&jw, "msg", fmt, ap);
485                 jw_end(&jw);
486
487                 tr2_dst_write_line(&tr2dst_event, &jw.json);
488                 jw_release(&jw);
489         }
490 }
491
492 static void fn_region_leave_printf_va_fl(
493         const char *file, int line, uint64_t us_elapsed_absolute,
494         uint64_t us_elapsed_region, const char *category, const char *label,
495         const struct repository *repo, const char *fmt, va_list ap)
496 {
497         const char *event_name = "region_leave";
498         struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
499         if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
500                 struct json_writer jw = JSON_WRITER_INIT;
501                 double t_rel = (double)us_elapsed_region / 1000000.0;
502
503                 jw_object_begin(&jw, 0);
504                 event_fmt_prepare(event_name, file, line, repo, &jw);
505                 jw_object_double(&jw, "t_rel", 6, t_rel);
506                 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
507                 if (category)
508                         jw_object_string(&jw, "category", category);
509                 if (label)
510                         jw_object_string(&jw, "label", label);
511                 maybe_add_string_va(&jw, "msg", fmt, ap);
512                 jw_end(&jw);
513
514                 tr2_dst_write_line(&tr2dst_event, &jw.json);
515                 jw_release(&jw);
516         }
517 }
518
519 static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
520                        uint64_t us_elapsed_region, const char *category,
521                        const struct repository *repo, const char *key,
522                        const char *value)
523 {
524         const char *event_name = "data";
525         struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
526         if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
527                 struct json_writer jw = JSON_WRITER_INIT;
528                 double t_abs = (double)us_elapsed_absolute / 1000000.0;
529                 double t_rel = (double)us_elapsed_region / 1000000.0;
530
531                 jw_object_begin(&jw, 0);
532                 event_fmt_prepare(event_name, file, line, repo, &jw);
533                 jw_object_double(&jw, "t_abs", 6, t_abs);
534                 jw_object_double(&jw, "t_rel", 6, t_rel);
535                 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
536                 jw_object_string(&jw, "category", category);
537                 jw_object_string(&jw, "key", key);
538                 jw_object_string(&jw, "value", value);
539                 jw_end(&jw);
540
541                 tr2_dst_write_line(&tr2dst_event, &jw.json);
542                 jw_release(&jw);
543         }
544 }
545
546 static void fn_data_json_fl(const char *file, int line,
547                             uint64_t us_elapsed_absolute,
548                             uint64_t us_elapsed_region, const char *category,
549                             const struct repository *repo, const char *key,
550                             const struct json_writer *value)
551 {
552         const char *event_name = "data_json";
553         struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
554         if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
555                 struct json_writer jw = JSON_WRITER_INIT;
556                 double t_abs = (double)us_elapsed_absolute / 1000000.0;
557                 double t_rel = (double)us_elapsed_region / 1000000.0;
558
559                 jw_object_begin(&jw, 0);
560                 event_fmt_prepare(event_name, file, line, repo, &jw);
561                 jw_object_double(&jw, "t_abs", 6, t_abs);
562                 jw_object_double(&jw, "t_rel", 6, t_rel);
563                 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
564                 jw_object_string(&jw, "category", category);
565                 jw_object_string(&jw, "key", key);
566                 jw_object_sub_jw(&jw, "value", value);
567                 jw_end(&jw);
568
569                 tr2_dst_write_line(&tr2dst_event, &jw.json);
570                 jw_release(&jw);
571         }
572 }
573
574 struct tr2_tgt tr2_tgt_event = {
575         &tr2dst_event,
576
577         fn_init,
578         fn_term,
579
580         fn_version_fl,
581         fn_start_fl,
582         fn_exit_fl,
583         fn_signal,
584         fn_atexit,
585         fn_error_va_fl,
586         fn_command_path_fl,
587         fn_command_name_fl,
588         fn_command_mode_fl,
589         fn_alias_fl,
590         fn_child_start_fl,
591         fn_child_exit_fl,
592         fn_thread_start_fl,
593         fn_thread_exit_fl,
594         fn_exec_fl,
595         fn_exec_result_fl,
596         fn_param_fl,
597         fn_repo_fl,
598         fn_region_enter_printf_va_fl,
599         fn_region_leave_printf_va_fl,
600         fn_data_fl,
601         fn_data_json_fl,
602         NULL, /* printf */
603 };