drd/tests/tsan_unittest: Avoid that this test reads from uninitialized memory
[valgrind.git] / coregrind / m_coredump / coredump-elf.c
blobec155954137fe352c6ce96b279dd705fe76c1847
2 /*--------------------------------------------------------------------*/
3 /*--- Dumping core. coredump-elf.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2000-2017 Julian Seward
11 jseward@acm.org
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
26 02111-1307, USA.
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_threadstate.h"
46 #include "pub_core_xarray.h"
47 #include "pub_core_clientstate.h"
48 #include "pub_core_options.h"
51 Dump core
53 Generate a standard ELF core file corresponding to the client state
54 at the time of a crash.
56 #include <elf.h>
57 #ifndef NT_PRXFPREG
58 #define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */
59 #endif /* NT_PRXFPREG */
61 #if VG_WORDSIZE == 8
62 #define ESZ(x) Elf64_##x
63 #elif VG_WORDSIZE == 4
64 #define ESZ(x) Elf32_##x
65 #else
66 #error VG_WORDSIZE needs to ==4 or ==8
67 #endif
69 /* If true, then this Segment may be mentioned in the core */
70 static Bool may_dump(const NSegment *seg)
72 if (seg->kind == SkAnonC ||
73 seg->kind == SkShmC ||
74 (seg->kind == SkFileC &&
75 !VKI_S_ISCHR(seg->mode) && !VKI_S_ISBLK(seg->mode)))
76 return True;
78 return False;
81 /* If true, then this Segment's contents will be in the core */
82 static Bool should_dump(const NSegment *seg)
84 return may_dump(seg); // && seg->hasW;
87 static void fill_ehdr(ESZ(Ehdr) *ehdr, Int num_phdrs)
89 VG_(memset)(ehdr, 0, sizeof(*ehdr));
91 VG_(memcpy)(ehdr->e_ident, ELFMAG, SELFMAG);
92 ehdr->e_ident[EI_CLASS] = VG_ELF_CLASS;
93 ehdr->e_ident[EI_DATA] = VG_ELF_DATA2XXX;
94 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
96 ehdr->e_type = ET_CORE;
97 ehdr->e_machine = VG_ELF_MACHINE;
98 ehdr->e_version = EV_CURRENT;
99 ehdr->e_entry = 0;
100 ehdr->e_phoff = sizeof(ESZ(Ehdr));
101 ehdr->e_shoff = 0;
102 ehdr->e_flags = 0;
103 ehdr->e_ehsize = sizeof(ESZ(Ehdr));
104 ehdr->e_phentsize = sizeof(ESZ(Phdr));
105 ehdr->e_phnum = num_phdrs;
106 ehdr->e_shentsize = 0;
107 ehdr->e_shnum = 0;
108 ehdr->e_shstrndx = 0;
112 static void fill_phdr(ESZ(Phdr) *phdr, const NSegment *seg, UInt off, Bool write)
114 SizeT len = seg->end - seg->start + 1;
116 write = write && should_dump(seg);
118 VG_(memset)(phdr, 0, sizeof(*phdr));
120 phdr->p_type = PT_LOAD;
121 phdr->p_offset = off;
122 phdr->p_vaddr = seg->start;
123 phdr->p_paddr = 0;
124 phdr->p_filesz = write ? len : 0;
125 phdr->p_memsz = len;
126 phdr->p_flags = 0;
128 if (seg->hasR)
129 phdr->p_flags |= PF_R;
130 if (seg->hasW)
131 phdr->p_flags |= PF_W;
132 if (seg->hasX)
133 phdr->p_flags |= PF_X;
135 phdr->p_align = VKI_PAGE_SIZE;
138 #if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
139 || defined(VGPV_mips32_linux_android)
140 /* Android's libc doesn't provide a definition for this. Hence: */
141 typedef
142 struct {
143 Elf32_Word n_namesz;
144 Elf32_Word n_descsz;
145 Elf32_Word n_type;
147 Elf32_Nhdr;
148 #endif
150 struct note {
151 struct note *next;
152 ESZ(Nhdr) note;
153 HChar name[0];
156 static UInt note_size(const struct note *n)
158 return sizeof(ESZ(Nhdr)) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4)
159 + VG_ROUNDUP(n->note.n_descsz, 4);
162 #if !defined(VGPV_arm_linux_android) \
163 && !defined(VGPV_x86_linux_android) \
164 && !defined(VGPV_mips32_linux_android) \
165 && !defined(VGPV_arm64_linux_android)
166 static void add_note(struct note **list, const HChar *name, UInt type,
167 const void *data, UInt datasz)
169 Int namelen = VG_(strlen)(name)+1;
170 Int notelen = sizeof(struct note) +
171 VG_ROUNDUP(namelen, 4) +
172 VG_ROUNDUP(datasz, 4);
173 struct note *n = VG_(malloc)("coredump-elf.an.1", notelen);
175 VG_(memset)(n, 0, notelen);
177 n->next = *list;
178 *list = n;
180 n->note.n_type = type;
181 n->note.n_namesz = namelen;
182 n->note.n_descsz = datasz;
184 VG_(memcpy)(n->name, name, namelen);
185 VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz);
187 #endif /* !defined(VGPV_*_linux_android) */
189 static void write_note(Int fd, const struct note *n)
191 VG_(write)(fd, &n->note, note_size(n));
194 static void fill_prpsinfo(const ThreadState *tst,
195 struct vki_elf_prpsinfo *prpsinfo)
197 VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
199 switch(tst->status) {
200 case VgTs_Runnable:
201 case VgTs_Yielding:
202 prpsinfo->pr_sname = 'R';
203 break;
205 case VgTs_WaitSys:
206 prpsinfo->pr_sname = 'S';
207 break;
209 case VgTs_Zombie:
210 prpsinfo->pr_sname = 'Z';
211 break;
213 case VgTs_Empty:
214 case VgTs_Init:
215 prpsinfo->pr_sname = '?';
216 break;
219 prpsinfo->pr_uid = 0;
220 prpsinfo->pr_gid = 0;
222 VG_(client_fname)(prpsinfo->pr_fname, sizeof(prpsinfo->pr_fname), False);
225 static void fill_prstatus(const ThreadState *tst,
226 /*OUT*/struct vki_elf_prstatus *prs,
227 const vki_siginfo_t *si)
229 #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
230 vki_elf_greg_t *regs;
231 #else
232 struct vki_user_regs_struct *regs;
233 #endif
234 const ThreadArchState* arch = &tst->arch;
236 VG_(memset)(prs, 0, sizeof(*prs));
238 prs->pr_info.si_signo = si->si_signo;
239 prs->pr_info.si_code = si->si_code;
240 prs->pr_info.si_errno = 0;
242 prs->pr_cursig = si->si_signo;
244 prs->pr_pid = tst->os_state.lwpid;
245 prs->pr_ppid = 0;
246 prs->pr_pgrp = VG_(getpgrp)();
247 prs->pr_sid = VG_(getpgrp)();
249 #if defined(VGP_s390x_linux)
250 /* prs->pr_reg has struct type. Need to take address. */
251 regs = (struct vki_user_regs_struct *)&(prs->pr_reg);
252 #elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
253 regs = (vki_elf_greg_t *)prs->pr_reg;
254 #else
255 regs = (struct vki_user_regs_struct *)prs->pr_reg;
256 vg_assert(sizeof(*regs) == sizeof(prs->pr_reg));
257 #endif
259 #if defined(VGP_x86_linux)
260 regs->eflags = LibVEX_GuestX86_get_eflags( &arch->vex );
261 regs->esp = arch->vex.guest_ESP;
262 regs->eip = arch->vex.guest_EIP;
264 regs->ebx = arch->vex.guest_EBX;
265 regs->ecx = arch->vex.guest_ECX;
266 regs->edx = arch->vex.guest_EDX;
267 regs->esi = arch->vex.guest_ESI;
268 regs->edi = arch->vex.guest_EDI;
269 regs->ebp = arch->vex.guest_EBP;
270 regs->eax = arch->vex.guest_EAX;
272 regs->cs = arch->vex.guest_CS;
273 regs->ds = arch->vex.guest_DS;
274 regs->ss = arch->vex.guest_SS;
275 regs->es = arch->vex.guest_ES;
276 regs->fs = arch->vex.guest_FS;
277 regs->gs = arch->vex.guest_GS;
279 #elif defined(VGP_amd64_linux)
280 regs->eflags = LibVEX_GuestAMD64_get_rflags( &arch->vex );
281 regs->rsp = arch->vex.guest_RSP;
282 regs->rip = arch->vex.guest_RIP;
284 regs->rbx = arch->vex.guest_RBX;
285 regs->rcx = arch->vex.guest_RCX;
286 regs->rdx = arch->vex.guest_RDX;
287 regs->rsi = arch->vex.guest_RSI;
288 regs->rdi = arch->vex.guest_RDI;
289 regs->rbp = arch->vex.guest_RBP;
290 regs->rax = arch->vex.guest_RAX;
291 regs->r8 = arch->vex.guest_R8;
292 regs->r9 = arch->vex.guest_R9;
293 regs->r10 = arch->vex.guest_R10;
294 regs->r11 = arch->vex.guest_R11;
295 regs->r12 = arch->vex.guest_R12;
296 regs->r13 = arch->vex.guest_R13;
297 regs->r14 = arch->vex.guest_R14;
298 regs->r15 = arch->vex.guest_R15;
300 #elif defined(VGP_ppc32_linux)
301 # define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n
302 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
303 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
304 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
305 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
306 # undef DO
308 regs->nip = arch->vex.guest_CIA;
309 regs->msr = 0xf032; /* pretty arbitrary */
310 regs->orig_gpr3 = arch->vex.guest_GPR3;
311 regs->ctr = arch->vex.guest_CTR;
312 regs->link = arch->vex.guest_LR;
313 regs->xer = LibVEX_GuestPPC32_get_XER( &arch->vex );
314 regs->ccr = LibVEX_GuestPPC32_get_CR( &arch->vex );
315 regs->mq = 0;
316 regs->trap = 0;
317 regs->dar = 0; /* should be fault address? */
318 regs->dsisr = 0;
319 regs->result = 0;
321 #elif defined(VGP_ppc64be_linux)
322 # define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n
323 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
324 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
325 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
326 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
327 # undef DO
329 regs->nip = arch->vex.guest_CIA;
330 regs->msr = 0xf032; /* pretty arbitrary */
331 regs->orig_gpr3 = arch->vex.guest_GPR3;
332 regs->ctr = arch->vex.guest_CTR;
333 regs->link = arch->vex.guest_LR;
334 regs->xer = LibVEX_GuestPPC64_get_XER( &arch->vex );
335 regs->ccr = LibVEX_GuestPPC64_get_CR( &arch->vex );
336 /* regs->mq = 0; */
337 regs->trap = 0;
338 regs->dar = 0; /* should be fault address? */
339 regs->dsisr = 0;
340 regs->result = 0;
342 #elif defined(VGP_ppc64le_linux)
343 # define DO(n) regs->gpr[n] = arch->vex.guest_GPR##n
344 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
345 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
346 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
347 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
348 # undef DO
350 regs->nip = arch->vex.guest_CIA;
351 regs->msr = 0xf033; /* pretty arbitrary */
352 regs->orig_gpr3 = arch->vex.guest_GPR3;
353 regs->ctr = arch->vex.guest_CTR;
354 regs->link = arch->vex.guest_LR;
355 regs->xer = LibVEX_GuestPPC64_get_XER( &(arch->vex) );
356 regs->ccr = LibVEX_GuestPPC64_get_CR( &(arch->vex) );
357 /* regs->mq = 0; */
358 regs->trap = 0;
359 regs->dar = 0; /* should be fault address? */
360 regs->dsisr = 0;
361 regs->result = 0;
363 #elif defined(VGP_arm_linux)
364 regs->ARM_r0 = arch->vex.guest_R0;
365 regs->ARM_r1 = arch->vex.guest_R1;
366 regs->ARM_r2 = arch->vex.guest_R2;
367 regs->ARM_r3 = arch->vex.guest_R3;
368 regs->ARM_r4 = arch->vex.guest_R4;
369 regs->ARM_r5 = arch->vex.guest_R5;
370 regs->ARM_r6 = arch->vex.guest_R6;
371 regs->ARM_r7 = arch->vex.guest_R7;
372 regs->ARM_r8 = arch->vex.guest_R8;
373 regs->ARM_r9 = arch->vex.guest_R9;
374 regs->ARM_r10 = arch->vex.guest_R10;
375 regs->ARM_fp = arch->vex.guest_R11;
376 regs->ARM_ip = arch->vex.guest_R12;
377 regs->ARM_sp = arch->vex.guest_R13;
378 regs->ARM_lr = arch->vex.guest_R14;
379 regs->ARM_pc = arch->vex.guest_R15T;
380 regs->ARM_cpsr = LibVEX_GuestARM_get_cpsr( &arch->vex );
382 #elif defined(VGP_arm64_linux)
383 regs->regs[0] = arch->vex.guest_X0;
384 regs->regs[1] = arch->vex.guest_X1;
385 regs->regs[2] = arch->vex.guest_X2;
386 regs->regs[3] = arch->vex.guest_X3;
387 regs->regs[4] = arch->vex.guest_X4;
388 regs->regs[5] = arch->vex.guest_X5;
389 regs->regs[6] = arch->vex.guest_X6;
390 regs->regs[7] = arch->vex.guest_X7;
391 regs->regs[8] = arch->vex.guest_X8;
392 regs->regs[9] = arch->vex.guest_X9;
393 regs->regs[10] = arch->vex.guest_X10;
394 regs->regs[11] = arch->vex.guest_X11;
395 regs->regs[12] = arch->vex.guest_X12;
396 regs->regs[13] = arch->vex.guest_X13;
397 regs->regs[14] = arch->vex.guest_X14;
398 regs->regs[15] = arch->vex.guest_X15;
399 regs->regs[16] = arch->vex.guest_X16;
400 regs->regs[17] = arch->vex.guest_X17;
401 regs->regs[18] = arch->vex.guest_X18;
402 regs->regs[19] = arch->vex.guest_X19;
403 regs->regs[20] = arch->vex.guest_X20;
404 regs->regs[21] = arch->vex.guest_X21;
405 regs->regs[22] = arch->vex.guest_X22;
406 regs->regs[23] = arch->vex.guest_X23;
407 regs->regs[24] = arch->vex.guest_X24;
408 regs->regs[25] = arch->vex.guest_X25;
409 regs->regs[26] = arch->vex.guest_X26;
410 regs->regs[27] = arch->vex.guest_X27;
411 regs->regs[28] = arch->vex.guest_X28;
412 regs->regs[29] = arch->vex.guest_X29;
413 regs->regs[30] = arch->vex.guest_X30;
414 regs->sp = arch->vex.guest_XSP;
415 regs->pc = arch->vex.guest_PC;
416 regs->pstate = LibVEX_GuestARM64_get_nzcv( &arch->vex ); /* is this correct? */
418 #elif defined(VGP_s390x_linux)
419 # define DO(n) regs->gprs[n] = arch->vex.guest_r##n
420 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
421 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
422 # undef DO
423 # define DO(n) regs->acrs[n] = arch->vex.guest_a##n
424 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
425 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
426 # undef DO
427 regs->orig_gpr2 = arch->vex.guest_r2;
429 #elif defined(VGP_mips32_linux)
430 # define DO(n) regs[VKI_MIPS32_EF_R##n] = arch->vex.guest_r##n
431 DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); DO(8);
432 DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); DO(16);
433 DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); DO(24);
434 DO(25); DO(28); DO(29); DO(30); DO(31);
435 # undef DO
436 regs[VKI_MIPS32_EF_LO] = arch->vex.guest_LO;
437 regs[VKI_MIPS32_EF_HI] = arch->vex.guest_HI;
438 regs[VKI_MIPS32_EF_CP0_STATUS] = arch->vex.guest_CP0_status;
439 regs[VKI_MIPS32_EF_CP0_EPC] = arch->vex.guest_PC;
440 #elif defined(VGP_mips64_linux)
441 # define DO(n) regs[VKI_MIPS64_EF_R##n] = arch->vex.guest_r##n
442 DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); DO(8);
443 DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); DO(16);
444 DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); DO(24);
445 DO(25); DO(28); DO(29); DO(30); DO(31);
446 # undef DO
447 regs[VKI_MIPS64_EF_LO] = arch->vex.guest_LO;
448 regs[VKI_MIPS64_EF_HI] = arch->vex.guest_HI;
449 regs[VKI_MIPS64_EF_CP0_STATUS] = arch->vex.guest_CP0_status;
450 regs[VKI_MIPS64_EF_CP0_EPC] = arch->vex.guest_PC;
451 #else
452 # error Unknown ELF platform
453 #endif
456 static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu)
458 __attribute__((unused))
459 const ThreadArchState* arch = &tst->arch;
461 #if defined(VGP_x86_linux)
462 //:: static void fill_fpu(vki_elf_fpregset_t *fpu, const HChar *from)
463 //:: {
464 //:: if (VG_(have_ssestate)) {
465 //:: UShort *to;
466 //:: Int i;
467 //::
468 //:: /* This is what the kernel does */
469 //:: VG_(memcpy)(fpu, from, 7*sizeof(long));
470 //::
471 //:: to = (UShort *)&fpu->st_space[0];
472 //:: from += 18 * sizeof(UShort);
473 //::
474 //:: for (i = 0; i < 8; i++, to += 5, from += 8)
475 //:: VG_(memcpy)(to, from, 5*sizeof(UShort));
476 //:: } else
477 //:: VG_(memcpy)(fpu, from, sizeof(*fpu));
478 //:: }
480 //:: fill_fpu(fpu, (const HChar *)&arch->m_sse);
482 #elif defined(VGP_amd64_linux)
483 //:: fpu->cwd = ?;
484 //:: fpu->swd = ?;
485 //:: fpu->twd = ?;
486 //:: fpu->fop = ?;
487 //:: fpu->rip = ?;
488 //:: fpu->rdp = ?;
489 //:: fpu->mxcsr = ?;
490 //:: fpu->mxcsr_mask = ?;
491 //:: fpu->st_space = ?;
493 # define DO(n) VG_(memcpy)(fpu->xmm_space + n * 4, \
494 &arch->vex.guest_YMM##n[0], 16)
495 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
496 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
497 # undef DO
499 VG_(memset)(fpu->padding, 0, sizeof(fpu->padding));
501 #elif defined(VGP_ppc32_linux)
502 /* The guest state has the FPR fields declared as ULongs, so need
503 to fish out the values without converting them.
504 NOTE: The 32 FP registers map to the first 32 VSX registers.*/
505 # define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
506 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
507 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
508 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
509 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
510 # undef DO
512 #elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
513 /* The guest state has the FPR fields declared as ULongs, so need
514 to fish out the values without converting them.
515 NOTE: The 32 FP registers map to the first 32 VSX registers.*/
516 # define DO(n) (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
517 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
518 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
519 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
520 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
521 # undef DO
523 #elif defined(VGP_arm_linux)
524 // umm ...
526 #elif defined(VGP_arm64_linux)
527 fpu->vregs[0] = *(const __uint128_t*)arch->vex.guest_Q0;
528 fpu->vregs[1] = *(const __uint128_t*)arch->vex.guest_Q1;
529 fpu->vregs[2] = *(const __uint128_t*)arch->vex.guest_Q2;
530 fpu->vregs[3] = *(const __uint128_t*)arch->vex.guest_Q3;
531 fpu->vregs[4] = *(const __uint128_t*)arch->vex.guest_Q4;
532 fpu->vregs[5] = *(const __uint128_t*)arch->vex.guest_Q5;
533 fpu->vregs[6] = *(const __uint128_t*)arch->vex.guest_Q6;
534 fpu->vregs[7] = *(const __uint128_t*)arch->vex.guest_Q7;
535 fpu->vregs[8] = *(const __uint128_t*)arch->vex.guest_Q8;
536 fpu->vregs[9] = *(const __uint128_t*)arch->vex.guest_Q9;
537 fpu->vregs[10] = *(const __uint128_t*)arch->vex.guest_Q10;
538 fpu->vregs[11] = *(const __uint128_t*)arch->vex.guest_Q11;
539 fpu->vregs[12] = *(const __uint128_t*)arch->vex.guest_Q12;
540 fpu->vregs[13] = *(const __uint128_t*)arch->vex.guest_Q13;
541 fpu->vregs[14] = *(const __uint128_t*)arch->vex.guest_Q14;
542 fpu->vregs[15] = *(const __uint128_t*)arch->vex.guest_Q15;
543 fpu->vregs[16] = *(const __uint128_t*)arch->vex.guest_Q16;
544 fpu->vregs[17] = *(const __uint128_t*)arch->vex.guest_Q17;
545 fpu->vregs[18] = *(const __uint128_t*)arch->vex.guest_Q18;
546 fpu->vregs[19] = *(const __uint128_t*)arch->vex.guest_Q19;
547 fpu->vregs[20] = *(const __uint128_t*)arch->vex.guest_Q20;
548 fpu->vregs[21] = *(const __uint128_t*)arch->vex.guest_Q21;
549 fpu->vregs[22] = *(const __uint128_t*)arch->vex.guest_Q22;
550 fpu->vregs[23] = *(const __uint128_t*)arch->vex.guest_Q23;
551 fpu->vregs[24] = *(const __uint128_t*)arch->vex.guest_Q24;
552 fpu->vregs[25] = *(const __uint128_t*)arch->vex.guest_Q25;
553 fpu->vregs[26] = *(const __uint128_t*)arch->vex.guest_Q26;
554 fpu->vregs[27] = *(const __uint128_t*)arch->vex.guest_Q27;
555 fpu->vregs[28] = *(const __uint128_t*)arch->vex.guest_Q28;
556 fpu->vregs[29] = *(const __uint128_t*)arch->vex.guest_Q29;
557 fpu->vregs[30] = *(const __uint128_t*)arch->vex.guest_Q30;
558 fpu->vregs[31] = *(const __uint128_t*)arch->vex.guest_Q31;
559 fpu->fpsr = *(const __vki_u32*)arch->vex.guest_QCFLAG;
560 fpu->fpcr = arch->vex.guest_FPCR;
562 #elif defined(VGP_s390x_linux)
563 /* NOTE: The 16 FP registers map to the first 16 VSX registers. */
564 # define DO(n) fpu->fprs[n].ui = *(const Double*)(&arch->vex.guest_v##n.w64[0])
565 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
566 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
567 # undef DO
568 #elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
569 # define DO(n) (*fpu)[n] = *(const double*)(&arch->vex.guest_f##n)
570 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
571 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
572 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
573 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
574 # undef DO
575 #else
576 # error Unknown ELF platform
577 #endif
580 #if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android)
581 static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu)
583 const ThreadArchState* arch = &tst->arch;
585 //:: xfpu->cwd = ?;
586 //:: xfpu->swd = ?;
587 //:: xfpu->twd = ?;
588 //:: xfpu->fop = ?;
589 //:: xfpu->fip = ?;
590 //:: xfpu->fcs = ?;
591 //:: xfpu->foo = ?;
592 //:: xfpu->fos = ?;
593 //:: xfpu->mxcsr = ?;
594 xfpu->reserved = 0;
595 //:: xfpu->st_space = ?;
597 # define DO(n) VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
598 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7);
599 # undef DO
601 VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding));
603 #endif
605 static
606 void dump_one_thread(struct note **notelist, const vki_siginfo_t *si, ThreadId tid)
608 vki_elf_fpregset_t fpu;
609 struct vki_elf_prstatus prstatus;
610 VG_(memset)(&fpu, 0, sizeof(fpu));
611 VG_(memset)(&prstatus, 0, sizeof(prstatus));
612 # if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android)
614 vki_elf_fpxregset_t xfpu;
615 VG_(memset)(&xfpu, 0, sizeof(xfpu));
616 fill_xfpu(&VG_(threads)[tid], &xfpu);
617 add_note(notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu));
619 # endif
621 fill_fpu(&VG_(threads)[tid], &fpu);
622 # if !defined(VGPV_arm_linux_android) \
623 && !defined(VGPV_x86_linux_android) \
624 && !defined(VGPV_mips32_linux_android) \
625 && !defined(VGPV_arm64_linux_android)
626 add_note(notelist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu));
627 # endif
629 fill_prstatus(&VG_(threads)[tid], &prstatus, si);
630 # if !defined(VGPV_arm_linux_android) \
631 && !defined(VGPV_x86_linux_android) \
632 && !defined(VGPV_mips32_linux_android) \
633 && !defined(VGPV_arm64_linux_android)
634 add_note(notelist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus));
635 # endif
638 static
639 void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, ULong max_size)
641 HChar* buf = NULL;
642 HChar *basename;
643 const HChar *coreext = "";
644 Int seq = 0;
645 Int core_fd;
646 NSegment const * seg;
647 ESZ(Ehdr) ehdr;
648 ESZ(Phdr) *phdrs = NULL;
649 Int num_phdrs;
650 Int i, idx;
651 UInt off;
652 struct note *notelist, *note;
653 UInt notesz;
654 struct vki_elf_prpsinfo prpsinfo;
655 Addr *seg_starts = NULL;
656 Int n_seg_starts;
658 if (VG_(clo_log_fname_unexpanded) != NULL) {
659 coreext = ".core";
660 basename = VG_(expand_file_name)("--log-file",
661 VG_(clo_log_fname_unexpanded));
662 } else
663 basename = VG_(strdup)("coredump-elf.mec.1", "vgcore");
665 vg_assert(coreext);
666 vg_assert(basename);
667 buf = VG_(malloc)( "coredump-elf.mec.1",
668 VG_(strlen)(coreext) + VG_(strlen)(basename)
669 + 100/*for the two %ds. */ );
671 for(;;) {
672 Int oflags = VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC;
673 SysRes sres;
675 if (seq == 0)
676 VG_(sprintf)(buf, "%s%s.%d",
677 basename, coreext, VG_(getpid)());
678 else
679 VG_(sprintf)(buf, "%s%s.%d.%d",
680 basename, coreext, VG_(getpid)(), seq);
681 seq++;
683 # if defined(VKI_O_LARGEFILE)
684 oflags |= VKI_O_LARGEFILE;
685 # endif
687 sres = VG_(open)(buf, oflags, VKI_S_IRUSR|VKI_S_IWUSR);
688 if (!sr_isError(sres)) {
689 core_fd = sr_Res(sres);
690 break;
693 if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST)
694 goto cleanup; /* can't create file */
697 /* Get the client segments */
698 seg_starts = VG_(get_segment_starts)(SkFileC | SkAnonC | SkShmC,
699 &n_seg_starts);
701 /* First, count how many memory segments to dump */
702 num_phdrs = 1; /* start with notes */
703 for(i = 0; i < n_seg_starts; i++) {
704 if (!may_dump(VG_(am_find_nsegment)(seg_starts[i])))
705 continue;
707 num_phdrs++;
710 fill_ehdr(&ehdr, num_phdrs);
712 notelist = NULL;
714 /* Second, work out their layout */
715 phdrs = VG_(malloc)("coredump-elf.mec.1", sizeof(*phdrs) * num_phdrs);
717 /* Add details for all threads except the one that faulted */
718 for(i = 1; i < VG_N_THREADS; i++) {
720 if (VG_(threads)[i].status == VgTs_Empty)
721 continue;
723 if (i == tid)
724 continue;
726 dump_one_thread(&notelist, si, i);
729 /* Add details for the faulting thread. Note that because we are
730 adding to the head of a linked list this thread will actually
731 come out first in the core file, which seems to be how
732 debuggers determine that it is the faulting thread. */
733 dump_one_thread(&notelist, si, tid);
735 fill_prpsinfo(&VG_(threads)[tid], &prpsinfo);
736 # if !defined(VGPV_arm_linux_android) \
737 && !defined(VGPV_x86_linux_android) \
738 && !defined(VGPV_mips32_linux_android) \
739 && !defined(VGPV_arm64_linux_android)
740 add_note(&notelist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
741 # endif
743 for (note = notelist, notesz = 0; note != NULL; note = note->next)
744 notesz += note_size(note);
746 off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs;
748 phdrs[0].p_type = PT_NOTE;
749 phdrs[0].p_offset = off;
750 phdrs[0].p_vaddr = 0;
751 phdrs[0].p_paddr = 0;
752 phdrs[0].p_filesz = notesz;
753 phdrs[0].p_memsz = 0;
754 phdrs[0].p_flags = 0;
755 phdrs[0].p_align = 0;
757 off += notesz;
759 off = VG_PGROUNDUP(off);
761 for(i = 0, idx = 1; i < n_seg_starts; i++) {
762 seg = VG_(am_find_nsegment)(seg_starts[i]);
764 if (!may_dump(seg))
765 continue;
767 fill_phdr(&phdrs[idx], seg, off,
768 (seg->end - seg->start + 1 + off) < max_size);
770 off += phdrs[idx].p_filesz;
772 idx++;
775 /* write everything out */
776 VG_(write)(core_fd, &ehdr, sizeof(ehdr));
777 VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs);
779 for(note = notelist; note != NULL; note = note->next)
780 write_note(core_fd, note);
782 VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET);
784 for(i = 0, idx = 1; i < n_seg_starts; i++) {
785 seg = VG_(am_find_nsegment)(seg_starts[i]);
787 if (!should_dump(seg))
788 continue;
790 if (phdrs[idx].p_filesz > 0) {
791 vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET)
792 == phdrs[idx].p_offset);
793 vg_assert(seg->end - seg->start + 1 >= phdrs[idx].p_filesz);
795 (void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz);
797 idx++;
800 VG_(close)(core_fd);
802 cleanup:
803 VG_(free)(basename);
804 VG_(free)(buf);
805 VG_(free)(seg_starts);
806 VG_(free)(phdrs);
809 void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, ULong max_size)
811 make_elf_coredump(tid, si, max_size);
814 #endif // defined(VGO_linux)
816 /*--------------------------------------------------------------------*/
817 /*--- end ---*/
818 /*--------------------------------------------------------------------*/