blob: fd42948d5db7e77e0a99b6182a3b53d69f1ff5ba [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;
Greg Clayton26100dc2011-01-07 01:57:07 +0000473 const bool prefer_file_cache = true;
474 if (target->ReadMemory (read_addr, prefer_file_cache, onebyte_buf, 1, error) != -1)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000475 {
476 *buf = onebyte_buf[0];
477 return 0;
478 }
479 return -1;
480}
481
482
483bool
484AssemblyParse_x86::instruction_length (Address addr, int &length)
485{
486 const char *triple;
Jason Molenda800d11d2010-11-04 00:53:20 +0000487
488 if (!addr.IsValid())
489 return false;
490
Jason Molenda3a4ea242010-09-10 07:49:16 +0000491 // FIXME should probably pass down the ArchSpec and work from that to make a portable triple
492 if (m_cpu == k_i386)
Jason Molendaa2c269c2010-10-26 00:47:17 +0000493 triple = "i386-unknown-unknown";
Jason Molenda3a4ea242010-09-10 07:49:16 +0000494 else
Jason Molendaa2c269c2010-10-26 00:47:17 +0000495 triple = "x86_64-unknown-unknown";
Jason Molenda3a4ea242010-09-10 07:49:16 +0000496
497 EDDisassemblerRef disasm;
498 EDInstRef cur_insn;
499
Jason Molendaa2c269c2010-10-26 00:47:17 +0000500 if (EDGetDisassembler (&disasm, triple, kEDAssemblySyntaxX86ATT) != 0)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000501 {
Jason Molenda8280cbe2010-10-25 11:12:07 +0000502 return false;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000503 }
504
505 uint64_t addr_offset = addr.GetOffset();
506 struct edis_byte_read_token arg;
507 arg.address = &addr;
508 arg.target = &m_target;
509 if (EDCreateInsts (&cur_insn, 1, disasm, read_byte_for_edis, addr_offset, &arg) != 1)
510 {
Jason Molenda8280cbe2010-10-25 11:12:07 +0000511 return false;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000512 }
513 length = EDInstByteSize (cur_insn);
514 EDReleaseInst (cur_insn);
515 return true;
516}
517
518
519bool
520AssemblyParse_x86::get_non_call_site_unwind_plan (UnwindPlan &unwind_plan)
521{
522 UnwindPlan up;
523 UnwindPlan::Row row;
524 int non_prologue_insn_count = 0;
Jason Molenda800d11d2010-11-04 00:53:20 +0000525 m_cur_insn = m_func_bounds.GetBaseAddress ();
Jason Molenda3a4ea242010-09-10 07:49:16 +0000526 int current_func_text_offset = 0;
527 int current_sp_bytes_offset_from_cfa = 0;
Jason Molendaa6b71de2010-11-04 09:40:56 +0000528 UnwindPlan::Row::RegisterLocation initial_regloc;
Jason Molenda19e54352010-11-16 03:01:20 +0000529 Error error;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000530
Jason Molenda800d11d2010-11-04 00:53:20 +0000531 if (!m_cur_insn.IsValid())
532 {
533 return false;
534 }
535
Jason Molenda3a4ea242010-09-10 07:49:16 +0000536 unwind_plan.SetPlanValidAddressRange (m_func_bounds);
537 unwind_plan.SetRegisterKind (eRegisterKindLLDB);
538
539 // At the start of the function, find the CFA by adding wordsize to the SP register
540 row.SetOffset (current_func_text_offset);
541 row.SetCFARegister (m_lldb_sp_regnum);
542 row.SetCFAOffset (m_wordsize);
543
544 // caller's stack pointer value before the call insn is the CFA address
Jason Molendaa6b71de2010-11-04 09:40:56 +0000545 initial_regloc.SetIsCFAPlusOffset (0);
546 row.SetRegisterInfo (m_lldb_sp_regnum, initial_regloc);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000547
548 // saved instruction pointer can be found at CFA - wordsize.
549 current_sp_bytes_offset_from_cfa = m_wordsize;
Jason Molendaa6b71de2010-11-04 09:40:56 +0000550 initial_regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa);
551 row.SetRegisterInfo (m_lldb_ip_regnum, initial_regloc);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000552
553 unwind_plan.AppendRow (row);
Greg Clayton26100dc2011-01-07 01:57:07 +0000554 const bool prefer_file_cache = true;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000555
556 while (m_func_bounds.ContainsFileAddress (m_cur_insn) && non_prologue_insn_count < 10)
557 {
558 int stack_offset, insn_len;
559 int machine_regno; // register numbers masked directly out of instructions
560 uint32_t lldb_regno; // register numbers in lldb's eRegisterKindLLDB numbering scheme
Jason Molenda3a4ea242010-09-10 07:49:16 +0000561
562 if (!instruction_length (m_cur_insn, insn_len) || insn_len == 0 || insn_len > kMaxInstructionByteSize)
563 {
564 // An unrecognized/junk instruction
565 break;
566 }
Greg Clayton26100dc2011-01-07 01:57:07 +0000567 if (m_target.ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000568 {
569 // Error reading the instruction out of the file, stop scanning
570 break;
571 }
572
573 if (push_rbp_pattern_p ())
574 {
575 row.SetOffset (current_func_text_offset + insn_len);
576 current_sp_bytes_offset_from_cfa += m_wordsize;
577 row.SetCFAOffset (current_sp_bytes_offset_from_cfa);
578 UnwindPlan::Row::RegisterLocation regloc;
579 regloc.SetAtCFAPlusOffset (-row.GetCFAOffset());
580 row.SetRegisterInfo (m_lldb_fp_regnum, regloc);
581 unwind_plan.AppendRow (row);
582 goto loopnext;
583 }
Jason Molendaa2c269c2010-10-26 00:47:17 +0000584
585 if (mov_rsp_rbp_pattern_p ())
586 {
587 row.SetOffset (current_func_text_offset + insn_len);
588 row.SetCFARegister (m_lldb_fp_regnum);
589 unwind_plan.AppendRow (row);
590 goto loopnext;
591 }
592
Jason Molenda8280cbe2010-10-25 11:12:07 +0000593 // This is the start() function (or a pthread equivalent), it starts with a pushl $0x0 which puts the
594 // saved pc value of 0 on the stack. In this case we want to pretend we didn't see a stack movement at all --
595 // normally the saved pc value is already on the stack by the time the function starts executing.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000596 if (push_0_pattern_p ())
597 {
Jason Molenda3a4ea242010-09-10 07:49:16 +0000598 goto loopnext;
599 }
600
601 if (push_reg_p (machine_regno))
602 {
603 current_sp_bytes_offset_from_cfa += m_wordsize;
604 if (nonvolatile_reg_p (machine_regno) && machine_regno_to_lldb_regno (machine_regno, lldb_regno))
605 {
606 row.SetOffset (current_func_text_offset + insn_len);
607 if (row.GetCFARegister() == m_lldb_sp_regnum)
608 {
609 row.SetCFAOffset (current_sp_bytes_offset_from_cfa);
610 }
611 UnwindPlan::Row::RegisterLocation regloc;
612 regloc.SetAtCFAPlusOffset (-current_sp_bytes_offset_from_cfa);
613 row.SetRegisterInfo (lldb_regno, regloc);
614 unwind_plan.AppendRow (row);
615 }
616 goto loopnext;
617 }
618
619 if (mov_reg_to_local_stack_frame_p (machine_regno, stack_offset) && nonvolatile_reg_p (machine_regno))
620 {
621 if (machine_regno_to_lldb_regno (machine_regno, lldb_regno))
622 {
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 goto loopnext;
629 }
630 }
631
632 if (sub_rsp_pattern_p (stack_offset))
633 {
634 current_sp_bytes_offset_from_cfa += stack_offset;
635 if (row.GetCFARegister() == m_lldb_sp_regnum)
636 {
637 row.SetOffset (current_func_text_offset + insn_len);
638 row.SetCFAOffset (current_sp_bytes_offset_from_cfa);
639 unwind_plan.AppendRow (row);
640 }
641 goto loopnext;
642 }
643
Jason Molenda3a4ea242010-09-10 07:49:16 +0000644 if (ret_pattern_p ())
645 {
646 // we know where the end of the function is; set the limit on the PlanValidAddressRange
647 // in case our initial "high pc" value was overly large
648 // int original_size = m_func_bounds.GetByteSize();
649 // int calculated_size = m_cur_insn.GetOffset() - m_func_bounds.GetBaseAddress().GetOffset() + insn_len + 1;
650 // m_func_bounds.SetByteSize (calculated_size);
651 // unwind_plan.SetPlanValidAddressRange (m_func_bounds);
652 break;
653 }
654
655 // FIXME recognize the i386 picbase setup instruction sequence,
656 // 0x1f16: call 0x1f1b ; main + 11 at /private/tmp/a.c:3
657 // 0x1f1b: popl %eax
658 // and record the temporary stack movements if the CFA is not expressed in terms of ebp.
659
660 non_prologue_insn_count++;
661loopnext:
662 m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len);
663 current_func_text_offset += insn_len;
664 }
665
Jason Molenda19e54352010-11-16 03:01:20 +0000666 // Now look at the byte at the end of the AddressRange for a limited attempt at describing the
667 // epilogue. If this function is built -fomit-frame-pointer (so the CFA is defined in terms of the
668 // stack pointer) we'd need to profile every instruction which causes rsp to change to backtrace
669 // all the time. But assuming the CFA is in terms of rbp most of the time, this one additional Row
670 // will be sufficient.
671
672 if (m_func_bounds.GetByteSize() > 2)
673 {
674 Address last_insn (m_func_bounds.GetBaseAddress());
675 last_insn.SetOffset (last_insn.GetOffset() + m_func_bounds.GetByteSize() - 1);
676 uint8_t bytebuf[1];
Greg Clayton26100dc2011-01-07 01:57:07 +0000677 if (m_target.ReadMemory (last_insn, prefer_file_cache, bytebuf, 1, error) != -1)
Jason Molenda19e54352010-11-16 03:01:20 +0000678 {
679 if (bytebuf[0] == 0xc3) // ret aka retq
680 {
681 // Create a fresh, empty Row and RegisterLocation - don't mention any other registers
682 UnwindPlan::Row epi_row;
683 UnwindPlan::Row::RegisterLocation epi_regloc;
684
685 // When the ret instruction is about to be executed, here's our state
686 epi_row.SetOffset (m_func_bounds.GetByteSize() - 1);
687 epi_row.SetCFARegister (m_lldb_sp_regnum);
688 epi_row.SetCFAOffset (m_wordsize);
689
690 // caller's stack pointer value before the call insn is the CFA address
691 epi_regloc.SetIsCFAPlusOffset (0);
692 epi_row.SetRegisterInfo (m_lldb_sp_regnum, epi_regloc);
693
694 // saved instruction pointer can be found at CFA - wordsize
695 epi_regloc.SetAtCFAPlusOffset (-m_wordsize);
696 epi_row.SetRegisterInfo (m_lldb_ip_regnum, epi_regloc);
697
698 unwind_plan.AppendRow (epi_row);
699 }
700 }
701 }
702
Jason Molenda8280cbe2010-10-25 11:12:07 +0000703 unwind_plan.SetSourceName ("assembly insn profiling");
704
Jason Molenda3a4ea242010-09-10 07:49:16 +0000705 return true;
706}
707
Jason Molenda8280cbe2010-10-25 11:12:07 +0000708/* The "fast unwind plan" is valid for functions that follow the usual convention of
709 using the frame pointer register (ebp, rbp), i.e. the function prologue looks like
710 push %rbp [0x55]
711 mov %rsp,%rbp [0x48 0x89 0xe5] (this is a 2-byte insn seq on i386)
712*/
713
Jason Molenda3a4ea242010-09-10 07:49:16 +0000714bool
Jason Molenda8280cbe2010-10-25 11:12:07 +0000715AssemblyParse_x86::get_fast_unwind_plan (AddressRange& func, UnwindPlan &unwind_plan)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000716{
Jason Molenda8280cbe2010-10-25 11:12:07 +0000717 UnwindPlan::Row row;
718 UnwindPlan::Row::RegisterLocation pc_reginfo;
719 UnwindPlan::Row::RegisterLocation sp_reginfo;
720 UnwindPlan::Row::RegisterLocation fp_reginfo;
721 unwind_plan.SetRegisterKind (eRegisterKindLLDB);
722
723 if (!func.GetBaseAddress().IsValid())
724 return false;
725
726 uint8_t bytebuf[4];
727 Error error;
Greg Clayton26100dc2011-01-07 01:57:07 +0000728 const bool prefer_file_cache = true;
729 if (m_target.ReadMemory (func.GetBaseAddress(), prefer_file_cache, bytebuf, sizeof (bytebuf), error) == -1)
Jason Molenda8280cbe2010-10-25 11:12:07 +0000730 return false;
731
732 uint8_t i386_prologue[] = {0x55, 0x89, 0xe5};
733 uint8_t x86_64_prologue[] = {0x55, 0x48, 0x89, 0xe5};
734 int prologue_size;
735
736 if (memcmp (bytebuf, i386_prologue, sizeof (i386_prologue)) == 0)
737 {
738 prologue_size = sizeof (i386_prologue);
739 }
740 else if (memcmp (bytebuf, x86_64_prologue, sizeof (x86_64_prologue)) == 0)
741 {
742 prologue_size = sizeof (x86_64_prologue);
743 }
744 else
745 {
746 return false;
747 }
748
749 pc_reginfo.SetAtCFAPlusOffset (-m_wordsize);
750 row.SetRegisterInfo (m_lldb_ip_regnum, pc_reginfo);
751
752 sp_reginfo.SetIsCFAPlusOffset (0);
753 row.SetRegisterInfo (m_lldb_sp_regnum, sp_reginfo);
754
755 // Zero instructions into the function
756 row.SetCFARegister (m_lldb_sp_regnum);
757 row.SetCFAOffset (m_wordsize);
758 row.SetOffset (0);
759 unwind_plan.AppendRow (row);
760
761 // push %rbp has executed - stack moved, rbp now saved
762 row.SetCFAOffset (2 * m_wordsize);
763 fp_reginfo.SetAtCFAPlusOffset (2 * -m_wordsize);
764 row.SetRegisterInfo (m_lldb_fp_regnum, fp_reginfo);
765 row.SetOffset (1);
766 unwind_plan.AppendRow (row);
767
768 // mov %rsp, %rbp has executed
769 row.SetCFARegister (m_lldb_fp_regnum);
770 row.SetCFAOffset (2 * m_wordsize);
771 row.SetOffset (prologue_size); /// 3 or 4 bytes depending on arch
772 unwind_plan.AppendRow (row);
773
774 unwind_plan.SetPlanValidAddressRange (func);
775 return true;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000776}
777
778bool
779AssemblyParse_x86::find_first_non_prologue_insn (Address &address)
780{
781 m_cur_insn = m_func_bounds.GetBaseAddress ();
Jason Molenda800d11d2010-11-04 00:53:20 +0000782 if (!m_cur_insn.IsValid())
783 {
784 return false;
785 }
786
Greg Clayton26100dc2011-01-07 01:57:07 +0000787 const bool prefer_file_cache = true;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000788 while (m_func_bounds.ContainsFileAddress (m_cur_insn))
789 {
790 Error error;
791 int insn_len, offset, regno;
792 if (!instruction_length (m_cur_insn, insn_len) || insn_len > kMaxInstructionByteSize || insn_len == 0)
793 {
794 // An error parsing the instruction, i.e. probably data/garbage - stop scanning
795 break;
796 }
Greg Clayton26100dc2011-01-07 01:57:07 +0000797 if (m_target.ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000798 {
799 // Error reading the instruction out of the file, stop scanning
800 break;
801 }
802
803 if (push_rbp_pattern_p () || mov_rsp_rbp_pattern_p () || sub_rsp_pattern_p (offset)
804 || push_reg_p (regno) || mov_reg_to_local_stack_frame_p (regno, offset))
805 {
806 m_cur_insn.SetOffset (m_cur_insn.GetOffset() + insn_len);
807 continue;
808 }
809
810 // Unknown non-prologue instruction - stop scanning
811 break;
812 }
813
814 address = m_cur_insn;
815 return true;
816}
817
818
819
820
821
822
823//-----------------------------------------------------------------------------------------------
824// UnwindAssemblyParser_x86 method definitions
825//-----------------------------------------------------------------------------------------------
826
827bool
828UnwindAssemblyProfiler_x86::GetNonCallSiteUnwindPlanFromAssembly (AddressRange& func, Thread& thread, UnwindPlan& unwind_plan)
829{
830 AssemblyParse_x86 asm_parse(thread.GetProcess().GetTarget(), &thread, m_cpu, func);
831 return asm_parse.get_non_call_site_unwind_plan (unwind_plan);
832}
833
834bool
835UnwindAssemblyProfiler_x86::GetFastUnwindPlan (AddressRange& func, Thread& thread, UnwindPlan &unwind_plan)
836{
837 AssemblyParse_x86 asm_parse(thread.GetProcess().GetTarget(), &thread, m_cpu, func);
Jason Molenda8280cbe2010-10-25 11:12:07 +0000838 return asm_parse.get_fast_unwind_plan (func, unwind_plan);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000839}
840
841bool
842UnwindAssemblyProfiler_x86::FirstNonPrologueInsn (AddressRange& func, Target& target, Thread* thread, Address& first_non_prologue_insn)
843{
844 AssemblyParse_x86 asm_parse(target, thread, m_cpu, func);
845 return asm_parse.find_first_non_prologue_insn (first_non_prologue_insn);
846}
847
Greg Claytondad0b762010-12-17 15:54:09 +0000848UnwindAssemblyProfiler *
849UnwindAssemblyProfiler_x86::CreateInstance (const ArchSpec &arch)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000850{
Greg Claytondad0b762010-12-17 15:54:09 +0000851 ArchSpec::CPU cpu = arch.GetGenericCPUType ();
852 if (cpu != ArchSpec::eCPU_x86_64 && cpu != ArchSpec::eCPU_i386)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000853 return NULL;
854
Greg Claytondad0b762010-12-17 15:54:09 +0000855 return new UnwindAssemblyProfiler_x86 (cpu == ArchSpec::eCPU_x86_64 ? k_x86_64 : k_i386);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000856}
857
858
859//------------------------------------------------------------------
860// PluginInterface protocol in UnwindAssemblyParser_x86
861//------------------------------------------------------------------
862
863const char *
864UnwindAssemblyProfiler_x86::GetPluginName()
865{
866 return "UnwindAssemblyProfiler_x86";
867}
868
869const char *
870UnwindAssemblyProfiler_x86::GetShortPluginName()
871{
872 return "unwindassemblyprofiler.x86";
873}
874
875
876uint32_t
877UnwindAssemblyProfiler_x86::GetPluginVersion()
878{
879 return 1;
880}
881
882void
883UnwindAssemblyProfiler_x86::GetPluginCommandHelp (const char *command, Stream *strm)
884{
885}
886
887Error
888UnwindAssemblyProfiler_x86::ExecutePluginCommand (Args &command, Stream *strm)
889{
890 Error error;
891 error.SetErrorString("No plug-in command are currently supported.");
892 return error;
893}
894
895Log *
896UnwindAssemblyProfiler_x86::EnablePluginLogging (Stream *strm, Args &command)
897{
898 return NULL;
899}
900
901void
902UnwindAssemblyProfiler_x86::Initialize()
903{
904 PluginManager::RegisterPlugin (GetPluginNameStatic(),
905 GetPluginDescriptionStatic(),
906 CreateInstance);
907}
908
909void
910UnwindAssemblyProfiler_x86::Terminate()
911{
912 PluginManager::UnregisterPlugin (CreateInstance);
913}
914
915
916const char *
917UnwindAssemblyProfiler_x86::GetPluginNameStatic()
918{
919 return "UnwindAssemblyProfiler_x86";
920}
921
922const char *
923UnwindAssemblyProfiler_x86::GetPluginDescriptionStatic()
924{
925 return "i386 and x86_64 assembly language profiler plugin.";
926}