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 #ifndef HIVIEWDFX_HIVIEW_TRACE_UTIL_H 17 #define HIVIEWDFX_HIVIEW_TRACE_UTIL_H 18 19 #include <cinttypes> 20 #include <string> 21 #include <vector> 22 23 #include "hitrace_dump.h" 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 using namespace Hitrace; 28 29 namespace CallerName { 30 inline constexpr char XPERF[] = "Xperf"; 31 inline constexpr char XPOWER[] = "Xpower"; 32 inline constexpr char RELIABILITY[] = "Reliability"; 33 inline constexpr char HIVIEW[] = "Hiview"; 34 inline constexpr char OTHER[] = "Other"; 35 inline constexpr char SCREEN[] = "Screen"; 36 }; 37 38 namespace ClientName { 39 inline constexpr char COMMAND[] = "Command"; 40 inline constexpr char COMMON_DEV[] = "Other"; 41 inline constexpr char APP[] = "APP"; 42 inline constexpr char BETACLUB[] = "BetaClub"; 43 }; 44 45 namespace BusinessName { 46 inline constexpr char BEHAVIOR[] = "behavior"; 47 inline constexpr char TELEMETRY[] = "Telemetry"; 48 } 49 50 enum class TraceScenario : uint8_t { 51 TRACE_COMMAND, 52 TRACE_COMMON, 53 TRACE_DYNAMIC, 54 TRACE_TELEMETRY, 55 }; 56 57 struct DumpTraceArgs { 58 TraceScenario scenario; 59 uint32_t maxDuration = 0; 60 uint64_t happenTime = 0; 61 }; 62 63 using DumpTraceCallback = std::function<void(TraceRetInfo)>; 64 65 enum class TraceStateCode : uint8_t { 66 SUCCESS, 67 DENY, // Change state deny 68 FAIL, // Invoke dump or drop interface in wrong state 69 POLICY_ERROR, 70 UPDATE_TIME, 71 NO_TRIGGER 72 }; 73 74 enum class TraceFlowCode : uint8_t { 75 TRACE_ALLOW, 76 TRACE_DUMP_DENY, 77 TRACE_UPLOAD_DENY, 78 TRACE_HAS_CAPTURED_TRACE 79 }; 80 81 enum class TelemetryPolicy { 82 DEFAULT, 83 POWER, 84 MANUAL 85 }; 86 87 struct TraceRet { 88 TraceRet() = default; 89 TraceRetTraceRet90 explicit TraceRet(TraceStateCode stateError) : stateError_(stateError) {} 91 TraceRetTraceRet92 explicit TraceRet(TraceErrorCode codeError) : codeError_(codeError) {} 93 TraceRetTraceRet94 explicit TraceRet(TraceFlowCode codeError) : flowError_(codeError) {} 95 GetStateErrorTraceRet96 TraceStateCode GetStateError() 97 { 98 return stateError_; 99 } 100 GetCodeErrorTraceRet101 TraceErrorCode GetCodeError() 102 { 103 return codeError_; 104 } 105 GetFlowErrorTraceRet106 TraceFlowCode GetFlowError() 107 { 108 return flowError_; 109 } 110 IsSuccessTraceRet111 bool IsSuccess() 112 { 113 bool isStateSuccess = stateError_ == TraceStateCode::SUCCESS || stateError_ == TraceStateCode::NO_TRIGGER || 114 stateError_ == TraceStateCode::UPDATE_TIME; 115 return isStateSuccess && codeError_ == TraceErrorCode::SUCCESS && flowError_ == TraceFlowCode::TRACE_ALLOW; 116 } 117 118 TraceStateCode stateError_ = TraceStateCode::SUCCESS; 119 TraceErrorCode codeError_ = TraceErrorCode::SUCCESS; 120 TraceFlowCode flowError_ = TraceFlowCode::TRACE_ALLOW; 121 }; 122 123 class TelemetryCallback { 124 public: 125 virtual ~TelemetryCallback() = default; 126 virtual void OnTelemetryStart() = 0; 127 virtual void OnTelemetryFinish() = 0; 128 virtual void OnTelemetryTraceOn() = 0; 129 virtual void OnTelemetryTraceOff() = 0; 130 }; 131 } 132 } 133 #endif // HIVIEWDFX_HIVIEW_TRACE_UTIL_H 134