1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
|
/*--------------------------------------------------------------------*/
/*--- Dumping core. coredump-elf.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Valgrind, a dynamic binary instrumentation
framework.
Copyright (C) 2000-2017 Julian Seward
jseward@acm.org
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
The GNU General Public License is contained in the file COPYING.
*/
#if defined(VGO_linux) || defined(VGO_freebsd)
#include "pub_core_basics.h"
#include "pub_core_vki.h"
#include "pub_core_aspacehl.h"
#include "pub_core_aspacemgr.h"
#include "pub_core_libcbase.h"
#include "pub_core_machine.h"
#include "pub_core_coredump.h"
#include "pub_core_libcprint.h"
#include "pub_core_libcfile.h" // VG_(close) et al
#include "pub_core_libcproc.h" // VG_(geteuid), VG_(getegid)
#include "pub_core_libcassert.h" // VG_(exit), vg_assert
#include "pub_core_mallocfree.h" // VG_(malloc), VG_(free)
#include "pub_core_threadstate.h"
#include "pub_core_xarray.h"
#include "pub_core_clientstate.h"
#include "pub_core_options.h"
/*
Dump core
Generate a standard ELF core file corresponding to the client state
at the time of a crash.
*/
#include <elf.h>
#ifndef NT_PRXFPREG
#define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */
#endif /* NT_PRXFPREG */
#if VG_WORDSIZE == 8
#define ESZ(x) Elf64_##x
#elif VG_WORDSIZE == 4
#define ESZ(x) Elf32_##x
#else
#error VG_WORDSIZE needs to ==4 or ==8
#endif
/* If true, then this Segment may be mentioned in the core */
static Bool may_dump(const NSegment *seg)
{
if (seg->kind == SkAnonC ||
seg->kind == SkShmC ||
(seg->kind == SkFileC &&
!VKI_S_ISCHR(seg->mode) && !VKI_S_ISBLK(seg->mode)))
return True;
return False;
}
/* If true, then this Segment's contents will be in the core */
static Bool should_dump(const NSegment *seg)
{
return may_dump(seg); // && seg->hasW;
}
static void fill_ehdr(ESZ(Ehdr) *ehdr, Int num_phdrs)
{
VG_(memset)(ehdr, 0, sizeof(*ehdr));
VG_(memcpy)(ehdr->e_ident, ELFMAG, SELFMAG);
ehdr->e_ident[EI_CLASS] = VG_ELF_CLASS;
ehdr->e_ident[EI_DATA] = VG_ELF_DATA2XXX;
ehdr->e_ident[EI_VERSION] = EV_CURRENT;
#if defined(VGO_freebsd)
ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
#endif
ehdr->e_type = ET_CORE;
ehdr->e_machine = VG_ELF_MACHINE;
ehdr->e_version = EV_CURRENT;
ehdr->e_entry = 0;
ehdr->e_phoff = sizeof(ESZ(Ehdr));
ehdr->e_shoff = 0;
#if defined(VGP_nanomips_linux)
ehdr->e_flags = VKI_EF_NANOMIPS_ABI_P32;
#else
ehdr->e_flags = 0;
#endif
ehdr->e_ehsize = sizeof(ESZ(Ehdr));
ehdr->e_phentsize = sizeof(ESZ(Phdr));
ehdr->e_phnum = num_phdrs;
ehdr->e_shentsize = 0;
ehdr->e_shnum = 0;
ehdr->e_shstrndx = 0;
}
static void fill_phdr(ESZ(Phdr) *phdr, const NSegment *seg, UInt off, Bool write)
{
SizeT len = seg->end - seg->start + 1;
write = write && should_dump(seg);
VG_(memset)(phdr, 0, sizeof(*phdr));
phdr->p_type = PT_LOAD;
phdr->p_offset = off;
phdr->p_vaddr = seg->start;
phdr->p_paddr = 0;
phdr->p_filesz = write ? len : 0;
phdr->p_memsz = len;
phdr->p_flags = 0;
if (seg->hasR)
phdr->p_flags |= PF_R;
if (seg->hasW)
phdr->p_flags |= PF_W;
if (seg->hasX)
phdr->p_flags |= PF_X;
phdr->p_align = VKI_PAGE_SIZE;
}
#if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
|| defined(VGPV_mips32_linux_android)
/* Android's libc doesn't provide a definition for this. Hence: */
typedef
struct {
Elf32_Word n_namesz;
Elf32_Word n_descsz;
Elf32_Word n_type;
}
Elf32_Nhdr;
#endif
struct note {
struct note *next;
ESZ(Nhdr) note;
HChar name[0];
};
static UInt note_size(const struct note *n)
{
return sizeof(ESZ(Nhdr)) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4)
+ VG_ROUNDUP(n->note.n_descsz, 4);
}
#if !defined(VGPV_arm_linux_android) \
&& !defined(VGPV_x86_linux_android) \
&& !defined(VGPV_mips32_linux_android) \
&& !defined(VGPV_arm64_linux_android)
static void add_note(struct note **list, const HChar *name, UInt type,
const void *data, UInt datasz)
{
Int namelen = VG_(strlen)(name)+1;
Int notelen = sizeof(struct note) +
VG_ROUNDUP(namelen, 4) +
VG_ROUNDUP(datasz, 4);
struct note *n = VG_(malloc)("coredump-elf.an.1", notelen);
VG_(memset)(n, 0, notelen);
n->next = *list;
*list = n;
n->note.n_type = type;
n->note.n_namesz = namelen;
n->note.n_descsz = datasz;
VG_(memcpy)(n->name, name, namelen);
VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz);
}
#endif /* !defined(VGPV_*_linux_android) */
static void write_note(Int fd, const struct note *n)
{
VG_(write)(fd, &n->note, note_size(n));
}
#if defined(VGO_freebsd)
static void fill_prpsinfo(const ThreadState *tst,
struct vki_elf_prpsinfo *prpsinfo)
{
VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
prpsinfo->pr_version = VKI_PRPSINFO_VERSION;
prpsinfo->pr_psinfosz = sizeof(struct vki_elf_prpsinfo);
VG_(client_fname)(prpsinfo->pr_fname, sizeof(prpsinfo->pr_fname), False);
// why?
VG_(strncpy)(prpsinfo->pr_psargs, prpsinfo->pr_fname, sizeof(prpsinfo->pr_psargs) - 1);
}
#else
static void fill_prpsinfo(const ThreadState *tst,
struct vki_elf_prpsinfo *prpsinfo)
{
VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
switch(tst->status) {
case VgTs_Runnable:
case VgTs_Yielding:
prpsinfo->pr_sname = 'R';
break;
case VgTs_WaitSys:
prpsinfo->pr_sname = 'S';
break;
case VgTs_Zombie:
prpsinfo->pr_sname = 'Z';
break;
case VgTs_Empty:
case VgTs_Init:
prpsinfo->pr_sname = '?';
break;
}
prpsinfo->pr_uid = 0;
prpsinfo->pr_gid = 0;
VG_(client_fname)(prpsinfo->pr_fname, sizeof(prpsinfo->pr_fname), False);
}
#endif
static void fill_prstatus(const ThreadState *tst,
/*OUT*/struct vki_elf_prstatus *prs,
const vki_siginfo_t *si)
{
#if defined(VGP_mips32_linux) || defined(VGP_mips64_linux) \
|| defined(VGP_nanomips_linux)
vki_elf_greg_t *regs;
#else
struct vki_user_regs_struct *regs;
#endif
const ThreadArchState* arch = &tst->arch;
VG_(memset)(prs, 0, sizeof(*prs));
#if defined(VGO_freebsd)
prs->pr_version = VKI_PRSTATUS_VERSION;
prs->pr_statussz = sizeof(struct vki_elf_prstatus);
prs->pr_gregsetsz = sizeof(vki_elf_gregset_t);
prs->pr_fpregsetsz = sizeof(vki_elf_fpregset_t);
prs->pr_osreldate = VG_(getosreldate)();
prs->pr_cursig = si->si_signo;
prs->pr_pid = tst->os_state.lwpid;
#else
prs->pr_info.si_signo = si->si_signo;
prs->pr_info.si_code = si->si_code;
prs->pr_info.si_errno = 0;
prs->pr_cursig = si->si_signo;
prs->pr_pid = tst->os_state.lwpid;
prs->pr_ppid = 0;
prs->pr_pgrp = VG_(getpgrp)();
prs->pr_sid = VG_(getpgrp)();
#endif
#if defined(VGP_s390x_linux)
/* prs->pr_reg has struct type. Need to take address. */
regs = (struct vki_user_regs_struct *)&(prs->pr_reg);
#elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux) \
|| defined(VGP_nanomips_linux)
regs = (vki_elf_greg_t *)prs->pr_reg;
#else
regs = (struct vki_user_regs_struct *)prs->pr_reg;
vg_assert(sizeof(*regs) == sizeof(prs->pr_reg));
#endif
#if defined(VGP_x86_linux)
regs->eflags = LibVEX_GuestX86_get_eflags( &arch->vex );
regs->esp = arch->vex.guest_ESP;
regs->eip = arch->vex.guest_EIP;
regs->ebx = arch->vex.guest_EBX;
regs->ecx = arch->vex.guest_ECX;
regs->edx = arch->vex.guest_EDX;
regs->esi = arch->vex.guest_ESI;
regs->edi = arch->vex.guest_EDI;
regs->ebp = arch->vex.guest_EBP;
regs->eax = arch->vex.guest_EAX;
regs->cs = arch->vex.guest_CS;
regs->ds = arch->vex.guest_DS;
regs->ss = arch->vex.guest_SS;
regs->es = arch->vex.guest_ES;
regs->fs = arch->vex.guest_FS;
regs->gs = arch->vex.guest_GS;
#elif defined(VGP_amd64_linux)
regs->eflags = LibVEX_GuestAMD64_get_rflags( &arch->vex );
regs->rsp = arch->vex.guest_RSP;
regs->rip = arch->vex.guest_RIP;
regs->rbx = arch->vex.guest_RBX;
regs->rcx = arch->vex.guest_RCX;
regs->rdx = arch->vex.guest_RDX;
regs->rsi = arch->vex.guest_RSI;
regs->rdi = arch->vex.guest_RDI;
regs->rbp = arch->vex.guest_RBP;
regs->rax = arch->vex.guest_RAX;
regs->r8 = arch->vex.guest_R8;
regs->r9 = arch->vex.guest_R9;
regs->r10 = arch->vex.guest_R10;
regs->r11 = arch->vex.guest_R11;
regs->r12 = arch->vex.guest_R12;
regs->r13 = arch->vex.guest_R13;
regs->r14 = arch->vex.guest_R14;
regs->r15 = arch->vex.guest_R15;
#elif defined(VGP_ppc32_linux)
# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
# undef DO
regs->nip = arch->vex.guest_CIA;
regs->msr = 0xf032; /* pretty arbitrary */
regs->orig_gpr3 = arch->vex.guest_GPR3;
regs->ctr = arch->vex.guest_CTR;
regs->link = arch->vex.guest_LR;
regs->xer = LibVEX_GuestPPC32_get_XER( &arch->vex );
regs->ccr = LibVEX_GuestPPC32_get_CR( &arch->vex );
regs->mq = 0;
regs->trap = 0;
regs->dar = 0; /* should be fault address? */
regs->dsisr = 0;
regs->result = 0;
#elif defined(VGP_ppc64be_linux)
# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
# undef DO
regs->nip = arch->vex.guest_CIA;
regs->msr = 0xf032; /* pretty arbitrary */
regs->orig_gpr3 = arch->vex.guest_GPR3;
regs->ctr = arch->vex.guest_CTR;
regs->link = arch->vex.guest_LR;
regs->xer = LibVEX_GuestPPC64_get_XER( &arch->vex );
regs->ccr = LibVEX_GuestPPC64_get_CR( &arch->vex );
/* regs->mq = 0; */
regs->trap = 0;
regs->dar = 0; /* should be fault address? */
regs->dsisr = 0;
regs->result = 0;
#elif defined(VGP_ppc64le_linux)
# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
# undef DO
regs->nip = arch->vex.guest_CIA;
regs->msr = 0xf033; /* pretty arbitrary */
regs->orig_gpr3 = arch->vex.guest_GPR3;
regs->ctr = arch->vex.guest_CTR;
regs->link = arch->vex.guest_LR;
regs->xer = LibVEX_GuestPPC64_get_XER( &(arch->vex) );
regs->ccr = LibVEX_GuestPPC64_get_CR( &(arch->vex) );
/* regs->mq = 0; */
regs->trap = 0;
regs->dar = 0; /* should be fault address? */
regs->dsisr = 0;
regs->result = 0;
#elif defined(VGP_arm_linux)
regs->ARM_r0 = arch->vex.guest_R0;
regs->ARM_r1 = arch->vex.guest_R1;
regs->ARM_r2 = arch->vex.guest_R2;
regs->ARM_r3 = arch->vex.guest_R3;
regs->ARM_r4 = arch->vex.guest_R4;
regs->ARM_r5 = arch->vex.guest_R5;
regs->ARM_r6 = arch->vex.guest_R6;
regs->ARM_r7 = arch->vex.guest_R7;
regs->ARM_r8 = arch->vex.guest_R8;
regs->ARM_r9 = arch->vex.guest_R9;
regs->ARM_r10 = arch->vex.guest_R10;
regs->ARM_fp = arch->vex.guest_R11;
regs->ARM_ip = arch->vex.guest_R12;
regs->ARM_sp = arch->vex.guest_R13;
regs->ARM_lr = arch->vex.guest_R14;
regs->ARM_pc = arch->vex.guest_R15T;
regs->ARM_cpsr = LibVEX_GuestARM_get_cpsr( &arch->vex );
#elif defined(VGP_arm64_linux)
regs->regs[0] = arch->vex.guest_X0;
regs->regs[1] = arch->vex.guest_X1;
regs->regs[2] = arch->vex.guest_X2;
regs->regs[3] = arch->vex.guest_X3;
regs->regs[4] = arch->vex.guest_X4;
regs->regs[5] = arch->vex.guest_X5;
regs->regs[6] = arch->vex.guest_X6;
regs->regs[7] = arch->vex.guest_X7;
regs->regs[8] = arch->vex.guest_X8;
regs->regs[9] = arch->vex.guest_X9;
regs->regs[10] = arch->vex.guest_X10;
regs->regs[11] = arch->vex.guest_X11;
regs->regs[12] = arch->vex.guest_X12;
regs->regs[13] = arch->vex.guest_X13;
regs->regs[14] = arch->vex.guest_X14;
regs->regs[15] = arch->vex.guest_X15;
regs->regs[16] = arch->vex.guest_X16;
regs->regs[17] = arch->vex.guest_X17;
regs->regs[18] = arch->vex.guest_X18;
regs->regs[19] = arch->vex.guest_X19;
regs->regs[20] = arch->vex.guest_X20;
regs->regs[21] = arch->vex.guest_X21;
regs->regs[22] = arch->vex.guest_X22;
regs->regs[23] = arch->vex.guest_X23;
regs->regs[24] = arch->vex.guest_X24;
regs->regs[25] = arch->vex.guest_X25;
regs->regs[26] = arch->vex.guest_X26;
regs->regs[27] = arch->vex.guest_X27;
regs->regs[28] = arch->vex.guest_X28;
regs->regs[29] = arch->vex.guest_X29;
regs->regs[30] = arch->vex.guest_X30;
regs->sp = arch->vex.guest_XSP;
regs->pc = arch->vex.guest_PC;
regs->pstate = LibVEX_GuestARM64_get_nzcv( &arch->vex ); /* is this correct? */
#elif defined(VGP_s390x_linux)
# define DO(n) regs->gprs[n] = arch->vex.guest_r##n
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
# undef DO
# define DO(n) regs->acrs[n] = arch->vex.guest_a##n
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
# undef DO
regs->orig_gpr2 = arch->vex.guest_r2;
#elif defined(VGP_mips32_linux)
# define DO(n) regs[VKI_MIPS32_EF_R##n] = arch->vex.guest_r##n
DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); DO(8);
DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); DO(16);
DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); DO(24);
DO(25); DO(28); DO(29); DO(30); DO(31);
# undef DO
regs[VKI_MIPS32_EF_LO] = arch->vex.guest_LO;
regs[VKI_MIPS32_EF_HI] = arch->vex.guest_HI;
regs[VKI_MIPS32_EF_CP0_STATUS] = arch->vex.guest_CP0_status;
regs[VKI_MIPS32_EF_CP0_EPC] = arch->vex.guest_PC;
#elif defined(VGP_mips64_linux)
# define DO(n) regs[VKI_MIPS64_EF_R##n] = arch->vex.guest_r##n
DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); DO(8);
DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); DO(16);
DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); DO(24);
DO(25); DO(28); DO(29); DO(30); DO(31);
# undef DO
regs[VKI_MIPS64_EF_LO] = arch->vex.guest_LO;
regs[VKI_MIPS64_EF_HI] = arch->vex.guest_HI;
regs[VKI_MIPS64_EF_CP0_STATUS] = arch->vex.guest_CP0_status;
regs[VKI_MIPS64_EF_CP0_EPC] = arch->vex.guest_PC;
#elif defined(VGP_nanomips_linux)
# define DO(n) regs[VKI_MIPS32_EF_R##n] = arch->vex.guest_r##n
DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); DO(8);
DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); DO(16);
DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); DO(24);
DO(25); DO(28); DO(29); DO(30); DO(31);
regs[VKI_MIPS32_EF_CP0_STATUS] = arch->vex.guest_CP0_status;
regs[VKI_MIPS32_EF_CP0_EPC] = arch->vex.guest_PC;
# undef DO
#elif defined(VGP_amd64_freebsd)
regs->rflags = LibVEX_GuestAMD64_get_rflags( &arch->vex );
regs->rsp = arch->vex.guest_RSP;
regs->rip = arch->vex.guest_RIP;
regs->rbx = arch->vex.guest_RBX;
regs->rcx = arch->vex.guest_RCX;
regs->rdx = arch->vex.guest_RDX;
regs->rsi = arch->vex.guest_RSI;
regs->rdi = arch->vex.guest_RDI;
regs->rbp = arch->vex.guest_RBP;
regs->rax = arch->vex.guest_RAX;
regs->r8 = arch->vex.guest_R8;
regs->r9 = arch->vex.guest_R9;
regs->r10 = arch->vex.guest_R10;
regs->r11 = arch->vex.guest_R11;
regs->r12 = arch->vex.guest_R12;
regs->r13 = arch->vex.guest_R13;
regs->r14 = arch->vex.guest_R14;
regs->r15 = arch->vex.guest_R15;
#elif defined(VGP_x86_freebsd)
regs->eflags = LibVEX_GuestX86_get_eflags( &arch->vex );
regs->esp = arch->vex.guest_ESP;
regs->eip = arch->vex.guest_EIP;
regs->ebx = arch->vex.guest_EBX;
regs->ecx = arch->vex.guest_ECX;
regs->edx = arch->vex.guest_EDX;
regs->esi = arch->vex.guest_ESI;
regs->edi = arch->vex.guest_EDI;
regs->ebp = arch->vex.guest_EBP;
regs->eax = arch->vex.guest_EAX;
regs->cs = arch->vex.guest_CS;
regs->ds = arch->vex.guest_DS;
regs->ss = arch->vex.guest_SS;
regs->es = arch->vex.guest_ES;
regs->fs = arch->vex.guest_FS;
regs->gs = arch->vex.guest_GS;
#elif defined(VGP_arm64_freebsd)
regs->gp_x[0] = arch->vex.guest_X0;
regs->gp_x[1] = arch->vex.guest_X1;
regs->gp_x[2] = arch->vex.guest_X2;
regs->gp_x[3] = arch->vex.guest_X3;
regs->gp_x[4] = arch->vex.guest_X4;
regs->gp_x[5] = arch->vex.guest_X5;
regs->gp_x[6] = arch->vex.guest_X6;
regs->gp_x[7] = arch->vex.guest_X7;
regs->gp_x[8] = arch->vex.guest_X8;
regs->gp_x[9] = arch->vex.guest_X9;
regs->gp_x[10] = arch->vex.guest_X10;
regs->gp_x[11] = arch->vex.guest_X11;
regs->gp_x[12] = arch->vex.guest_X12;
regs->gp_x[13] = arch->vex.guest_X13;
regs->gp_x[14] = arch->vex.guest_X14;
regs->gp_x[15] = arch->vex.guest_X15;
regs->gp_x[16] = arch->vex.guest_X16;
regs->gp_x[17] = arch->vex.guest_X17;
regs->gp_x[18] = arch->vex.guest_X18;
regs->gp_x[19] = arch->vex.guest_X19;
regs->gp_x[20] = arch->vex.guest_X20;
regs->gp_x[21] = arch->vex.guest_X21;
regs->gp_x[22] = arch->vex.guest_X22;
regs->gp_x[23] = arch->vex.guest_X23;
regs->gp_x[24] = arch->vex.guest_X24;
regs->gp_x[25] = arch->vex.guest_X25;
regs->gp_x[26] = arch->vex.guest_X26;
regs->gp_x[27] = arch->vex.guest_X27;
regs->gp_x[28] = arch->vex.guest_X28;
regs->gp_x[29] = arch->vex.guest_X29;
regs->gp_lr = arch->vex.guest_X30;
regs->gp_sp = arch->vex.guest_XSP;
regs->gp_elr = arch->vex.guest_PC;
regs->gp_spsr = LibVEX_GuestARM64_get_nzcv( &arch->vex ); /* is this correct? */
#else
# error Unknown ELF platform
#endif
}
static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu)
{
__attribute__((unused))
const ThreadArchState* arch = &tst->arch;
#if defined(VGP_x86_linux)
//:: static void fill_fpu(vki_elf_fpregset_t *fpu, const HChar *from)
//:: {
//:: if (VG_(have_ssestate)) {
//:: UShort *to;
//:: Int i;
//::
//:: /* This is what the kernel does */
//:: VG_(memcpy)(fpu, from, 7*sizeof(long));
//::
//:: to = (UShort *)&fpu->st_space[0];
//:: from += 18 * sizeof(UShort);
//::
//:: for (i = 0; i < 8; i++, to += 5, from += 8)
//:: VG_(memcpy)(to, from, 5*sizeof(UShort));
//:: } else
//:: VG_(memcpy)(fpu, from, sizeof(*fpu));
//:: }
//:: fill_fpu(fpu, (const HChar *)&arch->m_sse);
#elif defined(VGP_amd64_linux)
//:: fpu->cwd = ?;
//:: fpu->swd = ?;
//:: fpu->twd = ?;
//:: fpu->fop = ?;
//:: fpu->rip = ?;
//:: fpu->rdp = ?;
//:: fpu->mxcsr = ?;
//:: fpu->mxcsr_mask = ?;
//:: fpu->st_space = ?;
# define DO(n) VG_(memcpy)(fpu->xmm_space + n * 4, \
&arch->vex.guest_YMM##n[0], 16)
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
# undef DO
VG_(memset)(fpu->padding, 0, sizeof(fpu->padding));
#elif defined(VGP_ppc32_linux)
/* The guest state has the FPR fields declared as ULongs, so need
to fish out the values without converting them.
NOTE: The 32 FP registers map to the first 32 VSX registers.*/
# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
# undef DO
#elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
/* The guest state has the FPR fields declared as ULongs, so need
to fish out the values without converting them.
NOTE: The 32 FP registers map to the first 32 VSX registers.*/
# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
# undef DO
#elif defined(VGP_arm_linux)
// umm ...
#elif defined(VGP_arm64_linux)
fpu->vregs[0] = *(const __uint128_t*)arch->vex.guest_Q0;
fpu->vregs[1] = *(const __uint128_t*)arch->vex.guest_Q1;
fpu->vregs[2] = *(const __uint128_t*)arch->vex.guest_Q2;
fpu->vregs[3] = *(const __uint128_t*)arch->vex.guest_Q3;
fpu->vregs[4] = *(const __uint128_t*)arch->vex.guest_Q4;
fpu->vregs[5] = *(const __uint128_t*)arch->vex.guest_Q5;
fpu->vregs[6] = *(const __uint128_t*)arch->vex.guest_Q6;
fpu->vregs[7] = *(const __uint128_t*)arch->vex.guest_Q7;
fpu->vregs[8] = *(const __uint128_t*)arch->vex.guest_Q8;
fpu->vregs[9] = *(const __uint128_t*)arch->vex.guest_Q9;
fpu->vregs[10] = *(const __uint128_t*)arch->vex.guest_Q10;
fpu->vregs[11] = *(const __uint128_t*)arch->vex.guest_Q11;
fpu->vregs[12] = *(const __uint128_t*)arch->vex.guest_Q12;
fpu->vregs[13] = *(const __uint128_t*)arch->vex.guest_Q13;
fpu->vregs[14] = *(const __uint128_t*)arch->vex.guest_Q14;
fpu->vregs[15] = *(const __uint128_t*)arch->vex.guest_Q15;
fpu->vregs[16] = *(const __uint128_t*)arch->vex.guest_Q16;
fpu->vregs[17] = *(const __uint128_t*)arch->vex.guest_Q17;
fpu->vregs[18] = *(const __uint128_t*)arch->vex.guest_Q18;
fpu->vregs[19] = *(const __uint128_t*)arch->vex.guest_Q19;
fpu->vregs[20] = *(const __uint128_t*)arch->vex.guest_Q20;
fpu->vregs[21] = *(const __uint128_t*)arch->vex.guest_Q21;
fpu->vregs[22] = *(const __uint128_t*)arch->vex.guest_Q22;
fpu->vregs[23] = *(const __uint128_t*)arch->vex.guest_Q23;
fpu->vregs[24] = *(const __uint128_t*)arch->vex.guest_Q24;
fpu->vregs[25] = *(const __uint128_t*)arch->vex.guest_Q25;
fpu->vregs[26] = *(const __uint128_t*)arch->vex.guest_Q26;
fpu->vregs[27] = *(const __uint128_t*)arch->vex.guest_Q27;
fpu->vregs[28] = *(const __uint128_t*)arch->vex.guest_Q28;
fpu->vregs[29] = *(const __uint128_t*)arch->vex.guest_Q29;
fpu->vregs[30] = *(const __uint128_t*)arch->vex.guest_Q30;
fpu->vregs[31] = *(const __uint128_t*)arch->vex.guest_Q31;
fpu->fpsr = *(const __vki_u32*)arch->vex.guest_QCFLAG;
fpu->fpcr = arch->vex.guest_FPCR;
#elif defined(VGP_s390x_linux)
/* NOTE: The 16 FP registers map to the first 16 VSX registers. */
# define DO(n) fpu->fprs[n].ui = *(const Double*)(&arch->vex.guest_v##n.w64[0])
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
# undef DO
#elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
# define DO(n) (*fpu)[n] = *(const double*)(&arch->vex.guest_f##n)
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
# undef DO
#elif defined(VGP_nanomips_linux)
#elif defined(VGP_x86_freebsd)
#elif defined(VGP_amd64_freebsd)
# define DO(n) VG_(memcpy)(fpu->xmm_space + n * 4, \
&arch->vex.guest_YMM##n[0], 16)
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
# undef DO
#elif defined(VGP_arm64_freebsd)
fpu->fp_q[0] = *(const __uint128_t*)arch->vex.guest_Q0;
fpu->fp_q[1] = *(const __uint128_t*)arch->vex.guest_Q1;
fpu->fp_q[2] = *(const __uint128_t*)arch->vex.guest_Q2;
fpu->fp_q[3] = *(const __uint128_t*)arch->vex.guest_Q3;
fpu->fp_q[4] = *(const __uint128_t*)arch->vex.guest_Q4;
fpu->fp_q[5] = *(const __uint128_t*)arch->vex.guest_Q5;
fpu->fp_q[6] = *(const __uint128_t*)arch->vex.guest_Q6;
fpu->fp_q[7] = *(const __uint128_t*)arch->vex.guest_Q7;
fpu->fp_q[8] = *(const __uint128_t*)arch->vex.guest_Q8;
fpu->fp_q[9] = *(const __uint128_t*)arch->vex.guest_Q9;
fpu->fp_q[10] = *(const __uint128_t*)arch->vex.guest_Q10;
fpu->fp_q[11] = *(const __uint128_t*)arch->vex.guest_Q11;
fpu->fp_q[12] = *(const __uint128_t*)arch->vex.guest_Q12;
fpu->fp_q[13] = *(const __uint128_t*)arch->vex.guest_Q13;
fpu->fp_q[14] = *(const __uint128_t*)arch->vex.guest_Q14;
fpu->fp_q[15] = *(const __uint128_t*)arch->vex.guest_Q15;
fpu->fp_q[16] = *(const __uint128_t*)arch->vex.guest_Q16;
fpu->fp_q[17] = *(const __uint128_t*)arch->vex.guest_Q17;
fpu->fp_q[18] = *(const __uint128_t*)arch->vex.guest_Q18;
fpu->fp_q[19] = *(const __uint128_t*)arch->vex.guest_Q19;
fpu->fp_q[20] = *(const __uint128_t*)arch->vex.guest_Q20;
fpu->fp_q[21] = *(const __uint128_t*)arch->vex.guest_Q21;
fpu->fp_q[22] = *(const __uint128_t*)arch->vex.guest_Q22;
fpu->fp_q[23] = *(const __uint128_t*)arch->vex.guest_Q23;
fpu->fp_q[24] = *(const __uint128_t*)arch->vex.guest_Q24;
fpu->fp_q[25] = *(const __uint128_t*)arch->vex.guest_Q25;
fpu->fp_q[26] = *(const __uint128_t*)arch->vex.guest_Q26;
fpu->fp_q[27] = *(const __uint128_t*)arch->vex.guest_Q27;
fpu->fp_q[28] = *(const __uint128_t*)arch->vex.guest_Q28;
fpu->fp_q[29] = *(const __uint128_t*)arch->vex.guest_Q29;
fpu->fp_q[30] = *(const __uint128_t*)arch->vex.guest_Q30;
fpu->fp_q[31] = *(const __uint128_t*)arch->vex.guest_Q31;
fpu->fp_sr = *(const vki_uint32_t*)arch->vex.guest_QCFLAG;
fpu->fp_cr = arch->vex.guest_FPCR;
#else
# error Unknown ELF platform
#endif
}
#if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android)
static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu)
{
const ThreadArchState* arch = &tst->arch;
//:: xfpu->cwd = ?;
//:: xfpu->swd = ?;
//:: xfpu->twd = ?;
//:: xfpu->fop = ?;
//:: xfpu->fip = ?;
//:: xfpu->fcs = ?;
//:: xfpu->foo = ?;
//:: xfpu->fos = ?;
//:: xfpu->mxcsr = ?;
xfpu->reserved = 0;
//:: xfpu->st_space = ?;
# define DO(n) VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
# undef DO
VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding));
}
#endif
static
void dump_one_thread(struct note **notelist, const vki_siginfo_t *si, ThreadId tid)
{
vki_elf_fpregset_t fpu;
struct vki_elf_prstatus prstatus;
const HChar* name;
# if !defined(VGO_freebsd)
name = "CORE";
# else
/* lldb on FreeBSD expects a prstatus with name "FreeBSD"
* see llvm::Error ProcessElfCore::parseFreeBSDNotes(llvm::ArrayRef<CoreNote> notes) in ProcessElfCore.cpp
* Otherwise it exits with "Could not find NT_PRSTATUS note in core file." */
name = "FreeBSD";
# endif
VG_(memset)(&fpu, 0, sizeof(fpu));
VG_(memset)(&prstatus, 0, sizeof(prstatus));
# if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android)
{
vki_elf_fpxregset_t xfpu;
VG_(memset)(&xfpu, 0, sizeof(xfpu));
fill_xfpu(&VG_(threads)[tid], &xfpu);
add_note(notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu));
}
# endif
fill_fpu(&VG_(threads)[tid], &fpu);
# if !defined(VGPV_arm_linux_android) \
&& !defined(VGPV_x86_linux_android) \
&& !defined(VGPV_mips32_linux_android) \
&& !defined(VGPV_arm64_linux_android) \
&& !defined(VGP_nanomips_linux)
add_note(notelist, name, NT_FPREGSET, &fpu, sizeof(fpu));
# endif
fill_prstatus(&VG_(threads)[tid], &prstatus, si);
# if !defined(VGPV_arm_linux_android) \
&& !defined(VGPV_x86_linux_android) \
&& !defined(VGPV_mips32_linux_android) \
&& !defined(VGPV_arm64_linux_android)
add_note(notelist, name, NT_PRSTATUS, &prstatus, sizeof(prstatus));
# endif
}
static
void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, ULong max_size)
{
HChar* buf = NULL;
HChar *basename;
const HChar *coreext = "";
Int seq = 0;
Int core_fd;
NSegment const * seg;
ESZ(Ehdr) ehdr;
ESZ(Phdr) *phdrs = NULL;
Int num_phdrs;
Int i, idx;
UInt off;
struct note *notelist, *note;
UInt notesz;
struct vki_elf_prpsinfo prpsinfo;
Addr *seg_starts = NULL;
Int n_seg_starts;
if (VG_(clo_log_fname_unexpanded) != NULL) {
coreext = ".core";
basename = VG_(expand_file_name)("--log-file",
VG_(clo_log_fname_unexpanded));
} else
basename = VG_(strdup)("coredump-elf.mec.1", "vgcore");
vg_assert(coreext);
vg_assert(basename);
buf = VG_(malloc)( "coredump-elf.mec.1",
VG_(strlen)(coreext) + VG_(strlen)(basename)
+ 100/*for the two %ds. */ );
for(;;) {
Int oflags = VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC;
SysRes sres;
if (seq == 0)
VG_(sprintf)(buf, "%s%s.%d",
basename, coreext, VG_(getpid)());
else
VG_(sprintf)(buf, "%s%s.%d.%d",
basename, coreext, VG_(getpid)(), seq);
seq++;
# if defined(VKI_O_LARGEFILE)
oflags |= VKI_O_LARGEFILE;
# endif
sres = VG_(open)(buf, oflags, VKI_S_IRUSR|VKI_S_IWUSR);
if (!sr_isError(sres)) {
core_fd = sr_Res(sres);
break;
}
if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST)
goto cleanup; /* can't create file */
}
/* Get the client segments */
seg_starts = VG_(get_segment_starts)(SkFileC | SkAnonC | SkShmC,
&n_seg_starts);
/* First, count how many memory segments to dump */
num_phdrs = 1; /* start with notes */
for(i = 0; i < n_seg_starts; i++) {
if (!may_dump(VG_(am_find_nsegment)(seg_starts[i])))
continue;
num_phdrs++;
}
fill_ehdr(&ehdr, num_phdrs);
notelist = NULL;
/* Second, work out their layout */
phdrs = VG_(malloc)("coredump-elf.mec.1", sizeof(*phdrs) * num_phdrs);
/* Add details for all threads except the one that faulted */
for(i = 1; i < VG_N_THREADS; i++) {
if (VG_(threads)[i].status == VgTs_Empty)
continue;
if (i == tid)
continue;
dump_one_thread(¬elist, si, i);
}
/* Add details for the faulting thread. Note that because we are
adding to the head of a linked list this thread will actually
come out first in the core file, which seems to be how
debuggers determine that it is the faulting thread. */
dump_one_thread(¬elist, si, tid);
fill_prpsinfo(&VG_(threads)[tid], &prpsinfo);
# if !defined(VGPV_arm_linux_android) \
&& !defined(VGPV_x86_linux_android) \
&& !defined(VGPV_mips32_linux_android) \
&& !defined(VGPV_arm64_linux_android)
add_note(¬elist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
# endif
for (note = notelist, notesz = 0; note != NULL; note = note->next)
notesz += note_size(note);
off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs;
phdrs[0].p_type = PT_NOTE;
phdrs[0].p_offset = off;
phdrs[0].p_vaddr = 0;
phdrs[0].p_paddr = 0;
phdrs[0].p_filesz = notesz;
phdrs[0].p_memsz = 0;
phdrs[0].p_flags = 0;
phdrs[0].p_align = 0;
off += notesz;
off = VG_PGROUNDUP(off);
for(i = 0, idx = 1; i < n_seg_starts; i++) {
seg = VG_(am_find_nsegment)(seg_starts[i]);
if (!may_dump(seg))
continue;
fill_phdr(&phdrs[idx], seg, off,
(seg->end - seg->start + 1 + off) < max_size);
off += phdrs[idx].p_filesz;
idx++;
}
/* write everything out */
VG_(write)(core_fd, &ehdr, sizeof(ehdr));
VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs);
for(note = notelist; note != NULL; note = note->next)
write_note(core_fd, note);
VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET);
for(i = 0, idx = 1; i < n_seg_starts; i++) {
seg = VG_(am_find_nsegment)(seg_starts[i]);
if (!should_dump(seg))
continue;
if (phdrs[idx].p_filesz > 0) {
vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET)
== phdrs[idx].p_offset);
vg_assert(seg->end - seg->start + 1 >= phdrs[idx].p_filesz);
(void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz);
}
idx++;
}
VG_(close)(core_fd);
cleanup:
VG_(free)(basename);
VG_(free)(buf);
VG_(free)(seg_starts);
VG_(free)(phdrs);
}
void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, ULong max_size)
{
make_elf_coredump(tid, si, max_size);
}
#endif // defined(VGO_linux)
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/
|