• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "trace_common_state.h"
17 
18 #include "hiview_logger.h"
19 #include "trace_state_machine.h"
20 
21 namespace OHOS::HiviewDFX {
22 namespace {
23 DEFINE_LOG_TAG("TraceStateMachine");
24 }
CommonState(bool isCachOn,int32_t totalFileSize,int32_t sliceMaxDuration)25 CommonState::CommonState(bool isCachOn, int32_t totalFileSize, int32_t sliceMaxDuration)
26     : totalFileSize_(totalFileSize), sliceMaxDuration_(sliceMaxDuration)
27 {
28     if (isCachOn) {
29         Hitrace::CacheTraceOn(totalFileSize_, sliceMaxDuration_);
30     }
31 }
32 
OpenTrace(TraceScenario scenario,const std::vector<std::string> & tagGroups)33 TraceRet CommonState::OpenTrace(TraceScenario scenario, const std::vector<std::string> &tagGroups)
34 {
35     if (scenario != TraceScenario::TRACE_COMMAND) {
36         HIVIEW_LOGW(":%{public}s, invoke state deny", GetTag().c_str());
37         return TraceRet(TraceStateCode::DENY);
38     }
39 
40     // If is command open, first close common state trace
41     if (auto closeRet = Hitrace::CloseTrace(); closeRet != TraceErrorCode::SUCCESS) {
42         HIVIEW_LOGE(":%{public}s, CloseTrace error:%{public}d", GetTag().c_str(), closeRet);
43         return TraceRet(closeRet);
44     }
45 
46     // Second open command state trace
47     return TraceBaseState::OpenTrace(TraceScenario::TRACE_COMMAND, tagGroups);
48 }
49 
OpenTrace(TraceScenario scenario,const std::string & args)50 TraceRet CommonState::OpenTrace(TraceScenario scenario, const std::string &args)
51 {
52     if (scenario != TraceScenario::TRACE_COMMAND) {
53         HIVIEW_LOGW(":%{public}s, invoke state deny", GetTag().c_str());
54         return TraceRet(TraceStateCode::DENY);
55     }
56 
57     // Is command open, first close common state trace
58     if (auto closeRet = Hitrace::CloseTrace(); closeRet != TraceErrorCode::SUCCESS) {
59         HIVIEW_LOGE(":%{public}s, CloseTrace error:%{public}d", GetTag().c_str(), closeRet);
60         return TraceRet(closeRet);
61     }
62 
63     // Second open command state trace
64     return TraceBaseState::OpenTrace(TraceScenario::TRACE_COMMAND, args);
65 }
66 
DumpTrace(TraceScenario scenario,uint32_t maxDuration,uint64_t happenTime,TraceRetInfo & info)67 TraceRet CommonState::DumpTrace(TraceScenario scenario, uint32_t maxDuration, uint64_t happenTime, TraceRetInfo &info)
68 {
69     if (scenario == TraceScenario::TRACE_COMMON || scenario == TraceScenario::TRACE_COMMAND) {
70         info = Hitrace::DumpTrace(maxDuration, happenTime);
71         HIVIEW_LOGI(":%{public}s, DumpTrace result:%{public}d", GetTag().c_str(), info.errorCode);
72         return TraceRet(info.errorCode);
73     }
74     HIVIEW_LOGW(":%{public}s, scenario:%{public}d is fail", GetTag().c_str(), static_cast<int>(scenario));
75     return TraceRet(TraceStateCode::FAIL);
76 }
77 
DumpTraceAsync(const DumpTraceArgs & args,int64_t fileSizeLimit,TraceRetInfo & info,DumpTraceCallback callback)78 TraceRet CommonState::DumpTraceAsync(const DumpTraceArgs &args, int64_t fileSizeLimit,
79     TraceRetInfo &info, DumpTraceCallback callback)
80 {
81     if (args.scenario != TraceScenario::TRACE_COMMON) {
82         HIVIEW_LOGW(":%{public}s, scenario:%{public}d is fail", GetTag().c_str(), static_cast<int>(args.scenario));
83         return TraceRet(TraceStateCode::FAIL);
84     }
85     info = Hitrace::DumpTraceAsync(args.maxDuration, args.happenTime, fileSizeLimit, callback);
86     HIVIEW_LOGI(":%{public}s, DumpTrace result:%{public}d", GetTag().c_str(), info.errorCode);
87     return TraceRet(info.errorCode);
88 }
89 
TraceDropOn(TraceScenario scenario)90 TraceRet CommonState::TraceDropOn(TraceScenario scenario)
91 {
92     if (scenario != TraceScenario::TRACE_COMMON) {
93         HIVIEW_LOGW(":%{public}s, scenario:%{public}d is fail", GetTag().c_str(), static_cast<int>(scenario));
94         return TraceRet(TraceStateCode::FAIL);
95     }
96     if (auto ret = Hitrace::RecordTraceOn(); ret != TraceErrorCode::SUCCESS) {
97         HIVIEW_LOGE(":%{public}s, TraceDropOn error:%{public}d", GetTag().c_str(), ret);
98         return TraceRet(ret);
99     }
100     TraceStateMachine::GetInstance().TransToCommonDropState();
101     return {};
102 }
103 
CloseTrace(TraceScenario scenario)104 TraceRet CommonState::CloseTrace(TraceScenario scenario)
105 {
106     if (scenario == TraceScenario::TRACE_COMMON) {
107         return TraceBaseState::CloseTrace(scenario);
108     }
109 
110     // beta version can close trace by trace command
111     if (scenario == TraceScenario::TRACE_COMMAND && !TraceStateMachine::GetInstance().GetCommandState()) {
112         // Prevent traceStateMachine recovery to beta common state after close
113         TraceStateMachine::GetInstance().CloseVersionBeta();
114         return TraceBaseState::CloseTrace(scenario);
115     }
116     HIVIEW_LOGW(":%{public}s, scenario:%{public}d is fail", GetTag().c_str(), static_cast<int>(scenario));
117     return TraceRet(TraceStateCode::FAIL);
118 }
119 
TraceCacheOn()120 TraceRet CommonState::TraceCacheOn()
121 {
122     return TraceRet(Hitrace::CacheTraceOn(totalFileSize_, sliceMaxDuration_));
123 }
124 
TraceCacheOff()125 TraceRet CommonState::TraceCacheOff()
126 {
127     return TraceRet(Hitrace::CacheTraceOff());
128 }
129 
SetCacheParams(int32_t totalFileSize,int32_t sliceMaxDuration)130 TraceRet CommonState::SetCacheParams(int32_t totalFileSize, int32_t sliceMaxDuration)
131 {
132     totalFileSize_ = totalFileSize;
133     sliceMaxDuration_ = sliceMaxDuration;
134     return {};
135 }
136 
OpenTrace(TraceScenario scenario,const std::vector<std::string> & tagGroups)137 TraceRet CommonDropState::OpenTrace(TraceScenario scenario, const std::vector<std::string> &tagGroups)
138 {
139     if (scenario != TraceScenario::TRACE_COMMAND) {
140         HIVIEW_LOGW(":%{public}s, scenario:%{public}d is deny", GetTag().c_str(), static_cast<int>(scenario));
141         return TraceRet(TraceStateCode::DENY);
142     }
143     if (auto dropRet = Hitrace::RecordTraceOff(); dropRet.errorCode != TraceErrorCode::SUCCESS) {
144         HIVIEW_LOGE(":%{public}s, DumpTraceOff error:%{public}d", GetTag().c_str(), dropRet.errorCode);
145         return TraceRet(dropRet.errorCode);
146     }
147     if (auto closeRet = Hitrace::CloseTrace(); closeRet != TraceErrorCode::SUCCESS) {
148         HIVIEW_LOGE(":%{public}s, CloseTrace error:%{public}d", GetTag().c_str(), closeRet);
149         TraceStateMachine::GetInstance().TransToCommonState();
150         return TraceRet(closeRet);
151     }
152     return TraceBaseState::OpenTrace(TraceScenario::TRACE_COMMAND, tagGroups);
153 }
154 
OpenTrace(TraceScenario scenario,const std::string & args)155 TraceRet CommonDropState::OpenTrace(TraceScenario scenario, const std::string &args)
156 {
157     if (scenario != TraceScenario::TRACE_COMMAND) {
158         HIVIEW_LOGW(":%{public}s, scenario:%{public}d is deny", GetTag().c_str(), static_cast<int>(scenario));
159         return TraceRet(TraceStateCode::DENY);
160     }
161     if (auto dropRet = Hitrace::RecordTraceOff(); dropRet.errorCode != TraceErrorCode::SUCCESS) {
162         HIVIEW_LOGE(":%{public}s, DumpTraceOff error:%{public}d", GetTag().c_str(), dropRet.errorCode);
163         return TraceRet(dropRet.errorCode);
164     }
165     if (auto closeRet = Hitrace::CloseTrace(); closeRet != TraceErrorCode::SUCCESS) {
166         HIVIEW_LOGE(":%{public}s, CloseTrace error:%{public}d", GetTag().c_str(), closeRet);
167         TraceStateMachine::GetInstance().TransToCommonState();
168         return TraceRet(closeRet);
169     }
170     return TraceBaseState::OpenTrace(TraceScenario::TRACE_COMMAND, args);
171 }
172 
TraceDropOff(TraceScenario scenario,TraceRetInfo & info)173 TraceRet CommonDropState::TraceDropOff(TraceScenario scenario, TraceRetInfo &info)
174 {
175     if (scenario != TraceScenario::TRACE_COMMON) {
176         HIVIEW_LOGW(":%{public}s, scenario:%{public}d is fail", GetTag().c_str(), static_cast<int>(scenario));
177         return TraceRet(TraceStateCode::FAIL);
178     }
179     if (info = Hitrace::RecordTraceOff(); info.errorCode != TraceErrorCode::SUCCESS) {
180         HIVIEW_LOGE(":%{public}s, TraceDropOff error:%{public}d", GetTag().c_str(), info.errorCode);
181         return TraceRet(info.errorCode);
182     }
183     TraceStateMachine::GetInstance().TransToCommonState();
184     return {};
185 }
186 
CloseTrace(TraceScenario scenario)187 TraceRet CommonDropState::CloseTrace(TraceScenario scenario)
188 {
189     return TraceRet(TraceStateCode::FAIL);
190 }
191 }
192