106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#ifndef HOOK_MANAGER_H 1706f6ba60Sopenharmony_ci#define HOOK_MANAGER_H 1806f6ba60Sopenharmony_ci 1906f6ba60Sopenharmony_ci#include <map> 2006f6ba60Sopenharmony_ci#include <memory> 2106f6ba60Sopenharmony_ci#include <string> 2206f6ba60Sopenharmony_ci#include <vector> 2306f6ba60Sopenharmony_ci 2406f6ba60Sopenharmony_ci#include "buffer_writer.h" 2506f6ba60Sopenharmony_ci#include "manager_interface.h" 2606f6ba60Sopenharmony_ci#include "epoll_event_poller.h" 2706f6ba60Sopenharmony_ci#include "share_memory_allocator.h" 2806f6ba60Sopenharmony_ci#include "event_notifier.h" 2906f6ba60Sopenharmony_ci#include "native_hook_config.pb.h" 3006f6ba60Sopenharmony_ci#include "native_hook_result.pb.h" 3106f6ba60Sopenharmony_ci#include "virtual_runtime.h" 3206f6ba60Sopenharmony_ci#include "stack_data_repeater.h" 3306f6ba60Sopenharmony_ci#include "stack_preprocess.h" 3406f6ba60Sopenharmony_ci#include "native_memory_profiler_sa_config.h" 3506f6ba60Sopenharmony_ci#include "writer_adapter.h" 3606f6ba60Sopenharmony_ci 3706f6ba60Sopenharmony_ciusing BatchNativeHookDataPtr = STD_PTR(shared, BatchNativeHookData); 3806f6ba60Sopenharmony_ciclass ProfilerPluginConfig; 3906f6ba60Sopenharmony_ciclass PluginResult; 4006f6ba60Sopenharmony_ciclass CommandPoller; 4106f6ba60Sopenharmony_ci 4206f6ba60Sopenharmony_cistruct HookContext { 4306f6ba60Sopenharmony_ci int type; 4406f6ba60Sopenharmony_ci pid_t pid; 4506f6ba60Sopenharmony_ci pid_t tid; 4606f6ba60Sopenharmony_ci void* addr; 4706f6ba60Sopenharmony_ci uint32_t mallocSize; 4806f6ba60Sopenharmony_ci}; 4906f6ba60Sopenharmony_ci 5006f6ba60Sopenharmony_cistruct NmdParam { 5106f6ba60Sopenharmony_ci uint32_t fd = 0; 5206f6ba60Sopenharmony_ci uint32_t type = -1; 5306f6ba60Sopenharmony_ci}; 5406f6ba60Sopenharmony_ci 5506f6ba60Sopenharmony_cinamespace OHOS::Developtools::NativeDaemon { 5606f6ba60Sopenharmony_ciclass HookService; 5706f6ba60Sopenharmony_ciclass HookManager : public ManagerInterface, public std::enable_shared_from_this<HookManager> { 5806f6ba60Sopenharmony_cipublic: 5906f6ba60Sopenharmony_ci struct HookManagerCtx { 6006f6ba60Sopenharmony_ci HookManagerCtx(int32_t pid) : pid(pid) {} 6106f6ba60Sopenharmony_ci HookManagerCtx(const std::string& name) : processName(name) {} 6206f6ba60Sopenharmony_ci ~HookManagerCtx() {} 6306f6ba60Sopenharmony_ci int32_t pid = -1; 6406f6ba60Sopenharmony_ci std::string processName; 6506f6ba60Sopenharmony_ci std::string smbName; 6606f6ba60Sopenharmony_ci std::shared_ptr<ShareMemoryBlock> shareMemoryBlock = nullptr; 6706f6ba60Sopenharmony_ci std::shared_ptr<EventNotifier> eventNotifier = nullptr; 6806f6ba60Sopenharmony_ci std::unique_ptr<EpollEventPoller> eventPoller = nullptr; 6906f6ba60Sopenharmony_ci std::shared_ptr<StackDataRepeater> stackData = nullptr; 7006f6ba60Sopenharmony_ci std::shared_ptr<StackPreprocess> stackPreprocess = nullptr; 7106f6ba60Sopenharmony_ci bool isRecordAccurately = false; 7206f6ba60Sopenharmony_ci std::array<std::shared_ptr<StackDataRepeater::RawStack>, CACHE_ARRAY_SIZE> rawDataArray = {}; 7306f6ba60Sopenharmony_ci uint32_t rawStackCount = 0; 7406f6ba60Sopenharmony_ci void FlushStackArray(); 7506f6ba60Sopenharmony_ci }; 7606f6ba60Sopenharmony_ci HookManager() = default; 7706f6ba60Sopenharmony_ci ~HookManager(); 7806f6ba60Sopenharmony_ci bool RegisterAgentPlugin(const std::string& pluginPath); 7906f6ba60Sopenharmony_ci bool UnregisterAgentPlugin(const std::string& pluginPath); 8006f6ba60Sopenharmony_ci 8106f6ba60Sopenharmony_ci bool LoadPlugin(const std::string& pluginPath) override; 8206f6ba60Sopenharmony_ci bool UnloadPlugin(const std::string& pluginPath) override; 8306f6ba60Sopenharmony_ci bool UnloadPlugin(const uint32_t pluginId) override; 8406f6ba60Sopenharmony_ci 8506f6ba60Sopenharmony_ci // CommandPoller will call the following four interfaces after receiving the command 8606f6ba60Sopenharmony_ci bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config) override; 8706f6ba60Sopenharmony_ci bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds) override; 8806f6ba60Sopenharmony_ci bool StartPluginSession(const std::vector<uint32_t>& pluginIds, 8906f6ba60Sopenharmony_ci const std::vector<ProfilerPluginConfig>& config, PluginResult& result) override; 9006f6ba60Sopenharmony_ci bool StopPluginSession(const std::vector<uint32_t>& pluginIds) override; 9106f6ba60Sopenharmony_ci bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds) override; 9206f6ba60Sopenharmony_ci 9306f6ba60Sopenharmony_ci bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd, 9406f6ba60Sopenharmony_ci bool isProtobufSerialize = true) override; 9506f6ba60Sopenharmony_ci bool ResetWriter(uint32_t pluginId) override; 9606f6ba60Sopenharmony_ci void SetCommandPoller(const std::shared_ptr<CommandPoller>& p) override; 9706f6ba60Sopenharmony_ci void ResetStartupParam(); 9806f6ba60Sopenharmony_ci void SethookStandalone(bool); 9906f6ba60Sopenharmony_ci bool HandleHookContext(const std::shared_ptr<HookManagerCtx>& ctx); 10006f6ba60Sopenharmony_ci void StartPluginSession(); 10106f6ba60Sopenharmony_ci void ReadShareMemory(const std::shared_ptr<HookManagerCtx>& hookCtx); 10206f6ba60Sopenharmony_ci void SetHookConfig(const NativeHookConfig& hookConfig); 10306f6ba60Sopenharmony_ci void SetHookConfig(const std::shared_ptr<NativeMemoryProfilerSaConfig>& config); 10406f6ba60Sopenharmony_ci int32_t CreatePluginSession(); 10506f6ba60Sopenharmony_ci void RegisterWriter(const std::shared_ptr<Writer> writer); 10606f6ba60Sopenharmony_ci void WriteHookConfig(); 10706f6ba60Sopenharmony_ci std::pair<int, int> GetFds(int32_t pid, const std::string& name); 10806f6ba60Sopenharmony_ci inline void SetSaServiceConfig(bool saFlag, bool isProtobufSerialize) 10906f6ba60Sopenharmony_ci { 11006f6ba60Sopenharmony_ci isSaService_ = saFlag; 11106f6ba60Sopenharmony_ci isProtobufSerialize_ = isProtobufSerialize; 11206f6ba60Sopenharmony_ci } 11306f6ba60Sopenharmony_ci void GetClientConfig(ClientConfig& clientConfig); 11406f6ba60Sopenharmony_ci bool GetNoDataQueueFlag() 11506f6ba60Sopenharmony_ci { 11606f6ba60Sopenharmony_ci return noDataQueue_; 11706f6ba60Sopenharmony_ci } 11806f6ba60Sopenharmony_ci void SetPid(int pid) 11906f6ba60Sopenharmony_ci { 12006f6ba60Sopenharmony_ci pid_ = pid; 12106f6ba60Sopenharmony_ci } 12206f6ba60Sopenharmony_ci int GetPid() 12306f6ba60Sopenharmony_ci { 12406f6ba60Sopenharmony_ci return pid_; 12506f6ba60Sopenharmony_ci } 12606f6ba60Sopenharmony_ci void SetSaMode(bool saMode) 12706f6ba60Sopenharmony_ci { 12806f6ba60Sopenharmony_ci saMode_ = saMode; 12906f6ba60Sopenharmony_ci } 13006f6ba60Sopenharmony_ci void SetNmdInfo(std::pair<uint32_t, uint32_t>); 13106f6ba60Sopenharmony_ci void FlushRawStackArray(const std::shared_ptr<HookManagerCtx>& hookCtx, 13206f6ba60Sopenharmony_ci std::shared_ptr<StackDataRepeater::RawStack>& rawStack); 13306f6ba60Sopenharmony_ci 13406f6ba60Sopenharmony_ciprivate: 13506f6ba60Sopenharmony_ci int pid_ = -1; // for SA mode 13606f6ba60Sopenharmony_ci bool printMallocNmd_ = false; 13706f6ba60Sopenharmony_ci bool saMode_ = false; 13806f6ba60Sopenharmony_ci bool CheckProcess(); 13906f6ba60Sopenharmony_ci bool CheckProcessName(); 14006f6ba60Sopenharmony_ci void CheckHapEncryped(); 14106f6ba60Sopenharmony_ci void SetHookData(HookContext& hookContext, struct timespec ts, 14206f6ba60Sopenharmony_ci std::vector<OHOS::Developtools::NativeDaemon::CallFrame>& callFrames, 14306f6ba60Sopenharmony_ci BatchNativeHookDataPtr& batchNativeHookData); 14406f6ba60Sopenharmony_ci void CreateWriter(); 14506f6ba60Sopenharmony_ci 14606f6ba60Sopenharmony_ci std::shared_ptr<HookService> hookService_; 14706f6ba60Sopenharmony_ci std::shared_ptr<CommandPoller> commandPoller_; 14806f6ba60Sopenharmony_ci int agentIndex_ = -1; 14906f6ba60Sopenharmony_ci std::string agentPluginName_; 15006f6ba60Sopenharmony_ci NativeHookConfig hookConfig_; 15106f6ba60Sopenharmony_ci std::unique_ptr<uint8_t[]> buffer_; 15206f6ba60Sopenharmony_ci bool isHookStandalone_ {false}; 15306f6ba60Sopenharmony_ci FILE* fpHookData_ {nullptr}; 15406f6ba60Sopenharmony_ci std::vector<std::shared_ptr<HookManagerCtx>> hookCtx_; 15506f6ba60Sopenharmony_ci bool isSaService_{false}; 15606f6ba60Sopenharmony_ci bool noDataQueue_{false}; 15706f6ba60Sopenharmony_ci std::shared_ptr<WriterAdapter> writerAdapter_{nullptr}; 15806f6ba60Sopenharmony_ci bool isProtobufSerialize_{true}; 15906f6ba60Sopenharmony_ci std::shared_ptr<Writer> writer_{nullptr}; 16006f6ba60Sopenharmony_ci constexpr static uint32_t MAX_BUFFER_SIZE = 100 * 1024 * 1024; 16106f6ba60Sopenharmony_ci uint64_t shareMemorySize_{MAX_BUFFER_SIZE}; 16206f6ba60Sopenharmony_ci NmdParam nmdParamInfo_; 16306f6ba60Sopenharmony_ci}; 16406f6ba60Sopenharmony_ci} 16506f6ba60Sopenharmony_ci#endif // AGENT_MANAGER_H