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 #ifndef STACK_DATA_REPEATER_H 16 #define STACK_DATA_REPEATER_H 17 18 #include <condition_variable> 19 #include <deque> 20 #include <memory> 21 #include <mutex> 22 #include "logging.h" 23 #include "nocopyable.h" 24 #include "native_hook_result.pb.h" 25 #include "hook_common.h" 26 #include "utilities.h" 27 28 using BatchNativeHookDataPtr = STD_PTR(shared, BatchNativeHookData); 29 30 class StackDataRepeater { 31 public: 32 struct RawStack { 33 StackRawData stackConext; 34 bool reportFlag; 35 bool reduceStackFlag; 36 uint32_t stackSize; 37 std::unique_ptr<uint8_t[]> stackData; 38 }; 39 40 explicit StackDataRepeater(size_t maxSize); 41 ~StackDataRepeater(); 42 bool PutRawStack(const std::shared_ptr<RawStack>& rawData); 43 std::shared_ptr<RawStack> TakeRawData(uint32_t during, uint32_t batchCount, 44 std::shared_ptr<RawStack> batchRawStack[]); 45 void Close(); 46 void Reset(); 47 size_t Size(); 48 49 private: 50 std::mutex mutex_; 51 std::condition_variable slotCondVar_; 52 std::condition_variable itemCondVar_; 53 std::deque<std::shared_ptr<RawStack>> rawDataQueue_; 54 std::unordered_map<void *, std::shared_ptr<RawStack>> mallocMap_ = {}; 55 size_t maxSize_; 56 uint64_t reducedStackCount_; 57 bool closed_; 58 59 DISALLOW_COPY_AND_MOVE(StackDataRepeater); 60 }; 61 62 using RawStackPtr = STD_PTR(shared, StackDataRepeater::RawStack); 63 64 using StackDataRepeaterPtr = STD_PTR(shared, StackDataRepeater); 65 66 #endif // STACK_DATA_REPEATER_H