1 /*
2  * Copyright (c) 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 #ifndef HIVIEW_PLUGINS_UNIFIED_COLLECTOR_DYNTRACE_INCLUDE_TRACE_CONTEXT_H
16 #define HIVIEW_PLUGINS_UNIFIED_COLLECTOR_DYNTRACE_INCLUDE_TRACE_CONTEXT_H
17 #include <cinttypes>
18 #include <memory>
19 
20 #include "app_caller_event.h"
21 #include "sys_event.h"
22 #include "plugin.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 class AppTraceState;
27 class AppTraceContext {
28 public:
29     AppTraceContext(std::shared_ptr<AppTraceState> state);
30     ~AppTraceContext() = default;
31 
32 public:
33     int32_t TransferTo(std::shared_ptr<AppTraceState> state);
34     void PublishStackEvent(SysEvent& sysEvent);
35     void Reset();
36 
37     friend class StartTraceState;
38     friend class DumpTraceState;
39     friend class StopTraceState;
40 private:
41     int32_t pid_;
42     int64_t traceBegin_;
43     bool isOpenTrace_;
44     bool isTraceOn_;
45     bool isDumpTrace_;
46     std::shared_ptr<AppTraceState> state_;
47 };
48 
49 class AppTraceState {
50 public:
AppTraceState(std::shared_ptr<AppTraceContext> context,std::shared_ptr<AppCallerEvent> appCallerEvent)51     AppTraceState(std::shared_ptr<AppTraceContext> context,
52         std::shared_ptr<AppCallerEvent> appCallerEvent) : appTraceContext_(context), appCallerEvent_(appCallerEvent) {};
~AppTraceState()53     virtual ~AppTraceState() {};
54 
55 public:
56     virtual int32_t GetState() = 0;
57     virtual bool Accept(std::shared_ptr<AppTraceState> preState) = 0;
58     virtual int32_t CaptureTrace() = 0;
59 
60     friend class StartTraceState;
61     friend class DumpTraceState;
62     friend class StopTraceState;
63 protected:
64     std::shared_ptr<AppTraceContext> appTraceContext_;
65     std::shared_ptr<AppCallerEvent> appCallerEvent_;
66 };
67 
68 class StartTraceState : public AppTraceState {
69 public:
StartTraceState(std::shared_ptr<AppTraceContext> context,std::shared_ptr<AppCallerEvent> appCallerEvent,std::shared_ptr<Plugin> plugin)70     StartTraceState(std::shared_ptr<AppTraceContext> context,
71         std::shared_ptr<AppCallerEvent> appCallerEvent,
72         std::shared_ptr<Plugin> plugin) : AppTraceState(context, appCallerEvent), plugin_(plugin) {};
~StartTraceState()73     ~StartTraceState() {};
74 
75 public:
76     bool Accept(std::shared_ptr<AppTraceState> preState) override;
77     int32_t CaptureTrace() override;
78     int32_t GetState() override;
79 
80 private:
81     int32_t DoCaptureTrace();
82 
83 private:
84     std::shared_ptr<Plugin> plugin_;
85 };
86 
87 class DumpTraceState : public AppTraceState {
88 public:
DumpTraceState(std::shared_ptr<AppTraceContext> context,std::shared_ptr<AppCallerEvent> appCallerEvent,std::shared_ptr<Plugin> plugin)89     DumpTraceState(std::shared_ptr<AppTraceContext> context,
90         std::shared_ptr<AppCallerEvent> appCallerEvent,
91         std::shared_ptr<Plugin> plugin) : AppTraceState(context, appCallerEvent), plugin_(plugin) {};
~DumpTraceState()92     ~DumpTraceState() {};
93 
94 public:
95     bool Accept(std::shared_ptr<AppTraceState> preState) override;
96     int32_t CaptureTrace() override;
97     int32_t GetState() override;
98 
99 private:
100     int32_t DoCaptureTrace();
101 
102 private:
103     std::shared_ptr<Plugin> plugin_;
104 };
105 
106 class StopTraceState : public AppTraceState {
107 public:
StopTraceState(std::shared_ptr<AppTraceContext> context,std::shared_ptr<AppCallerEvent> appCallerEvent)108     StopTraceState(std::shared_ptr<AppTraceContext> context,
109         std::shared_ptr<AppCallerEvent> appCallerEvent) : AppTraceState(context, appCallerEvent) {};
~StopTraceState()110     ~StopTraceState() {};
111 
112 public:
113     bool Accept(std::shared_ptr<AppTraceState> state) override;
114     int32_t CaptureTrace() override;
115     int32_t GetState() override;
116 
117 private:
118     int32_t DoCaptureTrace();
119 };
120 }; // HiviewDFX
121 }; // HiviewDFX
122 #endif // HIVIEW_PLUGINS_UNIFIED_COLLECTOR_DYNTRACE_INCLUDE_TRACE_CONTEXT_H