1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2021-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_THREAD_H
17800b99b8Sopenharmony_ci#define DFX_THREAD_H
18800b99b8Sopenharmony_ci
19800b99b8Sopenharmony_ci#include <cstdint>
20800b99b8Sopenharmony_ci#include <string>
21800b99b8Sopenharmony_ci#include <sys/types.h>
22800b99b8Sopenharmony_ci#include <vector>
23800b99b8Sopenharmony_ci
24800b99b8Sopenharmony_ci#include "dfx_define.h"
25800b99b8Sopenharmony_ci#include "dfx_fault_stack.h"
26800b99b8Sopenharmony_ci#include "dfx_frame.h"
27800b99b8Sopenharmony_ci#include "dfx_maps.h"
28800b99b8Sopenharmony_ci#include "dfx_regs.h"
29800b99b8Sopenharmony_ci
30800b99b8Sopenharmony_cinamespace OHOS {
31800b99b8Sopenharmony_cinamespace HiviewDFX {
32800b99b8Sopenharmony_ci
33800b99b8Sopenharmony_cistruct DfxThreadInfo {
34800b99b8Sopenharmony_ci    pid_t pid = 0;
35800b99b8Sopenharmony_ci    pid_t tid = 0;
36800b99b8Sopenharmony_ci    pid_t nsTid = 0;
37800b99b8Sopenharmony_ci    std::string threadName = "";
38800b99b8Sopenharmony_ci};
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_ciclass DfxThread {
41800b99b8Sopenharmony_cipublic:
42800b99b8Sopenharmony_ci    static std::shared_ptr<DfxThread> Create(pid_t pid, pid_t tid, pid_t nsTid);
43800b99b8Sopenharmony_ci    DfxThread(pid_t pid, pid_t tid, pid_t nsTid);
44800b99b8Sopenharmony_ci    virtual ~DfxThread();
45800b99b8Sopenharmony_ci
46800b99b8Sopenharmony_ci    std::shared_ptr<DfxRegs> GetThreadRegs() const;
47800b99b8Sopenharmony_ci    void SetThreadRegs(const std::shared_ptr<DfxRegs> &regs);
48800b99b8Sopenharmony_ci    void AddFrame(DfxFrame& frame);
49800b99b8Sopenharmony_ci    const std::vector<DfxFrame>& GetFrames() const;
50800b99b8Sopenharmony_ci    void SetFrames(const std::vector<DfxFrame>& frames);
51800b99b8Sopenharmony_ci    void InitFaultStack(bool needParseStack = false);
52800b99b8Sopenharmony_ci    std::shared_ptr<FaultStack> GetFaultStack() const;
53800b99b8Sopenharmony_ci    std::string ToString() const;
54800b99b8Sopenharmony_ci
55800b99b8Sopenharmony_ci    void Detach();
56800b99b8Sopenharmony_ci    bool Attach(int timeout = PTRACE_ATTATCH_KEY_THREAD_TIMEOUT);
57800b99b8Sopenharmony_ci
58800b99b8Sopenharmony_ci    DfxThreadInfo threadInfo_;
59800b99b8Sopenharmony_ciprivate:
60800b99b8Sopenharmony_ci    enum class ThreadStatus {
61800b99b8Sopenharmony_ci        THREAD_STATUS_INVALID = -1,
62800b99b8Sopenharmony_ci        THREAD_STATUS_INIT = 0,
63800b99b8Sopenharmony_ci        THREAD_STATUS_ATTACHED = 1
64800b99b8Sopenharmony_ci    };
65800b99b8Sopenharmony_ci
66800b99b8Sopenharmony_ci    DfxThread() = default;
67800b99b8Sopenharmony_ci    void InitThreadInfo(pid_t pid, pid_t tid, pid_t nsTid);
68800b99b8Sopenharmony_ci
69800b99b8Sopenharmony_ci    ThreadStatus threadStatus = ThreadStatus::THREAD_STATUS_INVALID;
70800b99b8Sopenharmony_ci    std::shared_ptr<DfxRegs> regs_;
71800b99b8Sopenharmony_ci    std::vector<DfxFrame> frames_;
72800b99b8Sopenharmony_ci    std::shared_ptr<FaultStack> faultStack_;
73800b99b8Sopenharmony_ci};
74800b99b8Sopenharmony_ci} // namespace HiviewDFX
75800b99b8Sopenharmony_ci} // namespace OHOS
76800b99b8Sopenharmony_ci
77800b99b8Sopenharmony_ci#endif
78