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