njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 3 | /*--- Dumping core. coredump-elf.c ---*/ |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame^] | 10 | Copyright (C) 2000-2008 Julian Seward |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 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 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 33 | #include "pub_core_aspacemgr.h" |
| 34 | #include "pub_core_libcbase.h" |
| 35 | #include "pub_core_machine.h" |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 36 | #include "pub_core_coredump.h" |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 37 | #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" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 43 | #include "pub_core_xarray.h" |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 44 | #include "pub_core_clientstate.h" |
| 45 | #include "pub_core_options.h" |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 46 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 47 | #include "priv_elf.h" |
| 48 | |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 49 | /* |
| 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 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 60 | #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 |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 69 | TODO: MERGE THIS WITH DUPLICATES IN m_main.c and mc_leakcheck.c |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 70 | 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 | |
| 75 | static Addr* get_seg_starts ( /*OUT*/Int* n_acquired ) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 76 | { |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 77 | 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 */ |
| 102 | static 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; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* If true, then this Segment's contents will be in the core */ |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 114 | static Bool should_dump(const NSegment *seg) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 115 | { |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 116 | return may_dump(seg); // && seg->hasW; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 119 | static void fill_ehdr(ESZ(Ehdr) *ehdr, Int num_phdrs) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 120 | { |
| 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; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 125 | ehdr->e_ident[EI_DATA] = VG_ELF_DATA2XXX; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 126 | 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; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 132 | ehdr->e_phoff = sizeof(ESZ(Ehdr)); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 133 | ehdr->e_shoff = 0; |
| 134 | ehdr->e_flags = 0; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 135 | ehdr->e_ehsize = sizeof(ESZ(Ehdr)); |
| 136 | ehdr->e_phentsize = sizeof(ESZ(Phdr)); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 137 | ehdr->e_phnum = num_phdrs; |
| 138 | ehdr->e_shentsize = 0; |
| 139 | ehdr->e_shnum = 0; |
| 140 | ehdr->e_shstrndx = 0; |
| 141 | |
| 142 | } |
| 143 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 144 | static void fill_phdr(ESZ(Phdr) *phdr, const NSegment *seg, UInt off, Bool write) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 145 | { |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 146 | SizeT len = seg->end - seg->start; |
| 147 | |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 148 | 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; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 154 | phdr->p_vaddr = seg->start; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 155 | phdr->p_paddr = 0; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 156 | phdr->p_filesz = write ? len : 0; |
| 157 | phdr->p_memsz = len; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 158 | phdr->p_flags = 0; |
| 159 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 160 | if (seg->hasR) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 161 | phdr->p_flags |= PF_R; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 162 | if (seg->hasW) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 163 | phdr->p_flags |= PF_W; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 164 | if (seg->hasX) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 165 | phdr->p_flags |= PF_X; |
| 166 | |
| 167 | phdr->p_align = VKI_PAGE_SIZE; |
| 168 | } |
| 169 | |
| 170 | struct note { |
| 171 | struct note *next; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 172 | ESZ(Nhdr) note; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 173 | Char name[0]; |
| 174 | }; |
| 175 | |
| 176 | static UInt note_size(const struct note *n) |
| 177 | { |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 178 | return sizeof(ESZ(Nhdr)) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4) + VG_ROUNDUP(n->note.n_descsz, 4); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static 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 | |
| 202 | static void write_note(Int fd, const struct note *n) |
| 203 | { |
| 204 | VG_(write)(fd, &n->note, note_size(n)); |
| 205 | } |
| 206 | |
| 207 | static void fill_prpsinfo(const ThreadState *tst, struct vki_elf_prpsinfo *prpsinfo) |
| 208 | { |
| 209 | static Char name[VKI_PATH_MAX]; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 210 | |
| 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 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 236 | if (VG_(resolve_filename)(VG_(cl_exec_fd), name, VKI_PATH_MAX)) { |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 237 | 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 | |
| 248 | static 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 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 271 | ML_(fill_elfregs_from_tst)(regs, &tst->arch); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu) |
| 275 | { |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 276 | ML_(fill_elffpregs_from_tst)(fpu, &tst->arch); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 277 | } |
| 278 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 279 | #if defined(VGP_x86_linux) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 280 | static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu) |
| 281 | { |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 282 | ML_(fill_elffpxregs_from_tst)(xfpu, &tst->arch); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 283 | } |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 284 | #endif |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 285 | |
njn | ad4e18d | 2005-11-07 04:52:41 +0000 | [diff] [blame] | 286 | static |
| 287 | void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, UInt max_size) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 288 | { |
| 289 | Char buf[1000]; |
| 290 | Char *basename = "vgcore"; |
| 291 | Char *coreext = ""; |
| 292 | Int seq = 0; |
| 293 | Int core_fd; |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame] | 294 | NSegment const * seg; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 295 | ESZ(Ehdr) ehdr; |
| 296 | ESZ(Phdr) *phdrs; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 297 | 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; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 304 | Addr *seg_starts; |
| 305 | Int n_seg_starts; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 306 | |
| 307 | if (VG_(clo_log_name) != NULL) { |
| 308 | coreext = ".core"; |
| 309 | basename = VG_(clo_log_name); |
| 310 | } |
| 311 | |
| 312 | for(;;) { |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 313 | SysRes sres; |
| 314 | |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 315 | if (seq == 0) |
njn | 86f64bd | 2005-11-18 17:12:26 +0000 | [diff] [blame] | 316 | VG_(sprintf)(buf, "%s%s.%d", |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 317 | basename, coreext, VG_(getpid)()); |
| 318 | else |
njn | 86f64bd | 2005-11-18 17:12:26 +0000 | [diff] [blame] | 319 | VG_(sprintf)(buf, "%s%s.%d.%d", |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 320 | basename, coreext, VG_(getpid)(), seq); |
| 321 | seq++; |
| 322 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 323 | 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) { |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame] | 327 | core_fd = sres.res; |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 328 | break; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 329 | } |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 330 | |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame] | 331 | if (sres.isError && sres.err != VKI_EEXIST) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 332 | return; /* can't create file */ |
| 333 | } |
| 334 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 335 | /* Get the segments */ |
| 336 | seg_starts = get_seg_starts(&n_seg_starts); |
| 337 | |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 338 | /* First, count how many memory segments to dump */ |
| 339 | num_phdrs = 1; /* start with notes */ |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 340 | for(i = 0; i < n_seg_starts; i++) { |
| 341 | if (!may_dump(VG_(am_find_nsegment(seg_starts[i])))) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 342 | 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; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 356 | #if defined(VGP_x86_linux) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 357 | vki_elf_fpxregset_t xfpu; |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 358 | #endif |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 359 | |
| 360 | if (VG_(threads)[i].status == VgTs_Empty) |
| 361 | continue; |
| 362 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 363 | #if defined(VGP_x86_linux) |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 364 | fill_xfpu(&VG_(threads)[i], &xfpu); |
| 365 | add_note(¬elist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu)); |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 366 | #endif |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 367 | |
| 368 | fill_fpu(&VG_(threads)[i], &fpu); |
| 369 | add_note(¬elist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu)); |
| 370 | |
| 371 | fill_prstatus(&VG_(threads)[i], &prstatus, si); |
| 372 | add_note(¬elist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus)); |
| 373 | } |
| 374 | |
| 375 | fill_prpsinfo(&VG_(threads)[tid], &prpsinfo); |
| 376 | add_note(¬elist, "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 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 396 | for(i = 0, idx = 1; i < n_seg_starts; i++) { |
| 397 | seg = VG_(am_find_nsegment(seg_starts[i])); |
| 398 | |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 399 | if (!may_dump(seg)) |
| 400 | continue; |
| 401 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 402 | fill_phdr(&phdrs[idx], seg, off, (seg->end - seg->start + off) < max_size); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 403 | |
| 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 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 418 | for(i = 0, idx = 1; i < n_seg_starts; i++) { |
| 419 | seg = VG_(am_find_nsegment(seg_starts[i])); |
| 420 | |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 421 | 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); |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 428 | vg_assert(seg->end - seg->start >= phdrs[idx].p_filesz); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 429 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 430 | ret = VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 431 | } |
| 432 | idx++; |
| 433 | } |
| 434 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 435 | VG_(free)(seg_starts); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 436 | |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 437 | VG_(close)(core_fd); |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 438 | } |
| 439 | |
njn | ad4e18d | 2005-11-07 04:52:41 +0000 | [diff] [blame] | 440 | void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, UInt max_size) |
| 441 | { |
| 442 | make_elf_coredump(tid, si, max_size); |
| 443 | } |
| 444 | |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 445 | /*--------------------------------------------------------------------*/ |
| 446 | /*--- end ---*/ |
| 447 | /*--------------------------------------------------------------------*/ |