From: Jérémie Galarneau Date: Thu, 8 Jun 2023 18:20:09 +0000 (-0400) Subject: Wrap main functions to handle uncaught exceptions X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=d5ed3e6f3a8b52d7f2a3e2277873e0f1914ba515;p=lttng-tools.git Wrap main functions to handle uncaught exceptions Coverity reports multiple instances where formatting facilities can throw (e.g. invalid format string). A wrapper to handle formatting exceptions is added in a follow-up change, but it is a good practice to catch exceptions at the top level nonetheless. Reported-by: Coverity Scan Signed-off-by: Jérémie Galarneau Change-Id: Ie55af338b9ef1f7d6e8825055cfc2e7037cdd80e --- diff --git a/src/bin/lttng/commands/start.cpp b/src/bin/lttng/commands/start.cpp index 2552062e5..29cff53c2 100644 --- a/src/bin/lttng/commands/start.cpp +++ b/src/bin/lttng/commands/start.cpp @@ -103,7 +103,7 @@ cmd_error_code start_tracing(const char *session_name) return CMD_SUCCESS; } -cmd_error_code start_tracing(const lttng::cli::session_spec& spec) noexcept +cmd_error_code start_tracing(const lttng::cli::session_spec& spec) { bool had_warning = false; bool had_error = false; diff --git a/src/bin/lttng/commands/stop.cpp b/src/bin/lttng/commands/stop.cpp index d2e0bee14..8b33bd7f4 100644 --- a/src/bin/lttng/commands/stop.cpp +++ b/src/bin/lttng/commands/stop.cpp @@ -132,7 +132,7 @@ cmd_error_code stop_tracing(const char *session_name) return CMD_SUCCESS; } -cmd_error_code stop_tracing(const lttng::cli::session_spec& spec) noexcept +cmd_error_code stop_tracing(const lttng::cli::session_spec& spec) { bool had_warning = false; bool had_error = false; diff --git a/src/bin/lttng/lttng.cpp b/src/bin/lttng/lttng.cpp index 5f6e0f66e..b84fe3407 100644 --- a/src/bin/lttng/lttng.cpp +++ b/src/bin/lttng/lttng.cpp @@ -460,7 +460,7 @@ error: /* * main */ -int main(int argc, char *argv[]) +static int _main(int argc, char *argv[]) { int ret; @@ -478,3 +478,13 @@ int main(int argc, char *argv[]) return 0; } + +int main(int argc, char **argv) +{ + try { + return _main(argc, argv); + } catch (const std::exception& e) { + ERR_FMT("Unhandled exception caught by client: %s", e.what()); + abort(); + } +} diff --git a/tests/regression/tools/trigger/utils/notification-client.cpp b/tests/regression/tools/trigger/utils/notification-client.cpp index f73382158..58ac01e6c 100644 --- a/tests/regression/tools/trigger/utils/notification-client.cpp +++ b/tests/regression/tools/trigger/utils/notification-client.cpp @@ -7,6 +7,8 @@ #include "utils.h" +#include + #include #include #include @@ -81,7 +83,7 @@ end: return names_match; } -int main(int argc, char **argv) +static int _main(int argc, char **argv) { int ret; int option; @@ -279,3 +281,13 @@ end: free(expected_trigger_name); return !!ret; } + +int main(int argc, char **argv) +{ + try { + return _main(argc, argv); + } catch (const std::exception& e) { + ERR_FMT("Unhandled exception caught by notification client: %s", e.what()); + abort(); + } +} diff --git a/tests/unit/test_action.cpp b/tests/unit/test_action.cpp index 7d0727eda..c46b8fcea 100644 --- a/tests/unit/test_action.cpp +++ b/tests/unit/test_action.cpp @@ -9,6 +9,7 @@ * */ +#include #include #include @@ -541,7 +542,7 @@ static void test_action_snapshot_session() lttng_payload_reset(&payload); } -int main() +static int _main() { plan_tests(NUM_TESTS); test_action_notify(); @@ -552,3 +553,13 @@ int main() test_action_snapshot_session(); return exit_status(); } + +int main() +{ + try { + return _main(); + } catch (const std::exception& e) { + ERR_FMT("Unhandled exception caught by action unit test: %s", e.what()); + abort(); + } +}