Coverage Report

Created: 2025-12-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/trace2.c
Line
Count
Source
1
#define DISABLE_SIGN_COMPARE_WARNINGS
2
3
#include "git-compat-util.h"
4
#include "config.h"
5
#include "repository.h"
6
#include "run-command.h"
7
#include "sigchain.h"
8
#include "thread-utils.h"
9
#include "trace.h"
10
#include "trace2.h"
11
#include "trace2/tr2_cfg.h"
12
#include "trace2/tr2_cmd_name.h"
13
#include "trace2/tr2_ctr.h"
14
#include "trace2/tr2_dst.h"
15
#include "trace2/tr2_sid.h"
16
#include "trace2/tr2_sysenv.h"
17
#include "trace2/tr2_tgt.h"
18
#include "trace2/tr2_tls.h"
19
#include "trace2/tr2_tmr.h"
20
21
static int trace2_enabled;
22
static int trace2_redact = 1;
23
24
static int tr2_next_child_id; /* modify under lock */
25
static int tr2_next_exec_id; /* modify under lock */
26
static int tr2_next_repo_id = 1; /* modify under lock. zero is reserved */
27
28
/*
29
 * A table of the builtin TRACE2 targets.  Each of these may be independently
30
 * enabled or disabled.  Each TRACE2 API method will try to write an event to
31
 * *each* of the enabled targets.
32
 */
33
/* clang-format off */
34
static struct tr2_tgt *tr2_tgt_builtins[] =
35
{
36
  &tr2_tgt_normal,
37
  &tr2_tgt_perf,
38
  &tr2_tgt_event,
39
  NULL
40
};
41
/* clang-format on */
42
43
/* clang-format off */
44
#define for_each_builtin(j, tgt_j)      \
45
0
  for (j = 0, tgt_j = tr2_tgt_builtins[j];  \
46
0
       tgt_j;         \
47
0
       j++, tgt_j = tr2_tgt_builtins[j])
48
/* clang-format on */
49
50
/* clang-format off */
51
#define for_each_wanted_builtin(j, tgt_j)            \
52
0
  for_each_builtin(j, tgt_j)                   \
53
0
    if (tr2_dst_trace_want(tgt_j->pdst))
54
/* clang-format on */
55
56
/*
57
 * Force (rather than lazily) initialize any of the requested
58
 * builtin TRACE2 targets at startup (and before we've seen an
59
 * actual TRACE2 event call) so we can see if we need to setup
60
 * private data structures and thread-local storage.
61
 *
62
 * Return the number of builtin targets enabled.
63
 */
64
static int tr2_tgt_want_builtins(void)
65
0
{
66
0
  struct tr2_tgt *tgt_j;
67
0
  int j;
68
0
  int sum = 0;
69
70
0
  for_each_builtin (j, tgt_j)
71
0
    if (tgt_j->pfn_init())
72
0
      sum++;
73
74
0
  return sum;
75
0
}
76
77
/*
78
 * Properly terminate each builtin target.  Give each target
79
 * a chance to write a summary event and/or flush if necessary
80
 * and then close the fd.
81
 */
82
static void tr2_tgt_disable_builtins(void)
83
0
{
84
0
  struct tr2_tgt *tgt_j;
85
0
  int j;
86
87
0
  for_each_builtin (j, tgt_j)
88
0
    tgt_j->pfn_term();
89
0
}
90
91
/*
92
 * The signature of this function must match the pfn_timer
93
 * method in the targets.  (Think of this is an apply operation
94
 * across the set of active targets.)
95
 */
96
static void tr2_tgt_emit_a_timer(const struct tr2_timer_metadata *meta,
97
         const struct tr2_timer *timer,
98
         int is_final_data)
99
0
{
100
0
  struct tr2_tgt *tgt_j;
101
0
  int j;
102
103
0
  for_each_wanted_builtin (j, tgt_j)
104
0
    if (tgt_j->pfn_timer)
105
0
      tgt_j->pfn_timer(meta, timer, is_final_data);
106
0
}
107
108
/*
109
 * The signature of this function must match the pfn_counter
110
 * method in the targets.
111
 */
112
static void tr2_tgt_emit_a_counter(const struct tr2_counter_metadata *meta,
113
           const struct tr2_counter *counter,
114
           int is_final_data)
115
0
{
116
0
  struct tr2_tgt *tgt_j;
117
0
  int j;
118
119
0
  for_each_wanted_builtin (j, tgt_j)
120
0
    if (tgt_j->pfn_counter)
121
0
      tgt_j->pfn_counter(meta, counter, is_final_data);
122
0
}
123
124
static int tr2main_exit_code;
125
126
/*
127
 * Our atexit routine should run after everything has finished.
128
 *
129
 * Note that events generated here might not actually appear if
130
 * we are writing to fd 1 or 2 and our atexit routine runs after
131
 * the pager's atexit routine (since it closes them to shutdown
132
 * the pipes).
133
 */
134
static void tr2main_atexit_handler(void)
135
0
{
136
0
  struct tr2_tgt *tgt_j;
137
0
  int j;
138
0
  uint64_t us_now;
139
0
  uint64_t us_elapsed_absolute;
140
141
0
  us_now = getnanotime() / 1000;
142
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
143
144
  /*
145
   * Clear any unbalanced regions so that our atexit message
146
   * does not appear nested.  This improves the appearance of
147
   * the trace output if someone calls die(), for example.
148
   */
149
0
  tr2tls_pop_unwind_self();
150
151
  /*
152
   * Some timers want per-thread details.  If the main thread
153
   * used one of those timers, emit the details now (before
154
   * we emit the aggregate timer values).
155
   *
156
   * Likewise for counters.
157
   */
158
0
  tr2_emit_per_thread_timers(tr2_tgt_emit_a_timer);
159
0
  tr2_emit_per_thread_counters(tr2_tgt_emit_a_counter);
160
161
  /*
162
   * Add stopwatch timer and counter data for the main thread to
163
   * the final totals.  And then emit the final values.
164
   *
165
   * Technically, we shouldn't need to hold the lock to update
166
   * and output the final_timer_block and final_counter_block
167
   * (since all other threads should be dead by now), but it
168
   * doesn't hurt anything.
169
   */
170
0
  tr2tls_lock();
171
0
  tr2_update_final_timers();
172
0
  tr2_update_final_counters();
173
0
  tr2_emit_final_timers(tr2_tgt_emit_a_timer);
174
0
  tr2_emit_final_counters(tr2_tgt_emit_a_counter);
175
0
  tr2tls_unlock();
176
177
0
  for_each_wanted_builtin (j, tgt_j)
178
0
    if (tgt_j->pfn_atexit)
179
0
      tgt_j->pfn_atexit(us_elapsed_absolute,
180
0
            tr2main_exit_code);
181
182
0
  tr2_tgt_disable_builtins();
183
184
0
  tr2tls_release();
185
0
  tr2_sid_release();
186
0
  tr2_cmd_name_release();
187
0
  tr2_cfg_free_patterns();
188
0
  tr2_cfg_free_env_vars();
189
0
  tr2_sysenv_release();
190
191
0
  trace2_enabled = 0;
192
0
}
193
194
static void tr2main_signal_handler(int signo)
195
0
{
196
0
  struct tr2_tgt *tgt_j;
197
0
  int j;
198
0
  uint64_t us_now;
199
0
  uint64_t us_elapsed_absolute;
200
201
0
  us_now = getnanotime() / 1000;
202
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
203
204
0
  for_each_wanted_builtin (j, tgt_j)
205
0
    if (tgt_j->pfn_signal)
206
0
      tgt_j->pfn_signal(us_elapsed_absolute, signo);
207
208
0
  sigchain_pop(signo);
209
0
  raise(signo);
210
0
}
211
212
void trace2_initialize_clock(void)
213
0
{
214
0
  tr2tls_start_process_clock();
215
0
}
216
217
void trace2_initialize_fl(const char *file, int line)
218
0
{
219
0
  struct tr2_tgt *tgt_j;
220
0
  int j;
221
222
0
  if (trace2_enabled)
223
0
    return;
224
225
0
  tr2_sysenv_load();
226
227
0
  if (!tr2_tgt_want_builtins())
228
0
    return;
229
0
  trace2_enabled = 1;
230
0
  if (!git_env_bool("GIT_TRACE2_REDACT", 1))
231
0
    trace2_redact = 0;
232
233
0
  tr2_sid_get();
234
235
0
  atexit(tr2main_atexit_handler);
236
0
  sigchain_push(SIGPIPE, tr2main_signal_handler);
237
0
  tr2tls_init();
238
239
  /*
240
   * Emit 'version' message on each active builtin target.
241
   */
242
0
  for_each_wanted_builtin (j, tgt_j)
243
0
    if (tgt_j->pfn_version_fl)
244
0
      tgt_j->pfn_version_fl(file, line);
245
0
}
246
247
int trace2_is_enabled(void)
248
0
{
249
0
  return trace2_enabled;
250
0
}
251
252
/*
253
 * Redacts an argument, i.e. ensures that no password in
254
 * https://user:password@host/-style URLs is logged.
255
 *
256
 * Returns the original if nothing needed to be redacted.
257
 * Returns a pointer that needs to be `free()`d otherwise.
258
 */
259
static const char *redact_arg(const char *arg)
260
0
{
261
0
  const char *p, *colon;
262
0
  size_t at;
263
264
0
  if (!trace2_redact ||
265
0
      (!skip_prefix(arg, "https://", &p) &&
266
0
       !skip_prefix(arg, "http://", &p)))
267
0
    return arg;
268
269
0
  at = strcspn(p, "@/");
270
0
  if (p[at] != '@')
271
0
    return arg;
272
273
0
  colon = memchr(p, ':', at);
274
0
  if (!colon)
275
0
    return arg;
276
277
0
  return xstrfmt("%.*s:<REDACTED>%s", (int)(colon - arg), arg, p + at);
278
0
}
279
280
/*
281
 * Redacts arguments in an argument list.
282
 *
283
 * Returns the original if nothing needed to be redacted.
284
 * Otherwise, returns a new array that needs to be released
285
 * via `free_redacted_argv()`.
286
 */
287
static const char **redact_argv(const char **argv)
288
0
{
289
0
  int i, j;
290
0
  const char *redacted = NULL;
291
0
  const char **ret;
292
293
0
  if (!trace2_redact)
294
0
    return argv;
295
296
0
  for (i = 0; argv[i]; i++)
297
0
    if ((redacted = redact_arg(argv[i])) != argv[i])
298
0
      break;
299
300
0
  if (!argv[i])
301
0
    return argv;
302
303
0
  for (j = 0; argv[j]; j++)
304
0
    ; /* keep counting */
305
306
0
  ALLOC_ARRAY(ret, j + 1);
307
0
  ret[j] = NULL;
308
309
0
  for (j = 0; j < i; j++)
310
0
    ret[j] = argv[j];
311
0
  ret[i] = redacted;
312
0
  for (++i; argv[i]; i++) {
313
0
    redacted = redact_arg(argv[i]);
314
0
    ret[i] = redacted ? redacted : argv[i];
315
0
  }
316
317
0
  return ret;
318
0
}
319
320
static void free_redacted_argv(const char **redacted, const char **argv)
321
0
{
322
0
  int i;
323
324
0
  if (redacted != argv) {
325
0
    for (i = 0; argv[i]; i++)
326
0
      if (redacted[i] != argv[i])
327
0
        free((void *)redacted[i]);
328
0
    free((void *)redacted);
329
0
  }
330
0
}
331
332
void trace2_cmd_start_fl(const char *file, int line, const char **argv)
333
0
{
334
0
  struct tr2_tgt *tgt_j;
335
0
  int j;
336
0
  uint64_t us_now;
337
0
  uint64_t us_elapsed_absolute;
338
0
  const char **redacted;
339
340
0
  if (!trace2_enabled)
341
0
    return;
342
343
0
  us_now = getnanotime() / 1000;
344
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
345
346
0
  redacted = redact_argv(argv);
347
348
0
  for_each_wanted_builtin (j, tgt_j)
349
0
    if (tgt_j->pfn_start_fl)
350
0
      tgt_j->pfn_start_fl(file, line, us_elapsed_absolute,
351
0
              redacted);
352
353
0
  free_redacted_argv(redacted, argv);
354
0
}
355
356
void trace2_cmd_exit_fl(const char *file, int line, int code)
357
0
{
358
0
  struct tr2_tgt *tgt_j;
359
0
  int j;
360
0
  uint64_t us_now;
361
0
  uint64_t us_elapsed_absolute;
362
363
0
  if (!trace2_enabled)
364
0
    return;
365
366
0
  trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT);
367
368
0
  tr2main_exit_code = code;
369
370
0
  us_now = getnanotime() / 1000;
371
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
372
373
0
  for_each_wanted_builtin (j, tgt_j)
374
0
    if (tgt_j->pfn_exit_fl)
375
0
      tgt_j->pfn_exit_fl(file, line, us_elapsed_absolute,
376
0
             code);
377
0
}
378
379
void trace2_cmd_error_va_fl(const char *file, int line, const char *fmt,
380
          va_list ap)
381
0
{
382
0
  struct tr2_tgt *tgt_j;
383
0
  int j;
384
385
0
  if (!trace2_enabled)
386
0
    return;
387
388
  /*
389
   * We expect each target function to treat 'ap' as constant
390
   * and use va_copy (because an 'ap' can only be walked once).
391
   */
392
0
  for_each_wanted_builtin (j, tgt_j)
393
0
    if (tgt_j->pfn_error_va_fl)
394
0
      tgt_j->pfn_error_va_fl(file, line, fmt, ap);
395
0
}
396
397
void trace2_cmd_path_fl(const char *file, int line, const char *pathname)
398
0
{
399
0
  struct tr2_tgt *tgt_j;
400
0
  int j;
401
402
0
  if (!trace2_enabled)
403
0
    return;
404
405
0
  for_each_wanted_builtin (j, tgt_j)
406
0
    if (tgt_j->pfn_command_path_fl)
407
0
      tgt_j->pfn_command_path_fl(file, line, pathname);
408
0
}
409
410
void trace2_cmd_ancestry_fl(const char *file, int line, const char **parent_names)
411
0
{
412
0
  struct tr2_tgt *tgt_j;
413
0
  int j;
414
415
0
  if (!trace2_enabled)
416
0
    return;
417
418
0
  for_each_wanted_builtin (j, tgt_j)
419
0
    if (tgt_j->pfn_command_ancestry_fl)
420
0
      tgt_j->pfn_command_ancestry_fl(file, line, parent_names);
421
0
}
422
423
void trace2_cmd_name_fl(const char *file, int line, const char *name)
424
0
{
425
0
  struct tr2_tgt *tgt_j;
426
0
  const char *hierarchy;
427
0
  int j;
428
429
0
  if (!trace2_enabled)
430
0
    return;
431
432
0
  tr2_cmd_name_append_hierarchy(name);
433
0
  hierarchy = tr2_cmd_name_get_hierarchy();
434
435
0
  for_each_wanted_builtin (j, tgt_j)
436
0
    if (tgt_j->pfn_command_name_fl)
437
0
      tgt_j->pfn_command_name_fl(file, line, name, hierarchy);
438
439
0
  trace2_cmd_list_config();
440
0
  trace2_cmd_list_env_vars();
441
0
}
442
443
void trace2_cmd_mode_fl(const char *file, int line, const char *mode)
444
0
{
445
0
  struct tr2_tgt *tgt_j;
446
0
  int j;
447
448
0
  if (!trace2_enabled)
449
0
    return;
450
451
0
  for_each_wanted_builtin (j, tgt_j)
452
0
    if (tgt_j->pfn_command_mode_fl)
453
0
      tgt_j->pfn_command_mode_fl(file, line, mode);
454
0
}
455
456
void trace2_cmd_alias_fl(const char *file, int line, const char *alias,
457
       const char **argv)
458
0
{
459
0
  struct tr2_tgt *tgt_j;
460
0
  int j;
461
462
0
  if (!trace2_enabled)
463
0
    return;
464
465
0
  for_each_wanted_builtin (j, tgt_j)
466
0
    if (tgt_j->pfn_alias_fl)
467
0
      tgt_j->pfn_alias_fl(file, line, alias, argv);
468
0
}
469
470
void trace2_cmd_list_config_fl(const char *file, int line)
471
0
{
472
0
  static int emitted = 0;
473
474
0
  if (!trace2_enabled)
475
0
    return;
476
477
0
  if (emitted)
478
0
    return;
479
0
  emitted = 1;
480
481
0
  tr2_cfg_list_config_fl(file, line);
482
0
}
483
484
void trace2_cmd_list_env_vars_fl(const char *file, int line)
485
0
{
486
0
  static int emitted = 0;
487
488
0
  if (!trace2_enabled)
489
0
    return;
490
491
0
  if (emitted)
492
0
    return;
493
0
  emitted = 1;
494
495
0
  tr2_list_env_vars_fl(file, line);
496
0
}
497
498
void trace2_cmd_set_config_fl(const char *file, int line, const char *key,
499
            const char *value)
500
0
{
501
0
  if (!trace2_enabled)
502
0
    return;
503
504
0
  tr2_cfg_set_fl(file, line, key, value);
505
0
}
506
507
void trace2_child_start_fl(const char *file, int line,
508
         struct child_process *cmd)
509
0
{
510
0
  struct tr2_tgt *tgt_j;
511
0
  int j;
512
0
  uint64_t us_now;
513
0
  uint64_t us_elapsed_absolute;
514
0
  const char **orig_argv = cmd->args.v;
515
516
0
  if (!trace2_enabled)
517
0
    return;
518
519
0
  us_now = getnanotime() / 1000;
520
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
521
522
0
  cmd->trace2_child_id = tr2tls_locked_increment(&tr2_next_child_id);
523
0
  cmd->trace2_child_us_start = us_now;
524
525
  /*
526
   * The `pfn_child_start_fl` API takes a `struct child_process`
527
   * rather than a simple `argv` for the child because some
528
   * targets make use of the additional context bits/values. So
529
   * temporarily replace the original argv (inside the `strvec`)
530
   * with a possibly redacted version.
531
   */
532
0
  cmd->args.v = redact_argv(orig_argv);
533
534
0
  for_each_wanted_builtin (j, tgt_j)
535
0
    if (tgt_j->pfn_child_start_fl)
536
0
      tgt_j->pfn_child_start_fl(file, line,
537
0
              us_elapsed_absolute, cmd);
538
539
0
  if (cmd->args.v != orig_argv) {
540
0
    free_redacted_argv(cmd->args.v, orig_argv);
541
0
    cmd->args.v = orig_argv;
542
0
  }
543
0
}
544
545
void trace2_child_exit_fl(const char *file, int line, struct child_process *cmd,
546
        int child_exit_code)
547
0
{
548
0
  struct tr2_tgt *tgt_j;
549
0
  int j;
550
0
  uint64_t us_now;
551
0
  uint64_t us_elapsed_absolute;
552
0
  uint64_t us_elapsed_child;
553
554
0
  if (!trace2_enabled)
555
0
    return;
556
557
0
  us_now = getnanotime() / 1000;
558
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
559
560
0
  if (cmd->trace2_child_us_start)
561
0
    us_elapsed_child = us_now - cmd->trace2_child_us_start;
562
0
  else
563
0
    us_elapsed_child = 0;
564
565
0
  for_each_wanted_builtin (j, tgt_j)
566
0
    if (tgt_j->pfn_child_exit_fl)
567
0
      tgt_j->pfn_child_exit_fl(file, line,
568
0
             us_elapsed_absolute,
569
0
             cmd->trace2_child_id, cmd->pid,
570
0
             child_exit_code,
571
0
             us_elapsed_child);
572
0
}
573
574
void trace2_child_ready_fl(const char *file, int line,
575
         struct child_process *cmd,
576
         const char *ready)
577
0
{
578
0
  struct tr2_tgt *tgt_j;
579
0
  int j;
580
0
  uint64_t us_now;
581
0
  uint64_t us_elapsed_absolute;
582
0
  uint64_t us_elapsed_child;
583
584
0
  if (!trace2_enabled)
585
0
    return;
586
587
0
  us_now = getnanotime() / 1000;
588
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
589
590
0
  if (cmd->trace2_child_us_start)
591
0
    us_elapsed_child = us_now - cmd->trace2_child_us_start;
592
0
  else
593
0
    us_elapsed_child = 0;
594
595
0
  for_each_wanted_builtin (j, tgt_j)
596
0
    if (tgt_j->pfn_child_ready_fl)
597
0
      tgt_j->pfn_child_ready_fl(file, line,
598
0
              us_elapsed_absolute,
599
0
              cmd->trace2_child_id,
600
0
              cmd->pid,
601
0
              ready,
602
0
              us_elapsed_child);
603
0
}
604
605
int trace2_exec_fl(const char *file, int line, const char *exe,
606
       const char **argv)
607
0
{
608
0
  struct tr2_tgt *tgt_j;
609
0
  int j;
610
0
  int exec_id;
611
0
  uint64_t us_now;
612
0
  uint64_t us_elapsed_absolute;
613
0
  const char **redacted;
614
615
0
  if (!trace2_enabled)
616
0
    return -1;
617
618
0
  us_now = getnanotime() / 1000;
619
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
620
621
0
  exec_id = tr2tls_locked_increment(&tr2_next_exec_id);
622
623
0
  redacted = redact_argv(argv);
624
625
0
  for_each_wanted_builtin (j, tgt_j)
626
0
    if (tgt_j->pfn_exec_fl)
627
0
      tgt_j->pfn_exec_fl(file, line, us_elapsed_absolute,
628
0
             exec_id, exe, redacted);
629
630
0
  free_redacted_argv(redacted, argv);
631
632
0
  return exec_id;
633
0
}
634
635
void trace2_exec_result_fl(const char *file, int line, int exec_id, int code)
636
0
{
637
0
  struct tr2_tgt *tgt_j;
638
0
  int j;
639
0
  uint64_t us_now;
640
0
  uint64_t us_elapsed_absolute;
641
642
0
  if (!trace2_enabled)
643
0
    return;
644
645
0
  us_now = getnanotime() / 1000;
646
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
647
648
0
  for_each_wanted_builtin (j, tgt_j)
649
0
    if (tgt_j->pfn_exec_result_fl)
650
0
      tgt_j->pfn_exec_result_fl(
651
0
        file, line, us_elapsed_absolute, exec_id, code);
652
0
}
653
654
void trace2_thread_start_fl(const char *file, int line, const char *thread_base_name)
655
0
{
656
0
  struct tr2_tgt *tgt_j;
657
0
  int j;
658
0
  uint64_t us_now;
659
0
  uint64_t us_elapsed_absolute;
660
661
0
  if (!trace2_enabled)
662
0
    return;
663
664
0
  if (tr2tls_is_main_thread()) {
665
    /*
666
     * We should only be called from the new thread's thread-proc,
667
     * so this is technically a bug.  But in those cases where the
668
     * main thread also runs the thread-proc function (or when we
669
     * are built with threading disabled), we need to allow it.
670
     *
671
     * Convert this call to a region-enter so the nesting looks
672
     * correct.
673
     */
674
0
    trace2_region_enter_printf_fl(file, line, NULL, NULL, NULL,
675
0
                "thread-proc on main: %s",
676
0
                thread_base_name);
677
0
    return;
678
0
  }
679
680
0
  us_now = getnanotime() / 1000;
681
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
682
683
0
  tr2tls_create_self(thread_base_name, us_now);
684
685
0
  for_each_wanted_builtin (j, tgt_j)
686
0
    if (tgt_j->pfn_thread_start_fl)
687
0
      tgt_j->pfn_thread_start_fl(file, line,
688
0
               us_elapsed_absolute);
689
0
}
690
691
void trace2_thread_exit_fl(const char *file, int line)
692
0
{
693
0
  struct tr2_tgt *tgt_j;
694
0
  int j;
695
0
  uint64_t us_now;
696
0
  uint64_t us_elapsed_absolute;
697
0
  uint64_t us_elapsed_thread;
698
699
0
  if (!trace2_enabled)
700
0
    return;
701
702
0
  if (tr2tls_is_main_thread()) {
703
    /*
704
     * We should only be called from the exiting thread's
705
     * thread-proc, so this is technically a bug.  But in
706
     * those cases where the main thread also runs the
707
     * thread-proc function (or when we are built with
708
     * threading disabled), we need to allow it.
709
     *
710
     * Convert this call to a region-leave so the nesting
711
     * looks correct.
712
     */
713
0
    trace2_region_leave_printf_fl(file, line, NULL, NULL, NULL,
714
0
                "thread-proc on main");
715
0
    return;
716
0
  }
717
718
0
  us_now = getnanotime() / 1000;
719
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
720
721
  /*
722
   * Clear any unbalanced regions and then get the relative time
723
   * for the outer-most region (which we pushed when the thread
724
   * started).  This gives us the run time of the thread.
725
   */
726
0
  tr2tls_pop_unwind_self();
727
0
  us_elapsed_thread = tr2tls_region_elasped_self(us_now);
728
729
  /*
730
   * Some timers want per-thread details.  If this thread used
731
   * one of those timers, emit the details now.
732
   *
733
   * Likewise for counters.
734
   */
735
0
  tr2_emit_per_thread_timers(tr2_tgt_emit_a_timer);
736
0
  tr2_emit_per_thread_counters(tr2_tgt_emit_a_counter);
737
738
  /*
739
   * Add stopwatch timer and counter data from the current
740
   * (non-main) thread to the final totals.  (We'll accumulate
741
   * data for the main thread later during "atexit".)
742
   */
743
0
  tr2tls_lock();
744
0
  tr2_update_final_timers();
745
0
  tr2_update_final_counters();
746
0
  tr2tls_unlock();
747
748
0
  for_each_wanted_builtin (j, tgt_j)
749
0
    if (tgt_j->pfn_thread_exit_fl)
750
0
      tgt_j->pfn_thread_exit_fl(file, line,
751
0
              us_elapsed_absolute,
752
0
              us_elapsed_thread);
753
754
0
  tr2tls_unset_self();
755
0
}
756
757
void trace2_def_param_fl(const char *file, int line, const char *param,
758
       const char *value, const struct key_value_info *kvi)
759
0
{
760
0
  struct tr2_tgt *tgt_j;
761
0
  int j;
762
0
  const char *redacted;
763
764
0
  if (!trace2_enabled)
765
0
    return;
766
767
0
  redacted = value ? redact_arg(value) : NULL;
768
769
0
  for_each_wanted_builtin (j, tgt_j)
770
0
    if (tgt_j->pfn_param_fl)
771
0
      tgt_j->pfn_param_fl(file, line, param, redacted, kvi);
772
773
0
  if (redacted != value)
774
0
    free((void *)redacted);
775
0
}
776
777
void trace2_def_repo_fl(const char *file, int line, struct repository *repo)
778
0
{
779
0
  struct tr2_tgt *tgt_j;
780
0
  int j;
781
782
0
  if (!trace2_enabled)
783
0
    return;
784
785
0
  if (repo->trace2_repo_id)
786
0
    return;
787
788
0
  repo->trace2_repo_id = tr2tls_locked_increment(&tr2_next_repo_id);
789
790
0
  for_each_wanted_builtin (j, tgt_j)
791
0
    if (tgt_j->pfn_repo_fl)
792
0
      tgt_j->pfn_repo_fl(file, line, repo);
793
0
}
794
795
void trace2_region_enter_printf_va_fl(const char *file, int line,
796
              const char *category, const char *label,
797
              const struct repository *repo,
798
              const char *fmt, va_list ap)
799
0
{
800
0
  struct tr2_tgt *tgt_j;
801
0
  int j;
802
0
  uint64_t us_now;
803
0
  uint64_t us_elapsed_absolute;
804
805
0
  if (!trace2_enabled)
806
0
    return;
807
808
0
  us_now = getnanotime() / 1000;
809
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
810
811
  /*
812
   * Print the region-enter message at the current nesting
813
   * (indentation) level and then push a new level.
814
   *
815
   * We expect each target function to treat 'ap' as constant
816
   * and use va_copy.
817
   */
818
0
  for_each_wanted_builtin (j, tgt_j)
819
0
    if (tgt_j->pfn_region_enter_printf_va_fl)
820
0
      tgt_j->pfn_region_enter_printf_va_fl(
821
0
        file, line, us_elapsed_absolute, category,
822
0
        label, repo, fmt, ap);
823
824
0
  tr2tls_push_self(us_now);
825
0
}
826
827
void trace2_region_enter_fl(const char *file, int line, const char *category,
828
          const char *label, const struct repository *repo, ...)
829
0
{
830
0
  va_list ap;
831
0
  va_start(ap, repo);
832
0
  trace2_region_enter_printf_va_fl(file, line, category, label, repo,
833
0
           NULL, ap);
834
0
  va_end(ap);
835
836
0
}
837
838
void trace2_region_enter_printf_fl(const char *file, int line,
839
           const char *category, const char *label,
840
           const struct repository *repo,
841
           const char *fmt, ...)
842
0
{
843
0
  va_list ap;
844
845
0
  va_start(ap, fmt);
846
0
  trace2_region_enter_printf_va_fl(file, line, category, label, repo, fmt,
847
0
           ap);
848
0
  va_end(ap);
849
0
}
850
851
void trace2_region_leave_printf_va_fl(const char *file, int line,
852
              const char *category, const char *label,
853
              const struct repository *repo,
854
              const char *fmt, va_list ap)
855
0
{
856
0
  struct tr2_tgt *tgt_j;
857
0
  int j;
858
0
  uint64_t us_now;
859
0
  uint64_t us_elapsed_absolute;
860
0
  uint64_t us_elapsed_region;
861
862
0
  if (!trace2_enabled)
863
0
    return;
864
865
0
  us_now = getnanotime() / 1000;
866
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
867
868
  /*
869
   * Get the elapsed time in the current region before we
870
   * pop it off the stack.  Pop the stack.  And then print
871
   * the perf message at the new (shallower) level so that
872
   * it lines up with the corresponding push/enter.
873
   */
874
0
  us_elapsed_region = tr2tls_region_elasped_self(us_now);
875
876
0
  tr2tls_pop_self();
877
878
  /*
879
   * We expect each target function to treat 'ap' as constant
880
   * and use va_copy.
881
   */
882
0
  for_each_wanted_builtin (j, tgt_j)
883
0
    if (tgt_j->pfn_region_leave_printf_va_fl)
884
0
      tgt_j->pfn_region_leave_printf_va_fl(
885
0
        file, line, us_elapsed_absolute,
886
0
        us_elapsed_region, category, label, repo, fmt,
887
0
        ap);
888
0
}
889
890
void trace2_region_leave_fl(const char *file, int line, const char *category,
891
          const char *label, const struct repository *repo, ...)
892
0
{
893
0
  va_list ap;
894
0
  va_start(ap, repo);
895
0
  trace2_region_leave_printf_va_fl(file, line, category, label, repo,
896
0
           NULL, ap);
897
0
  va_end(ap);
898
0
}
899
900
void trace2_region_leave_printf_fl(const char *file, int line,
901
           const char *category, const char *label,
902
           const struct repository *repo,
903
           const char *fmt, ...)
904
0
{
905
0
  va_list ap;
906
907
0
  va_start(ap, fmt);
908
0
  trace2_region_leave_printf_va_fl(file, line, category, label, repo, fmt,
909
0
           ap);
910
0
  va_end(ap);
911
0
}
912
913
void trace2_data_string_fl(const char *file, int line, const char *category,
914
         const struct repository *repo, const char *key,
915
         const char *value)
916
0
{
917
0
  struct tr2_tgt *tgt_j;
918
0
  int j;
919
0
  uint64_t us_now;
920
0
  uint64_t us_elapsed_absolute;
921
0
  uint64_t us_elapsed_region;
922
923
0
  if (!trace2_enabled)
924
0
    return;
925
926
0
  us_now = getnanotime() / 1000;
927
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
928
0
  us_elapsed_region = tr2tls_region_elasped_self(us_now);
929
930
0
  for_each_wanted_builtin (j, tgt_j)
931
0
    if (tgt_j->pfn_data_fl)
932
0
      tgt_j->pfn_data_fl(file, line, us_elapsed_absolute,
933
0
             us_elapsed_region, category, repo,
934
0
             key, value);
935
0
}
936
937
void trace2_data_intmax_fl(const char *file, int line, const char *category,
938
         const struct repository *repo, const char *key,
939
         intmax_t value)
940
0
{
941
0
  struct strbuf buf_string = STRBUF_INIT;
942
943
0
  if (!trace2_enabled)
944
0
    return;
945
946
0
  strbuf_addf(&buf_string, "%" PRIdMAX, value);
947
0
  trace2_data_string_fl(file, line, category, repo, key, buf_string.buf);
948
0
  strbuf_release(&buf_string);
949
0
}
950
951
void trace2_data_json_fl(const char *file, int line, const char *category,
952
       const struct repository *repo, const char *key,
953
       const struct json_writer *value)
954
0
{
955
0
  struct tr2_tgt *tgt_j;
956
0
  int j;
957
0
  uint64_t us_now;
958
0
  uint64_t us_elapsed_absolute;
959
0
  uint64_t us_elapsed_region;
960
961
0
  if (!trace2_enabled)
962
0
    return;
963
964
0
  us_now = getnanotime() / 1000;
965
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
966
0
  us_elapsed_region = tr2tls_region_elasped_self(us_now);
967
968
0
  for_each_wanted_builtin (j, tgt_j)
969
0
    if (tgt_j->pfn_data_json_fl)
970
0
      tgt_j->pfn_data_json_fl(file, line, us_elapsed_absolute,
971
0
            us_elapsed_region, category,
972
0
            repo, key, value);
973
0
}
974
975
void trace2_printf_va_fl(const char *file, int line, const char *fmt,
976
       va_list ap)
977
0
{
978
0
  struct tr2_tgt *tgt_j;
979
0
  int j;
980
0
  uint64_t us_now;
981
0
  uint64_t us_elapsed_absolute;
982
983
0
  if (!trace2_enabled)
984
0
    return;
985
986
0
  us_now = getnanotime() / 1000;
987
0
  us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
988
989
  /*
990
   * We expect each target function to treat 'ap' as constant
991
   * and use va_copy.
992
   */
993
0
  for_each_wanted_builtin (j, tgt_j)
994
0
    if (tgt_j->pfn_printf_va_fl)
995
0
      tgt_j->pfn_printf_va_fl(file, line, us_elapsed_absolute,
996
0
            fmt, ap);
997
0
}
998
999
void trace2_printf_fl(const char *file, int line, const char *fmt, ...)
1000
0
{
1001
0
  va_list ap;
1002
1003
0
  va_start(ap, fmt);
1004
0
  trace2_printf_va_fl(file, line, fmt, ap);
1005
0
  va_end(ap);
1006
0
}
1007
1008
void trace2_timer_start(enum trace2_timer_id tid)
1009
0
{
1010
0
  if (!trace2_enabled)
1011
0
    return;
1012
1013
0
  if (tid < 0 || tid >= TRACE2_NUMBER_OF_TIMERS)
1014
0
    BUG("trace2_timer_start: invalid timer id: %d", tid);
1015
1016
0
  tr2_start_timer(tid);
1017
0
}
1018
1019
void trace2_timer_stop(enum trace2_timer_id tid)
1020
0
{
1021
0
  if (!trace2_enabled)
1022
0
    return;
1023
1024
0
  if (tid < 0 || tid >= TRACE2_NUMBER_OF_TIMERS)
1025
0
    BUG("trace2_timer_stop: invalid timer id: %d", tid);
1026
1027
0
  tr2_stop_timer(tid);
1028
0
}
1029
1030
void trace2_counter_add(enum trace2_counter_id cid, uint64_t value)
1031
0
{
1032
0
  if (!trace2_enabled)
1033
0
    return;
1034
1035
0
  if (cid < 0 || cid >= TRACE2_NUMBER_OF_COUNTERS)
1036
0
    BUG("trace2_counter_add: invalid counter id: %d", cid);
1037
1038
0
  tr2_counter_increment(cid, value);
1039
0
}
1040
1041
const char *trace2_session_id(void)
1042
0
{
1043
0
  return tr2_sid_get();
1044
0
}