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