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 std::unique_ptr<uint8_t[]> baseStackData; // save the shared memory data 34 BaseStackRawData* stackConext; // points to the foundation type data 35 uint8_t* stackData; 36 uint8_t* data; // fp mode data is ip, dwarf mode data is regs 37 uint32_t stackSize; 38 uint8_t fpDepth; // fp mode fpDepth is ip depth, dwarf mode is invalid 39 bool reportFlag; 40 bool reduceStackFlag; 41 }; 42 43 explicit StackDataRepeater(size_t maxSize); 44 ~StackDataRepeater(); 45 bool PutRawStack(const std::shared_ptr<RawStack>& rawData, bool isRecordAccurately); 46 std::shared_ptr<RawStack> TakeRawData(uint32_t during, clockid_t clockId, uint32_t batchCount, 47 std::shared_ptr<RawStack> batchRawStack[]); 48 void Close(); 49 void Reset(); 50 size_t Size(); 51 52 private: 53 std::mutex mutex_; 54 std::condition_variable slotCondVar_; 55 std::condition_variable itemCondVar_; 56 std::deque<std::shared_ptr<RawStack>> rawDataQueue_; 57 std::unordered_map<void *, std::shared_ptr<RawStack>> mallocMap_ = {}; 58 size_t maxSize_; 59 uint64_t reducedStackCount_; 60 bool closed_; 61 62 DISALLOW_COPY_AND_MOVE(StackDataRepeater); 63 }; 64 65 using RawStackPtr = STD_PTR(shared, StackDataRepeater::RawStack); 66 67 using StackDataRepeaterPtr = STD_PTR(shared, StackDataRepeater); 68 69 #endif // STACK_DATA_REPEATER_H