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