OSDN Git Service

2009-12-23 Stan Shebs <stan@codesourcery.com>
[pf3gnuchains/pf3gnuchains3x.git] / gdb / i386-darwin-nat.c
1 /* Darwin support for GDB, the GNU debugger.
2    Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2008, 2009
3    Free Software Foundation, Inc.
4
5    Contributed by Apple Computer, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "target.h"
26 #include "symfile.h"
27 #include "symtab.h"
28 #include "objfiles.h"
29 #include "gdbcmd.h"
30 #include "regcache.h"
31 #include "gdb_assert.h"
32 #include "i386-tdep.h"
33 #include "i387-tdep.h"
34 #include "gdbarch.h"
35 #include "arch-utils.h"
36 #include "gdbcore.h"
37
38 #include "darwin-nat.h"
39 #include "i386-darwin-tdep.h"
40
41 #ifdef BFD64
42 #include "amd64-nat.h"
43 #include "amd64-tdep.h"
44 #include "amd64-darwin-tdep.h"
45 #endif
46
47 /* Read register values from the inferior process.
48    If REGNO is -1, do this for all registers.
49    Otherwise, REGNO specifies which register (so we can save time).  */
50 static void
51 i386_darwin_fetch_inferior_registers (struct target_ops *ops,
52                                       struct regcache *regcache, int regno)
53 {
54   thread_t current_thread = ptid_get_tid (inferior_ptid);
55   int fetched = 0;
56   struct gdbarch *gdbarch = get_regcache_arch (regcache);
57
58 #ifdef BFD64
59   if (gdbarch_ptr_bit (gdbarch) == 64)
60     {
61       if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
62         {
63           x86_thread_state_t gp_regs;
64           unsigned int gp_count = x86_THREAD_STATE_COUNT;
65           kern_return_t ret;
66
67           ret = thread_get_state
68             (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs,
69              &gp_count);
70           if (ret != KERN_SUCCESS)
71             {
72               printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread);
73               MACH_CHECK_ERROR (ret);
74             }
75           amd64_supply_native_gregset (regcache, &gp_regs.uts, -1);
76           fetched++;
77         }
78
79       if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
80         {
81           x86_float_state_t fp_regs;
82           unsigned int fp_count = x86_FLOAT_STATE_COUNT;
83           kern_return_t ret;
84
85           ret = thread_get_state
86             (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
87              &fp_count);
88           if (ret != KERN_SUCCESS)
89             {
90               printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread);
91               MACH_CHECK_ERROR (ret);
92             }
93           amd64_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64.__fpu_fcw);
94           fetched++;
95         }
96     }
97   else
98 #endif
99     {
100       if (regno == -1 || regno < I386_NUM_GREGS)
101         {
102           i386_thread_state_t gp_regs;
103           unsigned int gp_count = i386_THREAD_STATE_COUNT;
104           kern_return_t ret;
105           int i;
106
107           ret = thread_get_state
108             (current_thread, i386_THREAD_STATE, (thread_state_t) & gp_regs,
109              &gp_count);
110           if (ret != KERN_SUCCESS)
111             {
112               printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread);
113               MACH_CHECK_ERROR (ret);
114             }
115           for (i = 0; i < I386_NUM_GREGS; i++)
116             regcache_raw_supply
117               (regcache, i,
118                (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
119
120           fetched++;
121         }
122
123       if (regno == -1
124           || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
125         {
126           i386_float_state_t fp_regs;
127           unsigned int fp_count = i386_FLOAT_STATE_COUNT;
128           kern_return_t ret;
129
130           ret = thread_get_state
131             (current_thread, i386_FLOAT_STATE, (thread_state_t) & fp_regs,
132              &fp_count);
133           if (ret != KERN_SUCCESS)
134             {
135               printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread);
136               MACH_CHECK_ERROR (ret);
137             }
138           i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
139           fetched++;
140         }
141     }
142
143   if (! fetched)
144     {
145       warning (_("unknown register %d"), regno);
146       regcache_raw_supply (regcache, regno, NULL);
147     }
148 }
149
150 /* Store our register values back into the inferior.
151    If REGNO is -1, do this for all registers.
152    Otherwise, REGNO specifies which register (so we can save time).  */
153
154 static void
155 i386_darwin_store_inferior_registers (struct target_ops *ops,
156                                       struct regcache *regcache, int regno)
157 {
158   thread_t current_thread = ptid_get_tid (inferior_ptid);
159   struct gdbarch *gdbarch = get_regcache_arch (regcache);
160
161 #ifdef BFD64
162   if (gdbarch_ptr_bit (gdbarch) == 64)
163     {
164       if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
165         {
166           x86_thread_state_t gp_regs;
167           kern_return_t ret;
168           unsigned int gp_count = x86_THREAD_STATE_COUNT;
169
170           ret = thread_get_state
171             (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
172              &gp_count);
173           MACH_CHECK_ERROR (ret);
174           gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
175           gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
176
177           amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
178
179           ret = thread_set_state (current_thread, x86_THREAD_STATE,
180                                   (thread_state_t) &gp_regs,
181                                   x86_THREAD_STATE_COUNT);
182           MACH_CHECK_ERROR (ret);
183         }
184
185       if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
186         {
187           x86_float_state_t fp_regs;
188           kern_return_t ret;
189           unsigned int fp_count = x86_FLOAT_STATE_COUNT;
190
191           ret = thread_get_state
192             (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
193              &fp_count);
194           MACH_CHECK_ERROR (ret);
195           gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
196           gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
197
198           amd64_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
199
200           ret = thread_set_state (current_thread, x86_FLOAT_STATE,
201                                   (thread_state_t) & fp_regs,
202                                   x86_FLOAT_STATE_COUNT);
203           MACH_CHECK_ERROR (ret);
204         }
205     }
206   else
207 #endif
208     {
209       if (regno == -1 || regno < I386_NUM_GREGS)
210         {
211           i386_thread_state_t gp_regs;
212           kern_return_t ret;
213           unsigned int gp_count = i386_THREAD_STATE_COUNT;
214           int i;
215
216           ret = thread_get_state
217             (current_thread, i386_THREAD_STATE, (thread_state_t) & gp_regs,
218              &gp_count);
219           MACH_CHECK_ERROR (ret);
220
221           for (i = 0; i < I386_NUM_GREGS; i++)
222             if (regno == -1 || regno == i)
223               regcache_raw_collect
224                 (regcache, i,
225                  (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
226
227           ret = thread_set_state (current_thread, i386_THREAD_STATE,
228                                   (thread_state_t) & gp_regs,
229                                   i386_THREAD_STATE_COUNT);
230           MACH_CHECK_ERROR (ret);
231         }
232
233       if (regno == -1
234           || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
235         {
236           i386_float_state_t fp_regs;
237           unsigned int fp_count = i386_FLOAT_STATE_COUNT;
238           kern_return_t ret;
239
240           ret = thread_get_state
241             (current_thread, i386_FLOAT_STATE, (thread_state_t) & fp_regs,
242              &fp_count);
243           MACH_CHECK_ERROR (ret);
244
245           i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
246
247           ret = thread_set_state (current_thread, i386_FLOAT_STATE,
248                                   (thread_state_t) & fp_regs,
249                                   i386_FLOAT_STATE_COUNT);
250           MACH_CHECK_ERROR (ret);
251         }
252     }
253 }
254
255
256 /* Support for debug registers, boosted mostly from i386-linux-nat.c.  */
257
258 #ifndef DR_FIRSTADDR
259 #define DR_FIRSTADDR 0
260 #endif
261
262 #ifndef DR_LASTADDR
263 #define DR_LASTADDR 3
264 #endif
265
266 #ifndef DR_STATUS
267 #define DR_STATUS 6
268 #endif
269
270 #ifndef DR_CONTROL
271 #define DR_CONTROL 7
272 #endif
273
274
275 static void
276 i386_darwin_dr_set (int regnum, uint32_t value)
277 {
278   int current_pid;
279   thread_t current_thread;
280   x86_debug_state_t dr_regs;
281   kern_return_t ret;
282   unsigned int dr_count = x86_DEBUG_STATE_COUNT;
283
284   gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
285
286   current_thread = ptid_get_tid (inferior_ptid);
287
288   dr_regs.dsh.flavor = x86_DEBUG_STATE32;
289   dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT;
290   dr_count = x86_DEBUG_STATE_COUNT;
291   ret = thread_get_state (current_thread, x86_DEBUG_STATE, 
292                           (thread_state_t) &dr_regs, &dr_count);
293
294   if (ret != KERN_SUCCESS)
295     {
296       printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
297       MACH_CHECK_ERROR (ret);
298     }
299
300   switch (regnum) 
301     {
302       case 0:
303         dr_regs.uds.ds32.__dr0 = value;
304         break;
305       case 1:
306         dr_regs.uds.ds32.__dr1 = value;
307         break;
308       case 2:
309         dr_regs.uds.ds32.__dr2 = value;
310         break;
311       case 3:
312         dr_regs.uds.ds32.__dr3 = value;
313         break;
314       case 4:
315         dr_regs.uds.ds32.__dr4 = value;
316         break;
317       case 5:
318         dr_regs.uds.ds32.__dr5 = value;
319         break;
320       case 6:
321         dr_regs.uds.ds32.__dr6 = value;
322         break;
323       case 7:
324         dr_regs.uds.ds32.__dr7 = value;
325         break;
326     }
327
328   ret = thread_set_state (current_thread, x86_DEBUG_STATE, 
329                           (thread_state_t) &dr_regs, dr_count);
330
331   if (ret != KERN_SUCCESS)
332     {
333       printf_unfiltered (_("Error writing debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
334       MACH_CHECK_ERROR (ret);
335     }
336 }
337
338 static uint32_t
339 i386_darwin_dr_get (int regnum)
340 {
341   thread_t current_thread;
342   x86_debug_state_t dr_regs;
343   kern_return_t ret;
344   unsigned int dr_count = x86_DEBUG_STATE_COUNT;
345
346   gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
347
348   current_thread = ptid_get_tid (inferior_ptid);
349
350   dr_regs.dsh.flavor = x86_DEBUG_STATE32;
351   dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT;
352   dr_count = x86_DEBUG_STATE_COUNT;
353   ret = thread_get_state (current_thread, x86_DEBUG_STATE, 
354                           (thread_state_t) &dr_regs, &dr_count);
355
356   if (ret != KERN_SUCCESS)
357     {
358       printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
359       MACH_CHECK_ERROR (ret);
360     }
361
362   switch (regnum) 
363     {
364       case 0:
365         return dr_regs.uds.ds32.__dr0;
366       case 1:
367         return dr_regs.uds.ds32.__dr1;
368       case 2:
369         return dr_regs.uds.ds32.__dr2;
370       case 3:
371         return dr_regs.uds.ds32.__dr3;
372       case 4:
373         return dr_regs.uds.ds32.__dr4;
374       case 5:
375         return dr_regs.uds.ds32.__dr5;
376       case 6:
377         return dr_regs.uds.ds32.__dr6;
378       case 7:
379         return dr_regs.uds.ds32.__dr7;
380       default:
381         return -1;
382     }
383 }
384
385 void
386 i386_darwin_dr_set_control (unsigned long control)
387 {
388   i386_darwin_dr_set (DR_CONTROL, control);
389 }
390
391 void
392 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
393 {
394   gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
395
396   i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
397 }
398
399 void
400 i386_darwin_dr_reset_addr (int regnum)
401 {
402   gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
403
404   i386_darwin_dr_set (DR_FIRSTADDR + regnum, 0L);
405 }
406
407 unsigned long
408 i386_darwin_dr_get_status (void)
409 {
410   return i386_darwin_dr_get (DR_STATUS);
411 }
412
413 void
414 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
415 {
416   if (gdbarch_osabi (target_gdbarch) == GDB_OSABI_UNKNOWN)
417     {
418       /* Attaching to a process.  Let's figure out what kind it is.  */
419       x86_thread_state_t gp_regs;
420       struct gdbarch_info info;
421       unsigned int gp_count = x86_THREAD_STATE_COUNT;
422       kern_return_t ret;
423
424       ret = thread_get_state (thread, x86_THREAD_STATE,
425                               (thread_state_t) &gp_regs, &gp_count);
426       if (ret != KERN_SUCCESS)
427         {
428           MACH_CHECK_ERROR (ret);
429           return;
430         }
431
432       gdbarch_info_init (&info);
433       gdbarch_info_fill (&info);
434       info.byte_order = gdbarch_byte_order (target_gdbarch);
435       info.osabi = GDB_OSABI_DARWIN;
436       if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
437         info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
438                                               bfd_mach_x86_64);
439       else
440         info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386, 
441                                               bfd_mach_i386_i386);
442       gdbarch_update_p (info);
443     }
444 }
445
446 #define X86_EFLAGS_T 0x100UL
447
448 /* Returning from a signal trampoline is done by calling a
449    special system call (sigreturn).  This system call
450    restores the registers that were saved when the signal was
451    raised, including %eflags/%rflags.  That means that single-stepping
452    won't work.  Instead, we'll have to modify the signal context
453    that's about to be restored, and set the trace flag there.  */
454
455 static int
456 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
457 {
458   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
459   static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
460   gdb_byte buf[sizeof (darwin_syscall)];
461
462   /* Check if PC is at a sigreturn system call.  */
463   if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
464       && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
465       && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
466     {
467       ULONGEST uctx_addr;
468       ULONGEST mctx_addr;
469       ULONGEST flags_addr;
470       unsigned int eflags;
471
472       uctx_addr = read_memory_unsigned_integer
473                     (regs->uts.ts32.__esp + 4, 4, byte_order);
474       mctx_addr = read_memory_unsigned_integer
475                     (uctx_addr + 28, 4, byte_order);
476
477       flags_addr = mctx_addr + 12 + 9 * 4;
478       read_memory (flags_addr, (gdb_byte *) &eflags, 4);
479       eflags |= X86_EFLAGS_T;
480       write_memory (flags_addr, (gdb_byte *) &eflags, 4);
481
482       return 1;
483     }
484   return 0;
485 }
486
487 #ifdef BFD64
488 static int
489 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
490 {
491   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
492   static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
493   gdb_byte buf[sizeof (darwin_syscall)];
494
495   /* Check if PC is at a sigreturn system call.  */
496   if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
497       && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
498       && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
499     {
500       ULONGEST mctx_addr;
501       ULONGEST flags_addr;
502       unsigned int rflags;
503
504       mctx_addr = read_memory_unsigned_integer
505                     (regs->uts.ts64.__rdi + 48, 8, byte_order);
506       flags_addr = mctx_addr + 16 + 17 * 8;
507
508       /* AMD64 is little endian.  */
509       read_memory (flags_addr, (gdb_byte *) &rflags, 4);
510       rflags |= X86_EFLAGS_T;
511       write_memory (flags_addr, (gdb_byte *) &rflags, 4);
512
513       return 1;
514     }
515   return 0;
516 }
517 #endif
518
519 void
520 darwin_set_sstep (thread_t thread, int enable)
521 {
522   x86_thread_state_t regs;
523   unsigned int count = x86_THREAD_STATE_COUNT;
524   kern_return_t kret;
525
526   kret = thread_get_state (thread, x86_THREAD_STATE,
527                            (thread_state_t) &regs, &count);
528   if (kret != KERN_SUCCESS)
529     {
530       printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
531                          kret, thread);
532       return;
533     }
534
535   switch (regs.tsh.flavor)
536     {
537     case x86_THREAD_STATE32:
538       {
539         __uint32_t bit = enable ? X86_EFLAGS_T : 0;
540         
541         if (enable && i386_darwin_sstep_at_sigreturn (&regs))
542           return;
543         if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
544           return;
545         regs.uts.ts32.__eflags = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
546         kret = thread_set_state (thread, x86_THREAD_STATE, 
547                                  (thread_state_t) &regs, count);
548         MACH_CHECK_ERROR (kret);
549       }
550       break;
551 #ifdef BFD64
552     case x86_THREAD_STATE64:
553       {
554         __uint64_t bit = enable ? X86_EFLAGS_T : 0;
555
556         if (enable && amd64_darwin_sstep_at_sigreturn (&regs))
557           return;
558         if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
559           return;
560         regs.uts.ts64.__rflags = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
561         kret = thread_set_state (thread, x86_THREAD_STATE, 
562                                  (thread_state_t) &regs, count);
563         MACH_CHECK_ERROR (kret);
564       }
565       break;
566 #endif
567     default:
568       error (_("darwin_set_sstep: unknown flavour: %d\n"), regs.tsh.flavor);
569     }
570 }
571
572 void
573 darwin_complete_target (struct target_ops *target)
574 {
575 #ifdef BFD64
576   amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
577   amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
578   amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
579   amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
580 #endif
581
582   target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
583   target->to_store_registers = i386_darwin_store_inferior_registers;
584 }