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