coredump-elf.c revision 42a0800226cca64a13b9c4f76de438cd9f8aca4f
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) \ 164 && !defined(VGPV_x86_linux_android) \ 165 && !defined(VGPV_mips32_linux_android) \ 166 && !defined(VGPV_arm64_linux_android) 167static void add_note(struct note **list, const HChar *name, UInt type, 168 const void *data, UInt datasz) 169{ 170 Int namelen = VG_(strlen)(name)+1; 171 Int notelen = sizeof(struct note) + 172 VG_ROUNDUP(namelen, 4) + 173 VG_ROUNDUP(datasz, 4); 174 struct note *n = VG_(malloc)("coredump-elf.an.1", notelen); 175 176 VG_(memset)(n, 0, notelen); 177 178 n->next = *list; 179 *list = n; 180 181 n->note.n_type = type; 182 n->note.n_namesz = namelen; 183 n->note.n_descsz = datasz; 184 185 VG_(memcpy)(n->name, name, namelen); 186 VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz); 187} 188#endif /* !defined(VGPV_*_linux_android) */ 189 190static void write_note(Int fd, const struct note *n) 191{ 192 VG_(write)(fd, &n->note, note_size(n)); 193} 194 195static void fill_prpsinfo(const ThreadState *tst, 196 struct vki_elf_prpsinfo *prpsinfo) 197{ 198 const HChar *name; 199 200 VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo)); 201 202 switch(tst->status) { 203 case VgTs_Runnable: 204 case VgTs_Yielding: 205 prpsinfo->pr_sname = 'R'; 206 break; 207 208 case VgTs_WaitSys: 209 prpsinfo->pr_sname = 'S'; 210 break; 211 212 case VgTs_Zombie: 213 prpsinfo->pr_sname = 'Z'; 214 break; 215 216 case VgTs_Empty: 217 case VgTs_Init: 218 prpsinfo->pr_sname = '?'; 219 break; 220 } 221 222 prpsinfo->pr_uid = 0; 223 prpsinfo->pr_gid = 0; 224 225 if (VG_(resolve_filename)(VG_(cl_exec_fd), &name)) { 226 const HChar *n = name + VG_(strlen)(name) - 1; 227 228 while (n > name && *n != '/') 229 n--; 230 if (n != name) 231 n++; 232 233 VG_(strncpy)(prpsinfo->pr_fname, n, sizeof(prpsinfo->pr_fname)); 234 } 235} 236 237static void fill_prstatus(const ThreadState *tst, 238 /*OUT*/struct vki_elf_prstatus *prs, 239 const vki_siginfo_t *si) 240{ 241 struct vki_user_regs_struct *regs; 242 const ThreadArchState* arch = &tst->arch; 243 244 VG_(memset)(prs, 0, sizeof(*prs)); 245 246 prs->pr_info.si_signo = si->si_signo; 247 prs->pr_info.si_code = si->si_code; 248 prs->pr_info.si_errno = 0; 249 250 prs->pr_cursig = si->si_signo; 251 252 prs->pr_pid = tst->os_state.lwpid; 253 prs->pr_ppid = 0; 254 prs->pr_pgrp = VG_(getpgrp)(); 255 prs->pr_sid = VG_(getpgrp)(); 256 257#if defined(VGP_s390x_linux) 258 /* prs->pr_reg has struct type. Need to take address. */ 259 regs = (struct vki_user_regs_struct *)&(prs->pr_reg); 260#else 261 regs = (struct vki_user_regs_struct *)prs->pr_reg; 262 vg_assert(sizeof(*regs) == sizeof(prs->pr_reg)); 263#endif 264 265#if defined(VGP_x86_linux) 266 regs->eflags = LibVEX_GuestX86_get_eflags( &arch->vex ); 267 regs->esp = arch->vex.guest_ESP; 268 regs->eip = arch->vex.guest_EIP; 269 270 regs->ebx = arch->vex.guest_EBX; 271 regs->ecx = arch->vex.guest_ECX; 272 regs->edx = arch->vex.guest_EDX; 273 regs->esi = arch->vex.guest_ESI; 274 regs->edi = arch->vex.guest_EDI; 275 regs->ebp = arch->vex.guest_EBP; 276 regs->eax = arch->vex.guest_EAX; 277 278 regs->cs = arch->vex.guest_CS; 279 regs->ds = arch->vex.guest_DS; 280 regs->ss = arch->vex.guest_SS; 281 regs->es = arch->vex.guest_ES; 282 regs->fs = arch->vex.guest_FS; 283 regs->gs = arch->vex.guest_GS; 284 285#elif defined(VGP_amd64_linux) 286 regs->eflags = LibVEX_GuestAMD64_get_rflags( &arch->vex ); 287 regs->rsp = arch->vex.guest_RSP; 288 regs->rip = arch->vex.guest_RIP; 289 290 regs->rbx = arch->vex.guest_RBX; 291 regs->rcx = arch->vex.guest_RCX; 292 regs->rdx = arch->vex.guest_RDX; 293 regs->rsi = arch->vex.guest_RSI; 294 regs->rdi = arch->vex.guest_RDI; 295 regs->rbp = arch->vex.guest_RBP; 296 regs->rax = arch->vex.guest_RAX; 297 regs->r8 = arch->vex.guest_R8; 298 regs->r9 = arch->vex.guest_R9; 299 regs->r10 = arch->vex.guest_R10; 300 regs->r11 = arch->vex.guest_R11; 301 regs->r12 = arch->vex.guest_R12; 302 regs->r13 = arch->vex.guest_R13; 303 regs->r14 = arch->vex.guest_R14; 304 regs->r15 = arch->vex.guest_R15; 305 306#elif defined(VGP_ppc32_linux) 307# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n 308 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 309 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 310 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 311 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 312# undef DO 313 314 regs->nip = arch->vex.guest_CIA; 315 regs->msr = 0xf032; /* pretty arbitrary */ 316 regs->orig_gpr3 = arch->vex.guest_GPR3; 317 regs->ctr = arch->vex.guest_CTR; 318 regs->link = arch->vex.guest_LR; 319 regs->xer = LibVEX_GuestPPC32_get_XER( &arch->vex ); 320 regs->ccr = LibVEX_GuestPPC32_get_CR( &arch->vex ); 321 regs->mq = 0; 322 regs->trap = 0; 323 regs->dar = 0; /* should be fault address? */ 324 regs->dsisr = 0; 325 regs->result = 0; 326 327#elif defined(VGP_ppc64be_linux) 328# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n 329 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 330 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 331 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 332 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 333# undef DO 334 335 regs->nip = arch->vex.guest_CIA; 336 regs->msr = 0xf032; /* pretty arbitrary */ 337 regs->orig_gpr3 = arch->vex.guest_GPR3; 338 regs->ctr = arch->vex.guest_CTR; 339 regs->link = arch->vex.guest_LR; 340 regs->xer = LibVEX_GuestPPC64_get_XER( &arch->vex ); 341 regs->ccr = LibVEX_GuestPPC64_get_CR( &arch->vex ); 342 /* regs->mq = 0; */ 343 regs->trap = 0; 344 regs->dar = 0; /* should be fault address? */ 345 regs->dsisr = 0; 346 regs->result = 0; 347 348#elif defined(VGP_ppc64le_linux) 349# define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n 350 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 351 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 352 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 353 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 354# undef DO 355 356 regs->nip = arch->vex.guest_CIA; 357 regs->msr = 0xf033; /* pretty arbitrary */ 358 regs->orig_gpr3 = arch->vex.guest_GPR3; 359 regs->ctr = arch->vex.guest_CTR; 360 regs->link = arch->vex.guest_LR; 361 regs->xer = LibVEX_GuestPPC64_get_XER( &(arch->vex) ); 362 regs->ccr = LibVEX_GuestPPC64_get_CR( &(arch->vex) ); 363 /* regs->mq = 0; */ 364 regs->trap = 0; 365 regs->dar = 0; /* should be fault address? */ 366 regs->dsisr = 0; 367 regs->result = 0; 368 369#elif defined(VGP_arm_linux) 370 regs->ARM_r0 = arch->vex.guest_R0; 371 regs->ARM_r1 = arch->vex.guest_R1; 372 regs->ARM_r2 = arch->vex.guest_R2; 373 regs->ARM_r3 = arch->vex.guest_R3; 374 regs->ARM_r4 = arch->vex.guest_R4; 375 regs->ARM_r5 = arch->vex.guest_R5; 376 regs->ARM_r6 = arch->vex.guest_R6; 377 regs->ARM_r7 = arch->vex.guest_R7; 378 regs->ARM_r8 = arch->vex.guest_R8; 379 regs->ARM_r9 = arch->vex.guest_R9; 380 regs->ARM_r10 = arch->vex.guest_R10; 381 regs->ARM_fp = arch->vex.guest_R11; 382 regs->ARM_ip = arch->vex.guest_R12; 383 regs->ARM_sp = arch->vex.guest_R13; 384 regs->ARM_lr = arch->vex.guest_R14; 385 regs->ARM_pc = arch->vex.guest_R15T; 386 regs->ARM_cpsr = LibVEX_GuestARM_get_cpsr( &arch->vex ); 387 388#elif defined(VGP_arm64_linux) 389 (void)arch; 390 I_die_here; 391 392#elif defined(VGP_s390x_linux) 393# define DO(n) regs->gprs[n] = arch->vex.guest_r##n 394 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 395 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 396# undef DO 397# define DO(n) regs->acrs[n] = arch->vex.guest_a##n 398 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 399 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 400# undef DO 401 regs->orig_gpr2 = arch->vex.guest_r2; 402 403#elif defined(VGP_mips32_linux) 404# define DO(n) regs->MIPS_r##n = arch->vex.guest_r##n 405 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 406 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 407 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 408 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 409# undef DO 410 regs->MIPS_hi = arch->vex.guest_HI; 411 regs->MIPS_lo = arch->vex.guest_LO; 412 413#elif defined(VGP_mips64_linux) 414# define DO(n) regs->MIPS_r##n = arch->vex.guest_r##n 415 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 416 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 417 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 418 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 419# undef DO 420 regs->MIPS_hi = arch->vex.guest_HI; 421 regs->MIPS_lo = arch->vex.guest_LO; 422#elif defined(VGP_tilegx_linux) 423# define DO(n) regs->regs[n] = arch->vex.guest_r##n 424 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 425 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 426 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 427 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 428 DO(32); DO(33); DO(34); DO(35); DO(36); DO(37); DO(38); DO(39); 429 DO(40); DO(41); DO(42); DO(43); DO(44); DO(45); DO(46); DO(47); 430 DO(48); DO(49); DO(50); DO(51); DO(52); DO(53); DO(54); DO(55); 431 regs->pc = arch->vex.guest_pc; 432 regs->orig_r0 = arch->vex.guest_r0; 433#else 434# error Unknown ELF platform 435#endif 436} 437 438static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu) 439{ 440 __attribute__((unused)) 441 const ThreadArchState* arch = &tst->arch; 442 443#if defined(VGP_x86_linux) 444//:: static void fill_fpu(vki_elf_fpregset_t *fpu, const HChar *from) 445//:: { 446//:: if (VG_(have_ssestate)) { 447//:: UShort *to; 448//:: Int i; 449//:: 450//:: /* This is what the kernel does */ 451//:: VG_(memcpy)(fpu, from, 7*sizeof(long)); 452//:: 453//:: to = (UShort *)&fpu->st_space[0]; 454//:: from += 18 * sizeof(UShort); 455//:: 456//:: for (i = 0; i < 8; i++, to += 5, from += 8) 457//:: VG_(memcpy)(to, from, 5*sizeof(UShort)); 458//:: } else 459//:: VG_(memcpy)(fpu, from, sizeof(*fpu)); 460//:: } 461 462//:: fill_fpu(fpu, (const HChar *)&arch->m_sse); 463 464#elif defined(VGP_amd64_linux) 465//:: fpu->cwd = ?; 466//:: fpu->swd = ?; 467//:: fpu->twd = ?; 468//:: fpu->fop = ?; 469//:: fpu->rip = ?; 470//:: fpu->rdp = ?; 471//:: fpu->mxcsr = ?; 472//:: fpu->mxcsr_mask = ?; 473//:: fpu->st_space = ?; 474 475# define DO(n) VG_(memcpy)(fpu->xmm_space + n * 4, \ 476 &arch->vex.guest_YMM##n[0], 16) 477 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 478 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 479# undef DO 480 481 VG_(memset)(fpu->padding, 0, sizeof(fpu->padding)); 482 483#elif defined(VGP_ppc32_linux) 484 /* The guest state has the FPR fields declared as ULongs, so need 485 to fish out the values without converting them. 486 NOTE: The 32 FP registers map to the first 32 VSX registers.*/ 487# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n) 488 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 489 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 490 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 491 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 492# undef DO 493 494#elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux) 495 /* The guest state has the FPR fields declared as ULongs, so need 496 to fish out the values without converting them. 497 NOTE: The 32 FP registers map to the first 32 VSX registers.*/ 498# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n) 499 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 500 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 501 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 502 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 503# undef DO 504 505#elif defined(VGP_arm_linux) || defined(VGP_tilegx_linux) 506 // umm ... 507 508#elif defined(VGP_arm64_linux) 509 I_die_here; 510 511#elif defined(VGP_s390x_linux) 512# define DO(n) fpu->fprs[n].ui = 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# undef DO 516#elif defined(VGP_mips32_linux) 517# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_f##n) 518 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 519 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 520 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 521 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 522# undef DO 523#elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux) 524# define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_f##n) 525 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 526 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 527 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 528 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 529# undef DO 530#else 531# error Unknown ELF platform 532#endif 533} 534 535#if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android) 536static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu) 537{ 538 const ThreadArchState* arch = &tst->arch; 539 540//:: xfpu->cwd = ?; 541//:: xfpu->swd = ?; 542//:: xfpu->twd = ?; 543//:: xfpu->fop = ?; 544//:: xfpu->fip = ?; 545//:: xfpu->fcs = ?; 546//:: xfpu->foo = ?; 547//:: xfpu->fos = ?; 548//:: xfpu->mxcsr = ?; 549 xfpu->reserved = 0; 550//:: xfpu->st_space = ?; 551 552# define DO(n) VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n)) 553 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 554# undef DO 555 556 VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding)); 557} 558#endif 559 560static 561void dump_one_thread(struct note **notelist, const vki_siginfo_t *si, ThreadId tid) 562{ 563 vki_elf_fpregset_t fpu; 564 struct vki_elf_prstatus prstatus; 565# if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android) 566 { 567 vki_elf_fpxregset_t xfpu; 568 fill_xfpu(&VG_(threads)[tid], &xfpu); 569 add_note(notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu)); 570 } 571# endif 572 573 fill_fpu(&VG_(threads)[tid], &fpu); 574# if !defined(VGPV_arm_linux_android) \ 575 && !defined(VGPV_x86_linux_android) \ 576 && !defined(VGPV_mips32_linux_android) \ 577 && !defined(VGPV_arm64_linux_android) 578 add_note(notelist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu)); 579# endif 580 581 fill_prstatus(&VG_(threads)[tid], &prstatus, si); 582# if !defined(VGPV_arm_linux_android) \ 583 && !defined(VGPV_x86_linux_android) \ 584 && !defined(VGPV_mips32_linux_android) \ 585 && !defined(VGPV_arm64_linux_android) 586 add_note(notelist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus)); 587# endif 588} 589 590static 591void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, ULong max_size) 592{ 593 HChar* buf = NULL; 594 const HChar *basename = "vgcore"; 595 const HChar *coreext = ""; 596 Int seq = 0; 597 Int core_fd; 598 NSegment const * seg; 599 ESZ(Ehdr) ehdr; 600 ESZ(Phdr) *phdrs; 601 Int num_phdrs; 602 Int i, idx; 603 UInt off; 604 struct note *notelist, *note; 605 UInt notesz; 606 struct vki_elf_prpsinfo prpsinfo; 607 Addr *seg_starts; 608 Int n_seg_starts; 609 610 if (VG_(clo_log_fname_expanded) != NULL) { 611 coreext = ".core"; 612 basename = VG_(expand_file_name)("--log-file", 613 VG_(clo_log_fname_expanded)); 614 } 615 616 vg_assert(coreext); 617 vg_assert(basename); 618 buf = VG_(malloc)( "coredump-elf.mec.1", 619 VG_(strlen)(coreext) + VG_(strlen)(basename) 620 + 100/*for the two %ds. */ ); 621 622 for(;;) { 623 Int oflags = VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC; 624 SysRes sres; 625 626 if (seq == 0) 627 VG_(sprintf)(buf, "%s%s.%d", 628 basename, coreext, VG_(getpid)()); 629 else 630 VG_(sprintf)(buf, "%s%s.%d.%d", 631 basename, coreext, VG_(getpid)(), seq); 632 seq++; 633 634# if defined(VKI_O_LARGEFILE) 635 oflags |= VKI_O_LARGEFILE; 636# endif 637 638 sres = VG_(open)(buf, oflags, VKI_S_IRUSR|VKI_S_IWUSR); 639 if (!sr_isError(sres)) { 640 core_fd = sr_Res(sres); 641 break; 642 } 643 644 if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST) 645 return; /* can't create file */ 646 } 647 648 /* Get the client segments */ 649 seg_starts = VG_(get_segment_starts)(SkFileC | SkAnonC | SkShmC, 650 &n_seg_starts); 651 652 /* First, count how many memory segments to dump */ 653 num_phdrs = 1; /* start with notes */ 654 for(i = 0; i < n_seg_starts; i++) { 655 if (!may_dump(VG_(am_find_nsegment(seg_starts[i])))) 656 continue; 657 658 num_phdrs++; 659 } 660 661 fill_ehdr(&ehdr, num_phdrs); 662 663 notelist = NULL; 664 665 /* Second, work out their layout */ 666 phdrs = VG_(malloc)("coredump-elf.mec.1", sizeof(*phdrs) * num_phdrs); 667 668 /* Add details for all threads except the one that faulted */ 669 for(i = 1; i < VG_N_THREADS; i++) { 670 671 if (VG_(threads)[i].status == VgTs_Empty) 672 continue; 673 674 if (i == tid) 675 continue; 676 677 dump_one_thread(¬elist, si, i); 678 } 679 680 /* Add details for the faulting thread. Note that because we are 681 adding to the head of a linked list this thread will actually 682 come out first in the core file, which seems to be how 683 debuggers determine that it is the faulting thread. */ 684 dump_one_thread(¬elist, si, tid); 685 686 fill_prpsinfo(&VG_(threads)[tid], &prpsinfo); 687# if !defined(VGPV_arm_linux_android) \ 688 && !defined(VGPV_x86_linux_android) \ 689 && !defined(VGPV_mips32_linux_android) \ 690 && !defined(VGPV_arm64_linux_android) 691 add_note(¬elist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo)); 692# endif 693 694 for (note = notelist, notesz = 0; note != NULL; note = note->next) 695 notesz += note_size(note); 696 697 off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs; 698 699 phdrs[0].p_type = PT_NOTE; 700 phdrs[0].p_offset = off; 701 phdrs[0].p_vaddr = 0; 702 phdrs[0].p_paddr = 0; 703 phdrs[0].p_filesz = notesz; 704 phdrs[0].p_memsz = 0; 705 phdrs[0].p_flags = 0; 706 phdrs[0].p_align = 0; 707 708 off += notesz; 709 710 off = VG_PGROUNDUP(off); 711 712 for(i = 0, idx = 1; i < n_seg_starts; i++) { 713 seg = VG_(am_find_nsegment(seg_starts[i])); 714 715 if (!may_dump(seg)) 716 continue; 717 718 fill_phdr(&phdrs[idx], seg, off, 719 (seg->end - seg->start + 1 + off) < max_size); 720 721 off += phdrs[idx].p_filesz; 722 723 idx++; 724 } 725 726 /* write everything out */ 727 VG_(write)(core_fd, &ehdr, sizeof(ehdr)); 728 VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs); 729 730 for(note = notelist; note != NULL; note = note->next) 731 write_note(core_fd, note); 732 733 VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET); 734 735 for(i = 0, idx = 1; i < n_seg_starts; i++) { 736 seg = VG_(am_find_nsegment(seg_starts[i])); 737 738 if (!should_dump(seg)) 739 continue; 740 741 if (phdrs[idx].p_filesz > 0) { 742 vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET) 743 == phdrs[idx].p_offset); 744 vg_assert(seg->end - seg->start + 1 >= phdrs[idx].p_filesz); 745 746 (void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz); 747 } 748 idx++; 749 } 750 751 VG_(free)(seg_starts); 752 753 VG_(close)(core_fd); 754} 755 756void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, ULong max_size) 757{ 758 make_elf_coredump(tid, si, max_size); 759} 760 761#endif // defined(VGO_linux) 762 763/*--------------------------------------------------------------------*/ 764/*--- end ---*/ 765/*--------------------------------------------------------------------*/ 766