1 /* 2 * Copyright (c) 2021 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 HIDUMPER_SERVICES_DUMP_MANAGER_SERVICE_H 16 #define HIDUMPER_SERVICES_DUMP_MANAGER_SERVICE_H 17 #include <atomic> 18 #include <map> 19 #include <vector> 20 #include <system_ability.h> 21 #include "event_runner.h" 22 #include "event_handler.h" 23 #include "delayed_sp_singleton.h" 24 #include "dump_common_utils.h" 25 #include "dump_broker_stub.h" 26 #include "system_ability_ondemand_reason.h" 27 namespace OHOS { 28 namespace HiviewDFX { 29 30 class RawParam; 31 #ifdef DUMP_TEST_MODE // for mock test 32 using DumpManagerServiceTestMainFunc = std::function<void(int argc, char *argv[], 33 const std::shared_ptr<RawParam>& reqCtl)>; 34 #endif // for mock test 35 class DumpManagerService final : public SystemAbility, public DumpBrokerStub { 36 DECLARE_SYSTEM_ABILITY(DumpManagerService) 37 public: 38 DumpManagerService(); 39 ~DumpManagerService(); 40 public: 41 virtual void OnStart() override; 42 virtual void OnStop() override; 43 public: 44 // Used for dump request 45 int32_t Request(std::vector<std::u16string> &args, int outfd) override; 46 public: 47 // Used for scan pid list over limit 48 int32_t ScanPidOverLimit(std::string requestType, int32_t limitSize, std::vector<int32_t> &pidList) override; 49 // Used for count fd nums 50 int32_t CountFdNums(int32_t pid, uint32_t &fdNums, std::string &detailFdInfo, 51 std::vector<std::string> &topLeakedTypeList) override; 52 public: 53 int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override; 54 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; IsServiceStarted()55 bool IsServiceStarted() const 56 { 57 return started_; 58 } 59 void DelayUnloadTask() override; 60 #ifdef DUMP_TEST_MODE // for mock test 61 void SetTestMainFunc(DumpManagerServiceTestMainFunc testMainFunc); 62 #endif // for mock test 63 private: 64 friend DumpDelayedSpSingleton<DumpManagerService>; 65 private: 66 bool Init(); 67 std::shared_ptr<RawParam> AddRequestRawParam(std::vector<std::u16string> &args, int outfd); 68 void EraseRequestRawParam(const std::shared_ptr<RawParam> rawParam); 69 void CancelAllRequest(); 70 int GetRequestSum(); 71 void GetIdleRequest(); 72 uint32_t GetRequestId(); 73 int32_t StartRequest(const std::shared_ptr<RawParam> rawParam); 74 void RequestMain(const std::shared_ptr<RawParam> rawParam); 75 bool HasDumpPermission() const; 76 uint32_t GetFileDescriptorNums(int32_t pid, std::string requestType) const; 77 std::string GetFdLink(const std::string &linkPath) const; 78 std::vector<std::string> GetFdLinks(int pid); 79 std::string MaybeKnownType(const std::string &link); 80 std::unordered_map<std::string, int> CountPaths(const std::vector<std::string>& links); 81 std::vector<std::pair<std::string, int>> TopN(const std::unordered_map<std::string, int>& counter, size_t n); 82 std::string GetSummary(const std::vector<std::pair<std::string, int>> &topLinks, 83 const std::vector<std::pair<std::string, int>> &topTypes); 84 std::string GetTopFdInfo(const std::vector<std::pair<std::string, int>> &topLinks); 85 std::string GetTopDirInfo(const std::vector<std::pair<std::string, int>> &topTypes, 86 const std::map<std::string, std::unordered_map<std::string, int>> &typePaths); 87 void SetCpuSchedAffinity(); 88 void HandleRequestError(std::vector<std::u16string> &args, int outfd, 89 const int32_t& errorCode, const std::string& errorMsg); 90 private: 91 std::mutex mutex_; 92 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 93 std::shared_ptr<AppExecFwk::EventHandler> handler_; 94 std::atomic<bool> started_ = false; 95 std::atomic<bool> blockRequest_ = false; 96 uint32_t requestIndex_ {0}; 97 std::map<uint32_t, std::shared_ptr<RawParam>> requestRawParamMap_; 98 #ifdef DUMP_TEST_MODE // for mock test 99 DumpManagerServiceTestMainFunc testMainFunc_ {nullptr}; 100 #endif // for mock test 101 }; 102 } // namespace HiviewDFX 103 } // namespace OHOS 104 #endif // HIDUMPER_SERVICES_DUMP_MANAGER_SERVICE_H 105