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_PREPROCESS_H 16 #define STACK_PREPROCESS_H 17 18 #include <chrono> 19 #include <thread> 20 #include <unordered_map> 21 22 #include "logging.h" 23 #include "nocopyable.h" 24 #include "stack_data_repeater.h" 25 #include "buffer_writer.h" 26 #include "virtual_runtime.h" 27 #include "hook_common.h" 28 #include "native_hook_config.pb.h" 29 #include "native_hook_result.pb.h" 30 31 class StackPreprocess { 32 public: 33 explicit StackPreprocess(const StackDataRepeaterPtr& dataRepeater, NativeHookConfig hookConfig); 34 35 ~StackPreprocess(); 36 void SetWriter(const std::shared_ptr<BufferWriter>& writer); 37 bool StartTakeResults(); 38 bool StopTakeResults(); 39 40 private: 41 void TakeResults(); 42 void SetHookData(RawStackPtr RawStack, std::vector<OHOS::Developtools::NativeDaemon::CallFrame>& callsFrames, 43 BatchNativeHookData& batchNativeHookData); 44 void writeFrames(RawStackPtr RawStack, const std::vector<OHOS::Developtools::NativeDaemon::CallFrame>& callsFrames); 45 void SetFrameInfo(Frame& frame, OHOS::Developtools::NativeDaemon::CallFrame& callsFrame, 46 BatchNativeHookData& batchNativeHookData); 47 uint32_t GetThreadIdx(std::string threadName, BatchNativeHookData& batchNativeHookData); 48 private: 49 std::shared_ptr<BufferWriter> writer_ = nullptr; 50 StackDataRepeaterPtr dataRepeater_ = nullptr; 51 std::thread thread_ {}; 52 std::unique_ptr<uint8_t[]> buffer_; 53 bool isStopTakeData_ = false; 54 std::shared_ptr<OHOS::Developtools::NativeDaemon::VirtualRuntime> runtime_instance; 55 DISALLOW_COPY_AND_MOVE(StackPreprocess); 56 std::unordered_map<std::string, uint32_t> functionMap_; 57 std::unordered_map<std::string, uint32_t> fileMap_; 58 std::unordered_map<std::string, uint32_t> threadMap_; 59 NativeHookConfig hookConfig_; 60 std::unique_ptr<FILE, decltype(&fclose)> fpHookData_; 61 uint32_t ignoreCnts_ = 0; 62 uint32_t eventCnts_ = 0; 63 }; 64 65 #endif // STACK_PREPROCESS_H