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