core_elf32.c revision 1.32.16.3 1 1.32.16.3 matt /* $NetBSD: core_elf32.c,v 1.32.16.3 2014/04/02 17:34:51 matt Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.1 thorpej /*
39 1.1 thorpej * core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
40 1.1 thorpej */
41 1.1 thorpej
42 1.1 thorpej #include <sys/cdefs.h>
43 1.32.16.3 matt __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.32.16.3 2014/04/02 17:34:51 matt Exp $");
44 1.32.16.3 matt
45 1.32.16.3 matt #ifdef _KERNEL_OPT
46 1.32.16.3 matt #include "opt_coredump.h"
47 1.32.16.3 matt #endif
48 1.1 thorpej
49 1.1 thorpej /* If not included by core_elf64.c, ELFSIZE won't be defined. */
50 1.1 thorpej #ifndef ELFSIZE
51 1.1 thorpej #define ELFSIZE 32
52 1.1 thorpej #endif
53 1.1 thorpej
54 1.1 thorpej #include <sys/param.h>
55 1.1 thorpej #include <sys/systm.h>
56 1.1 thorpej #include <sys/proc.h>
57 1.1 thorpej #include <sys/vnode.h>
58 1.18 matt #include <sys/exec.h>
59 1.1 thorpej #include <sys/exec_elf.h>
60 1.1 thorpej #include <sys/ptrace.h>
61 1.16 matt #include <sys/malloc.h>
62 1.25 elad #include <sys/kauth.h>
63 1.1 thorpej
64 1.1 thorpej #include <machine/reg.h>
65 1.1 thorpej
66 1.2 thorpej #include <uvm/uvm_extern.h>
67 1.2 thorpej
68 1.2 thorpej struct countsegs_state {
69 1.2 thorpej int npsections;
70 1.2 thorpej };
71 1.2 thorpej
72 1.22 thorpej static int ELFNAMEEND(coredump_countsegs)(struct proc *, void *,
73 1.22 thorpej struct uvm_coredump_state *);
74 1.2 thorpej
75 1.2 thorpej struct writesegs_state {
76 1.16 matt Elf_Phdr *psections;
77 1.2 thorpej off_t secoff;
78 1.2 thorpej };
79 1.2 thorpej
80 1.22 thorpej static int ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *,
81 1.22 thorpej struct uvm_coredump_state *);
82 1.22 thorpej
83 1.22 thorpej static int ELFNAMEEND(coredump_notes)(struct proc *, struct lwp *, void *,
84 1.22 thorpej size_t *);
85 1.22 thorpej static int ELFNAMEEND(coredump_note)(struct proc *, struct lwp *, void *,
86 1.22 thorpej size_t *);
87 1.1 thorpej
88 1.1 thorpej #define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */
89 1.1 thorpej #define elfround(x) roundup((x), ELFROUNDSIZE)
90 1.1 thorpej
91 1.23 cube #define elf_process_read_regs CONCAT(process_read_regs, ELFSIZE)
92 1.32.16.2 matt #ifdef __HAVE_PROCESS_XFPREGS
93 1.32.16.2 matt #define elf_process_read_xfpregs CONCAT(process_read_xfpregs, ELFSIZE)
94 1.32.16.2 matt #else
95 1.23 cube #define elf_process_read_fpregs CONCAT(process_read_fpregs, ELFSIZE)
96 1.32.16.2 matt #endif
97 1.23 cube #define elf_reg CONCAT(process_reg, ELFSIZE)
98 1.23 cube #define elf_fpreg CONCAT(process_fpreg, ELFSIZE)
99 1.23 cube
100 1.1 thorpej int
101 1.18 matt ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
102 1.1 thorpej {
103 1.4 thorpej struct proc *p;
104 1.1 thorpej Elf_Ehdr ehdr;
105 1.32.16.3 matt Elf_Shdr shdr;
106 1.32.16.3 matt Elf_Phdr *psections;
107 1.2 thorpej struct countsegs_state cs;
108 1.2 thorpej struct writesegs_state ws;
109 1.16 matt off_t notestart, secstart, offset;
110 1.6 matt size_t notesize;
111 1.16 matt int error, i;
112 1.1 thorpej
113 1.16 matt psections = NULL;
114 1.4 thorpej p = l->l_proc;
115 1.1 thorpej /*
116 1.1 thorpej * We have to make a total of 3 passes across the map:
117 1.1 thorpej *
118 1.1 thorpej * 1. Count the number of map entries (the number of
119 1.1 thorpej * PT_LOAD sections).
120 1.1 thorpej *
121 1.1 thorpej * 2. Write the P-section headers.
122 1.1 thorpej *
123 1.1 thorpej * 3. Write the P-sections.
124 1.1 thorpej */
125 1.1 thorpej
126 1.1 thorpej /* Pass 1: count the entries. */
127 1.2 thorpej cs.npsections = 0;
128 1.18 matt error = uvm_coredump_walkmap(p, NULL,
129 1.2 thorpej ELFNAMEEND(coredump_countsegs), &cs);
130 1.2 thorpej if (error)
131 1.16 matt goto out;
132 1.16 matt
133 1.16 matt /* Count the PT_NOTE section. */
134 1.16 matt cs.npsections++;
135 1.1 thorpej
136 1.1 thorpej /* Get the size of the notes. */
137 1.18 matt error = ELFNAMEEND(coredump_notes)(p, l, NULL, ¬esize);
138 1.1 thorpej if (error)
139 1.16 matt goto out;
140 1.1 thorpej
141 1.18 matt memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
142 1.1 thorpej memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
143 1.1 thorpej #if ELFSIZE == 32
144 1.1 thorpej ehdr.e_ident[EI_CLASS] = ELFCLASS32;
145 1.1 thorpej #elif ELFSIZE == 64
146 1.1 thorpej ehdr.e_ident[EI_CLASS] = ELFCLASS64;
147 1.1 thorpej #endif
148 1.1 thorpej ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
149 1.1 thorpej ehdr.e_ident[EI_VERSION] = EV_CURRENT;
150 1.1 thorpej /* XXX Should be the OSABI/ABI version of the executable. */
151 1.1 thorpej ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
152 1.2 thorpej ehdr.e_ident[EI_ABIVERSION] = 0;
153 1.1 thorpej
154 1.1 thorpej ehdr.e_type = ET_CORE;
155 1.1 thorpej /* XXX This should be the e_machine of the executable. */
156 1.1 thorpej ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
157 1.1 thorpej ehdr.e_version = EV_CURRENT;
158 1.1 thorpej ehdr.e_entry = 0;
159 1.1 thorpej ehdr.e_flags = 0;
160 1.1 thorpej ehdr.e_ehsize = sizeof(ehdr);
161 1.1 thorpej ehdr.e_phentsize = sizeof(Elf_Phdr);
162 1.32.16.3 matt if (cs.npsections < PN_XNUM) {
163 1.32.16.3 matt ehdr.e_phnum = cs.npsections;
164 1.32.16.3 matt ehdr.e_shentsize = 0;
165 1.32.16.3 matt ehdr.e_shnum = 0;
166 1.32.16.3 matt ehdr.e_shoff = 0;
167 1.32.16.3 matt ehdr.e_phoff = sizeof(ehdr);
168 1.32.16.3 matt } else {
169 1.32.16.3 matt ehdr.e_phnum = PN_XNUM;
170 1.32.16.3 matt ehdr.e_shentsize = sizeof(Elf_Shdr);
171 1.32.16.3 matt ehdr.e_shnum = 1;
172 1.32.16.3 matt ehdr.e_shoff = sizeof(ehdr);
173 1.32.16.3 matt ehdr.e_phoff = sizeof(ehdr) + sizeof(shdr);
174 1.32.16.3 matt }
175 1.1 thorpej ehdr.e_shstrndx = 0;
176 1.1 thorpej
177 1.32.16.1 matt #ifdef ELF_MD_COREDUMP_SETUP
178 1.32.16.1 matt ELF_MD_COREDUMP_SETUP(l, &ehdr);
179 1.32.16.1 matt #endif
180 1.32.16.1 matt
181 1.1 thorpej /* Write out the ELF header. */
182 1.18 matt error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
183 1.16 matt if (error)
184 1.16 matt goto out;
185 1.16 matt
186 1.32.16.3 matt /* Write out sections, if needed */
187 1.32.16.3 matt if (cs.npsections >= PN_XNUM) {
188 1.32.16.3 matt memset(&shdr, 0, sizeof(shdr));
189 1.32.16.3 matt shdr.sh_type = SHT_NULL;
190 1.32.16.3 matt shdr.sh_info = cs.npsections;
191 1.32.16.3 matt error = coredump_write(cookie, UIO_SYSSPACE, &shdr,
192 1.32.16.3 matt sizeof(shdr));
193 1.32.16.3 matt if (error)
194 1.32.16.3 matt goto out;
195 1.32.16.3 matt }
196 1.32.16.3 matt
197 1.32.16.3 matt offset = ehdr.e_phoff;
198 1.1 thorpej
199 1.32.16.3 matt notestart = offset + sizeof(Elf_Phdr) * cs.npsections;
200 1.16 matt secstart = notestart + notesize;
201 1.16 matt
202 1.16 matt psections = malloc(cs.npsections * sizeof(Elf_Phdr),
203 1.16 matt M_TEMP, M_WAITOK|M_ZERO);
204 1.1 thorpej
205 1.5 atatat /* Pass 2: now write the P-section headers. */
206 1.2 thorpej ws.secoff = secstart;
207 1.16 matt ws.psections = psections;
208 1.18 matt error = uvm_coredump_walkmap(p, cookie,
209 1.2 thorpej ELFNAMEEND(coredump_writeseghdrs), &ws);
210 1.2 thorpej if (error)
211 1.16 matt goto out;
212 1.1 thorpej
213 1.1 thorpej /* Write out the PT_NOTE header. */
214 1.16 matt ws.psections->p_type = PT_NOTE;
215 1.16 matt ws.psections->p_offset = notestart;
216 1.16 matt ws.psections->p_vaddr = 0;
217 1.16 matt ws.psections->p_paddr = 0;
218 1.16 matt ws.psections->p_filesz = notesize;
219 1.16 matt ws.psections->p_memsz = 0;
220 1.16 matt ws.psections->p_flags = PF_R;
221 1.16 matt ws.psections->p_align = ELFROUNDSIZE;
222 1.1 thorpej
223 1.18 matt error = coredump_write(cookie, UIO_SYSSPACE, psections,
224 1.18 matt cs.npsections * sizeof(Elf_Phdr));
225 1.1 thorpej if (error)
226 1.16 matt goto out;
227 1.1 thorpej
228 1.18 matt #ifdef DIAGNOSTIC
229 1.16 matt offset += cs.npsections * sizeof(Elf_Phdr);
230 1.16 matt if (offset != notestart)
231 1.2 thorpej panic("coredump: offset %lld != notestart %lld",
232 1.16 matt (long long) offset, (long long) notestart);
233 1.2 thorpej #endif
234 1.1 thorpej
235 1.1 thorpej /* Write out the notes. */
236 1.18 matt error = ELFNAMEEND(coredump_notes)(p, l, cookie, ¬esize);
237 1.16 matt if (error)
238 1.16 matt goto out;
239 1.2 thorpej
240 1.2 thorpej #ifdef DIAGNOSTIC
241 1.16 matt offset += notesize;
242 1.16 matt if (offset != secstart)
243 1.2 thorpej panic("coredump: offset %lld != secstart %lld",
244 1.16 matt (long long) offset, (long long) secstart);
245 1.2 thorpej #endif
246 1.2 thorpej
247 1.5 atatat /* Pass 3: finally, write the sections themselves. */
248 1.16 matt for (i = 0; i < cs.npsections - 1; i++) {
249 1.16 matt if (psections[i].p_filesz == 0)
250 1.16 matt continue;
251 1.16 matt
252 1.16 matt #ifdef DIAGNOSTIC
253 1.16 matt if (offset != psections[i].p_offset)
254 1.16 matt panic("coredump: offset %lld != p_offset[%d] %lld",
255 1.16 matt (long long) offset, i,
256 1.16 matt (long long) psections[i].p_filesz);
257 1.16 matt #endif
258 1.16 matt
259 1.18 matt error = coredump_write(cookie, UIO_USERSPACE,
260 1.18 matt (void *)(vaddr_t)psections[i].p_vaddr,
261 1.18 matt psections[i].p_filesz);
262 1.16 matt if (error)
263 1.16 matt goto out;
264 1.16 matt
265 1.16 matt #ifdef DIAGNOSTIC
266 1.16 matt offset += psections[i].p_filesz;
267 1.16 matt #endif
268 1.16 matt }
269 1.1 thorpej
270 1.16 matt out:
271 1.16 matt if (psections)
272 1.16 matt free(psections, M_TEMP);
273 1.2 thorpej return (error);
274 1.2 thorpej }
275 1.2 thorpej
276 1.22 thorpej static int
277 1.28 yamt ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie,
278 1.18 matt struct uvm_coredump_state *us)
279 1.2 thorpej {
280 1.2 thorpej struct countsegs_state *cs = us->cookie;
281 1.1 thorpej
282 1.2 thorpej cs->npsections++;
283 1.2 thorpej return (0);
284 1.2 thorpej }
285 1.2 thorpej
286 1.22 thorpej static int
287 1.28 yamt ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie,
288 1.18 matt struct uvm_coredump_state *us)
289 1.2 thorpej {
290 1.2 thorpej struct writesegs_state *ws = us->cookie;
291 1.2 thorpej Elf_Phdr phdr;
292 1.16 matt vsize_t size, realsize;
293 1.16 matt vaddr_t end;
294 1.2 thorpej int error;
295 1.2 thorpej
296 1.2 thorpej size = us->end - us->start;
297 1.16 matt realsize = us->realend - us->start;
298 1.16 matt end = us->realend;
299 1.16 matt
300 1.16 matt while (realsize > 0) {
301 1.16 matt long buf[1024 / sizeof(long)];
302 1.16 matt size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
303 1.16 matt const long *ep;
304 1.16 matt int i;
305 1.16 matt
306 1.16 matt end -= slen;
307 1.21 christos if ((error = copyin_proc(p, (void *)end, buf, slen)) != 0)
308 1.21 christos return error;
309 1.16 matt
310 1.16 matt ep = (const long *) &buf[slen / sizeof(buf[0])];
311 1.16 matt for (i = 0, ep--; buf <= ep; ep--, i++) {
312 1.16 matt if (*ep)
313 1.16 matt break;
314 1.16 matt }
315 1.16 matt realsize -= i * sizeof(buf[0]);
316 1.16 matt if (i * sizeof(buf[0]) < slen)
317 1.16 matt break;
318 1.16 matt }
319 1.2 thorpej
320 1.2 thorpej phdr.p_type = PT_LOAD;
321 1.2 thorpej phdr.p_offset = ws->secoff;
322 1.2 thorpej phdr.p_vaddr = us->start;
323 1.2 thorpej phdr.p_paddr = 0;
324 1.16 matt phdr.p_filesz = realsize;
325 1.2 thorpej phdr.p_memsz = size;
326 1.2 thorpej phdr.p_flags = 0;
327 1.2 thorpej if (us->prot & VM_PROT_READ)
328 1.2 thorpej phdr.p_flags |= PF_R;
329 1.2 thorpej if (us->prot & VM_PROT_WRITE)
330 1.2 thorpej phdr.p_flags |= PF_W;
331 1.2 thorpej if (us->prot & VM_PROT_EXECUTE)
332 1.2 thorpej phdr.p_flags |= PF_X;
333 1.2 thorpej phdr.p_align = PAGE_SIZE;
334 1.2 thorpej
335 1.2 thorpej ws->secoff += phdr.p_filesz;
336 1.16 matt *ws->psections++ = phdr;
337 1.1 thorpej
338 1.1 thorpej return (0);
339 1.1 thorpej }
340 1.1 thorpej
341 1.22 thorpej static int
342 1.18 matt ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l,
343 1.18 matt void *iocookie, size_t *sizep)
344 1.1 thorpej {
345 1.1 thorpej struct netbsd_elfcore_procinfo cpi;
346 1.1 thorpej Elf_Nhdr nhdr;
347 1.6 matt size_t size, notesize;
348 1.6 matt int error;
349 1.6 matt struct lwp *l0;
350 1.29 ad sigset_t ss1, ss2;
351 1.1 thorpej
352 1.1 thorpej size = 0;
353 1.1 thorpej
354 1.1 thorpej /* First, write an elfcore_procinfo. */
355 1.1 thorpej notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
356 1.1 thorpej elfround(sizeof(cpi));
357 1.18 matt if (iocookie) {
358 1.1 thorpej cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
359 1.1 thorpej cpi.cpi_cpisize = sizeof(cpi);
360 1.12 christos cpi.cpi_signo = p->p_sigctx.ps_signo;
361 1.12 christos cpi.cpi_sigcode = p->p_sigctx.ps_code;
362 1.8 nathanw cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
363 1.1 thorpej
364 1.29 ad /*
365 1.29 ad * XXX This should be per-LWP.
366 1.29 ad */
367 1.29 ad ss1 = p->p_sigpend.sp_set;
368 1.29 ad sigemptyset(&ss2);
369 1.29 ad LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
370 1.29 ad sigplusset(&l0->l_sigpend.sp_set, &ss1);
371 1.29 ad sigplusset(&l0->l_sigmask, &ss2);
372 1.29 ad }
373 1.29 ad memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
374 1.29 ad memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
375 1.1 thorpej memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
376 1.1 thorpej sizeof(cpi.cpi_sigignore));
377 1.1 thorpej memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
378 1.1 thorpej sizeof(cpi.cpi_sigcatch));
379 1.1 thorpej
380 1.1 thorpej cpi.cpi_pid = p->p_pid;
381 1.32 ad mutex_enter(proc_lock);
382 1.1 thorpej cpi.cpi_ppid = p->p_pptr->p_pid;
383 1.1 thorpej cpi.cpi_pgrp = p->p_pgid;
384 1.1 thorpej cpi.cpi_sid = p->p_session->s_sid;
385 1.32 ad mutex_exit(proc_lock);
386 1.1 thorpej
387 1.26 ad cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
388 1.26 ad cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
389 1.26 ad cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
390 1.26 ad
391 1.26 ad cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
392 1.26 ad cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
393 1.26 ad cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
394 1.1 thorpej
395 1.4 thorpej cpi.cpi_nlwps = p->p_nlwps;
396 1.24 christos (void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
397 1.24 christos cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
398 1.1 thorpej
399 1.1 thorpej nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
400 1.1 thorpej nhdr.n_descsz = sizeof(cpi);
401 1.1 thorpej nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
402 1.1 thorpej
403 1.18 matt error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
404 1.18 matt ELF_NOTE_NETBSD_CORE_NAME "\0\0\0", &cpi);
405 1.1 thorpej if (error)
406 1.1 thorpej return (error);
407 1.1 thorpej }
408 1.1 thorpej
409 1.1 thorpej size += notesize;
410 1.1 thorpej
411 1.1 thorpej /* XXX Add hook for machdep per-proc notes. */
412 1.1 thorpej
413 1.1 thorpej /*
414 1.6 matt * Now write the register info for the thread that caused the
415 1.6 matt * coredump.
416 1.6 matt */
417 1.18 matt error = ELFNAMEEND(coredump_note)(p, l, iocookie, ¬esize);
418 1.6 matt if (error)
419 1.6 matt return (error);
420 1.6 matt size += notesize;
421 1.6 matt
422 1.6 matt /*
423 1.1 thorpej * Now, for each LWP, write the register info and any other
424 1.29 ad * per-LWP notes. Since we're dumping core, we don't bother
425 1.29 ad * locking.
426 1.1 thorpej */
427 1.6 matt LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
428 1.6 matt if (l0 == l) /* we've taken care of this thread */
429 1.6 matt continue;
430 1.18 matt error = ELFNAMEEND(coredump_note)(p, l0, iocookie, ¬esize);
431 1.6 matt if (error)
432 1.6 matt return (error);
433 1.1 thorpej size += notesize;
434 1.6 matt }
435 1.6 matt
436 1.6 matt *sizep = size;
437 1.6 matt return (0);
438 1.6 matt }
439 1.1 thorpej
440 1.22 thorpej static int
441 1.18 matt ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l, void *iocookie,
442 1.18 matt size_t *sizep)
443 1.6 matt {
444 1.6 matt Elf_Nhdr nhdr;
445 1.6 matt int size, notesize, error;
446 1.6 matt int namesize;
447 1.18 matt char name[64+ELFROUNDSIZE];
448 1.23 cube elf_reg intreg;
449 1.1 thorpej #ifdef PT_GETFPREGS
450 1.23 cube elf_fpreg freg;
451 1.6 matt #endif
452 1.6 matt
453 1.6 matt size = 0;
454 1.6 matt
455 1.18 matt snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
456 1.18 matt ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
457 1.6 matt namesize = strlen(name) + 1;
458 1.18 matt memset(name + namesize, 0, elfround(namesize) - namesize);
459 1.6 matt
460 1.6 matt notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
461 1.18 matt if (iocookie) {
462 1.31 ad uvm_lwp_hold(l);
463 1.23 cube error = elf_process_read_regs(l, &intreg);
464 1.31 ad uvm_lwp_rele(l);
465 1.6 matt if (error)
466 1.6 matt return (error);
467 1.6 matt
468 1.6 matt nhdr.n_namesz = namesize;
469 1.6 matt nhdr.n_descsz = sizeof(intreg);
470 1.6 matt nhdr.n_type = PT_GETREGS;
471 1.6 matt
472 1.18 matt error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
473 1.18 matt name, &intreg);
474 1.6 matt if (error)
475 1.6 matt return (error);
476 1.1 thorpej
477 1.4 thorpej }
478 1.6 matt size += notesize;
479 1.6 matt
480 1.6 matt #ifdef PT_GETFPREGS
481 1.6 matt notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
482 1.18 matt if (iocookie) {
483 1.32.16.2 matt size_t freglen = sizeof(freg);
484 1.31 ad uvm_lwp_hold(l);
485 1.32.16.2 matt #ifdef __HAVE_PROCESS_XFPREGS
486 1.32.16.2 matt error = elf_process_read_xfpregs(l, &freg, &freglen);
487 1.32.16.2 matt #else
488 1.23 cube error = elf_process_read_fpregs(l, &freg);
489 1.32.16.2 matt #endif
490 1.31 ad uvm_lwp_rele(l);
491 1.6 matt if (error)
492 1.6 matt return (error);
493 1.6 matt
494 1.6 matt nhdr.n_namesz = namesize;
495 1.32.16.2 matt nhdr.n_descsz = freglen;
496 1.6 matt nhdr.n_type = PT_GETFPREGS;
497 1.1 thorpej
498 1.18 matt error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
499 1.18 matt name, &freg);
500 1.6 matt if (error)
501 1.6 matt return (error);
502 1.6 matt }
503 1.6 matt size += notesize;
504 1.6 matt #endif
505 1.1 thorpej *sizep = size;
506 1.6 matt /* XXX Add hook for machdep per-LWP notes. */
507 1.1 thorpej return (0);
508 1.1 thorpej }
509 1.1 thorpej
510 1.1 thorpej int
511 1.28 yamt ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Nhdr *nhdr,
512 1.28 yamt const char *name, void *data)
513 1.1 thorpej {
514 1.1 thorpej int error;
515 1.1 thorpej
516 1.18 matt error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
517 1.1 thorpej if (error)
518 1.18 matt return error;
519 1.1 thorpej
520 1.18 matt error = coredump_write(cookie, UIO_SYSSPACE, name,
521 1.18 matt elfround(nhdr->n_namesz));
522 1.1 thorpej if (error)
523 1.18 matt return error;
524 1.1 thorpej
525 1.18 matt return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->n_descsz);
526 1.1 thorpej }
527