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