diff --git a/Makefile b/Makefile index b8b0942d96ab8c..245ea0bf20b95d 100644 --- a/Makefile +++ b/Makefile @@ -740,6 +740,7 @@ TEST_BUILTINS_OBJS += test-strcmp-offset.o TEST_BUILTINS_OBJS += test-string-list.o TEST_BUILTINS_OBJS += test-submodule-config.o TEST_BUILTINS_OBJS += test-subprocess.o +TEST_BUILTINS_OBJS += test-trace2.o TEST_BUILTINS_OBJS += test-urlmatch-normalization.o TEST_BUILTINS_OBJS += test-wildmatch.o TEST_BUILTINS_OBJS += test-windows-named-pipe.o @@ -991,6 +992,7 @@ LIB_OBJS += trace2/tr2_tgt_event.o LIB_OBJS += trace2/tr2_tgt_normal.o LIB_OBJS += trace2/tr2_tgt_perf.o LIB_OBJS += trace2/tr2_tls.o +LIB_OBJS += trace2/tr2_verb.o LIB_OBJS += trailer.o LIB_OBJS += transport.o LIB_OBJS += transport-helper.o diff --git a/builtin/am.c b/builtin/am.c index 5e866d17c7c7b4..5fb5d966883be3 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -520,6 +520,7 @@ static int run_post_rewrite_hook(const struct am_state *state) cp.in = xopen(am_path(state, "rewritten"), O_RDONLY); cp.stdout_to_stderr = 1; + cp.trace2_hook_name = "post-rewrite"; ret = run_command(&cp); diff --git a/builtin/checkout.c b/builtin/checkout.c index 1968e64ac6c03e..e2def410d8a668 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -256,6 +256,8 @@ static int checkout_paths(const struct checkout_opts *opts, int errs = 0; struct lock_file lock_file = LOCK_INIT; + trace2_cmd_subverb(opts->patch_mode ? "patch" : "path"); + if (opts->track != BRANCH_TRACK_UNSPECIFIED) die(_("'%s' cannot be used with updating paths"), "--track"); @@ -927,6 +929,9 @@ static int switch_branches(const struct checkout_opts *opts, void *path_to_free; struct object_id rev; int flag, writeout_error = 0; + + trace2_cmd_subverb("branch"); + memset(&old_branch_info, 0, sizeof(old_branch_info)); old_branch_info.path = path_to_free = resolve_refdup("HEAD", 0, &rev, &flag); if (old_branch_info.path) @@ -1142,6 +1147,8 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts) int status; struct strbuf branch_ref = STRBUF_INIT; + trace2_cmd_subverb("unborn"); + if (!opts->new_branch) die(_("You are on a branch yet to be born")); strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch); diff --git a/builtin/commit.c b/builtin/commit.c index 4ef879b3d4681b..dbf008f25bbaec 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -143,6 +143,7 @@ static int opt_parse_porcelain(const struct option *opt, const char *arg, int un static int do_serialize = 0; static char *serialize_path = NULL; +static int reject_implicit = 0; static int do_implicit_deserialize = 0; static int do_explicit_deserialize = 0; static char *deserialize_path = NULL; @@ -200,7 +201,7 @@ static int opt_parse_deserialize(const struct option *opt, const char *arg, int if (arg) /* override config or stdin */ deserialize_path = xstrdup(arg); if (deserialize_path && *deserialize_path - && (access(deserialize_path, R_OK) != 0)) + && (wt_status_deserialize_access(deserialize_path, R_OK) != 0)) die("cannot find serialization file '%s'", deserialize_path); @@ -1389,6 +1390,8 @@ static int git_status_config(const char *k, const char *v, void *cb) if (v && *v && access(v, R_OK) == 0) { do_implicit_deserialize = 1; deserialize_path = xstrdup(v); + } else { + reject_implicit = 1; } return 0; } @@ -1544,6 +1547,17 @@ int cmd_status(int argc, const char **argv, const char *prefix) (do_implicit_deserialize || do_explicit_deserialize)); if (try_deserialize) goto skip_init; + /* + * If we implicitly received a status cache pathname from the config + * and the file does not exist, we silently reject it and do the normal + * status "collect". Fake up some trace2 messages to reflect this and + * assist post-processors know this case is different. + */ + if (!do_serialize && reject_implicit) { + trace2_cmd_subverb("implicit-deserialize"); + trace2_data_string("status", the_repository, "deserialize/reject", + "status-cache/access"); + } enable_fscache(1); read_cache_preload(&s.pathspec); @@ -1582,6 +1596,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) if (s.relative_paths) s.prefix = prefix; + trace2_cmd_subverb("deserialize"); result = wt_status_deserialize(&s, deserialize_path, dw); if (result == DESERIALIZE_OK) return 0; @@ -1599,6 +1614,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) fd = -1; } + trace2_cmd_subverb("collect"); wt_status_collect(&s); if (0 <= fd) @@ -1613,6 +1629,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) if (fd_serialize < 0) die_errno(_("could not serialize to '%s'"), serialize_path); + trace2_cmd_subverb("serialize"); wt_status_serialize_v1(fd_serialize, &s); close(fd_serialize); } diff --git a/builtin/rebase.c b/builtin/rebase.c index 3d2db0369c91dd..dbeb5913b523bf 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -954,6 +954,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ACTION_EDIT_TODO, ACTION_SHOW_CURRENT_PATCH, } action = NO_ACTION; + static const char *action_names[] = { + N_("undefined"), N_("continue"), N_("skip"), N_("abort"), + N_("quit"), N_("edit_todo"), N_("show_current_patch"), + NULL + }; int committer_date_is_author_date = 0; int ignore_date = 0; int ignore_whitespace = 0; @@ -1142,6 +1147,15 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) die(_("The --edit-todo action can only be used during " "interactive rebase.")); + if (trace2_is_enabled()) { + if (is_interactive(&options)) + trace2_cmd_subverb("interactive"); + else if (exec.nr) + trace2_cmd_subverb("interactive-exec"); + else + trace2_cmd_subverb(action_names[action]); + } + switch (action) { case ACTION_CONTINUE: { struct object_id head; diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index c17ce94e12ee34..d974680a274c5f 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -694,6 +694,8 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, proc.argv = argv; proc.in = -1; proc.stdout_to_stderr = 1; + proc.trace2_hook_name = hook_name; + if (feed_state->push_options) { int i; for (i = 0; i < feed_state->push_options->nr; i++) @@ -807,6 +809,7 @@ static int run_update_hook(struct command *cmd) proc.stdout_to_stderr = 1; proc.err = use_sideband ? -1 : 0; proc.argv = argv; + proc.trace2_hook_name = "update"; code = start_command(&proc); if (code) @@ -1184,6 +1187,7 @@ static void run_update_post_hook(struct command *commands) proc.no_stdin = 1; proc.stdout_to_stderr = 1; proc.err = use_sideband ? -1 : 0; + proc.trace2_hook_name = "post-update"; if (!start_command(&proc)) { if (use_sideband) diff --git a/builtin/reset.c b/builtin/reset.c index ba65e45cb40439..d0ccfbd0ae9d2d 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -415,6 +415,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix) if (patch_mode) { if (reset_type != NONE) die(_("--patch is incompatible with --{hard,mixed,soft}")); + trace2_cmd_subverb("patch-interactive"); return run_add_interactive(rev, "--patch=reset", &pathspec); } @@ -431,6 +432,13 @@ int cmd_reset(int argc, const char **argv, const char *prefix) if (reset_type == NONE) reset_type = MIXED; /* by default */ + if (read_from_stdin) + trace2_cmd_subverb("path-stdin"); + else if (pathspec.nr) + trace2_cmd_subverb("path"); + else + trace2_cmd_subverb(reset_type_names[reset_type]); + if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree())) setup_work_tree(); diff --git a/builtin/worktree.c b/builtin/worktree.c index 41e7714396dc33..59e9fc319fa6c3 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -353,6 +353,7 @@ static int add_worktree(const char *path, const char *refname, cp.dir = path; cp.env = env; cp.argv = NULL; + cp.trace2_hook_name = "post-checkout"; argv_array_pushl(&cp.args, absolute_path(hook), oid_to_hex(&null_oid), oid_to_hex(&commit->object.oid), diff --git a/common-main.c b/common-main.c index bfea45c18d82f3..d484aec20979ac 100644 --- a/common-main.c +++ b/common-main.c @@ -25,6 +25,8 @@ static void restore_sigpipe_to_default(void) int main(int argc, const char **argv) { + int result; + /* * Always open file descriptors 0/1/2 to avoid clobbering files * in die(). It also avoids messing up when the pipes are dup'ed @@ -33,7 +35,9 @@ int main(int argc, const char **argv) sanitize_stdfds(); restore_sigpipe_to_default(); - trace2_initialize(argv); + trace2_initialize(); + trace2_cmd_start(argv); + trace2_collect_process_info(); git_resolve_executable_dir(argv[0]); @@ -43,5 +47,9 @@ int main(int argc, const char **argv) attr_start(); - return cmd_main(argc, argv); + result = cmd_main(argc, argv); + + trace2_cmd_exit(result); + + return result; } diff --git a/compat/mingw.c b/compat/mingw.c index ac4c1beecf05a3..4c58534be4c264 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1957,6 +1957,7 @@ static int try_shell_exec(const char *cmd, char *const *argv) return 0; prog = path_lookup(interpr, 1); if (prog) { + int exec_id; int argc = 0; #ifndef _MSC_VER const @@ -1966,14 +1967,16 @@ static int try_shell_exec(const char *cmd, char *const *argv) ALLOC_ARRAY(argv2, argc + 1); argv2[0] = (char *)cmd; /* full path to the script file */ memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc); + exec_id = trace2_exec(prog, argv2); pid = mingw_spawnv(prog, argv2, interpr); if (pid >= 0) { int status; if (waitpid(pid, &status, 0) < 0) status = 255; - trace2_exec_result(status); + trace2_exec_result(exec_id, status); exit(status); } + trace2_exec_result(exec_id, -1); pid = 1; /* indicate that we tried but failed */ free(prog); free(argv2); @@ -1986,13 +1989,17 @@ int mingw_execv(const char *cmd, char *const *argv) /* check if git_command is a shell script */ if (!try_shell_exec(cmd, argv)) { int pid, status; + int exec_id; + exec_id = trace2_exec(cmd, (const char **)argv); pid = mingw_spawnv(cmd, (const char **)argv, NULL); - if (pid < 0) + if (pid < 0) { + trace2_exec_result(exec_id, -1); return -1; + } if (waitpid(pid, &status, 0) < 0) status = 255; - trace2_exec_result(status); + trace2_exec_result(exec_id, status); exit(status); } return -1; diff --git a/compat/win32/ancestry.c b/compat/win32/ancestry.c new file mode 100644 index 00000000000000..7ed3cc66618c95 --- /dev/null +++ b/compat/win32/ancestry.c @@ -0,0 +1,102 @@ +#include "../../cache.h" +#include "../../json-writer.h" +#include +#include + +/* + * Find the process data for the given PID in the given snapshot + * and update the PROCESSENTRY32 data. + */ +static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32) +{ + pe32->dwSize = sizeof(PROCESSENTRY32); + + if (Process32First(hSnapshot, pe32)) { + do { + if (pe32->th32ProcessID == pid) + return 1; + } while (Process32Next(hSnapshot, pe32)); + } + return 0; +} + +/* + * Accumulate JSON array: + * [ + * exe-name-parent, + * exe-name-grand-parent, + * ... + * ] + * + * Note: we only report the filename of the process executable; the + * only way to get its full pathname is to use OpenProcess() + * and GetModuleFileNameEx() or QueryfullProcessImageName() + * and that seems rather expensive (on top of the cost of + * getting the snapshot). + */ +static void get_processes(struct json_writer *jw, HANDLE hSnapshot) +{ + PROCESSENTRY32 pe32; + DWORD pid; + + pid = GetCurrentProcessId(); + + /* We only want parent processes, so skip self. */ + if (!find_pid(pid, hSnapshot, &pe32)) + return; + pid = pe32.th32ParentProcessID; + + while (find_pid(pid, hSnapshot, &pe32)) { + + jw_array_string(jw, pe32.szExeFile); + + pid = pe32.th32ParentProcessID; + } +} + +/* + * Emit JSON data for the current and parent processes. Individual + * trace2 targets can decide how to actually print it. + */ +static void get_ancestry(void) +{ + HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + + if (hSnapshot != INVALID_HANDLE_VALUE) { + struct json_writer jw = JSON_WRITER_INIT; + + jw_array_begin(&jw, 0); + get_processes(&jw, hSnapshot); + jw_end(&jw); + + trace2_data_json("process", the_repository, + "windows/ancestry", &jw); + + jw_release(&jw); + CloseHandle(hSnapshot); + } +} + +/* + * Is a debugger attached to the current process? + * + * This will catch debug runs (where the debugger started the process). + * This is the normal case. Since this code is called during our startup, + * it will not report instances where a debugger is attached dynamically + * to a running git process, but that is relatively rare. + */ +static void get_is_being_debugged(void) +{ + if (IsDebuggerPresent()) + trace2_data_intmax("process", the_repository, + "windows/debugger_present", 1); +} + +void trace2_collect_process_info(void) +{ + if (!trace2_is_enabled()) + return; + + get_is_being_debugged(); + get_ancestry(); +} diff --git a/config.mak.uname b/config.mak.uname index fded5c253012bf..fa399e14b454e6 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -427,7 +427,8 @@ ifeq ($(uname_S),Windows) BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE COMPAT_OBJS = compat/msvc.o compat/winansi.o \ compat/win32/pthread.o compat/win32/syslog.o \ - compat/win32/dirent.o compat/win32/fscache.o + compat/win32/dirent.o compat/win32/fscache.o \ + compat/win32/ancestry.o COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\" BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -SUBSYSTEM:CONSOLE # invalidcontinue.obj allows Git's source code to close the same file @@ -591,7 +592,8 @@ ifneq (,$(findstring MINGW,$(uname_S))) COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/winansi.o \ compat/win32/pthread.o compat/win32/syslog.o \ - compat/win32/dirent.o compat/win32/fscache.o + compat/win32/dirent.o compat/win32/fscache.o \ + compat/win32/ancestry.o BASIC_CFLAGS += -DWIN32 -DPROTECT_NTFS_DEFAULT=1 EXTLIBS += -lws2_32 GITLIBS += git.res diff --git a/exec-cmd.c b/exec-cmd.c index 640003564e611c..7deeab30397cf2 100644 --- a/exec-cmd.c +++ b/exec-cmd.c @@ -330,22 +330,16 @@ const char **prepare_git_cmd(struct argv_array *out, const char **argv) int execv_git_cmd(const char **argv) { struct argv_array nargv = ARGV_ARRAY_INIT; - int err; prepare_git_cmd(&nargv, argv); trace_argv_printf(nargv.argv, "trace: exec:"); - trace2_exec("git", (const char **)nargv.argv); /* execvp() can only ever return if it fails */ sane_execvp("git", (char **)nargv.argv); - err = errno; - trace_printf("trace: exec failed: %s\n", strerror(err)); - trace2_exec_result(err); + trace_printf("trace: exec failed: %s\n", strerror(errno)); argv_array_clear(&nargv); - - errno = err; return -1; } diff --git a/git.c b/git.c index a85c48508e6c48..d378381edeeb0d 100644 --- a/git.c +++ b/git.c @@ -752,21 +752,25 @@ static void execv_dashed_external(const char **argv) if (run_pre_command_hook(cmd.args.argv)) die("pre-command hook aborted command"); + /* + * The code in run_command() logs trace2 child_start/child_exit + * events, so we do not need to report exec/exec_result events here. + */ trace_argv_printf(cmd.args.argv, "trace: exec:"); - trace2_exec(NULL, cmd.args.argv); /* * If we fail because the command is not found, it is * OK to return. Otherwise, we just pass along the status code, * or our usual generic code if we were not even able to exec * the program. - * + */ + exit_code = status = run_command(&cmd); + + /* * If the child process ran and we are now going to exit, emit a * generic string as our trace2 command verb to indicate that we * launched a dashed command. */ - exit_code = status = run_command(&cmd); - trace2_exec_result(status); if (status >= 0) { trace2_cmd_verb("_run_dashed_"); exit(status); @@ -935,5 +939,5 @@ int cmd_main(int argc, const char **argv) fprintf(stderr, _("failed to run command '%s': %s\n"), cmd, strerror(errno)); - return trace2_cmd_exit(1); + return 1; } diff --git a/run-command.c b/run-command.c index 1c5a6c224ecf43..14ecaf83efbec7 100644 --- a/run-command.c +++ b/run-command.c @@ -220,9 +220,29 @@ static int exists_in_PATH(const char *file) int sane_execvp(const char *file, char * const argv[]) { +#ifndef GIT_WINDOWS_NATIVE + /* + * execvp() doesn't return, so we all we can do is tell trace2 + * what we are about to do and let it leave a hint in the log + * (unless of course the execvp() fails). + * + * we skip this for Windows because the compat layer already + * has to emulate the execvp() call anyway. + */ + int exec_id = trace2_exec(file, (const char**)argv); +#endif + if (!execvp(file, argv)) return 0; /* cannot happen ;-) */ +#ifndef GIT_WINDOWS_NATIVE + { + int ec = errno; + trace2_exec_result(exec_id, ec); + errno = ec; + } +#endif + /* * When a command can't be found because one of the directories * listed in $PATH is unsearchable, execvp reports EACCES, but @@ -1371,6 +1391,7 @@ int run_hook_argv(const char *const *env, const char *name, hook.env = env; hook.no_stdin = 1; hook.stdout_to_stderr = 1; + hook.trace2_hook_name = name; return run_command(&hook); } diff --git a/run-command.h b/run-command.h index e71ddd65afe5e1..3572b17223d5d2 100644 --- a/run-command.h +++ b/run-command.h @@ -16,6 +16,7 @@ struct child_process { int trace2_child_id; uint64_t trace2_child_us_start; const char *trace2_child_class; + const char *trace2_hook_name; /* * Using .in, .out, .err: diff --git a/sequencer.c b/sequencer.c index f312ce123e8abb..75a7458d817652 100644 --- a/sequencer.c +++ b/sequencer.c @@ -1039,6 +1039,7 @@ static int run_rewrite_hook(const struct object_id *oldoid, proc.argv = argv; proc.in = -1; proc.stdout_to_stderr = 1; + proc.trace2_hook_name = "post-rewrite"; code = start_command(&proc); if (code) @@ -3648,6 +3649,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) hook.in = open(rebase_path_rewritten_list(), O_RDONLY); hook.stdout_to_stderr = 1; + hook.trace2_hook_name = "post-rewrite"; argv_array_push(&hook.args, post_rewrite_hook); argv_array_push(&hook.args, "rebase"); /* we don't care if this hook failed */ diff --git a/t/helper/test-cmp.c b/t/helper/test-cmp.c index 98dd0791520fe0..1c646a54bf609b 100644 --- a/t/helper/test-cmp.c +++ b/t/helper/test-cmp.c @@ -5,6 +5,12 @@ #include "parse-options.h" #include "run-command.h" +#ifdef WIN32 +#define NO_SUCH_DIR "\\\\.\\GLOBALROOT\\invalid" +#else +#define NO_SUCH_DIR "/dev/null" +#endif + static int run_diff(const char *path1, const char *path2) { const char *argv[] = { @@ -12,8 +18,8 @@ static int run_diff(const char *path1, const char *path2) }; const char *env[] = { "GIT_PAGER=cat", - "GIT_DIR=/dev/null", - "HOME=/dev/null", + "GIT_DIR=" NO_SUCH_DIR, + "HOME=" NO_SUCH_DIR, NULL }; diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c index 630c76d1275485..a176235db25589 100644 --- a/t/helper/test-parse-options.c +++ b/t/helper/test-parse-options.c @@ -1,6 +1,7 @@ #include "cache.h" #include "parse-options.h" #include "string-list.h" +#include "trace2.h" static int boolean = 0; static int integer = 0; @@ -152,6 +153,8 @@ int cmd_main(int argc, const char **argv) int i; int ret = 0; + trace2_cmd_verb("_parse_"); + argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0); if (length_cb.called) { diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c index 1ac1e29d8a2851..e5342b1422464c 100644 --- a/t/helper/test-tool.c +++ b/t/helper/test-tool.c @@ -45,6 +45,7 @@ static struct test_cmd cmds[] = { { "string-list", cmd__string_list }, { "submodule-config", cmd__submodule_config }, { "subprocess", cmd__subprocess }, + { "trace2", cmd__trace2 }, { "urlmatch-normalization", cmd__urlmatch_normalization }, { "wildmatch", cmd__wildmatch }, #ifdef GIT_WINDOWS_NATIVE diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h index 55a90fbd12a1c8..12b99f367a3072 100644 --- a/t/helper/test-tool.h +++ b/t/helper/test-tool.h @@ -40,6 +40,7 @@ int cmd__strcmp_offset(int argc, const char **argv); int cmd__string_list(int argc, const char **argv); int cmd__submodule_config(int argc, const char **argv); int cmd__subprocess(int argc, const char **argv); +int cmd__trace2(int argc, const char **argv); int cmd__urlmatch_normalization(int argc, const char **argv); int cmd__wildmatch(int argc, const char **argv); #ifdef GIT_WINDOWS_NATIVE diff --git a/t/helper/test-trace2.c b/t/helper/test-trace2.c new file mode 100644 index 00000000000000..b867f58d32cb36 --- /dev/null +++ b/t/helper/test-trace2.c @@ -0,0 +1,273 @@ +#include "test-tool.h" +#include "cache.h" +#include "argv-array.h" +#include "run-command.h" +#include "exec-cmd.h" +#include "config.h" + +typedef int (fn_unit_test)(int argc, const char **argv); + +struct unit_test +{ + fn_unit_test *ut_fn; + const char *ut_name; + const char *ut_usage; +}; + +#define MyOk 0 +#define MyError 1 + +static int get_i(int *p_value, const char *data) +{ + char *endptr; + + if (!data || !*data) + return MyError; + + *p_value = strtol(data, &endptr, 10); + if (*endptr || errno == ERANGE) + return MyError; + + return MyOk; +} + +/* + * Cause process to exit with the requested value via "return". + * + * Rely on test-tool.c:cmd_main() to call trace2_cmd_exit() + * with our result. + * + * Test harness can confirm: + * [] the process-exit value. + * [] the "code" field in the "exit" trace2 event. + * [] the "code" field in the "atexit" trace2 event. + * [] the "name" field in the "cmd_verb" trace2 event. + * [] "def_param" events for all of the "interesting" pre-defined config settings. + */ +static int ut_001return(int argc, const char **argv) +{ + int rc; + + if (get_i(&rc, argv[0])) + die("expect "); + + return rc; +} + +/* + * Cause the process to exit with the requested value via "exit()". + * + * Test harness can confirm: + * [] the "code" field in the "exit" trace2 event. + * [] the "code" field in the "atexit" trace2 event. + * [] the "name" field in the "cmd_verb" trace2 event. + * [] "def_param" events for all of the "interesting" pre-defined config settings. + */ +static int ut_002exit(int argc, const char **argv) +{ + int rc; + + if (get_i(&rc, argv[0])) + die("expect "); + + exit(rc); +} + +/* + * Send an "error" event with each value in argv. Normally, git only issues + * a single "error" event immediately before issuing an "exit" event (such + * as in die() or BUG()), but multiple "error" events are allowed. + * + * Test harness can confirm: + * [] a trace2 "error" event for each value in argv. + * [] the "name" field in the "cmd_verb" trace2 event. + * [] (optional) the file:line in the "exit" event refers to this function. + */ +static int ut_003error(int argc, const char **argv) +{ + int k; + + if (!argv[0] || !*argv[0]) + die("expect "); + + for (k = 0; k < argc; k++) + error("%s", argv[k]); + + return 0; +} + +/* + * Run a child process and wait for it to finish and exit with its return code. + * test-tool trace2 004child [] + * + * For example: + * test-tool trace2 004child git version + * test-tool trace2 004child test-tool trace2 001return 0 + * test-tool trace2 004child test-tool trace2 004child test-tool trace2 004child + * test-tool trace2 004child git -c alias.xyz=version xyz + * + * Test harness can confirm: + * [] the "name" field in the "cmd_verb" trace2 event. + * [] that the outer process has a single component SID (or depth "d0" in + * the PERF stream). + * [] that "child_start" and "child_exit" events are generated for the child. + * [] if the child process is an instrumented executable: + * [] that "version", "start", ..., "exit", and "atexit" events are + * generated by the child process. + * [] that the child process events have a multiple component SID (or + * depth "dN+1" in the PERF stream). + * [] that the child exit code is propagated to the parent process "exit" + * and "atexit" events.. + * [] (optional) that the "t_abs" field in the child process "atexit" event + * is less than the "t_rel" field in the "child_exit" event of the parent + * process. + * [] if the child process is like the alias example above, + * [] (optional) the child process attempts to run "git-xyx" as a dashed + * command. + * [] the child process emits an "alias" event with "xyz" => "version" + * [] the child process runs "git version" as a child process. + * [] the child process has a 3 component SID (or depth "d2" in the PERF + * stream). + */ +static int ut_004child(int argc, const char **argv) +{ + int result; + + /* + * Allow empty so we can do arbitrarily deep + * command nesting and let the last one be null. + */ + if (!argc) + return 0; + + result = run_command_v_opt(argv, 0); + exit(result); +} + +/* + * Exec a git command. This may either create a child process (Windows) + * or replace the existing process. + * test-tool trace2 005exec + * + * For example: + * test-tool trace2 005exec version + * + * Test harness can confirm (on Windows): + * [] the "name" field in the "cmd_verb" trace2 event. + * [] that the outer process has a single component SID (or depth "d0" in + * the PERF stream). + * [] that "exec" and "exec_result" events are generated for the child + * process (since the Windows compatibility layer fakes an exec() with + * a CreateProcess(), WaitForSingleObject(), and exit()). + * [] that the child process has multiple component SID (or depth "dN+1" + * in the PERF stream). + * + * Test harness can confirm (on platforms with a real exec() function): + * [] TODO talk about process replacement and how it affects SID. + */ +static int ut_005exec(int argc, const char **argv) +{ + int result; + + if (!argc) + return 0; + + result = execv_git_cmd(argv); + return result; +} + +static int ut_006data(int argc, const char **argv) +{ + const char *usage_error = + "expect [ [...]]"; + + if (argc % 3 != 0) + die("%s", usage_error); + + while (argc) { + if (!argv[0] || !*argv[0] || + !argv[1] || !*argv[1] || + !argv[2] || !*argv[2]) + die("%s", usage_error); + + trace2_data_string(argv[0], the_repository, argv[1], argv[2]); + argv += 3; + argc -= 3; + } + + return 0; +} + +/* + * Usage: + * test-tool trace2 + * test-tool trace2 + * ... + */ +#define USAGE_PREFIX "test-tool trace2" + +static struct unit_test ut_table[] = { + { ut_001return, "001return", "" }, + { ut_002exit, "002exit", "" }, + { ut_003error, "003error", "+" }, + { ut_004child, "004child", "[]" }, + { ut_005exec, "005exec", "" }, + { ut_006data, "006data", "[ ]+" }, +}; + +#define for_each_ut(k, ut_k) \ + for (k = 0, ut_k = &ut_table[k]; \ + k < ARRAY_SIZE(ut_table); \ + k++, ut_k = &ut_table[k]) + +static int print_usage(void) +{ + int k; + struct unit_test *ut_k; + + fprintf(stderr, "usage:\n"); + for_each_ut(k, ut_k) { + fprintf(stderr, "\t%s %s %s\n", + USAGE_PREFIX, ut_k->ut_name, ut_k->ut_usage); + } + + return 129; +} + +/* + * Issue various trace2 events for testing. + * + * We assume that these trace2 routines has already been called: + * [] trace2_initialize() [common-main.c:main()] + * [] trace2_cmd_start() [common-main.c:main()] + * [] trace2_cmd_verb() [test-tool.c:cmd_main()] + * [] tracd2_cmd_list_config() [test-tool.c:cmd_main()] + * So that: + * [] the various trace2 streams are open. + * [] the process SID has been created. + * [] the "version" event has been generated. + * [] the "start" event has been generated. + * [] the "verb" event has been generated. + * [] this writes various "def_param" events for interesting config values. + * + * We further assume that if we return (rather than exit()), trace2_cmd_exit() + * will be called by test-tool.c:cmd_main(). + */ +int cmd__trace2(int argc, const char **argv) +{ + int k; + struct unit_test *ut_k; + + argc--; /* skip over "trace2" arg */ + argv++; + + if (argc) { + for_each_ut(k, ut_k) { + if (!strcmp(argv[0], ut_k->ut_name)) + return ut_k->ut_fn(argc - 1, argv + 1); + } + } + + return print_usage(); +} + diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 7c48ed2aba2843..530184689fd552 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -93,6 +93,7 @@ test_expect_success 'No extra GIT_* on alias scripts' ' sed -n \ -e "/^GIT_PREFIX=/d" \ -e "/^GIT_TEXTDOMAINDIR=/d" \ + -e "/^GIT_TR2_PARENT/d" \ -e "/^GIT_/s/=.*//p" | sort EOF @@ -307,10 +308,20 @@ test_expect_success 'init prefers command line to GIT_DIR' ' test_path_is_missing otherdir/refs ' +downcase_on_case_insensitive_fs () { + test false = "$(git config --get core.filemode)" || return 0 + for f + do + tr A-Z a-z <"$f" >"$f".downcased && + mv -f "$f".downcased "$f" || return 1 + done +} + test_expect_success 'init with separate gitdir' ' rm -rf newdir && git init --separate-git-dir realgitdir newdir && echo "gitdir: $(pwd)/realgitdir" >expected && + downcase_on_case_insensitive_fs expected newdir/.git && test_cmp expected newdir/.git && test_path_is_dir realgitdir/refs ' @@ -365,6 +376,7 @@ test_expect_success 're-init to update git link' ' git init --separate-git-dir ../surrealgitdir ) && echo "gitdir: $(pwd)/surrealgitdir" >expected && + downcase_on_case_insensitive_fs expected newdir/.git && test_cmp expected newdir/.git && test_path_is_dir surrealgitdir/refs && test_path_is_missing realgitdir/refs @@ -378,6 +390,7 @@ test_expect_success 're-init to move gitdir' ' git init --separate-git-dir ../realgitdir ) && echo "gitdir: $(pwd)/realgitdir" >expected && + downcase_on_case_insensitive_fs expected newdir/.git && test_cmp expected newdir/.git && test_path_is_dir realgitdir/refs ' diff --git a/t/t0210-trace2-normal.sh b/t/t0210-trace2-normal.sh new file mode 100755 index 00000000000000..d53a66a3bbe78e --- /dev/null +++ b/t/t0210-trace2-normal.sh @@ -0,0 +1,135 @@ +#!/bin/sh + +test_description='test trace2 facility (normal target)' +. ./test-lib.sh + +# Add t/helper directory to PATH so that we can use a relative +# path to run nested instances of test-tool.exe (see 004child). +# This helps with HEREDOC comparisons later. +TTDIR="$GIT_BUILD_DIR/t/helper/" && export TTDIR +PATH="$TTDIR:$PATH" && export PATH + +TT="test-tool" && export TT + +# Warning: use of 'test_cmp' may run test-tool.exe and/or git.exe +# Warning: to do the actual diff/comparison, so the HEREDOCs here +# Warning: only cover our actual calls to test-tool and/or git. +# Warning: So you may see extra lines in artifact files when +# Warning: interactively debugging. + +# Turn off any inherited trace2 settings for this test. +unset GIT_TR2 GIT_TR2_PERF GIT_TR2_EVENT +unset GIT_TR2_BARE +unset GIT_TR2_CONFIG_PARAMS + +V=$(git version | sed -e 's/^git version //') && export V + +# There are multiple trace2 targets: normal, perf, and event. +# Trace2 events will/can be written to each active target (subject +# to whatever filtering that target decides to do). +# This script tests the normal target in isolation. + +# Enable "normal" trace2 target. +GIT_TR2="$(pwd)/trace.normal" && export GIT_TR2 +# Enable "bare" feature which turns off " : " prefix. +GIT_TR2_BARE=1 && export GIT_TR2_BARE + +# Basic tests of the trace2 normal stream. Since this stream is used +# primarily with printf-style debugging/tracing, we do limited testing +# here. +# +# We do confirm the following API features: +# [] the 'version ' event +# [] the 'start ' event +# [] the 'cmd_verb ' event +# [] the 'exit