Handle exit(127) 94/286594/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 10 Jan 2023 09:40:01 +0000 (09:40 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 10 Jan 2023 09:53:44 +0000 (09:53 +0000)
If the process is terminated with status 127, we prints the backtrace
for debugging.

Change-Id: Ice9f2eb47ed53a73adde1d0d7834583837677321
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
tizen-cpp/app-core-cpp/app_core_base.cc

index 4ee310c..ad7aa52 100644 (file)
@@ -20,6 +20,7 @@
 #include <bundle_internal.h>
 #include <dlfcn.h>
 #include <errno.h>
+#include <execinfo.h>
 #include <gio/gio.h>
 #include <glib.h>
 #include <libintl.h>
@@ -58,6 +59,49 @@ AppCoreBase* AppCoreBase::context_ = nullptr;
 
 namespace {
 
+constexpr const int BACKTRACE_BUFFER_SIZE = 128;
+
+class ExitHandler {
+ public:
+  ExitHandler() {
+    on_exit(OnExit, this);
+  }
+
+ private:
+  static void OnExit(int status, void *user_data) {
+    if (status != 127)
+      return;
+
+    char* buffer[BACKTRACE_BUFFER_SIZE];
+    int nptrs = backtrace(reinterpret_cast<void**>(buffer),
+        BACKTRACE_BUFFER_SIZE);
+    fprintf(stderr, "%s backtrace() returned %d addesses\n",
+        __FUNCTION__, nptrs);
+
+    // To print numbers, we use backtrace_symbols() instead of backtrace_symbols_fd().
+    // If there is an issues related to the memory, we cannot use backtrace_symbols().
+    // backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
+    char** strings = backtrace_symbols(reinterpret_cast<void**>(buffer), nptrs);
+    if (strings == nullptr) {
+      perror("backtrace_symbols");
+      return;
+    }
+
+    Dl_info info;
+    for (int i = 0; i < nptrs; ++i) {
+      dladdr(buffer[i], &info);
+      fprintf(stderr, "[%3d] %s %s\n",
+          i, info.dli_sname ? info.dli_sname : "?", strings[i]);
+    }
+    free(strings);
+
+    fprintf(stderr, "%s(%d) abort()\n", __FUNCTION__, __LINE__);
+    abort();
+  }
+};
+
+ExitHandler exit_handler;
+
 enum TizenProfile {
   Unknown = 0x00,
   Mobile = 0x01,