* defs.h (extract_signed_integer, extract_unsigned_integer,
[deliverable/binutils-gdb.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 "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 target_ops *ops,
47 struct regcache *regcache, int regno)
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
147 static void
148 i386_darwin_store_inferior_registers (struct target_ops *ops,
149 struct regcache *regcache, int regno)
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
266 static void
267 i386_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
329 static uint32_t
330 i386_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
376 void
377 i386_darwin_dr_set_control (unsigned long control)
378 {
379 i386_darwin_dr_set (DR_CONTROL, control);
380 }
381
382 void
383 i386_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
390 void
391 i386_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
398 unsigned long
399 i386_darwin_dr_get_status (void)
400 {
401 return i386_darwin_dr_get (DR_STATUS);
402 }
403
404 void
405 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
406 {
407 if (gdbarch_osabi (target_gdbarch) == GDB_OSABI_UNKNOWN)
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);
425 info.byte_order = gdbarch_byte_order (target_gdbarch);
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
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
446 static int
447 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
448 {
449 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
450 static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
451 gdb_byte buf[sizeof (darwin_syscall)];
452
453 /* Check if PC is at a sigreturn system call. */
454 if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
455 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
456 && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
457 {
458 ULONGEST uctx_addr;
459 ULONGEST mctx_addr;
460 ULONGEST flags_addr;
461 unsigned int eflags;
462
463 uctx_addr = read_memory_unsigned_integer
464 (regs->uts.ts32.__esp + 4, 4, byte_order);
465 mctx_addr = read_memory_unsigned_integer
466 (uctx_addr + 28, 4, byte_order);
467
468 flags_addr = mctx_addr + 12 + 9 * 4;
469 read_memory (flags_addr, (gdb_byte *) &eflags, 4);
470 eflags |= X86_EFLAGS_T;
471 write_memory (flags_addr, (gdb_byte *) &eflags, 4);
472
473 return 1;
474 }
475 return 0;
476 }
477
478 static int
479 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
480 {
481 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
482 static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
483 gdb_byte buf[sizeof (darwin_syscall)];
484
485 /* Check if PC is at a sigreturn system call. */
486 if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
487 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
488 && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
489 {
490 ULONGEST mctx_addr;
491 ULONGEST flags_addr;
492 unsigned int rflags;
493
494 mctx_addr = read_memory_unsigned_integer
495 (regs->uts.ts64.__rdi + 48, 8, byte_order);
496 flags_addr = mctx_addr + 16 + 17 * 8;
497
498 /* AMD64 is little endian. */
499 read_memory (flags_addr, (gdb_byte *) &rflags, 4);
500 rflags |= X86_EFLAGS_T;
501 write_memory (flags_addr, (gdb_byte *) &rflags, 4);
502
503 return 1;
504 }
505 return 0;
506 }
507
508 void
509 darwin_set_sstep (thread_t thread, int enable)
510 {
511 x86_thread_state_t regs;
512 unsigned int count = x86_THREAD_STATE_COUNT;
513 kern_return_t kret;
514
515 kret = thread_get_state (thread, x86_THREAD_STATE,
516 (thread_state_t) &regs, &count);
517 if (kret != KERN_SUCCESS)
518 {
519 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
520 kret, thread);
521 return;
522 }
523
524 switch (regs.tsh.flavor)
525 {
526 case x86_THREAD_STATE32:
527 {
528 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
529
530 if (enable && i386_darwin_sstep_at_sigreturn (&regs))
531 return;
532 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
533 return;
534 regs.uts.ts32.__eflags = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
535 kret = thread_set_state (thread, x86_THREAD_STATE,
536 (thread_state_t) &regs, count);
537 MACH_CHECK_ERROR (kret);
538 }
539 break;
540 case x86_THREAD_STATE64:
541 {
542 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
543
544 if (enable && amd64_darwin_sstep_at_sigreturn (&regs))
545 return;
546 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
547 return;
548 regs.uts.ts64.__rflags = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
549 kret = thread_set_state (thread, x86_THREAD_STATE,
550 (thread_state_t) &regs, count);
551 MACH_CHECK_ERROR (kret);
552 }
553 break;
554 default:
555 error (_("darwin_set_sstep: unknown flavour: %d\n"), regs.tsh.flavor);
556 }
557 }
558
559 void
560 darwin_complete_target (struct target_ops *target)
561 {
562 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
563 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
564 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
565 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
566
567 target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
568 target->to_store_registers = i386_darwin_store_inferior_registers;
569 }
This page took 0.041954 seconds and 5 git commands to generate.