Skip to content

Commit 19d7594

Browse files
avargitster
authored andcommitted
common-main.c: move non-trace2 exit() behavior out of trace2.c
Change the exit() wrapper added in ee4512e (trace2: create new combined trace facility, 2019-02-22) so that we'll split up the trace2 logging concerns from wanting to wrap the "exit()" function itself for other purposes. This makes more sense structurally, as we won't seem to conflate non-trace2 behavior with the trace2 code. I'd previously added an explanation for this in 368b584 (common-main.c: call exit(), don't return, 2021-12-07), that comment is being adjusted here. Now the only thing we'll do if we're not using trace2 is to truncate the "code" argument to the lowest 8 bits. We only need to do that truncation on non-POSIX systems, but in ee4512e that "if defined(__MINGW32__)" code added in 47e3de0 (MinGW: truncate exit()'s argument to lowest 8 bits, 2009-07-05) was made to run everywhere. It might be good for clarify to narrow that down by an "ifdef" again, but I'm not certain that in the interim we haven't had some other non-POSIX systems rely the behavior. On a POSIX system taking the lowest 8 bits is implicit, see exit(3)[1] and wait(2)[2]. Let's leave a comment about that instead. 1. https://man7.org/linux/man-pages/man3/exit.3.html 2. https://man7.org/linux/man-pages/man2/wait.2.html Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2668e36 commit 19d7594

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

common-main.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,22 @@ int main(int argc, const char **argv)
5555

5656
result = cmd_main(argc, argv);
5757

58+
/* Not exit(3), but a wrapper calling our common_exit() */
59+
exit(result);
60+
}
61+
62+
/* We wrap exit() to call common_exit() in git-compat-util.h */
63+
int common_exit(const char *file, int line, int code)
64+
{
5865
/*
59-
* We define exit() to call trace2_cmd_exit_fl() in
60-
* git-compat-util.h. Whether we reach this or exit()
61-
* elsewhere we'll always run our trace2 exit handler.
66+
* For non-POSIX systems: Take the lowest 8 bits of the "code"
67+
* to e.g. turn -1 into 255. On a POSIX system this is
68+
* redundant, see exit(3) and wait(2), but as it doesn't harm
69+
* anything there we don't need to guard this with an "ifdef".
6270
*/
63-
exit(result);
71+
code &= 0xff;
72+
73+
trace2_cmd_exit_fl(file, line, code);
74+
75+
return code;
6476
}

git-compat-util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,8 +1451,8 @@ int cmd_main(int, const char **);
14511451
* Intercept all calls to exit() and route them to trace2 to
14521452
* optionally emit a message before calling the real exit().
14531453
*/
1454-
int trace2_cmd_exit_fl(const char *file, int line, int code);
1455-
#define exit(code) exit(trace2_cmd_exit_fl(__FILE__, __LINE__, (code)))
1454+
int common_exit(const char *file, int line, int code);
1455+
#define exit(code) exit(common_exit(__FILE__, __LINE__, (code)))
14561456

14571457
/*
14581458
* You can mark a stack variable with UNLEAK(var) to avoid it being

trace2.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,15 @@ void trace2_cmd_start_fl(const char *file, int line, const char **argv)
202202
argv);
203203
}
204204

205-
int trace2_cmd_exit_fl(const char *file, int line, int code)
205+
void trace2_cmd_exit_fl(const char *file, int line, int code)
206206
{
207207
struct tr2_tgt *tgt_j;
208208
int j;
209209
uint64_t us_now;
210210
uint64_t us_elapsed_absolute;
211211

212-
code &= 0xff;
213-
214212
if (!trace2_enabled)
215-
return code;
213+
return;
216214

217215
trace_git_fsync_stats();
218216
trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT);
@@ -226,8 +224,6 @@ int trace2_cmd_exit_fl(const char *file, int line, int code)
226224
if (tgt_j->pfn_exit_fl)
227225
tgt_j->pfn_exit_fl(file, line, us_elapsed_absolute,
228226
code);
229-
230-
return code;
231227
}
232228

233229
void trace2_cmd_error_va_fl(const char *file, int line, const char *fmt,

trace2.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,8 @@ void trace2_cmd_start_fl(const char *file, int line, const char **argv);
101101

102102
/*
103103
* Emit an 'exit' event.
104-
*
105-
* Write the exit-code that will be passed to exit() or returned
106-
* from main().
107-
*
108-
* Use this prior to actually calling exit().
109-
* See "#define exit()" in git-compat-util.h
110104
*/
111-
int trace2_cmd_exit_fl(const char *file, int line, int code);
105+
void trace2_cmd_exit_fl(const char *file, int line, int code);
112106

113107
#define trace2_cmd_exit(code) (trace2_cmd_exit_fl(__FILE__, __LINE__, (code)))
114108

0 commit comments

Comments
 (0)