Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.10.2.7
      1  1.10.2.7  christos /*	$NetBSD: core_elf32.c,v 1.10.2.7 2005/12/11 10:29:11 christos 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.10.2.7  christos __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.10.2.7 2005/12/11 10:29:11 christos Exp $");
     44       1.1   thorpej 
     45       1.1   thorpej /* If not included by core_elf64.c, ELFSIZE won't be defined. */
     46       1.1   thorpej #ifndef ELFSIZE
     47       1.1   thorpej #define	ELFSIZE		32
     48       1.1   thorpej #endif
     49       1.1   thorpej 
     50       1.1   thorpej #include <sys/param.h>
     51       1.1   thorpej #include <sys/systm.h>
     52       1.1   thorpej #include <sys/proc.h>
     53       1.1   thorpej #include <sys/vnode.h>
     54  1.10.2.6     skrll #include <sys/exec.h>
     55       1.1   thorpej #include <sys/exec_elf.h>
     56       1.1   thorpej #include <sys/ptrace.h>
     57  1.10.2.6     skrll #include <sys/malloc.h>
     58       1.1   thorpej 
     59       1.1   thorpej #include <machine/reg.h>
     60       1.1   thorpej 
     61       1.2   thorpej #include <uvm/uvm_extern.h>
     62       1.2   thorpej 
     63       1.2   thorpej struct countsegs_state {
     64       1.2   thorpej 	int	npsections;
     65       1.2   thorpej };
     66       1.2   thorpej 
     67  1.10.2.7  christos static int	ELFNAMEEND(coredump_countsegs)(struct proc *, void *,
     68  1.10.2.7  christos 		    struct uvm_coredump_state *);
     69       1.2   thorpej 
     70       1.2   thorpej struct writesegs_state {
     71  1.10.2.6     skrll 	Elf_Phdr *psections;
     72       1.2   thorpej 	off_t	secoff;
     73       1.2   thorpej };
     74       1.2   thorpej 
     75  1.10.2.7  christos static int	ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *,
     76  1.10.2.7  christos 		    struct uvm_coredump_state *);
     77  1.10.2.7  christos 
     78  1.10.2.7  christos static int	ELFNAMEEND(coredump_notes)(struct proc *, struct lwp *, void *,
     79  1.10.2.7  christos 		    size_t *);
     80  1.10.2.7  christos static int	ELFNAMEEND(coredump_note)(struct proc *, struct lwp *, void *,
     81  1.10.2.7  christos 		    size_t *);
     82       1.1   thorpej 
     83       1.1   thorpej #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
     84       1.1   thorpej #define	elfround(x)	roundup((x), ELFROUNDSIZE)
     85       1.1   thorpej 
     86       1.1   thorpej int
     87  1.10.2.6     skrll ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
     88       1.1   thorpej {
     89       1.4   thorpej 	struct proc *p;
     90       1.1   thorpej 	Elf_Ehdr ehdr;
     91  1.10.2.6     skrll 	Elf_Phdr phdr, *psections;
     92       1.2   thorpej 	struct countsegs_state cs;
     93       1.2   thorpej 	struct writesegs_state ws;
     94  1.10.2.6     skrll 	off_t notestart, secstart, offset;
     95       1.6      matt 	size_t notesize;
     96  1.10.2.6     skrll 	int error, i;
     97       1.1   thorpej 
     98  1.10.2.6     skrll 	psections = NULL;
     99       1.4   thorpej 	p = l->l_proc;
    100       1.1   thorpej 	/*
    101       1.1   thorpej 	 * We have to make a total of 3 passes across the map:
    102       1.1   thorpej 	 *
    103       1.1   thorpej 	 *	1. Count the number of map entries (the number of
    104       1.1   thorpej 	 *	   PT_LOAD sections).
    105       1.1   thorpej 	 *
    106       1.1   thorpej 	 *	2. Write the P-section headers.
    107       1.1   thorpej 	 *
    108       1.1   thorpej 	 *	3. Write the P-sections.
    109       1.1   thorpej 	 */
    110       1.1   thorpej 
    111       1.1   thorpej 	/* Pass 1: count the entries. */
    112       1.2   thorpej 	cs.npsections = 0;
    113  1.10.2.6     skrll 	error = uvm_coredump_walkmap(p, NULL,
    114       1.2   thorpej 	    ELFNAMEEND(coredump_countsegs), &cs);
    115       1.2   thorpej 	if (error)
    116  1.10.2.6     skrll 		goto out;
    117       1.1   thorpej 
    118       1.1   thorpej 	/* Count the PT_NOTE section. */
    119       1.2   thorpej 	cs.npsections++;
    120       1.1   thorpej 
    121  1.10.2.6     skrll 	/* Get the size of the notes. */
    122  1.10.2.6     skrll 	error = ELFNAMEEND(coredump_notes)(p, l, NULL, &notesize);
    123  1.10.2.6     skrll 	if (error)
    124  1.10.2.6     skrll 		goto out;
    125  1.10.2.6     skrll 
    126  1.10.2.6     skrll 	memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
    127       1.1   thorpej 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    128       1.1   thorpej #if ELFSIZE == 32
    129       1.1   thorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    130       1.1   thorpej #elif ELFSIZE == 64
    131       1.1   thorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    132       1.1   thorpej #endif
    133       1.1   thorpej 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    134       1.1   thorpej 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    135       1.1   thorpej 	/* XXX Should be the OSABI/ABI version of the executable. */
    136       1.1   thorpej 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    137       1.2   thorpej 	ehdr.e_ident[EI_ABIVERSION] = 0;
    138       1.1   thorpej 
    139       1.1   thorpej 	ehdr.e_type = ET_CORE;
    140       1.1   thorpej 	/* XXX This should be the e_machine of the executable. */
    141       1.1   thorpej 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    142       1.1   thorpej 	ehdr.e_version = EV_CURRENT;
    143       1.1   thorpej 	ehdr.e_entry = 0;
    144       1.1   thorpej 	ehdr.e_phoff = sizeof(ehdr);
    145       1.1   thorpej 	ehdr.e_shoff = 0;
    146       1.1   thorpej 	ehdr.e_flags = 0;
    147       1.1   thorpej 	ehdr.e_ehsize = sizeof(ehdr);
    148       1.1   thorpej 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    149       1.2   thorpej 	ehdr.e_phnum = cs.npsections;
    150       1.1   thorpej 	ehdr.e_shentsize = 0;
    151       1.1   thorpej 	ehdr.e_shnum = 0;
    152       1.1   thorpej 	ehdr.e_shstrndx = 0;
    153       1.1   thorpej 
    154       1.1   thorpej 	/* Write out the ELF header. */
    155  1.10.2.6     skrll 	error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
    156  1.10.2.6     skrll 	if (error)
    157  1.10.2.6     skrll 		goto out;
    158  1.10.2.6     skrll 
    159  1.10.2.6     skrll 	offset = sizeof(ehdr);
    160  1.10.2.6     skrll 
    161  1.10.2.6     skrll 	notestart = offset + sizeof(phdr) * cs.npsections;
    162  1.10.2.6     skrll 	secstart = notestart + notesize;
    163  1.10.2.6     skrll 
    164  1.10.2.6     skrll 	psections = malloc(cs.npsections * sizeof(Elf_Phdr),
    165  1.10.2.6     skrll 	    M_TEMP, M_WAITOK|M_ZERO);
    166       1.1   thorpej 
    167       1.5    atatat 	/* Pass 2: now write the P-section headers. */
    168       1.2   thorpej 	ws.secoff = secstart;
    169  1.10.2.6     skrll 	ws.psections = psections;
    170  1.10.2.6     skrll 	error = uvm_coredump_walkmap(p, cookie,
    171       1.2   thorpej 	    ELFNAMEEND(coredump_writeseghdrs), &ws);
    172       1.2   thorpej 	if (error)
    173  1.10.2.6     skrll 		goto out;
    174       1.1   thorpej 
    175       1.1   thorpej 	/* Write out the PT_NOTE header. */
    176  1.10.2.6     skrll 	ws.psections->p_type = PT_NOTE;
    177  1.10.2.6     skrll 	ws.psections->p_offset = notestart;
    178  1.10.2.6     skrll 	ws.psections->p_vaddr = 0;
    179  1.10.2.6     skrll 	ws.psections->p_paddr = 0;
    180  1.10.2.6     skrll 	ws.psections->p_filesz = notesize;
    181  1.10.2.6     skrll 	ws.psections->p_memsz = 0;
    182  1.10.2.6     skrll 	ws.psections->p_flags = PF_R;
    183  1.10.2.6     skrll 	ws.psections->p_align = ELFROUNDSIZE;
    184       1.1   thorpej 
    185  1.10.2.6     skrll 	error = coredump_write(cookie, UIO_SYSSPACE, psections,
    186  1.10.2.6     skrll 	    cs.npsections * sizeof(Elf_Phdr));
    187  1.10.2.6     skrll 	if (error)
    188  1.10.2.6     skrll 		goto out;
    189       1.1   thorpej 
    190       1.2   thorpej #ifdef DIAGNOSTIC
    191  1.10.2.6     skrll 	offset += cs.npsections * sizeof(Elf_Phdr);
    192  1.10.2.6     skrll 	if (offset != notestart)
    193       1.2   thorpej 		panic("coredump: offset %lld != notestart %lld",
    194  1.10.2.6     skrll 		    (long long) offset, (long long) notestart);
    195       1.2   thorpej #endif
    196       1.1   thorpej 
    197       1.1   thorpej 	/* Write out the notes. */
    198  1.10.2.6     skrll 	error = ELFNAMEEND(coredump_notes)(p, l, cookie, &notesize);
    199  1.10.2.6     skrll 	if (error)
    200  1.10.2.6     skrll 		goto out;
    201       1.2   thorpej 
    202       1.2   thorpej #ifdef DIAGNOSTIC
    203  1.10.2.6     skrll 	offset += notesize;
    204  1.10.2.6     skrll 	if (offset != secstart)
    205       1.2   thorpej 		panic("coredump: offset %lld != secstart %lld",
    206  1.10.2.6     skrll 		    (long long) offset, (long long) secstart);
    207       1.2   thorpej #endif
    208       1.2   thorpej 
    209       1.5    atatat 	/* Pass 3: finally, write the sections themselves. */
    210  1.10.2.6     skrll 	for (i = 0; i < cs.npsections - 1; i++) {
    211  1.10.2.6     skrll 		if (psections[i].p_filesz == 0)
    212  1.10.2.6     skrll 			continue;
    213  1.10.2.6     skrll 
    214  1.10.2.6     skrll #ifdef DIAGNOSTIC
    215  1.10.2.6     skrll 		if (offset != psections[i].p_offset)
    216  1.10.2.6     skrll 			panic("coredump: offset %lld != p_offset[%d] %lld",
    217  1.10.2.6     skrll 			    (long long) offset, i,
    218  1.10.2.6     skrll 			    (long long) psections[i].p_filesz);
    219  1.10.2.6     skrll #endif
    220  1.10.2.6     skrll 
    221  1.10.2.6     skrll 		error = coredump_write(cookie, UIO_USERSPACE,
    222  1.10.2.6     skrll 		    (void *)(vaddr_t)psections[i].p_vaddr,
    223  1.10.2.6     skrll 		    psections[i].p_filesz);
    224  1.10.2.6     skrll 		if (error)
    225  1.10.2.6     skrll 			goto out;
    226  1.10.2.6     skrll 
    227  1.10.2.6     skrll #ifdef DIAGNOSTIC
    228  1.10.2.6     skrll 		offset += psections[i].p_filesz;
    229  1.10.2.6     skrll #endif
    230  1.10.2.6     skrll 	}
    231       1.1   thorpej 
    232  1.10.2.6     skrll   out:
    233  1.10.2.6     skrll 	if (psections)
    234  1.10.2.6     skrll 		free(psections, M_TEMP);
    235       1.2   thorpej 	return (error);
    236       1.2   thorpej }
    237       1.2   thorpej 
    238  1.10.2.7  christos static int
    239  1.10.2.6     skrll ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie,
    240  1.10.2.6     skrll     struct uvm_coredump_state *us)
    241       1.2   thorpej {
    242       1.2   thorpej 	struct countsegs_state *cs = us->cookie;
    243       1.1   thorpej 
    244       1.2   thorpej 	cs->npsections++;
    245       1.2   thorpej 	return (0);
    246       1.2   thorpej }
    247       1.2   thorpej 
    248  1.10.2.7  christos static int
    249  1.10.2.6     skrll ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie,
    250  1.10.2.6     skrll     struct uvm_coredump_state *us)
    251       1.2   thorpej {
    252       1.2   thorpej 	struct writesegs_state *ws = us->cookie;
    253       1.2   thorpej 	Elf_Phdr phdr;
    254  1.10.2.6     skrll 	vsize_t size, realsize;
    255  1.10.2.6     skrll 	vaddr_t end;
    256       1.2   thorpej 	int error;
    257       1.2   thorpej 
    258       1.2   thorpej 	size = us->end - us->start;
    259  1.10.2.6     skrll 	realsize = us->realend - us->start;
    260  1.10.2.6     skrll 	end = us->realend;
    261  1.10.2.6     skrll 
    262  1.10.2.6     skrll 	while (realsize > 0) {
    263  1.10.2.6     skrll 		long buf[1024 / sizeof(long)];
    264  1.10.2.6     skrll 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
    265  1.10.2.6     skrll 		const long *ep;
    266  1.10.2.6     skrll 		int i;
    267  1.10.2.6     skrll 
    268  1.10.2.6     skrll 		end -= slen;
    269  1.10.2.6     skrll 		if ((error = copyin_proc(p, (void *)end, buf, slen)) != 0)
    270  1.10.2.6     skrll 			return error;
    271  1.10.2.6     skrll 
    272  1.10.2.6     skrll 		ep = (const long *) &buf[slen / sizeof(buf[0])];
    273  1.10.2.6     skrll 		for (i = 0, ep--; buf <= ep; ep--, i++) {
    274  1.10.2.6     skrll 			if (*ep)
    275  1.10.2.6     skrll 				break;
    276  1.10.2.6     skrll 		}
    277  1.10.2.6     skrll 		realsize -= i * sizeof(buf[0]);
    278  1.10.2.6     skrll 		if (i * sizeof(buf[0]) < slen)
    279  1.10.2.6     skrll 			break;
    280  1.10.2.6     skrll 	}
    281       1.2   thorpej 
    282       1.2   thorpej 	phdr.p_type = PT_LOAD;
    283       1.2   thorpej 	phdr.p_offset = ws->secoff;
    284       1.2   thorpej 	phdr.p_vaddr = us->start;
    285       1.2   thorpej 	phdr.p_paddr = 0;
    286  1.10.2.6     skrll 	phdr.p_filesz = realsize;
    287       1.2   thorpej 	phdr.p_memsz = size;
    288       1.2   thorpej 	phdr.p_flags = 0;
    289       1.2   thorpej 	if (us->prot & VM_PROT_READ)
    290       1.2   thorpej 		phdr.p_flags |= PF_R;
    291       1.2   thorpej 	if (us->prot & VM_PROT_WRITE)
    292       1.2   thorpej 		phdr.p_flags |= PF_W;
    293       1.2   thorpej 	if (us->prot & VM_PROT_EXECUTE)
    294       1.2   thorpej 		phdr.p_flags |= PF_X;
    295       1.2   thorpej 	phdr.p_align = PAGE_SIZE;
    296       1.2   thorpej 
    297       1.2   thorpej 	ws->secoff += phdr.p_filesz;
    298  1.10.2.6     skrll 	*ws->psections++ = phdr;
    299       1.2   thorpej 
    300       1.2   thorpej 	return (0);
    301       1.2   thorpej }
    302       1.2   thorpej 
    303  1.10.2.7  christos static int
    304  1.10.2.6     skrll ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l,
    305  1.10.2.6     skrll     void *iocookie, size_t *sizep)
    306       1.1   thorpej {
    307       1.1   thorpej 	struct netbsd_elfcore_procinfo cpi;
    308       1.1   thorpej 	Elf_Nhdr nhdr;
    309       1.6      matt 	size_t size, notesize;
    310       1.6      matt 	int error;
    311       1.6      matt 	struct lwp *l0;
    312       1.1   thorpej 
    313       1.1   thorpej 	size = 0;
    314       1.1   thorpej 
    315       1.1   thorpej 	/* First, write an elfcore_procinfo. */
    316       1.1   thorpej 	notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
    317       1.1   thorpej 	    elfround(sizeof(cpi));
    318  1.10.2.6     skrll 	if (iocookie) {
    319       1.1   thorpej 		cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    320       1.1   thorpej 		cpi.cpi_cpisize = sizeof(cpi);
    321  1.10.2.2     skrll 		cpi.cpi_signo = p->p_sigctx.ps_signo;
    322       1.1   thorpej 		cpi.cpi_sigcode = p->p_sigctx.ps_code;
    323       1.8   nathanw 		cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
    324       1.1   thorpej 
    325       1.1   thorpej 		memcpy(&cpi.cpi_sigpend, &p->p_sigctx.ps_siglist,
    326       1.1   thorpej 		    sizeof(cpi.cpi_sigpend));
    327       1.1   thorpej 		memcpy(&cpi.cpi_sigmask, &p->p_sigctx.ps_sigmask,
    328       1.1   thorpej 		    sizeof(cpi.cpi_sigmask));
    329       1.1   thorpej 		memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    330       1.1   thorpej 		    sizeof(cpi.cpi_sigignore));
    331       1.1   thorpej 		memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    332       1.1   thorpej 		    sizeof(cpi.cpi_sigcatch));
    333       1.1   thorpej 
    334       1.1   thorpej 		cpi.cpi_pid = p->p_pid;
    335       1.1   thorpej 		cpi.cpi_ppid = p->p_pptr->p_pid;
    336       1.1   thorpej 		cpi.cpi_pgrp = p->p_pgid;
    337       1.1   thorpej 		cpi.cpi_sid = p->p_session->s_sid;
    338       1.1   thorpej 
    339       1.1   thorpej 		cpi.cpi_ruid = p->p_cred->p_ruid;
    340       1.1   thorpej 		cpi.cpi_euid = p->p_ucred->cr_uid;
    341       1.1   thorpej 		cpi.cpi_svuid = p->p_cred->p_svuid;
    342       1.1   thorpej 
    343       1.1   thorpej 		cpi.cpi_rgid = p->p_cred->p_rgid;
    344       1.1   thorpej 		cpi.cpi_egid = p->p_ucred->cr_gid;
    345       1.1   thorpej 		cpi.cpi_svgid = p->p_cred->p_svgid;
    346       1.1   thorpej 
    347       1.4   thorpej 		cpi.cpi_nlwps = p->p_nlwps;
    348       1.7    itojun 		strlcpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
    349       1.1   thorpej 
    350       1.1   thorpej 		nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
    351       1.1   thorpej 		nhdr.n_descsz = sizeof(cpi);
    352       1.1   thorpej 		nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
    353       1.1   thorpej 
    354  1.10.2.6     skrll 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    355  1.10.2.6     skrll 		    ELF_NOTE_NETBSD_CORE_NAME "\0\0\0", &cpi);
    356       1.1   thorpej 		if (error)
    357       1.1   thorpej 			return (error);
    358       1.1   thorpej 	}
    359       1.1   thorpej 
    360       1.1   thorpej 	size += notesize;
    361       1.1   thorpej 
    362       1.1   thorpej 	/* XXX Add hook for machdep per-proc notes. */
    363       1.1   thorpej 
    364       1.1   thorpej 	/*
    365       1.6      matt 	 * Now write the register info for the thread that caused the
    366       1.6      matt 	 * coredump.
    367       1.6      matt 	 */
    368  1.10.2.6     skrll 	error = ELFNAMEEND(coredump_note)(p, l, iocookie, &notesize);
    369       1.6      matt 	if (error)
    370       1.6      matt 		return (error);
    371       1.6      matt 	size += notesize;
    372       1.6      matt 
    373       1.6      matt 	/*
    374       1.1   thorpej 	 * Now, for each LWP, write the register info and any other
    375       1.1   thorpej 	 * per-LWP notes.
    376       1.1   thorpej 	 */
    377       1.6      matt 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    378       1.6      matt 		if (l0 == l)		/* we've taken care of this thread */
    379       1.6      matt 			continue;
    380  1.10.2.6     skrll 		error = ELFNAMEEND(coredump_note)(p, l0, iocookie, &notesize);
    381       1.6      matt 		if (error)
    382       1.6      matt 			return (error);
    383       1.1   thorpej 		size += notesize;
    384       1.6      matt 	}
    385       1.6      matt 
    386       1.6      matt 	*sizep = size;
    387       1.6      matt 	return (0);
    388       1.6      matt }
    389       1.1   thorpej 
    390  1.10.2.7  christos static int
    391  1.10.2.6     skrll ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l, void *iocookie,
    392  1.10.2.6     skrll     size_t *sizep)
    393       1.6      matt {
    394       1.6      matt 	Elf_Nhdr nhdr;
    395       1.6      matt 	int size, notesize, error;
    396       1.6      matt 	int namesize;
    397  1.10.2.6     skrll 	char name[64+ELFROUNDSIZE];
    398       1.6      matt 	struct reg intreg;
    399       1.1   thorpej #ifdef PT_GETFPREGS
    400       1.6      matt 	struct fpreg freg;
    401       1.6      matt #endif
    402       1.6      matt 
    403       1.6      matt 	size = 0;
    404       1.6      matt 
    405  1.10.2.6     skrll 	snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
    406  1.10.2.6     skrll 	    ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
    407       1.6      matt 	namesize = strlen(name) + 1;
    408  1.10.2.6     skrll 	memset(name + namesize, 0, elfround(namesize) - namesize);
    409       1.6      matt 
    410       1.6      matt 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
    411  1.10.2.6     skrll 	if (iocookie) {
    412       1.6      matt 		PHOLD(l);
    413       1.6      matt 		error = process_read_regs(l, &intreg);
    414       1.6      matt 		PRELE(l);
    415       1.6      matt 		if (error)
    416       1.6      matt 			return (error);
    417       1.6      matt 
    418       1.6      matt 		nhdr.n_namesz = namesize;
    419       1.6      matt 		nhdr.n_descsz = sizeof(intreg);
    420       1.6      matt 		nhdr.n_type = PT_GETREGS;
    421       1.6      matt 
    422  1.10.2.6     skrll 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    423  1.10.2.6     skrll 		    name, &intreg);
    424       1.6      matt 		if (error)
    425       1.6      matt 			return (error);
    426       1.1   thorpej 
    427       1.4   thorpej 	}
    428       1.6      matt 	size += notesize;
    429       1.6      matt 
    430       1.6      matt #ifdef PT_GETFPREGS
    431       1.6      matt 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
    432  1.10.2.6     skrll 	if (iocookie) {
    433       1.6      matt 		PHOLD(l);
    434       1.6      matt 		error = process_read_fpregs(l, &freg);
    435       1.6      matt 		PRELE(l);
    436       1.6      matt 		if (error)
    437       1.6      matt 			return (error);
    438       1.6      matt 
    439       1.6      matt 		nhdr.n_namesz = namesize;
    440       1.6      matt 		nhdr.n_descsz = sizeof(freg);
    441       1.6      matt 		nhdr.n_type = PT_GETFPREGS;
    442       1.1   thorpej 
    443  1.10.2.6     skrll 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    444  1.10.2.6     skrll 		    name, &freg);
    445       1.6      matt 		if (error)
    446       1.6      matt 			return (error);
    447       1.6      matt 	}
    448       1.6      matt 	size += notesize;
    449       1.6      matt #endif
    450       1.1   thorpej 	*sizep = size;
    451       1.6      matt 	/* XXX Add hook for machdep per-LWP notes. */
    452       1.1   thorpej 	return (0);
    453       1.1   thorpej }
    454       1.1   thorpej 
    455       1.1   thorpej int
    456  1.10.2.6     skrll ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Nhdr *nhdr,
    457  1.10.2.6     skrll     const char *name, void *data)
    458       1.1   thorpej {
    459       1.1   thorpej 	int error;
    460       1.1   thorpej 
    461  1.10.2.6     skrll 	error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
    462       1.1   thorpej 	if (error)
    463  1.10.2.6     skrll 		return error;
    464       1.1   thorpej 
    465  1.10.2.6     skrll 	error = coredump_write(cookie, UIO_SYSSPACE, name,
    466  1.10.2.6     skrll 	    elfround(nhdr->n_namesz));
    467       1.1   thorpej 	if (error)
    468  1.10.2.6     skrll 		return error;
    469       1.1   thorpej 
    470  1.10.2.6     skrll 	return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->n_descsz);
    471       1.1   thorpej }
    472