Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
3
4 #include "test-tool.h"
5 #include "strvec.h"
6 #include "run-command.h"
7 #include "exec-cmd.h"
8 #include "config.h"
9 #include "repository.h"
10 #include "trace2.h"
11
12 typedef int(fn_unit_test)(int argc, const char **argv);
13
14 struct unit_test {
15 fn_unit_test *ut_fn;
16 const char *ut_name;
17 const char *ut_usage;
18 };
19
20 #define MyOk 0
21 #define MyError 1
22
23 static int get_i(int *p_value, const char *data)
24 {
25 char *endptr;
26
27 if (!data || !*data)
28 return MyError;
29
30 errno = 0;
31 *p_value = strtol(data, &endptr, 10);
32 if (*endptr || errno == ERANGE)
33 return MyError;
34
35 return MyOk;
36 }
37
38 /*
39 * Cause process to exit with the requested value via "return".
40 *
41 * Rely on test-tool.c:cmd_main() to call trace2_cmd_exit()
42 * with our result.
43 *
44 * Test harness can confirm:
45 * [] the process-exit value.
46 * [] the "code" field in the "exit" trace2 event.
47 * [] the "code" field in the "atexit" trace2 event.
48 * [] the "name" field in the "cmd_name" trace2 event.
49 * [] "def_param" events for all of the "interesting" pre-defined
50 * config settings.
51 */
52 static int ut_001return(int argc UNUSED, const char **argv)
53 {
54 int rc;
55
56 if (get_i(&rc, argv[0]))
57 die("expect <exit_code>");
58
59 return rc;
60 }
61
62 /*
63 * Cause the process to exit with the requested value via "exit()".
64 *
65 * Test harness can confirm:
66 * [] the "code" field in the "exit" trace2 event.
67 * [] the "code" field in the "atexit" trace2 event.
68 * [] the "name" field in the "cmd_name" trace2 event.
69 * [] "def_param" events for all of the "interesting" pre-defined
70 * config settings.
71 */
72 static int ut_002exit(int argc UNUSED, const char **argv)
73 {
74 int rc;
75
76 if (get_i(&rc, argv[0]))
77 die("expect <exit_code>");
78
79 exit(rc);
80 }
81
82 /*
83 * Send an "error" event with each value in argv. Normally, git only issues
84 * a single "error" event immediately before issuing an "exit" event (such
85 * as in die() or BUG()), but multiple "error" events are allowed.
86 *
87 * Test harness can confirm:
88 * [] a trace2 "error" event for each value in argv.
89 * [] the "name" field in the "cmd_name" trace2 event.
90 * [] (optional) the file:line in the "exit" event refers to this function.
91 */
92 static int ut_003error(int argc, const char **argv)
93 {
94 int k;
95
96 if (!argv[0] || !*argv[0])
97 die("expect <error_message>");
98
99 for (k = 0; k < argc; k++)
100 error("%s", argv[k]);
101
102 return 0;
103 }
104
105 /*
106 * Run a child process and wait for it to finish and exit with its return code.
107 * test-tool trace2 004child [<child-command-line>]
108 *
109 * For example:
110 * test-tool trace2 004child git version
111 * test-tool trace2 004child test-tool trace2 001return 0
112 * test-tool trace2 004child test-tool trace2 004child test-tool trace2 004child
113 * test-tool trace2 004child git -c alias.xyz=version xyz
114 *
115 * Test harness can confirm:
116 * [] the "name" field in the "cmd_name" trace2 event.
117 * [] that the outer process has a single component SID (or depth "d0" in
118 * the PERF stream).
119 * [] that "child_start" and "child_exit" events are generated for the child.
120 * [] if the child process is an instrumented executable:
121 * [] that "version", "start", ..., "exit", and "atexit" events are
122 * generated by the child process.
123 * [] that the child process events have a multiple component SID (or
124 * depth "dN+1" in the PERF stream).
125 * [] that the child exit code is propagated to the parent process "exit"
126 * and "atexit" events..
127 * [] (optional) that the "t_abs" field in the child process "atexit" event
128 * is less than the "t_rel" field in the "child_exit" event of the parent
129 * process.
130 * [] if the child process is like the alias example above,
131 * [] (optional) the child process attempts to run "git-xyx" as a dashed
132 * command.
133 * [] the child process emits an "alias" event with "xyz" => "version"
134 * [] the child process runs "git version" as a child process.
135 * [] the child process has a 3 component SID (or depth "d2" in the PERF
136 * stream).
137 */
138 static int ut_004child(int argc, const char **argv)
139 {
140 struct child_process cmd = CHILD_PROCESS_INIT;
141 int result;
142
143 /*
144 * Allow empty <child_command_line> so we can do arbitrarily deep
145 * command nesting and let the last one be null.
146 */
147 if (!argc)
148 return 0;
149
150 strvec_pushv(&cmd.args, argv);
151 result = run_command(&cmd);
152 exit(result);
153 }
154
155 /*
156 * Exec a git command. This may either create a child process (Windows)
157 * or replace the existing process.
158 * test-tool trace2 005exec <git_command_args>
159 *
160 * For example:
161 * test-tool trace2 005exec version
162 *
163 * Test harness can confirm (on Windows):
164 * [] the "name" field in the "cmd_name" trace2 event.
165 * [] that the outer process has a single component SID (or depth "d0" in
166 * the PERF stream).
167 * [] that "exec" and "exec_result" events are generated for the child
168 * process (since the Windows compatibility layer fakes an exec() with
169 * a CreateProcess(), WaitForSingleObject(), and exit()).
170 * [] that the child process has multiple component SID (or depth "dN+1"
171 * in the PERF stream).
172 *
173 * Test harness can confirm (on platforms with a real exec() function):
174 * [] TODO talk about process replacement and how it affects SID.
175 */
176 static int ut_005exec(int argc, const char **argv)
177 {
178 int result;
179
180 if (!argc)
181 return 0;
182
183 result = execv_git_cmd(argv);
184 return result;
185 }
186
187 static int ut_006data(int argc, const char **argv)
188 {
189 const char *usage_error =
190 "expect <cat0> <k0> <v0> [<cat1> <k1> <v1> [...]]";
191
192 if (argc % 3 != 0)
193 die("%s", usage_error);
194
195 while (argc) {
196 if (!argv[0] || !*argv[0] || !argv[1] || !*argv[1] ||
197 !argv[2] || !*argv[2])
198 die("%s", usage_error);
199
200 trace2_data_string(argv[0], the_repository, argv[1], argv[2]);
201 argv += 3;
202 argc -= 3;
203 }
204
205 return 0;
206 }
207
208 static int ut_007BUG(int argc UNUSED, const char **argv UNUSED)
209 {
210 /*
211 * Exercise BUG() to ensure that the message is printed to trace2.
212 */
213 BUG("the bug message");
214 }
215
216 static int ut_008bug(int argc UNUSED, const char **argv UNUSED)
217 {
218 bug("a bug message");
219 bug("another bug message");
220 BUG_if_bug("an explicit BUG_if_bug() following bug() call(s) is nice, but not required");
221 return 0;
222 }
223
224 static int ut_009bug_BUG(int argc UNUSED, const char **argv UNUSED)
225 {
226 bug("a bug message");
227 bug("another bug message");
228 /* The BUG_if_bug(...) isn't here, but we'll spot bug() calls on exit()! */
229 return 0;
230 }
231
232 static int ut_010bug_BUG(int argc UNUSED, const char **argv UNUSED)
233 {
234 bug("a %s message", "bug");
235 BUG("a %s message", "BUG");
236 }
237
238 /*
239 * Single-threaded timer test. Create several intervals using the
240 * TEST1 timer. The test script can verify that an aggregate Trace2
241 * "timer" event is emitted indicating that we started+stopped the
242 * timer the requested number of times.
243 */
244 static int ut_100timer(int argc, const char **argv)
245 {
246 const char *usage_error =
247 "expect <count> <ms_delay>";
248
249 int count = 0;
250 int delay = 0;
251 int k;
252
253 if (argc != 2)
254 die("%s", usage_error);
255 if (get_i(&count, argv[0]))
256 die("%s", usage_error);
257 if (get_i(&delay, argv[1]))
258 die("%s", usage_error);
259
260 for (k = 0; k < count; k++) {
261 trace2_timer_start(TRACE2_TIMER_ID_TEST1);
262 sleep_millisec(delay);
263 trace2_timer_stop(TRACE2_TIMER_ID_TEST1);
264 }
265
266 return 0;
267 }
268
269 struct ut_101_data {
270 int count;
271 int delay;
272 };
273
274 static void *ut_101timer_thread_proc(void *_ut_101_data)
275 {
276 struct ut_101_data *data = _ut_101_data;
277 int k;
278
279 trace2_thread_start("ut_101");
280
281 for (k = 0; k < data->count; k++) {
282 trace2_timer_start(TRACE2_TIMER_ID_TEST2);
283 sleep_millisec(data->delay);
284 trace2_timer_stop(TRACE2_TIMER_ID_TEST2);
285 }
286
287 trace2_thread_exit();
288 return NULL;
289 }
290
291 /*
292 * Multi-threaded timer test. Create several threads that each create
293 * several intervals using the TEST2 timer. The test script can verify
294 * that an individual Trace2 "th_timer" events for each thread and an
295 * aggregate "timer" event are generated.
296 */
297 static int ut_101timer(int argc, const char **argv)
298 {
299 const char *usage_error =
300 "expect <count> <ms_delay> <threads>";
301
302 struct ut_101_data data = { 0, 0 };
303 int nr_threads = 0;
304 int k;
305 pthread_t *pids = NULL;
306
307 if (argc != 3)
308 die("%s", usage_error);
309 if (get_i(&data.count, argv[0]))
310 die("%s", usage_error);
311 if (get_i(&data.delay, argv[1]))
312 die("%s", usage_error);
313 if (get_i(&nr_threads, argv[2]))
314 die("%s", usage_error);
315
316 CALLOC_ARRAY(pids, nr_threads);
317
318 for (k = 0; k < nr_threads; k++) {
319 if (pthread_create(&pids[k], NULL, ut_101timer_thread_proc, &data))
320 die("failed to create thread[%d]", k);
321 }
322
323 for (k = 0; k < nr_threads; k++) {
324 if (pthread_join(pids[k], NULL))
325 die("failed to join thread[%d]", k);
326 }
327
328 free(pids);
329
330 return 0;
331 }
332
333 /*
334 * Single-threaded counter test. Add several values to the TEST1 counter.
335 * The test script can verify that the final sum is reported in the "counter"
336 * event.
337 */
338 static int ut_200counter(int argc, const char **argv)
339 {
340 const char *usage_error =
341 "expect <v1> [<v2> [...]]";
342 int value;
343 int k;
344
345 if (argc < 1)
346 die("%s", usage_error);
347
348 for (k = 0; k < argc; k++) {
349 if (get_i(&value, argv[k]))
350 die("invalid value[%s] -- %s",
351 argv[k], usage_error);
352 trace2_counter_add(TRACE2_COUNTER_ID_TEST1, value);
353 }
354
355 return 0;
356 }
357
358 /*
359 * Multi-threaded counter test. Create seveal threads that each increment
360 * the TEST2 global counter. The test script can verify that an individual
361 * "th_counter" event is generated with a partial sum for each thread and
362 * that a final aggregate "counter" event is generated.
363 */
364
365 struct ut_201_data {
366 int v1;
367 int v2;
368 };
369
370 static void *ut_201counter_thread_proc(void *_ut_201_data)
371 {
372 struct ut_201_data *data = _ut_201_data;
373
374 trace2_thread_start("ut_201");
375
376 trace2_counter_add(TRACE2_COUNTER_ID_TEST2, data->v1);
377 trace2_counter_add(TRACE2_COUNTER_ID_TEST2, data->v2);
378
379 trace2_thread_exit();
380 return NULL;
381 }
382
383 static int ut_201counter(int argc, const char **argv)
384 {
385 const char *usage_error =
386 "expect <v1> <v2> <threads>";
387
388 struct ut_201_data data = { 0, 0 };
389 int nr_threads = 0;
390 int k;
391 pthread_t *pids = NULL;
392
393 if (argc != 3)
394 die("%s", usage_error);
395 if (get_i(&data.v1, argv[0]))
396 die("%s", usage_error);
397 if (get_i(&data.v2, argv[1]))
398 die("%s", usage_error);
399 if (get_i(&nr_threads, argv[2]))
400 die("%s", usage_error);
401
402 CALLOC_ARRAY(pids, nr_threads);
403
404 for (k = 0; k < nr_threads; k++) {
405 if (pthread_create(&pids[k], NULL, ut_201counter_thread_proc, &data))
406 die("failed to create thread[%d]", k);
407 }
408
409 for (k = 0; k < nr_threads; k++) {
410 if (pthread_join(pids[k], NULL))
411 die("failed to join thread[%d]", k);
412 }
413
414 free(pids);
415
416 return 0;
417 }
418
419 static int ut_300redact_start(int argc, const char **argv)
420 {
421 if (!argc)
422 die("expect <argv...>");
423
424 trace2_cmd_start(argv);
425
426 return 0;
427 }
428
429 static int ut_301redact_child_start(int argc, const char **argv)
430 {
431 struct child_process cmd = CHILD_PROCESS_INIT;
432 int k;
433
434 if (!argc)
435 die("expect <argv...>");
436
437 for (k = 0; argv[k]; k++)
438 strvec_push(&cmd.args, argv[k]);
439
440 trace2_child_start(&cmd);
441
442 strvec_clear(&cmd.args);
443
444 return 0;
445 }
446
447 static int ut_302redact_exec(int argc, const char **argv)
448 {
449 if (!argc)
450 die("expect <exe> <argv...>");
451
452 trace2_exec(argv[0], &argv[1]);
453
454 return 0;
455 }
456
457 static int ut_303redact_def_param(int argc, const char **argv)
458 {
459 struct key_value_info kvi = KVI_INIT;
460
461 if (argc < 2)
462 die("expect <key> <value>");
463
464 trace2_def_param(argv[0], argv[1], &kvi);
465
466 return 0;
467 }
468
469 /*
470 * Run a child process with specific trace2 environment settings so that
471 * we can capture its trace2 output (including cmd_ancestry) in isolation.
472 *
473 * test-tool trace2 400ancestry <target> <output_file> [<child_command_line>]
474 *
475 * <target> is one of: normal, perf, event
476 *
477 * For example:
478 * test-tool trace2 400ancestry normal out.normal test-tool trace2 001return 0
479 *
480 * The child process inherits a controlled trace2 environment where only
481 * the specified target is directed to <output_file>. The parent's trace2
482 * environment variables are cleared in the child so that only the child's
483 * events are captured.
484 *
485 * This is used by t0213-trace2-ancestry.sh to test cmd_ancestry events.
486 * The child process will see "test-tool" as its immediate parent in the
487 * process ancestry, giving us a predictable value to verify.
488 */
489 static int ut_400ancestry(int argc, const char **argv)
490 {
491 struct child_process cmd = CHILD_PROCESS_INIT;
492 const char *target;
493 const char *outfile;
494 int result;
495
496 if (argc < 3)
497 die("expect <target> <output_file> <child_command_line>");
498
499 target = argv[0];
500 outfile = argv[1];
501 argv += 2;
502 argc -= 2;
503
504 /* Clear all trace2 environment variables in the child. */
505 strvec_push(&cmd.env, "GIT_TRACE2=");
506 strvec_push(&cmd.env, "GIT_TRACE2_PERF=");
507 strvec_push(&cmd.env, "GIT_TRACE2_EVENT=");
508 strvec_push(&cmd.env, "GIT_TRACE2_BRIEF=1");
509
510 /* Set only the requested target. */
511 if (!strcmp(target, "normal"))
512 strvec_pushf(&cmd.env, "GIT_TRACE2=%s", outfile);
513 else if (!strcmp(target, "perf"))
514 strvec_pushf(&cmd.env, "GIT_TRACE2_PERF=%s", outfile);
515 else if (!strcmp(target, "event"))
516 strvec_pushf(&cmd.env, "GIT_TRACE2_EVENT=%s", outfile);
517 else
518 die("invalid target '%s', expected: normal, perf, event",
519 target);
520
521 strvec_pushv(&cmd.args, argv);
522 result = run_command(&cmd);
523 exit(result);
524 }
525
526 /*
527 * Usage:
528 * test-tool trace2 <ut_name_1> <ut_usage_1>
529 * test-tool trace2 <ut_name_2> <ut_usage_2>
530 * ...
531 */
532 #define USAGE_PREFIX "test-tool trace2"
533
534 /* clang-format off */
535 static struct unit_test ut_table[] = {
536 { ut_001return, "001return", "<exit_code>" },
537 { ut_002exit, "002exit", "<exit_code>" },
538 { ut_003error, "003error", "<error_message>+" },
539 { ut_004child, "004child", "[<child_command_line>]" },
540 { ut_005exec, "005exec", "<git_command_args>" },
541 { ut_006data, "006data", "[<category> <key> <value>]+" },
542 { ut_007BUG, "007bug", "" },
543 { ut_008bug, "008bug", "" },
544 { ut_009bug_BUG, "009bug_BUG","" },
545 { ut_010bug_BUG, "010bug_BUG","" },
546
547 { ut_100timer, "100timer", "<count> <ms_delay>" },
548 { ut_101timer, "101timer", "<count> <ms_delay> <threads>" },
549
550 { ut_200counter, "200counter", "<v1> [<v2> [<v3> [...]]]" },
551 { ut_201counter, "201counter", "<v1> <v2> <threads>" },
552
553 { ut_300redact_start, "300redact_start", "<argv...>" },
554 { ut_301redact_child_start, "301redact_child_start", "<argv...>" },
555 { ut_302redact_exec, "302redact_exec", "<exe> <argv...>" },
556 { ut_303redact_def_param, "303redact_def_param", "<key> <value>" },
557
558 { ut_400ancestry, "400ancestry", "<target> <output_file> [<child_command_line>]" },
559 };
560 /* clang-format on */
561
562 /* clang-format off */
563 #define for_each_ut(k, ut_k) \
564 for (k = 0, ut_k = &ut_table[k]; \
565 k < ARRAY_SIZE(ut_table); \
566 k++, ut_k = &ut_table[k])
567 /* clang-format on */
568
569 static int print_usage(void)
570 {
571 int k;
572 struct unit_test *ut_k;
573
574 fprintf(stderr, "usage:\n");
575 for_each_ut (k, ut_k)
576 fprintf(stderr, "\t%s %s %s\n", USAGE_PREFIX, ut_k->ut_name,
577 ut_k->ut_usage);
578
579 return 129;
580 }
581
582 /*
583 * Issue various trace2 events for testing.
584 *
585 * We assume that these trace2 routines has already been called:
586 * [] trace2_initialize() [common-main.c:main()]
587 * [] trace2_cmd_start() [common-main.c:main()]
588 * [] trace2_cmd_name() [test-tool.c:cmd_main()]
589 * [] tracd2_cmd_list_config() [test-tool.c:cmd_main()]
590 * So that:
591 * [] the various trace2 streams are open.
592 * [] the process SID has been created.
593 * [] the "version" event has been generated.
594 * [] the "start" event has been generated.
595 * [] the "cmd_name" event has been generated.
596 * [] this writes various "def_param" events for interesting config values.
597 *
598 * We return from here and let test-tool.c::cmd_main() pass the exit
599 * code to common-main.c::main(), which will use it to call
600 * trace2_cmd_exit().
601 */
602 int cmd__trace2(int argc, const char **argv)
603 {
604 int k;
605 struct unit_test *ut_k;
606
607 argc--; /* skip over "trace2" arg */
608 argv++;
609
610 if (argc)
611 for_each_ut (k, ut_k)
612 if (!strcmp(argv[0], ut_k->ut_name))
613 return ut_k->ut_fn(argc - 1, argv + 1);
614
615 return print_usage();
616 }