1 //===-- UnwindAssembly-x86.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "UnwindAssembly-x86.h"
12 #include "llvm-c/Disassembler.h"
13 #include "llvm/Support/TargetSelect.h"
15 #include "lldb/Core/Address.h"
16 #include "lldb/Core/Error.h"
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Core/PluginManager.h"
19 #include "lldb/Symbol/UnwindPlan.h"
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"
25 #include "lldb/Target/UnwindAssembly.h"
28 using namespace lldb_private;
35 enum i386_register_numbers {
47 enum x86_64_register_numbers {
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}
85 const int size_of_i386_register_map = sizeof (i386_register_map) / sizeof (struct regmap_ent);
87 static int i386_register_map_initialized = 0;
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}
109 const int size_of_x86_64_register_map = sizeof (x86_64_register_map) / sizeof (struct regmap_ent);
111 static int x86_64_register_map_initialized = 0;
113 //-----------------------------------------------------------------------------------------------
114 // AssemblyParse_x86 local-file class definition & implementation functions
115 //-----------------------------------------------------------------------------------------------
117 class AssemblyParse_x86 {
120 AssemblyParse_x86 (const ExecutionContext &exe_ctx, int cpu, ArchSpec &arch, AddressRange func);
122 ~AssemblyParse_x86 ();
124 bool get_non_call_site_unwind_plan (UnwindPlan &unwind_plan);
126 bool get_fast_unwind_plan (AddressRange& func, UnwindPlan &unwind_plan);
128 bool find_first_non_prologue_insn (Address &address);
131 enum { kMaxInstructionByteSize = 32 };
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);
145 const ExecutionContext m_exe_ctx;
147 AddressRange m_func_bounds;
150 uint8_t m_cur_insn_bytes[kMaxInstructionByteSize];
152 int m_machine_ip_regnum;
153 int m_machine_sp_regnum;
154 int m_machine_fp_regnum;
156 int m_lldb_ip_regnum;
157 int m_lldb_sp_regnum;
158 int m_lldb_fp_regnum;
163 ::LLVMDisasmContextRef m_disasm_context;
165 DISALLOW_COPY_AND_ASSIGN (AssemblyParse_x86);
168 AssemblyParse_x86::AssemblyParse_x86 (const ExecutionContext &exe_ctx, int cpu, ArchSpec &arch, AddressRange func) :
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),
182 int *initialized_flag = NULL;
185 m_machine_ip_regnum = k_machine_eip;
186 m_machine_sp_regnum = k_machine_esp;
187 m_machine_fp_regnum = k_machine_ebp;
189 initialized_flag = &i386_register_map_initialized;
193 m_machine_ip_regnum = k_machine_rip;
194 m_machine_sp_regnum = k_machine_rsp;
195 m_machine_fp_regnum = k_machine_rbp;
197 initialized_flag = &x86_64_register_map_initialized;
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);
204 Thread *thread = m_exe_ctx.GetThreadPtr();
205 if (thread && *initialized_flag == 0)
207 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
210 struct regmap_ent *ent;
214 ent = i386_register_map;
215 count = size_of_i386_register_map;
219 ent = x86_64_register_map;
220 count = size_of_x86_64_register_map;
222 for (i = 0; i < count; i++, ent++)
224 const RegisterInfo *ri = reg_ctx->GetRegisterInfoByName (ent->name);
226 ent->lldb_regno = ri->kinds[eRegisterKindLLDB];
228 *initialized_flag = 1;
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)
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;
245 m_disasm_context = ::LLVMCreateDisasm(m_arch.GetTriple().getTriple().c_str(),
252 AssemblyParse_x86::~AssemblyParse_x86 ()
254 ::LLVMDisasmDispose(m_disasm_context);
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.
261 AssemblyParse_x86::nonvolatile_reg_p (int machine_regno)
265 switch (machine_regno) {
267 case k_machine_ebp: // not actually a nonvolatile but often treated as such by convention
276 if (m_cpu == k_x86_64)
278 switch (machine_regno) {
281 case k_machine_rbp: // not actually a nonvolatile but often treated as such by convention
295 // Macro to detect if this is a REX mode prefix byte.
296 #define REX_W_PREFIX_P(opcode) (((opcode) & (~0x5)) == 0x48)
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)
301 // The high bit which should be added to the destination register number (the "B" bit)
302 #define REX_W_DSTREG(opcode) ((opcode) & 0x1)
305 bool AssemblyParse_x86::push_rbp_pattern_p () {
306 uint8_t *p = m_cur_insn_bytes;
312 // pushq $0 ; the first instruction in start() [0x6a 0x00]
313 bool AssemblyParse_x86::push_0_pattern_p ()
315 uint8_t *p = m_cur_insn_bytes;
316 if (*p == 0x6a && *(p + 1) == 0x0)
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)
327 if (*(p) == 0x8b && *(p + 1) == 0xec)
329 if (*(p) == 0x89 && *(p + 1) == 0xe5)
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)
339 // 8-bit immediate operand
340 if (*p == 0x83 && *(p + 1) == 0xec) {
341 amount = (int8_t) *(p + 2);
344 // 32-bit immediate operand
345 if (*p == 0x81 && *(p + 1) == 0xec) {
346 amount = (int32_t) extract_4 (p + 2);
349 // Not handled: [0x83 0xc4] for imm8 with neg values
350 // [0x81 0xc4] for imm32 with neg values
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;
364 if (*p >= 0x50 && *p <= 0x57) {
365 regno = (*p - 0x50) | regno_prefix_bit;
371 // Look for an instruction sequence storing a nonvolatile register
372 // on to the stack frame.
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;
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.
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);
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. */
401 if (opcode_destreg_masked_out == 0x45)
403 else if (opcode_destreg_masked_out == 0x85)
410 offset = (int8_t) *(p + 2);
412 offset = (uint32_t) extract_4 (p + 2);
416 regno = ((*(p + 1) >> 3) & 0x7) | src_reg_prefix_bit;
417 rbp_offset = offset > 0 ? offset : -offset;
423 // ret [0xc9] or [0xc2 imm8] or [0xca imm8]
425 AssemblyParse_x86::ret_pattern_p ()
427 uint8_t *p = m_cur_insn_bytes;
428 if (*p == 0xc9 || *p == 0xc2 || *p == 0xca || *p == 0xc3)
434 AssemblyParse_x86::extract_4 (uint8_t *b)
437 for (int i = 3; i >= 0; i--)
443 AssemblyParse_x86::machine_regno_to_lldb_regno (int machine_regno, uint32_t &lldb_regno)
445 struct regmap_ent *ent;
449 ent = i386_register_map;
450 count = size_of_i386_register_map;
454 ent = x86_64_register_map;
455 count = size_of_x86_64_register_map;
457 for (i = 0; i < count; i++, ent++)
459 if (ent->machine_regno == machine_regno)
460 if (ent->lldb_regno != -1)
462 lldb_regno = ent->lldb_regno;
470 AssemblyParse_x86::instruction_length (Address addr, int &length)
472 const uint32_t max_op_byte_size = m_arch.GetMaximumOpcodeByteSize();
473 llvm::SmallVector <uint8_t, 32> opcode_data;
474 opcode_data.resize (max_op_byte_size);
479 const bool prefer_file_cache = true;
481 Target *target = m_exe_ctx.GetTargetPtr();
482 if (target->ReadMemory (addr, prefer_file_cache, opcode_data.data(), max_op_byte_size, error) == -1)
487 char out_string[512];
488 const addr_t pc = addr.GetFileAddress();
489 const size_t inst_size = ::LLVMDisasmInstruction (m_disasm_context,
502 AssemblyParse_x86::get_non_call_site_unwind_plan (UnwindPlan &unwind_plan)
504 UnwindPlan::RowSP row(new UnwindPlan::Row);
505 int non_prologue_insn_count = 0;
506 m_cur_insn = m_func_bounds.GetBaseAddress ();
507 int current_func_text_offset = 0;
508 int current_sp_bytes_offset_from_cfa = 0;
509 UnwindPlan::Row::RegisterLocation initial_regloc;
512 if (!m_cur_insn.IsValid())
517 unwind_plan.SetPlanValidAddressRange (m_func_bounds);
518 unwind_plan.SetRegisterKind (eRegisterKindLLDB);
520 // At the start of the function, find the CFA by adding wordsize to the SP register
521 row->SetOffset (current_func_text_offset);
522 row->SetCFARegister (m_lldb_sp_regnum);
523 row->SetCFAOffset (m_wordsize);
525 // caller's stack pointer value before the call insn is the CFA address
526 initial_regloc.SetIsCFAPlusOffset (0);
527 row->SetRegisterInfo (m_lldb_sp_regnum, initial_regloc);
529 // saved instruction pointer can be found at CFA - wordsize.
530 current_sp_bytes_offset_from_cfa = m_wordsize;
531 initial_regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa);
532 row->SetRegisterInfo (m_lldb_ip_regnum, initial_regloc);
534 unwind_plan.AppendRow (row);
536 // Allocate a new Row, populate it with the existing Row contents.
537 UnwindPlan::Row *newrow = new UnwindPlan::Row;
538 *newrow = *row.get();
541 const bool prefer_file_cache = true;
543 Target *target = m_exe_ctx.GetTargetPtr();
544 while (m_func_bounds.ContainsFileAddress (m_cur_insn) && non_prologue_insn_count < 10)
546 int stack_offset, insn_len;
547 int machine_regno; // register numbers masked directly out of instructions
548 uint32_t lldb_regno; // register numbers in lldb's eRegisterKindLLDB numbering scheme
550 if (!instruction_length (m_cur_insn, insn_len) || insn_len == 0 || insn_len > kMaxInstructionByteSize)
552 // An unrecognized/junk instruction
555 if (target->ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1)
557 // Error reading the instruction out of the file, stop scanning
561 if (push_rbp_pattern_p ())
563 row->SetOffset (current_func_text_offset + insn_len);
564 current_sp_bytes_offset_from_cfa += m_wordsize;
565 row->SetCFAOffset (current_sp_bytes_offset_from_cfa);
566 UnwindPlan::Row::RegisterLocation regloc;
567 regloc.SetAtCFAPlusOffset (-row->GetCFAOffset());
568 row->SetRegisterInfo (m_lldb_fp_regnum, regloc);
569 unwind_plan.AppendRow (row);
570 // Allocate a new Row, populate it with the existing Row contents.
571 newrow = new UnwindPlan::Row;
572 *newrow = *row.get();
577 if (mov_rsp_rbp_pattern_p ())
579 row->SetOffset (current_func_text_offset + insn_len);
580 row->SetCFARegister (m_lldb_fp_regnum);
581 unwind_plan.AppendRow (row);
582 // Allocate a new Row, populate it with the existing Row contents.
583 newrow = new UnwindPlan::Row;
584 *newrow = *row.get();
589 // This is the start() function (or a pthread equivalent), it starts with a pushl $0x0 which puts the
590 // saved pc value of 0 on the stack. In this case we want to pretend we didn't see a stack movement at all --
591 // normally the saved pc value is already on the stack by the time the function starts executing.
592 if (push_0_pattern_p ())
597 if (push_reg_p (machine_regno))
599 current_sp_bytes_offset_from_cfa += m_wordsize;
600 if (nonvolatile_reg_p (machine_regno) && machine_regno_to_lldb_regno (machine_regno, lldb_regno))
602 row->SetOffset (current_func_text_offset + insn_len);
603 if (row->GetCFARegister() == m_lldb_sp_regnum)
605 row->SetCFAOffset (current_sp_bytes_offset_from_cfa);
607 UnwindPlan::Row::RegisterLocation regloc;
608 regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa);
609 row->SetRegisterInfo (lldb_regno, regloc);
610 unwind_plan.AppendRow (row);
611 // Allocate a new Row, populate it with the existing Row contents.
612 newrow = new UnwindPlan::Row;
613 *newrow = *row.get();
619 if (mov_reg_to_local_stack_frame_p (machine_regno, stack_offset) && nonvolatile_reg_p (machine_regno))
621 if (machine_regno_to_lldb_regno (machine_regno, lldb_regno))
623 row->SetOffset (current_func_text_offset + insn_len);
624 UnwindPlan::Row::RegisterLocation regloc;
625 regloc.SetAtCFAPlusOffset (-row->GetCFAOffset());
626 row->SetRegisterInfo (lldb_regno, regloc);
627 unwind_plan.AppendRow (row);
628 // Allocate a new Row, populate it with the existing Row contents.
629 newrow = new UnwindPlan::Row;
630 *newrow = *row.get();
636 if (sub_rsp_pattern_p (stack_offset))
638 current_sp_bytes_offset_from_cfa += stack_offset;
639 if (row->GetCFARegister() == m_lldb_sp_regnum)
641 row->SetOffset (current_func_text_offset + insn_len);
642 row->SetCFAOffset (current_sp_bytes_offset_from_cfa);
643 unwind_plan.AppendRow (row);
644 // Allocate a new Row, populate it with the existing Row contents.
645 newrow = new UnwindPlan::Row;
646 *newrow = *row.get();
652 if (ret_pattern_p ())
654 // we know where the end of the function is; set the limit on the PlanValidAddressRange
655 // in case our initial "high pc" value was overly large
656 // int original_size = m_func_bounds.GetByteSize();
657 // int calculated_size = m_cur_insn.GetOffset() - m_func_bounds.GetBaseAddress().GetOffset() + insn_len + 1;
658 // m_func_bounds.SetByteSize (calculated_size);
659 // unwind_plan.SetPlanValidAddressRange (m_func_bounds);
663 // FIXME recognize the i386 picbase setup instruction sequence,
664 // 0x1f16: call 0x1f1b ; main + 11 at /private/tmp/a.c:3
666 // and record the temporary stack movements if the CFA is not expressed in terms of ebp.
668 non_prologue_insn_count++;
670 m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len);
671 current_func_text_offset += insn_len;
674 // Now look at the byte at the end of the AddressRange for a limited attempt at describing the
675 // epilogue. We're looking for the sequence
677 // [ 0x5d ] mov %rbp, %rsp
679 // [ 0xe8 xx xx xx xx ] call __stack_chk_fail (this is sometimes the final insn in the function)
681 // We want to add a Row describing how to unwind when we're stopped on the 'ret' instruction where the
682 // CFA is no longer defined in terms of rbp, but is now defined in terms of rsp like on function entry.
684 uint64_t ret_insn_offset = LLDB_INVALID_ADDRESS;
685 Address end_of_fun(m_func_bounds.GetBaseAddress());
686 end_of_fun.SetOffset (end_of_fun.GetOffset() + m_func_bounds.GetByteSize());
688 if (m_func_bounds.GetByteSize() > 7)
691 Address last_seven_bytes(end_of_fun);
692 last_seven_bytes.SetOffset (last_seven_bytes.GetOffset() - 7);
693 if (target->ReadMemory (last_seven_bytes, prefer_file_cache, bytebuf, 7, error) != -1)
695 if (bytebuf[5] == 0x5d && bytebuf[6] == 0xc3) // mov, ret
697 ret_insn_offset = m_func_bounds.GetByteSize() - 1;
699 else if (bytebuf[0] == 0x5d && bytebuf[1] == 0xc3 && bytebuf[2] == 0xe8) // mov, ret, call
701 ret_insn_offset = m_func_bounds.GetByteSize() - 6;
704 } else if (m_func_bounds.GetByteSize() > 2)
707 Address last_two_bytes(end_of_fun);
708 last_two_bytes.SetOffset (last_two_bytes.GetOffset() - 2);
709 if (target->ReadMemory (last_two_bytes, prefer_file_cache, bytebuf, 2, error) != -1)
711 if (bytebuf[0] == 0x5d && bytebuf[1] == 0xc3) // mov, ret
713 ret_insn_offset = m_func_bounds.GetByteSize() - 1;
718 if (ret_insn_offset != LLDB_INVALID_ADDRESS)
720 // Create a fresh, empty Row and RegisterLocation - don't mention any other registers
721 UnwindPlan::RowSP epi_row(new UnwindPlan::Row);
722 UnwindPlan::Row::RegisterLocation epi_regloc;
724 // When the ret instruction is about to be executed, here's our state
725 epi_row->SetOffset (ret_insn_offset);
726 epi_row->SetCFARegister (m_lldb_sp_regnum);
727 epi_row->SetCFAOffset (m_wordsize);
729 // caller's stack pointer value before the call insn is the CFA address
730 epi_regloc.SetIsCFAPlusOffset (0);
731 epi_row->SetRegisterInfo (m_lldb_sp_regnum, epi_regloc);
733 // saved instruction pointer can be found at CFA - wordsize
734 epi_regloc.SetAtCFAPlusOffset (-m_wordsize);
735 epi_row->SetRegisterInfo (m_lldb_ip_regnum, epi_regloc);
737 unwind_plan.AppendRow (epi_row);
740 unwind_plan.SetSourceName ("assembly insn profiling");
741 unwind_plan.SetSourcedFromCompiler (eLazyBoolNo);
742 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolYes);
747 /* The "fast unwind plan" is valid for functions that follow the usual convention of
748 using the frame pointer register (ebp, rbp), i.e. the function prologue looks like
750 mov %rsp,%rbp [0x48 0x89 0xe5] (this is a 2-byte insn seq on i386)
754 AssemblyParse_x86::get_fast_unwind_plan (AddressRange& func, UnwindPlan &unwind_plan)
756 UnwindPlan::RowSP row(new UnwindPlan::Row);
757 UnwindPlan::Row::RegisterLocation pc_reginfo;
758 UnwindPlan::Row::RegisterLocation sp_reginfo;
759 UnwindPlan::Row::RegisterLocation fp_reginfo;
760 unwind_plan.SetRegisterKind (eRegisterKindLLDB);
762 if (!func.GetBaseAddress().IsValid())
765 Target *target = m_exe_ctx.GetTargetPtr();
769 const bool prefer_file_cache = true;
770 if (target->ReadMemory (func.GetBaseAddress(), prefer_file_cache, bytebuf, sizeof (bytebuf), error) == -1)
773 uint8_t i386_prologue[] = {0x55, 0x89, 0xe5};
774 uint8_t x86_64_prologue[] = {0x55, 0x48, 0x89, 0xe5};
777 if (memcmp (bytebuf, i386_prologue, sizeof (i386_prologue)) == 0)
779 prologue_size = sizeof (i386_prologue);
781 else if (memcmp (bytebuf, x86_64_prologue, sizeof (x86_64_prologue)) == 0)
783 prologue_size = sizeof (x86_64_prologue);
790 pc_reginfo.SetAtCFAPlusOffset (-m_wordsize);
791 row->SetRegisterInfo (m_lldb_ip_regnum, pc_reginfo);
793 sp_reginfo.SetIsCFAPlusOffset (0);
794 row->SetRegisterInfo (m_lldb_sp_regnum, sp_reginfo);
796 // Zero instructions into the function
797 row->SetCFARegister (m_lldb_sp_regnum);
798 row->SetCFAOffset (m_wordsize);
800 unwind_plan.AppendRow (row);
801 UnwindPlan::Row *newrow = new UnwindPlan::Row;
802 *newrow = *row.get();
805 // push %rbp has executed - stack moved, rbp now saved
806 row->SetCFAOffset (2 * m_wordsize);
807 fp_reginfo.SetAtCFAPlusOffset (2 * -m_wordsize);
808 row->SetRegisterInfo (m_lldb_fp_regnum, fp_reginfo);
810 unwind_plan.AppendRow (row);
812 newrow = new UnwindPlan::Row;
813 *newrow = *row.get();
816 // mov %rsp, %rbp has executed
817 row->SetCFARegister (m_lldb_fp_regnum);
818 row->SetCFAOffset (2 * m_wordsize);
819 row->SetOffset (prologue_size); /// 3 or 4 bytes depending on arch
820 unwind_plan.AppendRow (row);
822 newrow = new UnwindPlan::Row;
823 *newrow = *row.get();
826 unwind_plan.SetPlanValidAddressRange (func);
827 unwind_plan.SetSourceName ("fast unwind assembly profiling");
828 unwind_plan.SetSourcedFromCompiler (eLazyBoolNo);
829 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
834 AssemblyParse_x86::find_first_non_prologue_insn (Address &address)
836 m_cur_insn = m_func_bounds.GetBaseAddress ();
837 if (!m_cur_insn.IsValid())
842 const bool prefer_file_cache = true;
843 Target *target = m_exe_ctx.GetTargetPtr();
844 while (m_func_bounds.ContainsFileAddress (m_cur_insn))
847 int insn_len, offset, regno;
848 if (!instruction_length (m_cur_insn, insn_len) || insn_len > kMaxInstructionByteSize || insn_len == 0)
850 // An error parsing the instruction, i.e. probably data/garbage - stop scanning
853 if (target->ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1)
855 // Error reading the instruction out of the file, stop scanning
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))
862 m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len);
866 // Unknown non-prologue instruction - stop scanning
870 address = m_cur_insn;
879 //-----------------------------------------------------------------------------------------------
880 // UnwindAssemblyParser_x86 method definitions
881 //-----------------------------------------------------------------------------------------------
883 UnwindAssembly_x86::UnwindAssembly_x86 (const ArchSpec &arch, int cpu) :
884 lldb_private::UnwindAssembly(arch),
891 UnwindAssembly_x86::~UnwindAssembly_x86 ()
896 UnwindAssembly_x86::GetNonCallSiteUnwindPlanFromAssembly (AddressRange& func, Thread& thread, UnwindPlan& unwind_plan)
898 ExecutionContext exe_ctx (thread.shared_from_this());
899 AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, m_arch, func);
900 return asm_parse.get_non_call_site_unwind_plan (unwind_plan);
904 UnwindAssembly_x86::GetFastUnwindPlan (AddressRange& func, Thread& thread, UnwindPlan &unwind_plan)
906 ExecutionContext exe_ctx (thread.shared_from_this());
907 AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, m_arch, func);
908 return asm_parse.get_fast_unwind_plan (func, unwind_plan);
912 UnwindAssembly_x86::FirstNonPrologueInsn (AddressRange& func, const ExecutionContext &exe_ctx, Address& first_non_prologue_insn)
914 AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, m_arch, func);
915 return asm_parse.find_first_non_prologue_insn (first_non_prologue_insn);
919 UnwindAssembly_x86::CreateInstance (const ArchSpec &arch)
921 const llvm::Triple::ArchType cpu = arch.GetMachine ();
922 if (cpu == llvm::Triple::x86)
923 return new UnwindAssembly_x86 (arch, k_i386);
924 else if (cpu == llvm::Triple::x86_64)
925 return new UnwindAssembly_x86 (arch, k_x86_64);
930 //------------------------------------------------------------------
931 // PluginInterface protocol in UnwindAssemblyParser_x86
932 //------------------------------------------------------------------
935 UnwindAssembly_x86::GetPluginName()
937 return GetPluginNameStatic();
942 UnwindAssembly_x86::GetPluginVersion()
948 UnwindAssembly_x86::Initialize()
950 PluginManager::RegisterPlugin (GetPluginNameStatic(),
951 GetPluginDescriptionStatic(),
956 UnwindAssembly_x86::Terminate()
958 PluginManager::UnregisterPlugin (CreateInstance);
962 lldb_private::ConstString
963 UnwindAssembly_x86::GetPluginNameStatic()
965 static ConstString g_name("x86");
970 UnwindAssembly_x86::GetPluginDescriptionStatic()
972 return "i386 and x86_64 assembly language profiler plugin.";