1 /* 2 * Copyright (c) 2024 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 16 #ifndef RELIABILITY_SAMPLE_STACK_PRINTER_H 17 #define RELIABILITY_SAMPLE_STACK_PRINTER_H 18 19 #include <map> 20 #include <vector> 21 22 #include "unwinder.h" 23 #include "dfx_frame.h" 24 #include "thread_sampler.h" 25 #include "unique_stack_table.h" 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 struct SampleStackItem { 30 uintptr_t pc; 31 int32_t count; 32 uint64_t level; 33 std::shared_ptr<DfxFrame> current; 34 SampleStackItem* child; 35 SampleStackItem* siblings; 36 SampleStackItemSampleStackItem37 SampleStackItem() : pc(0), 38 count(0), 39 level(0), 40 current(nullptr), 41 child(nullptr), 42 siblings(nullptr) 43 {}; 44 }; 45 class SampleStackPrinter { 46 public: SampleStackPrinter(std::shared_ptr<Unwinder> unwinder,std::shared_ptr<DfxMaps> maps)47 SampleStackPrinter(std::shared_ptr<Unwinder> unwinder, std::shared_ptr<DfxMaps> maps) : unwinder_(unwinder), 48 maps_(maps) 49 { 50 root_ = nullptr; 51 }; 52 SampleStackPrinter(const SampleStackPrinter& other) = delete; 53 SampleStackPrinter& operator=(const SampleStackPrinter& other) = delete; ~SampleStackPrinter()54 ~SampleStackPrinter() 55 { 56 FreeNodes(); 57 }; 58 59 void Insert(std::vector<uintptr_t>& pcs, int32_t count); 60 std::string GetFullStack(const std::vector<TimeAndFrames>& timeAndFrameList); 61 std::string GetTreeStack(std::vector<StackIdAndCount>& stackIdCount, 62 std::unique_ptr<UniqueStackTable>& uniqueStackTable); 63 std::string Print(); 64 65 private: 66 void FreeNodes(); 67 SampleStackItem* Insert(SampleStackItem* curNode, 68 uintptr_t pc, int32_t count, uint64_t level, SampleStackItem* acientNode); 69 SampleStackItem* AdjustSiblings(SampleStackItem* acient, SampleStackItem* cur, SampleStackItem* node); 70 SampleStackItem* root_; 71 std::shared_ptr<Unwinder> unwinder_; 72 std::shared_ptr<DfxMaps> maps_; 73 }; 74 } // end of namespace HiviewDFX 75 } // end of namespace OHOS 76 #endif 77