• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "CrashHandler.h"
17 
18 #include <csignal>
19 #include <cstdlib>
20 #include <execinfo.h>
21 #include <sstream>
22 #include <unistd.h>
23 
24 #include "PreviewerEngineLog.h"
25 #include "PublicMethods.h"
26 
27 using namespace std;
28 
InitExceptionHandler()29 void CrashHandler::InitExceptionHandler()
30 {
31     if (signal(SIGSEGV, ApplicationCrashHandler) == SIG_ERR) {
32         ELOG("InitExceptionHandler failed");
33         return;
34     }
35 }
36 
ApplicationCrashHandler(int signal)37 void CrashHandler::ApplicationCrashHandler(int signal)
38 {
39     const uint32_t MAX_STACK_SIZE = 128;
40     int8_t crashBeginLog[] = "[JsEngine Crash]Engine Crash Info Begin.\n";
41     write(STDERR_FILENO, crashBeginLog, sizeof(crashBeginLog) - 1);
42     int8_t stackIntLog[PublicMethods::MAX_ITOA_BIT] = {0};
43     uint32_t itoaLength = PublicMethods::Ulltoa(signal, stackIntLog);
44     int8_t signalLog[] = "[JsEngine Crash]Error: signal : 0x";
45     write(STDERR_FILENO, signalLog, sizeof(signalLog) - 1);
46     write(STDERR_FILENO, stackIntLog, itoaLength);
47 
48     // get void*'s for all entries on the stack
49     void *array[10];
50     size_t size = backtrace(array, MAX_STACK_SIZE);
51     // print out all the frames to stdout
52     backtrace_symbols_fd(array, size, STDERR_FILENO);
53 
54     int8_t crashEndLog[] = "\n[JsEngine Crash]Engine Crash Info End.\n";
55     write(STDERR_FILENO, crashEndLog, sizeof(crashEndLog) - 1);
56 }
57