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
()

ocaml/xapi-guard/lib/server_interface.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ let shutdown = Lwt_switch.create ()
3838

3939
let () =
4040
let cleanup n =
41-
let n = Fmt.(to_to_string Dump.signal n) in
42-
debug "Triggering cleanup on signal %s, and waiting for servers to stop" n ;
41+
debug "Triggering cleanup on signal %a, and waiting for servers to stop"
42+
Debug.Pp.signal n ;
4343
Lwt.async (fun () ->
4444
let* () = Lwt_switch.turn_off shutdown in
4545
info "Cleanup complete, exiting" ;

ocaml/xapi/helpers.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,14 @@ let call_script ?(log_output = Always) ?env ?stdin ?timeout script args =
104104
(ExnHelper.string_of_exn e) ;
105105
raise e
106106
| Forkhelpers.Spawn_internal_error (stderr, stdout, status) as e ->
107-
let signal = Unixext.string_of_signal in
108107
let message =
109108
match status with
110109
| Unix.WEXITED n ->
111110
Printf.sprintf "exited with code %d" n
112111
| Unix.WSIGNALED n ->
113-
Printf.sprintf "was killed by signal %s" (signal n)
112+
Printf.sprintf "was killed by signal %a" Debug.Pp.signal n
114113
| Unix.WSTOPPED n ->
115-
Printf.sprintf "was stopped by signal %s" (signal n)
114+
Printf.sprintf "was stopped by signal %a" Debug.Pp.signal n
116115
in
117116
if should_log_output_on_failure then
118117
debug "%s %s %s [stdout = '%s'; stderr = '%s']" script

ocaml/xapi/sm_exec.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ let exec_xmlrpc ~dbg ?context:_ ?(needs_session = true) (driver : string)
393393
(Backend_error
394394
( Api_errors.sr_backend_failure
395395
, [
396-
"received signal: " ^ Unixext.string_of_signal i
396+
Printf.sprintf "received signal: %a" Debug.Pp.signal i
397397
; output
398398
; log
399399
]

ocaml/xapi/xapi_extensions.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ let call_extension rpc =
5050
( Api_errors.internal_error
5151
, [
5252
path
53-
; Printf.sprintf "signal: %s"
54-
(Xapi_stdext_unix.Unixext.string_of_signal i)
53+
; Printf.sprintf "signal: %a" Debug.Pp.signal i
5554
; output
5655
; log
5756
]

ocaml/xapi/xapi_plugins.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ let call_plugin session_id plugin_name fn_name args =
4949
raise
5050
(Api_errors.Server_error
5151
( Api_errors.xenapi_plugin_failure
52-
, [
53-
Printf.sprintf "signal: %s"
54-
(Xapi_stdext_unix.Unixext.string_of_signal i)
55-
; output
56-
; log
57-
]
52+
, [Printf.sprintf "signal: %a" Debug.Pp.signal i; output; log]
5853
)
5954
)
6055
| Forkhelpers.Spawn_internal_error (log, output, Unix.WEXITED _) ->

ocaml/xcp-rrdd/bin/rrdd/xcp_rrdd.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ let configure_writers () =
735735
(** we need to make sure we call exit on fatal signals to make sure profiling
736736
data is dumped *)
737737
let stop err writers signal =
738-
debug "caught signal %s" (Xapi_stdext_unix.Unixext.string_of_signal signal) ;
738+
debug "caught signal %a" Debug.Pp.signal signal ;
739739
List.iter (fun (_, writer) -> writer.Rrd_writer.cleanup ()) writers ;
740740
exit err
741741

ocaml/xcp-rrdd/lib/plugin/utils.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ let exec_cmd (module D : Debug.DEBUG) ~cmdstring ~(f : string -> 'a option) =
5959
(try loop () with End_of_file -> ()) ;
6060
Unix.close out_readme ;
6161
let pid, status = Forkhelpers.waitpid pid in
62-
let signal = Xapi_stdext_unix.Unixext.string_of_signal in
6362
( match status with
6463
| Unix.WEXITED n ->
6564
D.debug "Process %d exited normally with code %d" pid n
6665
| Unix.WSIGNALED s ->
67-
D.debug "Process %d was killed by signal %s" pid (signal s)
66+
D.debug "Process %d was killed by signal %a" pid Debug.Pp.signal s
6867
| Unix.WSTOPPED s ->
69-
D.debug "Process %d was stopped by signal %s" pid (signal s)
68+
D.debug "Process %d was stopped by signal %a" pid Debug.Pp.signal s
7069
) ;
7170
List.rev !vals

ocaml/xenopsd/lib/cancellable_subprocess.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,14 @@ let run (task : Xenops_task.task_handle) ?env ?stdin fds
7777
| Unix.WSTOPPED n ->
7878
raise (Spawn_internal_error (err, out, Unix.WSTOPPED n))
7979
| Unix.WSIGNALED s ->
80-
let signal = Unixext.string_of_signal s in
8180
if !cancelled then (
8281
debug
83-
"Subprocess %s exited with signal %s and cancel requested; \
82+
"Subprocess %s exited with signal %a and cancel requested; \
8483
raising Cancelled"
85-
cmd signal ;
84+
cmd Debug.Pp.signal s ;
8685
Xenops_task.raise_cancelled task
8786
) else (
88-
debug "Subprocess %s exited with signal %s" cmd signal ;
87+
debug "Subprocess %s exited with signal %a" cmd Debug.Pp.signal s ;
8988
raise (Spawn_internal_error (err, out, Unix.WSIGNALED s))
9089
)
9190
)

ocaml/xenopsd/lib/suspend_image.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,15 @@ let with_conversion_script task name hvm fd f =
275275
| Unix.WSIGNALED n ->
276276
Error
277277
(Failure
278-
(Printf.sprintf "Conversion script exited with signal %s"
279-
(Unixext.string_of_signal n)
278+
(Printf.sprintf "Conversion script exited with signal %a"
279+
Debug.Pp.signal n
280280
)
281281
)
282282
| Unix.WSTOPPED n ->
283283
Error
284284
(Failure
285-
(Printf.sprintf "Conversion script stopped with signal %s"
286-
(Unixext.string_of_signal n)
285+
(Printf.sprintf "Conversion script stopped with signal %a"
286+
Debug.Pp.signal n
287287
)
288288
)
289289
)

ocaml/xenopsd/lib/xenopsd.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ let main backend =
461461
(* we need to catch this to make sure at_exit handlers are triggered. In
462462
particular, triggers for the bisect_ppx coverage profiling *)
463463
let signal_handler n =
464-
debug "caught signal %s" (Unixext.string_of_signal n) ;
464+
debug "caught signal %a" Debug.Pp.signal n ;
465465
exit 0
466466
in
467467
Sys.set_signal Sys.sigpipe Sys.Signal_ignore ;

0 commit comments

Comments
 (0)