blob: 064cea5aa385e12a89257003775d971be78484fa [file] [log] [blame]
njn67229832005-08-28 04:38:12 +00001
2/*--------------------------------------------------------------------*/
tomf4c23102005-10-31 17:05:21 +00003/*--- Dumping core. coredump-elf.c ---*/
njn67229832005-08-28 04:38:12 +00004/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj4d474d02008-02-11 11:34:59 +000010 Copyright (C) 2000-2008 Julian Seward
njn67229832005-08-28 04:38:12 +000011 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#include "pub_core_basics.h"
sewardj4cfea4f2006-10-14 19:26:10 +000032#include "pub_core_vki.h"
tomf4c23102005-10-31 17:05:21 +000033#include "pub_core_aspacemgr.h"
34#include "pub_core_libcbase.h"
35#include "pub_core_machine.h"
njn67229832005-08-28 04:38:12 +000036#include "pub_core_coredump.h"
tomf4c23102005-10-31 17:05:21 +000037#include "pub_core_libcprint.h"
38#include "pub_core_libcfile.h" // VG_(close) et al
39#include "pub_core_libcproc.h" // VG_(geteuid), VG_(getegid)
40#include "pub_core_libcassert.h" // VG_(exit), vg_assert
41#include "pub_core_mallocfree.h" // VG_(malloc), VG_(free)
42#include "pub_core_threadstate.h"
sewardj14c7cc52007-02-25 15:08:24 +000043#include "pub_core_xarray.h"
tomf4c23102005-10-31 17:05:21 +000044#include "pub_core_clientstate.h"
45#include "pub_core_options.h"
njn67229832005-08-28 04:38:12 +000046
tomf4c23102005-10-31 17:05:21 +000047#include "priv_elf.h"
48
njn67229832005-08-28 04:38:12 +000049/*
50 Dump core
51
52 Generate a standard ELF core file corresponding to the client state
53 at the time of a crash.
54 */
55#include <elf.h>
56#ifndef NT_PRXFPREG
57#define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */
58#endif /* NT_PRXFPREG */
59
tomf4c23102005-10-31 17:05:21 +000060#if VG_WORDSIZE == 8
61#define ESZ(x) Elf64_##x
62#elif VG_WORDSIZE == 4
63#define ESZ(x) Elf32_##x
64#else
65#error VG_WORDSIZE needs to ==4 or ==8
66#endif
67
68/* TODO: GIVE THIS A PROPER HOME
njn1d0825f2006-03-27 11:37:07 +000069 TODO: MERGE THIS WITH DUPLICATES IN m_main.c and mc_leakcheck.c
tomf4c23102005-10-31 17:05:21 +000070 Extract from aspacem a vector of the current segment start
71 addresses. The vector is dynamically allocated and should be freed
72 by the caller when done. REQUIRES m_mallocfree to be running.
73 Writes the number of addresses required into *n_acquired. */
74
75static Addr* get_seg_starts ( /*OUT*/Int* n_acquired )
njn67229832005-08-28 04:38:12 +000076{
tomf4c23102005-10-31 17:05:21 +000077 Addr* starts;
78 Int n_starts, r = 0;
79
80 n_starts = 1;
81 while (True) {
82 starts = VG_(malloc)( n_starts * sizeof(Addr) );
83 if (starts == NULL)
84 break;
85 r = VG_(am_get_segment_starts)( starts, n_starts );
86 if (r >= 0)
87 break;
88 VG_(free)(starts);
89 n_starts *= 2;
90 }
91
92 if (starts == NULL) {
93 *n_acquired = 0;
94 return NULL;
95 }
96
97 *n_acquired = r;
98 return starts;
99}
100
101/* If true, then this Segment may be mentioned in the core */
102static Bool may_dump(const NSegment *seg)
103{
104 if (seg->kind == SkAnonC ||
105 seg->kind == SkShmC ||
106 (seg->kind == SkFileC &&
107 !VKI_S_ISCHR(seg->mode) && !VKI_S_ISBLK(seg->mode)))
108 return True;
109
110 return False;
njn67229832005-08-28 04:38:12 +0000111}
112
113/* If true, then this Segment's contents will be in the core */
tomf4c23102005-10-31 17:05:21 +0000114static Bool should_dump(const NSegment *seg)
njn67229832005-08-28 04:38:12 +0000115{
tomf4c23102005-10-31 17:05:21 +0000116 return may_dump(seg); // && seg->hasW;
njn67229832005-08-28 04:38:12 +0000117}
118
tomf4c23102005-10-31 17:05:21 +0000119static void fill_ehdr(ESZ(Ehdr) *ehdr, Int num_phdrs)
njn67229832005-08-28 04:38:12 +0000120{
121 VG_(memset)(ehdr, 0, sizeof(*ehdr));
122
123 VG_(memcpy)(ehdr->e_ident, ELFMAG, SELFMAG);
124 ehdr->e_ident[EI_CLASS] = VG_ELF_CLASS;
tomf4c23102005-10-31 17:05:21 +0000125 ehdr->e_ident[EI_DATA] = VG_ELF_DATA2XXX;
njn67229832005-08-28 04:38:12 +0000126 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
127
128 ehdr->e_type = ET_CORE;
129 ehdr->e_machine = VG_ELF_MACHINE;
130 ehdr->e_version = EV_CURRENT;
131 ehdr->e_entry = 0;
tomf4c23102005-10-31 17:05:21 +0000132 ehdr->e_phoff = sizeof(ESZ(Ehdr));
njn67229832005-08-28 04:38:12 +0000133 ehdr->e_shoff = 0;
134 ehdr->e_flags = 0;
tomf4c23102005-10-31 17:05:21 +0000135 ehdr->e_ehsize = sizeof(ESZ(Ehdr));
136 ehdr->e_phentsize = sizeof(ESZ(Phdr));
njn67229832005-08-28 04:38:12 +0000137 ehdr->e_phnum = num_phdrs;
138 ehdr->e_shentsize = 0;
139 ehdr->e_shnum = 0;
140 ehdr->e_shstrndx = 0;
141
142}
143
tomf4c23102005-10-31 17:05:21 +0000144static void fill_phdr(ESZ(Phdr) *phdr, const NSegment *seg, UInt off, Bool write)
njn67229832005-08-28 04:38:12 +0000145{
tomf4c23102005-10-31 17:05:21 +0000146 SizeT len = seg->end - seg->start;
147
njn67229832005-08-28 04:38:12 +0000148 write = write && should_dump(seg);
149
150 VG_(memset)(phdr, 0, sizeof(*phdr));
151
152 phdr->p_type = PT_LOAD;
153 phdr->p_offset = off;
tomf4c23102005-10-31 17:05:21 +0000154 phdr->p_vaddr = seg->start;
njn67229832005-08-28 04:38:12 +0000155 phdr->p_paddr = 0;
tomf4c23102005-10-31 17:05:21 +0000156 phdr->p_filesz = write ? len : 0;
157 phdr->p_memsz = len;
njn67229832005-08-28 04:38:12 +0000158 phdr->p_flags = 0;
159
tomf4c23102005-10-31 17:05:21 +0000160 if (seg->hasR)
njn67229832005-08-28 04:38:12 +0000161 phdr->p_flags |= PF_R;
tomf4c23102005-10-31 17:05:21 +0000162 if (seg->hasW)
njn67229832005-08-28 04:38:12 +0000163 phdr->p_flags |= PF_W;
tomf4c23102005-10-31 17:05:21 +0000164 if (seg->hasX)
njn67229832005-08-28 04:38:12 +0000165 phdr->p_flags |= PF_X;
166
167 phdr->p_align = VKI_PAGE_SIZE;
168}
169
170struct note {
171 struct note *next;
tomf4c23102005-10-31 17:05:21 +0000172 ESZ(Nhdr) note;
njn67229832005-08-28 04:38:12 +0000173 Char name[0];
174};
175
176static UInt note_size(const struct note *n)
177{
tomf4c23102005-10-31 17:05:21 +0000178 return sizeof(ESZ(Nhdr)) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4) + VG_ROUNDUP(n->note.n_descsz, 4);
njn67229832005-08-28 04:38:12 +0000179}
180
181static void add_note(struct note **list, const Char *name, UInt type, const void *data, UInt datasz)
182{
183 Int namelen = VG_(strlen)(name)+1;
184 Int notelen = sizeof(struct note) +
185 VG_ROUNDUP(namelen, 4) +
186 VG_ROUNDUP(datasz, 4);
187 struct note *n = VG_(arena_malloc)(VG_AR_CORE, notelen);
188
189 VG_(memset)(n, 0, notelen);
190
191 n->next = *list;
192 *list = n;
193
194 n->note.n_type = type;
195 n->note.n_namesz = namelen;
196 n->note.n_descsz = datasz;
197
198 VG_(memcpy)(n->name, name, namelen);
199 VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz);
200}
201
202static void write_note(Int fd, const struct note *n)
203{
204 VG_(write)(fd, &n->note, note_size(n));
205}
206
207static void fill_prpsinfo(const ThreadState *tst, struct vki_elf_prpsinfo *prpsinfo)
208{
209 static Char name[VKI_PATH_MAX];
njn67229832005-08-28 04:38:12 +0000210
211 VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
212
213 switch(tst->status) {
214 case VgTs_Runnable:
215 case VgTs_Yielding:
216 prpsinfo->pr_sname = 'R';
217 break;
218
219 case VgTs_WaitSys:
220 prpsinfo->pr_sname = 'S';
221 break;
222
223 case VgTs_Zombie:
224 prpsinfo->pr_sname = 'Z';
225 break;
226
227 case VgTs_Empty:
228 case VgTs_Init:
229 prpsinfo->pr_sname = '?';
230 break;
231 }
232
233 prpsinfo->pr_uid = 0;
234 prpsinfo->pr_gid = 0;
235
tomf4c23102005-10-31 17:05:21 +0000236 if (VG_(resolve_filename)(VG_(cl_exec_fd), name, VKI_PATH_MAX)) {
njn67229832005-08-28 04:38:12 +0000237 Char *n = name+VG_(strlen)(name)-1;
238
239 while (n > name && *n != '/')
240 n--;
241 if (n != name)
242 n++;
243
244 VG_(strncpy)(prpsinfo->pr_fname, n, sizeof(prpsinfo->pr_fname));
245 }
246}
247
248static void fill_prstatus(const ThreadState *tst,
249 struct vki_elf_prstatus *prs,
250 const vki_siginfo_t *si)
251{
252 struct vki_user_regs_struct *regs;
253
254 VG_(memset)(prs, 0, sizeof(*prs));
255
256 prs->pr_info.si_signo = si->si_signo;
257 prs->pr_info.si_code = si->si_code;
258 prs->pr_info.si_errno = 0;
259
260 prs->pr_cursig = si->si_signo;
261
262 prs->pr_pid = tst->os_state.lwpid;
263 prs->pr_ppid = 0;
264 prs->pr_pgrp = VG_(getpgrp)();
265 prs->pr_sid = VG_(getpgrp)();
266
267 regs = (struct vki_user_regs_struct *)prs->pr_reg;
268
269 vg_assert(sizeof(*regs) == sizeof(prs->pr_reg));
270
tomf4c23102005-10-31 17:05:21 +0000271 ML_(fill_elfregs_from_tst)(regs, &tst->arch);
njn67229832005-08-28 04:38:12 +0000272}
273
274static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu)
275{
tomf4c23102005-10-31 17:05:21 +0000276 ML_(fill_elffpregs_from_tst)(fpu, &tst->arch);
njn67229832005-08-28 04:38:12 +0000277}
278
tomf4c23102005-10-31 17:05:21 +0000279#if defined(VGP_x86_linux)
njn67229832005-08-28 04:38:12 +0000280static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu)
281{
tomf4c23102005-10-31 17:05:21 +0000282 ML_(fill_elffpxregs_from_tst)(xfpu, &tst->arch);
njn67229832005-08-28 04:38:12 +0000283}
tomf4c23102005-10-31 17:05:21 +0000284#endif
njn67229832005-08-28 04:38:12 +0000285
njnad4e18d2005-11-07 04:52:41 +0000286static
287void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
njn67229832005-08-28 04:38:12 +0000288{
289 Char buf[1000];
290 Char *basename = "vgcore";
291 Char *coreext = "";
292 Int seq = 0;
293 Int core_fd;
sewardje8089302006-10-17 02:15:17 +0000294 NSegment const * seg;
tomf4c23102005-10-31 17:05:21 +0000295 ESZ(Ehdr) ehdr;
296 ESZ(Phdr) *phdrs;
njn67229832005-08-28 04:38:12 +0000297 Int num_phdrs;
298 Int i, idx;
299 UInt off;
300 struct note *notelist, *note;
301 UInt notesz;
302 struct vki_elf_prpsinfo prpsinfo;
303 struct vki_elf_prstatus prstatus;
tomf4c23102005-10-31 17:05:21 +0000304 Addr *seg_starts;
305 Int n_seg_starts;
njn67229832005-08-28 04:38:12 +0000306
307 if (VG_(clo_log_name) != NULL) {
308 coreext = ".core";
309 basename = VG_(clo_log_name);
310 }
311
312 for(;;) {
tomf4c23102005-10-31 17:05:21 +0000313 SysRes sres;
314
njn67229832005-08-28 04:38:12 +0000315 if (seq == 0)
njn86f64bd2005-11-18 17:12:26 +0000316 VG_(sprintf)(buf, "%s%s.%d",
njn67229832005-08-28 04:38:12 +0000317 basename, coreext, VG_(getpid)());
318 else
njn86f64bd2005-11-18 17:12:26 +0000319 VG_(sprintf)(buf, "%s%s.%d.%d",
njn67229832005-08-28 04:38:12 +0000320 basename, coreext, VG_(getpid)(), seq);
321 seq++;
322
tomf4c23102005-10-31 17:05:21 +0000323 sres = VG_(open)(buf,
324 VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC,
325 VKI_S_IRUSR|VKI_S_IWUSR);
326 if (!sres.isError) {
sewardje8089302006-10-17 02:15:17 +0000327 core_fd = sres.res;
njn67229832005-08-28 04:38:12 +0000328 break;
tomf4c23102005-10-31 17:05:21 +0000329 }
njn67229832005-08-28 04:38:12 +0000330
sewardje8089302006-10-17 02:15:17 +0000331 if (sres.isError && sres.err != VKI_EEXIST)
njn67229832005-08-28 04:38:12 +0000332 return; /* can't create file */
333 }
334
tomf4c23102005-10-31 17:05:21 +0000335 /* Get the segments */
336 seg_starts = get_seg_starts(&n_seg_starts);
337
njn67229832005-08-28 04:38:12 +0000338 /* First, count how many memory segments to dump */
339 num_phdrs = 1; /* start with notes */
tomf4c23102005-10-31 17:05:21 +0000340 for(i = 0; i < n_seg_starts; i++) {
341 if (!may_dump(VG_(am_find_nsegment(seg_starts[i]))))
njn67229832005-08-28 04:38:12 +0000342 continue;
343
344 num_phdrs++;
345 }
346
347 fill_ehdr(&ehdr, num_phdrs);
348
349 notelist = NULL;
350
351 /* Second, work out their layout */
352 phdrs = VG_(arena_malloc)(VG_AR_CORE, sizeof(*phdrs) * num_phdrs);
353
354 for(i = 1; i < VG_N_THREADS; i++) {
355 vki_elf_fpregset_t fpu;
tomf4c23102005-10-31 17:05:21 +0000356#if defined(VGP_x86_linux)
njn67229832005-08-28 04:38:12 +0000357 vki_elf_fpxregset_t xfpu;
tomf4c23102005-10-31 17:05:21 +0000358#endif
njn67229832005-08-28 04:38:12 +0000359
360 if (VG_(threads)[i].status == VgTs_Empty)
361 continue;
362
tomf4c23102005-10-31 17:05:21 +0000363#if defined(VGP_x86_linux)
njn67229832005-08-28 04:38:12 +0000364 fill_xfpu(&VG_(threads)[i], &xfpu);
365 add_note(&notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu));
tomf4c23102005-10-31 17:05:21 +0000366#endif
njn67229832005-08-28 04:38:12 +0000367
368 fill_fpu(&VG_(threads)[i], &fpu);
369 add_note(&notelist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu));
370
371 fill_prstatus(&VG_(threads)[i], &prstatus, si);
372 add_note(&notelist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus));
373 }
374
375 fill_prpsinfo(&VG_(threads)[tid], &prpsinfo);
376 add_note(&notelist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
377
378 for(note = notelist, notesz = 0; note != NULL; note = note->next)
379 notesz += note_size(note);
380
381 off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs;
382
383 phdrs[0].p_type = PT_NOTE;
384 phdrs[0].p_offset = off;
385 phdrs[0].p_vaddr = 0;
386 phdrs[0].p_paddr = 0;
387 phdrs[0].p_filesz = notesz;
388 phdrs[0].p_memsz = 0;
389 phdrs[0].p_flags = 0;
390 phdrs[0].p_align = 0;
391
392 off += notesz;
393
394 off = VG_PGROUNDUP(off);
395
tomf4c23102005-10-31 17:05:21 +0000396 for(i = 0, idx = 1; i < n_seg_starts; i++) {
397 seg = VG_(am_find_nsegment(seg_starts[i]));
398
njn67229832005-08-28 04:38:12 +0000399 if (!may_dump(seg))
400 continue;
401
tomf4c23102005-10-31 17:05:21 +0000402 fill_phdr(&phdrs[idx], seg, off, (seg->end - seg->start + off) < max_size);
njn67229832005-08-28 04:38:12 +0000403
404 off += phdrs[idx].p_filesz;
405
406 idx++;
407 }
408
409 /* write everything out */
410 VG_(write)(core_fd, &ehdr, sizeof(ehdr));
411 VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs);
412
413 for(note = notelist; note != NULL; note = note->next)
414 write_note(core_fd, note);
415
416 VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET);
417
tomf4c23102005-10-31 17:05:21 +0000418 for(i = 0, idx = 1; i < n_seg_starts; i++) {
419 seg = VG_(am_find_nsegment(seg_starts[i]));
420
njn67229832005-08-28 04:38:12 +0000421 if (!should_dump(seg))
422 continue;
423
424 if (phdrs[idx].p_filesz > 0) {
425 Int ret;
426
427 vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET) == phdrs[idx].p_offset);
tomf4c23102005-10-31 17:05:21 +0000428 vg_assert(seg->end - seg->start >= phdrs[idx].p_filesz);
njn67229832005-08-28 04:38:12 +0000429
tomf4c23102005-10-31 17:05:21 +0000430 ret = VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz);
njn67229832005-08-28 04:38:12 +0000431 }
432 idx++;
433 }
434
tomf4c23102005-10-31 17:05:21 +0000435 VG_(free)(seg_starts);
njn67229832005-08-28 04:38:12 +0000436
tomf4c23102005-10-31 17:05:21 +0000437 VG_(close)(core_fd);
njn67229832005-08-28 04:38:12 +0000438}
439
njnad4e18d2005-11-07 04:52:41 +0000440void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
441{
442 make_elf_coredump(tid, si, max_size);
443}
444
njn67229832005-08-28 04:38:12 +0000445/*--------------------------------------------------------------------*/
446/*--- end ---*/
447/*--------------------------------------------------------------------*/