Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.3.2.3
      1  1.3.2.3  nathanw /*	$NetBSD: core_elf32.c,v 1.3.2.3 2002/01/09 02:58:33 nathanw Exp $	*/
      2  1.3.2.2  nathanw 
      3  1.3.2.2  nathanw /*
      4  1.3.2.2  nathanw  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  1.3.2.2  nathanw  * All rights reserved.
      6  1.3.2.2  nathanw  *
      7  1.3.2.2  nathanw  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  1.3.2.2  nathanw  *
      9  1.3.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
     10  1.3.2.2  nathanw  * modification, are permitted provided that the following conditions
     11  1.3.2.2  nathanw  * are met:
     12  1.3.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     13  1.3.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     14  1.3.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.3.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     16  1.3.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     17  1.3.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     18  1.3.2.2  nathanw  *    must display the following acknowledgement:
     19  1.3.2.2  nathanw  *	This product includes software developed for the NetBSD Project by
     20  1.3.2.2  nathanw  *	Wasabi Systems, Inc.
     21  1.3.2.2  nathanw  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.3.2.2  nathanw  *    or promote products derived from this software without specific prior
     23  1.3.2.2  nathanw  *    written permission.
     24  1.3.2.2  nathanw  *
     25  1.3.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.3.2.2  nathanw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.3.2.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.3.2.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.3.2.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.3.2.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.3.2.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.3.2.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.3.2.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.3.2.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.3.2.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     36  1.3.2.2  nathanw  */
     37  1.3.2.2  nathanw 
     38  1.3.2.2  nathanw /*
     39  1.3.2.2  nathanw  * core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
     40  1.3.2.2  nathanw  */
     41  1.3.2.2  nathanw 
     42  1.3.2.2  nathanw #include <sys/cdefs.h>
     43  1.3.2.3  nathanw __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.3.2.3 2002/01/09 02:58:33 nathanw Exp $");
     44  1.3.2.2  nathanw 
     45  1.3.2.2  nathanw /* If not included by core_elf64.c, ELFSIZE won't be defined. */
     46  1.3.2.2  nathanw #ifndef ELFSIZE
     47  1.3.2.2  nathanw #define	ELFSIZE		32
     48  1.3.2.2  nathanw #endif
     49  1.3.2.2  nathanw 
     50  1.3.2.2  nathanw #include <sys/param.h>
     51  1.3.2.2  nathanw #include <sys/systm.h>
     52  1.3.2.3  nathanw #include <sys/lwp.h>
     53  1.3.2.2  nathanw #include <sys/proc.h>
     54  1.3.2.2  nathanw #include <sys/vnode.h>
     55  1.3.2.2  nathanw #include <sys/exec_elf.h>
     56  1.3.2.2  nathanw #include <sys/ptrace.h>
     57  1.3.2.2  nathanw 
     58  1.3.2.2  nathanw #include <machine/reg.h>
     59  1.3.2.2  nathanw 
     60  1.3.2.2  nathanw #include <uvm/uvm_extern.h>
     61  1.3.2.2  nathanw 
     62  1.3.2.2  nathanw struct countsegs_state {
     63  1.3.2.2  nathanw 	int	npsections;
     64  1.3.2.2  nathanw };
     65  1.3.2.2  nathanw 
     66  1.3.2.2  nathanw int	ELFNAMEEND(coredump_countsegs)(struct proc *, struct vnode *,
     67  1.3.2.2  nathanw 	    struct ucred *, struct uvm_coredump_state *);
     68  1.3.2.2  nathanw 
     69  1.3.2.2  nathanw struct writesegs_state {
     70  1.3.2.2  nathanw 	off_t	offset;
     71  1.3.2.2  nathanw 	off_t	secoff;
     72  1.3.2.2  nathanw };
     73  1.3.2.2  nathanw 
     74  1.3.2.2  nathanw int	ELFNAMEEND(coredump_writeseghdrs)(struct proc *, struct vnode *,
     75  1.3.2.2  nathanw 	    struct ucred *, struct uvm_coredump_state *);
     76  1.3.2.2  nathanw int	ELFNAMEEND(coredump_writesegs)(struct proc *, struct vnode *,
     77  1.3.2.2  nathanw 	    struct ucred *, struct uvm_coredump_state *);
     78  1.3.2.2  nathanw 
     79  1.3.2.2  nathanw int	ELFNAMEEND(coredump_notes)(struct proc *, struct vnode *,
     80  1.3.2.2  nathanw 	    struct ucred *, int *, off_t);
     81  1.3.2.2  nathanw 
     82  1.3.2.2  nathanw #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
     83  1.3.2.2  nathanw #define	elfround(x)	roundup((x), ELFROUNDSIZE)
     84  1.3.2.2  nathanw 
     85  1.3.2.2  nathanw int
     86  1.3.2.3  nathanw ELFNAMEEND(coredump)(struct lwp *l, struct vnode *vp, struct ucred *cred)
     87  1.3.2.2  nathanw {
     88  1.3.2.3  nathanw 	struct proc *p;
     89  1.3.2.2  nathanw 	Elf_Ehdr ehdr;
     90  1.3.2.2  nathanw 	Elf_Phdr phdr;
     91  1.3.2.2  nathanw 	struct countsegs_state cs;
     92  1.3.2.2  nathanw 	struct writesegs_state ws;
     93  1.3.2.2  nathanw 	off_t notestart, secstart;
     94  1.3.2.2  nathanw 	int notesize, error;
     95  1.3.2.2  nathanw 
     96  1.3.2.3  nathanw 	p = l->l_proc;
     97  1.3.2.2  nathanw 	/*
     98  1.3.2.2  nathanw 	 * We have to make a total of 3 passes across the map:
     99  1.3.2.2  nathanw 	 *
    100  1.3.2.2  nathanw 	 *	1. Count the number of map entries (the number of
    101  1.3.2.2  nathanw 	 *	   PT_LOAD sections).
    102  1.3.2.2  nathanw 	 *
    103  1.3.2.2  nathanw 	 *	2. Write the P-section headers.
    104  1.3.2.2  nathanw 	 *
    105  1.3.2.2  nathanw 	 *	3. Write the P-sections.
    106  1.3.2.2  nathanw 	 */
    107  1.3.2.2  nathanw 
    108  1.3.2.2  nathanw 	/* Pass 1: count the entries. */
    109  1.3.2.2  nathanw 	cs.npsections = 0;
    110  1.3.2.2  nathanw 	error = uvm_coredump_walkmap(p, vp, cred,
    111  1.3.2.2  nathanw 	    ELFNAMEEND(coredump_countsegs), &cs);
    112  1.3.2.2  nathanw 	if (error)
    113  1.3.2.2  nathanw 		return (error);
    114  1.3.2.2  nathanw 
    115  1.3.2.2  nathanw 	/* Get the size of the notes. */
    116  1.3.2.2  nathanw 	error = ELFNAMEEND(coredump_notes)(p, vp, cred, &notesize, 0);
    117  1.3.2.2  nathanw 	if (error)
    118  1.3.2.2  nathanw 		return (error);
    119  1.3.2.2  nathanw 
    120  1.3.2.2  nathanw 	/* Count the PT_NOTE section. */
    121  1.3.2.2  nathanw 	cs.npsections++;
    122  1.3.2.2  nathanw 
    123  1.3.2.2  nathanw 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    124  1.3.2.2  nathanw #if ELFSIZE == 32
    125  1.3.2.2  nathanw 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    126  1.3.2.2  nathanw #elif ELFSIZE == 64
    127  1.3.2.2  nathanw 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    128  1.3.2.2  nathanw #endif
    129  1.3.2.2  nathanw 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    130  1.3.2.2  nathanw 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    131  1.3.2.2  nathanw 	/* XXX Should be the OSABI/ABI version of the executable. */
    132  1.3.2.2  nathanw 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    133  1.3.2.2  nathanw 	ehdr.e_ident[EI_ABIVERSION] = 0;
    134  1.3.2.2  nathanw 
    135  1.3.2.2  nathanw 	ehdr.e_type = ET_CORE;
    136  1.3.2.2  nathanw 	/* XXX This should be the e_machine of the executable. */
    137  1.3.2.2  nathanw 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    138  1.3.2.2  nathanw 	ehdr.e_version = EV_CURRENT;
    139  1.3.2.2  nathanw 	ehdr.e_entry = 0;
    140  1.3.2.2  nathanw 	ehdr.e_phoff = sizeof(ehdr);
    141  1.3.2.2  nathanw 	ehdr.e_shoff = 0;
    142  1.3.2.2  nathanw 	ehdr.e_flags = 0;
    143  1.3.2.2  nathanw 	ehdr.e_ehsize = sizeof(ehdr);
    144  1.3.2.2  nathanw 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    145  1.3.2.2  nathanw 	ehdr.e_phnum = cs.npsections;
    146  1.3.2.2  nathanw 	ehdr.e_shentsize = 0;
    147  1.3.2.2  nathanw 	ehdr.e_shnum = 0;
    148  1.3.2.2  nathanw 	ehdr.e_shstrndx = 0;
    149  1.3.2.2  nathanw 
    150  1.3.2.2  nathanw 	/* Write out the ELF header. */
    151  1.3.2.2  nathanw 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&ehdr,
    152  1.3.2.2  nathanw 	    (int)sizeof(ehdr), (off_t)0,
    153  1.3.2.2  nathanw 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    154  1.3.2.2  nathanw 
    155  1.3.2.2  nathanw 	ws.offset = ehdr.e_phoff;
    156  1.3.2.2  nathanw 	notestart = ws.offset + (sizeof(phdr) * cs.npsections);
    157  1.3.2.2  nathanw 	secstart = round_page(notestart + notesize);
    158  1.3.2.2  nathanw 
    159  1.3.2.2  nathanw 	/* Now write the P-section headers. */
    160  1.3.2.2  nathanw 	ws.secoff = secstart;
    161  1.3.2.2  nathanw 	error = uvm_coredump_walkmap(p, vp, cred,
    162  1.3.2.2  nathanw 	    ELFNAMEEND(coredump_writeseghdrs), &ws);
    163  1.3.2.2  nathanw 	if (error)
    164  1.3.2.2  nathanw 		return (error);
    165  1.3.2.2  nathanw 
    166  1.3.2.2  nathanw 	/* Write out the PT_NOTE header. */
    167  1.3.2.2  nathanw 	phdr.p_type = PT_NOTE;
    168  1.3.2.2  nathanw 	phdr.p_offset = notestart;
    169  1.3.2.2  nathanw 	phdr.p_vaddr = 0;
    170  1.3.2.2  nathanw 	phdr.p_paddr = 0;
    171  1.3.2.2  nathanw 	phdr.p_filesz = notesize;
    172  1.3.2.2  nathanw 	phdr.p_memsz = 0;
    173  1.3.2.2  nathanw 	phdr.p_flags = PF_R;
    174  1.3.2.2  nathanw 	phdr.p_align = ELFROUNDSIZE;
    175  1.3.2.2  nathanw 
    176  1.3.2.2  nathanw 	error = vn_rdwr(UIO_WRITE, vp,
    177  1.3.2.2  nathanw 	    (caddr_t)&phdr, sizeof(phdr),
    178  1.3.2.2  nathanw 	    ws.offset, UIO_SYSSPACE,
    179  1.3.2.2  nathanw 	    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    180  1.3.2.2  nathanw 	if (error)
    181  1.3.2.2  nathanw 		return (error);
    182  1.3.2.2  nathanw 
    183  1.3.2.2  nathanw 	ws.offset += sizeof(phdr);
    184  1.3.2.2  nathanw 
    185  1.3.2.2  nathanw #ifdef DIAGNOSTIC
    186  1.3.2.2  nathanw 	if (ws.offset != notestart)
    187  1.3.2.2  nathanw 		panic("coredump: offset %lld != notestart %lld",
    188  1.3.2.2  nathanw 		    (long long) ws.offset, (long long) notestart);
    189  1.3.2.2  nathanw #endif
    190  1.3.2.2  nathanw 
    191  1.3.2.2  nathanw 	/* Write out the notes. */
    192  1.3.2.2  nathanw 	error = ELFNAMEEND(coredump_notes)(p, vp, cred, &notesize, ws.offset);
    193  1.3.2.2  nathanw 
    194  1.3.2.2  nathanw 	ws.offset += notesize;
    195  1.3.2.2  nathanw 
    196  1.3.2.2  nathanw #ifdef DIAGNOSTIC
    197  1.3.2.2  nathanw 	if (round_page(ws.offset) != secstart)
    198  1.3.2.2  nathanw 		panic("coredump: offset %lld != secstart %lld",
    199  1.3.2.2  nathanw 		    (long long) round_page(ws.offset), (long long) secstart);
    200  1.3.2.2  nathanw #endif
    201  1.3.2.2  nathanw 
    202  1.3.2.2  nathanw 	/* ...and finally, the sections themselves. */
    203  1.3.2.2  nathanw 	ws.secoff = secstart;
    204  1.3.2.2  nathanw 	error = uvm_coredump_walkmap(p, vp, cred,
    205  1.3.2.2  nathanw 	    ELFNAMEEND(coredump_writesegs), &ws);
    206  1.3.2.2  nathanw 	if (error)
    207  1.3.2.2  nathanw 		return (error);
    208  1.3.2.2  nathanw 
    209  1.3.2.2  nathanw 	return (error);
    210  1.3.2.2  nathanw }
    211  1.3.2.2  nathanw 
    212  1.3.2.2  nathanw int
    213  1.3.2.2  nathanw ELFNAMEEND(coredump_countsegs)(struct proc *p, struct vnode *vp,
    214  1.3.2.2  nathanw     struct ucred *cred, struct uvm_coredump_state *us)
    215  1.3.2.2  nathanw {
    216  1.3.2.2  nathanw 	struct countsegs_state *cs = us->cookie;
    217  1.3.2.2  nathanw 
    218  1.3.2.2  nathanw 	cs->npsections++;
    219  1.3.2.2  nathanw 	return (0);
    220  1.3.2.2  nathanw }
    221  1.3.2.2  nathanw 
    222  1.3.2.2  nathanw int
    223  1.3.2.2  nathanw ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, struct vnode *vp,
    224  1.3.2.2  nathanw     struct ucred *cred, struct uvm_coredump_state *us)
    225  1.3.2.2  nathanw {
    226  1.3.2.2  nathanw 	struct writesegs_state *ws = us->cookie;
    227  1.3.2.2  nathanw 	Elf_Phdr phdr;
    228  1.3.2.2  nathanw 	vsize_t size;
    229  1.3.2.2  nathanw 	int error;
    230  1.3.2.2  nathanw 
    231  1.3.2.2  nathanw 	size = us->end - us->start;
    232  1.3.2.2  nathanw 
    233  1.3.2.2  nathanw 	phdr.p_type = PT_LOAD;
    234  1.3.2.2  nathanw 	phdr.p_offset = ws->secoff;
    235  1.3.2.2  nathanw 	phdr.p_vaddr = us->start;
    236  1.3.2.2  nathanw 	phdr.p_paddr = 0;
    237  1.3.2.2  nathanw 	phdr.p_filesz = (us->flags & UVM_COREDUMP_NODUMP) ? 0 : size;
    238  1.3.2.2  nathanw 	phdr.p_memsz = size;
    239  1.3.2.2  nathanw 	phdr.p_flags = 0;
    240  1.3.2.2  nathanw 	if (us->prot & VM_PROT_READ)
    241  1.3.2.2  nathanw 		phdr.p_flags |= PF_R;
    242  1.3.2.2  nathanw 	if (us->prot & VM_PROT_WRITE)
    243  1.3.2.2  nathanw 		phdr.p_flags |= PF_W;
    244  1.3.2.2  nathanw 	if (us->prot & VM_PROT_EXECUTE)
    245  1.3.2.2  nathanw 		phdr.p_flags |= PF_X;
    246  1.3.2.2  nathanw 	phdr.p_align = PAGE_SIZE;
    247  1.3.2.2  nathanw 
    248  1.3.2.2  nathanw 	error = vn_rdwr(UIO_WRITE, vp,
    249  1.3.2.2  nathanw 	    (caddr_t)&phdr, sizeof(phdr),
    250  1.3.2.2  nathanw 	    ws->offset, UIO_SYSSPACE,
    251  1.3.2.2  nathanw 	    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    252  1.3.2.2  nathanw 	if (error)
    253  1.3.2.2  nathanw 		return (error);
    254  1.3.2.2  nathanw 
    255  1.3.2.2  nathanw 	ws->offset += sizeof(phdr);
    256  1.3.2.2  nathanw 	ws->secoff += phdr.p_filesz;
    257  1.3.2.2  nathanw 
    258  1.3.2.2  nathanw 	return (0);
    259  1.3.2.2  nathanw }
    260  1.3.2.2  nathanw 
    261  1.3.2.2  nathanw int
    262  1.3.2.2  nathanw ELFNAMEEND(coredump_writesegs)(struct proc *p, struct vnode *vp,
    263  1.3.2.2  nathanw     struct ucred *cred, struct uvm_coredump_state *us)
    264  1.3.2.2  nathanw {
    265  1.3.2.2  nathanw 	struct writesegs_state *ws = us->cookie;
    266  1.3.2.2  nathanw 	vsize_t size;
    267  1.3.2.2  nathanw 	int error;
    268  1.3.2.2  nathanw 
    269  1.3.2.2  nathanw 	if (us->flags & UVM_COREDUMP_NODUMP)
    270  1.3.2.2  nathanw 		return (0);
    271  1.3.2.2  nathanw 
    272  1.3.2.2  nathanw 	size = us->end - us->start;
    273  1.3.2.2  nathanw 
    274  1.3.2.2  nathanw 	error = vn_rdwr(UIO_WRITE, vp,
    275  1.3.2.2  nathanw 	    (caddr_t) us->start, size,
    276  1.3.2.2  nathanw 	    ws->secoff, UIO_USERSPACE,
    277  1.3.2.2  nathanw 	    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    278  1.3.2.2  nathanw 	if (error)
    279  1.3.2.2  nathanw 		return (error);
    280  1.3.2.2  nathanw 
    281  1.3.2.2  nathanw 	ws->secoff += size;
    282  1.3.2.2  nathanw 
    283  1.3.2.2  nathanw 	return (0);
    284  1.3.2.2  nathanw }
    285  1.3.2.2  nathanw 
    286  1.3.2.2  nathanw int
    287  1.3.2.2  nathanw ELFNAMEEND(coredump_notes)(struct proc *p, struct vnode *vp,
    288  1.3.2.2  nathanw     struct ucred *cred, int *sizep, off_t offset)
    289  1.3.2.2  nathanw {
    290  1.3.2.2  nathanw 	struct netbsd_elfcore_procinfo cpi;
    291  1.3.2.2  nathanw 	Elf_Nhdr nhdr;
    292  1.3.2.2  nathanw 	int size, notesize, error;
    293  1.3.2.2  nathanw 	char name[64];
    294  1.3.2.2  nathanw 	int namesize;
    295  1.3.2.3  nathanw 	struct lwp *l;
    296  1.3.2.2  nathanw 	struct reg intreg;
    297  1.3.2.2  nathanw #ifdef PT_GETFPREGS
    298  1.3.2.2  nathanw 	struct fpreg freg;
    299  1.3.2.2  nathanw #endif
    300  1.3.2.2  nathanw 
    301  1.3.2.2  nathanw 	size = 0;
    302  1.3.2.2  nathanw 
    303  1.3.2.2  nathanw 	/* First, write an elfcore_procinfo. */
    304  1.3.2.2  nathanw 	notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
    305  1.3.2.2  nathanw 	    elfround(sizeof(cpi));
    306  1.3.2.2  nathanw 	if (offset) {
    307  1.3.2.2  nathanw 		cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    308  1.3.2.2  nathanw 		cpi.cpi_cpisize = sizeof(cpi);
    309  1.3.2.2  nathanw 		cpi.cpi_signo = p->p_sigctx.ps_sig;
    310  1.3.2.2  nathanw 		cpi.cpi_sigcode = p->p_sigctx.ps_code;
    311  1.3.2.2  nathanw 
    312  1.3.2.2  nathanw 		memcpy(&cpi.cpi_sigpend, &p->p_sigctx.ps_siglist,
    313  1.3.2.2  nathanw 		    sizeof(cpi.cpi_sigpend));
    314  1.3.2.2  nathanw 		memcpy(&cpi.cpi_sigmask, &p->p_sigctx.ps_sigmask,
    315  1.3.2.2  nathanw 		    sizeof(cpi.cpi_sigmask));
    316  1.3.2.2  nathanw 		memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    317  1.3.2.2  nathanw 		    sizeof(cpi.cpi_sigignore));
    318  1.3.2.2  nathanw 		memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    319  1.3.2.2  nathanw 		    sizeof(cpi.cpi_sigcatch));
    320  1.3.2.2  nathanw 
    321  1.3.2.2  nathanw 		cpi.cpi_pid = p->p_pid;
    322  1.3.2.2  nathanw 		cpi.cpi_ppid = p->p_pptr->p_pid;
    323  1.3.2.2  nathanw 		cpi.cpi_pgrp = p->p_pgid;
    324  1.3.2.2  nathanw 		cpi.cpi_sid = p->p_session->s_sid;
    325  1.3.2.2  nathanw 
    326  1.3.2.2  nathanw 		cpi.cpi_ruid = p->p_cred->p_ruid;
    327  1.3.2.2  nathanw 		cpi.cpi_euid = p->p_ucred->cr_uid;
    328  1.3.2.2  nathanw 		cpi.cpi_svuid = p->p_cred->p_svuid;
    329  1.3.2.2  nathanw 
    330  1.3.2.2  nathanw 		cpi.cpi_rgid = p->p_cred->p_rgid;
    331  1.3.2.2  nathanw 		cpi.cpi_egid = p->p_ucred->cr_gid;
    332  1.3.2.2  nathanw 		cpi.cpi_svgid = p->p_cred->p_svgid;
    333  1.3.2.2  nathanw 
    334  1.3.2.3  nathanw 		cpi.cpi_nlwps = p->p_nlwps;
    335  1.3.2.2  nathanw 		strcpy(cpi.cpi_name, p->p_comm);
    336  1.3.2.2  nathanw 
    337  1.3.2.2  nathanw 		nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
    338  1.3.2.2  nathanw 		nhdr.n_descsz = sizeof(cpi);
    339  1.3.2.2  nathanw 		nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
    340  1.3.2.2  nathanw 
    341  1.3.2.2  nathanw 		error = ELFNAMEEND(coredump_writenote)(p, vp, cred, offset,
    342  1.3.2.2  nathanw 		    &nhdr, ELF_NOTE_NETBSD_CORE_NAME, &cpi);
    343  1.3.2.2  nathanw 		if (error)
    344  1.3.2.2  nathanw 			return (error);
    345  1.3.2.2  nathanw 
    346  1.3.2.2  nathanw 		offset += notesize;
    347  1.3.2.2  nathanw 	}
    348  1.3.2.2  nathanw 
    349  1.3.2.2  nathanw 	size += notesize;
    350  1.3.2.2  nathanw 
    351  1.3.2.2  nathanw 	/* XXX Add hook for machdep per-proc notes. */
    352  1.3.2.2  nathanw 
    353  1.3.2.2  nathanw 	/*
    354  1.3.2.2  nathanw 	 * Now, for each LWP, write the register info and any other
    355  1.3.2.2  nathanw 	 * per-LWP notes.
    356  1.3.2.2  nathanw 	 */
    357  1.3.2.3  nathanw 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    358  1.3.2.3  nathanw 		sprintf(name, "%s@%d", ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
    359  1.3.2.2  nathanw 		namesize = strlen(name) + 1;
    360  1.3.2.2  nathanw 
    361  1.3.2.2  nathanw 		notesize = sizeof(nhdr) + elfround(namesize) +
    362  1.3.2.2  nathanw 		    elfround(sizeof(intreg));
    363  1.3.2.2  nathanw 		if (offset) {
    364  1.3.2.3  nathanw 			error = process_read_regs(l, &intreg);
    365  1.3.2.2  nathanw 			if (error)
    366  1.3.2.2  nathanw 				return (error);
    367  1.3.2.2  nathanw 
    368  1.3.2.2  nathanw 			nhdr.n_namesz = namesize;
    369  1.3.2.2  nathanw 			nhdr.n_descsz = sizeof(intreg);
    370  1.3.2.2  nathanw 			nhdr.n_type = PT_GETREGS;
    371  1.3.2.2  nathanw 
    372  1.3.2.2  nathanw 			error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
    373  1.3.2.2  nathanw 			    offset, &nhdr, name, &intreg);
    374  1.3.2.2  nathanw 			if (error)
    375  1.3.2.2  nathanw 				return (error);
    376  1.3.2.2  nathanw 
    377  1.3.2.2  nathanw 			offset += notesize;
    378  1.3.2.2  nathanw 		}
    379  1.3.2.2  nathanw 		size += notesize;
    380  1.3.2.2  nathanw 
    381  1.3.2.2  nathanw #ifdef PT_GETFPREGS
    382  1.3.2.2  nathanw 		notesize = sizeof(nhdr) + elfround(namesize) +
    383  1.3.2.2  nathanw 		    elfround(sizeof(freg));
    384  1.3.2.2  nathanw 		if (offset) {
    385  1.3.2.3  nathanw 			error = process_read_fpregs(l, &freg);
    386  1.3.2.2  nathanw 			if (error)
    387  1.3.2.2  nathanw 				return (error);
    388  1.3.2.2  nathanw 
    389  1.3.2.2  nathanw 			nhdr.n_namesz = namesize;
    390  1.3.2.2  nathanw 			nhdr.n_descsz = sizeof(freg);
    391  1.3.2.2  nathanw 			nhdr.n_type = PT_GETFPREGS;
    392  1.3.2.2  nathanw 
    393  1.3.2.2  nathanw 			error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
    394  1.3.2.2  nathanw 			    offset, &nhdr, name, &freg);
    395  1.3.2.2  nathanw 			if (error)
    396  1.3.2.2  nathanw 				return (error);
    397  1.3.2.2  nathanw 
    398  1.3.2.2  nathanw 			offset += notesize;
    399  1.3.2.2  nathanw 		}
    400  1.3.2.2  nathanw 		size += notesize;
    401  1.3.2.2  nathanw #endif
    402  1.3.2.2  nathanw 		/* XXX Add hook for machdep per-LWP notes. */
    403  1.3.2.3  nathanw 	}
    404  1.3.2.2  nathanw 
    405  1.3.2.2  nathanw 	*sizep = size;
    406  1.3.2.2  nathanw 	return (0);
    407  1.3.2.2  nathanw }
    408  1.3.2.2  nathanw 
    409  1.3.2.2  nathanw int
    410  1.3.2.2  nathanw ELFNAMEEND(coredump_writenote)(struct proc *p, struct vnode *vp,
    411  1.3.2.2  nathanw     struct ucred *cred, off_t offset, Elf_Nhdr *nhdr, const char *name,
    412  1.3.2.2  nathanw     void *data)
    413  1.3.2.2  nathanw {
    414  1.3.2.2  nathanw 	int error;
    415  1.3.2.2  nathanw 
    416  1.3.2.2  nathanw 	error = vn_rdwr(UIO_WRITE, vp,
    417  1.3.2.2  nathanw 	    (caddr_t) nhdr, sizeof(*nhdr),
    418  1.3.2.2  nathanw 	    offset, UIO_SYSSPACE,
    419  1.3.2.2  nathanw 	    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    420  1.3.2.2  nathanw 	if (error)
    421  1.3.2.2  nathanw 		return (error);
    422  1.3.2.2  nathanw 
    423  1.3.2.2  nathanw 	offset += sizeof(*nhdr);
    424  1.3.2.2  nathanw 
    425  1.3.2.2  nathanw 	error = vn_rdwr(UIO_WRITE, vp,
    426  1.3.2.2  nathanw 	    (caddr_t)name, nhdr->n_namesz,
    427  1.3.2.2  nathanw 	    offset, UIO_SYSSPACE,
    428  1.3.2.2  nathanw 	    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    429  1.3.2.2  nathanw 	if (error)
    430  1.3.2.2  nathanw 		return (error);
    431  1.3.2.2  nathanw 
    432  1.3.2.2  nathanw 	offset += elfround(nhdr->n_namesz);
    433  1.3.2.2  nathanw 
    434  1.3.2.2  nathanw 	error = vn_rdwr(UIO_WRITE, vp,
    435  1.3.2.2  nathanw 	    data, nhdr->n_descsz,
    436  1.3.2.2  nathanw 	    offset, UIO_SYSSPACE,
    437  1.3.2.2  nathanw 	    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    438  1.3.2.2  nathanw 
    439  1.3.2.2  nathanw 	return (error);
    440  1.3.2.2  nathanw }
    441