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