1 /*
2  * Copyright (C) 2021 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 "open_stacktrace_catcher.h"
16 
17 #include <ctime>
18 #include <string>
19 
20 #include "common_utils.h"
21 #include "file_util.h"
22 #include "log_catcher_utils.h"
23 #include "hiview_logger.h"
24 #include "securec.h"
25 #include <sys/wait.h>
26 #include <unistd.h>
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 DEFINE_LOG_LABEL(0xD002D01, "EventLogger-OpenStacktraceCatcher");
OpenStacktraceCatcher()31 OpenStacktraceCatcher::OpenStacktraceCatcher() : EventLogCatcher()
32 {
33     name_ = "OpenStacktraceCatcher";
34 }
35 
Initialize(const std::string & packageNam,int pid,int intParam)36 bool OpenStacktraceCatcher::Initialize(const std::string& packageNam, int pid, int intParam)
37 {
38     if (pid <= 0 && packageNam.length() == 0) {
39         description_ = "OpenStacktraceCatcher -- pid is null, packageName is null\n";
40         return false;
41     }
42     packageName_ = packageNam;
43     if (pid > 0) {
44         pid_ = pid;
45     } else {
46         pid_ = CommonUtils::GetPidByName(packageNam);
47     }
48 
49     if (pid_ <= 0) {
50         description_ = "OpenStacktraceCatcher -- packageName is " + packageName_ + " pid is null\n";
51         return false;
52     }
53 
54     if (packageName_.length() == 0) {
55         packageName_ = "(null)";
56     }
57 
58     description_ = "OpenStacktraceCatcher -- pid==" + std::to_string(pid_) + " packageName is " + packageName_ + "\n";
59     return EventLogCatcher::Initialize(packageName_, pid_, intParam);
60 };
61 
62 // may block, run in another thread
Catch(int fd,int jsonFd)63 int OpenStacktraceCatcher::Catch(int fd, int jsonFd)
64 {
65     if (pid_ <= 0) {
66         return 0;
67     }
68     int originSize = GetFdSize(fd);
69 
70 #ifdef DUMP_STACK_IN_PROCESS
71     LogCatcherUtils::DumpStacktrace(fd, pid_);
72 #else
73     ForkAndDumpStackTrace(fd);
74 #endif
75     logSize_ = GetFdSize(fd) - originSize;
76     return logSize_;
77 }
78 
WaitChildPid(pid_t pid)79 inline void OpenStacktraceCatcher::WaitChildPid(pid_t pid)
80 {
81     int retryCount = 0;
82     while (retryCount < MAX_RETRY_COUNT) {
83         if (waitpid(pid, NULL, WNOHANG) != 0) {
84             break;
85         }
86         retryCount++;
87         usleep(SLEEP_TIME_US);
88     }
89 }
90 
ForkAndDumpStackTrace(int32_t fd)91 int32_t OpenStacktraceCatcher::ForkAndDumpStackTrace(int32_t fd)
92 {
93     int pid = -1;
94     int leftTimeMicroSecond = 30000000; // 30000000us
95     if ((pid = fork()) < 0) {
96         HIVIEW_LOGE("Fork error, err:%{public}d", errno);
97         return 0;
98     }
99 
100     if (pid == 0) {
101         auto newFd = dup(fd);
102         int ret = LogCatcherUtils::DumpStacktrace(newFd, pid_);
103         HIVIEW_LOGD("LogCatcherUtils::DumpStacktrace ret %{public}d", ret);
104         close(newFd);
105         _exit(ret);
106     }
107 
108     while (true) {
109         int status = 0;
110         pid_t p = waitpid(pid, &status, WNOHANG);
111         if (p < 0) {
112             HIVIEW_LOGW("Waitpid return p=%{public}d, err:%{public}d", p, errno);
113             return -1;
114         }
115 
116         if (p == pid) {
117             HIVIEW_LOGD("Dump process exited status is %{public}d", status);
118             return WEXITSTATUS(status);
119         }
120 
121         if (needStop_ || leftTimeMicroSecond <= 0) {
122             HIVIEW_LOGW("Dump stacktrace timeout, killing pid %{public}d.", pid);
123             std::string str = "Dump stacktrace timeout, Catch for " + std::to_string(pid_);
124             FileUtil::SaveStringToFd(fd, str);
125             kill(pid, SIGKILL);
126             WaitChildPid(pid);
127             return -1;
128         }
129 
130         usleep(SLEEP_TIME_US); // poll every 0.1 sec
131         leftTimeMicroSecond -= SLEEP_TIME_US;
132     }
133 }
134 } // namespace HiviewDFX
135 } // namespace OHOS
136