• 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 
InitExceptionHandler()27 void CrashHandler::InitExceptionHandler()
28 {
29     if (signal(SIGSEGV, ApplicationCrashHandler) == SIG_ERR) {
30         ELOG("InitExceptionHandler failed");
31         return;
32     }
33 }
34 
ApplicationCrashHandler(int signal)35 void CrashHandler::ApplicationCrashHandler(int signal)
36 {
37     const uint32_t MAX_STACK_SIZE = 128;
38     int8_t crashBeginLog[] = "[JsEngine Crash]Engine Crash Info Begin.\n";
39     write(STDERR_FILENO, crashBeginLog, sizeof(crashBeginLog) - 1);
40     int8_t stackIntLog[PublicMethods::MAX_ITOA_BIT] = {0};
41     uint32_t itoaLength = PublicMethods::Ulltoa(signal, stackIntLog);
42     int8_t signalLog[] = "[JsEngine Crash]Error: signal : 0x";
43     write(STDERR_FILENO, signalLog, sizeof(signalLog) - 1);
44     write(STDERR_FILENO, stackIntLog, itoaLength);
45 
46     // get void*'s for all entries on the stack
47     void *array[10];
48     size_t size = backtrace(array, MAX_STACK_SIZE);
49     // print out all the frames to stdout
50     backtrace_symbols_fd(array, size, STDERR_FILENO);
51 
52     int8_t crashEndLog[] = "\n[JsEngine Crash]Engine Crash Info End.\n";
53     write(STDERR_FILENO, crashEndLog, sizeof(crashEndLog) - 1);
54 }