tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef SIMPLE_PERF_OFFLINE_UNWINDER_H_
#define SIMPLE_PERF_OFFLINE_UNWINDER_H_
 
#include <memory>
#include <vector>
#include <unordered_map>
 
#include "perf_regs.h"
#include "thread_tree.h"
 
#if defined(__linux__)
#include <unwindstack/Maps.h>
#endif
 
namespace simpleperf {
struct ThreadEntry;
 
struct UnwindingResult {
  // time used for unwinding, in ns.
  uint64_t used_time;
  enum {
    UNKNOWN_REASON,
    EXCEED_MAX_FRAMES_LIMIT,
    ACCESS_REG_FAILED,
    ACCESS_STACK_FAILED,
    ACCESS_MEM_FAILED,
    FIND_PROC_INFO_FAILED,
    EXECUTE_DWARF_INSTRUCTION_FAILED,
    DIFFERENT_ARCH,
    MAP_MISSING,
  } stop_reason;
  union {
    // for ACCESS_REG_FAILED
    uint64_t regno;
    // for ACCESS_MEM_FAILED and ACCESS_STACK_FAILED
    uint64_t addr;
  } stop_info;
  uint64_t stack_start;
  uint64_t stack_end;
};
 
#if defined(__linux__)
class UnwindMaps : public unwindstack::Maps {
 public:
  void UpdateMaps(const MapSet& map_set);
 
 private:
  uint64_t version_ = 0u;
  std::vector<const MapEntry*> entries_;
};
 
class OfflineUnwinder {
 public:
  OfflineUnwinder(bool collect_stat);
 
  bool UnwindCallChain(const ThreadEntry& thread, const RegSet& regs, const char* stack,
                       size_t stack_size, std::vector<uint64_t>* ips, std::vector<uint64_t>* sps);
 
  bool HasStat() const {
    return collect_stat_;
  }
 
  const UnwindingResult& GetUnwindingResult() const {
    return unwinding_result_;
  }
 
  bool IsCallChainBrokenForIncompleteJITDebugInfo() {
    return is_callchain_broken_for_incomplete_jit_debug_info_;
  }
 
 private:
  bool collect_stat_;
  UnwindingResult unwinding_result_;
  bool is_callchain_broken_for_incomplete_jit_debug_info_;
 
  std::unordered_map<pid_t, UnwindMaps> cached_maps_;
};
 
#else  // defined(__linux__)
 
class OfflineUnwinder {
 public:
  OfflineUnwinder(bool) {}
  bool UnwindCallChain(const ThreadEntry&, const RegSet&, const char*, size_t,
                       std::vector<uint64_t>*, std::vector<uint64_t>*) {
    return false;
  }
};
 
#endif  // !defined(__linux__)
 
} // namespace simpleperf
 
#endif  // SIMPLE_PERF_OFFLINE_UNWINDER_H_