• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #include <stdint.h>
18 #include <sys/ptrace.h>
19 #include <sys/uio.h>
20 
21 #include <algorithm>
22 #include <vector>
23 
24 #include <unwindstack/Elf.h>
25 #include <unwindstack/MapInfo.h>
26 #include <unwindstack/Regs.h>
27 #include <unwindstack/RegsArm.h>
28 #include <unwindstack/RegsArm64.h>
29 #include <unwindstack/RegsX86.h>
30 #include <unwindstack/RegsX86_64.h>
31 #include <unwindstack/UserArm.h>
32 #include <unwindstack/UserArm64.h>
33 #include <unwindstack/UserX86.h>
34 #include <unwindstack/UserX86_64.h>
35 
36 namespace unwindstack {
37 
38 // The largest user structure.
39 // constexpr size_t MAX_USER_REGS_SIZE = sizeof(mips64_user_regs) + 10;
40 static constexpr size_t kMaxUserRegsSize = std::max(
41     sizeof(arm_user_regs),
42     std::max(sizeof(arm64_user_regs), std::max(sizeof(x86_user_regs), sizeof(x86_64_user_regs))));
43 
44 // This function assumes that reg_data is already aligned to a 64 bit value.
45 // If not this could crash with an unaligned access.
RemoteGet(pid_t pid)46 Regs* Regs::RemoteGet(pid_t pid) {
47   // Make the buffer large enough to contain the largest registers type.
48   std::vector<uint64_t> buffer(kMaxUserRegsSize / sizeof(uint64_t));
49   struct iovec io;
50   io.iov_base = buffer.data();
51   io.iov_len = buffer.size() * sizeof(uint64_t);
52 
53   if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
54     return nullptr;
55   }
56 
57   // Infer the process architecture from the size of its register structure.
58   switch (io.iov_len) {
59   case sizeof(x86_user_regs):
60     return RegsX86::Read(buffer.data());
61   case sizeof(x86_64_user_regs):
62     return RegsX86_64::Read(buffer.data());
63   case sizeof(arm_user_regs):
64     return RegsArm::Read(buffer.data());
65   case sizeof(arm64_user_regs):
66     return RegsArm64::Read(buffer.data());
67   }
68   return nullptr;
69 }
70 
RemoteGetArch(pid_t pid)71 ArchEnum Regs::RemoteGetArch(pid_t pid) {
72   // Make the buffer large enough to contain the largest registers type.
73   std::vector<uint64_t> buffer(kMaxUserRegsSize / sizeof(uint64_t));
74   struct iovec io;
75   io.iov_base = buffer.data();
76   io.iov_len = buffer.size() * sizeof(uint64_t);
77 
78   if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
79     return ARCH_UNKNOWN;
80   }
81 
82   // Infer the process architecture from the size of its register structure.
83   switch (io.iov_len) {
84     case sizeof(x86_user_regs):
85       return ARCH_X86;
86     case sizeof(x86_64_user_regs):
87       return ARCH_X86_64;
88     case sizeof(arm_user_regs):
89       return ARCH_ARM;
90     case sizeof(arm64_user_regs):
91       return ARCH_ARM64;
92   }
93   return ARCH_UNKNOWN;
94 }
95 
CreateFromUcontext(ArchEnum arch,void * ucontext)96 Regs* Regs::CreateFromUcontext(ArchEnum arch, void* ucontext) {
97   switch (arch) {
98     case ARCH_X86:
99       return RegsX86::CreateFromUcontext(ucontext);
100     case ARCH_X86_64:
101       return RegsX86_64::CreateFromUcontext(ucontext);
102     case ARCH_ARM:
103       return RegsArm::CreateFromUcontext(ucontext);
104     case ARCH_ARM64:
105       return RegsArm64::CreateFromUcontext(ucontext);
106     default:
107       return nullptr;
108   }
109 }
110 
CurrentArch()111 ArchEnum Regs::CurrentArch() {
112 #if defined(__arm__)
113   return ARCH_ARM;
114 #elif defined(__aarch64__)
115   return ARCH_ARM64;
116 #elif defined(__i386__)
117   return ARCH_X86;
118 #elif defined(__x86_64__)
119   return ARCH_X86_64;
120 #else
121   abort();
122 #endif
123 }
124 
CreateFromLocal()125 Regs* Regs::CreateFromLocal() {
126   Regs* regs;
127 #if defined(__arm__)
128   regs = new RegsArm();
129 #elif defined(__aarch64__)
130   regs = new RegsArm64();
131 #elif defined(__i386__)
132   regs = new RegsX86();
133 #elif defined(__x86_64__)
134   regs = new RegsX86_64();
135 #else
136   abort();
137 #endif
138   return regs;
139 }
140 
GetPcAdjustment(uint64_t rel_pc,Elf * elf,ArchEnum arch)141 uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf, ArchEnum arch) {
142   switch (arch) {
143     case ARCH_ARM: {
144       if (!elf->valid()) {
145         return 2;
146       }
147 
148       uint64_t load_bias = elf->GetLoadBias();
149       if (rel_pc < load_bias) {
150         if (rel_pc < 2) {
151           return 0;
152         }
153         return 2;
154       }
155       uint64_t adjusted_rel_pc = rel_pc - load_bias;
156       if (adjusted_rel_pc < 5) {
157         if (adjusted_rel_pc < 2) {
158           return 0;
159         }
160         return 2;
161       }
162 
163       if (adjusted_rel_pc & 1) {
164         // This is a thumb instruction, it could be 2 or 4 bytes.
165         uint32_t value;
166         if (!elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
167             (value & 0xe000f000) != 0xe000f000) {
168           return 2;
169         }
170       }
171       return 4;
172     }
173   case ARCH_ARM64: {
174     if (rel_pc < 4) {
175       return 0;
176     }
177     return 4;
178   }
179   case ARCH_MIPS:
180   case ARCH_MIPS64: {
181     if (rel_pc < 8) {
182       return 0;
183     }
184     // For now, just assume no compact branches
185     return 8;
186   }
187   case ARCH_X86:
188   case ARCH_X86_64: {
189     if (rel_pc == 0) {
190       return 0;
191     }
192     return 1;
193   }
194   case ARCH_UNKNOWN:
195     return 0;
196   }
197 }
198 
199 }  // namespace unwindstack
200