blob: 8521231e6e68f96188332378996fb59257a420f6 [file] [log] [blame]
Jason Molenda3a4ea242010-09-10 07:49:16 +00001//===-- UnwindAssemblyProfiler-x86.cpp --------------------------*- C++ -*-===//
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
10#include "UnwindAssemblyProfiler-x86.h"
11
12#include "lldb/lldb-private.h"
13#include "lldb/Utility/UnwindAssemblyProfiler.h"
14#include "lldb/Core/Address.h"
15#include "lldb/Core/Error.h"
16#include "lldb/Core/ArchSpec.h"
17#include "lldb/Core/PluginManager.h"
18#include "lldb/Target/ExecutionContext.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Target/RegisterContext.h"
21#include "lldb/Target/Thread.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Symbol/UnwindPlan.h"
24
25#include "lldb/lldb-enumerations.h"
26#include "llvm-c/EnhancedDisassembly.h"
27
28#include "UnwindAssemblyProfiler-x86.h"
29
30
31using namespace lldb;
32using namespace lldb_private;
33
34enum CPU {
35 k_i386,
36 k_x86_64
37};
38
39enum i386_register_numbers {
40 k_machine_eax = 0,
41 k_machine_ecx = 1,
42 k_machine_edx = 2,
43 k_machine_ebx = 3,
44 k_machine_esp = 4,
45 k_machine_ebp = 5,
46 k_machine_esi = 6,
47 k_machine_edi = 7,
48 k_machine_eip = 8
49};
50
51enum x86_64_register_numbers {
52 k_machine_rax = 0,
53 k_machine_rcx = 1,
54 k_machine_rdx = 2,
55 k_machine_rbx = 3,
56 k_machine_rsp = 4,
57 k_machine_rbp = 5,
58 k_machine_rsi = 6,
59 k_machine_rdi = 7,
60 k_machine_r8 = 8,
61 k_machine_r9 = 9,
62 k_machine_r10 = 10,
63 k_machine_r11 = 11,
64 k_machine_r12 = 12,
65 k_machine_r13 = 13,
66 k_machine_r14 = 14,
67 k_machine_r15 = 15,
68 k_machine_rip = 16
69};
70
71struct regmap_ent {
72 const char *name;
73 int machine_regno;
74 int lldb_regno;
75};
76
77static struct regmap_ent i386_register_map[] = {
78 {"eax", k_machine_eax, -1},
79 {"ecx", k_machine_ecx, -1},
80 {"edx", k_machine_edx, -1},
81 {"ebx", k_machine_ebx, -1},
82 {"esp", k_machine_esp, -1},
83 {"ebp", k_machine_ebp, -1},
84 {"esi", k_machine_esi, -1},
85 {"edi", k_machine_edi, -1},
86 {"eip", k_machine_eip, -1}
87};
88
89const int size_of_i386_register_map = sizeof (i386_register_map) / sizeof (struct regmap_ent);
90
91static int i386_register_map_initialized = 0;
92
93static struct regmap_ent x86_64_register_map[] = {
94 {"rax", k_machine_rax, -1},
95 {"rcx", k_machine_rcx, -1},
96 {"rdx", k_machine_rdx, -1},
97 {"rbx", k_machine_rbx, -1},
98 {"rsp", k_machine_rsp, -1},
99 {"rbp", k_machine_rbp, -1},
100 {"rsi", k_machine_rsi, -1},
101 {"rdi", k_machine_rdi, -1},
102 {"r8", k_machine_r8, -1},
103 {"r9", k_machine_r9, -1},
104 {"r10", k_machine_r10, -1},
105 {"r11", k_machine_r11, -1},
106 {"r12", k_machine_r12, -1},
107 {"r13", k_machine_r13, -1},
108 {"r14", k_machine_r14, -1},
109 {"r15", k_machine_r15, -1},
110 {"rip", k_machine_rip, -1}
111};
112
113const int size_of_x86_64_register_map = sizeof (x86_64_register_map) / sizeof (struct regmap_ent);
114
115static int x86_64_register_map_initialized = 0;
116
117//-----------------------------------------------------------------------------------------------
118// AssemblyParse_x86 local-file class definition & implementation functions
119//-----------------------------------------------------------------------------------------------
120
121class AssemblyParse_x86 {
122public:
123
124 AssemblyParse_x86 (Target &target, Thread *thread, int cpu, AddressRange func);
125
126 bool get_non_call_site_unwind_plan (UnwindPlan &unwind_plan);
127
Jason Molenda8280cbe2010-10-25 11:12:07 +0000128 bool get_fast_unwind_plan (AddressRange& func, UnwindPlan &unwind_plan);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000129
130 bool find_first_non_prologue_insn (Address &address);
131
132private:
133 enum { kMaxInstructionByteSize = 32 };
134
135 bool nonvolatile_reg_p (int machine_regno);
136 bool push_rbp_pattern_p ();
137 bool push_0_pattern_p ();
138 bool mov_rsp_rbp_pattern_p ();
139 bool sub_rsp_pattern_p (int& amount);
140 bool push_reg_p (int& regno);
141 bool mov_reg_to_local_stack_frame_p (int& regno, int& fp_offset);
142 bool ret_pattern_p ();
143 uint32_t extract_4 (uint8_t *b);
144 bool machine_regno_to_lldb_regno (int machine_regno, uint32_t& lldb_regno);
145 bool instruction_length (Address addr, int &length);
146
147 Target &m_target;
148 Thread* m_thread;
149
150 AddressRange m_func_bounds;
151
152 Address m_cur_insn;
153 uint8_t m_cur_insn_bytes[kMaxInstructionByteSize];
154
155 int m_machine_ip_regnum;
156 int m_machine_sp_regnum;
157 int m_machine_fp_regnum;
158
159 int m_lldb_ip_regnum;
160 int m_lldb_sp_regnum;
161 int m_lldb_fp_regnum;
162
163 int m_wordsize;
164 int m_cpu;
Jason Molendaa6b71de2010-11-04 09:40:56 +0000165
166 DISALLOW_COPY_AND_ASSIGN (AssemblyParse_x86);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000167};
168
169AssemblyParse_x86::AssemblyParse_x86 (Target& target, Thread* thread, int cpu, AddressRange func) :
Jason Molendaa6b71de2010-11-04 09:40:56 +0000170 m_target (target), m_thread (thread), m_cpu(cpu), m_func_bounds(func),
171 m_machine_ip_regnum (-1), m_machine_sp_regnum (-1), m_machine_fp_regnum (-1),
172 m_lldb_ip_regnum (-1), m_lldb_sp_regnum (-1), m_lldb_fp_regnum (-1),
173 m_wordsize (-1), m_cur_insn ()
Jason Molenda8280cbe2010-10-25 11:12:07 +0000174{
Jason Molenda3a4ea242010-09-10 07:49:16 +0000175 int *initialized_flag = NULL;
176 m_lldb_ip_regnum = m_lldb_sp_regnum = m_lldb_fp_regnum = -1;
177 if (cpu == k_i386)
178 {
179 m_machine_ip_regnum = k_machine_eip;
180 m_machine_sp_regnum = k_machine_esp;
181 m_machine_fp_regnum = k_machine_ebp;
182 m_wordsize = 4;
183 initialized_flag = &i386_register_map_initialized;
184 }
185 else
186 {
187 m_machine_ip_regnum = k_machine_rip;
188 m_machine_sp_regnum = k_machine_rsp;
189 m_machine_fp_regnum = k_machine_rbp;
190 m_wordsize = 8;
191 initialized_flag = &x86_64_register_map_initialized;
192 }
193
194 // we only look at prologue - it will be complete earlier than 512 bytes into func
195 if (m_func_bounds.GetByteSize() == 0)
196 m_func_bounds.SetByteSize(512);
197
198 if (m_thread && *initialized_flag == 0)
199 {
200 RegisterContext *rctx = m_thread->GetRegisterContext();
201 if (rctx)
202 {
203 struct regmap_ent *ent;
204 int count, i;
205 if (cpu == k_i386)
206 {
207 ent = i386_register_map;
208 count = size_of_i386_register_map;
209 }
210 else
211 {
212 ent = x86_64_register_map;
213 count = size_of_x86_64_register_map;
214 }
215 for (i = 0; i < count; i++, ent++)
216 {
217 const RegisterInfo *ri = rctx->GetRegisterInfoByName (ent->name);
218 if (ri)
219 ent->lldb_regno = ri->kinds[eRegisterKindLLDB];
220 }
221 *initialized_flag = 1;
222 }
223 }
224
225 // on initial construction we may not have a Thread so these have to remain
226 // uninitialized until we can get a RegisterContext to set up the register map table
227 if (*initialized_flag == 1)
228 {
229 uint32_t lldb_regno;
230 if (machine_regno_to_lldb_regno (m_machine_sp_regnum, lldb_regno))
231 m_lldb_sp_regnum = lldb_regno;
232 if (machine_regno_to_lldb_regno (m_machine_fp_regnum, lldb_regno))
233 m_lldb_fp_regnum = lldb_regno;
234 if (machine_regno_to_lldb_regno (m_machine_ip_regnum, lldb_regno))
235 m_lldb_ip_regnum = lldb_regno;
236 }
237}
238
239
240// This function expects an x86 native register number (i.e. the bits stripped out of the
241// actual instruction), not an lldb register number.
242
243bool
244AssemblyParse_x86::nonvolatile_reg_p (int machine_regno)
245{
246 if (m_cpu == k_i386)
247 {
248 switch (machine_regno) {
249 case k_machine_ebx:
250 case k_machine_ebp: // not actually a nonvolatile but often treated as such by convention
251 case k_machine_esi:
252 case k_machine_edi:
253 case k_machine_esp:
254 return true;
255 default:
256 return false;
257 }
258 }
259 if (m_cpu == k_x86_64)
260 {
261 switch (machine_regno) {
262 case k_machine_rbx:
263 case k_machine_rsp:
264 case k_machine_rbp: // not actually a nonvolatile but often treated as such by convention
265 case k_machine_r12:
266 case k_machine_r13:
267 case k_machine_r14:
268 case k_machine_r15:
269 return true;
270 default:
271 return false;
272 }
273 }
274 return false;
275}
276
277
278// Macro to detect if this is a REX mode prefix byte.
279#define REX_W_PREFIX_P(opcode) (((opcode) & (~0x5)) == 0x48)
280
281// The high bit which should be added to the source register number (the "R" bit)
282#define REX_W_SRCREG(opcode) (((opcode) & 0x4) >> 2)
283
284// The high bit which should be added to the destination register number (the "B" bit)
285#define REX_W_DSTREG(opcode) ((opcode) & 0x1)
286
287// pushq %rbp [0x55]
288bool AssemblyParse_x86::push_rbp_pattern_p () {
289 uint8_t *p = m_cur_insn_bytes;
290 if (*p == 0x55)
291 return true;
292 return false;
293}
294
295// pushq $0 ; the first instruction in start() [0x6a 0x00]
296bool AssemblyParse_x86::push_0_pattern_p ()
297{
298 uint8_t *p = m_cur_insn_bytes;
299 if (*p == 0x6a && *(p + 1) == 0x0)
300 return true;
301 return false;
302}
303
304// movq %rsp, %rbp [0x48 0x8b 0xec] or [0x48 0x89 0xe5]
305// movl %esp, %ebp [0x8b 0xec] or [0x89 0xe5]
306bool AssemblyParse_x86::mov_rsp_rbp_pattern_p () {
307 uint8_t *p = m_cur_insn_bytes;
308 if (m_wordsize == 8 && *p == 0x48)
309 p++;
310 if (*(p) == 0x8b && *(p + 1) == 0xec)
311 return true;
312 if (*(p) == 0x89 && *(p + 1) == 0xe5)
313 return true;
314 return false;
315}
316
317// subq $0x20, %rsp
318bool AssemblyParse_x86::sub_rsp_pattern_p (int& amount) {
319 uint8_t *p = m_cur_insn_bytes;
320 if (m_wordsize == 8 && *p == 0x48)
321 p++;
322 // 8-bit immediate operand
323 if (*p == 0x83 && *(p + 1) == 0xec) {
324 amount = (int8_t) *(p + 2);
325 return true;
326 }
327 // 32-bit immediate operand
328 if (*p == 0x81 && *(p + 1) == 0xec) {
329 amount = (int32_t) extract_4 (p + 2);
330 return true;
331 }
332 // Not handled: [0x83 0xc4] for imm8 with neg values
333 // [0x81 0xc4] for imm32 with neg values
334 return false;
335}
336
337// pushq %rbx
338// pushl $ebx
339bool AssemblyParse_x86::push_reg_p (int& regno) {
340 uint8_t *p = m_cur_insn_bytes;
341 int regno_prefix_bit = 0;
342 // If we have a rex prefix byte, check to see if a B bit is set
343 if (m_wordsize == 8 && *p == 0x41) {
344 regno_prefix_bit = 1 << 3;
345 p++;
346 }
347 if (*p >= 0x50 && *p <= 0x57) {
348 regno = (*p - 0x50) | regno_prefix_bit;
349 return true;
350 }
351 return false;
352}
353
354// Look for an instruction sequence storing a nonvolatile register
355// on to the stack frame.
356
357// movq %rax, -0x10(%rbp) [0x48 0x89 0x45 0xf0]
358// movl %eax, -0xc(%ebp) [0x89 0x45 0xf4]
359bool AssemblyParse_x86::mov_reg_to_local_stack_frame_p (int& regno, int& rbp_offset) {
360 uint8_t *p = m_cur_insn_bytes;
361 int src_reg_prefix_bit = 0;
362 int target_reg_prefix_bit = 0;
363
364 if (m_wordsize == 8 && REX_W_PREFIX_P (*p)) {
365 src_reg_prefix_bit = REX_W_SRCREG (*p) << 3;
366 target_reg_prefix_bit = REX_W_DSTREG (*p) << 3;
367 if (target_reg_prefix_bit == 1) {
368 // rbp/ebp don't need a prefix bit - we know this isn't the
369 // reg we care about.
370 return false;
371 }
372 p++;
373 }
374
375 if (*p == 0x89) {
376 /* Mask off the 3-5 bits which indicate the destination register
377 if this is a ModR/M byte. */
378 int opcode_destreg_masked_out = *(p + 1) & (~0x38);
379
380 /* Is this a ModR/M byte with Mod bits 01 and R/M bits 101
381 and three bits between them, e.g. 01nnn101
382 We're looking for a destination of ebp-disp8 or ebp-disp32. */
383 int immsize;
384 if (opcode_destreg_masked_out == 0x45)
385 immsize = 2;
386 else if (opcode_destreg_masked_out == 0x85)
387 immsize = 4;
388 else
389 return false;
390
391 int offset = 0;
392 if (immsize == 2)
393 offset = (int8_t) *(p + 2);
394 if (immsize == 4)
395 offset = (uint32_t) extract_4 (p + 2);
396 if (offset > 0)
397 return false;
398
399 regno = ((*(p + 1) >> 3) & 0x7) | src_reg_prefix_bit;
400 rbp_offset = offset > 0 ? offset : -offset;
401 return true;
402 }
403 return false;
404}
405
406// ret [0xc9] or [0xc2 imm8] or [0xca imm8]
407bool
408AssemblyParse_x86::ret_pattern_p ()
409{
410 uint8_t *p = m_cur_insn_bytes;
411 if (*p == 0xc9 || *p == 0xc2 || *p == 0xca || *p == 0xc3)
412 return true;
413 return false;
414}
415
416uint32_t
417AssemblyParse_x86::extract_4 (uint8_t *b)
418{
419 uint32_t v = 0;
420 for (int i = 3; i >= 0; i--)
421 v = (v << 8) | b[i];
422 return v;
423}
424
425bool
426AssemblyParse_x86::machine_regno_to_lldb_regno (int machine_regno, uint32_t &lldb_regno)
427{
428 struct regmap_ent *ent;
429 int count, i;
430 if (m_cpu == k_i386)
431 {
432 ent = i386_register_map;
433 count = size_of_i386_register_map;
434 }
435 else
436 {
437 ent = x86_64_register_map;
438 count = size_of_x86_64_register_map;
439 }
440 for (i = 0; i < count; i++, ent++)
441 {
442 if (ent->machine_regno == machine_regno)
443 if (ent->lldb_regno != -1)
444 {
445 lldb_regno = ent->lldb_regno;
446 return true;
447 }
448 }
449 return false;
450}
451
452struct edis_byte_read_token
453{
454 Address *address;
455 Target *target;
456};
457
458
459static int
460read_byte_for_edis (uint8_t *buf, uint64_t offset_address, void *arg)
461{
462 if (arg == 0)
463 return -1;
464 struct edis_byte_read_token *tok = (edis_byte_read_token *) arg;
465 Address *base_address = tok->address;
466 Target *target = tok->target;
467
468 Address read_addr = *base_address;
469 read_addr.SetOffset (offset_address);
470
471 uint8_t onebyte_buf[1];
472 Error error;
473 if (target->ReadMemory (read_addr, onebyte_buf, 1, error) != -1)
474 {
475 *buf = onebyte_buf[0];
476 return 0;
477 }
478 return -1;
479}
480
481
482bool
483AssemblyParse_x86::instruction_length (Address addr, int &length)
484{
485 const char *triple;
Jason Molenda800d11d2010-11-04 00:53:20 +0000486
487 if (!addr.IsValid())
488 return false;
489
Jason Molenda3a4ea242010-09-10 07:49:16 +0000490 // FIXME should probably pass down the ArchSpec and work from that to make a portable triple
491 if (m_cpu == k_i386)
Jason Molendaa2c269c2010-10-26 00:47:17 +0000492 triple = "i386-unknown-unknown";
Jason Molenda3a4ea242010-09-10 07:49:16 +0000493 else
Jason Molendaa2c269c2010-10-26 00:47:17 +0000494 triple = "x86_64-unknown-unknown";
Jason Molenda3a4ea242010-09-10 07:49:16 +0000495
496 EDDisassemblerRef disasm;
497 EDInstRef cur_insn;
498
Jason Molendaa2c269c2010-10-26 00:47:17 +0000499 if (EDGetDisassembler (&disasm, triple, kEDAssemblySyntaxX86ATT) != 0)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000500 {
Jason Molenda8280cbe2010-10-25 11:12:07 +0000501 return false;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000502 }
503
504 uint64_t addr_offset = addr.GetOffset();
505 struct edis_byte_read_token arg;
506 arg.address = &addr;
507 arg.target = &m_target;
508 if (EDCreateInsts (&cur_insn, 1, disasm, read_byte_for_edis, addr_offset, &arg) != 1)
509 {
Jason Molenda8280cbe2010-10-25 11:12:07 +0000510 return false;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000511 }
512 length = EDInstByteSize (cur_insn);
513 EDReleaseInst (cur_insn);
514 return true;
515}
516
517
518bool
519AssemblyParse_x86::get_non_call_site_unwind_plan (UnwindPlan &unwind_plan)
520{
521 UnwindPlan up;
522 UnwindPlan::Row row;
523 int non_prologue_insn_count = 0;
Jason Molenda800d11d2010-11-04 00:53:20 +0000524 m_cur_insn = m_func_bounds.GetBaseAddress ();
Jason Molenda3a4ea242010-09-10 07:49:16 +0000525 int current_func_text_offset = 0;
526 int current_sp_bytes_offset_from_cfa = 0;
Jason Molendaa6b71de2010-11-04 09:40:56 +0000527 UnwindPlan::Row::RegisterLocation initial_regloc;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000528
Jason Molenda800d11d2010-11-04 00:53:20 +0000529 if (!m_cur_insn.IsValid())
530 {
531 return false;
532 }
533
Jason Molenda3a4ea242010-09-10 07:49:16 +0000534 unwind_plan.SetPlanValidAddressRange (m_func_bounds);
535 unwind_plan.SetRegisterKind (eRegisterKindLLDB);
536
537 // At the start of the function, find the CFA by adding wordsize to the SP register
538 row.SetOffset (current_func_text_offset);
539 row.SetCFARegister (m_lldb_sp_regnum);
540 row.SetCFAOffset (m_wordsize);
541
542 // caller's stack pointer value before the call insn is the CFA address
Jason Molendaa6b71de2010-11-04 09:40:56 +0000543 initial_regloc.SetIsCFAPlusOffset (0);
544 row.SetRegisterInfo (m_lldb_sp_regnum, initial_regloc);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000545
546 // saved instruction pointer can be found at CFA - wordsize.
547 current_sp_bytes_offset_from_cfa = m_wordsize;
Jason Molendaa6b71de2010-11-04 09:40:56 +0000548 initial_regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa);
549 row.SetRegisterInfo (m_lldb_ip_regnum, initial_regloc);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000550
551 unwind_plan.AppendRow (row);
552
553 while (m_func_bounds.ContainsFileAddress (m_cur_insn) && non_prologue_insn_count < 10)
554 {
555 int stack_offset, insn_len;
556 int machine_regno; // register numbers masked directly out of instructions
557 uint32_t lldb_regno; // register numbers in lldb's eRegisterKindLLDB numbering scheme
558 Error error;
559
560 if (!instruction_length (m_cur_insn, insn_len) || insn_len == 0 || insn_len > kMaxInstructionByteSize)
561 {
562 // An unrecognized/junk instruction
563 break;
564 }
565 if (m_target.ReadMemory (m_cur_insn, m_cur_insn_bytes, insn_len, error) == -1)
566 {
567 // Error reading the instruction out of the file, stop scanning
568 break;
569 }
570
571 if (push_rbp_pattern_p ())
572 {
573 row.SetOffset (current_func_text_offset + insn_len);
574 current_sp_bytes_offset_from_cfa += m_wordsize;
575 row.SetCFAOffset (current_sp_bytes_offset_from_cfa);
576 UnwindPlan::Row::RegisterLocation regloc;
577 regloc.SetAtCFAPlusOffset (-row.GetCFAOffset());
578 row.SetRegisterInfo (m_lldb_fp_regnum, regloc);
579 unwind_plan.AppendRow (row);
580 goto loopnext;
581 }
Jason Molendaa2c269c2010-10-26 00:47:17 +0000582
583 if (mov_rsp_rbp_pattern_p ())
584 {
585 row.SetOffset (current_func_text_offset + insn_len);
586 row.SetCFARegister (m_lldb_fp_regnum);
587 unwind_plan.AppendRow (row);
588 goto loopnext;
589 }
590
Jason Molenda8280cbe2010-10-25 11:12:07 +0000591 // This is the start() function (or a pthread equivalent), it starts with a pushl $0x0 which puts the
592 // saved pc value of 0 on the stack. In this case we want to pretend we didn't see a stack movement at all --
593 // normally the saved pc value is already on the stack by the time the function starts executing.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000594 if (push_0_pattern_p ())
595 {
Jason Molenda3a4ea242010-09-10 07:49:16 +0000596 goto loopnext;
597 }
598
599 if (push_reg_p (machine_regno))
600 {
601 current_sp_bytes_offset_from_cfa += m_wordsize;
602 if (nonvolatile_reg_p (machine_regno) && machine_regno_to_lldb_regno (machine_regno, lldb_regno))
603 {
604 row.SetOffset (current_func_text_offset + insn_len);
605 if (row.GetCFARegister() == m_lldb_sp_regnum)
606 {
607 row.SetCFAOffset (current_sp_bytes_offset_from_cfa);
608 }
609 UnwindPlan::Row::RegisterLocation regloc;
610 regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa);
611 row.SetRegisterInfo (lldb_regno, regloc);
612 unwind_plan.AppendRow (row);
613 }
614 goto loopnext;
615 }
616
617 if (mov_reg_to_local_stack_frame_p (machine_regno, stack_offset) && nonvolatile_reg_p (machine_regno))
618 {
619 if (machine_regno_to_lldb_regno (machine_regno, lldb_regno))
620 {
621 row.SetOffset (current_func_text_offset + insn_len);
622 UnwindPlan::Row::RegisterLocation regloc;
623 regloc.SetAtCFAPlusOffset (-row.GetCFAOffset());
624 row.SetRegisterInfo (lldb_regno, regloc);
625 unwind_plan.AppendRow (row);
626 goto loopnext;
627 }
628 }
629
630 if (sub_rsp_pattern_p (stack_offset))
631 {
632 current_sp_bytes_offset_from_cfa += stack_offset;
633 if (row.GetCFARegister() == m_lldb_sp_regnum)
634 {
635 row.SetOffset (current_func_text_offset + insn_len);
636 row.SetCFAOffset (current_sp_bytes_offset_from_cfa);
637 unwind_plan.AppendRow (row);
638 }
639 goto loopnext;
640 }
641
Jason Molenda3a4ea242010-09-10 07:49:16 +0000642 if (ret_pattern_p ())
643 {
644 // we know where the end of the function is; set the limit on the PlanValidAddressRange
645 // in case our initial "high pc" value was overly large
646 // int original_size = m_func_bounds.GetByteSize();
647 // int calculated_size = m_cur_insn.GetOffset() - m_func_bounds.GetBaseAddress().GetOffset() + insn_len + 1;
648 // m_func_bounds.SetByteSize (calculated_size);
649 // unwind_plan.SetPlanValidAddressRange (m_func_bounds);
650 break;
651 }
652
653 // FIXME recognize the i386 picbase setup instruction sequence,
654 // 0x1f16: call 0x1f1b ; main + 11 at /private/tmp/a.c:3
655 // 0x1f1b: popl %eax
656 // and record the temporary stack movements if the CFA is not expressed in terms of ebp.
657
658 non_prologue_insn_count++;
659loopnext:
660 m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len);
661 current_func_text_offset += insn_len;
662 }
663
Jason Molenda8280cbe2010-10-25 11:12:07 +0000664 unwind_plan.SetSourceName ("assembly insn profiling");
665
Jason Molenda3a4ea242010-09-10 07:49:16 +0000666 return true;
667}
668
Jason Molenda8280cbe2010-10-25 11:12:07 +0000669/* The "fast unwind plan" is valid for functions that follow the usual convention of
670 using the frame pointer register (ebp, rbp), i.e. the function prologue looks like
671 push %rbp [0x55]
672 mov %rsp,%rbp [0x48 0x89 0xe5] (this is a 2-byte insn seq on i386)
673*/
674
Jason Molenda3a4ea242010-09-10 07:49:16 +0000675bool
Jason Molenda8280cbe2010-10-25 11:12:07 +0000676AssemblyParse_x86::get_fast_unwind_plan (AddressRange& func, UnwindPlan &unwind_plan)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000677{
Jason Molenda8280cbe2010-10-25 11:12:07 +0000678 UnwindPlan::Row row;
679 UnwindPlan::Row::RegisterLocation pc_reginfo;
680 UnwindPlan::Row::RegisterLocation sp_reginfo;
681 UnwindPlan::Row::RegisterLocation fp_reginfo;
682 unwind_plan.SetRegisterKind (eRegisterKindLLDB);
683
684 if (!func.GetBaseAddress().IsValid())
685 return false;
686
687 uint8_t bytebuf[4];
688 Error error;
689 if (m_target.ReadMemory (func.GetBaseAddress(), bytebuf, sizeof (bytebuf), error) == -1)
690 return false;
691
692 uint8_t i386_prologue[] = {0x55, 0x89, 0xe5};
693 uint8_t x86_64_prologue[] = {0x55, 0x48, 0x89, 0xe5};
694 int prologue_size;
695
696 if (memcmp (bytebuf, i386_prologue, sizeof (i386_prologue)) == 0)
697 {
698 prologue_size = sizeof (i386_prologue);
699 }
700 else if (memcmp (bytebuf, x86_64_prologue, sizeof (x86_64_prologue)) == 0)
701 {
702 prologue_size = sizeof (x86_64_prologue);
703 }
704 else
705 {
706 return false;
707 }
708
709 pc_reginfo.SetAtCFAPlusOffset (-m_wordsize);
710 row.SetRegisterInfo (m_lldb_ip_regnum, pc_reginfo);
711
712 sp_reginfo.SetIsCFAPlusOffset (0);
713 row.SetRegisterInfo (m_lldb_sp_regnum, sp_reginfo);
714
715 // Zero instructions into the function
716 row.SetCFARegister (m_lldb_sp_regnum);
717 row.SetCFAOffset (m_wordsize);
718 row.SetOffset (0);
719 unwind_plan.AppendRow (row);
720
721 // push %rbp has executed - stack moved, rbp now saved
722 row.SetCFAOffset (2 * m_wordsize);
723 fp_reginfo.SetAtCFAPlusOffset (2 * -m_wordsize);
724 row.SetRegisterInfo (m_lldb_fp_regnum, fp_reginfo);
725 row.SetOffset (1);
726 unwind_plan.AppendRow (row);
727
728 // mov %rsp, %rbp has executed
729 row.SetCFARegister (m_lldb_fp_regnum);
730 row.SetCFAOffset (2 * m_wordsize);
731 row.SetOffset (prologue_size); /// 3 or 4 bytes depending on arch
732 unwind_plan.AppendRow (row);
733
734 unwind_plan.SetPlanValidAddressRange (func);
735 return true;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000736}
737
738bool
739AssemblyParse_x86::find_first_non_prologue_insn (Address &address)
740{
741 m_cur_insn = m_func_bounds.GetBaseAddress ();
Jason Molenda800d11d2010-11-04 00:53:20 +0000742 if (!m_cur_insn.IsValid())
743 {
744 return false;
745 }
746
Jason Molenda3a4ea242010-09-10 07:49:16 +0000747 while (m_func_bounds.ContainsFileAddress (m_cur_insn))
748 {
749 Error error;
750 int insn_len, offset, regno;
751 if (!instruction_length (m_cur_insn, insn_len) || insn_len > kMaxInstructionByteSize || insn_len == 0)
752 {
753 // An error parsing the instruction, i.e. probably data/garbage - stop scanning
754 break;
755 }
756 if (m_target.ReadMemory (m_cur_insn, m_cur_insn_bytes, insn_len, error) == -1)
757 {
758 // Error reading the instruction out of the file, stop scanning
759 break;
760 }
761
762 if (push_rbp_pattern_p () || mov_rsp_rbp_pattern_p () || sub_rsp_pattern_p (offset)
763 || push_reg_p (regno) || mov_reg_to_local_stack_frame_p (regno, offset))
764 {
765 m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len);
766 continue;
767 }
768
769 // Unknown non-prologue instruction - stop scanning
770 break;
771 }
772
773 address = m_cur_insn;
774 return true;
775}
776
777
778
779
780
781
782//-----------------------------------------------------------------------------------------------
783// UnwindAssemblyParser_x86 method definitions
784//-----------------------------------------------------------------------------------------------
785
786bool
787UnwindAssemblyProfiler_x86::GetNonCallSiteUnwindPlanFromAssembly (AddressRange& func, Thread& thread, UnwindPlan& unwind_plan)
788{
789 AssemblyParse_x86 asm_parse(thread.GetProcess().GetTarget(), &thread, m_cpu, func);
790 return asm_parse.get_non_call_site_unwind_plan (unwind_plan);
791}
792
793bool
794UnwindAssemblyProfiler_x86::GetFastUnwindPlan (AddressRange& func, Thread& thread, UnwindPlan &unwind_plan)
795{
796 AssemblyParse_x86 asm_parse(thread.GetProcess().GetTarget(), &thread, m_cpu, func);
Jason Molenda8280cbe2010-10-25 11:12:07 +0000797 return asm_parse.get_fast_unwind_plan (func, unwind_plan);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000798}
799
800bool
801UnwindAssemblyProfiler_x86::FirstNonPrologueInsn (AddressRange& func, Target& target, Thread* thread, Address& first_non_prologue_insn)
802{
803 AssemblyParse_x86 asm_parse(target, thread, m_cpu, func);
804 return asm_parse.find_first_non_prologue_insn (first_non_prologue_insn);
805}
806
807lldb_private::UnwindAssemblyProfiler *
808UnwindAssemblyProfiler_x86::CreateInstance (const lldb_private::ArchSpec &arch)
809{
810 uint32_t cpu = arch.GetCPUType ();
811 if (cpu != CPU_TYPE_X86_64 && cpu != CPU_TYPE_I386)
812 return NULL;
813
814 return new UnwindAssemblyProfiler_x86 (cpu == CPU_TYPE_X86_64 ? k_x86_64 : k_i386);
815}
816
817
818//------------------------------------------------------------------
819// PluginInterface protocol in UnwindAssemblyParser_x86
820//------------------------------------------------------------------
821
822const char *
823UnwindAssemblyProfiler_x86::GetPluginName()
824{
825 return "UnwindAssemblyProfiler_x86";
826}
827
828const char *
829UnwindAssemblyProfiler_x86::GetShortPluginName()
830{
831 return "unwindassemblyprofiler.x86";
832}
833
834
835uint32_t
836UnwindAssemblyProfiler_x86::GetPluginVersion()
837{
838 return 1;
839}
840
841void
842UnwindAssemblyProfiler_x86::GetPluginCommandHelp (const char *command, Stream *strm)
843{
844}
845
846Error
847UnwindAssemblyProfiler_x86::ExecutePluginCommand (Args &command, Stream *strm)
848{
849 Error error;
850 error.SetErrorString("No plug-in command are currently supported.");
851 return error;
852}
853
854Log *
855UnwindAssemblyProfiler_x86::EnablePluginLogging (Stream *strm, Args &command)
856{
857 return NULL;
858}
859
860void
861UnwindAssemblyProfiler_x86::Initialize()
862{
863 PluginManager::RegisterPlugin (GetPluginNameStatic(),
864 GetPluginDescriptionStatic(),
865 CreateInstance);
866}
867
868void
869UnwindAssemblyProfiler_x86::Terminate()
870{
871 PluginManager::UnregisterPlugin (CreateInstance);
872}
873
874
875const char *
876UnwindAssemblyProfiler_x86::GetPluginNameStatic()
877{
878 return "UnwindAssemblyProfiler_x86";
879}
880
881const char *
882UnwindAssemblyProfiler_x86::GetPluginDescriptionStatic()
883{
884 return "i386 and x86_64 assembly language profiler plugin.";
885}