/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef HOOK_MANAGER_H #define HOOK_MANAGER_H #include #include #include #include #include "buffer_writer.h" #include "manager_interface.h" #include "hook_service.h" #include "epoll_event_poller.h" #include "share_memory_allocator.h" #include "event_notifier.h" #include "native_hook_config.pb.h" #include "native_hook_result.pb.h" #include "virtual_runtime.h" #include "stack_data_repeater.h" #include "stack_preprocess.h" using BatchNativeHookDataPtr = STD_PTR(shared, BatchNativeHookData); class ProfilerPluginConfig; class PluginResult; class CommandPoller; struct HookContext { int type; pid_t pid; pid_t tid; void* addr; uint32_t mallocSize; }; class HookManager : public ManagerInterface { public: HookManager(); bool RegisterAgentPlugin(const std::string& pluginPath); bool UnregisterAgentPlugin(const std::string& pluginPath); bool LoadPlugin(const std::string& pluginPath) override; bool UnloadPlugin(const std::string& pluginPath) override; bool UnloadPlugin(const uint32_t pluginId) override; // CommandPoller will call the following four interfaces after receiving the command bool CreatePluginSession(const std::vector& config) override; bool DestroyPluginSession(const std::vector& pluginIds) override; bool StartPluginSession(const std::vector& pluginIds, const std::vector& config, PluginResult& result) override; bool StopPluginSession(const std::vector& pluginIds) override; bool ReportPluginBasicData(const std::vector& pluginIds) override; bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd) override; bool ResetWriter(uint32_t pluginId) override; void SetCommandPoller(const std::shared_ptr& p) override; private: void ReadShareMemory(); void RegisterWriter(const BufferWriterPtr& writer); bool SendProtobufPackage(uint8_t *cache, size_t length); bool CheckProcess(); void CheckProcessName(); void SetHookData(HookContext& hookContext, struct timespec ts, std::vector& callsFrames, BatchNativeHookDataPtr& batchNativeHookData); std::shared_ptr hookService_; std::shared_ptr commandPoller_; int agentIndex_ = -1; std::string agentPluginName_; std::unique_ptr eventPoller_; std::shared_ptr shareMemoryBlock_; std::shared_ptr eventNotifier_; NativeHookConfig hookConfig_; std::string smbName_; std::unique_ptr buffer_; StackDataRepeaterPtr stackData_; std::shared_ptr stackPreprocess_; int pid_; }; #endif // AGENT_MANAGER_H