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