• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <stdint.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include <memory>
24 #include <mutex>
25 #include <optional>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include <unwindstack/Arch.h>
31 #include <unwindstack/DexFiles.h>
32 #include <unwindstack/Error.h>
33 #include <unwindstack/JitDebug.h>
34 #include <unwindstack/Maps.h>
35 #include <unwindstack/Memory.h>
36 #include <unwindstack/Regs.h>
37 #include <unwindstack/SharedString.h>
38 #include <unwindstack/Unwinder.h>
39 
40 namespace unwindstack {
41 
42 struct AndroidUnwinderData {
43   AndroidUnwinderData() = default;
AndroidUnwinderDataAndroidUnwinderData44   explicit AndroidUnwinderData(const size_t max_frames) : max_frames(max_frames) {}
AndroidUnwinderDataAndroidUnwinderData45   explicit AndroidUnwinderData(const bool show_all_frames) : show_all_frames(show_all_frames) {}
46 
47   void DemangleFunctionNames();
48 
49   std::string GetErrorString();
50 
51   std::vector<FrameData> frames;
52   ErrorData error;
53   std::optional<std::unique_ptr<Regs>> saved_initial_regs;
54   const std::optional<size_t> max_frames;
55   const bool show_all_frames = false;
56 };
57 
58 class AndroidUnwinder {
59  public:
AndroidUnwinder(pid_t pid)60   AndroidUnwinder(pid_t pid) : pid_(pid) {}
AndroidUnwinder(pid_t pid,std::shared_ptr<Memory> & memory)61   AndroidUnwinder(pid_t pid, std::shared_ptr<Memory>& memory)
62       : pid_(pid), process_memory_(memory) {}
AndroidUnwinder(pid_t pid,ArchEnum arch)63   AndroidUnwinder(pid_t pid, ArchEnum arch) : pid_(pid), arch_(arch) {}
AndroidUnwinder(pid_t pid,const std::vector<std::string> initial_map_names_to_skip)64   AndroidUnwinder(pid_t pid, const std::vector<std::string> initial_map_names_to_skip)
65       : pid_(pid), initial_map_names_to_skip_(std::move(initial_map_names_to_skip)) {}
AndroidUnwinder(pid_t pid,const std::vector<std::string> initial_map_names_to_skip,const std::vector<std::string> map_suffixes_to_ignore)66   AndroidUnwinder(pid_t pid, const std::vector<std::string> initial_map_names_to_skip,
67                   const std::vector<std::string> map_suffixes_to_ignore)
68       : pid_(pid),
69         initial_map_names_to_skip_(std::move(initial_map_names_to_skip)),
70         map_suffixes_to_ignore_(std::move(map_suffixes_to_ignore)) {}
71   virtual ~AndroidUnwinder() = default;
72 
73   bool Initialize(ErrorData& error);
74 
GetProcessMemory()75   std::shared_ptr<Memory>& GetProcessMemory() { return process_memory_; }
GetMaps()76   unwindstack::Maps* GetMaps() { return maps_.get(); }
77 
GetJitDebug()78   const JitDebug& GetJitDebug() { return *jit_debug_.get(); }
GetDexFiles()79   const DexFiles& GetDexFiles() { return *dex_files_.get(); }
80 
81   std::string FormatFrame(const FrameData& frame) const;
82 
83   bool Unwind(AndroidUnwinderData& data);
84   bool Unwind(std::optional<pid_t> tid, AndroidUnwinderData& data);
85   bool Unwind(void* ucontext, AndroidUnwinderData& data);
86   bool Unwind(Regs* initial_regs, AndroidUnwinderData& data);
87 
88   FrameData BuildFrameFromPcOnly(uint64_t pc);
89 
90   static AndroidUnwinder* Create(pid_t pid);
91 
92  protected:
93   virtual bool InternalInitialize(ErrorData& error) = 0;
94 
95   virtual bool InternalUnwind(std::optional<pid_t> tid, AndroidUnwinderData& data) = 0;
96 
97   pid_t pid_;
98 
99   size_t max_frames_ = kMaxNumFrames;
100   std::vector<std::string> initial_map_names_to_skip_;
101   std::vector<std::string> map_suffixes_to_ignore_;
102   std::once_flag initialize_;
103   bool initialize_status_ = false;
104 
105   ArchEnum arch_ = ARCH_UNKNOWN;
106 
107   std::shared_ptr<Maps> maps_;
108   std::shared_ptr<Memory> process_memory_;
109   std::unique_ptr<JitDebug> jit_debug_;
110   std::unique_ptr<DexFiles> dex_files_;
111 
112   static constexpr size_t kMaxNumFrames = 512;
113 };
114 
115 class AndroidLocalUnwinder : public AndroidUnwinder {
116  public:
AndroidLocalUnwinder()117   AndroidLocalUnwinder() : AndroidUnwinder(getpid()) {
118     initial_map_names_to_skip_.emplace_back(kUnwindstackLib);
119   }
AndroidLocalUnwinder(std::shared_ptr<Memory> & process_memory)120   AndroidLocalUnwinder(std::shared_ptr<Memory>& process_memory)
121       : AndroidUnwinder(getpid(), process_memory) {
122     initial_map_names_to_skip_.emplace_back(kUnwindstackLib);
123   }
AndroidLocalUnwinder(const std::vector<std::string> & initial_map_names_to_skip)124   AndroidLocalUnwinder(const std::vector<std::string>& initial_map_names_to_skip)
125       : AndroidUnwinder(getpid(), initial_map_names_to_skip) {
126     initial_map_names_to_skip_.emplace_back(kUnwindstackLib);
127   }
AndroidLocalUnwinder(const std::vector<std::string> & initial_map_names_to_skip,const std::vector<std::string> & map_suffixes_to_ignore)128   AndroidLocalUnwinder(const std::vector<std::string>& initial_map_names_to_skip,
129                        const std::vector<std::string>& map_suffixes_to_ignore)
130       : AndroidUnwinder(getpid(), initial_map_names_to_skip, map_suffixes_to_ignore) {
131     initial_map_names_to_skip_.emplace_back(kUnwindstackLib);
132   }
133   virtual ~AndroidLocalUnwinder() = default;
134 
135  protected:
136   static constexpr const char* kUnwindstackLib = "libunwindstack.so";
137 
138   bool InternalInitialize(ErrorData& error) override;
139 
140   bool InternalUnwind(std::optional<pid_t> tid, AndroidUnwinderData& data) override;
141 };
142 
143 class AndroidRemoteUnwinder : public AndroidUnwinder {
144  public:
AndroidRemoteUnwinder(pid_t pid)145   AndroidRemoteUnwinder(pid_t pid) : AndroidUnwinder(pid) {}
AndroidRemoteUnwinder(pid_t pid,std::shared_ptr<Memory> & process_memory)146   AndroidRemoteUnwinder(pid_t pid, std::shared_ptr<Memory>& process_memory)
147       : AndroidUnwinder(pid, process_memory) {}
AndroidRemoteUnwinder(pid_t pid,ArchEnum arch)148   AndroidRemoteUnwinder(pid_t pid, ArchEnum arch) : AndroidUnwinder(pid, arch) {}
AndroidRemoteUnwinder(pid_t pid,const std::vector<std::string> initial_map_names_to_skip)149   AndroidRemoteUnwinder(pid_t pid, const std::vector<std::string> initial_map_names_to_skip)
150       : AndroidUnwinder(pid, initial_map_names_to_skip) {}
AndroidRemoteUnwinder(pid_t pid,const std::vector<std::string> initial_map_names_to_skip,const std::vector<std::string> map_suffixes_to_ignore)151   AndroidRemoteUnwinder(pid_t pid, const std::vector<std::string> initial_map_names_to_skip,
152                         const std::vector<std::string> map_suffixes_to_ignore)
153       : AndroidUnwinder(pid, initial_map_names_to_skip, map_suffixes_to_ignore) {}
154   virtual ~AndroidRemoteUnwinder() = default;
155 
156  protected:
157   bool InternalInitialize(ErrorData& error) override;
158 
159   bool InternalUnwind(std::optional<pid_t> tid, AndroidUnwinderData& data) override;
160 };
161 
162 }  // namespace unwindstack
163