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 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
|
/*--------------------------------------------------------------------*/
/*--- Dumping core on Solaris. coredump-solaris.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Valgrind, a dynamic binary instrumentation
framework.
Copyright (C) 2013-2017 Ivo Raisr
ivosh@ivosh.net
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_solaris)
#include "pub_core_basics.h"
#include "pub_core_vki.h"
#include "pub_core_aspacehl.h"
#include "pub_core_aspacemgr.h"
#include "pub_core_coredump.h"
#include "pub_core_debuglog.h"
#include "pub_core_libcassert.h"
#include "pub_core_libcbase.h"
#include "pub_core_libcfile.h"
#include "pub_core_libcprint.h"
#include "pub_core_libcproc.h"
#include "pub_core_machine.h"
#include "pub_core_mallocfree.h"
#include "pub_core_options.h"
#include "pub_core_syscall.h"
#include "pub_core_threadstate.h"
#include "pub_core_xarray.h"
#include "pub_core_clientstate.h"
typedef struct __attribute__ ((__packed__)) note {
struct note *next;
VKI_ESZ(Nhdr) nhdr;
HChar name[8];
HChar data[0];
} note_t;
static void add_note(note_t **list, UInt type, const void *data,
UInt datasz);
/* 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);
}
#if defined(SOLARIS_PRXREGSET_T)
static Bool should_dump_xregs(const ThreadState *tst)
{
#if defined(VGP_x86_solaris)
return False;
#elif defined(VGP_amd64_solaris)
const ThreadArchState *arch = (const ThreadArchState *) &tst->arch;
/* Dump 256-bit wide %ymm only when their upper half is non-zero. */
#define YMM_NON_ZERO(reg) \
((reg[4] != 0) || (reg[5] != 0) || (reg[6] != 0) || (reg[7] != 0))
if (YMM_NON_ZERO(arch->vex.guest_YMM0) ||
YMM_NON_ZERO(arch->vex.guest_YMM1) ||
YMM_NON_ZERO(arch->vex.guest_YMM2) ||
YMM_NON_ZERO(arch->vex.guest_YMM3) ||
YMM_NON_ZERO(arch->vex.guest_YMM4) ||
YMM_NON_ZERO(arch->vex.guest_YMM5) ||
YMM_NON_ZERO(arch->vex.guest_YMM6) ||
YMM_NON_ZERO(arch->vex.guest_YMM7) ||
YMM_NON_ZERO(arch->vex.guest_YMM9) ||
YMM_NON_ZERO(arch->vex.guest_YMM0) ||
YMM_NON_ZERO(arch->vex.guest_YMM10) ||
YMM_NON_ZERO(arch->vex.guest_YMM11) ||
YMM_NON_ZERO(arch->vex.guest_YMM12) ||
YMM_NON_ZERO(arch->vex.guest_YMM13) ||
YMM_NON_ZERO(arch->vex.guest_YMM14) ||
YMM_NON_ZERO(arch->vex.guest_YMM15))
return True;
return False;
#undef YMM_NON_ZERO
#else
# error Unknown ELF platform
#endif
}
#endif /* SOLARIS_PRXREGSET_T */
static void write_part(Int fd, const HChar *filename,
void *buf, SizeT buf_size, const HChar *part)
{
Int ret = VG_(write)(fd, buf, buf_size);
if (ret < 0) {
VG_(umsg)("Failed to write %s to coredump file %s, it may be "
"incomplete.\n", part, filename);
VG_(debugLog)(1, "coredump-solaris", "write_part: failed to write "
"%s to file %s. Buffer address=%p, length=%lu. "
"Error=%d.\n", part, filename, buf, buf_size, -ret);
}
}
/*====================================================================*/
/*=== Miscellaneous getters ===*/
/*====================================================================*/
static Int get_uid(void)
{
return sr_Res(VG_(do_syscall0)(SYS_getuid));
}
static Int get_gid(void)
{
return sr_Res(VG_(do_syscall0)(SYS_getgid));
}
static Int get_dmodel(void)
{
#if defined(VGP_x86_solaris)
return PR_MODEL_ILP32;
#elif defined(VGP_amd64_solaris)
return PR_MODEL_LP64;
#else
# error "Unknown platform"
#endif
}
static vki_zoneid_t get_zoneid(void)
{
SysRes sres = VG_(do_syscall2)(SYS_zone, VKI_ZONE_LOOKUP,
(UWord) NULL);
if (sr_isError(sres))
return 0;
return sr_Res(sres);
}
static UInt count_auxv(void)
{
UInt count = 1;
vki_auxv_t *auxv = (vki_auxv_t *) VG_(client_auxv);
while (auxv->a_type != VKI_AT_NULL) {
count += 1;
auxv++;
}
return count;
}
static Addr compute_stkbase(const ThreadState *tst)
{
return tst->client_stack_highest_byte + 1
- tst->client_stack_szB;
}
static Int get_wstat(const vki_siginfo_t *si)
{
return (si->si_signo & 0xff) | WCOREFLG;
}
/*====================================================================*/
/*=== Utility fillers ===*/
/*====================================================================*/
static void fill_platform(HChar *buf, UInt buf_size)
{
vg_assert(buf != NULL);
vg_assert(buf_size >= 1);
buf[0] = '\0';
VG_(do_syscall3)(SYS_systeminfo, VKI_SI_PLATFORM,
(UWord) buf, buf_size);
}
static void fill_zonename(HChar *buf, UInt buf_size)
{
vg_assert(buf != NULL);
vg_assert(buf_size >= 1);
buf[0] = '\0';
VG_(do_syscall5)(SYS_zone, VKI_ZONE_GETATTR, get_zoneid(),
VKI_ZONE_ATTR_NAME, (UWord) buf, buf_size);
}
static void fill_thread_state(const ThreadState *tst,
HChar *state, HChar *sname)
{
switch (tst->status) {
case VgTs_Runnable:
case VgTs_Yielding:
*state = VKI_SRUN;
*sname = 'R';
break;
case VgTs_WaitSys:
*state = VKI_SSLEEP;
*sname = 'S';
break;
case VgTs_Zombie:
*state = VKI_SZOMB;
*sname = 'Z';
break;
case VgTs_Empty:
case VgTs_Init:
*state = 0;
*sname = '?';
break;
}
}
static void fill_siginfo(const vki_siginfo_t *si, vki_siginfo_t *di,
Short *signo)
{
di->si_signo = si->si_signo;
di->si_code = si->si_code;
di->si_errno = 0;
di->si_addr = si->si_addr;
*signo = si->si_signo;
}
static void fill_argv(Int *argc, Addr *argv)
{
Addr *ptr = (Addr *) VG_(get_initial_client_SP)();
*argc = *ptr++;
*argv = (Addr) ptr;
}
static void fill_scheduling_class(HChar *buf, SizeT buf_size)
{
vg_assert(buf != NULL);
vg_assert(buf_size >= 1);
/* Valgrind currently schedules one thread at time which
resembles the default timeshare class. */
VG_(strncpy)(buf, "TS", buf_size);
}
static void fill_regset(vki_prgregset_t *regs, const ThreadState *tst)
{
const ThreadArchState *arch = (const ThreadArchState *) &tst->arch;
#if defined(VGP_x86_solaris)
(*regs)[VKI_EIP] = arch->vex.guest_EIP;
(*regs)[VKI_EAX] = arch->vex.guest_EAX;
(*regs)[VKI_EBX] = arch->vex.guest_EBX;
(*regs)[VKI_ECX] = arch->vex.guest_ECX;
(*regs)[VKI_EDX] = arch->vex.guest_EDX;
(*regs)[VKI_ESI] = arch->vex.guest_ESI;
(*regs)[VKI_EDI] = arch->vex.guest_EDI;
(*regs)[VKI_EBP] = arch->vex.guest_EBP;
(*regs)[VKI_UESP] = arch->vex.guest_ESP;
(*regs)[VKI_SS] = arch->vex.guest_SS;
(*regs)[VKI_CS] = arch->vex.guest_CS;
(*regs)[VKI_DS] = arch->vex.guest_DS;
(*regs)[VKI_ES] = arch->vex.guest_ES;
(*regs)[VKI_FS] = arch->vex.guest_FS;
(*regs)[VKI_GS] = arch->vex.guest_GS;
(*regs)[VKI_EFL] = LibVEX_GuestX86_get_eflags(&arch->vex);
#elif defined(VGP_amd64_solaris)
(*regs)[VKI_REG_RIP] = arch->vex.guest_RIP;
(*regs)[VKI_REG_RAX] = arch->vex.guest_RAX;
(*regs)[VKI_REG_RBX] = arch->vex.guest_RBX;
(*regs)[VKI_REG_RCX] = arch->vex.guest_RCX;
(*regs)[VKI_REG_RDX] = arch->vex.guest_RDX;
(*regs)[VKI_REG_RBP] = arch->vex.guest_RBP;
(*regs)[VKI_REG_RSI] = arch->vex.guest_RSI;
(*regs)[VKI_REG_RDI] = arch->vex.guest_RDI;
(*regs)[VKI_REG_R8] = arch->vex.guest_R8;
(*regs)[VKI_REG_R9] = arch->vex.guest_R9;
(*regs)[VKI_REG_R10] = arch->vex.guest_R10;
(*regs)[VKI_REG_R11] = arch->vex.guest_R11;
(*regs)[VKI_REG_R12] = arch->vex.guest_R12;
(*regs)[VKI_REG_R13] = arch->vex.guest_R13;
(*regs)[VKI_REG_R14] = arch->vex.guest_R14;
(*regs)[VKI_REG_R15] = arch->vex.guest_R15;
(*regs)[VKI_REG_RSP] = arch->vex.guest_RSP;
(*regs)[VKI_REG_CS] = VKI_UCS_SEL;
(*regs)[VKI_REG_DS] = 0;
(*regs)[VKI_REG_ES] = 0;
(*regs)[VKI_REG_FS] = 0;
(*regs)[VKI_REG_GS] = 0;
(*regs)[VKI_REG_SS] = VKI_UDS_SEL;
(*regs)[VKI_REG_FSBASE] = arch->vex.guest_FS_CONST;
(*regs)[VKI_REG_GSBASE] = 0;
(*regs)[VKI_REG_RFL] = LibVEX_GuestAMD64_get_rflags(&arch->vex);
#else
# error "Unknown platform"
#endif
}
static void fill_fpregset(vki_fpregset_t *fpu, const ThreadState *tst)
{
const ThreadArchState *arch = (const ThreadArchState *) &tst->arch;
#if defined(VGP_x86_solaris)
VG_(memset)(fpu, 0, sizeof(*fpu));
struct vki_fpchip_state *fs = &fpu->fp_reg_set.fpchip_state;
vg_assert(sizeof(fs->state) == 108);
LibVEX_GuestX86_get_x87(CONST_CAST(VexGuestX86State *, &arch->vex),
(UChar *) &fs->state);
/* SSE */
UInt mxcsr = LibVEX_GuestX86_get_mxcsr(CONST_CAST(VexGuestX86State *,
&arch->vex));
fs->mxcsr = mxcsr;
/* XMM registers */
#define COPY_OUT_XMM(dest, src) \
do { \
dest._l[0] = src[0]; \
dest._l[1] = src[1]; \
dest._l[2] = src[2]; \
dest._l[3] = src[3]; \
} while (0);
COPY_OUT_XMM(fs->xmm[0], arch->vex.guest_XMM0);
COPY_OUT_XMM(fs->xmm[1], arch->vex.guest_XMM1);
COPY_OUT_XMM(fs->xmm[2], arch->vex.guest_XMM2);
COPY_OUT_XMM(fs->xmm[3], arch->vex.guest_XMM3);
COPY_OUT_XMM(fs->xmm[4], arch->vex.guest_XMM4);
COPY_OUT_XMM(fs->xmm[5], arch->vex.guest_XMM5);
COPY_OUT_XMM(fs->xmm[6], arch->vex.guest_XMM6);
COPY_OUT_XMM(fs->xmm[7], arch->vex.guest_XMM7);
#undef COPY_OUT_XMM
#elif defined(VGP_amd64_solaris)
VG_(memset)(fpu, 0, sizeof(*fpu));
struct vki_fpchip_state *fs = &fpu->fp_reg_set.fpchip_state;
/* LibVEX_GuestAMD64_fxsave() requires at least 416 bytes. */
vg_assert(sizeof(*fs) >= 416);
LibVEX_GuestAMD64_fxsave(CONST_CAST(VexGuestAMD64State *, &arch->vex),
(Addr) fs);
#else
# error Unknown platform
#endif
}
/*====================================================================*/
/*=== Header fillers ===*/
/*====================================================================*/
static void fill_ehdr(VKI_ESZ(Ehdr) *ehdr, Int num_phdrs)
{
VG_(memset)(ehdr, 0, sizeof(*ehdr));
VG_(memcpy)(ehdr->e_ident, VKI_ELFMAG, VKI_SELFMAG);
ehdr->e_ident[VKI_EI_CLASS] = VG_ELF_CLASS;
ehdr->e_ident[VKI_EI_DATA] = VG_ELF_DATA2XXX;
ehdr->e_ident[VKI_EI_VERSION] = VKI_EV_CURRENT;
ehdr->e_type = VKI_ET_CORE;
ehdr->e_machine = VG_ELF_MACHINE;
ehdr->e_version = VKI_EV_CURRENT;
ehdr->e_entry = 0;
ehdr->e_flags = 0;
ehdr->e_ehsize = sizeof(VKI_ESZ(Ehdr));
ehdr->e_phoff = sizeof(VKI_ESZ(Ehdr));
ehdr->e_phentsize = sizeof(VKI_ESZ(Phdr));
/* If the count of program headers can't fit in the mere 16 bits
* shortsightedly allotted to them in the ELF header, we use the
* extended formats and put the real values in the section header
* at index 0.
*/
if (num_phdrs >= VKI_PN_XNUM) {
ehdr->e_phnum = VKI_PN_XNUM;
ehdr->e_shnum = 1;
ehdr->e_shoff = ehdr->e_phoff + ehdr->e_phentsize * num_phdrs;
ehdr->e_shentsize = sizeof(VKI_ESZ(Shdr));
} else {
ehdr->e_phnum = num_phdrs;
ehdr->e_shnum = 0;
ehdr->e_shoff = 0;
ehdr->e_shentsize = 0;
}
ehdr->e_shstrndx = 0;
}
static void fill_phdr(VKI_ESZ(Phdr) *phdr, const NSegment *seg, UInt off,
Bool really_write)
{
SizeT len = seg->end - seg->start + 1;
really_write = really_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 = really_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;
}
/* Fills the section header at index zero when num_phdrs >= PN_XNUM. */
static void fill_zero_shdr(VKI_ESZ(Shdr) *shdr, UInt num_phdrs)
{
vg_assert(num_phdrs >= VKI_PN_XNUM);
VG_(memset)(shdr, 0, sizeof(*shdr));
shdr->sh_name = 0; // STR_NONE
shdr->sh_info = num_phdrs;
}
static void fill_prpsinfo(vki_elf_prpsinfo_t *prpsinfo,
const ThreadState *tst,
const vki_siginfo_t *si)
{
VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
fill_thread_state(tst, &prpsinfo->pr_state, &prpsinfo->pr_sname);
prpsinfo->pr_uid = get_uid();
prpsinfo->pr_gid = get_gid();
prpsinfo->pr_pid = VG_(getpid)();
prpsinfo->pr_ppid = VG_(getppid)();
prpsinfo->pr_pgrp = VG_(getpgrp)();
prpsinfo->pr_sid = VG_(getpgrp)();
fill_scheduling_class(prpsinfo->pr_clname, sizeof(prpsinfo->pr_clname));
VG_(client_fname)(prpsinfo->pr_fname, sizeof(prpsinfo->pr_fname), True);
VG_(client_cmd_and_args)(prpsinfo->pr_psargs,
sizeof(prpsinfo->pr_psargs));
fill_argv(&prpsinfo->pr_argc, (Addr *) &prpsinfo->pr_argv);
prpsinfo->pr_envp = (char **) VG_(client_envp);
prpsinfo->pr_wstat = get_wstat(si);
prpsinfo->pr_euid = VG_(geteuid)();
prpsinfo->pr_egid = VG_(getegid)();
prpsinfo->pr_dmodel = get_dmodel();
}
static void fill_prstatus(vki_elf_prstatus_t *prs,
const ThreadState *tst,
const vki_siginfo_t *si)
{
VG_(memset)(prs, 0, sizeof(*prs));
prs->pr_flags = VKI_ELF_OLD_PR_PCINVAL;
fill_siginfo(si, &prs->pr_info, &prs->pr_cursig);
prs->pr_nlwp = VG_(count_living_threads)();
prs->pr_sighold = tst->sig_mask;
prs->pr_pid = VG_(getpid)();
prs->pr_ppid = VG_(getppid)();
prs->pr_pgrp = VG_(getpgrp)();
prs->pr_sid = VG_(getpgrp)();
fill_scheduling_class(prs->pr_clname, sizeof(prs->pr_clname));
prs->pr_who = tst->os_state.lwpid;
prs->pr_brkbase = (vki_caddr_t) VG_(brk_base);
prs->pr_brksize = VG_(brk_limit) - VG_(brk_base);
prs->pr_stkbase = (vki_caddr_t) compute_stkbase(tst);
prs->pr_stksize = tst->client_stack_szB;
fill_regset(&prs->pr_reg, tst);
}
static void fill_psinfo(vki_psinfo_t *psinfo, const ThreadState *tst,
const vki_siginfo_t *si)
{
VG_(memset)(psinfo, 0, sizeof(*psinfo));
psinfo->pr_nlwp = VG_(count_living_threads)();
psinfo->pr_uid = get_uid();
psinfo->pr_gid = get_gid();
psinfo->pr_pid = VG_(getpid)();
psinfo->pr_ppid = VG_(getppid)();
psinfo->pr_pgid = VG_(getpgrp)();
psinfo->pr_sid = VG_(getpgrp)();
psinfo->pr_euid = VG_(geteuid)();
psinfo->pr_egid = VG_(getegid)();
VG_(client_fname)(psinfo->pr_fname, sizeof(psinfo->pr_fname), True);
psinfo->pr_wstat = get_wstat(si);
VG_(client_cmd_and_args)(psinfo->pr_psargs,
sizeof(psinfo->pr_psargs));
fill_argv(&psinfo->pr_argc, (Addr *) &psinfo->pr_argv);
psinfo->pr_envp = (uintptr_t) VG_(client_envp);
psinfo->pr_dmodel = get_dmodel();
psinfo->pr_zoneid = get_zoneid();
psinfo->pr_lwp.pr_lwpid = tst->os_state.lwpid;
fill_thread_state(tst, &psinfo->pr_lwp.pr_state,
&psinfo->pr_lwp.pr_sname);
fill_scheduling_class(psinfo->pr_lwp.pr_clname,
sizeof(psinfo->pr_lwp.pr_clname));
}
static void fill_pstatus(vki_pstatus_t *pstatus,
const ThreadState *tst,
const vki_siginfo_t *si)
{
VG_(memset)(pstatus, 0, sizeof(*pstatus));
pstatus->pr_flags = VKI_PR_PCINVAL;
pstatus->pr_nlwp = VG_(count_living_threads)();
pstatus->pr_pid = VG_(getpid)();
pstatus->pr_ppid = VG_(getppid)();
pstatus->pr_pgid = VG_(getpgrp)();
pstatus->pr_sid = VG_(getpgrp)();
pstatus->pr_brkbase = (uintptr_t) VG_(brk_base);
pstatus->pr_brksize = VG_(brk_limit) - VG_(brk_base);
pstatus->pr_stkbase = (uintptr_t) compute_stkbase(tst);
pstatus->pr_stksize = tst->client_stack_szB;
pstatus->pr_dmodel = get_dmodel();
pstatus->pr_zoneid = get_zoneid();
pstatus->pr_lwp.pr_flags = VKI_PR_PCINVAL;
pstatus->pr_lwp.pr_lwpid = tst->os_state.lwpid;
fill_siginfo(si, &pstatus->pr_lwp.pr_info,
&pstatus->pr_lwp.pr_cursig);
pstatus->pr_lwp.pr_lwphold = tst->sig_mask;
fill_scheduling_class(pstatus->pr_lwp.pr_clname,
sizeof(pstatus->pr_lwp.pr_clname));
fill_regset(&pstatus->pr_lwp.pr_reg, tst);
fill_fpregset(&pstatus->pr_lwp.pr_fpreg, tst);
}
#if defined(SOLARIS_PRXREGSET_T)
static void fill_xregs(vki_prxregset_t *xregs, const ThreadState *tst)
{
const ThreadArchState *arch = (const ThreadArchState *) &tst->arch;
#if defined(VGP_x86_solaris)
VG_(memset)(xregs, 0, sizeof(*xregs));
xregs->pr_xsize = sizeof(xregs->pr_un.pr_xsave);
/* SSE */
UInt mxcsr = LibVEX_GuestX86_get_mxcsr(CONST_CAST(VexGuestX86State *,
&arch->vex));
xregs->pr_un.pr_xsave.pr_mxcsr = mxcsr;
/* XMM registers */
#define COPY_OUT_XMM(dest, src) \
do { \
dest._l[0] = src[0]; \
dest._l[1] = src[1]; \
dest._l[2] = src[2]; \
dest._l[3] = src[3]; \
} while (0);
COPY_OUT_XMM(xregs->pr_un.pr_xsave.pr_xmm[0], arch->vex.guest_XMM0);
COPY_OUT_XMM(xregs->pr_un.pr_xsave.pr_xmm[1], arch->vex.guest_XMM1);
COPY_OUT_XMM(xregs->pr_un.pr_xsave.pr_xmm[2], arch->vex.guest_XMM2);
COPY_OUT_XMM(xregs->pr_un.pr_xsave.pr_xmm[3], arch->vex.guest_XMM3);
COPY_OUT_XMM(xregs->pr_un.pr_xsave.pr_xmm[4], arch->vex.guest_XMM4);
COPY_OUT_XMM(xregs->pr_un.pr_xsave.pr_xmm[5], arch->vex.guest_XMM5);
COPY_OUT_XMM(xregs->pr_un.pr_xsave.pr_xmm[6], arch->vex.guest_XMM6);
COPY_OUT_XMM(xregs->pr_un.pr_xsave.pr_xmm[7], arch->vex.guest_XMM7);
#undef COPY_OUT_XMM
#elif defined(VGP_amd64_solaris)
VG_(memset)(xregs, 0, sizeof(*xregs));
xregs->pr_xsize = sizeof(xregs->pr_un.pr_xsave);
/* LibVEX_GuestAMD64_fxsave() requires at least 416 bytes. */
vg_assert(sizeof(xregs->pr_un.pr_xsave) >= 416);
LibVEX_GuestAMD64_fxsave(CONST_CAST(VexGuestAMD64State *, &arch->vex),
(Addr) &xregs->pr_un.pr_xsave);
#else
# error "Unknown platform"
#endif
}
#endif /* SOLARIS_PRXREGSET_T */
static void fill_utsname(struct vki_utsname *uts)
{
VG_(memset)(uts, 0, sizeof(*uts));
VG_(do_syscall3)(SYS_systeminfo, VKI_SI_SYSNAME,
(UWord) &uts->sysname, sizeof(uts->sysname));
VG_(do_syscall3)(SYS_systeminfo, VKI_SI_HOSTNAME,
(UWord) &uts->nodename, sizeof(uts->nodename));
VG_(do_syscall3)(SYS_systeminfo, VKI_SI_RELEASE,
(UWord) &uts->release, sizeof(uts->release));
VG_(do_syscall3)(SYS_systeminfo, VKI_SI_VERSION,
(UWord) &uts->version, sizeof(uts->version));
VG_(do_syscall3)(SYS_systeminfo, VKI_SI_MACHINE,
(UWord) &uts->machine, sizeof(uts->machine));
}
static vki_prcred_t *create_prcred(SizeT *size)
{
UInt group_list[VKI_NGROUPS_MAX];
Int ngroups = VG_(getgroups)(VKI_NGROUPS_MAX, group_list);
if (ngroups == -1)
ngroups = 0;
*size = sizeof(vki_prcred_t) + (ngroups - 1) * sizeof(gid_t);
vki_prcred_t *prcred = VG_(malloc)("coredump-elf.cp.1", *size);
VG_(memset)(prcred, 0, *size);
prcred->pr_euid = VG_(geteuid)();
prcred->pr_ruid = get_uid();
prcred->pr_suid = prcred->pr_euid;
prcred->pr_egid = VG_(getegid)();
prcred->pr_rgid = get_gid();
prcred->pr_sgid = prcred->pr_egid;
prcred->pr_ngroups = ngroups;
UInt i;
for (i = 0; i < ngroups; i++)
prcred->pr_groups[i] = group_list[i];
return prcred;
}
static void fill_core_content(vki_core_content_t *content)
{
*content = VKI_CC_CONTENT_STACK | VKI_CC_CONTENT_HEAP
| VKI_CC_CONTENT_SHANON | VKI_CC_CONTENT_TEXT
| VKI_CC_CONTENT_DATA | VKI_CC_CONTENT_RODATA
| VKI_CC_CONTENT_ANON | VKI_CC_CONTENT_SHM
| VKI_CC_CONTENT_ISM | VKI_CC_CONTENT_DISM;
}
static vki_prpriv_t *create_prpriv(SizeT *size)
{
Int fd = VG_(fd_open)("/proc/self/priv", O_RDONLY, 0);
if (fd < 0)
return NULL;
struct vg_stat stats;
if (VG_(fstat)(fd, &stats) != 0) {
VG_(close)(fd);
return NULL;
}
vki_prpriv_t *prpriv = VG_(malloc)("coredump-elf.cp.1", stats.size);
if (VG_(read)(fd, prpriv, stats.size) != stats.size) {
VG_(free)(prpriv);
VG_(close)(fd);
return NULL;
}
VG_(close)(fd);
*size = stats.size;
return prpriv;
}
static vki_priv_impl_info_t *create_priv_info(SizeT *size)
{
/* Size of the returned priv_impl_info_t is apriori unknown. */
vki_priv_impl_info_t first_cut[100];
SysRes sres = VG_(do_syscall5)(SYS_privsys, VKI_PRIVSYS_GETIMPLINFO,
0, 0, (UWord) first_cut,
sizeof(first_cut));
if (sr_isError(sres))
return NULL;
SizeT real_size = first_cut[0].priv_headersize
+ first_cut[0].priv_globalinfosize;
vki_priv_impl_info_t *priv_info = VG_(malloc)("coredump-elf.cpi.1",
real_size);
if (real_size <= sizeof(first_cut)) {
/* if the first_cut was large enough */
VG_(memcpy)(priv_info, first_cut, real_size);
} else {
/* otherwise repeat the syscall with buffer large enough */
sres = VG_(do_syscall5)(SYS_privsys, VKI_PRIVSYS_GETIMPLINFO,
0, 0, (UWord) priv_info, real_size);
if (sr_isError(sres)) {
VG_(free)(priv_info);
return NULL;
}
}
*size = real_size;
return priv_info;
}
static void fill_lwpsinfo(vki_lwpsinfo_t *lwp,
const ThreadState *tst)
{
VG_(memset)(lwp, 0, sizeof(*lwp));
lwp->pr_lwpid = tst->os_state.lwpid;
fill_thread_state(tst, &lwp->pr_state, &lwp->pr_sname);
fill_scheduling_class(lwp->pr_clname, sizeof(lwp->pr_clname));
}
static void fill_lwpstatus(vki_lwpstatus_t *lwp,
const ThreadState *tst,
const vki_siginfo_t *si)
{
VG_(memset)(lwp, 0, sizeof(*lwp));
lwp->pr_flags = VKI_PR_PCINVAL;
lwp->pr_lwpid = tst->os_state.lwpid;
fill_siginfo(si, &lwp->pr_info, &lwp->pr_cursig);
fill_scheduling_class(lwp->pr_clname, sizeof(lwp->pr_clname));
fill_regset(&lwp->pr_reg, tst);
fill_fpregset(&lwp->pr_fpreg, tst);
}
static void fill_old_note_for_thread(note_t **notes,
const ThreadState *tst,
const vki_siginfo_t *si)
{
vki_elf_prstatus_t prstatus;
fill_prstatus(&prstatus, tst, si);
add_note(notes, VKI_NT_PRSTATUS, &prstatus, sizeof(vki_elf_prstatus_t));
vki_fpregset_t fpu;
fill_fpregset(&fpu, tst);
add_note(notes, VKI_NT_PRFPREG, &fpu, sizeof(vki_fpregset_t));
#if defined(SOLARIS_PRXREGSET_T)
if (should_dump_xregs(tst)) {
vki_prxregset_t xregs;
fill_xregs(&xregs, tst);
add_note(notes, VKI_NT_PRXREG, &xregs, sizeof(vki_prxregset_t));
}
#endif /* SOLARIS_PRXREGSET_T */
}
static void fill_new_note_for_thread(note_t **notes,
const ThreadState *tst,
const vki_siginfo_t *si)
{
vki_lwpsinfo_t lwpsinfo;
fill_lwpsinfo(&lwpsinfo, tst);
add_note(notes, VKI_NT_LWPSINFO, &lwpsinfo, sizeof(vki_lwpsinfo_t));
vki_lwpstatus_t lwpstatus;
fill_lwpstatus(&lwpstatus, tst, si);
add_note(notes, VKI_NT_LWPSTATUS, &lwpstatus, sizeof(vki_lwpstatus_t));
#if defined(SOLARIS_PRXREGSET_T)
if (should_dump_xregs(tst)) {
vki_prxregset_t xregs;
fill_xregs(&xregs, tst);
add_note(notes, VKI_NT_PRXREG, &xregs, sizeof(vki_prxregset_t));
}
#endif /* SOLARIS_PRXREGSET_T */
}
/*====================================================================*/
/*=== Note utility functions ===*/
/*====================================================================*/
static void add_note(note_t **list, UInt type, const void *data,
UInt datasz)
{
UInt note_size = sizeof(note_t) + VG_ROUNDUP(datasz, 4);
note_t *n = VG_(malloc)("coredump-elf.an.1", note_size);
VG_(memset)(n, 0, note_size);
n->nhdr.n_type = type;
n->nhdr.n_namesz = 5;
n->nhdr.n_descsz = VG_ROUNDUP(datasz, 4);
VG_(memcpy)(n->name, "CORE", 4);
VG_(memcpy)(n->data, data, datasz);
if (*list == NULL) {
*list = n;
return;
}
note_t *tail = *list;
while (tail->next != NULL)
tail = tail->next;
tail->next = n;
}
static UInt note_size(const note_t *note)
{
return sizeof(note_t) - sizeof(note_t *) + note->nhdr.n_descsz;
}
static UInt notes_size(const note_t *list)
{
UInt size = 0;
const note_t *note;
for (note = list; note != NULL; note = note->next)
size += note_size(note);
return size;
}
static void fill_notes_phdr(VKI_ESZ(Phdr) *phdr, UInt offset,
UInt size_of_notes)
{
phdr->p_type = PT_NOTE;
phdr->p_offset = offset;
phdr->p_vaddr = 0;
phdr->p_paddr = 0;
phdr->p_filesz = size_of_notes;
phdr->p_memsz = 0;
phdr->p_flags = PF_R;
phdr->p_align = 0;
}
static void write_notes(Int fd, const HChar *filename,
const note_t *list)
{
const note_t *note;
for (note = list; note != NULL; note = note->next)
write_part(fd, filename, CONST_CAST(void *, ¬e->nhdr),
note_size(note), "notes");
}
static void free_notes(note_t *list)
{
while (list != NULL) {
note_t *next = list->next;
VG_(free)(list);
list = next;
}
}
/*====================================================================*/
/*=== Main coredump function ===*/
/*====================================================================*/
void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si,
ULong max_size)
{
const HChar *basename = "vgcore";
const HChar *coreext = "";
Int core_fd;
if (VG_(clo_log_fname_unexpanded) != NULL) {
coreext = ".core";
basename = VG_(expand_file_name)("--log-file",
VG_(clo_log_fname_unexpanded));
}
vg_assert(coreext != NULL);
vg_assert(basename != NULL);
UInt filename_size = VG_(strlen)(coreext) + VG_(strlen)(basename)
+ 100; /* for the two %d's */
HChar *filename = VG_(malloc)("coredump-elf.mc.1", filename_size);
/* Try to come with a non-existent coredump filename. */
UInt seq = 0;
for (;;) {
Int oflags = VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC;
if (seq == 0)
VG_(snprintf)(filename, filename_size, "%s%s.%d",
basename, coreext, VG_(getpid)());
else
VG_(snprintf)(filename, filename_size, "%s%s.%d.%u",
basename, coreext, VG_(getpid)(), seq);
seq++;
#ifdef VKI_O_LARGEFILE
oflags |= VKI_O_LARGEFILE;
#endif
SysRes sres = VG_(open)(filename, 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) {
VG_(umsg)("Cannot create coredump file %s (%lu)\n",
filename, sr_Err(sres));
VG_(free)(filename);
return;
}
}
/* Get the client segments. Free seg_starts after use. */
Int n_seg_starts;
Addr *seg_starts = VG_(get_segment_starts)(SkFileC | SkAnonC | SkShmC,
&n_seg_starts);
/* Count how many memory segments to dump. */
Int i;
UInt num_phdrs = 2; /* two CORE note sections */
for (i = 0; i < n_seg_starts; i++) {
if (!may_dump(VG_(am_find_nsegment)(seg_starts[i])))
continue;
num_phdrs++;
}
VKI_ESZ(Ehdr) ehdr;
fill_ehdr(&ehdr, num_phdrs);
VKI_ESZ(Shdr) shdr;
if (ehdr.e_shnum > 0)
fill_zero_shdr(&shdr, num_phdrs);
UInt phdrs_size = num_phdrs * ehdr.e_phentsize;
/* Construct the old-style notes. */
note_t *old_notes = NULL;
vki_elf_prpsinfo_t prpsinfo;
fill_prpsinfo(&prpsinfo, &VG_(threads)[tid], si);
add_note(&old_notes, VKI_NT_PRPSINFO, &prpsinfo,
sizeof(vki_elf_prpsinfo_t));
HChar platform[256 + 1];
fill_platform(platform, sizeof(platform));
add_note(&old_notes, VKI_NT_PLATFORM, platform,
VG_(strlen)(platform) + 1);
add_note(&old_notes, VKI_NT_AUXV, VG_(client_auxv),
count_auxv() * sizeof(auxv_t));
/* Add detail about the faulting thread as the first note.
This is how gdb determines which thread faulted. Note that
mdb does not need such aid. */
fill_old_note_for_thread(&old_notes, &VG_(threads)[tid], si);
/* Now add details for all threads except the one that faulted. */
ThreadId t_idx;
for (t_idx = 1; t_idx < VG_N_THREADS; t_idx++)
if ((VG_(threads)[t_idx].status != VgTs_Empty) &&
(VG_(threads)[t_idx].status != VgTs_Zombie)) {
if (t_idx == tid)
continue;
fill_old_note_for_thread(&old_notes, &VG_(threads)[t_idx], si);
}
/* Construct the new-style notes. */
note_t *new_notes = NULL;
vki_psinfo_t psinfo;
fill_psinfo(&psinfo, &VG_(threads)[tid], si);
add_note(&new_notes, VKI_NT_PSINFO, &psinfo, sizeof(vki_psinfo_t));
vki_pstatus_t pstatus;
fill_pstatus(&pstatus, &VG_(threads)[tid], si);
add_note(&new_notes, VKI_NT_PSTATUS, &pstatus, sizeof(vki_pstatus_t));
add_note(&new_notes, VKI_NT_PLATFORM, platform,
VG_(strlen)(platform) + 1);
add_note(&new_notes, VKI_NT_AUXV, VG_(client_auxv),
count_auxv() * sizeof(auxv_t));
struct vki_utsname uts;
fill_utsname(&uts);
add_note(&new_notes, VKI_NT_UTSNAME, &uts,
sizeof(struct vki_utsname));
SizeT prcred_size;
vki_prcred_t *prcred = create_prcred(&prcred_size);
if (prcred != NULL) {
add_note(&new_notes, VKI_NT_PRCRED, prcred, prcred_size);
VG_(free)(prcred);
}
vki_core_content_t core_content;
fill_core_content(&core_content);
add_note(&new_notes, VKI_NT_CONTENT, &core_content,
sizeof(vki_core_content_t));
SizeT priv_size;
vki_prpriv_t *prpriv = create_prpriv(&priv_size);
if (prpriv != NULL) {
add_note(&new_notes, VKI_NT_PRPRIV, prpriv, priv_size);
VG_(free)(prpriv);
}
vki_priv_impl_info_t *priv_info = create_priv_info(&priv_size);
if (priv_info != NULL) {
add_note(&new_notes, VKI_NT_PRPRIVINFO, priv_info, priv_size);
VG_(free)(priv_info);
}
HChar zonename[VKI_ZONENAME_MAX + 1];
fill_zonename(zonename, sizeof(zonename));
add_note(&new_notes, VKI_NT_ZONENAME, zonename,
VG_(strlen)(zonename) + 1);
/* Add detail about the faulting thread as the first note.
This is how gdb determines which thread faulted. Note that
mdb does not need such aid. */
fill_new_note_for_thread(&new_notes, &VG_(threads)[tid], si);
/* Now add details for all threads except the one that faulted. */
for (t_idx = 1; t_idx < VG_N_THREADS; t_idx++) {
if ((VG_(threads)[t_idx].status != VgTs_Empty) &&
(VG_(threads)[t_idx].status != VgTs_Zombie)) {
if (t_idx == tid)
continue;
fill_new_note_for_thread(&new_notes, &VG_(threads)[t_idx], si);
}
}
VKI_ESZ(Phdr) *phdrs = VG_(malloc)("coredump-elf.mc.2", phdrs_size);
UInt size_of_notes = notes_size(old_notes);
UInt offset = ehdr.e_ehsize + phdrs_size +
(ehdr.e_shnum * ehdr.e_shentsize);
/* fill program header for old notes */
fill_notes_phdr(&phdrs[0], offset, size_of_notes);
offset += size_of_notes;
size_of_notes = notes_size(new_notes);
/* fill program header for new notes */
fill_notes_phdr(&phdrs[1], offset, size_of_notes);
offset += size_of_notes;
/* fill program headers for segments */
UInt idx;
for (i = 0, idx = 2; i < n_seg_starts; i++) {
NSegment const *seg = VG_(am_find_nsegment)(seg_starts[i]);
if (!may_dump(seg))
continue;
fill_phdr(&phdrs[idx], seg, offset,
(seg->end - seg->start + 1 + offset) < max_size);
offset += phdrs[idx].p_filesz;
idx++;
}
/* write everything out */
write_part(core_fd, filename, &ehdr, sizeof(ehdr),
"elf headers");
write_part(core_fd, filename, phdrs, phdrs_size,
"program headers");
if (ehdr.e_shnum > 0)
write_part(core_fd, filename, &shdr, sizeof(shdr),
"section headers");
write_notes(core_fd, filename, old_notes);
write_notes(core_fd, filename, new_notes);
VG_(lseek)(core_fd, phdrs[2].p_offset, VKI_SEEK_SET);
for (i = 0, idx = 2; i < n_seg_starts; i++) {
NSegment const *seg = VG_(am_find_nsegment)(seg_starts[i]);
if (!should_dump(seg))
continue;
if (phdrs[idx].p_filesz > 0) {
Off64T off = VG_(lseek)(core_fd, phdrs[idx].p_offset,
VKI_SEEK_SET);
vg_assert(off == phdrs[idx].p_offset);
vg_assert(seg->end - seg->start + 1 >= phdrs[idx].p_filesz);
write_part(core_fd, filename, (void *) seg->start,
phdrs[idx].p_filesz, "program segment");
}
idx++;
}
VG_(close)(core_fd);
VG_(free)(filename);
VG_(free)(phdrs);
free_notes(old_notes);
free_notes(new_notes);
VG_(free)(seg_starts);
}
#endif
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/
|