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