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