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