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

[PATCH 7/8] tools/oxenstored: Set uncaught exception handler


  • To: Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Tue, 22 Nov 2022 15:20:42 +0000
  • Authentication-results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: Edwin Török <edvin.torok@xxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Christian Lindig <christian.lindig@xxxxxxxxxx>, David Scott <dave@xxxxxxxxxx>, Rob Hoes <Rob.Hoes@xxxxxxxxxx>
  • Delivery-date: Tue, 22 Nov 2022 15:21:10 +0000
  • Ironport-data: A9a23:zzVsDauHIailmdrr4jvyaEIcUufnVAReMUV32f8akzHdYApBsoF/q tZmKTrQb/aNMDOheYsjYY2xpB4O6pHTmtZrHAFvpSk0F3xE+JbJXdiXEBz9bniYRiHhoOCLz O1FM4Wdc5pkJpP4jk3wWlQ0hSAkjclkfpKlVKiffHg0HVU/IMsYoUoLs/YjhYJ1isSODQqIu Nfjy+XSI1bg0DNvWo4uw/vrRChH4bKj5lv0gnRkPaoR5QaEzSFJZH4iDfrZw0XQE9E88tGSH 44v/JnhlkvF8hEkDM+Sk7qTWiXmlZaLYGBiIlIPM0STqkAqSh4ai87XB9JFAatjsB2bnsgZ9 Tl4ncfYpTHFnEH7sL91vxFwS0mSNEDdkVPNCSDXXce7lyUqf5ZwqhnH4Y5f0YAwo45K7W9yG fMwCjwnUiqs3vuMwo20eO0yq+4MB867M9ZK0p1g5Wmx4fcORJnCR+PB5MNC3Sd2jcdLdRrcT 5NHM3w1Nk2GOkARfAdMYH49tL7Aan3XWjtUsl+K44Ew5HDe1ldZ27nxKtvFPNeNQK25m27I+ zmYpzmlU3n2MvTG8hvfzHS1gNTguh7kG9ouPaeJ/M9D1Qj7Kms7V0RNCArTTeOCokK3Rd93M UEf/Ssq668o+ySDXtT7GhG1vnOAlhodQMZLVf037hmXzajZ6BrfAXILJgOtc/R/6pVwH2Zzk AbUwZW5XlSDrYF5V1q/+p2olxzrIBIXEmAwYXEpZyQu5Ins9dRbYg30cjpzLEKkpoSrR2Cok 2vT/XlWa6Y71pBSifjilbzTq3f1/8WSEFZojunCdjj9hj6VcrJJcGBBBbLzyf9bZLiUQVCa1 JTvs5jPtbteZX1hecHkfQnsIF1Kz6zfWNEkqQQzd6TNDhz0k5NjFKgJiN2EGG9nM9wfZRjia 1LJtAVa6fd7ZSX0PPcqPtzuUZp0ksAM8OgJsdiNPrJzjmVZLlfbrEmCm2bNt4wSrKTcuf5mY srKGSpdJX0bFb5m3FKLqxQ1iNcWK+FX7T27eK0XODz9jObOPyfME+9t3ZnnRrlR0Z5oaT79q 753X/ZmAT0GOAEiSkE7KbIuEG0=
  • Ironport-hdrordr: A9a23:EGspqaik0FwumPxo0MLjia6tSnBQXtwji2hC6mlwRA09TySZ// rAoB19726TtN9xYgBGpTnuAsi9qB/nmKKdpLNhX4tKPzOW3FdATrsD0WKK+VSJcEfDH6xmpM JdmsBFeaTN5DNB4/oSjjPVLz9Z+qjlzJyV
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

From: Edwin Török <edvin.torok@xxxxxxxxxx>

Unhandled exceptions go to stderr by default, but this doesn't typically work
for oxenstored because:
 * daemonize reopens stderr as /dev/null
 * systemd redirects stderr to /dev/null too

Debugging an unhandled exception requires reproducing the issue locally when
using --no-fork, and is not conducive to figuring out what went wrong on a
remote system.

Install a custom handler which also tries to render the backtrace to the
configured syslog facility, and DAEMON|ERR otherwise.

Signed-off-by: Edwin Török <edvin.torok@xxxxxxxxxx>
Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
Acked-by: Christian Lindig <christian.lindig@xxxxxxxxxx>
---
CC: Christian Lindig <christian.lindig@xxxxxxxxxx>
CC: David Scott <dave@xxxxxxxxxx>
CC: Edwin Torok <edvin.torok@xxxxxxxxxx>
CC: Rob Hoes <Rob.Hoes@xxxxxxxxxx>

Drop print_flush as prerr_endline already flushes.
---
 tools/ocaml/xenstored/logging.ml   | 29 +++++++++++++++++++++++++++++
 tools/ocaml/xenstored/xenstored.ml |  3 ++-
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/tools/ocaml/xenstored/logging.ml b/tools/ocaml/xenstored/logging.ml
index 39c3036155a2..255051437d60 100644
--- a/tools/ocaml/xenstored/logging.ml
+++ b/tools/ocaml/xenstored/logging.ml
@@ -342,3 +342,32 @@ let xb_answer ~tid ~con ~ty data =
 let watch_not_fired ~con perms path =
        let data = Printf.sprintf "EPERM perms=[%s] path=%s" perms path in
        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)
+               (Printexc.raw_backtrace_to_string bt)
+
+let fallback_exception_handler exn bt =
+       (* stderr goes to /dev/null, so use the logger where possible,
+          but always print to stderr too, in case everything else fails,
+          e.g. this can be used to debug with --no-fork
+
+          this function should try not to raise exceptions, but if it does
+          the ocaml runtime should still print the exception, both the 
original,
+          and the one from this function, but to stderr this time
+        *)
+       let msg = msg_of exn bt in
+       prerr_endline msg;
+       (* See Printexc.set_uncaught_exception_handler, need to flush,
+          so has to call stop and flush *)
+       match !xenstored_logger with
+       | Some l -> error "xenstored-fallback" "%s" msg; l.stop ()
+       | None ->
+               (* Too early, no logger set yet.
+                  We normally try to use the configured logger so we don't 
flood syslog
+                  during development for example, or if the user has a file set
+                *)
+               try Syslog.log Syslog.Daemon Syslog.Err msg
+               with e ->
+                       let bt = Printexc.get_raw_backtrace () in
+                       prerr_endline @@ msg_of e bt
diff --git a/tools/ocaml/xenstored/xenstored.ml 
b/tools/ocaml/xenstored/xenstored.ml
index 23621bd49397..257481285f05 100644
--- a/tools/ocaml/xenstored/xenstored.ml
+++ b/tools/ocaml/xenstored/xenstored.ml
@@ -357,7 +357,8 @@ let tweak_gc () =
        Gc.set { (Gc.get ()) with Gc.max_overhead = !Define.gc_max_overhead }
 
 
-let _ =
+let () =
+       Printexc.set_uncaught_exception_handler 
Logging.fallback_exception_handler;
        let cf = do_argv in
        let pidfile =
                if Sys.file_exists (config_filename cf) then
-- 
2.11.0




 


Rackspace

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