• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "src/extendrt/signal_handler.h"
17 #include <execinfo.h>
18 #include <signal.h>
19 #include <map>
20 #include <string>
21 #include <vector>
22 #include "src/common/log_adapter.h"
23 
24 namespace mindspore {
25 namespace {
26 static std::map<int, std::string> kSigs = {
27   {SIGSEGV, "SIGSEGV"}, {SIGABRT, "SIGABRT"}, {SIGFPE, "SIGFPE"}, {SIGBUS, "SIGBUS"}, {SIGILL, "SIGILL"}};
28 
GetSigStr(int sig)29 std::string GetSigStr(int sig) { return kSigs[sig]; }
30 }  // namespace
31 
GetBackTrace(int sig)32 std::string GetBackTrace(int sig) {
33   constexpr int kFrameSize = 128;
34   void *frames_buf[kFrameSize];
35   auto frames_num = backtrace(frames_buf, kFrameSize);
36   char **frames = backtrace_symbols(frames_buf, frames_num);
37   if (frames == nullptr) {
38     MS_LOG(ERROR) << "backtrace_symbols failed!";
39     std::cerr << "backtrace_symbols failed!";
40     return "";
41   }
42 
43   std::stringstream frames_result;
44   frames_result << "Capture signal [" << GetSigStr(sig) << "]" << std::endl;
45   for (int i = 0; i < frames_num; ++i) {
46     frames_result << frames[i] << std::endl;
47   }
48 
49   free(frames);
50   return frames_result.str();
51 }
52 
SignalHandler(int sig)53 void SignalHandler(int sig) {
54   std::string stack_info = GetBackTrace(sig);
55   MS_LOG(ERROR) << stack_info;
56   std::cerr << stack_info << std::endl;
57   struct sigaction sa_default;
58   sigemptyset(&sa_default.sa_mask);
59   sa_default.sa_handler = SIG_DFL;
60   sigaction(SIGABRT, &sa_default, NULL);
61   abort();
62 }
63 
CaptureSignal()64 void CaptureSignal() {
65   static bool capture_already = false;
66   if (capture_already) {
67     return;
68   }
69   std::vector<int> sigs = {SIGSEGV, SIGABRT, SIGFPE, SIGBUS, SIGILL};
70   for (auto sig : sigs) {
71     struct sigaction sa;
72     sa.sa_handler = SignalHandler;
73     auto ret = sigaction(sig, &sa, NULL);
74     if (ret != 0) {
75       MS_LOG(ERROR) << "sigaction [" << sig << "] failed!";
76     }
77   }
78   capture_already = true;
79 }
80 }  // namespace mindspore
81