Coverage Report

Created: 2024-09-08 06:23

/src/git/trace2/tr2_tgt_normal.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "config.h"
3
#include "repository.h"
4
#include "run-command.h"
5
#include "strbuf.h"
6
#include "quote.h"
7
#include "version.h"
8
#include "trace2/tr2_dst.h"
9
#include "trace2/tr2_sysenv.h"
10
#include "trace2/tr2_tbuf.h"
11
#include "trace2/tr2_tgt.h"
12
#include "trace2/tr2_tls.h"
13
#include "trace2/tr2_tmr.h"
14
15
static struct tr2_dst tr2dst_normal = {
16
  .sysenv_var = TR2_SYSENV_NORMAL,
17
};
18
19
/*
20
 * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>"
21
 * fields from each line written to the builtin normal target.
22
 *
23
 * Unit tests may want to use this to help with testing.
24
 */
25
static int tr2env_normal_be_brief;
26
27
0
#define TR2FMT_NORMAL_FL_WIDTH (50)
28
29
static int fn_init(void)
30
0
{
31
0
  int want = tr2_dst_trace_want(&tr2dst_normal);
32
0
  int want_brief;
33
0
  const char *brief;
34
35
0
  if (!want)
36
0
    return want;
37
38
0
  brief = tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF);
39
0
  if (brief && *brief &&
40
0
      ((want_brief = git_parse_maybe_bool(brief)) != -1))
41
0
    tr2env_normal_be_brief = want_brief;
42
43
0
  return want;
44
0
}
45
46
static void fn_term(void)
47
0
{
48
0
  tr2_dst_trace_disable(&tr2dst_normal);
49
0
}
50
51
static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf)
52
0
{
53
0
  strbuf_setlen(buf, 0);
54
55
0
  if (!tr2env_normal_be_brief) {
56
0
    struct tr2_tbuf tb_now;
57
58
0
    tr2_tbuf_local_time(&tb_now);
59
0
    strbuf_addstr(buf, tb_now.buf);
60
0
    strbuf_addch(buf, ' ');
61
62
0
    if (file && *file)
63
0
      strbuf_addf(buf, "%s:%d ", file, line);
64
0
    while (buf->len < TR2FMT_NORMAL_FL_WIDTH)
65
0
      strbuf_addch(buf, ' ');
66
0
  }
67
0
}
68
69
static void normal_io_write_fl(const char *file, int line,
70
             const struct strbuf *buf_payload)
71
0
{
72
0
  struct strbuf buf_line = STRBUF_INIT;
73
74
0
  normal_fmt_prepare(file, line, &buf_line);
75
0
  strbuf_addbuf(&buf_line, buf_payload);
76
0
  tr2_dst_write_line(&tr2dst_normal, &buf_line);
77
0
  strbuf_release(&buf_line);
78
0
}
79
80
static void fn_version_fl(const char *file, int line)
81
0
{
82
0
  struct strbuf buf_payload = STRBUF_INIT;
83
84
0
  strbuf_addf(&buf_payload, "version %s", git_version_string);
85
0
  normal_io_write_fl(file, line, &buf_payload);
86
0
  strbuf_release(&buf_payload);
87
0
}
88
89
static void fn_start_fl(const char *file, int line,
90
      uint64_t us_elapsed_absolute UNUSED, const char **argv)
91
0
{
92
0
  struct strbuf buf_payload = STRBUF_INIT;
93
94
0
  strbuf_addstr(&buf_payload, "start ");
95
0
  sq_append_quote_argv_pretty(&buf_payload, argv);
96
0
  normal_io_write_fl(file, line, &buf_payload);
97
0
  strbuf_release(&buf_payload);
98
0
}
99
100
static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
101
           int code)
102
0
{
103
0
  struct strbuf buf_payload = STRBUF_INIT;
104
0
  double elapsed = (double)us_elapsed_absolute / 1000000.0;
105
106
0
  strbuf_addf(&buf_payload, "exit elapsed:%.6f code:%d", elapsed, code);
107
0
  normal_io_write_fl(file, line, &buf_payload);
108
0
  strbuf_release(&buf_payload);
109
0
}
110
111
static void fn_signal(uint64_t us_elapsed_absolute, int signo)
112
0
{
113
0
  struct strbuf buf_payload = STRBUF_INIT;
114
0
  double elapsed = (double)us_elapsed_absolute / 1000000.0;
115
116
0
  strbuf_addf(&buf_payload, "signal elapsed:%.6f code:%d", elapsed,
117
0
        signo);
118
0
  normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
119
0
  strbuf_release(&buf_payload);
120
0
}
121
122
static void fn_atexit(uint64_t us_elapsed_absolute, int code)
123
0
{
124
0
  struct strbuf buf_payload = STRBUF_INIT;
125
0
  double elapsed = (double)us_elapsed_absolute / 1000000.0;
126
127
0
  strbuf_addf(&buf_payload, "atexit elapsed:%.6f code:%d", elapsed, code);
128
0
  normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
129
0
  strbuf_release(&buf_payload);
130
0
}
131
132
static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
133
           va_list ap)
134
0
{
135
0
  if (fmt && *fmt) {
136
0
    va_list copy_ap;
137
138
0
    va_copy(copy_ap, ap);
139
0
    strbuf_vaddf(buf, fmt, copy_ap);
140
0
    va_end(copy_ap);
141
0
    return;
142
0
  }
143
0
}
144
145
static void fn_error_va_fl(const char *file, int line, const char *fmt,
146
         va_list ap)
147
0
{
148
0
  struct strbuf buf_payload = STRBUF_INIT;
149
150
0
  strbuf_addstr(&buf_payload, "error");
151
0
  if (fmt && *fmt) {
152
0
    strbuf_addch(&buf_payload, ' ');
153
0
    maybe_append_string_va(&buf_payload, fmt, ap);
154
0
  }
155
0
  normal_io_write_fl(file, line, &buf_payload);
156
0
  strbuf_release(&buf_payload);
157
0
}
158
159
static void fn_command_path_fl(const char *file, int line, const char *pathname)
160
0
{
161
0
  struct strbuf buf_payload = STRBUF_INIT;
162
163
0
  strbuf_addf(&buf_payload, "cmd_path %s", pathname);
164
0
  normal_io_write_fl(file, line, &buf_payload);
165
0
  strbuf_release(&buf_payload);
166
0
}
167
168
static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
169
0
{
170
0
  const char *parent_name = NULL;
171
0
  struct strbuf buf_payload = STRBUF_INIT;
172
173
  /* cmd_ancestry parent <- grandparent <- great-grandparent */
174
0
  strbuf_addstr(&buf_payload, "cmd_ancestry ");
175
0
  while ((parent_name = *parent_names++)) {
176
0
    strbuf_addstr(&buf_payload, parent_name);
177
    /* if we'll write another one after this, add a delimiter */
178
0
    if (parent_names && *parent_names)
179
0
      strbuf_addstr(&buf_payload, " <- ");
180
0
  }
181
182
0
  normal_io_write_fl(file, line, &buf_payload);
183
0
  strbuf_release(&buf_payload);
184
0
}
185
186
static void fn_command_name_fl(const char *file, int line, const char *name,
187
             const char *hierarchy)
188
0
{
189
0
  struct strbuf buf_payload = STRBUF_INIT;
190
191
0
  strbuf_addf(&buf_payload, "cmd_name %s", name);
192
0
  if (hierarchy && *hierarchy)
193
0
    strbuf_addf(&buf_payload, " (%s)", hierarchy);
194
0
  normal_io_write_fl(file, line, &buf_payload);
195
0
  strbuf_release(&buf_payload);
196
0
}
197
198
static void fn_command_mode_fl(const char *file, int line, const char *mode)
199
0
{
200
0
  struct strbuf buf_payload = STRBUF_INIT;
201
202
0
  strbuf_addf(&buf_payload, "cmd_mode %s", mode);
203
0
  normal_io_write_fl(file, line, &buf_payload);
204
0
  strbuf_release(&buf_payload);
205
0
}
206
207
static void fn_alias_fl(const char *file, int line, const char *alias,
208
      const char **argv)
209
0
{
210
0
  struct strbuf buf_payload = STRBUF_INIT;
211
212
0
  strbuf_addf(&buf_payload, "alias %s -> ", alias);
213
0
  sq_append_quote_argv_pretty(&buf_payload, argv);
214
0
  normal_io_write_fl(file, line, &buf_payload);
215
0
  strbuf_release(&buf_payload);
216
0
}
217
218
static void fn_child_start_fl(const char *file, int line,
219
            uint64_t us_elapsed_absolute UNUSED,
220
            const struct child_process *cmd)
221
0
{
222
0
  struct strbuf buf_payload = STRBUF_INIT;
223
224
0
  strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id);
225
226
0
  if (cmd->dir) {
227
0
    strbuf_addstr(&buf_payload, " cd ");
228
0
    sq_quote_buf_pretty(&buf_payload, cmd->dir);
229
0
    strbuf_addstr(&buf_payload, ";");
230
0
  }
231
232
  /*
233
   * TODO if (cmd->env) { Consider dumping changes to environment. }
234
   * See trace_add_env() in run-command.c as used by original trace.c
235
   */
236
237
0
  strbuf_addch(&buf_payload, ' ');
238
0
  if (cmd->git_cmd)
239
0
    strbuf_addstr(&buf_payload, "git ");
240
0
  sq_append_quote_argv_pretty(&buf_payload, cmd->args.v);
241
242
0
  normal_io_write_fl(file, line, &buf_payload);
243
0
  strbuf_release(&buf_payload);
244
0
}
245
246
static void fn_child_exit_fl(const char *file, int line,
247
           uint64_t us_elapsed_absolute UNUSED,
248
           int cid, int pid,
249
           int code, uint64_t us_elapsed_child)
250
0
{
251
0
  struct strbuf buf_payload = STRBUF_INIT;
252
0
  double elapsed = (double)us_elapsed_child / 1000000.0;
253
254
0
  strbuf_addf(&buf_payload, "child_exit[%d] pid:%d code:%d elapsed:%.6f",
255
0
        cid, pid, code, elapsed);
256
0
  normal_io_write_fl(file, line, &buf_payload);
257
0
  strbuf_release(&buf_payload);
258
0
}
259
260
static void fn_child_ready_fl(const char *file, int line,
261
            uint64_t us_elapsed_absolute UNUSED,
262
            int cid, int pid,
263
            const char *ready, uint64_t us_elapsed_child)
264
0
{
265
0
  struct strbuf buf_payload = STRBUF_INIT;
266
0
  double elapsed = (double)us_elapsed_child / 1000000.0;
267
268
0
  strbuf_addf(&buf_payload, "child_ready[%d] pid:%d ready:%s elapsed:%.6f",
269
0
        cid, pid, ready, elapsed);
270
0
  normal_io_write_fl(file, line, &buf_payload);
271
0
  strbuf_release(&buf_payload);
272
0
}
273
274
static void fn_exec_fl(const char *file, int line,
275
           uint64_t us_elapsed_absolute UNUSED,
276
           int exec_id, const char *exe, const char **argv)
277
0
{
278
0
  struct strbuf buf_payload = STRBUF_INIT;
279
280
0
  strbuf_addf(&buf_payload, "exec[%d] ", exec_id);
281
0
  if (exe) {
282
0
    strbuf_addstr(&buf_payload, exe);
283
0
    strbuf_addch(&buf_payload, ' ');
284
0
  }
285
0
  sq_append_quote_argv_pretty(&buf_payload, argv);
286
0
  normal_io_write_fl(file, line, &buf_payload);
287
0
  strbuf_release(&buf_payload);
288
0
}
289
290
static void fn_exec_result_fl(const char *file, int line,
291
            uint64_t us_elapsed_absolute UNUSED,
292
            int exec_id, int code)
293
0
{
294
0
  struct strbuf buf_payload = STRBUF_INIT;
295
296
0
  strbuf_addf(&buf_payload, "exec_result[%d] code:%d", exec_id, code);
297
0
  if (code > 0)
298
0
    strbuf_addf(&buf_payload, " err:%s", strerror(code));
299
0
  normal_io_write_fl(file, line, &buf_payload);
300
0
  strbuf_release(&buf_payload);
301
0
}
302
303
static void fn_param_fl(const char *file, int line, const char *param,
304
      const char *value, const struct key_value_info *kvi)
305
0
{
306
0
  struct strbuf buf_payload = STRBUF_INIT;
307
0
  enum config_scope scope = kvi->scope;
308
0
  const char *scope_name = config_scope_name(scope);
309
310
0
  strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
311
0
        value);
312
0
  normal_io_write_fl(file, line, &buf_payload);
313
0
  strbuf_release(&buf_payload);
314
0
}
315
316
static void fn_repo_fl(const char *file, int line,
317
           const struct repository *repo)
318
0
{
319
0
  struct strbuf buf_payload = STRBUF_INIT;
320
321
0
  strbuf_addstr(&buf_payload, "worktree ");
322
0
  sq_quote_buf_pretty(&buf_payload, repo->worktree);
323
0
  normal_io_write_fl(file, line, &buf_payload);
324
0
  strbuf_release(&buf_payload);
325
0
}
326
327
static void fn_printf_va_fl(const char *file, int line,
328
          uint64_t us_elapsed_absolute UNUSED,
329
          const char *fmt,
330
          va_list ap)
331
0
{
332
0
  struct strbuf buf_payload = STRBUF_INIT;
333
334
0
  maybe_append_string_va(&buf_payload, fmt, ap);
335
0
  normal_io_write_fl(file, line, &buf_payload);
336
0
  strbuf_release(&buf_payload);
337
0
}
338
339
static void fn_timer(const struct tr2_timer_metadata *meta,
340
         const struct tr2_timer *timer,
341
         int is_final_data)
342
0
{
343
0
  const char *event_name = is_final_data ? "timer" : "th_timer";
344
0
  struct strbuf buf_payload = STRBUF_INIT;
345
0
  double t_total = NS_TO_SEC(timer->total_ns);
346
0
  double t_min = NS_TO_SEC(timer->min_ns);
347
0
  double t_max = NS_TO_SEC(timer->max_ns);
348
349
0
  strbuf_addf(&buf_payload, ("%s %s/%s"
350
0
           " intervals:%"PRIu64
351
0
           " total:%8.6f min:%8.6f max:%8.6f"),
352
0
        event_name, meta->category, meta->name,
353
0
        timer->interval_count,
354
0
        t_total, t_min, t_max);
355
356
0
  normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
357
0
  strbuf_release(&buf_payload);
358
0
}
359
360
static void fn_counter(const struct tr2_counter_metadata *meta,
361
           const struct tr2_counter *counter,
362
           int is_final_data)
363
0
{
364
0
  const char *event_name = is_final_data ? "counter" : "th_counter";
365
0
  struct strbuf buf_payload = STRBUF_INIT;
366
367
0
  strbuf_addf(&buf_payload, "%s %s/%s value:%"PRIu64,
368
0
        event_name, meta->category, meta->name,
369
0
        counter->value);
370
371
0
  normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
372
0
  strbuf_release(&buf_payload);
373
0
}
374
375
struct tr2_tgt tr2_tgt_normal = {
376
  .pdst = &tr2dst_normal,
377
378
  .pfn_init = fn_init,
379
  .pfn_term = fn_term,
380
381
  .pfn_version_fl = fn_version_fl,
382
  .pfn_start_fl = fn_start_fl,
383
  .pfn_exit_fl = fn_exit_fl,
384
  .pfn_signal = fn_signal,
385
  .pfn_atexit = fn_atexit,
386
  .pfn_error_va_fl = fn_error_va_fl,
387
  .pfn_command_path_fl = fn_command_path_fl,
388
  .pfn_command_ancestry_fl = fn_command_ancestry_fl,
389
  .pfn_command_name_fl = fn_command_name_fl,
390
  .pfn_command_mode_fl = fn_command_mode_fl,
391
  .pfn_alias_fl = fn_alias_fl,
392
  .pfn_child_start_fl = fn_child_start_fl,
393
  .pfn_child_exit_fl = fn_child_exit_fl,
394
  .pfn_child_ready_fl = fn_child_ready_fl,
395
  .pfn_thread_start_fl = NULL,
396
  .pfn_thread_exit_fl = NULL,
397
  .pfn_exec_fl = fn_exec_fl,
398
  .pfn_exec_result_fl = fn_exec_result_fl,
399
  .pfn_param_fl = fn_param_fl,
400
  .pfn_repo_fl = fn_repo_fl,
401
  .pfn_region_enter_printf_va_fl = NULL,
402
  .pfn_region_leave_printf_va_fl = NULL,
403
  .pfn_data_fl = NULL,
404
  .pfn_data_json_fl = NULL,
405
  .pfn_printf_va_fl = fn_printf_va_fl,
406
  .pfn_timer = fn_timer,
407
  .pfn_counter = fn_counter,
408
};