• 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 #ifndef TRACE_CONTENT_H
17 #define TRACE_CONTENT_H
18 
19 #include <cinttypes>
20 #include <string>
21 
22 #include "hitrace_define.h"
23 #include "trace_buffer_manager.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace Hitrace {
28 constexpr int ALIGNMENT_COEFFICIENT = 4;
29 constexpr uint16_t MAGIC_NUMBER = 57161;
30 constexpr uint8_t FILE_RAW_TRACE = 0;
31 constexpr uint16_t VERSION_NUMBER = 1;
32 
33 struct alignas(ALIGNMENT_COEFFICIENT) TraceFileHeader {
34     uint16_t magicNumber {MAGIC_NUMBER};
35     uint8_t fileType {FILE_RAW_TRACE};
36     uint16_t versionNumber {VERSION_NUMBER};
37     uint32_t reserved {0};
38 };
39 
40 enum ContentType : uint8_t {
41     CONTENT_TYPE_DEFAULT = 0,
42     CONTENT_TYPE_EVENTS_FORMAT = 1,
43     CONTENT_TYPE_CMDLINES  = 2,
44     CONTENT_TYPE_TGIDS = 3,
45     CONTENT_TYPE_CPU_RAW = 4,
46     CONTENT_TYPE_HEADER_PAGE = 30,
47     CONTENT_TYPE_PRINTK_FORMATS = 31,
48     CONTENT_TYPE_KALLSYMS = 32,
49     CONTENT_TYPE_BASE_INFO = 33
50 };
51 
52 struct alignas(ALIGNMENT_COEFFICIENT) TraceFileContentHeader {
53     uint8_t type = CONTENT_TYPE_DEFAULT;
54     uint32_t length = 0;
55 };
56 
57 struct PageHeader {
58     uint64_t timestamp = 0;
59     uint64_t size = 0;
60     uint8_t overwrite = 0;
61     uint8_t *startPos = nullptr;
62     uint8_t *endPos = nullptr;
63 };
64 
65 class ITraceContent {
66 public:
67     ITraceContent(const int fd, const std::string& tracefsPath, const std::string& traceFilePath, const bool ishm);
68     virtual ~ITraceContent();
69     virtual bool WriteTraceContent() = 0;
70     bool WriteTraceData(const uint8_t contentType);
71     void DoWriteTraceData(const uint8_t* buffer, const int bytes, ssize_t& writeLen);
72     bool DoWriteTraceContentHeader(TraceFileContentHeader& contentHeader, const uint8_t contentType);
73     void UpdateTraceContentHeader(TraceFileContentHeader& contentHeader, const uint32_t writeLen);
74     bool IsFileExist();
75     bool CheckPage(uint8_t* page);
GetTraceFilePath()76     std::string GetTraceFilePath() const { return traceFilePath_; }
77     static int GetCurrentFileSize();
78     static void ResetCurrentFileSize();
79     void WriteProcessLists(ssize_t& writeLen);
80 
81 protected:
82     int traceFileFd_ = -1;
83     int traceSourceFd_ = -1;
84     std::string tracefsPath_;
85     std::string traceFilePath_;
86     bool isHm_;
87 };
88 
89 class ITraceFileHdrContent : public ITraceContent {
90 public:
ITraceFileHdrContent(const int fd,const std::string & tracefsPath,const std::string & traceFilePath,const bool ishm)91     ITraceFileHdrContent(const int fd,
92         const std::string& tracefsPath, const std::string& traceFilePath, const bool ishm)
93         : ITraceContent(fd, tracefsPath, traceFilePath, ishm) {}
94     bool WriteTraceContent() override = 0;
95     bool InitTraceFileHdr(TraceFileHeader& fileHdr);
96 };
97 
98 class TraceFileHdrLinux : public ITraceFileHdrContent {
99 public:
TraceFileHdrLinux(const int fd,const std::string & tracefsPath,const std::string & traceFilePath)100     TraceFileHdrLinux(const int fd, const std::string& tracefsPath, const std::string& traceFilePath)
101         : ITraceFileHdrContent(fd, tracefsPath, traceFilePath, false) {}
102     bool WriteTraceContent() override;
103 };
104 
105 class TraceFileHdrHM : public ITraceFileHdrContent {
106 public:
TraceFileHdrHM(const int fd,const std::string & tracefsPath,const std::string & traceFilePath)107     TraceFileHdrHM(const int fd, const std::string& tracefsPath, const std::string& traceFilePath)
108         : ITraceFileHdrContent(fd, tracefsPath, traceFilePath, true) {}
109     bool WriteTraceContent() override;
110 };
111 
112 class TraceBaseInfoContent : public ITraceContent {
113 public:
TraceBaseInfoContent(const int fd,const std::string & tracefsPath,const std::string & traceFilePath,const bool ishm)114     TraceBaseInfoContent(const int fd,
115         const std::string& tracefsPath, const std::string& traceFilePath, const bool ishm)
116         : ITraceContent(fd, tracefsPath, traceFilePath, ishm) {}
117     bool WriteTraceContent() override;
118 
119 private:
120     ssize_t WriteKeyValue(const std::string& key, const std::string& value);
121     ssize_t WriteUnixTimeMs();
122     ssize_t WriteBootTimeMs();
123     ssize_t WriteKernelVersion();
124 };
125 
126 class TraceEventFmtContent : public ITraceContent {
127 public:
128     TraceEventFmtContent(const int fd, const std::string& tracefsPath, const std::string& traceFilePath,
129         const bool ishm);
130     bool WriteTraceContent() override;
131 };
132 
133 class TraceCmdLinesContent : public ITraceContent {
134 public:
135     TraceCmdLinesContent(const int fd, const std::string& tracefsPath, const std::string& traceFilePath,
136         const bool ishm);
137     bool WriteTraceContent() override;
138 };
139 
140 class TraceTgidsContent : public ITraceContent {
141 public:
142     TraceTgidsContent(const int fd, const std::string& tracefsPath, const std::string& traceFilePath,
143         const bool ishm);
144     bool WriteTraceContent() override;
145 };
146 
147 class ITraceCpuRawContent : public ITraceContent {
148 public:
ITraceCpuRawContent(const int fd,const std::string & tracefsPath,const std::string & traceFilePath,const bool ishm,const TraceDumpRequest & request)149     ITraceCpuRawContent(const int fd, const std::string& tracefsPath, const std::string& traceFilePath,
150         const bool ishm, const TraceDumpRequest& request)
151         : ITraceContent(fd, tracefsPath, traceFilePath, ishm), request_(request) {}
152     bool WriteTraceContent() override = 0;
153 
154     bool WriteTracePipeRawData(const std::string& srcPath, const int cpuIdx);
155     void ReadTracePipeRawLoop(const int srcFd,
156         int& bytes, bool& endFlag, int& pageChkFailedTime, bool& printFirstPageTime);
157     bool IsWriteFileOverflow(const int outputFileSize, const ssize_t writeLen, const int fileSizeThreshold);
158 
GetDumpStatus()159     TraceErrorCode GetDumpStatus() { return dumpStatus_; }
GetFirstPageTimeStamp()160     uint64_t GetFirstPageTimeStamp() { return firstPageTimeStamp_; }
GetLastPageTimeStamp()161     uint64_t GetLastPageTimeStamp() { return lastPageTimeStamp_; }
162     bool IsOverFlow();
163 
164 protected:
165     TraceDumpRequest request_;
166     TraceErrorCode dumpStatus_ = TraceErrorCode::UNSET;
167     uint64_t firstPageTimeStamp_ = std::numeric_limits<uint64_t>::max();
168     uint64_t lastPageTimeStamp_ = 0;
169     bool isOverFlow_ = false;
170 };
171 
172 class TraceCpuRawLinux : public ITraceCpuRawContent {
173 public:
TraceCpuRawLinux(const int fd,const std::string & tracefsPath,const std::string & traceFilePath,const TraceDumpRequest & request)174     TraceCpuRawLinux(const int fd,
175         const std::string& tracefsPath, const std::string& traceFilePath, const TraceDumpRequest& request)
176         : ITraceCpuRawContent(fd, tracefsPath, traceFilePath, false, request) {}
177     bool WriteTraceContent() override;
178 };
179 
180 class TraceCpuRawHM : public ITraceCpuRawContent {
181 public:
TraceCpuRawHM(const int fd,const std::string & tracefsPath,const std::string & traceFilePath,const TraceDumpRequest & request)182     TraceCpuRawHM(const int fd,
183         const std::string& tracefsPath, const std::string& traceFilePath, const TraceDumpRequest& request)
184         : ITraceCpuRawContent(fd, tracefsPath, traceFilePath, true, request) {}
185     bool WriteTraceContent() override;
186 };
187 
188 class ITraceCpuRawRead : public ITraceContent {
189 public:
ITraceCpuRawRead(const std::string & tracefsPath,const bool ishm,const TraceDumpRequest & request)190     ITraceCpuRawRead(const std::string& tracefsPath, const bool ishm, const TraceDumpRequest& request)
191         : ITraceContent(-1, tracefsPath, "", ishm), request_(request) {}
192     bool WriteTraceContent() override = 0;
193 
194     bool CacheTracePipeRawData(const std::string& srcPath, const int cpuIdx);
195     bool CopyTracePipeRawLoop(const int srcFd, const int cpu, ssize_t& writeLen,
196         int& pageChkFailedTime, bool& printFirstPageTime);
197 
GetDumpStatus()198     TraceErrorCode GetDumpStatus() { return dumpStatus_; }
GetFirstPageTimeStamp()199     uint64_t GetFirstPageTimeStamp() { return firstPageTimeStamp_; }
GetLastPageTimeStamp()200     uint64_t GetLastPageTimeStamp() { return lastPageTimeStamp_; }
201 
202 protected:
203     TraceDumpRequest request_;
204     TraceErrorCode dumpStatus_ = TraceErrorCode::UNSET;
205     uint64_t firstPageTimeStamp_ = std::numeric_limits<uint64_t>::max();
206     uint64_t lastPageTimeStamp_ = 0;
207 };
208 
209 class TraceCpuRawReadLinux : public ITraceCpuRawRead {
210 public:
TraceCpuRawReadLinux(const std::string & tracefsPath,const TraceDumpRequest & request)211     TraceCpuRawReadLinux(const std::string& tracefsPath, const TraceDumpRequest& request)
212         : ITraceCpuRawRead(tracefsPath, false, request) {}
213     bool WriteTraceContent() override;
214 };
215 
216 class TraceCpuRawReadHM : public ITraceCpuRawRead {
217 public:
TraceCpuRawReadHM(const std::string & tracefsPath,const TraceDumpRequest & request)218     TraceCpuRawReadHM(const std::string& tracefsPath, const TraceDumpRequest& request)
219         : ITraceCpuRawRead(tracefsPath, true, request) {}
220     bool WriteTraceContent() override;
221 };
222 
223 class ITraceCpuRawWrite : public ITraceContent {
224 public:
ITraceCpuRawWrite(const int fd,const std::string & traceFilePath,const uint64_t taskId,const bool ishm)225     ITraceCpuRawWrite(const int fd, const std::string& traceFilePath, const uint64_t taskId, const bool ishm)
226         : ITraceContent(fd, "", traceFilePath, ishm), taskId_(taskId) {}
227     bool WriteTraceContent() override = 0;
228 
229 protected:
230     uint64_t taskId_ = 0; // Task ID for the current write operation
231 };
232 
233 class TraceCpuRawWriteLinux : public ITraceCpuRawWrite {
234 public:
TraceCpuRawWriteLinux(const int fd,const std::string & traceFilePath,const uint64_t taskId)235     TraceCpuRawWriteLinux(const int fd, const std::string& traceFilePath, const uint64_t taskId)
236         : ITraceCpuRawWrite(fd, traceFilePath, taskId, false) {}
237     bool WriteTraceContent() override;
238 };
239 
240 class TraceCpuRawWriteHM : public ITraceCpuRawWrite {
241 public:
TraceCpuRawWriteHM(const int fd,const std::string & traceFilePath,const uint64_t taskId)242     TraceCpuRawWriteHM(const int fd, const std::string& traceFilePath, const uint64_t taskId)
243         : ITraceCpuRawWrite(fd, traceFilePath, taskId, true) {}
244     bool WriteTraceContent() override;
245 };
246 
247 class ITraceHeaderPageContent : public ITraceContent {
248 public:
ITraceHeaderPageContent(const int fd,const std::string & tracefsPath,const std::string & traceFilePath,const bool ishm)249     ITraceHeaderPageContent(const int fd,
250         const std::string& tracefsPath, const std::string& traceFilePath, const bool ishm)
251         : ITraceContent(fd, tracefsPath, traceFilePath, ishm) {}
252     bool WriteTraceContent() override = 0;
253 };
254 
255 class TraceHeaderPageLinux : public ITraceHeaderPageContent {
256 public:
257     TraceHeaderPageLinux(const int fd, const std::string& tracefsPath, const std::string& traceFilePath);
258     bool WriteTraceContent() override;
259 };
260 
261 class TraceHeaderPageHM : public ITraceHeaderPageContent {
262 public:
TraceHeaderPageHM(const int fd,const std::string & tracefsPath,const std::string & traceFilePath)263     TraceHeaderPageHM(const int fd, const std::string& tracefsPath, const std::string& traceFilePath)
264         : ITraceHeaderPageContent(fd, tracefsPath, traceFilePath, true) {}
265     bool WriteTraceContent() override;
266 };
267 
268 class ITracePrintkFmtContent : public ITraceContent {
269 public:
ITracePrintkFmtContent(const int fd,const std::string & tracefsPath,const std::string & traceFilePath,const bool ishm)270     ITracePrintkFmtContent(const int fd,
271         const std::string& tracefsPath, const std::string& traceFilePath, const bool ishm)
272         : ITraceContent(fd, tracefsPath, traceFilePath, ishm) {}
273     bool WriteTraceContent() override = 0;
274 };
275 
276 class TracePrintkFmtLinux : public ITracePrintkFmtContent {
277 public:
278     TracePrintkFmtLinux(const int fd, const std::string& tracefsPath, const std::string& traceFilePath);
279     bool WriteTraceContent() override;
280 };
281 
282 class TracePrintkFmtHM : public ITracePrintkFmtContent {
283 public:
TracePrintkFmtHM(const int fd,const std::string & tracefsPath,const std::string & traceFilePath)284     TracePrintkFmtHM(const int fd, const std::string& tracefsPath, const std::string& traceFilePath)
285         : ITracePrintkFmtContent(fd, tracefsPath, traceFilePath, true) {}
286     bool WriteTraceContent() override;
287 };
288 } // namespace Hitrace
289 } // namespace HiviewDFX
290 } // namespace OHOS
291 #endif // TRACE_CONTENT_H