• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (dfxProcess != nullptr) {
57         dfxProcess->SetPid(pid);
58         dfxProcess->FillProcessName();
59     }
60     if (!dfxProcess->InitProcessMaps()) {
61         DfxLogWarn("Fail to init process maps.");
62         return nullptr;
63     }
64     if (!dfxProcess->InitProcessThreads(keyThread)) {
65         DfxLogWarn("Fail to init threads.");
66         return nullptr;
67     }
68 
69     DfxLogDebug("Init process dump with pid:%d.", dfxProcess->GetPid());
70     return dfxProcess;
71 }
72 
InitProcessMaps()73 bool DfxProcess::InitProcessMaps()
74 {
75     auto maps = DfxElfMaps::Create(pid_);
76     if (!maps) {
77         return false;
78     }
79 
80     SetMaps(maps);
81     return true;
82 }
83 
InitProcessThreads(std::shared_ptr<DfxThread> keyThread)84 bool DfxProcess::InitProcessThreads(std::shared_ptr<DfxThread> keyThread)
85 {
86     if (!keyThread) {
87         keyThread = std::make_shared<DfxThread>(pid_, pid_, pid_);
88     }
89 
90     if (!keyThread->Attach()) {
91         DfxLogWarn("Fail to attach thread.");
92         return false;
93     }
94     threads_.push_back(keyThread);
95     return true;
96 }
97 
SetRecycleTid(pid_t nstid)98 void DfxProcess::SetRecycleTid(pid_t nstid)
99 {
100     recycleTid_ = nstid;
101 }
102 
InitOtherThreads(bool attach)103 bool DfxProcess::InitOtherThreads(bool attach)
104 {
105     char path[NAME_LEN] = {0};
106     if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "/proc/%d/task", GetPid()) <= 0) {
107         return false;
108     }
109 
110     char realPath[PATH_MAX];
111     if (!realpath(path, realPath)) {
112         return false;
113     }
114 
115     DIR *dir = opendir(realPath);
116     if (!dir) {
117         return false;
118     }
119 
120     struct dirent *ent;
121     while ((ent = readdir(dir))) {
122         if ((strcmp(ent->d_name, ".") == 0) || (strcmp(ent->d_name, "..") == 0)) {
123             continue;
124         }
125 
126         pid_t tid = atoi(ent->d_name);
127         if (tid == 0) {
128             continue;
129         }
130 
131         pid_t nstid = tid;
132         if (GetNs()) {
133             TidToNstid(tid, nstid);
134         }
135 
136         if (isSignalDump_ && (nstid == recycleTid_)) {
137             DfxLogDebug("skip recycle tid:%d nstid:%d.", recycleTid_, nstid);
138             continue;
139         }
140 
141         InsertThreadNode(tid, nstid, attach);
142     }
143     closedir(dir);
144     return true;
145 }
146 
InsertThreadNode(pid_t tid,pid_t nsTid,bool attach)147 void DfxProcess::InsertThreadNode(pid_t tid, pid_t nsTid, bool attach)
148 {
149     for (auto iter = threads_.begin(); iter != threads_.end(); iter++) {
150         if ((*iter)->GetRealTid() == nsTid) {
151             (*iter)->SetThreadId(tid);
152             (*iter)->ReadThreadName();
153             return;
154         }
155     }
156 
157     auto thread = std::make_shared<DfxThread>(pid_, tid, nsTid);
158     if (attach) {
159         thread->Attach();
160     }
161     threads_.push_back(thread);
162 }
163 
TidToNstid(const int tid,int & nstid)164 int DfxProcess::TidToNstid(const int tid, int& nstid)
165 {
166     char path[NAME_LEN];
167     (void)memset_s(path, sizeof(path), '\0', sizeof(path));
168     if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "/proc/%d/task/%d/status", pid_, tid) <= 0) {
169         DfxLogWarn("snprintf_s error.");
170         return -1;
171     }
172 
173     char buf[STATUS_LINE_SIZE];
174     FILE *fp = fopen(path, "r");
175     if (fp == nullptr) {
176         return -1;
177     }
178 
179     int p = 0, 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 
GetMaps() const229 std::shared_ptr<DfxElfMaps> DfxProcess::GetMaps() const
230 {
231     return maps_;
232 }
233 
GetThreads() const234 std::vector<std::shared_ptr<DfxThread>> DfxProcess::GetThreads() const
235 {
236     return threads_;
237 }
238 
SetPid(pid_t pid)239 void DfxProcess::SetPid(pid_t pid)
240 {
241     pid_ = pid;
242 }
243 
SetUid(uid_t uid)244 void DfxProcess::SetUid(uid_t uid)
245 {
246     uid_ = uid;
247 }
248 
SetNsPid(pid_t pid)249 void DfxProcess::SetNsPid(pid_t pid)
250 {
251     nsPid_ = pid;
252 }
253 
GetNsPid() const254 pid_t DfxProcess::GetNsPid() const
255 {
256     if (nsPid_ > 0) {
257         return nsPid_;
258     }
259     return pid_;
260 }
261 
SetProcessName(const std::string & processName)262 void DfxProcess::SetProcessName(const std::string &processName)
263 {
264     processName_ = processName;
265 }
266 
SetMaps(std::shared_ptr<DfxElfMaps> maps)267 void DfxProcess::SetMaps(std::shared_ptr<DfxElfMaps> maps)
268 {
269     maps_ = maps;
270 }
271 
SetThreads(const std::vector<std::shared_ptr<DfxThread>> & threads)272 void DfxProcess::SetThreads(const std::vector<std::shared_ptr<DfxThread>> &threads)
273 {
274     threads_ = threads;
275 }
276 
Detach()277 void DfxProcess::Detach()
278 {
279     if (threads_.empty()) {
280         return;
281     }
282 
283     for (auto iter = threads_.begin(); iter != threads_.end(); iter++) {
284         (*iter)->Detach();
285     }
286 }
287 
PrintProcessMapsByConfig()288 void DfxProcess::PrintProcessMapsByConfig()
289 {
290     if (DfxConfig::GetInstance().GetDisplayMaps()) {
291         if (GetMaps()) {
292             DfxRingBufferWrapper::GetInstance().AppendMsg("\nMaps:\n");
293         }
294         auto mapsVector = maps_->GetValues();
295         for (auto iter = mapsVector.begin(); iter != mapsVector.end(); iter++) {
296             DfxRingBufferWrapper::GetInstance().AppendMsg((*iter)->PrintMap());
297         }
298     } else {
299         DfxLogDebug("hidden Maps");
300     }
301 }
302 
PrintThreadsHeaderByConfig()303 void DfxProcess::PrintThreadsHeaderByConfig()
304 {
305     if (DfxConfig::GetInstance().GetDisplayBacktrace()) {
306         if (!isSignalDump_) {
307             DfxRingBufferWrapper::GetInstance().AppendMsg("Other thread info:\n");
308         }
309     } else {
310         DfxLogDebug("hidden thread info.");
311     }
312 }
313 } // namespace HiviewDFX
314 } // namespace OHOS
315