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 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 "hook_service.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 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 HookManager : public ManagerInterface { 51 public: 52 HookManager(); 53 HookManager(std::shared_ptr<ShareMemoryBlock>& shareMemoryBlock, std::shared_ptr<EventNotifier>& eventNotifier, 54 StackDataRepeaterPtr& stackData); 55 bool RegisterAgentPlugin(const std::string& pluginPath); 56 bool UnregisterAgentPlugin(const std::string& pluginPath); 57 58 bool LoadPlugin(const std::string& pluginPath) override; 59 bool UnloadPlugin(const std::string& pluginPath) override; 60 bool UnloadPlugin(const uint32_t pluginId) override; 61 62 // CommandPoller will call the following four interfaces after receiving the command 63 bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config) override; 64 bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds) override; 65 bool StartPluginSession(const std::vector<uint32_t>& pluginIds, 66 const std::vector<ProfilerPluginConfig>& config, PluginResult& result) override; 67 bool StopPluginSession(const std::vector<uint32_t>& pluginIds) override; 68 bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds) override; 69 70 bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd) override; 71 bool ResetWriter(uint32_t pluginId) override; 72 void SetCommandPoller(const std::shared_ptr<CommandPoller>& p) override; 73 void ResetStartupParam(); 74 void ReadShareMemory(); 75 void SetHookConfig(const NativeHookConfig& hookConfig); 76 77 private: 78 void RegisterWriter(const BufferWriterPtr& writer); 79 bool CheckProcess(); 80 void CheckProcessName(); 81 void SetHookData(HookContext& hookContext, struct timespec ts, 82 std::vector<OHOS::Developtools::NativeDaemon::CallFrame>& callFrames, 83 BatchNativeHookDataPtr& batchNativeHookData); 84 void GetClientConfig(const NativeHookConfig& nativeHookConfig, ClientConfig& clientConfig); 85 86 std::shared_ptr<HookService> hookService_; 87 std::shared_ptr<CommandPoller> commandPoller_; 88 int agentIndex_ = -1; 89 std::string agentPluginName_; 90 std::unique_ptr<EpollEventPoller> eventPoller_; 91 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock_; 92 std::shared_ptr<EventNotifier> eventNotifier_; 93 NativeHookConfig hookConfig_; 94 std::string smbName_; 95 std::unique_ptr<uint8_t[]> buffer_; 96 StackDataRepeaterPtr stackData_; 97 std::shared_ptr<StackPreprocess> stackPreprocess_; 98 int pid_; 99 bool isRecordAccurately_ = false; 100 }; 101 } 102 #endif // AGENT_MANAGER_H