blob: 81ef33637f5ea102b6c9edbf9bf16eb0a6e75609 [file] [log] [blame]
Yabin Cui03381452017-12-13 11:31:53 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SIMPLE_PERF_OFFLINE_UNWINDER_H_
18#define SIMPLE_PERF_OFFLINE_UNWINDER_H_
19
Christopher Ferris15933b62018-02-22 19:06:42 -080020#include <memory>
Yabin Cui03381452017-12-13 11:31:53 -080021#include <vector>
Christopher Ferris15933b62018-02-22 19:06:42 -080022#include <unordered_map>
Yabin Cui03381452017-12-13 11:31:53 -080023
24#include "perf_regs.h"
25
Christopher Ferris15933b62018-02-22 19:06:42 -080026#include <backtrace/BacktraceMap.h>
27
Yabin Cui03381452017-12-13 11:31:53 -080028namespace simpleperf {
29struct ThreadEntry;
30
31struct UnwindingResult {
32 // time used for unwinding, in ns.
33 uint64_t used_time;
34 enum {
35 UNKNOWN_REASON,
36 EXCEED_MAX_FRAMES_LIMIT,
37 ACCESS_REG_FAILED,
38 ACCESS_STACK_FAILED,
39 ACCESS_MEM_FAILED,
40 FIND_PROC_INFO_FAILED,
41 EXECUTE_DWARF_INSTRUCTION_FAILED,
42 DIFFERENT_ARCH,
Yabin Cui750b3732017-12-18 17:46:36 -080043 MAP_MISSING,
Yabin Cui03381452017-12-13 11:31:53 -080044 } stop_reason;
45 union {
46 // for ACCESS_REG_FAILED
47 uint64_t regno;
48 // for ACCESS_MEM_FAILED and ACCESS_STACK_FAILED
49 uint64_t addr;
50 } stop_info;
Yabin Cui85faad92018-01-04 17:17:55 -080051 uint64_t stack_start;
52 uint64_t stack_end;
Yabin Cui03381452017-12-13 11:31:53 -080053};
54
55class OfflineUnwinder {
56 public:
Yabin Cui6e75e1b2018-02-06 13:42:16 -080057 OfflineUnwinder(bool collect_stat);
Yabin Cui03381452017-12-13 11:31:53 -080058
Yabin Cui6e75e1b2018-02-06 13:42:16 -080059 bool UnwindCallChain(const ThreadEntry& thread, const RegSet& regs, const char* stack,
60 size_t stack_size, std::vector<uint64_t>* ips, std::vector<uint64_t>* sps);
Yabin Cui03381452017-12-13 11:31:53 -080061
62 bool HasStat() const {
63 return collect_stat_;
64 }
65
66 const UnwindingResult& GetUnwindingResult() const {
67 return unwinding_result_;
68 }
69
70 private:
Yabin Cui03381452017-12-13 11:31:53 -080071 bool collect_stat_;
72 UnwindingResult unwinding_result_;
Christopher Ferris15933b62018-02-22 19:06:42 -080073
74 // Cache of the most recently used map.
75 struct CachedMap {
76 uint64_t version = 0u;
77 std::unique_ptr<BacktraceMap> map;
78 };
79 // use unused attribute to pass mac build.
80 std::unordered_map<pid_t, CachedMap> cached_maps_ __attribute__((unused));
Yabin Cui03381452017-12-13 11:31:53 -080081};
82
83} // namespace simpleperf
84
85#endif // SIMPLE_PERF_OFFLINE_UNWINDER_H_