1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#ifndef DFX_PROCINFO_H
17800b99b8Sopenharmony_ci#define DFX_PROCINFO_H
18800b99b8Sopenharmony_ci
19800b99b8Sopenharmony_ci#include <cstdio>
20800b99b8Sopenharmony_ci#include <functional>
21800b99b8Sopenharmony_ci#include <memory>
22800b99b8Sopenharmony_ci#include <string>
23800b99b8Sopenharmony_ci#include <vector>
24800b99b8Sopenharmony_ci
25800b99b8Sopenharmony_cinamespace OHOS {
26800b99b8Sopenharmony_cinamespace HiviewDFX {
27800b99b8Sopenharmony_ci/**
28800b99b8Sopenharmony_ci * @brief  process information
29800b99b8Sopenharmony_ci*/
30800b99b8Sopenharmony_citypedef struct ProcInfo {
31800b99b8Sopenharmony_ci    /** process id */
32800b99b8Sopenharmony_ci    int pid;
33800b99b8Sopenharmony_ci    /** parent process id */
34800b99b8Sopenharmony_ci    int ppid;
35800b99b8Sopenharmony_ci    /** namespace process id */
36800b99b8Sopenharmony_ci    int nsPid;
37800b99b8Sopenharmony_ci    /** namespace is enabled or not */
38800b99b8Sopenharmony_ci    bool ns = false;
39800b99b8Sopenharmony_ci} ProcInfo;
40800b99b8Sopenharmony_ci
41800b99b8Sopenharmony_ci/**
42800b99b8Sopenharmony_ci * @brief Get process status
43800b99b8Sopenharmony_ci *
44800b99b8Sopenharmony_ci * @param procInfo structure containing information about process(output parameter)
45800b99b8Sopenharmony_ci * @return if succeed return true, otherwise return false
46800b99b8Sopenharmony_ci*/
47800b99b8Sopenharmony_cibool GetProcStatus(struct ProcInfo& procInfo);
48800b99b8Sopenharmony_ci/**
49800b99b8Sopenharmony_ci * @brief Get process status by specified process id
50800b99b8Sopenharmony_ci *
51800b99b8Sopenharmony_ci * @param realPid real process id
52800b99b8Sopenharmony_ci * @param procInfo structure containing information about process(output parameter)
53800b99b8Sopenharmony_ci * @return if succeed return true, otherwise return false
54800b99b8Sopenharmony_ci*/
55800b99b8Sopenharmony_cibool GetProcStatusByPid(int realPid, struct ProcInfo& procInfo);
56800b99b8Sopenharmony_ci/**
57800b99b8Sopenharmony_ci * @brief convert real tid to namespace tid
58800b99b8Sopenharmony_ci *
59800b99b8Sopenharmony_ci * @param pid process id
60800b99b8Sopenharmony_ci * @param tid thread id
61800b99b8Sopenharmony_ci * @param nsTid namespace tid(output parameter)
62800b99b8Sopenharmony_ci * @return if succeed return true, otherwise return false
63800b99b8Sopenharmony_ci*/
64800b99b8Sopenharmony_cibool TidToNstid(const int pid, const int tid, int& nstid);
65800b99b8Sopenharmony_ci/**
66800b99b8Sopenharmony_ci * @brief convert real tid to namespace tid
67800b99b8Sopenharmony_ci *
68800b99b8Sopenharmony_ci * @param pid process id
69800b99b8Sopenharmony_ci * @param tid thread id
70800b99b8Sopenharmony_ci * @return if succeed return true, otherwise return false
71800b99b8Sopenharmony_ci*/
72800b99b8Sopenharmony_cibool IsThreadInPid(int32_t pid, int32_t tid);
73800b99b8Sopenharmony_ci/**
74800b99b8Sopenharmony_ci * @brief Get thread id by process id and function
75800b99b8Sopenharmony_ci *
76800b99b8Sopenharmony_ci * @param pid process id
77800b99b8Sopenharmony_ci * @param tids thread ids(output parameter)
78800b99b8Sopenharmony_ci * @param func function
79800b99b8Sopenharmony_ci * @return if succeed return true, otherwise return false
80800b99b8Sopenharmony_ci*/
81800b99b8Sopenharmony_cibool GetTidsByPidWithFunc(const int pid, std::vector<int>& tids, std::function<bool(int)> const& func);
82800b99b8Sopenharmony_ci/**
83800b99b8Sopenharmony_ci * @brief Get thread ids and namespace thread ids by process id
84800b99b8Sopenharmony_ci *
85800b99b8Sopenharmony_ci * @param pid process id
86800b99b8Sopenharmony_ci * @param tids thread ids(output parameter)
87800b99b8Sopenharmony_ci * @param nsTid namespace tids(output parameter)
88800b99b8Sopenharmony_ci * @return if succeed return true, otherwise return false
89800b99b8Sopenharmony_ci*/
90800b99b8Sopenharmony_cibool GetTidsByPid(const int pid, std::vector<int>& tids, std::vector<int>& nstids);
91800b99b8Sopenharmony_ci/**
92800b99b8Sopenharmony_ci * @brief read thread name by id
93800b99b8Sopenharmony_ci * @param tid thread id
94800b99b8Sopenharmony_ci * @param str thread name
95800b99b8Sopenharmony_ci*/
96800b99b8Sopenharmony_civoid ReadThreadName(const int tid, std::string& str);
97800b99b8Sopenharmony_ci/**
98800b99b8Sopenharmony_ci * @brief read thread name by id
99800b99b8Sopenharmony_ci * @param tid thread id
100800b99b8Sopenharmony_ci * @param str thread name
101800b99b8Sopenharmony_ci*/
102800b99b8Sopenharmony_civoid ReadThreadNameByPidAndTid(const int pid, const int tid, std::string& str);
103800b99b8Sopenharmony_ci/**
104800b99b8Sopenharmony_ci * @brief read process name by id
105800b99b8Sopenharmony_ci * @param pid process id
106800b99b8Sopenharmony_ci * @param str process name
107800b99b8Sopenharmony_ci*/
108800b99b8Sopenharmony_civoid ReadProcessName(const int pid, std::string& str);
109800b99b8Sopenharmony_ci
110800b99b8Sopenharmony_ci/**
111800b99b8Sopenharmony_ci * @brief read process status by id
112800b99b8Sopenharmony_ci * @param result content of status
113800b99b8Sopenharmony_ci * @param pid process id
114800b99b8Sopenharmony_ci * @param withThreadName whether output thread name or not
115800b99b8Sopenharmony_ci*/
116800b99b8Sopenharmony_civoid ReadProcessStatus(std::string& result, const int pid);
117800b99b8Sopenharmony_ci
118800b99b8Sopenharmony_ci/**
119800b99b8Sopenharmony_ci * @brief read process wchan by id
120800b99b8Sopenharmony_ci * @param result content of wchan
121800b99b8Sopenharmony_ci * @param pid process id
122800b99b8Sopenharmony_ci * @param onlyPid if only print process wchan
123800b99b8Sopenharmony_ci * @param withThreadName whether output thread name or not
124800b99b8Sopenharmony_ci*/
125800b99b8Sopenharmony_civoid ReadProcessWchan(std::string& result, const int pid, bool onlyPid = false, bool withThreadName = false);
126800b99b8Sopenharmony_ci
127800b99b8Sopenharmony_ci/**
128800b99b8Sopenharmony_ci * @brief read thread wchan by id
129800b99b8Sopenharmony_ci * @param result content of wchan
130800b99b8Sopenharmony_ci * @param tid thread id
131800b99b8Sopenharmony_ci * @param withThreadName whether output thread name or not
132800b99b8Sopenharmony_ci*/
133800b99b8Sopenharmony_civoid ReadThreadWchan(std::string& result, const int tid, bool withThreadName = false);
134800b99b8Sopenharmony_ci/**
135800b99b8Sopenharmony_ci * @brief Get stacktrace head info
136800b99b8Sopenharmony_ci*/
137800b99b8Sopenharmony_cistd::string GetStacktraceHeader();
138800b99b8Sopenharmony_ci} // nameapace HiviewDFX
139800b99b8Sopenharmony_ci} // nameapace OHOS
140800b99b8Sopenharmony_ci#endif
141