| Greg Clayton | 078daac | 2011-04-25 21:07:40 +0000 | [diff] [blame] | 1 | //===-- UnwindAssembly-x86.cpp ----------------------------------*- C++ -*-===// |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| Greg Clayton | 078daac | 2011-04-25 21:07:40 +0000 | [diff] [blame] | 10 | #include "UnwindAssembly-x86.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 11 | |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 12 | #include "llvm-c/Disassembler.h" |
| Johnny Chen | 62212f0 | 2011-11-29 01:09:49 +0000 | [diff] [blame] | 13 | #include "llvm/Support/TargetSelect.h" |
| Greg Clayton | dc5eb69 | 2011-04-25 18:36:36 +0000 | [diff] [blame] | 14 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Address.h" |
| 16 | #include "lldb/Core/Error.h" |
| 17 | #include "lldb/Core/ArchSpec.h" |
| 18 | #include "lldb/Core/PluginManager.h" |
| Greg Clayton | dc5eb69 | 2011-04-25 18:36:36 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/UnwindPlan.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 20 | #include "lldb/Target/ExecutionContext.h" |
| 21 | #include "lldb/Target/Process.h" |
| 22 | #include "lldb/Target/RegisterContext.h" |
| 23 | #include "lldb/Target/Thread.h" |
| 24 | #include "lldb/Target/Target.h" |
| Greg Clayton | 7be2542 | 2011-04-25 21:14:26 +0000 | [diff] [blame] | 25 | #include "lldb/Target/UnwindAssembly.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace lldb; |
| 28 | using namespace lldb_private; |
| 29 | |
| 30 | enum CPU { |
| 31 | k_i386, |
| 32 | k_x86_64 |
| 33 | }; |
| 34 | |
| 35 | enum i386_register_numbers { |
| 36 | k_machine_eax = 0, |
| 37 | k_machine_ecx = 1, |
| 38 | k_machine_edx = 2, |
| 39 | k_machine_ebx = 3, |
| 40 | k_machine_esp = 4, |
| 41 | k_machine_ebp = 5, |
| 42 | k_machine_esi = 6, |
| 43 | k_machine_edi = 7, |
| 44 | k_machine_eip = 8 |
| 45 | }; |
| 46 | |
| 47 | enum x86_64_register_numbers { |
| 48 | k_machine_rax = 0, |
| 49 | k_machine_rcx = 1, |
| 50 | k_machine_rdx = 2, |
| 51 | k_machine_rbx = 3, |
| 52 | k_machine_rsp = 4, |
| 53 | k_machine_rbp = 5, |
| 54 | k_machine_rsi = 6, |
| 55 | k_machine_rdi = 7, |
| 56 | k_machine_r8 = 8, |
| 57 | k_machine_r9 = 9, |
| 58 | k_machine_r10 = 10, |
| 59 | k_machine_r11 = 11, |
| 60 | k_machine_r12 = 12, |
| 61 | k_machine_r13 = 13, |
| 62 | k_machine_r14 = 14, |
| 63 | k_machine_r15 = 15, |
| 64 | k_machine_rip = 16 |
| 65 | }; |
| 66 | |
| 67 | struct regmap_ent { |
| 68 | const char *name; |
| 69 | int machine_regno; |
| 70 | int lldb_regno; |
| 71 | }; |
| 72 | |
| 73 | static struct regmap_ent i386_register_map[] = { |
| 74 | {"eax", k_machine_eax, -1}, |
| 75 | {"ecx", k_machine_ecx, -1}, |
| 76 | {"edx", k_machine_edx, -1}, |
| 77 | {"ebx", k_machine_ebx, -1}, |
| 78 | {"esp", k_machine_esp, -1}, |
| 79 | {"ebp", k_machine_ebp, -1}, |
| 80 | {"esi", k_machine_esi, -1}, |
| 81 | {"edi", k_machine_edi, -1}, |
| 82 | {"eip", k_machine_eip, -1} |
| 83 | }; |
| 84 | |
| 85 | const int size_of_i386_register_map = sizeof (i386_register_map) / sizeof (struct regmap_ent); |
| 86 | |
| 87 | static int i386_register_map_initialized = 0; |
| 88 | |
| 89 | static struct regmap_ent x86_64_register_map[] = { |
| 90 | {"rax", k_machine_rax, -1}, |
| 91 | {"rcx", k_machine_rcx, -1}, |
| 92 | {"rdx", k_machine_rdx, -1}, |
| 93 | {"rbx", k_machine_rbx, -1}, |
| 94 | {"rsp", k_machine_rsp, -1}, |
| 95 | {"rbp", k_machine_rbp, -1}, |
| 96 | {"rsi", k_machine_rsi, -1}, |
| 97 | {"rdi", k_machine_rdi, -1}, |
| 98 | {"r8", k_machine_r8, -1}, |
| 99 | {"r9", k_machine_r9, -1}, |
| 100 | {"r10", k_machine_r10, -1}, |
| 101 | {"r11", k_machine_r11, -1}, |
| 102 | {"r12", k_machine_r12, -1}, |
| 103 | {"r13", k_machine_r13, -1}, |
| 104 | {"r14", k_machine_r14, -1}, |
| 105 | {"r15", k_machine_r15, -1}, |
| 106 | {"rip", k_machine_rip, -1} |
| 107 | }; |
| 108 | |
| 109 | const int size_of_x86_64_register_map = sizeof (x86_64_register_map) / sizeof (struct regmap_ent); |
| 110 | |
| 111 | static int x86_64_register_map_initialized = 0; |
| 112 | |
| 113 | //----------------------------------------------------------------------------------------------- |
| 114 | // AssemblyParse_x86 local-file class definition & implementation functions |
| 115 | //----------------------------------------------------------------------------------------------- |
| 116 | |
| 117 | class AssemblyParse_x86 { |
| 118 | public: |
| 119 | |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 120 | AssemblyParse_x86 (const ExecutionContext &exe_ctx, int cpu, ArchSpec &arch, AddressRange func); |
| 121 | |
| 122 | ~AssemblyParse_x86 (); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 123 | |
| 124 | bool get_non_call_site_unwind_plan (UnwindPlan &unwind_plan); |
| 125 | |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 126 | bool get_fast_unwind_plan (AddressRange& func, UnwindPlan &unwind_plan); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 127 | |
| 128 | bool find_first_non_prologue_insn (Address &address); |
| 129 | |
| 130 | private: |
| 131 | enum { kMaxInstructionByteSize = 32 }; |
| 132 | |
| 133 | bool nonvolatile_reg_p (int machine_regno); |
| 134 | bool push_rbp_pattern_p (); |
| 135 | bool push_0_pattern_p (); |
| 136 | bool mov_rsp_rbp_pattern_p (); |
| 137 | bool sub_rsp_pattern_p (int& amount); |
| 138 | bool push_reg_p (int& regno); |
| 139 | bool mov_reg_to_local_stack_frame_p (int& regno, int& fp_offset); |
| 140 | bool ret_pattern_p (); |
| 141 | uint32_t extract_4 (uint8_t *b); |
| 142 | bool machine_regno_to_lldb_regno (int machine_regno, uint32_t& lldb_regno); |
| 143 | bool instruction_length (Address addr, int &length); |
| 144 | |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 145 | const ExecutionContext m_exe_ctx; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 146 | |
| 147 | AddressRange m_func_bounds; |
| 148 | |
| 149 | Address m_cur_insn; |
| 150 | uint8_t m_cur_insn_bytes[kMaxInstructionByteSize]; |
| 151 | |
| 152 | int m_machine_ip_regnum; |
| 153 | int m_machine_sp_regnum; |
| 154 | int m_machine_fp_regnum; |
| 155 | |
| 156 | int m_lldb_ip_regnum; |
| 157 | int m_lldb_sp_regnum; |
| 158 | int m_lldb_fp_regnum; |
| 159 | |
| 160 | int m_wordsize; |
| 161 | int m_cpu; |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 162 | ArchSpec m_arch; |
| 163 | ::LLVMDisasmContextRef m_disasm_context; |
| Jason Molenda | fa19c3e7 | 2010-11-04 09:40:56 +0000 | [diff] [blame] | 164 | |
| 165 | DISALLOW_COPY_AND_ASSIGN (AssemblyParse_x86); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 168 | AssemblyParse_x86::AssemblyParse_x86 (const ExecutionContext &exe_ctx, int cpu, ArchSpec &arch, AddressRange func) : |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 169 | m_exe_ctx (exe_ctx), |
| 170 | m_func_bounds(func), |
| 171 | m_cur_insn (), |
| 172 | m_machine_ip_regnum (LLDB_INVALID_REGNUM), |
| 173 | m_machine_sp_regnum (LLDB_INVALID_REGNUM), |
| 174 | m_machine_fp_regnum (LLDB_INVALID_REGNUM), |
| 175 | m_lldb_ip_regnum (LLDB_INVALID_REGNUM), |
| 176 | m_lldb_sp_regnum (LLDB_INVALID_REGNUM), |
| 177 | m_lldb_fp_regnum (LLDB_INVALID_REGNUM), |
| 178 | m_wordsize (-1), |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 179 | m_cpu(cpu), |
| 180 | m_arch(arch) |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 181 | { |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 182 | int *initialized_flag = NULL; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 183 | if (cpu == k_i386) |
| 184 | { |
| 185 | m_machine_ip_regnum = k_machine_eip; |
| 186 | m_machine_sp_regnum = k_machine_esp; |
| 187 | m_machine_fp_regnum = k_machine_ebp; |
| 188 | m_wordsize = 4; |
| 189 | initialized_flag = &i386_register_map_initialized; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | m_machine_ip_regnum = k_machine_rip; |
| 194 | m_machine_sp_regnum = k_machine_rsp; |
| 195 | m_machine_fp_regnum = k_machine_rbp; |
| 196 | m_wordsize = 8; |
| 197 | initialized_flag = &x86_64_register_map_initialized; |
| 198 | } |
| 199 | |
| 200 | // we only look at prologue - it will be complete earlier than 512 bytes into func |
| 201 | if (m_func_bounds.GetByteSize() == 0) |
| 202 | m_func_bounds.SetByteSize(512); |
| 203 | |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 204 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
| 205 | if (thread && *initialized_flag == 0) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 206 | { |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 207 | RegisterContext *reg_ctx = thread->GetRegisterContext().get(); |
| Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 208 | if (reg_ctx) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 209 | { |
| 210 | struct regmap_ent *ent; |
| 211 | int count, i; |
| 212 | if (cpu == k_i386) |
| 213 | { |
| 214 | ent = i386_register_map; |
| 215 | count = size_of_i386_register_map; |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | ent = x86_64_register_map; |
| 220 | count = size_of_x86_64_register_map; |
| 221 | } |
| 222 | for (i = 0; i < count; i++, ent++) |
| 223 | { |
| Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 224 | const RegisterInfo *ri = reg_ctx->GetRegisterInfoByName (ent->name); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 225 | if (ri) |
| 226 | ent->lldb_regno = ri->kinds[eRegisterKindLLDB]; |
| 227 | } |
| 228 | *initialized_flag = 1; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // on initial construction we may not have a Thread so these have to remain |
| 233 | // uninitialized until we can get a RegisterContext to set up the register map table |
| 234 | if (*initialized_flag == 1) |
| 235 | { |
| 236 | uint32_t lldb_regno; |
| 237 | if (machine_regno_to_lldb_regno (m_machine_sp_regnum, lldb_regno)) |
| 238 | m_lldb_sp_regnum = lldb_regno; |
| 239 | if (machine_regno_to_lldb_regno (m_machine_fp_regnum, lldb_regno)) |
| 240 | m_lldb_fp_regnum = lldb_regno; |
| 241 | if (machine_regno_to_lldb_regno (m_machine_ip_regnum, lldb_regno)) |
| 242 | m_lldb_ip_regnum = lldb_regno; |
| 243 | } |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 244 | |
| 245 | m_disasm_context = ::LLVMCreateDisasm(m_arch.GetTriple().getTriple().c_str(), |
| 246 | (void*)this, |
| 247 | /*TagType=*/1, |
| 248 | NULL, |
| 249 | NULL); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 252 | AssemblyParse_x86::~AssemblyParse_x86 () |
| 253 | { |
| 254 | ::LLVMDisasmDispose(m_disasm_context); |
| 255 | } |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 256 | |
| 257 | // This function expects an x86 native register number (i.e. the bits stripped out of the |
| 258 | // actual instruction), not an lldb register number. |
| 259 | |
| 260 | bool |
| 261 | AssemblyParse_x86::nonvolatile_reg_p (int machine_regno) |
| 262 | { |
| 263 | if (m_cpu == k_i386) |
| 264 | { |
| 265 | switch (machine_regno) { |
| 266 | case k_machine_ebx: |
| 267 | case k_machine_ebp: // not actually a nonvolatile but often treated as such by convention |
| 268 | case k_machine_esi: |
| 269 | case k_machine_edi: |
| 270 | case k_machine_esp: |
| 271 | return true; |
| 272 | default: |
| 273 | return false; |
| 274 | } |
| 275 | } |
| 276 | if (m_cpu == k_x86_64) |
| 277 | { |
| 278 | switch (machine_regno) { |
| 279 | case k_machine_rbx: |
| 280 | case k_machine_rsp: |
| 281 | case k_machine_rbp: // not actually a nonvolatile but often treated as such by convention |
| 282 | case k_machine_r12: |
| 283 | case k_machine_r13: |
| 284 | case k_machine_r14: |
| 285 | case k_machine_r15: |
| 286 | return true; |
| 287 | default: |
| 288 | return false; |
| 289 | } |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | // Macro to detect if this is a REX mode prefix byte. |
| 296 | #define REX_W_PREFIX_P(opcode) (((opcode) & (~0x5)) == 0x48) |
| 297 | |
| 298 | // The high bit which should be added to the source register number (the "R" bit) |
| 299 | #define REX_W_SRCREG(opcode) (((opcode) & 0x4) >> 2) |
| 300 | |
| 301 | // The high bit which should be added to the destination register number (the "B" bit) |
| 302 | #define REX_W_DSTREG(opcode) ((opcode) & 0x1) |
| 303 | |
| 304 | // pushq %rbp [0x55] |
| 305 | bool AssemblyParse_x86::push_rbp_pattern_p () { |
| 306 | uint8_t *p = m_cur_insn_bytes; |
| 307 | if (*p == 0x55) |
| 308 | return true; |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | // pushq $0 ; the first instruction in start() [0x6a 0x00] |
| 313 | bool AssemblyParse_x86::push_0_pattern_p () |
| 314 | { |
| 315 | uint8_t *p = m_cur_insn_bytes; |
| 316 | if (*p == 0x6a && *(p + 1) == 0x0) |
| 317 | return true; |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | // movq %rsp, %rbp [0x48 0x8b 0xec] or [0x48 0x89 0xe5] |
| 322 | // movl %esp, %ebp [0x8b 0xec] or [0x89 0xe5] |
| 323 | bool AssemblyParse_x86::mov_rsp_rbp_pattern_p () { |
| 324 | uint8_t *p = m_cur_insn_bytes; |
| 325 | if (m_wordsize == 8 && *p == 0x48) |
| 326 | p++; |
| 327 | if (*(p) == 0x8b && *(p + 1) == 0xec) |
| 328 | return true; |
| 329 | if (*(p) == 0x89 && *(p + 1) == 0xe5) |
| 330 | return true; |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | // subq $0x20, %rsp |
| 335 | bool AssemblyParse_x86::sub_rsp_pattern_p (int& amount) { |
| 336 | uint8_t *p = m_cur_insn_bytes; |
| 337 | if (m_wordsize == 8 && *p == 0x48) |
| 338 | p++; |
| 339 | // 8-bit immediate operand |
| 340 | if (*p == 0x83 && *(p + 1) == 0xec) { |
| 341 | amount = (int8_t) *(p + 2); |
| 342 | return true; |
| 343 | } |
| 344 | // 32-bit immediate operand |
| 345 | if (*p == 0x81 && *(p + 1) == 0xec) { |
| 346 | amount = (int32_t) extract_4 (p + 2); |
| 347 | return true; |
| 348 | } |
| 349 | // Not handled: [0x83 0xc4] for imm8 with neg values |
| 350 | // [0x81 0xc4] for imm32 with neg values |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | // pushq %rbx |
| 355 | // pushl $ebx |
| 356 | bool AssemblyParse_x86::push_reg_p (int& regno) { |
| 357 | uint8_t *p = m_cur_insn_bytes; |
| 358 | int regno_prefix_bit = 0; |
| 359 | // If we have a rex prefix byte, check to see if a B bit is set |
| 360 | if (m_wordsize == 8 && *p == 0x41) { |
| 361 | regno_prefix_bit = 1 << 3; |
| 362 | p++; |
| 363 | } |
| 364 | if (*p >= 0x50 && *p <= 0x57) { |
| 365 | regno = (*p - 0x50) | regno_prefix_bit; |
| 366 | return true; |
| 367 | } |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | // Look for an instruction sequence storing a nonvolatile register |
| 372 | // on to the stack frame. |
| 373 | |
| 374 | // movq %rax, -0x10(%rbp) [0x48 0x89 0x45 0xf0] |
| 375 | // movl %eax, -0xc(%ebp) [0x89 0x45 0xf4] |
| 376 | bool AssemblyParse_x86::mov_reg_to_local_stack_frame_p (int& regno, int& rbp_offset) { |
| 377 | uint8_t *p = m_cur_insn_bytes; |
| 378 | int src_reg_prefix_bit = 0; |
| 379 | int target_reg_prefix_bit = 0; |
| 380 | |
| 381 | if (m_wordsize == 8 && REX_W_PREFIX_P (*p)) { |
| 382 | src_reg_prefix_bit = REX_W_SRCREG (*p) << 3; |
| 383 | target_reg_prefix_bit = REX_W_DSTREG (*p) << 3; |
| 384 | if (target_reg_prefix_bit == 1) { |
| 385 | // rbp/ebp don't need a prefix bit - we know this isn't the |
| 386 | // reg we care about. |
| 387 | return false; |
| 388 | } |
| 389 | p++; |
| 390 | } |
| 391 | |
| 392 | if (*p == 0x89) { |
| 393 | /* Mask off the 3-5 bits which indicate the destination register |
| 394 | if this is a ModR/M byte. */ |
| 395 | int opcode_destreg_masked_out = *(p + 1) & (~0x38); |
| 396 | |
| 397 | /* Is this a ModR/M byte with Mod bits 01 and R/M bits 101 |
| 398 | and three bits between them, e.g. 01nnn101 |
| 399 | We're looking for a destination of ebp-disp8 or ebp-disp32. */ |
| 400 | int immsize; |
| 401 | if (opcode_destreg_masked_out == 0x45) |
| 402 | immsize = 2; |
| 403 | else if (opcode_destreg_masked_out == 0x85) |
| 404 | immsize = 4; |
| 405 | else |
| 406 | return false; |
| 407 | |
| 408 | int offset = 0; |
| 409 | if (immsize == 2) |
| 410 | offset = (int8_t) *(p + 2); |
| 411 | if (immsize == 4) |
| 412 | offset = (uint32_t) extract_4 (p + 2); |
| 413 | if (offset > 0) |
| 414 | return false; |
| 415 | |
| 416 | regno = ((*(p + 1) >> 3) & 0x7) | src_reg_prefix_bit; |
| 417 | rbp_offset = offset > 0 ? offset : -offset; |
| 418 | return true; |
| 419 | } |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | // ret [0xc9] or [0xc2 imm8] or [0xca imm8] |
| 424 | bool |
| 425 | AssemblyParse_x86::ret_pattern_p () |
| 426 | { |
| 427 | uint8_t *p = m_cur_insn_bytes; |
| 428 | if (*p == 0xc9 || *p == 0xc2 || *p == 0xca || *p == 0xc3) |
| 429 | return true; |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | uint32_t |
| 434 | AssemblyParse_x86::extract_4 (uint8_t *b) |
| 435 | { |
| 436 | uint32_t v = 0; |
| 437 | for (int i = 3; i >= 0; i--) |
| 438 | v = (v << 8) | b[i]; |
| 439 | return v; |
| 440 | } |
| 441 | |
| 442 | bool |
| 443 | AssemblyParse_x86::machine_regno_to_lldb_regno (int machine_regno, uint32_t &lldb_regno) |
| 444 | { |
| 445 | struct regmap_ent *ent; |
| 446 | int count, i; |
| 447 | if (m_cpu == k_i386) |
| 448 | { |
| 449 | ent = i386_register_map; |
| 450 | count = size_of_i386_register_map; |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | ent = x86_64_register_map; |
| 455 | count = size_of_x86_64_register_map; |
| 456 | } |
| 457 | for (i = 0; i < count; i++, ent++) |
| 458 | { |
| 459 | if (ent->machine_regno == machine_regno) |
| 460 | if (ent->lldb_regno != -1) |
| 461 | { |
| 462 | lldb_regno = ent->lldb_regno; |
| 463 | return true; |
| 464 | } |
| 465 | } |
| 466 | return false; |
| 467 | } |
| 468 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 469 | bool |
| 470 | AssemblyParse_x86::instruction_length (Address addr, int &length) |
| 471 | { |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 472 | const uint32_t max_op_byte_size = m_arch.GetMaximumOpcodeByteSize(); |
| Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 473 | |
| 474 | if (!addr.IsValid()) |
| 475 | return false; |
| 476 | |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 477 | uint8_t *opcode_data = (uint8_t *) malloc (max_op_byte_size); |
| 478 | if (opcode_data == NULL) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 479 | { |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 480 | return false; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 483 | const bool prefer_file_cache = true; |
| 484 | Error error; |
| 485 | Target *target = m_exe_ctx.GetTargetPtr(); |
| 486 | if (target->ReadMemory (addr, prefer_file_cache, opcode_data, max_op_byte_size, error) == -1) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 487 | { |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 488 | return false; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 489 | } |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 490 | |
| 491 | char out_string[512]; |
| 492 | const addr_t pc = addr.GetFileAddress(); |
| 493 | const size_t inst_size = ::LLVMDisasmInstruction (m_disasm_context, |
| 494 | opcode_data, |
| 495 | max_op_byte_size, |
| 496 | pc, // PC value |
| 497 | out_string, |
| 498 | sizeof(out_string)); |
| 499 | |
| 500 | length = inst_size; |
| 501 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 502 | return true; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | bool |
| 507 | AssemblyParse_x86::get_non_call_site_unwind_plan (UnwindPlan &unwind_plan) |
| 508 | { |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 509 | UnwindPlan::RowSP row(new UnwindPlan::Row); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 510 | int non_prologue_insn_count = 0; |
| Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 511 | m_cur_insn = m_func_bounds.GetBaseAddress (); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 512 | int current_func_text_offset = 0; |
| 513 | int current_sp_bytes_offset_from_cfa = 0; |
| Jason Molenda | fa19c3e7 | 2010-11-04 09:40:56 +0000 | [diff] [blame] | 514 | UnwindPlan::Row::RegisterLocation initial_regloc; |
| Jason Molenda | 5920333 | 2010-11-16 03:01:20 +0000 | [diff] [blame] | 515 | Error error; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 516 | |
| Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 517 | if (!m_cur_insn.IsValid()) |
| 518 | { |
| 519 | return false; |
| 520 | } |
| 521 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 522 | unwind_plan.SetPlanValidAddressRange (m_func_bounds); |
| 523 | unwind_plan.SetRegisterKind (eRegisterKindLLDB); |
| 524 | |
| 525 | // At the start of the function, find the CFA by adding wordsize to the SP register |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 526 | row->SetOffset (current_func_text_offset); |
| 527 | row->SetCFARegister (m_lldb_sp_regnum); |
| 528 | row->SetCFAOffset (m_wordsize); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 529 | |
| 530 | // caller's stack pointer value before the call insn is the CFA address |
| Jason Molenda | fa19c3e7 | 2010-11-04 09:40:56 +0000 | [diff] [blame] | 531 | initial_regloc.SetIsCFAPlusOffset (0); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 532 | row->SetRegisterInfo (m_lldb_sp_regnum, initial_regloc); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 533 | |
| 534 | // saved instruction pointer can be found at CFA - wordsize. |
| 535 | current_sp_bytes_offset_from_cfa = m_wordsize; |
| Jason Molenda | fa19c3e7 | 2010-11-04 09:40:56 +0000 | [diff] [blame] | 536 | initial_regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 537 | row->SetRegisterInfo (m_lldb_ip_regnum, initial_regloc); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 538 | |
| 539 | unwind_plan.AppendRow (row); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 540 | |
| 541 | // Allocate a new Row, populate it with the existing Row contents. |
| 542 | UnwindPlan::Row *newrow = new UnwindPlan::Row; |
| 543 | *newrow = *row.get(); |
| 544 | row.reset(newrow); |
| 545 | |
| Greg Clayton | db59823 | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 546 | const bool prefer_file_cache = true; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 547 | |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 548 | Target *target = m_exe_ctx.GetTargetPtr(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 549 | while (m_func_bounds.ContainsFileAddress (m_cur_insn) && non_prologue_insn_count < 10) |
| 550 | { |
| 551 | int stack_offset, insn_len; |
| 552 | int machine_regno; // register numbers masked directly out of instructions |
| 553 | uint32_t lldb_regno; // register numbers in lldb's eRegisterKindLLDB numbering scheme |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 554 | |
| 555 | if (!instruction_length (m_cur_insn, insn_len) || insn_len == 0 || insn_len > kMaxInstructionByteSize) |
| 556 | { |
| 557 | // An unrecognized/junk instruction |
| 558 | break; |
| 559 | } |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 560 | if (target->ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 561 | { |
| 562 | // Error reading the instruction out of the file, stop scanning |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | if (push_rbp_pattern_p ()) |
| 567 | { |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 568 | row->SetOffset (current_func_text_offset + insn_len); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 569 | current_sp_bytes_offset_from_cfa += m_wordsize; |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 570 | row->SetCFAOffset (current_sp_bytes_offset_from_cfa); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 571 | UnwindPlan::Row::RegisterLocation regloc; |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 572 | regloc.SetAtCFAPlusOffset (-row->GetCFAOffset()); |
| 573 | row->SetRegisterInfo (m_lldb_fp_regnum, regloc); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 574 | unwind_plan.AppendRow (row); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 575 | // Allocate a new Row, populate it with the existing Row contents. |
| 576 | newrow = new UnwindPlan::Row; |
| 577 | *newrow = *row.get(); |
| 578 | row.reset(newrow); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 579 | goto loopnext; |
| 580 | } |
| Jason Molenda | 5c01cb6 | 2010-10-26 00:47:17 +0000 | [diff] [blame] | 581 | |
| 582 | if (mov_rsp_rbp_pattern_p ()) |
| 583 | { |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 584 | row->SetOffset (current_func_text_offset + insn_len); |
| 585 | row->SetCFARegister (m_lldb_fp_regnum); |
| Jason Molenda | 5c01cb6 | 2010-10-26 00:47:17 +0000 | [diff] [blame] | 586 | unwind_plan.AppendRow (row); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 587 | // Allocate a new Row, populate it with the existing Row contents. |
| 588 | newrow = new UnwindPlan::Row; |
| 589 | *newrow = *row.get(); |
| 590 | row.reset(newrow); |
| Jason Molenda | 5c01cb6 | 2010-10-26 00:47:17 +0000 | [diff] [blame] | 591 | goto loopnext; |
| 592 | } |
| 593 | |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 594 | // This is the start() function (or a pthread equivalent), it starts with a pushl $0x0 which puts the |
| 595 | // saved pc value of 0 on the stack. In this case we want to pretend we didn't see a stack movement at all -- |
| 596 | // normally the saved pc value is already on the stack by the time the function starts executing. |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 597 | if (push_0_pattern_p ()) |
| 598 | { |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 599 | goto loopnext; |
| 600 | } |
| 601 | |
| 602 | if (push_reg_p (machine_regno)) |
| 603 | { |
| 604 | current_sp_bytes_offset_from_cfa += m_wordsize; |
| 605 | if (nonvolatile_reg_p (machine_regno) && machine_regno_to_lldb_regno (machine_regno, lldb_regno)) |
| 606 | { |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 607 | row->SetOffset (current_func_text_offset + insn_len); |
| 608 | if (row->GetCFARegister() == m_lldb_sp_regnum) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 609 | { |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 610 | row->SetCFAOffset (current_sp_bytes_offset_from_cfa); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 611 | } |
| 612 | UnwindPlan::Row::RegisterLocation regloc; |
| 613 | regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 614 | row->SetRegisterInfo (lldb_regno, regloc); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 615 | unwind_plan.AppendRow (row); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 616 | // Allocate a new Row, populate it with the existing Row contents. |
| 617 | newrow = new UnwindPlan::Row; |
| 618 | *newrow = *row.get(); |
| 619 | row.reset(newrow); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 620 | } |
| 621 | goto loopnext; |
| 622 | } |
| 623 | |
| 624 | if (mov_reg_to_local_stack_frame_p (machine_regno, stack_offset) && nonvolatile_reg_p (machine_regno)) |
| 625 | { |
| 626 | if (machine_regno_to_lldb_regno (machine_regno, lldb_regno)) |
| 627 | { |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 628 | row->SetOffset (current_func_text_offset + insn_len); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 629 | UnwindPlan::Row::RegisterLocation regloc; |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 630 | regloc.SetAtCFAPlusOffset (-row->GetCFAOffset()); |
| 631 | row->SetRegisterInfo (lldb_regno, regloc); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 632 | unwind_plan.AppendRow (row); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 633 | // Allocate a new Row, populate it with the existing Row contents. |
| 634 | newrow = new UnwindPlan::Row; |
| 635 | *newrow = *row.get(); |
| 636 | row.reset(newrow); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 637 | goto loopnext; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (sub_rsp_pattern_p (stack_offset)) |
| 642 | { |
| 643 | current_sp_bytes_offset_from_cfa += stack_offset; |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 644 | if (row->GetCFARegister() == m_lldb_sp_regnum) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 645 | { |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 646 | row->SetOffset (current_func_text_offset + insn_len); |
| 647 | row->SetCFAOffset (current_sp_bytes_offset_from_cfa); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 648 | unwind_plan.AppendRow (row); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 649 | // Allocate a new Row, populate it with the existing Row contents. |
| 650 | newrow = new UnwindPlan::Row; |
| 651 | *newrow = *row.get(); |
| 652 | row.reset(newrow); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 653 | } |
| 654 | goto loopnext; |
| 655 | } |
| 656 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 657 | if (ret_pattern_p ()) |
| 658 | { |
| 659 | // we know where the end of the function is; set the limit on the PlanValidAddressRange |
| 660 | // in case our initial "high pc" value was overly large |
| 661 | // int original_size = m_func_bounds.GetByteSize(); |
| 662 | // int calculated_size = m_cur_insn.GetOffset() - m_func_bounds.GetBaseAddress().GetOffset() + insn_len + 1; |
| 663 | // m_func_bounds.SetByteSize (calculated_size); |
| 664 | // unwind_plan.SetPlanValidAddressRange (m_func_bounds); |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | // FIXME recognize the i386 picbase setup instruction sequence, |
| 669 | // 0x1f16: call 0x1f1b ; main + 11 at /private/tmp/a.c:3 |
| 670 | // 0x1f1b: popl %eax |
| 671 | // and record the temporary stack movements if the CFA is not expressed in terms of ebp. |
| 672 | |
| 673 | non_prologue_insn_count++; |
| 674 | loopnext: |
| 675 | m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len); |
| 676 | current_func_text_offset += insn_len; |
| 677 | } |
| 678 | |
| Jason Molenda | 5920333 | 2010-11-16 03:01:20 +0000 | [diff] [blame] | 679 | // Now look at the byte at the end of the AddressRange for a limited attempt at describing the |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 680 | // epilogue. We're looking for the sequence |
| Jason Molenda | 5920333 | 2010-11-16 03:01:20 +0000 | [diff] [blame] | 681 | |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 682 | // [ 0x5d ] mov %rbp, %rsp |
| 683 | // [ 0xc3 ] ret |
| 684 | // [ 0xe8 xx xx xx xx ] call __stack_chk_fail (this is sometimes the final insn in the function) |
| 685 | |
| 686 | // We want to add a Row describing how to unwind when we're stopped on the 'ret' instruction where the |
| 687 | // CFA is no longer defined in terms of rbp, but is now defined in terms of rsp like on function entry. |
| 688 | |
| 689 | uint64_t ret_insn_offset = LLDB_INVALID_ADDRESS; |
| 690 | Address end_of_fun(m_func_bounds.GetBaseAddress()); |
| 691 | end_of_fun.SetOffset (end_of_fun.GetOffset() + m_func_bounds.GetByteSize()); |
| 692 | |
| 693 | if (m_func_bounds.GetByteSize() > 7) |
| Jason Molenda | 5920333 | 2010-11-16 03:01:20 +0000 | [diff] [blame] | 694 | { |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 695 | uint8_t bytebuf[7]; |
| 696 | Address last_seven_bytes(end_of_fun); |
| 697 | last_seven_bytes.SetOffset (last_seven_bytes.GetOffset() - 7); |
| 698 | if (target->ReadMemory (last_seven_bytes, prefer_file_cache, bytebuf, 7, error) != -1) |
| Jason Molenda | 5920333 | 2010-11-16 03:01:20 +0000 | [diff] [blame] | 699 | { |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 700 | if (bytebuf[5] == 0x5d && bytebuf[6] == 0xc3) // mov, ret |
| Jason Molenda | 5920333 | 2010-11-16 03:01:20 +0000 | [diff] [blame] | 701 | { |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 702 | ret_insn_offset = m_func_bounds.GetByteSize() - 1; |
| 703 | } |
| 704 | else if (bytebuf[0] == 0x5d && bytebuf[1] == 0xc3 && bytebuf[2] == 0xe8) // mov, ret, call |
| 705 | { |
| 706 | ret_insn_offset = m_func_bounds.GetByteSize() - 6; |
| Jason Molenda | 5920333 | 2010-11-16 03:01:20 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 709 | } else if (m_func_bounds.GetByteSize() > 2) |
| 710 | { |
| 711 | uint8_t bytebuf[2]; |
| 712 | Address last_two_bytes(end_of_fun); |
| 713 | last_two_bytes.SetOffset (last_two_bytes.GetOffset() - 2); |
| 714 | if (target->ReadMemory (last_two_bytes, prefer_file_cache, bytebuf, 2, error) != -1) |
| 715 | { |
| 716 | if (bytebuf[0] == 0x5d && bytebuf[1] == 0xc3) // mov, ret |
| 717 | { |
| 718 | ret_insn_offset = m_func_bounds.GetByteSize() - 1; |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | if (ret_insn_offset != LLDB_INVALID_ADDRESS) |
| 724 | { |
| 725 | // Create a fresh, empty Row and RegisterLocation - don't mention any other registers |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 726 | UnwindPlan::RowSP epi_row(new UnwindPlan::Row); |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 727 | UnwindPlan::Row::RegisterLocation epi_regloc; |
| 728 | |
| 729 | // When the ret instruction is about to be executed, here's our state |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 730 | epi_row->SetOffset (ret_insn_offset); |
| 731 | epi_row->SetCFARegister (m_lldb_sp_regnum); |
| 732 | epi_row->SetCFAOffset (m_wordsize); |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 733 | |
| 734 | // caller's stack pointer value before the call insn is the CFA address |
| 735 | epi_regloc.SetIsCFAPlusOffset (0); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 736 | epi_row->SetRegisterInfo (m_lldb_sp_regnum, epi_regloc); |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 737 | |
| 738 | // saved instruction pointer can be found at CFA - wordsize |
| 739 | epi_regloc.SetAtCFAPlusOffset (-m_wordsize); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 740 | epi_row->SetRegisterInfo (m_lldb_ip_regnum, epi_regloc); |
| Jason Molenda | 6f5e8c2 | 2012-05-25 01:54:06 +0000 | [diff] [blame] | 741 | |
| 742 | unwind_plan.AppendRow (epi_row); |
| Jason Molenda | 5920333 | 2010-11-16 03:01:20 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 745 | unwind_plan.SetSourceName ("assembly insn profiling"); |
| 746 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 750 | /* The "fast unwind plan" is valid for functions that follow the usual convention of |
| 751 | using the frame pointer register (ebp, rbp), i.e. the function prologue looks like |
| 752 | push %rbp [0x55] |
| 753 | mov %rsp,%rbp [0x48 0x89 0xe5] (this is a 2-byte insn seq on i386) |
| 754 | */ |
| 755 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 756 | bool |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 757 | AssemblyParse_x86::get_fast_unwind_plan (AddressRange& func, UnwindPlan &unwind_plan) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 758 | { |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 759 | UnwindPlan::RowSP row(new UnwindPlan::Row); |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 760 | UnwindPlan::Row::RegisterLocation pc_reginfo; |
| 761 | UnwindPlan::Row::RegisterLocation sp_reginfo; |
| 762 | UnwindPlan::Row::RegisterLocation fp_reginfo; |
| 763 | unwind_plan.SetRegisterKind (eRegisterKindLLDB); |
| 764 | |
| 765 | if (!func.GetBaseAddress().IsValid()) |
| 766 | return false; |
| 767 | |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 768 | Target *target = m_exe_ctx.GetTargetPtr(); |
| 769 | |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 770 | uint8_t bytebuf[4]; |
| 771 | Error error; |
| Greg Clayton | db59823 | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 772 | const bool prefer_file_cache = true; |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 773 | if (target->ReadMemory (func.GetBaseAddress(), prefer_file_cache, bytebuf, sizeof (bytebuf), error) == -1) |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 774 | return false; |
| 775 | |
| 776 | uint8_t i386_prologue[] = {0x55, 0x89, 0xe5}; |
| 777 | uint8_t x86_64_prologue[] = {0x55, 0x48, 0x89, 0xe5}; |
| 778 | int prologue_size; |
| 779 | |
| 780 | if (memcmp (bytebuf, i386_prologue, sizeof (i386_prologue)) == 0) |
| 781 | { |
| 782 | prologue_size = sizeof (i386_prologue); |
| 783 | } |
| 784 | else if (memcmp (bytebuf, x86_64_prologue, sizeof (x86_64_prologue)) == 0) |
| 785 | { |
| 786 | prologue_size = sizeof (x86_64_prologue); |
| 787 | } |
| 788 | else |
| 789 | { |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | pc_reginfo.SetAtCFAPlusOffset (-m_wordsize); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 794 | row->SetRegisterInfo (m_lldb_ip_regnum, pc_reginfo); |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 795 | |
| 796 | sp_reginfo.SetIsCFAPlusOffset (0); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 797 | row->SetRegisterInfo (m_lldb_sp_regnum, sp_reginfo); |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 798 | |
| 799 | // Zero instructions into the function |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 800 | row->SetCFARegister (m_lldb_sp_regnum); |
| 801 | row->SetCFAOffset (m_wordsize); |
| 802 | row->SetOffset (0); |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 803 | unwind_plan.AppendRow (row); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 804 | UnwindPlan::Row *newrow = new UnwindPlan::Row; |
| 805 | *newrow = *row.get(); |
| 806 | row.reset(newrow); |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 807 | |
| 808 | // push %rbp has executed - stack moved, rbp now saved |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 809 | row->SetCFAOffset (2 * m_wordsize); |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 810 | fp_reginfo.SetAtCFAPlusOffset (2 * -m_wordsize); |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 811 | row->SetRegisterInfo (m_lldb_fp_regnum, fp_reginfo); |
| 812 | row->SetOffset (1); |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 813 | unwind_plan.AppendRow (row); |
| 814 | |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 815 | newrow = new UnwindPlan::Row; |
| 816 | *newrow = *row.get(); |
| 817 | row.reset(newrow); |
| 818 | |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 819 | // mov %rsp, %rbp has executed |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 820 | row->SetCFARegister (m_lldb_fp_regnum); |
| 821 | row->SetCFAOffset (2 * m_wordsize); |
| 822 | row->SetOffset (prologue_size); /// 3 or 4 bytes depending on arch |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 823 | unwind_plan.AppendRow (row); |
| 824 | |
| Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 825 | newrow = new UnwindPlan::Row; |
| 826 | *newrow = *row.get(); |
| 827 | row.reset(newrow); |
| 828 | |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 829 | unwind_plan.SetPlanValidAddressRange (func); |
| 830 | return true; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | bool |
| 834 | AssemblyParse_x86::find_first_non_prologue_insn (Address &address) |
| 835 | { |
| 836 | m_cur_insn = m_func_bounds.GetBaseAddress (); |
| Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 837 | if (!m_cur_insn.IsValid()) |
| 838 | { |
| 839 | return false; |
| 840 | } |
| 841 | |
| Greg Clayton | db59823 | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 842 | const bool prefer_file_cache = true; |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 843 | Target *target = m_exe_ctx.GetTargetPtr(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 844 | while (m_func_bounds.ContainsFileAddress (m_cur_insn)) |
| 845 | { |
| 846 | Error error; |
| 847 | int insn_len, offset, regno; |
| 848 | if (!instruction_length (m_cur_insn, insn_len) || insn_len > kMaxInstructionByteSize || insn_len == 0) |
| 849 | { |
| 850 | // An error parsing the instruction, i.e. probably data/garbage - stop scanning |
| 851 | break; |
| 852 | } |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 853 | if (target->ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 854 | { |
| 855 | // Error reading the instruction out of the file, stop scanning |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | if (push_rbp_pattern_p () || mov_rsp_rbp_pattern_p () || sub_rsp_pattern_p (offset) |
| 860 | || push_reg_p (regno) || mov_reg_to_local_stack_frame_p (regno, offset)) |
| 861 | { |
| 862 | m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len); |
| 863 | continue; |
| 864 | } |
| 865 | |
| 866 | // Unknown non-prologue instruction - stop scanning |
| 867 | break; |
| 868 | } |
| 869 | |
| 870 | address = m_cur_insn; |
| 871 | return true; |
| 872 | } |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | //----------------------------------------------------------------------------------------------- |
| 880 | // UnwindAssemblyParser_x86 method definitions |
| 881 | //----------------------------------------------------------------------------------------------- |
| 882 | |
| Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 883 | UnwindAssembly_x86::UnwindAssembly_x86 (const ArchSpec &arch, int cpu) : |
| 884 | lldb_private::UnwindAssembly(arch), |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 885 | m_cpu(cpu), |
| 886 | m_arch(arch) |
| Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 887 | { |
| 888 | } |
| 889 | |
| 890 | |
| 891 | UnwindAssembly_x86::~UnwindAssembly_x86 () |
| 892 | { |
| 893 | } |
| 894 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 895 | bool |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 896 | UnwindAssembly_x86::GetNonCallSiteUnwindPlanFromAssembly (AddressRange& func, Thread& thread, UnwindPlan& unwind_plan) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 897 | { |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 898 | ExecutionContext exe_ctx (thread.shared_from_this()); |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 899 | AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, m_arch, func); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 900 | return asm_parse.get_non_call_site_unwind_plan (unwind_plan); |
| 901 | } |
| 902 | |
| 903 | bool |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 904 | UnwindAssembly_x86::GetFastUnwindPlan (AddressRange& func, Thread& thread, UnwindPlan &unwind_plan) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 905 | { |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 906 | ExecutionContext exe_ctx (thread.shared_from_this()); |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 907 | AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, m_arch, func); |
| Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 908 | return asm_parse.get_fast_unwind_plan (func, unwind_plan); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | bool |
| Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 912 | UnwindAssembly_x86::FirstNonPrologueInsn (AddressRange& func, const ExecutionContext &exe_ctx, Address& first_non_prologue_insn) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 913 | { |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame^] | 914 | AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, m_arch, func); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 915 | return asm_parse.find_first_non_prologue_insn (first_non_prologue_insn); |
| 916 | } |
| 917 | |
| Greg Clayton | 7be2542 | 2011-04-25 21:14:26 +0000 | [diff] [blame] | 918 | UnwindAssembly * |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 919 | UnwindAssembly_x86::CreateInstance (const ArchSpec &arch) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 920 | { |
| Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 921 | const llvm::Triple::ArchType cpu = arch.GetMachine (); |
| 922 | if (cpu == llvm::Triple::x86) |
| Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 923 | return new UnwindAssembly_x86 (arch, k_i386); |
| Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 924 | else if (cpu == llvm::Triple::x86_64) |
| Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 925 | return new UnwindAssembly_x86 (arch, k_x86_64); |
| Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 926 | return NULL; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | |
| 930 | //------------------------------------------------------------------ |
| 931 | // PluginInterface protocol in UnwindAssemblyParser_x86 |
| 932 | //------------------------------------------------------------------ |
| 933 | |
| 934 | const char * |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 935 | UnwindAssembly_x86::GetPluginName() |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 936 | { |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 937 | return "UnwindAssembly_x86"; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | const char * |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 941 | UnwindAssembly_x86::GetShortPluginName() |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 942 | { |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 943 | return "unwindassembly.x86"; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | |
| 947 | uint32_t |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 948 | UnwindAssembly_x86::GetPluginVersion() |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 949 | { |
| 950 | return 1; |
| 951 | } |
| 952 | |
| 953 | void |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 954 | UnwindAssembly_x86::Initialize() |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 955 | { |
| 956 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 957 | GetPluginDescriptionStatic(), |
| 958 | CreateInstance); |
| 959 | } |
| 960 | |
| 961 | void |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 962 | UnwindAssembly_x86::Terminate() |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 963 | { |
| 964 | PluginManager::UnregisterPlugin (CreateInstance); |
| 965 | } |
| 966 | |
| 967 | |
| 968 | const char * |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 969 | UnwindAssembly_x86::GetPluginNameStatic() |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 970 | { |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 971 | return "UnwindAssembly_x86"; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | const char * |
| Greg Clayton | ffc922e3 | 2011-04-25 21:05:07 +0000 | [diff] [blame] | 975 | UnwindAssembly_x86::GetPluginDescriptionStatic() |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 976 | { |
| 977 | return "i386 and x86_64 assembly language profiler plugin."; |
| 978 | } |