1 /*
2 * Copyright (C) 2022-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
16 #include <algorithm>
17 #include <fcntl.h>
18 #include <sstream>
19 #include <unistd.h>
20 #include "common_define.h"
21 #include "hitrace_meter_test_utils.h"
22 #include "securec.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace HitraceTest {
27 #ifdef LOG_DOMAIN
28 #undef LOG_DOMAIN
29 #define LOG_DOMAIN 0xD002D33
30 #endif
31 #ifdef LOG_TAG
32 #undef LOG_TAG
33 #define LOG_TAG "HitraceTest"
34 #endif
35
36 constexpr int HITRACEID_LEN = 64;
37 const char g_traceLevel[4] = {'D', 'I', 'C', 'M'};
38 static std::string g_traceRootPath;
39 static char g_pid[6];
40 const std::string SEPARATOR = "|";
41
Init(const char (& pid)[6])42 bool Init(const char (&pid)[6])
43 {
44 int ret = strcpy_s(g_pid, sizeof(g_pid), pid);
45 if (ret != 0) {
46 HILOG_ERROR(LOG_CORE, "pid[%{public}s] strcpy_s fail ret: %{public}d.", pid, ret);
47 return false;
48 }
49 if (access((DEBUGFS_TRACING_DIR + TRACE_MARKER_NODE).c_str(), F_OK) != -1) {
50 g_traceRootPath = DEBUGFS_TRACING_DIR;
51 } else if (access((TRACEFS_DIR + TRACE_MARKER_NODE).c_str(), F_OK) != -1) {
52 g_traceRootPath = TRACEFS_DIR;
53 } else {
54 HILOG_ERROR(LOG_CORE, "Error: Finding trace folder failed");
55 return false;
56 }
57 return true;
58 }
59
CleanTrace()60 bool CleanTrace()
61 {
62 if (g_traceRootPath.empty()) {
63 HILOG_ERROR(LOG_CORE, "Error: trace path not found.");
64 return false;
65 }
66 std::ofstream ofs;
67 ofs.open(g_traceRootPath + TRACE_NODE, std::ofstream::out);
68 if (!ofs.is_open()) {
69 HILOG_ERROR(LOG_CORE, "Error: opening trace path failed.");
70 return false;
71 }
72 ofs << "";
73 ofs.close();
74 return true;
75 }
76
WriteStringToFile(const std::string & filename,const std::string & str)77 static bool WriteStringToFile(const std::string& filename, const std::string& str)
78 {
79 if (access((g_traceRootPath + filename).c_str(), W_OK) == 0) {
80 if (g_traceRootPath == "") {
81 HILOG_ERROR(LOG_CORE, "Error: trace path not found.");
82 return false;
83 }
84 std::ofstream out;
85 out.open(g_traceRootPath + filename, std::ios::out);
86 out << str;
87 out.close();
88 return true;
89 }
90 return false;
91 }
92
CleanFtrace()93 bool CleanFtrace()
94 {
95 return WriteStringToFile("events/enable", "0");
96 }
97
SetFtrace(const std::string & filename,bool enabled)98 bool SetFtrace(const std::string& filename, bool enabled)
99 {
100 return WriteStringToFile(filename, enabled ? "1" : "0");
101 }
102
SetFtrace(const std::string & filename,uint64_t value)103 bool SetFtrace(const std::string& filename, uint64_t value)
104 {
105 return WriteStringToFile(filename, std::to_string(value));
106 }
107
ReadFile(const std::string & filename)108 static std::stringstream ReadFile(const std::string& filename)
109 {
110 std::stringstream ss;
111 char resolvedPath[PATH_MAX] = { 0 };
112 if (realpath(filename.c_str(), resolvedPath) == nullptr) {
113 fprintf(stderr, "Error: _fullpath %s failed", filename.c_str());
114 return ss;
115 }
116 std::ifstream fin(resolvedPath);
117 if (!fin.is_open()) {
118 fprintf(stderr, "opening file: %s failed!", filename.c_str());
119 return ss;
120 }
121 ss << fin.rdbuf();
122 fin.close();
123 return ss;
124 }
125
ReadTrace(std::string filename)126 std::vector<std::string> ReadTrace(std::string filename)
127 {
128 if (filename == "") {
129 filename = g_traceRootPath + TRACE_NODE;
130 }
131 std::vector<std::string> list;
132 if (access(filename.c_str(), F_OK) != -1) {
133 std::stringstream ss = ReadFile(filename);
134 std::string line;
135 while (getline(ss, line)) {
136 list.emplace_back(move(line));
137 }
138 }
139 return list;
140 }
141
FindResult(std::string record,const std::vector<std::string> & list)142 bool FindResult(std::string record, const std::vector<std::string>& list)
143 {
144 for (int i = list.size() - 1; i >= 0; i--) {
145 std::string ele = list[i];
146 if (ele.find(record) != std::string::npos) {
147 HILOG_INFO(LOG_CORE, "FindResult: %{public}s", ele.c_str());
148 return true;
149 }
150 }
151 return false;
152 }
153
GetRecord(const HiTraceId * hiTraceId)154 static std::string GetRecord(const HiTraceId* hiTraceId)
155 {
156 std::string record = "";
157 char buf[HITRACEID_LEN] = {0};
158 #ifdef HITRACE_METER_SDK_C
159 int bytes = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "[%llx,%llx,%llx]#",
160 OH_HiTrace_GetChainId(hiTraceId), OH_HiTrace_GetSpanId(hiTraceId), OH_HiTrace_GetParentSpanId(hiTraceId));
161 #else
162 int bytes = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "[%llx,%llx,%llx]#",
163 (*hiTraceId).GetChainId(), (*hiTraceId).GetSpanId(), (*hiTraceId).GetParentSpanId());
164 #endif
165 if (EXPECTANTLY(bytes > 0)) {
166 record += buf;
167 }
168 std::transform(record.cbegin(), record.cend(), record.begin(), [](unsigned char c) { return tolower(c); });
169 return record;
170 }
171
SetNullptrToEmpty(TraceInfo & traceInfo)172 static void SetNullptrToEmpty(TraceInfo& traceInfo)
173 {
174 if (traceInfo.name == nullptr) {
175 traceInfo.name = "";
176 }
177 if (traceInfo.customCategory == nullptr) {
178 traceInfo.customCategory = "";
179 }
180 if (traceInfo.customArgs == nullptr) {
181 traceInfo.customArgs = "";
182 }
183 }
184
GetTraceResult(TraceInfo & traceInfo,const std::vector<std::string> & list,char (& record)[RECORD_SIZE_MAX+1])185 bool GetTraceResult(TraceInfo& traceInfo, const std::vector<std::string>& list,
186 char (&record)[RECORD_SIZE_MAX + 1])
187 {
188 if (list.empty()) {
189 return false;
190 }
191 #ifdef HITRACE_METER_SDK_C
192 std::string bitStr = "62";
193 #else
194 constexpr int bitStrSize = 7;
195 char bitStrC[bitStrSize] = {0};
196 ParseTagBits(traceInfo.tag, bitStrC, bitStrSize);
197 std::string bitStr = std::string(bitStrC);
198 #endif
199 std::string chainStr = "";
200 if (traceInfo.hiTraceId != nullptr) {
201 chainStr = GetRecord(traceInfo.hiTraceId);
202 }
203 SetNullptrToEmpty(traceInfo);
204 std::string name = std::string(traceInfo.name);
205 std::string customCategory = std::string(traceInfo.customCategory);
206 std::string customArgs = std::string(traceInfo.customArgs);
207 std::string recordStr = std::string(1, traceInfo.type) + SEPARATOR + g_pid + SEPARATOR;
208 if (traceInfo.type == 'E') {
209 recordStr += g_traceLevel[traceInfo.level] + bitStr;
210 } else {
211 recordStr += "H:" + chainStr + name + SEPARATOR;
212 if (traceInfo.type == 'B') {
213 recordStr += g_traceLevel[traceInfo.level] + bitStr;
214 if (customArgs != "") {
215 recordStr += SEPARATOR + customArgs;
216 }
217 } else if (traceInfo.type == 'S') {
218 recordStr += std::to_string(traceInfo.value) + SEPARATOR + g_traceLevel[traceInfo.level] + bitStr;
219 if (customArgs != "") {
220 recordStr += SEPARATOR + customCategory + SEPARATOR + customArgs;
221 } else if (customCategory != "") {
222 recordStr += SEPARATOR + customCategory;
223 }
224 } else {
225 recordStr += std::to_string(traceInfo.value) + SEPARATOR + g_traceLevel[traceInfo.level] + bitStr;
226 }
227 }
228 recordStr = recordStr.substr(0, sizeof(record) - 1);
229 int bytes = snprintf_s(record, sizeof(record), sizeof(record) - 1, "%s", recordStr.c_str());
230 if (bytes == -1) {
231 HILOG_INFO(LOG_CORE, "GetTraceResult: recordStr may be truncated");
232 }
233 return FindResult(std::string(record), list);
234 }
235 }
236 }
237 }
238