1 /*
2  * Copyright (c) 2021-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 
16 #ifndef DFX_PROCESS_H
17 #define DFX_PROCESS_H
18 
19 #include <cinttypes>
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include "dfx_regs.h"
25 #include "dfx_thread.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 struct DfxProcessInfo {
30     pid_t pid = 0;
31     pid_t nsPid = 0;
32     uid_t uid = 0;
33     std::string processName = "";
34 };
35 
36 class DfxProcess {
37 public:
38     static std::shared_ptr<DfxProcess> Create(pid_t pid, pid_t nsPid);
39     DfxProcess(pid_t pid, pid_t nsPid);
40     virtual ~DfxProcess() = default;
41     void Attach(bool hasKey = false);
42     void Detach();
43 
44     bool InitOtherThreads(bool attach = false);
45     std::vector<std::shared_ptr<DfxThread>>& GetOtherThreads();
46     void ClearOtherThreads();
47     pid_t ChangeTid(pid_t tid, bool ns);
48 
49     void SetFatalMessage(const std::string &msg);
50     std::string GetFatalMessage() const;
51     static std::string GetProcessLifeCycle(pid_t pid);
52 
53     DfxProcessInfo processInfo_;
54     pid_t recycleTid_ = 0;
55     std::shared_ptr<DfxThread> keyThread_ = nullptr; // comment: crash thread or dump target thread
56     std::shared_ptr<DfxThread> vmThread_ = nullptr; // comment: vm thread object in crash scenario
57     std::string reason = "";
58     std::string openFiles = "";
59     std::shared_ptr<DfxRegs> regs_;
60 private:
61     DfxProcess() = default;
62     void InitProcessInfo(pid_t pid, pid_t nsPid);
63 
64     std::string fatalMsg_ = "";
65     std::vector<std::shared_ptr<DfxThread>> otherThreads_;
66     std::map<int, int> kvThreads_;
67 };
68 } // namespace HiviewDFX
69 } // namespace OHOS
70 #endif
71