1 /* 2 * Copyright (c) 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 #ifndef NATIVE_LEAK_STATE_H 16 #define NATIVE_LEAK_STATE_H 17 18 #include <fstream> 19 #include <memory> 20 #include <string> 21 22 #include "plugin.h" 23 #include "app_event_handler.h" 24 #include "app_event_publisher.h" 25 #include "fault_common_base.h" 26 #include "fault_detector_base.h" 27 #include "fault_info_base.h" 28 #include "fault_state_base.h" 29 #include "ffrt.h" 30 #include "mem_profiler_collector.h" 31 #include "native_leak_info.h" 32 #include "native_leak_util.h" 33 34 namespace OHOS { 35 namespace HiviewDFX { 36 37 class NativeLeakSampleState : public FaultStateBase { 38 public: 39 void CollectBaseInfo(std::shared_ptr<NativeLeakInfo> &userMonitorInfo) const; 40 ErrCode ChangeNextState(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 41 ErrCode StateProcess(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 42 private: 43 bool CollectUserBaseInfo(std::shared_ptr<NativeLeakInfo> &userMonitorInfo) const; 44 void RemoveData(std::shared_ptr<NativeLeakInfo> &userMonitorInfo) const; 45 }; 46 47 class NativeLeakJudgeState : public FaultStateBase { 48 public: 49 ErrCode ChangeNextState(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 50 ErrCode StateProcess(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 51 52 private: 53 bool IsMemoryLeak(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 54 std::string JudgeMemoryLeakGrade(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 55 std::string JudgeSmallMemoryLeakGrade(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 56 std::string JudgeMemoryLeakGradeByRatio(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 57 std::string JudgeOtherMemoryLeakGrade(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 58 }; 59 60 class NativeLeakDumpState : public FaultStateBase { 61 public: 62 NativeLeakDumpState(); 63 ErrCode ChangeNextState(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 64 ErrCode StateProcess(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 65 void DumpUserMemInfo(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 66 67 private: 68 void GetMemoryLeakLog(std::shared_ptr<NativeLeakInfo> &userMonitorInfo, uint32_t flag); 69 void DumpGeneralInfo(std::ofstream &fout, std::shared_ptr<NativeLeakInfo> &userMonitorInfo) const; 70 void DumpDetailInfo(std::ofstream &fout, std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 71 void DumpStackInfo(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 72 void DumpExtraInfo(std::shared_ptr<NativeLeakInfo> &userMonitorInfo, uint32_t type) const; 73 bool ForkProcessToDumpExtraInfo( 74 const std::string &path, std::shared_ptr<NativeLeakInfo> &userMonitorInfo, uint32_t type) const; 75 bool DumpUserMemInfoToSmapsFile(int writeFd, std::shared_ptr<NativeLeakInfo> &userMonitorInfo) const; 76 std::string GetExtraInfo(uint32_t type) const; 77 void ExecuteChildProcessGetSmapsInfo(int pid) const; 78 void ExecuteChildProcessGetRsInfo() const; 79 void ExecuteChildProcessGetGpuInfo() const; 80 void LaunchMemoryDebug(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 81 void GetProfiler(std::shared_ptr<NativeLeakInfo> &userMonitorInfo); 82 bool SuccessToSendCmd(std::shared_ptr<NativeLeakInfo> &userMonitorInfo, MemCmd cmdType); 83 int32_t SendCmd(std::shared_ptr<NativeLeakInfo> &userMonitorInfo, MemCmd cmdType) const; 84 85 private: 86 ffrt::mutex dumpStateMtx_; 87 #ifdef HAS_HIPROFILER 88 std::shared_ptr<UCollectUtil::MemProfilerCollector> memProfilerCollector_; 89 #endif 90 }; 91 92 class NativeLeakReportState : public FaultStateBase { 93 public: 94 ErrCode ChangeNextState(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 95 ErrCode StateProcess(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 96 void setEventHandler(std::shared_ptr<AppEventHandler> handler); 97 private: 98 void PostEvent(std::shared_ptr<FaultInfoBase> &monitorInfo); 99 std::shared_ptr<AppEventHandler> appEventHandler_ { nullptr }; 100 }; 101 102 class NativeLeakRemovalState : public FaultStateBase { 103 public: 104 ErrCode ChangeNextState(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 105 ErrCode StateProcess(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultDetectorBase &detectorObj) override; 106 }; 107 } // namespace HiviewDFX 108 } // namespace OHOS 109 #endif // NATIVE_LEAK_STATE_H 110