• 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_source.h"
17 
18 #include <fcntl.h>
19 #include <hilog/log.h>
20 #include <string>
21 #include <unistd.h>
22 
23 #include "common_utils.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace Hitrace {
28 namespace {
29 #ifdef LOG_DOMAIN
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD002D33
32 #endif
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #define LOG_TAG "HitraceSource"
36 #endif
37 
UpdateFileFd(const std::string & traceFile,int & fd)38 static bool UpdateFileFd(const std::string& traceFile, int& fd)
39 {
40     std::string path = CanonicalizeSpecPath(traceFile.c_str());
41     int newFd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); // 0644 : -rw-r--r--
42     if (newFd < 0) {
43         HILOG_ERROR(LOG_CORE, "TraceSource: open %{public}s failed.", traceFile.c_str());
44         return false;
45     }
46     if (fd != -1) {
47         close(fd);
48     }
49     fd = newFd;
50     return true;
51 }
52 }
53 
54 // 模板实现
55 template <typename Strategy>
TraceSource(const std::string & tracefsPath,const std::string & traceFilePath)56 TraceSource<Strategy>::TraceSource(const std::string& tracefsPath, const std::string& traceFilePath)
57     : tracefsPath_(tracefsPath), traceFilePath_(traceFilePath)
58 {
59     if (traceFilePath.empty()) {
60         return;
61     }
62     std::string path = CanonicalizeSpecPath(traceFilePath.c_str());
63     traceFileFd_ = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); // 0644 : -rw-r--r--
64     if (traceFileFd_ < 0) {
65         HILOG_ERROR(LOG_CORE, "TraceSource: open %{public}s failed.", traceFilePath.c_str());
66     }
67 }
68 
69 template <typename Strategy>
~TraceSource()70 TraceSource<Strategy>::~TraceSource()
71 {
72     if (traceFileFd_ != -1) {
73         close(traceFileFd_);
74     }
75 }
76 
77 template <typename Strategy>
GetTraceFileHeader()78 std::shared_ptr<ITraceFileHdrContent> TraceSource<Strategy>::GetTraceFileHeader()
79 {
80     return Strategy::CreateFileHdr(traceFileFd_, tracefsPath_, traceFilePath_);
81 }
82 
83 template <typename Strategy>
GetTraceBaseInfo()84 std::shared_ptr<TraceBaseInfoContent> TraceSource<Strategy>::GetTraceBaseInfo()
85 {
86     return Strategy::CreateBaseInfo(traceFileFd_, tracefsPath_, traceFilePath_);
87 }
88 
89 template <typename Strategy>
GetTraceCpuRaw(const TraceDumpRequest & request)90 std::shared_ptr<ITraceCpuRawContent> TraceSource<Strategy>::GetTraceCpuRaw(const TraceDumpRequest& request)
91 {
92     return Strategy::CreateCpuRaw(traceFileFd_, tracefsPath_, traceFilePath_, request);
93 }
94 
95 template <typename Strategy>
GetTraceHeaderPage()96 std::shared_ptr<ITraceHeaderPageContent> TraceSource<Strategy>::GetTraceHeaderPage()
97 {
98     return Strategy::CreateHeaderPage(traceFileFd_, tracefsPath_, traceFilePath_);
99 }
100 
101 template <typename Strategy>
GetTracePrintkFmt()102 std::shared_ptr<ITracePrintkFmtContent> TraceSource<Strategy>::GetTracePrintkFmt()
103 {
104     return Strategy::CreatePrintkFmt(traceFileFd_, tracefsPath_, traceFilePath_);
105 }
106 
107 template <typename Strategy>
GetTraceEventFmt()108 std::shared_ptr<TraceEventFmtContent> TraceSource<Strategy>::GetTraceEventFmt()
109 {
110     return Strategy::CreateEventFmt(traceFileFd_, tracefsPath_, traceFilePath_);
111 }
112 
113 template <typename Strategy>
GetTraceCmdLines()114 std::shared_ptr<TraceCmdLinesContent> TraceSource<Strategy>::GetTraceCmdLines()
115 {
116     return Strategy::CreateCmdLines(traceFileFd_, tracefsPath_, traceFilePath_);
117 }
118 
119 template <typename Strategy>
GetTraceTgids()120 std::shared_ptr<TraceTgidsContent> TraceSource<Strategy>::GetTraceTgids()
121 {
122     return Strategy::CreateTgids(traceFileFd_, tracefsPath_, traceFilePath_);
123 }
124 
125 template <typename Strategy>
GetTraceCpuRawRead(const TraceDumpRequest & request)126 std::shared_ptr<ITraceCpuRawRead> TraceSource<Strategy>::GetTraceCpuRawRead(const TraceDumpRequest& request)
127 {
128     return Strategy::CreateCpuRawRead(tracefsPath_, request);
129 }
130 
131 template <typename Strategy>
GetTraceCpuRawWrite(const uint64_t taskId)132 std::shared_ptr<ITraceCpuRawWrite> TraceSource<Strategy>::GetTraceCpuRawWrite(const uint64_t taskId)
133 {
134     return Strategy::CreateCpuRawWrite(traceFileFd_, traceFilePath_, taskId);
135 }
136 
137 template <typename Strategy>
GetTraceFilePath()138 std::string TraceSource<Strategy>::GetTraceFilePath()
139 {
140     return traceFilePath_;
141 }
142 
143 template <typename Strategy>
UpdateTraceFile(const std::string & traceFilePath)144 bool TraceSource<Strategy>::UpdateTraceFile(const std::string& traceFilePath)
145 {
146     if (!UpdateFileFd(traceFilePath, traceFileFd_)) {
147         return false;
148     }
149     traceFilePath_ = traceFilePath;
150     return true;
151 }
152 
153 // 显式实例化
154 template class TraceSource<LinuxTraceSourceStrategy>;
155 template class TraceSource<HMTraceSourceStrategy>;
156 } // namespace Hitrace
157 } // namespace HiviewDFX
158 } // namespace OHOS