coredump-elf.c revision f34ee4e2fec720d392fe8db5b7de57884174654c
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( &((ThreadArchState*)arch)->vex );
362   regs->ccr = LibVEX_GuestPPC64_get_CR( &((ThreadArchState*)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
423#else
424#  error Unknown ELF platform
425#endif
426}
427
428static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu)
429{
430   __attribute__((unused))
431   const ThreadArchState* arch = &tst->arch;
432
433#if defined(VGP_x86_linux)
434//:: static void fill_fpu(vki_elf_fpregset_t *fpu, const HChar *from)
435//:: {
436//::    if (VG_(have_ssestate)) {
437//::       UShort *to;
438//::       Int i;
439//::
440//::       /* This is what the kernel does */
441//::       VG_(memcpy)(fpu, from, 7*sizeof(long));
442//::
443//::       to = (UShort *)&fpu->st_space[0];
444//::       from += 18 * sizeof(UShort);
445//::
446//::       for (i = 0; i < 8; i++, to += 5, from += 8)
447//:: 	 VG_(memcpy)(to, from, 5*sizeof(UShort));
448//::    } else
449//::       VG_(memcpy)(fpu, from, sizeof(*fpu));
450//:: }
451
452//::    fill_fpu(fpu, (const HChar *)&arch->m_sse);
453
454#elif defined(VGP_amd64_linux)
455//::    fpu->cwd = ?;
456//::    fpu->swd = ?;
457//::    fpu->twd = ?;
458//::    fpu->fop = ?;
459//::    fpu->rip = ?;
460//::    fpu->rdp = ?;
461//::    fpu->mxcsr = ?;
462//::    fpu->mxcsr_mask = ?;
463//::    fpu->st_space = ?;
464
465#  define DO(n)  VG_(memcpy)(fpu->xmm_space + n * 4, \
466                             &arch->vex.guest_YMM##n[0], 16)
467   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
468   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
469#  undef DO
470
471   VG_(memset)(fpu->padding, 0, sizeof(fpu->padding));
472
473#elif defined(VGP_ppc32_linux)
474   /* The guest state has the FPR fields declared as ULongs, so need
475      to fish out the values without converting them.
476      NOTE: The 32 FP registers map to the first 32 VSX registers.*/
477#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
478   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
479   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
480   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
481   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
482#  undef DO
483
484#elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
485   /* The guest state has the FPR fields declared as ULongs, so need
486      to fish out the values without converting them.
487      NOTE: The 32 FP registers map to the first 32 VSX registers.*/
488#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
489   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
490   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
491   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
492   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
493#  undef DO
494
495#elif defined(VGP_arm_linux)
496   // umm ...
497
498#elif defined(VGP_arm64_linux)
499   I_die_here;
500
501#elif defined(VGP_s390x_linux)
502#  define DO(n)  fpu->fprs[n].ui = arch->vex.guest_f##n
503   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
504   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
505# undef DO
506#elif defined(VGP_mips32_linux)
507#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_f##n)
508   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
509   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
510   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
511   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
512#  undef DO
513#elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
514#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_f##n)
515   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
516   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
517   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
518   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
519#  undef DO
520#else
521#  error Unknown ELF platform
522#endif
523}
524
525#if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android)
526static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu)
527{
528   const ThreadArchState* arch = &tst->arch;
529
530//::    xfpu->cwd = ?;
531//::    xfpu->swd = ?;
532//::    xfpu->twd = ?;
533//::    xfpu->fop = ?;
534//::    xfpu->fip = ?;
535//::    xfpu->fcs = ?;
536//::    xfpu->foo = ?;
537//::    xfpu->fos = ?;
538//::    xfpu->mxcsr = ?;
539   xfpu->reserved = 0;
540//::    xfpu->st_space = ?;
541
542#  define DO(n)  VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
543   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
544#  undef DO
545
546   VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding));
547}
548#endif
549
550static
551void dump_one_thread(struct note **notelist, const vki_siginfo_t *si, ThreadId tid)
552{
553   vki_elf_fpregset_t  fpu;
554   struct vki_elf_prstatus prstatus;
555#     if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android)
556      {
557         vki_elf_fpxregset_t xfpu;
558         fill_xfpu(&VG_(threads)[tid], &xfpu);
559         add_note(notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu));
560      }
561#     endif
562
563      fill_fpu(&VG_(threads)[tid], &fpu);
564#     if !defined(VGPV_arm_linux_android) \
565         && !defined(VGPV_x86_linux_android) \
566         && !defined(VGPV_mips32_linux_android) \
567         && !defined(VGPV_arm64_linux_android)
568      add_note(notelist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu));
569#     endif
570
571      fill_prstatus(&VG_(threads)[tid], &prstatus, si);
572#     if !defined(VGPV_arm_linux_android) \
573         && !defined(VGPV_x86_linux_android) \
574         && !defined(VGPV_mips32_linux_android) \
575         && !defined(VGPV_arm64_linux_android)
576      add_note(notelist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus));
577#     endif
578}
579
580static
581void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, ULong max_size)
582{
583   HChar* buf = NULL;
584   const HChar *basename = "vgcore";
585   const HChar *coreext = "";
586   Int seq = 0;
587   Int core_fd;
588   NSegment const * seg;
589   ESZ(Ehdr) ehdr;
590   ESZ(Phdr) *phdrs;
591   Int num_phdrs;
592   Int i, idx;
593   UInt off;
594   struct note *notelist, *note;
595   UInt notesz;
596   struct vki_elf_prpsinfo prpsinfo;
597   Addr *seg_starts;
598   Int n_seg_starts;
599
600   if (VG_(clo_log_fname_expanded) != NULL) {
601      coreext = ".core";
602      basename = VG_(expand_file_name)("--log-file",
603                                       VG_(clo_log_fname_expanded));
604   }
605
606   vg_assert(coreext);
607   vg_assert(basename);
608   buf = VG_(malloc)( "coredump-elf.mec.1",
609                      VG_(strlen)(coreext) + VG_(strlen)(basename)
610                         + 100/*for the two %ds. */ );
611
612   for(;;) {
613      Int oflags = VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC;
614      SysRes sres;
615
616      if (seq == 0)
617	 VG_(sprintf)(buf, "%s%s.%d",
618		      basename, coreext, VG_(getpid)());
619      else
620	 VG_(sprintf)(buf, "%s%s.%d.%d",
621		      basename, coreext, VG_(getpid)(), seq);
622      seq++;
623
624#     if defined(VKI_O_LARGEFILE)
625      oflags |= VKI_O_LARGEFILE;
626#     endif
627
628      sres = VG_(open)(buf, oflags, VKI_S_IRUSR|VKI_S_IWUSR);
629      if (!sr_isError(sres)) {
630         core_fd = sr_Res(sres);
631	 break;
632      }
633
634      if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST)
635	 return;		/* can't create file */
636   }
637
638   /* Get the segments */
639   seg_starts = VG_(get_segment_starts)(&n_seg_starts);
640
641   /* First, count how many memory segments to dump */
642   num_phdrs = 1;		/* start with notes */
643   for(i = 0; i < n_seg_starts; i++) {
644      if (!may_dump(VG_(am_find_nsegment(seg_starts[i]))))
645	 continue;
646
647      num_phdrs++;
648   }
649
650   fill_ehdr(&ehdr, num_phdrs);
651
652   notelist = NULL;
653
654   /* Second, work out their layout */
655   phdrs = VG_(malloc)("coredump-elf.mec.1", sizeof(*phdrs) * num_phdrs);
656
657   /* Add details for all threads except the one that faulted */
658   for(i = 1; i < VG_N_THREADS; i++) {
659
660      if (VG_(threads)[i].status == VgTs_Empty)
661	 continue;
662
663      if (i == tid)
664	 continue;
665
666      dump_one_thread(&notelist, si, i);
667   }
668
669   /* Add details for the faulting thread. Note that because we are
670      adding to the head of a linked list this thread will actually
671      come out first in the core file, which seems to be how
672      debuggers determine that it is the faulting thread. */
673   dump_one_thread(&notelist, si, tid);
674
675   fill_prpsinfo(&VG_(threads)[tid], &prpsinfo);
676#  if !defined(VGPV_arm_linux_android) \
677      && !defined(VGPV_x86_linux_android) \
678      && !defined(VGPV_mips32_linux_android) \
679      && !defined(VGPV_arm64_linux_android)
680   add_note(&notelist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
681#  endif
682
683   for (note = notelist, notesz = 0; note != NULL; note = note->next)
684      notesz += note_size(note);
685
686   off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs;
687
688   phdrs[0].p_type = PT_NOTE;
689   phdrs[0].p_offset = off;
690   phdrs[0].p_vaddr = 0;
691   phdrs[0].p_paddr = 0;
692   phdrs[0].p_filesz = notesz;
693   phdrs[0].p_memsz = 0;
694   phdrs[0].p_flags = 0;
695   phdrs[0].p_align = 0;
696
697   off += notesz;
698
699   off = VG_PGROUNDUP(off);
700
701   for(i = 0, idx = 1; i < n_seg_starts; i++) {
702      seg = VG_(am_find_nsegment(seg_starts[i]));
703
704      if (!may_dump(seg))
705	 continue;
706
707      fill_phdr(&phdrs[idx], seg, off,
708                (seg->end - seg->start + 1 + off) < max_size);
709
710      off += phdrs[idx].p_filesz;
711
712      idx++;
713   }
714
715   /* write everything out */
716   VG_(write)(core_fd, &ehdr, sizeof(ehdr));
717   VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs);
718
719   for(note = notelist; note != NULL; note = note->next)
720      write_note(core_fd, note);
721
722   VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET);
723
724   for(i = 0, idx = 1; i < n_seg_starts; i++) {
725      seg = VG_(am_find_nsegment(seg_starts[i]));
726
727      if (!should_dump(seg))
728	 continue;
729
730      if (phdrs[idx].p_filesz > 0) {
731	 vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET)
732                   == phdrs[idx].p_offset);
733	 vg_assert(seg->end - seg->start + 1 >= phdrs[idx].p_filesz);
734
735	 (void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz);
736      }
737      idx++;
738   }
739
740   VG_(free)(seg_starts);
741
742   VG_(close)(core_fd);
743}
744
745void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, ULong max_size)
746{
747   make_elf_coredump(tid, si, max_size);
748}
749
750#endif // defined(VGO_linux)
751
752/*--------------------------------------------------------------------*/
753/*--- end                                                          ---*/
754/*--------------------------------------------------------------------*/
755