1 /*
2 * Copyright (c) 2021-2022 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 /* This files contains process module. */
17
18 #include "dfx_process.h"
19
20 #include <climits>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 #include <dirent.h>
25 #include <securec.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <vector>
29 #include "dfx_config.h"
30 #include "dfx_define.h"
31 #include "dfx_define.h"
32 #include "dfx_logger.h"
33 #include "dfx_maps.h"
34 #include "dfx_ring_buffer_wrapper.h"
35 #include "dfx_signal.h"
36 #include "dfx_thread.h"
37 #include "dfx_util.h"
38
39 namespace OHOS {
40 namespace HiviewDFX {
41 static const int ARGS_COUNT_TWO = 2;
42
FillProcessName()43 void DfxProcess::FillProcessName()
44 {
45 char path[NAME_LEN] = "\0";
46 if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "/proc/%d/cmdline", pid_) <= 0) {
47 return;
48 }
49
50 ReadStringFromFile(path, processName_, NAME_LEN);
51 }
52
CreateProcessWithKeyThread(pid_t pid,std::shared_ptr<DfxThread> keyThread)53 std::shared_ptr<DfxProcess> DfxProcess::CreateProcessWithKeyThread(pid_t pid, std::shared_ptr<DfxThread> keyThread)
54 {
55 auto dfxProcess = std::make_shared<DfxProcess>();
56 dfxProcess->SetPid(pid);
57 dfxProcess->FillProcessName();
58
59 if (!dfxProcess->InitProcessMaps()) {
60 DfxLogWarn("Fail to init process maps.");
61 return nullptr;
62 }
63 if (!dfxProcess->InitProcessThreads(keyThread)) {
64 DfxLogWarn("Fail to init threads.");
65 return nullptr;
66 }
67
68 DfxLogDebug("Init process dump with pid:%d.", dfxProcess->GetPid());
69 return dfxProcess;
70 }
71
InitProcessMaps()72 bool DfxProcess::InitProcessMaps()
73 {
74 auto maps = DfxElfMaps::Create(pid_);
75 if (!maps) {
76 return false;
77 }
78
79 SetMaps(maps);
80 return true;
81 }
82
InitProcessThreads(std::shared_ptr<DfxThread> keyThread)83 bool DfxProcess::InitProcessThreads(std::shared_ptr<DfxThread> keyThread)
84 {
85 if (!keyThread) {
86 keyThread = std::make_shared<DfxThread>(pid_, pid_, pid_);
87 }
88
89 if (!keyThread->Attach()) {
90 DfxLogWarn("Fail to attach thread.");
91 return false;
92 }
93 threads_.push_back(keyThread);
94 return true;
95 }
96
SetRecycleTid(pid_t nstid)97 void DfxProcess::SetRecycleTid(pid_t nstid)
98 {
99 recycleTid_ = nstid;
100 }
101
InitOtherThreads(bool attach)102 bool DfxProcess::InitOtherThreads(bool attach)
103 {
104 char path[NAME_LEN] = {0};
105 if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "/proc/%d/task", GetPid()) <= 0) {
106 return false;
107 }
108
109 char realPath[PATH_MAX];
110 if (!realpath(path, realPath)) {
111 return false;
112 }
113
114 DIR *dir = opendir(realPath);
115 if (!dir) {
116 return false;
117 }
118
119 struct dirent *ent;
120 while ((ent = readdir(dir))) {
121 if ((strcmp(ent->d_name, ".") == 0) || (strcmp(ent->d_name, "..") == 0)) {
122 continue;
123 }
124
125 pid_t tid = atoi(ent->d_name);
126 if (tid == 0) {
127 continue;
128 }
129
130 pid_t nstid = tid;
131 if (GetNs()) {
132 TidToNstid(tid, nstid);
133 }
134
135 if (isSignalDump_ && (nstid == recycleTid_)) {
136 DfxLogDebug("skip recycle tid:%d nstid:%d.", recycleTid_, nstid);
137 continue;
138 }
139
140 InsertThreadNode(tid, nstid, attach);
141 }
142 closedir(dir);
143 return true;
144 }
145
InsertThreadNode(pid_t tid,pid_t nsTid,bool attach)146 void DfxProcess::InsertThreadNode(pid_t tid, pid_t nsTid, bool attach)
147 {
148 for (auto iter = threads_.begin(); iter != threads_.end(); iter++) {
149 if ((*iter)->GetRealTid() == nsTid) {
150 (*iter)->SetThreadId(tid);
151 (*iter)->ReadThreadName();
152 return;
153 }
154 }
155
156 auto thread = std::make_shared<DfxThread>(pid_, tid, nsTid);
157 if (attach) {
158 thread->Attach();
159 }
160 threads_.push_back(thread);
161 }
162
TidToNstid(const int tid,int & nstid)163 int DfxProcess::TidToNstid(const int tid, int& nstid)
164 {
165 char path[NAME_LEN];
166 (void)memset_s(path, sizeof(path), '\0', sizeof(path));
167 if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "/proc/%d/task/%d/status", pid_, tid) <= 0) {
168 DfxLogWarn("snprintf_s error.");
169 return -1;
170 }
171
172 char buf[STATUS_LINE_SIZE];
173 FILE *fp = fopen(path, "r");
174 if (fp == nullptr) {
175 return -1;
176 }
177
178 int p = 0;
179 int t = 0;
180 while (!feof(fp)) {
181 if (fgets(buf, STATUS_LINE_SIZE, fp) == NULL) {
182 fclose(fp);
183 return -1;
184 }
185
186 // NSpid: 1892 1
187 if (strncmp(buf, NSPID_STR_NAME, strlen(NSPID_STR_NAME)) == 0) {
188 if (sscanf_s(buf, "%*[^0-9]%d%*[^0-9]%d", &p, &t) != ARGS_COUNT_TWO) {
189 DfxLogWarn("TidToNstid sscanf_s failed. pid:%d, tid:%d", pid_, tid);
190 }
191 nstid = t;
192 break;
193 }
194 }
195 (void)fclose(fp);
196 return 0;
197 }
198
SetIsSignalDump(bool isSignalDump)199 void DfxProcess::SetIsSignalDump(bool isSignalDump)
200 {
201 isSignalDump_ = isSignalDump;
202 }
203
GetIsSignalDump() const204 bool DfxProcess::GetIsSignalDump() const
205 {
206 return isSignalDump_;
207 }
208
GetPid() const209 pid_t DfxProcess::GetPid() const
210 {
211 return pid_;
212 }
213
GetUid() const214 uid_t DfxProcess::GetUid() const
215 {
216 return uid_;
217 }
218
GetNs() const219 bool DfxProcess::GetNs() const
220 {
221 return pid_ != nsPid_;
222 }
223
GetProcessName() const224 std::string DfxProcess::GetProcessName() const
225 {
226 return processName_;
227 }
228
GetFatalMessage() const229 std::string DfxProcess::GetFatalMessage() const
230 {
231 return fatalMsg_;
232 }
233
GetMaps() const234 std::shared_ptr<DfxElfMaps> DfxProcess::GetMaps() const
235 {
236 return maps_;
237 }
238
GetThreads() const239 std::vector<std::shared_ptr<DfxThread>> DfxProcess::GetThreads() const
240 {
241 return threads_;
242 }
243
SetPid(pid_t pid)244 void DfxProcess::SetPid(pid_t pid)
245 {
246 pid_ = pid;
247 }
248
SetUid(uid_t uid)249 void DfxProcess::SetUid(uid_t uid)
250 {
251 uid_ = uid;
252 }
253
SetNsPid(pid_t pid)254 void DfxProcess::SetNsPid(pid_t pid)
255 {
256 nsPid_ = pid;
257 }
258
GetNsPid() const259 pid_t DfxProcess::GetNsPid() const
260 {
261 if (nsPid_ > 0) {
262 return nsPid_;
263 }
264 return pid_;
265 }
266
SetProcessName(const std::string & processName)267 void DfxProcess::SetProcessName(const std::string &processName)
268 {
269 processName_ = processName;
270 }
271
SetFatalMessage(const std::string & msg)272 void DfxProcess::SetFatalMessage(const std::string &msg)
273 {
274 fatalMsg_ = msg;
275 }
276
SetMaps(std::shared_ptr<DfxElfMaps> maps)277 void DfxProcess::SetMaps(std::shared_ptr<DfxElfMaps> maps)
278 {
279 maps_ = maps;
280 }
281
SetThreads(const std::vector<std::shared_ptr<DfxThread>> & threads)282 void DfxProcess::SetThreads(const std::vector<std::shared_ptr<DfxThread>> &threads)
283 {
284 threads_ = threads;
285 }
286
Detach()287 void DfxProcess::Detach()
288 {
289 if (threads_.empty()) {
290 return;
291 }
292
293 for (auto iter = threads_.begin(); iter != threads_.end(); iter++) {
294 (*iter)->Detach();
295 }
296 }
297
PrintProcessMapsByConfig()298 void DfxProcess::PrintProcessMapsByConfig()
299 {
300 if (DfxConfig::GetInstance().GetDisplayMaps()) {
301 if (GetMaps()) {
302 DfxRingBufferWrapper::GetInstance().AppendMsg("\nMaps:\n");
303 }
304 auto mapsVector = maps_->GetValues();
305 for (auto iter = mapsVector.begin(); iter != mapsVector.end(); iter++) {
306 DfxRingBufferWrapper::GetInstance().AppendMsg((*iter)->PrintMap());
307 }
308 } else {
309 DfxLogDebug("hidden Maps");
310 }
311 }
312
PrintThreadsHeaderByConfig()313 void DfxProcess::PrintThreadsHeaderByConfig()
314 {
315 if (DfxConfig::GetInstance().GetDisplayBacktrace()) {
316 if (!isSignalDump_) {
317 DfxRingBufferWrapper::GetInstance().AppendMsg("Other thread info:\n");
318 }
319 } else {
320 DfxLogDebug("hidden thread info.");
321 }
322 }
323 } // namespace HiviewDFX
324 } // namespace OHOS
325