1 /* 2 * Copyright (c) 2021-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 HIVIEW_SERVICE_ABILITY_H 17 #define HIVIEW_SERVICE_ABILITY_H 18 19 #include <string> 20 #include <memory> 21 22 #include "app_caller_parcelable.h" 23 #include "hiview_err_code.h" 24 #include "hiview_file_info.h" 25 #include "hiview_service.h" 26 #include "hiview_service_ability_stub.h" 27 #include "hiview_logger.h" 28 #include "memory_caller_parcelable.h" 29 #include "singleton.h" 30 #include "system_ability.h" 31 32 namespace OHOS { 33 namespace HiviewDFX { 34 class HiviewServiceAbility : public SystemAbility, public HiviewServiceAbilityStub { 35 DECLARE_SYSTEM_ABILITY(HiviewServiceAbility); 36 37 public: 38 HiviewServiceAbility(); 39 ~HiviewServiceAbility(); 40 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 41 static void StartService(HiviewService *service); 42 static void StartServiceAbility(int sleepS); 43 static HiviewService* GetOrSetHiviewService(HiviewService *service = nullptr); 44 45 ErrCode ListFiles(const std::string& logType, std::vector<HiviewFileInfo>& fileInfos) override; 46 ErrCode Copy(const std::string& logType, const std::string& logName, const std::string& dest) override; 47 ErrCode Move(const std::string& logType, const std::string& logName, const std::string& dest) override; 48 ErrCode Remove(const std::string& logType, const std::string& logName) override; 49 50 ErrCode OpenSnapshotTrace(const std::vector<std::string>& tagGroups, int32_t& errNo, int32_t& ret) override; 51 ErrCode DumpSnapshotTrace(int32_t client, int32_t& errNo, std::vector<std::string>& files) override; 52 ErrCode OpenRecordingTrace(const std::string& tags, int32_t& errNo, int32_t& ret) override; 53 ErrCode RecordingTraceOn(int32_t& errNo, int32_t& ret) override; 54 ErrCode RecordingTraceOff(int32_t& errNo, std::vector<std::string>& files) override; 55 ErrCode CloseTrace(int32_t& errNo, int32_t& ret) override; 56 ErrCode CaptureDurationTrace(const AppCallerParcelable& appCaller, int32_t& errNo, int32_t& ret) override; 57 ErrCode GetSysCpuUsage(int32_t& errNo, double& ret) override; 58 ErrCode SetAppResourceLimit( 59 const MemoryCallerParcelable& memoryCallerParcelable, int32_t& errNo, int32_t& ret) override; 60 ErrCode SetSplitMemoryValue( 61 const std::vector<MemoryCallerParcelable>& memCallerParcelableList, int32_t& errNo, int32_t& ret) override; 62 ErrCode GetGraphicUsage(int32_t& errNo, int32_t& ret) override; 63 64 protected: 65 void OnDump() override; 66 void OnStart() override; 67 void OnStop() override; 68 69 private: 70 template<typename T> TraceCalling(std::function<CollectResult<T> (HiviewService *)> traceRetHandler,int32_t & errNo,T & ret)71 void TraceCalling(std::function<CollectResult<T>(HiviewService*)> traceRetHandler, int32_t& errNo, T& ret) 72 { 73 if (auto service = GetOrSetHiviewService(); service == nullptr) { 74 errNo = UCollect::UcError::UNSUPPORT; 75 } else { 76 CollectResult<T> collectRet = traceRetHandler(service); 77 errNo = collectRet.retCode; 78 ret = collectRet.data; 79 } 80 } 81 82 private: 83 void GetFileInfoUnderDir(const std::string& dirPath, std::vector<HiviewFileInfo>& fileInfos); 84 ErrCode CopyOrMoveFile( 85 const std::string& logType, const std::string& logName, const std::string& dest, bool isMove); 86 }; 87 88 class HiviewServiceAbilityDeathRecipient : public IRemoteObject::DeathRecipient { 89 public: 90 HiviewServiceAbilityDeathRecipient(); 91 ~HiviewServiceAbilityDeathRecipient(); 92 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 93 }; 94 } // namespace HiviewDFX 95 } // namespace OHOS 96 #endif // IHIVIEW_SERVICE_H