• 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 #include "trace_dynamic_state.h"
16 
17 #include "trace_state_machine.h"
18 #include "hiview_logger.h"
19 
20 namespace OHOS::HiviewDFX {
21 namespace {
22 DEFINE_LOG_TAG("TraceStateMachine");
23 const std::vector<std::string> TELEMETRY_TAG_GROUPS_DEFAULT = {"telemetry"};
24 }
OpenTrace(TraceScenario scenario,const std::vector<std::string> & tagGroups)25 TraceRet DynamicState::OpenTrace(TraceScenario scenario, const std::vector<std::string> &tagGroups)
26 {
27     if (auto closeRet = Hitrace::CloseTrace(); closeRet != TraceErrorCode::SUCCESS) {
28         HIVIEW_LOGE("%{public}s:  CloseTrace result:%{public}d", GetTag().c_str(), closeRet);
29         return TraceRet(closeRet);
30     }
31     return TraceBaseState::OpenTrace(scenario, tagGroups);
32 }
33 
OpenTrace(TraceScenario scenario,const std::string & args)34 TraceRet DynamicState::OpenTrace(TraceScenario scenario, const std::string &args)
35 {
36     if (auto closeRet = Hitrace::CloseTrace(); closeRet != TraceErrorCode::SUCCESS) {
37         HIVIEW_LOGE("%{public}s: CloseTrace result:%{public}d", GetTag().c_str(), closeRet);
38         return TraceRet(closeRet);
39     }
40     return TraceBaseState::OpenTrace(scenario, args);
41 }
42 
OpenTelemetryTrace(const std::string & args,TelemetryPolicy policy)43 TraceRet DynamicState::OpenTelemetryTrace(const std::string &args, TelemetryPolicy policy)
44 {
45     if (auto closeRet = Hitrace::CloseTrace(); closeRet != TraceErrorCode::SUCCESS) {
46         HIVIEW_LOGE("%{public}s:  CloseTrace result:%{public}d", GetTag().c_str(), closeRet);
47         return TraceRet(closeRet);
48     }
49     auto ret = args.empty() ? Hitrace::OpenTrace(TELEMETRY_TAG_GROUPS_DEFAULT) : Hitrace::OpenTrace(args);
50     HIVIEW_LOGI("%{public}s: args:%{public}s: result:%{public}d", GetTag().c_str(), args.c_str(), ret);
51     if (ret != TraceErrorCode::SUCCESS) {
52         return TraceRet(ret);
53     }
54     TraceStateMachine::GetInstance().TransToTeleMetryState(policy);
55     return {};
56 }
57 
OpenAppTrace(int32_t appPid)58 TraceRet DynamicState::OpenAppTrace(int32_t appPid)
59 {
60     HIVIEW_LOGW("DynamicState already open, occupied by appid:%{public}d", appPid);
61     return TraceRet(TraceStateCode::DENY);
62 }
63 
DumpTrace(TraceScenario scenario,uint32_t maxDuration,uint64_t happenTime,TraceRetInfo & info)64 TraceRet DynamicState::DumpTrace(TraceScenario scenario, uint32_t maxDuration, uint64_t happenTime, TraceRetInfo &info)
65 {
66     if (scenario != TraceScenario::TRACE_DYNAMIC) {
67         HIVIEW_LOGW(":%{public}s, scenario:%{public}d is fail", GetTag().c_str(), static_cast<int>(scenario));
68         return TraceRet(TraceStateCode::FAIL);
69     }
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 
CloseTrace(TraceScenario scenario)75 TraceRet DynamicState::CloseTrace(TraceScenario scenario)
76 {
77     if (scenario != TraceScenario::TRACE_DYNAMIC) {
78         HIVIEW_LOGW(":%{public}s, scenario:%{public}d is fail", GetTag().c_str(), static_cast<int>(scenario));
79         return TraceRet(TraceStateCode::FAIL);
80     }
81     return TraceBaseState::CloseTrace(scenario);
82 }
83 }