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