[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] tools/oxenstored: Render backtraces more nicely in Syslog


  • To: Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Thu, 1 Dec 2022 21:19:06 +0000
  • Authentication-results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Christian Lindig <christian.lindig@xxxxxxxxxx>, David Scott <dave@xxxxxxxxxx>, Edwin Torok <edvin.torok@xxxxxxxxxx>, Rob Hoes <Rob.Hoes@xxxxxxxxxx>
  • Delivery-date: Thu, 01 Dec 2022 21:19:26 +0000
  • Ironport-data: A9a23:PJEf46gDreShGYU2+V5YmHt0X161ZxAKZh0ujC45NGQN5FlHY01je htvWD2BPvmJMWKnKosiO4/n9xgDscfUzdJnGgo+ris9Qy4b9cadCdqndUqhZCn6wu8v7q5Ex 55HNoSfdpBcolv0/ErF3m3J9CEkvU2wbuOgTrWCYmUpH1QMpB4J0XpLg/Q+jpNjne+3CgaMv cKai8DEMRqu1iUc3lg8sspvkzsy+qWs0N8klgZmP6oS5geHzyJ94K83fsldEVOpGuG4IcbiL wrz5OnR1n/U+R4rFuSknt7TGqHdauePVeQmoiM+t5mK2nCulARrukoIHKN0hXNsoyeIh7hMJ OBl7vRcf+uL0prkw4zxWzEAe8130DYvFLXveRBTuuTLp6HKnueFL1yDwyjaMKVBktubD12i+ tQBEy40Mj3EjNuM+6DhZq4zvJUgIc/CadZ3VnFIlVk1DN4jSJHHBa7L+cVZzHE7gcUm8fT2P pRDL2A1NVKZPkMJagx/5JEWxY9EglHWdTFCpU3Tjq0w+2XJlyR60aT3McqTcduPLSlQthbF/ zyZrjSjav0cHMCdzhCO9nKGvOjSmwKjVq0JLaCItdc/1TV/wURMUUZLBDNXu8KRhkegVvpFJ kcT+y5oqrI9nGS7Q9+4UxCmrXqsuh8H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y4BijERg/NKnubJRDiGLscM3zAppbr6PFtjdvaDaAg3d83gt DMQVlYvWk9dwbnDtCPxoRC9lTITH+mVTrgx8lC79xyu6H6RrDsLSqfIWpe4feIsrEaGInWSv yzMJVZD7vqKnbvA59A20nkV5xbORAlIbMoUxYAKi2zS+7wW/HXX7bgAYzuzZLWYEsZZl8=
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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.

Fixes: ee7815f49faf ("tools/oxenstored: Set uncaught exception handler")
Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
---
CC: Christian Lindig <christian.lindig@xxxxxxxxxx>
CC: David Scott <dave@xxxxxxxxxx>
CC: Edwin Torok <edvin.torok@xxxxxxxxxx>
CC: Rob Hoes <Rob.Hoes@xxxxxxxxxx>
---
 tools/ocaml/xenstored/logging.ml     |  2 +-
 tools/ocaml/xenstored/syslog_stubs.c | 26 +++++++++++++++++++++++---
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/tools/ocaml/xenstored/logging.ml b/tools/ocaml/xenstored/logging.ml
index 255051437d60..f233bc9a3956 100644
--- a/tools/ocaml/xenstored/logging.ml
+++ b/tools/ocaml/xenstored/logging.ml
@@ -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 =
diff --git a/tools/ocaml/xenstored/syslog_stubs.c 
b/tools/ocaml/xenstored/syslog_stubs.c
index e16c3a9491d0..760e78ff73dc 100644
--- a/tools/ocaml/xenstored/syslog_stubs.c
+++ b/tools/ocaml/xenstored/syslog_stubs.c
@@ -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);
-- 
2.11.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.