/* * Copyright (C) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef TRACE_SOURCE_H #define TRACE_SOURCE_H #include #include #include "trace_content.h" namespace OHOS { namespace HiviewDFX { namespace Hitrace { /** * @brief ITraceSource is the interface for trace source. * @note The trace source is responsible for providing the trace data to the trace dump executor. */ class ITraceSource { public: ITraceSource() {} virtual ~ITraceSource() {} virtual std::shared_ptr GetTraceFileHeader() = 0; virtual std::shared_ptr GetTraceBaseInfo() = 0; virtual std::shared_ptr GetTraceCpuRaw(const TraceDumpRequest& request) = 0; virtual std::shared_ptr GetTraceHeaderPage() = 0; virtual std::shared_ptr GetTracePrintkFmt() = 0; virtual std::shared_ptr GetTraceEventFmt() = 0; virtual std::shared_ptr GetTraceCmdLines() = 0; virtual std::shared_ptr GetTraceTgids() = 0; virtual std::shared_ptr GetTraceCpuRawRead(const TraceDumpRequest& request) = 0; virtual std::shared_ptr GetTraceCpuRawWrite(const uint64_t taskId) = 0; virtual std::string GetTraceFilePath() = 0; virtual bool UpdateTraceFile(const std::string& traceFilePath) = 0; }; struct LinuxTraceSourceStrategy { static std::shared_ptr CreateFileHdr(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file); } static std::shared_ptr CreateBaseInfo(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file, false); } static std::shared_ptr CreateCpuRaw(int fd, const std::string& fs, const std::string& file, const TraceDumpRequest& req) { return std::make_shared(fd, fs, file, req); } static std::shared_ptr CreateHeaderPage(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file); } static std::shared_ptr CreatePrintkFmt(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file); } static std::shared_ptr CreateEventFmt(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file, false); } static std::shared_ptr CreateCmdLines(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file, false); } static std::shared_ptr CreateTgids(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file, false); } static std::shared_ptr CreateCpuRawRead(const std::string& fs, const TraceDumpRequest& req) { return std::make_shared(fs, req); } static std::shared_ptr CreateCpuRawWrite(int fd, const std::string& file, uint64_t taskId) { return std::make_shared(fd, file, taskId); } }; struct HMTraceSourceStrategy { static std::shared_ptr CreateFileHdr(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file); } static std::shared_ptr CreateBaseInfo(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file, true); } static std::shared_ptr CreateCpuRaw(int fd, const std::string& fs, const std::string& file, const TraceDumpRequest& req) { return std::make_shared(fd, fs, file, req); } static std::shared_ptr CreateHeaderPage(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file); } static std::shared_ptr CreatePrintkFmt(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file); } static std::shared_ptr CreateEventFmt(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file, true); } static std::shared_ptr CreateCmdLines(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file, true); } static std::shared_ptr CreateTgids(int fd, const std::string& fs, const std::string& file) { return std::make_shared(fd, fs, file, true); } static std::shared_ptr CreateCpuRawRead(const std::string& fs, const TraceDumpRequest& req) { return std::make_shared(fs, req); } static std::shared_ptr CreateCpuRawWrite(int fd, const std::string& file, uint64_t taskId) { return std::make_shared(fd, file, taskId); } }; template class TraceSource : public ITraceSource { public: TraceSource() = delete; TraceSource(const std::string& tracefsPath, const std::string& traceFilePath); ~TraceSource() override; std::shared_ptr GetTraceFileHeader() override; std::shared_ptr GetTraceBaseInfo() override; std::shared_ptr GetTraceCpuRaw(const TraceDumpRequest& request) override; std::shared_ptr GetTraceHeaderPage() override; std::shared_ptr GetTracePrintkFmt() override; std::shared_ptr GetTraceEventFmt() override; std::shared_ptr GetTraceCmdLines() override; std::shared_ptr GetTraceTgids() override; std::shared_ptr GetTraceCpuRawRead(const TraceDumpRequest& request) override; std::shared_ptr GetTraceCpuRawWrite(const uint64_t taskId) override; std::string GetTraceFilePath() override; bool UpdateTraceFile(const std::string& traceFilePath) override; private: int traceFileFd_ = -1; std::string tracefsPath_; std::string traceFilePath_; }; using TraceSourceLinux = TraceSource; using TraceSourceHM = TraceSource; } // namespace Hitrace } // namespace HiviewDFX } // namespace OHOS #endif // TRACE_SOURCE_H