--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "dotnet_signal_handler.h"
+
+#include "log.h"
+
+#include <cstdio>
+#include <vector>
+
+#include <signal.h>
+#include <iostream>
+#include <exception>
+#include <execinfo.h>
+
+using namespace std;
+
+#define CALLSTACK_SIZE 30
+
+std::vector<std::string> backtraceStr;
+char **backtraceStrings;
+int backtracePtrs;
+
+static void printBacktrace()
+{
+ if (backtraceStr.size() > 0) {
+ _ERR("Backtrace returned %d addresses :", backtraceStr.size());
+
+ for (unsigned int i = 0; i < backtraceStr.size(); i++) {
+ _ERR("[Backtrace #%d] : %s", i, backtraceStr[i].c_str());
+ }
+ }
+
+ backtraceStr.clear();
+}
+
+static bool nativeCrashCheck()
+{
+ int i;
+ void *buf[CALLSTACK_SIZE + 1];
+
+ backtracePtrs = backtrace(buf, CALLSTACK_SIZE);
+ if (backtracePtrs == 0) {
+ _ERR("No backtrace captured");
+ return false;
+ }
+
+ backtraceStrings = backtrace_symbols(buf, backtracePtrs);
+
+ if (backtraceStrings == NULL) {
+ _ERR("No backtrace captured");
+ return false;
+ }
+
+ backtraceStr.clear();
+
+ for (i = backtracePtrs - 1; i >= 0; i--) {
+ if (strstr(backtraceStrings[i], "__default_rt_sa_restorer") != NULL)
+ return backtraceStr.size() > 0;
+
+ backtraceStr.push_back(std::string(backtraceStrings[i]));
+ }
+
+ return true;
+}
+
+static void onSigabrt(int signum)
+{
+ _ERR("<<< SIGABRT %d >>>", signum);
+
+ backtracePtrs = 0;
+ backtraceStrings = NULL;
+
+ if (nativeCrashCheck()) {
+ printBacktrace();
+#ifdef RAISE_DEFAULT_SIGNAL_HANDLER
+ SIG_DFL(signum);
+#endif
+ } else {
+ _ERR("Crash from managed code");
+ }
+
+ if (backtraceStrings != NULL) {
+ free(backtraceStrings);
+ backtraceStrings = NULL;
+ }
+
+ exit(0);
+}
+
+static void onSigsegv(int signum)
+{
+ _ERR("<<< SIGSEGV %d >>>", signum);
+
+ backtracePtrs = 0;
+ backtraceStrings = NULL;
+
+ if (nativeCrashCheck()) {
+ printBacktrace();
+#ifdef RAISE_DEFAULT_SIGNAL_HANDLER
+ SIG_DFL(signum);
+#endif
+ } else {
+ _ERR("Crash from managed code");
+ }
+
+ if (backtraceStrings != NULL) {
+ free(backtraceStrings);
+ backtraceStrings = NULL;
+ }
+
+ exit(0);
+}
+
+static void onSigtrap(int signum)
+{
+ _ERR("<<< SIGTRAP %d >>>", signum);
+
+ exit(0);
+}
+
+void registerSignalHandler()
+{
+ _DBG("Register signal handler");
+
+ if (SIG_ERR == signal(SIGABRT, &onSigabrt) ||
+ SIG_ERR == signal(SIGSEGV, &onSigsegv) ||
+ SIG_ERR == signal(SIGTRAP, &onSigtrap)) {
+ perror("[dotnet-launcher] signal register error\n");
+ _ERR("Signal register error");
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef __DOTNET_SIGNAL_HANDLER__
+#define __DOTNET_SIGNAL_HANDLER__
+
+void registerSignalHandler();
+
+#endif /* __DOTNET_SIGNAL_HANDLER__ */