• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     };
~SampleStackPrinter()52     ~SampleStackPrinter()
53     {
54         FreeNodes();
55     };
56 
57     void Insert(std::vector<uintptr_t>& pcs, int32_t count);
58     std::string GetFullStack(std::vector<TimeAndFrames>& timeAndFrameList);
59     std::string GetTreeStack(std::map<uint64_t, std::vector<uint64_t>>& stackIdTimeMap,
60         std::unique_ptr<UniqueStackTable>& uniqueStackTable);
61     std::string Print();
62 
63 private:
64     void FreeNodes();
65     SampleStackItem* Insert(SampleStackItem* curNode, uintptr_t pc, int32_t count, uint64_t level);
66     SampleStackItem* root_;
67     std::shared_ptr<Unwinder> unwinder_;
68     std::shared_ptr<DfxMaps> maps_;
69     std::vector<SampleStackItem*> allNodes_;
70 };
71 } // end of namespace HiviewDFX
72 } // end of namespace OHOS
73 #endif
74