2 /*--------------------------------------------------------------------*/
3 /*--- Dumping core. coredump-elf.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2010 Julian Seward
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.
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.
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
28 The GNU General Public License is contained in the file COPYING.
31 #if defined(VGO_linux)
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"
54 Generate a standard ELF core file corresponding to the client state
55 at the time of a crash.
59 #define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */
60 #endif /* NT_PRXFPREG */
63 #define ESZ(x) Elf64_##x
64 #elif VG_WORDSIZE == 4
65 #define ESZ(x) Elf32_##x
67 #error VG_WORDSIZE needs to ==4 or ==8
70 /* If true, then this Segment may be mentioned in the core */
71 static Bool may_dump(const NSegment *seg)
73 if (seg->kind == SkAnonC ||
74 seg->kind == SkShmC ||
75 (seg->kind == SkFileC &&
76 !VKI_S_ISCHR(seg->mode) && !VKI_S_ISBLK(seg->mode)))
82 /* If true, then this Segment's contents will be in the core */
83 static Bool should_dump(const NSegment *seg)
85 return may_dump(seg); // && seg->hasW;
88 static void fill_ehdr(ESZ(Ehdr) *ehdr, Int num_phdrs)
90 VG_(memset)(ehdr, 0, sizeof(*ehdr));
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;
97 ehdr->e_type = ET_CORE;
98 ehdr->e_machine = VG_ELF_MACHINE;
99 ehdr->e_version = EV_CURRENT;
101 ehdr->e_phoff = sizeof(ESZ(Ehdr));
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;
109 ehdr->e_shstrndx = 0;
113 static void fill_phdr(ESZ(Phdr) *phdr, const NSegment *seg, UInt off, Bool write)
115 SizeT len = seg->end - seg->start;
117 write = write && should_dump(seg);
119 VG_(memset)(phdr, 0, sizeof(*phdr));
121 phdr->p_type = PT_LOAD;
122 phdr->p_offset = off;
123 phdr->p_vaddr = seg->start;
125 phdr->p_filesz = write ? len : 0;
130 phdr->p_flags |= PF_R;
132 phdr->p_flags |= PF_W;
134 phdr->p_flags |= PF_X;
136 phdr->p_align = VKI_PAGE_SIZE;
145 static UInt note_size(const struct note *n)
147 return sizeof(ESZ(Nhdr)) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4) + VG_ROUNDUP(n->note.n_descsz, 4);
150 static void add_note(struct note **list, const Char *name, UInt type, const void *data, UInt datasz)
152 Int namelen = VG_(strlen)(name)+1;
153 Int notelen = sizeof(struct note) +
154 VG_ROUNDUP(namelen, 4) +
155 VG_ROUNDUP(datasz, 4);
156 struct note *n = VG_(arena_malloc)(VG_AR_CORE, "coredump-elf.an.1", notelen);
158 VG_(memset)(n, 0, notelen);
163 n->note.n_type = type;
164 n->note.n_namesz = namelen;
165 n->note.n_descsz = datasz;
167 VG_(memcpy)(n->name, name, namelen);
168 VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz);
171 static void write_note(Int fd, const struct note *n)
173 VG_(write)(fd, &n->note, note_size(n));
176 static void fill_prpsinfo(const ThreadState *tst, struct vki_elf_prpsinfo *prpsinfo)
178 static Char name[VKI_PATH_MAX];
180 VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
182 switch(tst->status) {
185 prpsinfo->pr_sname = 'R';
189 prpsinfo->pr_sname = 'S';
193 prpsinfo->pr_sname = 'Z';
198 prpsinfo->pr_sname = '?';
202 prpsinfo->pr_uid = 0;
203 prpsinfo->pr_gid = 0;
205 if (VG_(resolve_filename)(VG_(cl_exec_fd), name, VKI_PATH_MAX)) {
206 Char *n = name+VG_(strlen)(name)-1;
208 while (n > name && *n != '/')
213 VG_(strncpy)(prpsinfo->pr_fname, n, sizeof(prpsinfo->pr_fname));
217 static void fill_prstatus(const ThreadState *tst,
218 struct vki_elf_prstatus *prs,
219 const vki_siginfo_t *si)
221 struct vki_user_regs_struct *regs;
222 ThreadArchState* arch = (ThreadArchState*)&tst->arch;
224 VG_(memset)(prs, 0, sizeof(*prs));
226 prs->pr_info.si_signo = si->si_signo;
227 prs->pr_info.si_code = si->si_code;
228 prs->pr_info.si_errno = 0;
230 prs->pr_cursig = si->si_signo;
232 prs->pr_pid = tst->os_state.lwpid;
234 prs->pr_pgrp = VG_(getpgrp)();
235 prs->pr_sid = VG_(getpgrp)();
237 #ifdef VGP_s390x_linux
238 /* prs->pr_reg has struct type. Need to take address. */
239 regs = (struct vki_user_regs_struct *)&(prs->pr_reg);
241 regs = (struct vki_user_regs_struct *)prs->pr_reg;
243 vg_assert(sizeof(*regs) == sizeof(prs->pr_reg));
246 #if defined(VGP_x86_linux)
247 regs->eflags = LibVEX_GuestX86_get_eflags( &arch->vex );
248 regs->esp = arch->vex.guest_ESP;
249 regs->eip = arch->vex.guest_EIP;
251 regs->ebx = arch->vex.guest_EBX;
252 regs->ecx = arch->vex.guest_ECX;
253 regs->edx = arch->vex.guest_EDX;
254 regs->esi = arch->vex.guest_ESI;
255 regs->edi = arch->vex.guest_EDI;
256 regs->ebp = arch->vex.guest_EBP;
257 regs->eax = arch->vex.guest_EAX;
259 regs->cs = arch->vex.guest_CS;
260 regs->ds = arch->vex.guest_DS;
261 regs->ss = arch->vex.guest_SS;
262 regs->es = arch->vex.guest_ES;
263 regs->fs = arch->vex.guest_FS;
264 regs->gs = arch->vex.guest_GS;
266 #elif defined(VGP_amd64_linux)
267 regs->eflags = LibVEX_GuestAMD64_get_rflags( &((ThreadArchState*)arch)->vex );
268 regs->rsp = arch->vex.guest_RSP;
269 regs->rip = arch->vex.guest_RIP;
271 regs->rbx = arch->vex.guest_RBX;
272 regs->rcx = arch->vex.guest_RCX;
273 regs->rdx = arch->vex.guest_RDX;
274 regs->rsi = arch->vex.guest_RSI;
275 regs->rdi = arch->vex.guest_RDI;
276 regs->rbp = arch->vex.guest_RBP;
277 regs->rax = arch->vex.guest_RAX;
278 regs->r8 = arch->vex.guest_R8;
279 regs->r9 = arch->vex.guest_R9;
280 regs->r10 = arch->vex.guest_R10;
281 regs->r11 = arch->vex.guest_R11;
282 regs->r12 = arch->vex.guest_R12;
283 regs->r13 = arch->vex.guest_R13;
284 regs->r14 = arch->vex.guest_R14;
285 regs->r15 = arch->vex.guest_R15;
287 //:: regs->cs = arch->vex.guest_CS;
288 //:: regs->fs = arch->vex.guest_FS;
289 //:: regs->gs = arch->vex.guest_GS;
291 #elif defined(VGP_ppc32_linux)
292 # define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n
293 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
294 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
295 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
296 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
299 regs->nip = arch->vex.guest_CIA;
300 regs->msr = 0xf032; /* pretty arbitrary */
301 regs->orig_gpr3 = arch->vex.guest_GPR3;
302 regs->ctr = arch->vex.guest_CTR;
303 regs->link = arch->vex.guest_LR;
304 regs->xer = LibVEX_GuestPPC32_get_XER( &((ThreadArchState*)arch)->vex );
305 regs->ccr = LibVEX_GuestPPC32_get_CR( &((ThreadArchState*)arch)->vex );
308 regs->dar = 0; /* should be fault address? */
312 #elif defined(VGP_ppc64_linux)
313 # define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n
314 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
315 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
316 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
317 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
320 regs->nip = arch->vex.guest_CIA;
321 regs->msr = 0xf032; /* pretty arbitrary */
322 regs->orig_gpr3 = arch->vex.guest_GPR3;
323 regs->ctr = arch->vex.guest_CTR;
324 regs->link = arch->vex.guest_LR;
325 regs->xer = LibVEX_GuestPPC64_get_XER( &((ThreadArchState*)arch)->vex );
326 regs->ccr = LibVEX_GuestPPC64_get_CR( &((ThreadArchState*)arch)->vex );
329 regs->dar = 0; /* should be fault address? */
333 #elif defined(VGP_arm_linux)
334 regs->ARM_r0 = arch->vex.guest_R0;
335 regs->ARM_r1 = arch->vex.guest_R1;
336 regs->ARM_r2 = arch->vex.guest_R2;
337 regs->ARM_r3 = arch->vex.guest_R3;
338 regs->ARM_r4 = arch->vex.guest_R4;
339 regs->ARM_r5 = arch->vex.guest_R5;
340 regs->ARM_r6 = arch->vex.guest_R6;
341 regs->ARM_r7 = arch->vex.guest_R7;
342 regs->ARM_r8 = arch->vex.guest_R8;
343 regs->ARM_r9 = arch->vex.guest_R9;
344 regs->ARM_r10 = arch->vex.guest_R10;
345 regs->ARM_fp = arch->vex.guest_R11;
346 regs->ARM_ip = arch->vex.guest_R12;
347 regs->ARM_sp = arch->vex.guest_R13;
348 regs->ARM_lr = arch->vex.guest_R14;
349 regs->ARM_pc = arch->vex.guest_R15T;
350 regs->ARM_cpsr = LibVEX_GuestARM_get_cpsr( &((ThreadArchState*)arch)->vex );
352 #elif defined(VGP_s390x_linux)
353 # define DO(n) regs->gprs[n] = arch->vex.guest_r##n
354 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
355 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
357 # define DO(n) regs->acrs[n] = arch->vex.guest_a##n
358 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
359 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
361 regs->orig_gpr2 = arch->vex.guest_r2;
363 # error Unknown ELF platform
367 static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu)
369 __attribute__((unused))
370 ThreadArchState* arch = (ThreadArchState*)&tst->arch;
372 #if defined(VGP_x86_linux)
373 //:: static void fill_fpu(vki_elf_fpregset_t *fpu, const Char *from)
375 //:: if (VG_(have_ssestate)) {
379 //:: /* This is what the kernel does */
380 //:: VG_(memcpy)(fpu, from, 7*sizeof(long));
382 //:: to = (UShort *)&fpu->st_space[0];
383 //:: from += 18 * sizeof(UShort);
385 //:: for (i = 0; i < 8; i++, to += 5, from += 8)
386 //:: VG_(memcpy)(to, from, 5*sizeof(UShort));
388 //:: VG_(memcpy)(fpu, from, sizeof(*fpu));
391 //:: fill_fpu(fpu, (const Char *)&arch->m_sse);
393 #elif defined(VGP_amd64_linux)
401 //:: fpu->mxcsr_mask = ?;
402 //:: fpu->st_space = ?;
404 # define DO(n) VG_(memcpy)(fpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##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);
409 VG_(memset)(fpu->padding, 0, sizeof(fpu->padding));
411 #elif defined(VGP_ppc32_linux)
412 /* The guest state has the FPR fields declared as ULongs, so need
413 to fish out the values without converting them.
414 NOTE: The 32 FP registers map to the first 32 VSX registers.*/
415 # define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
416 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
417 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
418 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
419 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
422 #elif defined(VGP_ppc64_linux)
423 /* The guest state has the FPR fields declared as ULongs, so need
424 to fish out the values without converting them.
425 NOTE: The 32 FP registers map to the first 32 VSX registers.*/
426 # define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
427 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
428 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
429 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
430 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
433 #elif defined(VGP_arm_linux)
436 #elif defined(VGP_s390x_linux)
437 # define DO(n) fpu->fprs[n].ui = arch->vex.guest_f##n
438 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
439 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
442 # error Unknown ELF platform
446 #if defined(VGP_x86_linux)
447 static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu)
449 ThreadArchState* arch = (ThreadArchState*)&tst->arch;
459 //:: xfpu->mxcsr = ?;
461 //:: xfpu->st_space = ?;
463 # define DO(n) VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
464 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
467 VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding));
472 void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
475 Char *basename = "vgcore";
479 NSegment const * seg;
485 struct note *notelist, *note;
487 struct vki_elf_prpsinfo prpsinfo;
488 struct vki_elf_prstatus prstatus;
492 if (VG_(clo_log_fname_expanded) != NULL) {
494 basename = VG_(expand_file_name)(
495 "--log-file (while creating core filename)",
496 VG_(clo_log_fname_expanded));
501 buf = VG_(malloc)( "coredump-elf.mec.1",
502 VG_(strlen)(coreext) + VG_(strlen)(basename)
503 + 100/*for the two %ds. */ );
510 VG_(sprintf)(buf, "%s%s.%d",
511 basename, coreext, VG_(getpid)());
513 VG_(sprintf)(buf, "%s%s.%d.%d",
514 basename, coreext, VG_(getpid)(), seq);
517 sres = VG_(open)(buf,
518 VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC,
519 VKI_S_IRUSR|VKI_S_IWUSR);
520 if (!sr_isError(sres)) {
521 core_fd = sr_Res(sres);
525 if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST)
526 return; /* can't create file */
529 /* Get the segments */
530 seg_starts = VG_(get_segment_starts)(&n_seg_starts);
532 /* First, count how many memory segments to dump */
533 num_phdrs = 1; /* start with notes */
534 for(i = 0; i < n_seg_starts; i++) {
535 if (!may_dump(VG_(am_find_nsegment(seg_starts[i]))))
541 fill_ehdr(&ehdr, num_phdrs);
545 /* Second, work out their layout */
546 phdrs = VG_(arena_malloc)(VG_AR_CORE, "coredump-elf.mec.1",
547 sizeof(*phdrs) * num_phdrs);
549 for(i = 1; i < VG_N_THREADS; i++) {
550 vki_elf_fpregset_t fpu;
552 if (VG_(threads)[i].status == VgTs_Empty)
555 #if defined(VGP_x86_linux)
557 vki_elf_fpxregset_t xfpu;
558 fill_xfpu(&VG_(threads)[i], &xfpu);
559 add_note(¬elist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu));
563 fill_fpu(&VG_(threads)[i], &fpu);
564 add_note(¬elist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu));
566 fill_prstatus(&VG_(threads)[i], &prstatus, si);
567 add_note(¬elist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus));
570 fill_prpsinfo(&VG_(threads)[tid], &prpsinfo);
571 add_note(¬elist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
573 for(note = notelist, notesz = 0; note != NULL; note = note->next)
574 notesz += note_size(note);
576 off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs;
578 phdrs[0].p_type = PT_NOTE;
579 phdrs[0].p_offset = off;
580 phdrs[0].p_vaddr = 0;
581 phdrs[0].p_paddr = 0;
582 phdrs[0].p_filesz = notesz;
583 phdrs[0].p_memsz = 0;
584 phdrs[0].p_flags = 0;
585 phdrs[0].p_align = 0;
589 off = VG_PGROUNDUP(off);
591 for(i = 0, idx = 1; i < n_seg_starts; i++) {
592 seg = VG_(am_find_nsegment(seg_starts[i]));
597 fill_phdr(&phdrs[idx], seg, off, (seg->end - seg->start + off) < max_size);
599 off += phdrs[idx].p_filesz;
604 /* write everything out */
605 VG_(write)(core_fd, &ehdr, sizeof(ehdr));
606 VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs);
608 for(note = notelist; note != NULL; note = note->next)
609 write_note(core_fd, note);
611 VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET);
613 for(i = 0, idx = 1; i < n_seg_starts; i++) {
614 seg = VG_(am_find_nsegment(seg_starts[i]));
616 if (!should_dump(seg))
619 if (phdrs[idx].p_filesz > 0) {
620 vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET) == phdrs[idx].p_offset);
621 vg_assert(seg->end - seg->start >= phdrs[idx].p_filesz);
623 (void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz);
628 VG_(free)(seg_starts);
633 void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
635 make_elf_coredump(tid, si, max_size);
638 #endif // defined(VGO_linux)
640 /*--------------------------------------------------------------------*/
642 /*--------------------------------------------------------------------*/