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 24 #include "buffer_writer.h" 25 #include "manager_interface.h" 26 #include "epoll_event_poller.h" 27 #include "share_memory_allocator.h" 28 #include "event_notifier.h" 29 #include "native_hook_config.pb.h" 30 #include "native_hook_result.pb.h" 31 #include "virtual_runtime.h" 32 #include "stack_data_repeater.h" 33 #include "stack_preprocess.h" 34 #include "native_memory_profiler_sa_config.h" 35 36 using BatchNativeHookDataPtr = STD_PTR(shared, BatchNativeHookData); 37 class ProfilerPluginConfig; 38 class PluginResult; 39 class CommandPoller; 40 41 struct HookContext { 42 int type; 43 pid_t pid; 44 pid_t tid; 45 void* addr; 46 uint32_t mallocSize; 47 }; 48 49 namespace OHOS::Developtools::NativeDaemon { 50 class HookService; 51 class HookManager : public ManagerInterface, public std::enable_shared_from_this<HookManager> { 52 public: 53 struct HookManagerCtx { HookManagerCtxHookManagerCtx54 HookManagerCtx(int32_t pid) : pid(pid) {} HookManagerCtxHookManagerCtx55 HookManagerCtx(const std::string& name) : processName(name) {} ~HookManagerCtxHookManagerCtx56 ~HookManagerCtx() {} 57 int32_t pid = -1; 58 std::string processName; 59 std::string smbName; 60 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock = nullptr; 61 std::shared_ptr<EventNotifier> eventNotifier = nullptr; 62 std::unique_ptr<EpollEventPoller> eventPoller = nullptr; 63 std::shared_ptr<StackDataRepeater> stackData = nullptr; 64 std::shared_ptr<StackPreprocess> stackPreprocess = nullptr; 65 bool isRecordAccurately = false; 66 }; 67 HookManager() = default; 68 bool RegisterAgentPlugin(const std::string& pluginPath); 69 bool UnregisterAgentPlugin(const std::string& pluginPath); 70 71 bool LoadPlugin(const std::string& pluginPath) override; 72 bool UnloadPlugin(const std::string& pluginPath) override; 73 bool UnloadPlugin(const uint32_t pluginId) override; 74 75 // CommandPoller will call the following four interfaces after receiving the command 76 bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config) override; 77 bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds) override; 78 bool StartPluginSession(const std::vector<uint32_t>& pluginIds, 79 const std::vector<ProfilerPluginConfig>& config, PluginResult& result) override; 80 bool StopPluginSession(const std::vector<uint32_t>& pluginIds) override; 81 bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds) override; 82 83 bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd, 84 bool isProtobufSerialize = true) override; 85 bool ResetWriter(uint32_t pluginId) override; 86 void SetCommandPoller(const std::shared_ptr<CommandPoller>& p) override; 87 void ResetStartupParam(); 88 void SethookStandalone(bool); 89 bool HandleHookContext(const std::shared_ptr<HookManagerCtx>& ctx); 90 void StartPluginSession(); 91 void ReadShareMemory(const std::shared_ptr<HookManagerCtx>& hookCtx); 92 void SetHookConfig(const NativeHookConfig& hookConfig); 93 void SetHookConfig(const std::shared_ptr<NativeMemoryProfilerSaConfig>& config); 94 int32_t CreatePluginSession(); 95 void RegisterWriter(const std::shared_ptr<Writer> writer); 96 void WriteHookConfig(); 97 std::pair<int, int> GetFds(int32_t pid, const std::string& name); SetSaServiceFlag(bool flag)98 inline void SetSaServiceFlag(bool flag) 99 { 100 isSaService_ = flag; 101 } 102 void GetClientConfig(ClientConfig& clientConfig); 103 104 private: 105 bool CheckProcess(); 106 bool CheckProcessName(); 107 void SetHookData(HookContext& hookContext, struct timespec ts, 108 std::vector<OHOS::Developtools::NativeDaemon::CallFrame>& callFrames, 109 BatchNativeHookDataPtr& batchNativeHookData); 110 111 std::shared_ptr<HookService> hookService_; 112 std::shared_ptr<CommandPoller> commandPoller_; 113 int agentIndex_ = -1; 114 std::string agentPluginName_; 115 NativeHookConfig hookConfig_; 116 std::unique_ptr<uint8_t[]> buffer_; 117 bool isHookStandalone_ {false}; 118 FILE* fpHookData_ {nullptr}; 119 std::vector<std::shared_ptr<HookManagerCtx>> hookCtx_; 120 bool isSaService_{false}; 121 bool noDataQueue_{false}; 122 }; 123 } 124 #endif // AGENT_MANAGER_H