Skip to content

Commit c0fbb69

Browse files
committed
Debug: add pretty-printing function for signals
When signals are are written to logs, the POSIX name should be used to minimize confusion. It makes sense that the function that does this is in the logging library instead of the unix one, as most users will be already be using the logging library, but not all the unix one. Moving it there also allows for a more ergonomic usage with the logging functions. Signed-off-by: Pau Ruiz Safont <[email protected]>
1 parent 5b86063 commit c0fbb69

File tree

20 files changed

+37
-106
lines changed

20 files changed

+37
-106
lines changed

doc/content/design/coverage/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ terminated by receiving the `TERM` signal, a signal handler must be
9898
installed:
9999

100100
let stop signal =
101-
let name = Xapi_stdext_unix.Unixext.string_of_signal signal in
102-
printf "caught signal %s\n" name;
101+
printf "caught signal %a\n" Debug.Pp.signal signal;
103102
exit 0
104103

105104
Sys.set_signal Sys.sigterm (Sys.Signal_handle stop)

ocaml/forkexecd/src/child.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ let report_child_exit comms_sock args child_pid status =
111111
Fe.WEXITED n
112112
| Unix.WSIGNALED n ->
113113
log_failure args child_pid
114-
(Printf.sprintf "exited with signal: %s" (Unixext.string_of_signal n)) ;
114+
(Printf.sprintf "exited with signal: %a" Debug.Pp.signal n) ;
115115
Fe.WSIGNALED n
116116
| Unix.WSTOPPED n ->
117117
log_failure args child_pid
118-
(Printf.sprintf "stopped with signal: %s" (Unixext.string_of_signal n)) ;
118+
(Printf.sprintf "stopped with signal: %a" Debug.Pp.signal n) ;
119119
Fe.WSTOPPED n
120120
in
121121
let result = Fe.Finished pr in

ocaml/libs/log/debug.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,8 @@ functor
353353
with e -> log_backtrace_internal ~level:Syslog.Debug ~msg:"debug" e ()
354354
end
355355

356-
module Pp = struct let mtime_span () = Fmt.str "%a" Mtime.Span.pp end
356+
module Pp = struct
357+
let mtime_span () = Fmt.to_to_string Mtime.Span.pp
358+
359+
let signal () = Fmt.(to_to_string Dump.signal)
360+
end

ocaml/libs/log/debug.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,8 @@ val is_disabled : string -> Syslog.level -> bool
9191

9292
module Pp : sig
9393
val mtime_span : unit -> Mtime.Span.t -> string
94+
95+
val signal : unit -> int -> string
96+
(** signal pretty-prints an ocaml signal number as its POSIX name, see
97+
{Fmt.Dump.signal} *)
9498
end

ocaml/libs/xapi-compression/xapi_compression.ml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,17 @@ module Make (Algorithm : ALGORITHM) = struct
123123
error "%s" msg ; failwith msg
124124
in
125125
Unixfd.safe_close close_later ;
126-
let open Xapi_stdext_unix in
127126
match snd (Forkhelpers.waitpid pid) with
128127
| Unix.WEXITED 0 ->
129128
()
130129
| Unix.WEXITED i ->
131130
failwith_error (Printf.sprintf "exit code %d" i)
132131
| Unix.WSIGNALED i ->
133132
failwith_error
134-
(Printf.sprintf "killed by signal: %s"
135-
(Unixext.string_of_signal i)
136-
)
133+
(Printf.sprintf "killed by signal: %a" Debug.Pp.signal i)
137134
| Unix.WSTOPPED i ->
138135
failwith_error
139-
(Printf.sprintf "stopped by signal: %s"
140-
(Unixext.string_of_signal i)
141-
)
136+
(Printf.sprintf "stopped by signal: %a" Debug.Pp.signal i)
142137
)
143138

144139
let compress fd f = go Compress Active fd f

ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/unixext.ml

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -371,64 +371,6 @@ let kill_and_wait ?(signal = Sys.sigterm) ?(timeout = 10.) pid =
371371
raise Process_still_alive
372372
)
373373

374-
let string_of_signal = function
375-
| s when s = Sys.sigabrt ->
376-
"SIGABRT"
377-
| s when s = Sys.sigalrm ->
378-
"SIGALRM"
379-
| s when s = Sys.sigfpe ->
380-
"SIGFPE"
381-
| s when s = Sys.sighup ->
382-
"SIGHUP"
383-
| s when s = Sys.sigill ->
384-
"SIGILL"
385-
| s when s = Sys.sigint ->
386-
"SIGINT"
387-
| s when s = Sys.sigkill ->
388-
"SIGKILL"
389-
| s when s = Sys.sigpipe ->
390-
"SIGPIPE"
391-
| s when s = Sys.sigquit ->
392-
"SIGQUIT"
393-
| s when s = Sys.sigsegv ->
394-
"SIGSEGV"
395-
| s when s = Sys.sigterm ->
396-
"SIGTERM"
397-
| s when s = Sys.sigusr1 ->
398-
"SIGUSR1"
399-
| s when s = Sys.sigusr2 ->
400-
"SIGUSR2"
401-
| s when s = Sys.sigchld ->
402-
"SIGCHLD"
403-
| s when s = Sys.sigcont ->
404-
"SIGCONT"
405-
| s when s = Sys.sigstop ->
406-
"SIGSTOP"
407-
| s when s = Sys.sigttin ->
408-
"SIGTTIN"
409-
| s when s = Sys.sigttou ->
410-
"SIGTTOU"
411-
| s when s = Sys.sigvtalrm ->
412-
"SIGVTALRM"
413-
| s when s = Sys.sigprof ->
414-
"SIGPROF"
415-
| s when s = Sys.sigbus ->
416-
"SIGBUS"
417-
| s when s = Sys.sigpoll ->
418-
"SIGPOLL"
419-
| s when s = Sys.sigsys ->
420-
"SIGSYS"
421-
| s when s = Sys.sigtrap ->
422-
"SIGTRAP"
423-
| s when s = Sys.sigurg ->
424-
"SIGURG"
425-
| s when s = Sys.sigxcpu ->
426-
"SIGXCPU"
427-
| s when s = Sys.sigxfsz ->
428-
"SIGXFSZ"
429-
| s ->
430-
Printf.sprintf "SIG(%d)" s
431-
432374
let with_polly f =
433375
let polly = Polly.create () in
434376
let finally () = Polly.close polly in

ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/unixext.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ exception Process_still_alive
122122

123123
val kill_and_wait : ?signal:int -> ?timeout:float -> int -> unit
124124

125-
val string_of_signal : int -> string
126-
(** [string_of_signal x] translates an ocaml signal number into
127-
* a string suitable for logging. *)
128-
129125
val proxy : Unix.file_descr -> Unix.file_descr -> unit
130126

131127
val really_read : Unix.file_descr -> bytes -> int -> int -> unit

ocaml/nbd/src/cleanup.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ module Runtime = struct
220220
exit 0
221221
| Signal n ->
222222
Printf.eprintf "unexpected signal %s in signal handler - exiting"
223-
(Xapi_stdext_unix.Unixext.string_of_signal n) ;
223+
Fmt.(to_to_string Dump.signal n) ;
224224
flush stderr ;
225225
exit 1
226226
| e ->
@@ -230,7 +230,7 @@ module Runtime = struct
230230
exit 1
231231

232232
let cleanup_resources signal =
233-
let name = Xapi_stdext_unix.Unixext.string_of_signal signal in
233+
let name = Fmt.(to_to_string Dump.signal signal) in
234234
let cleanup () =
235235
Lwt_log.warning_f "Caught signal %s, cleaning up" name >>= fun () ->
236236
(* First we have to close the open file descriptors corresponding to the

ocaml/nbd/src/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(libraries
55
cmdliner
66
consts
7+
fmt
78
local_xapi_session
89
lwt
910
lwt.unix
@@ -19,7 +20,6 @@
1920
xapi-consts
2021
xapi-inventory
2122
xapi-types
22-
xapi-stdext-unix
2323
xen-api-client-lwt
2424
)
2525
)

ocaml/networkd/bin/network_server.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ let on_shutdown signal =
5353
let dbg = "shutdown" in
5454
Debug.with_thread_associated dbg
5555
(fun () ->
56-
debug "xcp-networkd caught signal %s; performing cleanup actions."
57-
(Xapi_stdext_unix.Unixext.string_of_signal signal) ;
56+
debug "xcp-networkd caught signal %a; performing cleanup actions."
57+
Debug.Pp.signal signal ;
5858
write_config ()
5959
)
6060
()

0 commit comments

Comments
 (0)