1 /*
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 _LIBBACKTRACE_UNWIND_OFFLINE_H
18 #define _LIBBACKTRACE_UNWIND_OFFLINE_H
19 
20 #include <libunwind.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23 #include <ucontext.h>
24 
25 #include <unordered_map>
26 #include <unordered_set>
27 
28 #include <backtrace/Backtrace.h>
29 
30 struct Space {
31   uint64_t start;
32   uint64_t end;
33   const uint8_t* data;
34 
SpaceSpace35   Space() {
36     Clear();
37   }
38 
39   void Clear();
40   size_t Read(uint64_t addr, uint8_t* buffer, size_t size);
41 };
42 
43 struct DebugFrameInfo;
44 
45 class BacktraceOffline : public Backtrace {
46  public:
BacktraceOffline(pid_t pid,pid_t tid,BacktraceMap * map,const backtrace_stackinfo_t & stack,bool cache_file)47   BacktraceOffline(pid_t pid, pid_t tid, BacktraceMap* map, const backtrace_stackinfo_t& stack,
48                    bool cache_file)
49       : Backtrace(pid, tid, map),
50         cache_file_(cache_file),
51         context_(nullptr) {
52     stack_space_.start = stack.start;
53     stack_space_.end = stack.end;
54     stack_space_.data = stack.data;
55   }
56 
57   virtual ~BacktraceOffline() = default;
58 
59   bool Unwind(size_t num_ignore_frames, ucontext_t* context) override;
60 
61   bool ReadWord(uintptr_t ptr, word_t* out_value) override;
62 
63   size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes) override;
64 
65   bool FindProcInfo(unw_addr_space_t addr_space, uint64_t ip, unw_proc_info_t* proc_info,
66                     int need_unwind_info);
67 
68   bool ReadReg(size_t reg_index, uint64_t* value);
69 
70  protected:
71   std::string GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) override;
72   DebugFrameInfo* GetDebugFrameInFile(const std::string& filename);
73 
74   bool cache_file_;
75   ucontext_t* context_;
76   Space eh_frame_hdr_space_;
77   Space eh_frame_space_;
78   Space arm_extab_space_;
79   Space arm_exidx_space_;
80   Space stack_space_;
81 };
82 
83 #endif  // _LIBBACKTRACE_BACKTRACE_OFFLINE_H
84