Skip to content

Commit 5b2d1bd

Browse files
committed
Display Windows NTSTATUS exit codes in hex
On Windows, "negative" exit codes are probably NTSTATUS values. For example, if a program accesses an invalid memory location, Unix sends a SIGSEGV signal which, if unhandled, will terminate the process (setting some kind of non-zero exit code - for example, Linux sets the exit code to 128 + signal number to give a fairly memorable 139). In the equivalent scenario, Windows throws an EXCEPTION_ACCESS_VIOLATION which, if handled by the default exception handler, will terminate the process with exit code STATUS_ACCESS_VIOLATION. These codes are large negative numbers, which are not terribly memorable in decimal, so for negative exit codes we instead display them in hexadecimal as 0xc0000005 is slightly more memorable than -1073741819.
1 parent 714fb00 commit 5b2d1bd

4 files changed

Lines changed: 15 additions & 3 deletions

File tree

bin/admin.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let show () cap_path terse pool worker =
6464

6565
let check_exit_status = function
6666
| Unix.WEXITED 0 -> ()
67-
| Unix.WEXITED x -> Fmt.failwith "Sub-process failed with exit code %d" x
67+
| Unix.WEXITED x -> Fmt.failwith "Sub-process failed with exit code %a" Logging.pp_exit_status x
6868
| Unix.WSIGNALED x -> Fmt.failwith "Sub-process failed with signal %a" Fmt.Dump.signal x
6969
| Unix.WSTOPPED x -> Fmt.failwith "Sub-process stopped with signal %a" Fmt.Dump.signal x
7070

bin/logging.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
let pp_exit_status f n =
2+
if Sys.win32 && n < 0 then
3+
Fmt.pf f "0x%08lx" (Int32.of_int n)
4+
else
5+
Fmt.int f n
6+
17
let pp_timestamp f x =
28
let open Unix in
39
let tm = localtime x in

bin/worker.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let or_die = function
1414

1515
let check_exit_status = function
1616
| Unix.WEXITED 0 -> ()
17-
| Unix.WEXITED x -> Fmt.failwith "Sub-process failed with exit code %d" x
17+
| Unix.WEXITED x -> Fmt.failwith "Sub-process failed with exit code %a" Logging.pp_exit_status x
1818
| Unix.WSIGNALED x -> Fmt.failwith "Sub-process failed with signal %a" Fmt.Dump.signal x
1919
| Unix.WSTOPPED x -> Fmt.failwith "Sub-process stopped with signal %a" Fmt.Dump.signal x
2020

worker/process.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ let exec ~label ~log ~switch ?env ?(stdin="") ?(stderr=`FD_copy Unix.stdout) ?(i
4141
| Unix.WSIGNALED x -> Fmt.error_msg "%s failed with signal %a" label Fmt.Dump.signal x
4242
| Unix.WSTOPPED x -> Fmt.error_msg "%s stopped with signal %a" label Fmt.Dump.signal x
4343

44+
let pp_exit_status f n =
45+
if Sys.win32 && n < 0 then
46+
Fmt.pf f "0x%08lx" (Int32.of_int n)
47+
else
48+
Fmt.int f n
49+
4450
let check_call ~label ~log ~switch ?env ?stdin ?stderr ?is_success cmd =
4551
exec ~label ~log ~switch ?env ?stdin ?stderr ?is_success cmd >|= function
4652
| Ok () -> Ok ()
4753
| Error `Cancelled -> Error `Cancelled
48-
| Error (`Exit_code n) -> Fmt.error_msg "%s failed with exit-code %d" label n
54+
| Error (`Exit_code n) -> Fmt.error_msg "%s failed with exit-code %a" label pp_exit_status n
4955
| Error (`Msg _) as e -> e

0 commit comments

Comments
 (0)