tools/oxenstored: Render backtraces more nicely in Syslog
PATCH 1/1 tools/oxenstored: Render backtraces more nicely in Syslog
fallback_exception_handler feeds a string with embedded newlines directly into syslog(). While this is an improvement on getting nothing, syslogd escapes all control characters it gets, and emits one (long) line into the log.
Fix the problem generally in the syslog stub. As we already have a local copy of the string, split it in place and emit one syslog() call per line.
Also tweak Logging.msg_of to avoid putting an extra newline on a string which already ends with one.
---
CC: Christian Lindig <[email protected]>
CC: David Scott <[email protected]>
CC: Edwin Torok <[email protected]>
CC: Rob Hoes <[email protected]>
--- tools/ocaml/xenstored/logging.ml | 2 +- tools/ocaml/xenstored/syslog_stubs.c | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-)
tools/ocaml/xenstored/logging.ml +1 -1
@@ -344,7 +344,7 @@ let watch_not_fired ~con perms path = access_logging ~tid:0 ~con ~data Watch_not_fired ~level:Info let msg_of exn bt = - Printf.sprintf "Fatal exception: %s\n%s\n" (Printexc.to_string exn) + Printf.sprintf "Fatal exception: %s\n%s" (Printexc.to_string exn) (Printexc.raw_backtrace_to_string bt) let fallback_exception_handler exn bt =
tools/ocaml/xenstored/syslog_stubs.c +23 -3
@@ -37,14 +37,34 @@ value stub_syslog(value facility, value level, value msg) {CAMLparam3 (facility, level, msg);char *c_msg =strdup (String_val (msg)); +char *s = c_msg, *ss;int c_facility = __syslog_facility_table[Int_val (facility)] | __syslog_level_table[Int_val (level)];if ( !c_msg )caml_raise_out_of_memory (); -caml_enter_blocking_section (); -syslog (c_facility,"%s" , c_msg); -caml_leave_blocking_section (); + +/* +* syslog() doesn't like embedded newlines, and c_msg generally +* contains them. +* +* Split the message in place by converting \n to \0, and issue one +* syslog() call per line, skipping the final iteration if c_msg ends +* with a newline anyway. +*/ +do { + ss =strchr (s,'\n' ); +if ( ss ) + *ss ='\0' ; +else if ( *s =='\0' ) +break ; + +caml_enter_blocking_section (); +syslog (c_facility,"%s" , s); +caml_leave_blocking_section (); + + s = ss +1 ; + }while ( ss );free (c_msg);CAMLreturn (Val_unit);
Signature · 1 line
2.11.0
Quoted 14 lines · Andrew Cooper
fallback_exception_handlerfeeds a string with embedded newlines directly intosyslog(). While this is an improvement on getting nothing, syslogd escapes all control characters it gets, and emits one (long) line into the log.Fix the problem generally in the syslog stub. As we already have a local copy of the string, split it in place and emit one
syslog()call per line.Also tweak Logging.msg_of to avoid putting an extra newline on a string which already ends with one.
Quoted 7 lines
Fixes ee7815f49faf ("tools/oxenstored: Set uncaught exception handler")Signed-off-by Andrew Cooper <[email protected]>---
CC: Christian Lindig <[email protected]>
CC: David Scott <[email protected]>
CC: Edwin Torok <[email protected]>
CC: Rob Hoes <[email protected]>
Quoted 23 lines
---
tools/ocaml/xenstored/logging.ml| 2 +-tools/ocaml/xenstored/syslog_stubs.c| 26 +++++++++++++++++++++++---2 files changed, 24 insertions(+), 4 deletions(-)tools/ocaml/xenstored/logging.ml +0
-0@@ -344,7 +344,7 @@ let watch_not_fired ~con perms path = access_logging ~tid:0 ~con ~data Watch_not_fired ~level:Infolet
msg_ofexn bt =tools/ocaml/xenstored/logging.ml +1
-1- Printf.sprintf "Fatal exception: %s\n%s\n" (Printexc.to_string exn) + Printf.sprintf "Fatal exception: %s\n%s" (Printexc.to_string exn)(Printexc.raw_backtrace_to_string bt)
let
fallback_exception_handlerexn bt =tools/ocaml/xenstored/syslog_stubs.c +0
-0
tools/ocaml/xenstored/syslog_stubs.c value stub_syslog(value facility, value level, value msg) · line 39
Quoted 3 lines
tools/ocaml/xenstored/syslog_stubs.c +0
-0@@ -37,14 +37,34 @@ value stub_syslog(value facility, value level, value msg) {CAMLparam3 (facility, level, msg);char *c_msg =strdup (String_val (msg));
Is this working as expected? An OCaml string may contain embedded null bytes.
Quoted 37 lines
tools/ocaml/xenstored/syslog_stubs.c +23
-3+char *s = c_msg, *ss;int c_facility = __syslog_facility_table[Int_val (facility)] | __syslog_level_table[Int_val (level)];if ( !c_msg )caml_raise_out_of_memory (); -caml_enter_blocking_section (); -syslog (c_facility,"%s" , c_msg); -caml_leave_blocking_section (); + +/* +* syslog() doesn't like embedded newlines, and c_msg generally +* contains them. +* +* Split the message in place by converting \n to \0, and issue one +* syslog() call per line, skipping the final iteration if c_msg ends +* with a newline anyway. +*/ +do { + ss =strchr (s,'\n' ); +if ( ss ) + *ss ='\0' ; +else if ( *s =='\0' ) +break ; + +caml_enter_blocking_section (); +syslog (c_facility,"%s" , s); +caml_leave_blocking_section (); + + s = ss +1 ; + }while ( ss );free (c_msg);CAMLreturn (Val_unit);Signature · 1 line
2.11.0
Quoted 12 lines · Christian Lindig
Quoted 12 lines · Andrew Cooper
fallback_exception_handlerfeeds a string with embedded newlines directly intosyslog(). While this is an improvement on getting nothing, syslogd escapes all control characters it gets, and emits one (long) line into the log.Fix the problem generally in the syslog stub. As we already have a local copy of the string, split it in place and emit one
syslog()call per line.Also tweak Logging.msg_of to avoid putting an extra newline on a string which already ends with one.
Quoted 8 lines
Quoted 7 lines
Fixes ee7815f49faf ("tools/oxenstored: Set uncaught exception handler")Signed-off-by Andrew Cooper <[email protected]>---
CC: Christian Lindig <[email protected]>
CC: David Scott <[email protected]>
CC: Edwin Torok <[email protected]>
CC: Rob Hoes <[email protected]>Acked-by Christian Lindig <[email protected]>
Thanks.
Quoted 18 lines
Quoted 18 lines
---
tools/ocaml/xenstored/logging.ml| 2 +-tools/ocaml/xenstored/syslog_stubs.c| 26 +++++++++++++++++++++++---2 files changed, 24 insertions(+), 4 deletions(-)tools/ocaml/xenstored/logging.ml +0
-0@@ -344,7 +344,7 @@ let watch_not_fired ~con perms path = access_logging ~tid:0 ~con ~data Watch_not_fired ~level:Infolet
msg_ofexn bt =tools/ocaml/xenstored/logging.ml +1
-1- Printf.sprintf "Fatal exception: %s\n%s\n" (Printexc.to_string exn) + Printf.sprintf "Fatal exception: %s\n%s" (Printexc.to_string exn)(Printexc.raw_backtrace_to_string bt)
Quoted 10 lines
tools/ocaml/xenstored/syslog_stubs.c value stub_syslog(value facility, value level, value msg) · line 39
Quoted 9 lines
let
fallback_exception_handlerexn bt =tools/ocaml/xenstored/syslog_stubs.c +0
-0@@ -37,14 +37,34 @@ value stub_syslog(value facility, value level, value msg) {CAMLparam3 (facility, level, msg);char *c_msg =strdup (String_val (msg));Is this working as expected? An OCaml string may contain embedded null bytes.
I wondered the same, but there's really nothing sane to be done.
This is for handling human readable strings, and attempting to force NULs out via syslog() can't be done (because of syslog()'s API).
~Andrew