coredump-elf.c revision 77eb20b3865e7b17c7695c7e7a526b52935f593e
1 2/*--------------------------------------------------------------------*/ 3/*--- Dumping core. coredump-elf.c ---*/ 4/*--------------------------------------------------------------------*/ 5 6/* 7 This file is part of Valgrind, a dynamic binary instrumentation 8 framework. 9 10 Copyright (C) 2000-2013 Julian Seward 11 jseward@acm.org 12 13 This program is free software; you can redistribute it and/or 14 modify it under the terms of the GNU General Public License as 15 published by the Free Software Foundation; either version 2 of the 16 License, or (at your option) any later version. 17 18 This program is distributed in the hope that it will be useful, but 19 WITHOUT ANY WARRANTY; without even the implied warranty of 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 General Public License for more details. 22 23 You should have received a copy of the GNU General Public License 24 along with this program; if not, write to the Free Software 25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 26 02111-1307, USA. 27 28 The GNU General Public License is contained in the file COPYING. 29*/ 30 31#if defined(VGO_linux) 32 33#include "pub_core_basics.h" 34#include "pub_core_vki.h" 35#include "pub_core_aspacehl.h" 36#include "pub_core_aspacemgr.h" 37#include "pub_core_libcbase.h" 38#include "pub_core_machine.h" 39#include "pub_core_coredump.h" 40#include "pub_core_libcprint.h" 41#include "pub_core_libcfile.h" // VG_(close) et al 42#include "pub_core_libcproc.h" // VG_(geteuid), VG_(getegid) 43#include "pub_core_libcassert.h" // VG_(exit), vg_assert 44#include "pub_core_mallocfree.h" // VG_(malloc), VG_(free) 45#include "pub_core_libcsetjmp.h" // to keep _threadstate.h happy 46#include "pub_core_threadstate.h" 47#include "pub_core_xarray.h" 48#include "pub_core_clientstate.h" 49#include "pub_core_options.h" 50 51/* 52 Dump core 53 54 Generate a standard ELF core file corresponding to the client state 55 at the time of a crash. 56 */ 57#include <elf.h> 58#ifndef NT_PRXFPREG 59#define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */ 60#endif /* NT_PRXFPREG */ 61 62#if VG_WORDSIZE == 8 63#define ESZ(x) Elf64_##x 64#elif VG_WORDSIZE == 4 65#define ESZ(x) Elf32_##x 66#else 67#error VG_WORDSIZE needs to ==4 or ==8 68#endif 69 70/* If true, then this Segment may be mentioned in the core */ 71static Bool may_dump(const NSegment *seg) 72{ 73 if (seg->kind == SkAnonC || 74 seg->kind == SkShmC || 75 (seg->kind == SkFileC && 76 !VKI_S_ISCHR(seg->mode) && !VKI_S_ISBLK(seg->mode))) 77 return True; 78 79 return False; 80} 81 82/* If true, then this Segment's contents will be in the core */ 83static Bool should_dump(const NSegment *seg) 84{ 85 return may_dump(seg); // && seg->hasW; 86} 87 88static void fill_ehdr(ESZ(Ehdr) *ehdr, Int num_phdrs) 89{ 90 VG_(memset)(ehdr, 0, sizeof(*ehdr)); 91 92 VG_(memcpy)(ehdr->e_ident, ELFMAG, SELFMAG); 93 ehdr->e_ident[EI_CLASS] = VG_ELF_CLASS; 94 ehdr->e_ident[EI_DATA] = VG_ELF_DATA2XXX; 95 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 96 97 ehdr->e_type = ET_CORE; 98 ehdr->e_machine = VG_ELF_MACHINE; 99 ehdr->e_version = EV_CURRENT; 100 ehdr->e_entry = 0; 101 ehdr->e_phoff = sizeof(ESZ(Ehdr)); 102 ehdr->e_shoff = 0; 103 ehdr->e_flags = 0; 104 ehdr->e_ehsize = sizeof(ESZ(Ehdr)); 105 ehdr->e_phentsize = sizeof(ESZ(Phdr)); 106 ehdr->e_phnum = num_phdrs; 107 ehdr->e_shentsize = 0; 108 ehdr->e_shnum = 0; 109 ehdr->e_shstrndx = 0; 110 111} 112 113static void fill_phdr(ESZ(Phdr) *phdr, const NSegment *seg, UInt off, Bool write) 114{ 115 SizeT len = seg->end - seg->start + 1; 116 117 write = write && should_dump(seg); 118 119 VG_(memset)(phdr, 0, sizeof(*phdr)); 120 121 phdr->p_type = PT_LOAD; 122 phdr->p_offset = off; 123 phdr->p_vaddr = seg->start; 124 phdr->p_paddr = 0; 125 phdr->p_filesz = write ? len : 0; 126 phdr->p_memsz = len; 127 phdr->p_flags = 0; 128 129 if (seg->hasR) 130 phdr->p_flags |= PF_R; 131 if (seg->hasW) 132 phdr->p_flags |= PF_W; 133 if (seg->hasX) 134 phdr->p_flags |= PF_X; 135 136 phdr->p_align = VKI_PAGE_SIZE; 137} 138 139#if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \ 140 || defined(VGPV_mips32_linux_android) 141/* Android's libc doesn't provide a definition for this. Hence: */ 142typedef 143 struct { 144 Elf32_Word n_namesz; 145 Elf32_Word n_descsz; 146 Elf32_Word n_type; 147 } 148 Elf32_Nhdr; 149#endif 150 151struct note { 152 struct note *next; 153 ESZ(Nhdr) note; 154 HChar name[0]; 155}; 156 157static UInt note_size(const struct note *n) 158{ 159 return sizeof(ESZ(Nhdr)) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4) 160 + VG_ROUNDUP(n->note.n_descsz, 4); 161} 162 163#if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android) \ 164 && !defined(VGPV_mips32_linux_android) 165static void add_note(struct note **list, const HChar *name, UInt type, 166 const void *data, UInt datasz) 167{ 168 Int namelen = VG_(strlen)(name)+1; 169 Int notelen = sizeof(struct note) + 170 VG_ROUNDUP(namelen, 4) + 171 VG_ROUNDUP(datasz, 4); 172 struct note *n = VG_(malloc)("coredump-elf.an.1", notelen); 173 174 VG_(memset)(n, 0, notelen); 175 176 n->next = *list; 177 *list = n; 178 179 n->note.n_type = type; 180 n->note.n_namesz = namelen; 181 n->note.n_descsz = datasz; 182 183 VG_(memcpy)(n->name, name, namelen); 184 VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz); 185} 186#endif /* !defined(VGPV_*_linux_android) */ 187 188static void write_note(Int fd, const struct note *n) 189{ 190 VG_(write)(fd, &n->note, note_size(n)); 191} 192 193static void fill_prpsinfo(const ThreadState *tst, 194 struct vki_elf_prpsinfo *prpsinfo) 195{ 196 static HChar name[VKI_PATH_MAX]; 197 198 VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo)); 199 200 switch(tst->status) { 201 case VgTs_Runnable: 202 case VgTs_Yielding: 203 prpsinfo->pr_sname = 'R'; 204 break; 205 206 case VgTs_WaitSys: 207 prpsinfo->pr_sname = 'S'; 208 break; 209 210 case VgTs_Zombie: 211 prpsinfo->pr_sname = 'Z'; 212 break; 213 214 case VgTs_Empty: 215 case VgTs_Init: 216 prpsinfo->pr_sname = '?'; 217 break; 218 } 219 220 prpsinfo->pr_uid = 0; 221 prpsinfo->pr_gid = 0; 222 223 if (VG_(resolve_filename)(VG_(cl_exec_fd), name, VKI_PATH_MAX)) { 224 HChar *n = name+VG_(strlen)(name)-1; 225 226 while (n > name && *n != '/') 227 n--; 228 if (n != name) 229 n++; 230 231 VG_(strncpy)(prpsinfo->pr_fname, n, sizeof(prpsinfo->pr_fname)); 232 } 233} 234 235static void fill_prstatus(const ThreadState *tst, 236 /*OUT*/struct vki_elf_prstatus *prs, 237 const vki_siginfo_t *si) 238{ 239 struct vki_user_regs_struct *regs; 240 const ThreadArchState* arch = &tst->arch; 241 242 VG_(memset)(prs, 0, sizeof(*prs)); 243 244 prs->pr_info.si_signo = si->si_signo; 245 prs->pr_info.si_code = si->si_code; 246 prs->pr_info.si_errno = 0; 247 248 prs->pr_cursig = si->si_signo; 249 250 prs->pr_pid = tst->os_state.lwpid; 251 prs->pr_ppid = 0; 252 prs->pr_pgrp = VG_(getpgrp)(); 253 prs->pr_sid = VG_(getpgrp)(); 254 255#if defined(VGP_s390x_linux) 256 /* prs->pr_reg has struct type. Need to take address. */ 257 regs = (struct vki_user_regs_struct *)&(prs->pr_reg); 258#else 259 regs = (struct vki_user_regs_struct *)prs->pr_reg; 260 vg_assert(sizeof(*regs) == sizeof(prs->pr_reg)); 261#endif 262 263#if defined(VGP_x86_linux) 264 regs->eflags = LibVEX_GuestX86_get_eflags( &arch->vex ); 265 regs->esp = arch->vex.guest_ESP; 266 regs->eip = arch->vex.guest_EIP; 267 268 regs->ebx = arch->vex.guest_EBX; 269 regs->ecx = arch->vex.guest_ECX; 270 regs->edx = arch->vex.guest_EDX; 271 regs->esi = arch->vex.guest_ESI; 272 regs->edi = arch->vex.guest_EDI; 273 regs->ebp = arch->vex.guest_EBP; 274 regs->eax = arch->vex.guest_EAX; 275 276 regs->cs = arch->vex.guest_CS; 277 regs->ds = arch->vex.guest_DS; 278 regs->ss = arch->vex.guest_SS; 279 regs->es = arch->vex.guest_ES; 280 regs->fs = arch->vex.guest_FS; 281 regs->gs = arch->vex.guest_GS; 282 283#elif defined(VGP_amd64_linux) 284 regs->eflags = LibVEX_GuestAMD64_get_rflags( &arch->vex ); 285 regs->rsp = arch->vex.guest_RSP; 286 regs->rip = arch->vex.guest_RIP; 287 288 regs->rbx = arch->vex.guest_RBX; 289 regs->rcx = arch->vex.guest_RCX; 290 regs->rdx = arch->vex.guest_RDX; 291 regs->rsi = arch->vex.guest_RSI; 292 regs->rdi = arch->vex.guest_RDI; 293 regs->rbp = arch->vex.guest_RBP; 294 regs->rax = arch->vex.guest_RAX; 295 regs->r8 = arch->vex.guest_R8; 296 regs->r9 = arch->vex.guest_R9; 297 regs->r10 = arch->vex.guest_R10; 298 regs->r11 = arch->vex.guest_R11; 299 regs->r12 = arch->vex.guest_R12; 300 regs->r13 = arch->vex.guest_R13; 301 regs->r14 = arch->vex.guest_R14; 302 regs->r15 = arch->vex.guest_R15; 303 304#elif defined(VGP_ppc32_linux) 305# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n 306 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 307 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 308 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 309 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 310# undef DO 311 312 regs->nip = arch->vex.guest_CIA; 313 regs->msr = 0xf032; /* pretty arbitrary */ 314 regs->orig_gpr3 = arch->vex.guest_GPR3; 315 regs->ctr = arch->vex.guest_CTR; 316 regs->link = arch->vex.guest_LR; 317 regs->xer = LibVEX_GuestPPC32_get_XER( &arch->vex ); 318 regs->ccr = LibVEX_GuestPPC32_get_CR( &arch->vex ); 319 regs->mq = 0; 320 regs->trap = 0; 321 regs->dar = 0; /* should be fault address? */ 322 regs->dsisr = 0; 323 regs->result = 0; 324 325#elif defined(VGP_ppc64be_linux) 326# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n 327 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 328 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 329 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 330 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 331# undef DO 332 333 regs->nip = arch->vex.guest_CIA; 334 regs->msr = 0xf032; /* pretty arbitrary */ 335 regs->orig_gpr3 = arch->vex.guest_GPR3; 336 regs->ctr = arch->vex.guest_CTR; 337 regs->link = arch->vex.guest_LR; 338 regs->xer = LibVEX_GuestPPC64_get_XER( &arch->vex ); 339 regs->ccr = LibVEX_GuestPPC64_get_CR( &arch->vex ); 340 /* regs->mq = 0; */ 341 regs->trap = 0; 342 regs->dar = 0; /* should be fault address? */ 343 regs->dsisr = 0; 344 regs->result = 0; 345 346#elif defined(VGP_ppc64le_linux) 347# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n 348 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 349 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 350 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 351 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 352# undef DO 353 354 regs->nip = arch->vex.guest_CIA; 355 regs->msr = 0xf033; /* pretty arbitrary */ 356 regs->orig_gpr3 = arch->vex.guest_GPR3; 357 regs->ctr = arch->vex.guest_CTR; 358 regs->link = arch->vex.guest_LR; 359 regs->xer = LibVEX_GuestPPC64_get_XER( &((ThreadArchState*)arch)->vex ); 360 regs->ccr = LibVEX_GuestPPC64_get_CR( &((ThreadArchState*)arch)->vex ); 361 /* regs->mq = 0; */ 362 regs->trap = 0; 363 regs->dar = 0; /* should be fault address? */ 364 regs->dsisr = 0; 365 regs->result = 0; 366 367#elif defined(VGP_arm_linux) 368 regs->ARM_r0 = arch->vex.guest_R0; 369 regs->ARM_r1 = arch->vex.guest_R1; 370 regs->ARM_r2 = arch->vex.guest_R2; 371 regs->ARM_r3 = arch->vex.guest_R3; 372 regs->ARM_r4 = arch->vex.guest_R4; 373 regs->ARM_r5 = arch->vex.guest_R5; 374 regs->ARM_r6 = arch->vex.guest_R6; 375 regs->ARM_r7 = arch->vex.guest_R7; 376 regs->ARM_r8 = arch->vex.guest_R8; 377 regs->ARM_r9 = arch->vex.guest_R9; 378 regs->ARM_r10 = arch->vex.guest_R10; 379 regs->ARM_fp = arch->vex.guest_R11; 380 regs->ARM_ip = arch->vex.guest_R12; 381 regs->ARM_sp = arch->vex.guest_R13; 382 regs->ARM_lr = arch->vex.guest_R14; 383 regs->ARM_pc = arch->vex.guest_R15T; 384 regs->ARM_cpsr = LibVEX_GuestARM_get_cpsr( &arch->vex ); 385 386#elif defined(VGP_arm64_linux) 387 (void)arch; 388 I_die_here; 389 390#elif defined(VGP_s390x_linux) 391# define DO(n) regs->gprs[n] = arch->vex.guest_r##n 392 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 393 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 394# undef DO 395# define DO(n) regs->acrs[n] = arch->vex.guest_a##n 396 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 397 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 398# undef DO 399 regs->orig_gpr2 = arch->vex.guest_r2; 400 401#elif defined(VGP_mips32_linux) 402# define DO(n) regs->MIPS_r##n = arch->vex.guest_r##n 403 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 404 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 405 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 406 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 407# undef DO 408 regs->MIPS_hi = arch->vex.guest_HI; 409 regs->MIPS_lo = arch->vex.guest_LO; 410 411#elif defined(VGP_mips64_linux) 412# define DO(n) regs->MIPS_r##n = arch->vex.guest_r##n 413 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 414 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 415 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 416 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 417# undef DO 418 regs->MIPS_hi = arch->vex.guest_HI; 419 regs->MIPS_lo = arch->vex.guest_LO; 420 421#else 422# error Unknown ELF platform 423#endif 424} 425 426static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu) 427{ 428 __attribute__((unused)) 429 const ThreadArchState* arch = &tst->arch; 430 431#if defined(VGP_x86_linux) 432//:: static void fill_fpu(vki_elf_fpregset_t *fpu, const HChar *from) 433//:: { 434//:: if (VG_(have_ssestate)) { 435//:: UShort *to; 436//:: Int i; 437//:: 438//:: /* This is what the kernel does */ 439//:: VG_(memcpy)(fpu, from, 7*sizeof(long)); 440//:: 441//:: to = (UShort *)&fpu->st_space[0]; 442//:: from += 18 * sizeof(UShort); 443//:: 444//:: for (i = 0; i < 8; i++, to += 5, from += 8) 445//:: VG_(memcpy)(to, from, 5*sizeof(UShort)); 446//:: } else 447//:: VG_(memcpy)(fpu, from, sizeof(*fpu)); 448//:: } 449 450//:: fill_fpu(fpu, (const HChar *)&arch->m_sse); 451 452#elif defined(VGP_amd64_linux) 453//:: fpu->cwd = ?; 454//:: fpu->swd = ?; 455//:: fpu->twd = ?; 456//:: fpu->fop = ?; 457//:: fpu->rip = ?; 458//:: fpu->rdp = ?; 459//:: fpu->mxcsr = ?; 460//:: fpu->mxcsr_mask = ?; 461//:: fpu->st_space = ?; 462 463# define DO(n) VG_(memcpy)(fpu->xmm_space + n * 4, \ 464 &arch->vex.guest_YMM##n[0], 16) 465 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 466 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 467# undef DO 468 469 VG_(memset)(fpu->padding, 0, sizeof(fpu->padding)); 470 471#elif defined(VGP_ppc32_linux) 472 /* The guest state has the FPR fields declared as ULongs, so need 473 to fish out the values without converting them. 474 NOTE: The 32 FP registers map to the first 32 VSX registers.*/ 475# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n) 476 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 477 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 478 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 479 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 480# undef DO 481 482#elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux) 483 /* The guest state has the FPR fields declared as ULongs, so need 484 to fish out the values without converting them. 485 NOTE: The 32 FP registers map to the first 32 VSX registers.*/ 486# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n) 487 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 488 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 489 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 490 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 491# undef DO 492 493#elif defined(VGP_arm_linux) 494 // umm ... 495 496#elif defined(VGP_arm64_linux) 497 I_die_here; 498 499#elif defined(VGP_s390x_linux) 500# define DO(n) fpu->fprs[n].ui = arch->vex.guest_f##n 501 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 502 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 503# undef DO 504#elif defined(VGP_mips32_linux) 505# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_f##n) 506 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 507 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 508 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 509 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 510# undef DO 511#elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux) 512# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_f##n) 513 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 514 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 515 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 516 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 517# undef DO 518#else 519# error Unknown ELF platform 520#endif 521} 522 523#if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android) 524static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu) 525{ 526 const ThreadArchState* arch = &tst->arch; 527 528//:: xfpu->cwd = ?; 529//:: xfpu->swd = ?; 530//:: xfpu->twd = ?; 531//:: xfpu->fop = ?; 532//:: xfpu->fip = ?; 533//:: xfpu->fcs = ?; 534//:: xfpu->foo = ?; 535//:: xfpu->fos = ?; 536//:: xfpu->mxcsr = ?; 537 xfpu->reserved = 0; 538//:: xfpu->st_space = ?; 539 540# define DO(n) VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n)) 541 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 542# undef DO 543 544 VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding)); 545} 546#endif 547 548static 549void dump_one_thread(struct note **notelist, const vki_siginfo_t *si, ThreadId tid) 550{ 551 vki_elf_fpregset_t fpu; 552 struct vki_elf_prstatus prstatus; 553# if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android) 554 { 555 vki_elf_fpxregset_t xfpu; 556 fill_xfpu(&VG_(threads)[tid], &xfpu); 557 add_note(notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu)); 558 } 559# endif 560 561 fill_fpu(&VG_(threads)[tid], &fpu); 562# if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android) \ 563 && !defined(VGPV_mips32_linux_android) 564 add_note(notelist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu)); 565# endif 566 567 fill_prstatus(&VG_(threads)[tid], &prstatus, si); 568# if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android) \ 569 && !defined(VGPV_mips32_linux_android) 570 add_note(notelist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus)); 571# endif 572} 573 574static 575void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, ULong max_size) 576{ 577 HChar* buf = NULL; 578 const HChar *basename = "vgcore"; 579 const HChar *coreext = ""; 580 Int seq = 0; 581 Int core_fd; 582 NSegment const * seg; 583 ESZ(Ehdr) ehdr; 584 ESZ(Phdr) *phdrs; 585 Int num_phdrs; 586 Int i, idx; 587 UInt off; 588 struct note *notelist, *note; 589 UInt notesz; 590 struct vki_elf_prpsinfo prpsinfo; 591 Addr *seg_starts; 592 Int n_seg_starts; 593 594 if (VG_(clo_log_fname_expanded) != NULL) { 595 coreext = ".core"; 596 basename = VG_(expand_file_name)( 597 "--log-file (while creating core filename)", 598 VG_(clo_log_fname_expanded)); 599 } 600 601 vg_assert(coreext); 602 vg_assert(basename); 603 buf = VG_(malloc)( "coredump-elf.mec.1", 604 VG_(strlen)(coreext) + VG_(strlen)(basename) 605 + 100/*for the two %ds. */ ); 606 vg_assert(buf); 607 608 for(;;) { 609 Int oflags = VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC; 610 SysRes sres; 611 612 if (seq == 0) 613 VG_(sprintf)(buf, "%s%s.%d", 614 basename, coreext, VG_(getpid)()); 615 else 616 VG_(sprintf)(buf, "%s%s.%d.%d", 617 basename, coreext, VG_(getpid)(), seq); 618 seq++; 619 620# if defined(VKI_O_LARGEFILE) 621 oflags |= VKI_O_LARGEFILE; 622# endif 623 624 sres = VG_(open)(buf, oflags, VKI_S_IRUSR|VKI_S_IWUSR); 625 if (!sr_isError(sres)) { 626 core_fd = sr_Res(sres); 627 break; 628 } 629 630 if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST) 631 return; /* can't create file */ 632 } 633 634 /* Get the segments */ 635 seg_starts = VG_(get_segment_starts)(&n_seg_starts); 636 637 /* First, count how many memory segments to dump */ 638 num_phdrs = 1; /* start with notes */ 639 for(i = 0; i < n_seg_starts; i++) { 640 if (!may_dump(VG_(am_find_nsegment(seg_starts[i])))) 641 continue; 642 643 num_phdrs++; 644 } 645 646 fill_ehdr(&ehdr, num_phdrs); 647 648 notelist = NULL; 649 650 /* Second, work out their layout */ 651 phdrs = VG_(malloc)("coredump-elf.mec.1", sizeof(*phdrs) * num_phdrs); 652 653 /* Add details for all threads except the one that faulted */ 654 for(i = 1; i < VG_N_THREADS; i++) { 655 656 if (VG_(threads)[i].status == VgTs_Empty) 657 continue; 658 659 if (i == tid) 660 continue; 661 662 dump_one_thread(¬elist, si, i); 663 } 664 665 /* Add details for the faulting thread. Note that because we are 666 adding to the head of a linked list this thread will actually 667 come out first in the core file, which seems to be how 668 debuggers determine that it is the faulting thread. */ 669 dump_one_thread(¬elist, si, tid); 670 671 fill_prpsinfo(&VG_(threads)[tid], &prpsinfo); 672# if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android) \ 673 && !defined(VGPV_mips32_linux_android) 674 add_note(¬elist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo)); 675# endif 676 677 for (note = notelist, notesz = 0; note != NULL; note = note->next) 678 notesz += note_size(note); 679 680 off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs; 681 682 phdrs[0].p_type = PT_NOTE; 683 phdrs[0].p_offset = off; 684 phdrs[0].p_vaddr = 0; 685 phdrs[0].p_paddr = 0; 686 phdrs[0].p_filesz = notesz; 687 phdrs[0].p_memsz = 0; 688 phdrs[0].p_flags = 0; 689 phdrs[0].p_align = 0; 690 691 off += notesz; 692 693 off = VG_PGROUNDUP(off); 694 695 for(i = 0, idx = 1; i < n_seg_starts; i++) { 696 seg = VG_(am_find_nsegment(seg_starts[i])); 697 698 if (!may_dump(seg)) 699 continue; 700 701 fill_phdr(&phdrs[idx], seg, off, 702 (seg->end - seg->start + 1 + off) < max_size); 703 704 off += phdrs[idx].p_filesz; 705 706 idx++; 707 } 708 709 /* write everything out */ 710 VG_(write)(core_fd, &ehdr, sizeof(ehdr)); 711 VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs); 712 713 for(note = notelist; note != NULL; note = note->next) 714 write_note(core_fd, note); 715 716 VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET); 717 718 for(i = 0, idx = 1; i < n_seg_starts; i++) { 719 seg = VG_(am_find_nsegment(seg_starts[i])); 720 721 if (!should_dump(seg)) 722 continue; 723 724 if (phdrs[idx].p_filesz > 0) { 725 vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET) 726 == phdrs[idx].p_offset); 727 vg_assert(seg->end - seg->start + 1 >= phdrs[idx].p_filesz); 728 729 (void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz); 730 } 731 idx++; 732 } 733 734 VG_(free)(seg_starts); 735 736 VG_(close)(core_fd); 737} 738 739void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, ULong max_size) 740{ 741 make_elf_coredump(tid, si, max_size); 742} 743 744#endif // defined(VGO_linux) 745 746/*--------------------------------------------------------------------*/ 747/*--- end ---*/ 748/*--------------------------------------------------------------------*/ 749