1 /*
2  * Copyright (c) 2022-2024 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 #include "dfx_crash_local_handler.h"
16 
17 #include <securec.h>
18 #include <csignal>
19 #include <sys/time.h>
20 #include <sys/ucontext.h>
21 #include <cinttypes>
22 #include <unistd.h>
23 #include <pthread.h>
24 #include <cerrno>
25 #include "dfx_log.h"
26 #include "dfx_cutil.h"
27 #include "dfx_signal.h"
28 #include "dfx_signalhandler_exception.h"
29 #include "faultloggerd_client.h"
30 #include "hisysevent.h"
31 #include "string_printf.h"
32 #include "unwinder.h"
33 
34 #ifdef LOG_DOMAIN
35 #undef LOG_DOMAIN
36 #define LOG_DOMAIN 0xD002D11
37 #endif
38 
39 #ifdef LOG_TAG
40 #undef LOG_TAG
41 #define LOG_TAG "DfxCrashLocalHandler"
42 #endif
43 
44 #define MAX_FRAME 64
45 #define BUF_SZ 512
46 #define MAPINFO_SIZE 256
47 #define TIME_DIV 1000
48 #define BUF_SZ_SMALL 256
49 
RequestOutputLogFile(const struct ProcessDumpRequest * request)50 static __attribute__((noinline)) int RequestOutputLogFile(const struct ProcessDumpRequest* request)
51 {
52     struct FaultLoggerdRequest faultloggerdRequest;
53     (void)memset_s(&faultloggerdRequest, sizeof(faultloggerdRequest), 0, sizeof(struct FaultLoggerdRequest));
54 
55     faultloggerdRequest.type = (int32_t)CPP_CRASH;
56     faultloggerdRequest.pid = request->pid;
57     faultloggerdRequest.tid = request->tid;
58     faultloggerdRequest.uid = request->uid;
59     faultloggerdRequest.time = request->timeStamp;
60     return RequestFileDescriptorEx(&faultloggerdRequest);
61 }
62 
PrintLog(int fd,const char * format,...)63 static __attribute__((noinline)) void PrintLog(int fd, const char *format, ...)
64 {
65     char buf[BUF_SZ] = {0};
66     va_list args;
67     va_start(args, format);
68     int size = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, args);
69     va_end(args);
70     if (size == -1) {
71         if (fd > 0) {
72             const char* error = "PrintLog vsnprintf_s fail\n";
73             (void)OHOS_TEMP_FAILURE_RETRY(write(fd, error, strlen(error)));
74         }
75         return;
76     }
77     DFXLOG_ERROR("%s", buf);
78     if (fd > 0) {
79         (void)OHOS_TEMP_FAILURE_RETRY(write(fd, buf, strlen(buf)));
80     }
81 }
82 
CrashLocalUnwind(const int fd,const struct ProcessDumpRequest * request,std::string & errMessage)83 __attribute__((noinline)) void CrashLocalUnwind(const int fd,
84                                                 const struct ProcessDumpRequest* request,
85                                                 std::string& errMessage)
86 {
87     if (request == nullptr) {
88         return;
89     }
90     std::string logContext = OHOS::HiviewDFX::StringPrintf("Tid:%d, Name:%s\n", request->tid, request->threadName);
91     OHOS::HiviewDFX::Unwinder unwind;
92     unwind.UnwindLocalWithContext(request->context);
93     logContext.append(unwind.GetFramesStr(unwind.GetFrames()));
94     errMessage += logContext;
95     auto regs = OHOS::HiviewDFX::DfxRegs::CreateFromUcontext(request->context);
96     logContext.append(regs->PrintRegs());
97     logContext.append("\nMaps:\n");
98     for (const auto &map : unwind.GetMaps()->GetMaps()) {
99         logContext.append(map->ToString());
100     }
101 
102     for (unsigned int i = 0; i < logContext.length(); i += BUF_SZ_SMALL) {
103         PrintLog(fd, "%s", logContext.substr(i, BUF_SZ_SMALL).c_str());
104     }
105 }
106 
107 // currently, only stacktrace is logged to faultloggerd
CrashLocalHandler(struct ProcessDumpRequest * request)108 void CrashLocalHandler(struct ProcessDumpRequest* request)
109 {
110     int fd = RequestOutputLogFile(request);
111     CrashLocalHandlerFd(fd, request);
112     if (fd >= 0) {
113         close(fd);
114     }
115 }
116 
PrintTimeStamp(const int fd,const struct ProcessDumpRequest * request)117 static void PrintTimeStamp(const int fd, const struct ProcessDumpRequest* request)
118 {
119     uint64_t currentTime = request->timeStamp;
120     char secBuf[BUF_SZ] = {0};
121     char printBuf[BUF_SZ] = {0};
122     time_t sec = static_cast<time_t>(currentTime / TIME_DIV);
123     uint64_t millisec = currentTime % TIME_DIV;
124     struct tm* t = localtime(&sec);
125     if (!t) {
126         return;
127     }
128     (void)strftime(secBuf, sizeof(secBuf) - 1, "%Y-%m-%d %H:%M:%S", t);
129     if (snprintf_s(printBuf, sizeof(printBuf), sizeof(printBuf) - 1,
130             "%s.%03u\n", secBuf, millisec) < 0) {
131         DFXLOG_ERROR("snprintf timestamp fail");
132         return;
133     }
134     PrintLog(fd, "Timestamp:%s", printBuf);
135 }
136 
CrashLocalHandlerFd(const int fd,struct ProcessDumpRequest * request)137 void CrashLocalHandlerFd(const int fd, struct ProcessDumpRequest* request)
138 {
139     if (request == nullptr) {
140         return;
141     }
142     PrintTimeStamp(fd, request);
143     PrintLog(fd, "Pid:%d\n", request->pid);
144     PrintLog(fd, "Uid:%d\n", request->uid);
145     PrintLog(fd, "Process name:%s\n", request->processName);
146     if (request->siginfo.si_pid == request->pid) {
147         request->siginfo.si_uid = request->uid;
148     }
149     std::string reason = OHOS::HiviewDFX::DfxSignal::PrintSignal(request->siginfo) + "\n";
150     std::string errMessage = reason;
151     PrintLog(fd, reason.c_str());
152     PrintLog(fd, "Fault thread info:\n");
153     CrashLocalUnwind(fd, request, errMessage);
154     HiSysEventWrite(
155         OHOS::HiviewDFX::HiSysEvent::Domain::RELIABILITY,
156         "CPP_CRASH_EXCEPTION",
157         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
158         "PROCESS_NAME", request->processName,
159         "PID", request->pid,
160         "UID", request->uid,
161         "HAPPEN_TIME", request->timeStamp,
162         "ERROR_CODE", CRASH_DUMP_LOCAL_REPORT,
163         "ERROR_MSG", errMessage);
164 }
165