1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 HOOK_MANAGER_H 17 #define HOOK_MANAGER_H 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 #include "hook_record_factory.h" 24 #include "hook_record.h" 25 #include "buffer_writer.h" 26 #include "manager_interface.h" 27 #include "epoll_event_poller.h" 28 #include "share_memory_allocator.h" 29 #include "event_notifier.h" 30 #include "native_hook_config.pb.h" 31 #include "native_hook_result.pb.h" 32 #include "virtual_runtime.h" 33 #include "stack_data_repeater.h" 34 #include "stack_preprocess.h" 35 #include "native_memory_profiler_sa_config.h" 36 #include "writer_adapter.h" 37 38 using BatchNativeHookDataPtr = STD_PTR(shared, BatchNativeHookData); 39 class ProfilerPluginConfig; 40 class PluginResult; 41 class CommandPoller; 42 43 struct HookContext { 44 int type; 45 pid_t pid; 46 pid_t tid; 47 void* addr; 48 uint32_t mallocSize; 49 }; 50 51 struct NmdParam { 52 uint32_t fd = 0; 53 uint32_t type = -1; 54 }; 55 56 namespace OHOS::Developtools::NativeDaemon { 57 class HookService; 58 class HookManager : public ManagerInterface, public std::enable_shared_from_this<HookManager> { 59 public: 60 struct HookManagerCtx { HookManagerCtxHookManagerCtx61 HookManagerCtx(int32_t pid) : pid(pid) {} HookManagerCtxHookManagerCtx62 HookManagerCtx(const std::string& name) : processName(name) {} ~HookManagerCtxHookManagerCtx63 ~HookManagerCtx() {} 64 int32_t pid = -1; 65 std::string processName; 66 std::vector<std::shared_ptr<ShareMemoryBlock>> shareMemoryBlockList; 67 std::vector<std::shared_ptr<EventNotifier>> eventNotifierList; 68 std::vector<std::shared_ptr<EpollEventPoller>> eventPollerList; 69 std::vector<std::string> smbNames; 70 std::shared_ptr<StackDataRepeater> stackData = nullptr; 71 std::shared_ptr<StackPreprocess> stackPreprocess = nullptr; 72 bool isRecordAccurately = false; 73 void FlushStackArray(); 74 }; 75 HookManager() = default; 76 ~HookManager(); 77 bool RegisterAgentPlugin(const std::string& pluginPath); 78 bool UnregisterAgentPlugin(const std::string& pluginPath); 79 80 bool LoadPlugin(const std::string& pluginPath) override; 81 bool UnloadPlugin(const std::string& pluginPath) override; 82 bool UnloadPlugin(const uint32_t pluginId) override; 83 84 // CommandPoller will call the following four interfaces after receiving the command 85 bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config) override; 86 bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds) override; 87 bool StartPluginSession(const std::vector<uint32_t>& pluginIds, 88 const std::vector<ProfilerPluginConfig>& config, PluginResult& result) override; 89 bool StopPluginSession(const std::vector<uint32_t>& pluginIds) override; 90 bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds) override; 91 92 bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd, 93 bool isProtobufSerialize = true) override; 94 bool ResetWriter(uint32_t pluginId) override; 95 void SetCommandPoller(const std::shared_ptr<CommandPoller>& p) override; 96 void ResetStartupParam(); 97 void SethookStandalone(bool); 98 bool HandleHookContext(const std::shared_ptr<HookManagerCtx>& ctx); 99 void StartPluginSession(std::unordered_map<pid_t, std::pair<uid_t, gid_t>>* pidInfo = nullptr); 100 void ReadShareMemory(const std::shared_ptr<HookManagerCtx>& hookCtx, int sharedMemoryIndex); 101 void SetHookConfig(const NativeHookConfig& hookConfig); 102 void SetHookConfig(const std::shared_ptr<NativeMemoryProfilerSaConfig>& config); 103 int32_t CreatePluginSession(); 104 void RegisterWriter(const std::shared_ptr<Writer> writer); 105 void WriteHookConfig(); 106 void DumpNmdInfo(const char* nmdResult); 107 std::string GetCmdArgs(const NativeHookConfig& traceConfig); 108 std::vector<int> GetFds(int32_t pid, const std::string& name, int sharedMemCount); SetSaServiceConfig(bool saFlag,bool isProtobufSerialize)109 inline void SetSaServiceConfig(bool saFlag, bool isProtobufSerialize) 110 { 111 isSaService_ = saFlag; 112 isProtobufSerialize_ = isProtobufSerialize; 113 } 114 void GetClientConfig(ClientConfig& clientConfig); GetNoDataQueueFlag()115 bool GetNoDataQueueFlag() 116 { 117 return noDataQueue_; 118 } SetPid(int pid)119 void SetPid(int pid) 120 { 121 pid_ = pid; 122 } GetPid()123 int GetPid() 124 { 125 return pid_; 126 } 127 GetHookConfig()128 NativeHookConfig GetHookConfig() 129 { 130 return hookConfig_; 131 } 132 SetSaMode(bool saMode)133 void SetSaMode(bool saMode) 134 { 135 saMode_ = saMode; 136 } 137 void SetNmdInfo(std::pair<uint32_t, uint32_t>); 138 void FlushRawStackArray(const std::shared_ptr<HookManagerCtx>& hookCtx, 139 std::shared_ptr<HookRecord>& hookRecord); 140 std::string simplifiedNmd_; 141 volatile bool nmdComplete_ = false; 142 143 private: 144 int pid_ = -1; // for SA mode 145 bool printMallocNmd_ = false; 146 bool saMode_ = false; 147 bool CheckProcess(); 148 bool CheckProcessName(); 149 void CheckHapEncryped(); 150 void SetHookData(HookContext& hookContext, struct timespec ts, 151 std::vector<OHOS::Developtools::NativeDaemon::CallFrame>& callFrames, 152 BatchNativeHookDataPtr& batchNativeHookData); 153 void CreateWriter(); 154 155 std::shared_ptr<HookService> hookService_; 156 std::shared_ptr<CommandPoller> commandPoller_; 157 int agentIndex_ = -1; 158 std::string agentPluginName_; 159 NativeHookConfig hookConfig_; 160 std::unique_ptr<uint8_t[]> buffer_; 161 bool isHookStandalone_ {false}; 162 FILE* fpHookData_ {nullptr}; 163 std::vector<std::shared_ptr<HookManagerCtx>> hookCtx_; 164 bool isSaService_{false}; 165 bool noDataQueue_{false}; 166 std::shared_ptr<WriterAdapter> writerAdapter_{nullptr}; 167 bool isProtobufSerialize_{true}; 168 std::shared_ptr<Writer> writer_{nullptr}; 169 constexpr static uint32_t MAX_BUFFER_SIZE = 100 * 1024 * 1024; 170 uint64_t shareMemorySize_{MAX_BUFFER_SIZE}; 171 NmdParam nmdParamInfo_; 172 uint32_t largestSize_ = 0; 173 uint32_t secondLargestSize_ = 0; 174 uint32_t maxGrowthSize_ = 0; 175 int32_t maxProcessCount_ = 4; 176 std::shared_ptr<HookRecordFactory> factory_{nullptr}; 177 }; 178 } 179 #endif // AGENT_MANAGER_H