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_SOCKET_CLIENT 17 #define HOOK_SOCKET_CLIENT 18 19 #include "hook_common.h" 20 #include "service_base.h" 21 #include "stack_writer.h" 22 23 class UnixSocketClient; 24 class Sampling; 25 26 class HookSocketClient : public ServiceBase { 27 public: 28 HookSocketClient(int pid, ClientConfig *config, Sampling *sampler, 29 std::atomic<Range>* targetedRange = nullptr, std::atomic<int>* memCount = nullptr, 30 void (*disableHookCallback)() = nullptr); 31 ~HookSocketClient(); 32 bool Connect(const std::string addrname); 33 bool ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size) override; GetSmbFds()34 std::vector<int> GetSmbFds() 35 { 36 return smbFds_; 37 } GetEventFds()38 std::vector<int> GetEventFds() 39 { 40 return eventFds_; 41 } 42 void Flush(); 43 bool SendStack(const void* data, size_t size); 44 bool SendStackWithPayload(const void* data, size_t size, const void* payload, 45 size_t payloadSize, int smbIndex = 0); 46 void DisableHook(); 47 bool PeerIsConnected(); 48 bool SendNmdInfo(); 49 bool SendSimplifiedNmdInfo(); 50 bool SendEndMsg(); GetNmdType()51 int GetNmdType() 52 { 53 return nmdType_; 54 } 55 56 private: 57 std::shared_ptr<UnixSocketClient> unixSocketClient_; 58 std::vector<int> smbFds_; 59 std::vector<int> eventFds_; 60 int pid_; 61 int nmdType_ = -1; 62 ClientConfig *config_ = nullptr; 63 Sampling *sampler_ = nullptr; 64 std::atomic<Range>* targetedRange_ = nullptr; 65 std::atomic<int>* sharedMemCount_ = nullptr; 66 std::vector<std::shared_ptr<StackWriter>> stackWriterList_; 67 void (*disableHookCallback_)(){nullptr}; 68 uint32_t largestSize_ = 0; 69 uint32_t secondLargestSize_ = 0; 70 uint32_t maxGrowthSize_ = 0; 71 uint32_t sampleInterval_ = 0; 72 }; 73 74 #endif // HOOK_SOCKET_CLIENT