blob: d6463e22474ff9d67ea3ee4f67058a13f3ffd4ef [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 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000200 RegisterContext *reg_ctx = m_thread->GetRegisterContext().get();
201 if (reg_ctx)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000202 {
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 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000217 const RegisterInfo *ri = reg_ctx->GetRegisterInfoByName (ent->name);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000218 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 Molenda19e54352010-11-16 03:01:20 +0000528 Error error;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000529
Jason Molenda800d11d2010-11-04 00:53:20 +0000530 if (!m_cur_insn.IsValid())
531 {
532 return false;
533 }
534
Jason Molenda3a4ea242010-09-10 07:49:16 +0000535 unwind_plan.SetPlanValidAddressRange (m_func_bounds);
536 unwind_plan.SetRegisterKind (eRegisterKindLLDB);
537
538 // At the start of the function, find the CFA by adding wordsize to the SP register
539 row.SetOffset (current_func_text_offset);
540 row.SetCFARegister (m_lldb_sp_regnum);
541 row.SetCFAOffset (m_wordsize);
542
543 // caller's stack pointer value before the call insn is the CFA address
Jason Molendaa6b71de2010-11-04 09:40:56 +0000544 initial_regloc.SetIsCFAPlusOffset (0);
545 row.SetRegisterInfo (m_lldb_sp_regnum, initial_regloc);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000546
547 // saved instruction pointer can be found at CFA - wordsize.
548 current_sp_bytes_offset_from_cfa = m_wordsize;
Jason Molendaa6b71de2010-11-04 09:40:56 +0000549 initial_regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa);
550 row.SetRegisterInfo (m_lldb_ip_regnum, initial_regloc);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000551
552 unwind_plan.AppendRow (row);
553
554 while (m_func_bounds.ContainsFileAddress (m_cur_insn) && non_prologue_insn_count < 10)
555 {
556 int stack_offset, insn_len;
557 int machine_regno; // register numbers masked directly out of instructions
558 uint32_t lldb_regno; // register numbers in lldb's eRegisterKindLLDB numbering scheme
Jason Molenda3a4ea242010-09-10 07:49:16 +0000559
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 Molenda19e54352010-11-16 03:01:20 +0000664 // Now look at the byte at the end of the AddressRange for a limited attempt at describing the
665 // epilogue. If this function is built -fomit-frame-pointer (so the CFA is defined in terms of the
666 // stack pointer) we'd need to profile every instruction which causes rsp to change to backtrace
667 // all the time. But assuming the CFA is in terms of rbp most of the time, this one additional Row
668 // will be sufficient.
669
670 if (m_func_bounds.GetByteSize() > 2)
671 {
672 Address last_insn (m_func_bounds.GetBaseAddress());
673 last_insn.SetOffset (last_insn.GetOffset() + m_func_bounds.GetByteSize() - 1);
674 uint8_t bytebuf[1];
675 if (m_target.ReadMemory (last_insn, bytebuf, 1, error) != -1)
676 {
677 if (bytebuf[0] == 0xc3) // ret aka retq
678 {
679 // Create a fresh, empty Row and RegisterLocation - don't mention any other registers
680 UnwindPlan::Row epi_row;
681 UnwindPlan::Row::RegisterLocation epi_regloc;
682
683 // When the ret instruction is about to be executed, here's our state
684 epi_row.SetOffset (m_func_bounds.GetByteSize() - 1);
685 epi_row.SetCFARegister (m_lldb_sp_regnum);
686 epi_row.SetCFAOffset (m_wordsize);
687
688 // caller's stack pointer value before the call insn is the CFA address
689 epi_regloc.SetIsCFAPlusOffset (0);
690 epi_row.SetRegisterInfo (m_lldb_sp_regnum, epi_regloc);
691
692 // saved instruction pointer can be found at CFA - wordsize
693 epi_regloc.SetAtCFAPlusOffset (-m_wordsize);
694 epi_row.SetRegisterInfo (m_lldb_ip_regnum, epi_regloc);
695
696 unwind_plan.AppendRow (epi_row);
697 }
698 }
699 }
700
Jason Molenda8280cbe2010-10-25 11:12:07 +0000701 unwind_plan.SetSourceName ("assembly insn profiling");
702
Jason Molenda3a4ea242010-09-10 07:49:16 +0000703 return true;
704}
705
Jason Molenda8280cbe2010-10-25 11:12:07 +0000706/* The "fast unwind plan" is valid for functions that follow the usual convention of
707 using the frame pointer register (ebp, rbp), i.e. the function prologue looks like
708 push %rbp [0x55]
709 mov %rsp,%rbp [0x48 0x89 0xe5] (this is a 2-byte insn seq on i386)
710*/
711
Jason Molenda3a4ea242010-09-10 07:49:16 +0000712bool
Jason Molenda8280cbe2010-10-25 11:12:07 +0000713AssemblyParse_x86::get_fast_unwind_plan (AddressRange& func, UnwindPlan &unwind_plan)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000714{
Jason Molenda8280cbe2010-10-25 11:12:07 +0000715 UnwindPlan::Row row;
716 UnwindPlan::Row::RegisterLocation pc_reginfo;
717 UnwindPlan::Row::RegisterLocation sp_reginfo;
718 UnwindPlan::Row::RegisterLocation fp_reginfo;
719 unwind_plan.SetRegisterKind (eRegisterKindLLDB);
720
721 if (!func.GetBaseAddress().IsValid())
722 return false;
723
724 uint8_t bytebuf[4];
725 Error error;
726 if (m_target.ReadMemory (func.GetBaseAddress(), bytebuf, sizeof (bytebuf), error) == -1)
727 return false;
728
729 uint8_t i386_prologue[] = {0x55, 0x89, 0xe5};
730 uint8_t x86_64_prologue[] = {0x55, 0x48, 0x89, 0xe5};
731 int prologue_size;
732
733 if (memcmp (bytebuf, i386_prologue, sizeof (i386_prologue)) == 0)
734 {
735 prologue_size = sizeof (i386_prologue);
736 }
737 else if (memcmp (bytebuf, x86_64_prologue, sizeof (x86_64_prologue)) == 0)
738 {
739 prologue_size = sizeof (x86_64_prologue);
740 }
741 else
742 {
743 return false;
744 }
745
746 pc_reginfo.SetAtCFAPlusOffset (-m_wordsize);
747 row.SetRegisterInfo (m_lldb_ip_regnum, pc_reginfo);
748
749 sp_reginfo.SetIsCFAPlusOffset (0);
750 row.SetRegisterInfo (m_lldb_sp_regnum, sp_reginfo);
751
752 // Zero instructions into the function
753 row.SetCFARegister (m_lldb_sp_regnum);
754 row.SetCFAOffset (m_wordsize);
755 row.SetOffset (0);
756 unwind_plan.AppendRow (row);
757
758 // push %rbp has executed - stack moved, rbp now saved
759 row.SetCFAOffset (2 * m_wordsize);
760 fp_reginfo.SetAtCFAPlusOffset (2 * -m_wordsize);
761 row.SetRegisterInfo (m_lldb_fp_regnum, fp_reginfo);
762 row.SetOffset (1);
763 unwind_plan.AppendRow (row);
764
765 // mov %rsp, %rbp has executed
766 row.SetCFARegister (m_lldb_fp_regnum);
767 row.SetCFAOffset (2 * m_wordsize);
768 row.SetOffset (prologue_size); /// 3 or 4 bytes depending on arch
769 unwind_plan.AppendRow (row);
770
771 unwind_plan.SetPlanValidAddressRange (func);
772 return true;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000773}
774
775bool
776AssemblyParse_x86::find_first_non_prologue_insn (Address &address)
777{
778 m_cur_insn = m_func_bounds.GetBaseAddress ();
Jason Molenda800d11d2010-11-04 00:53:20 +0000779 if (!m_cur_insn.IsValid())
780 {
781 return false;
782 }
783
Jason Molenda3a4ea242010-09-10 07:49:16 +0000784 while (m_func_bounds.ContainsFileAddress (m_cur_insn))
785 {
786 Error error;
787 int insn_len, offset, regno;
788 if (!instruction_length (m_cur_insn, insn_len) || insn_len > kMaxInstructionByteSize || insn_len == 0)
789 {
790 // An error parsing the instruction, i.e. probably data/garbage - stop scanning
791 break;
792 }
793 if (m_target.ReadMemory (m_cur_insn, m_cur_insn_bytes, insn_len, error) == -1)
794 {
795 // Error reading the instruction out of the file, stop scanning
796 break;
797 }
798
799 if (push_rbp_pattern_p () || mov_rsp_rbp_pattern_p () || sub_rsp_pattern_p (offset)
800 || push_reg_p (regno) || mov_reg_to_local_stack_frame_p (regno, offset))
801 {
802 m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len);
803 continue;
804 }
805
806 // Unknown non-prologue instruction - stop scanning
807 break;
808 }
809
810 address = m_cur_insn;
811 return true;
812}
813
814
815
816
817
818
819//-----------------------------------------------------------------------------------------------
820// UnwindAssemblyParser_x86 method definitions
821//-----------------------------------------------------------------------------------------------
822
823bool
824UnwindAssemblyProfiler_x86::GetNonCallSiteUnwindPlanFromAssembly (AddressRange& func, Thread& thread, UnwindPlan& unwind_plan)
825{
826 AssemblyParse_x86 asm_parse(thread.GetProcess().GetTarget(), &thread, m_cpu, func);
827 return asm_parse.get_non_call_site_unwind_plan (unwind_plan);
828}
829
830bool
831UnwindAssemblyProfiler_x86::GetFastUnwindPlan (AddressRange& func, Thread& thread, UnwindPlan &unwind_plan)
832{
833 AssemblyParse_x86 asm_parse(thread.GetProcess().GetTarget(), &thread, m_cpu, func);
Jason Molenda8280cbe2010-10-25 11:12:07 +0000834 return asm_parse.get_fast_unwind_plan (func, unwind_plan);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000835}
836
837bool
838UnwindAssemblyProfiler_x86::FirstNonPrologueInsn (AddressRange& func, Target& target, Thread* thread, Address& first_non_prologue_insn)
839{
840 AssemblyParse_x86 asm_parse(target, thread, m_cpu, func);
841 return asm_parse.find_first_non_prologue_insn (first_non_prologue_insn);
842}
843
Greg Claytondad0b762010-12-17 15:54:09 +0000844UnwindAssemblyProfiler *
845UnwindAssemblyProfiler_x86::CreateInstance (const ArchSpec &arch)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000846{
Greg Claytondad0b762010-12-17 15:54:09 +0000847 ArchSpec::CPU cpu = arch.GetGenericCPUType ();
848 if (cpu != ArchSpec::eCPU_x86_64 && cpu != ArchSpec::eCPU_i386)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000849 return NULL;
850
Greg Claytondad0b762010-12-17 15:54:09 +0000851 return new UnwindAssemblyProfiler_x86 (cpu == ArchSpec::eCPU_x86_64 ? k_x86_64 : k_i386);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000852}
853
854
855//------------------------------------------------------------------
856// PluginInterface protocol in UnwindAssemblyParser_x86
857//------------------------------------------------------------------
858
859const char *
860UnwindAssemblyProfiler_x86::GetPluginName()
861{
862 return "UnwindAssemblyProfiler_x86";
863}
864
865const char *
866UnwindAssemblyProfiler_x86::GetShortPluginName()
867{
868 return "unwindassemblyprofiler.x86";
869}
870
871
872uint32_t
873UnwindAssemblyProfiler_x86::GetPluginVersion()
874{
875 return 1;
876}
877
878void
879UnwindAssemblyProfiler_x86::GetPluginCommandHelp (const char *command, Stream *strm)
880{
881}
882
883Error
884UnwindAssemblyProfiler_x86::ExecutePluginCommand (Args &command, Stream *strm)
885{
886 Error error;
887 error.SetErrorString("No plug-in command are currently supported.");
888 return error;
889}
890
891Log *
892UnwindAssemblyProfiler_x86::EnablePluginLogging (Stream *strm, Args &command)
893{
894 return NULL;
895}
896
897void
898UnwindAssemblyProfiler_x86::Initialize()
899{
900 PluginManager::RegisterPlugin (GetPluginNameStatic(),
901 GetPluginDescriptionStatic(),
902 CreateInstance);
903}
904
905void
906UnwindAssemblyProfiler_x86::Terminate()
907{
908 PluginManager::UnregisterPlugin (CreateInstance);
909}
910
911
912const char *
913UnwindAssemblyProfiler_x86::GetPluginNameStatic()
914{
915 return "UnwindAssemblyProfiler_x86";
916}
917
918const char *
919UnwindAssemblyProfiler_x86::GetPluginDescriptionStatic()
920{
921 return "i386 and x86_64 assembly language profiler plugin.";
922}