Home | History | Annotate | Line # | Download | only in kern
core_netbsd.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: core_netbsd.c,v 1.1 2001/12/08 00:35:29 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  1.1  thorpej  * Copyright (c) 1991, 1993 The Regents of the University of California.
      6  1.1  thorpej  * Copyright (c) 1988 University of Utah.
      7  1.1  thorpej  *
      8  1.1  thorpej  * All rights reserved.
      9  1.1  thorpej  *
     10  1.1  thorpej  * This code is derived from software contributed to Berkeley by
     11  1.1  thorpej  * the Systems Programming Group of the University of Utah Computer
     12  1.1  thorpej  * Science Department.
     13  1.1  thorpej  *
     14  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     15  1.1  thorpej  * modification, are permitted provided that the following conditions
     16  1.1  thorpej  * are met:
     17  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     18  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     19  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     20  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     21  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     22  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     23  1.1  thorpej  *    must display the following acknowledgement:
     24  1.1  thorpej  *      This product includes software developed by Charles D. Cranor,
     25  1.1  thorpej  *	Washington University, the University of California, Berkeley and
     26  1.1  thorpej  *	its contributors.
     27  1.1  thorpej  * 4. Neither the name of the University nor the names of its contributors
     28  1.1  thorpej  *    may be used to endorse or promote products derived from this software
     29  1.1  thorpej  *    without specific prior written permission.
     30  1.1  thorpej  *
     31  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     32  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     33  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     34  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     35  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     39  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     40  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     41  1.1  thorpej  * SUCH DAMAGE.
     42  1.1  thorpej  *
     43  1.1  thorpej  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
     44  1.1  thorpej  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
     45  1.1  thorpej  * from: NetBSD: uvm_unix.c,v 1.25 2001/11/10 07:37:01 lukem Exp
     46  1.1  thorpej  */
     47  1.1  thorpej 
     48  1.1  thorpej /*
     49  1.1  thorpej  * core_netbsd.c: Support for the historic NetBSD core file format.
     50  1.1  thorpej  */
     51  1.1  thorpej 
     52  1.1  thorpej #include <sys/cdefs.h>
     53  1.1  thorpej __KERNEL_RCSID(0, "$NetBSD: core_netbsd.c,v 1.1 2001/12/08 00:35:29 thorpej Exp $");
     54  1.1  thorpej 
     55  1.1  thorpej #include <sys/param.h>
     56  1.1  thorpej #include <sys/systm.h>
     57  1.1  thorpej #include <sys/proc.h>
     58  1.1  thorpej #include <sys/vnode.h>
     59  1.1  thorpej #include <sys/core.h>
     60  1.1  thorpej 
     61  1.1  thorpej #include <uvm/uvm.h>
     62  1.1  thorpej 
     63  1.1  thorpej int
     64  1.1  thorpej coredump_netbsd(struct proc *p, struct vnode *vp, struct ucred *cred)
     65  1.1  thorpej {
     66  1.1  thorpej 	struct core core;
     67  1.1  thorpej 	struct coreseg cseg;
     68  1.1  thorpej 	struct vmspace *vm = p->p_vmspace;
     69  1.1  thorpej 	struct vm_map *map = &vm->vm_map;
     70  1.1  thorpej 	struct vm_map_entry *entry;
     71  1.1  thorpej 	vaddr_t start, end, maxstack;
     72  1.1  thorpej 	off_t offset;
     73  1.1  thorpej 	int flag, error;
     74  1.1  thorpej 
     75  1.1  thorpej 	core.c_midmag = 0;
     76  1.1  thorpej 	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
     77  1.1  thorpej 	core.c_nseg = 0;
     78  1.1  thorpej 	core.c_signo = p->p_sigctx.ps_sig;
     79  1.1  thorpej 	core.c_ucode = p->p_sigctx.ps_code;
     80  1.1  thorpej 	core.c_cpusize = 0;
     81  1.1  thorpej 	core.c_tsize = (u_long)ctob(vm->vm_tsize);
     82  1.1  thorpej 	core.c_dsize = (u_long)ctob(vm->vm_dsize);
     83  1.1  thorpej 	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
     84  1.1  thorpej 	error = cpu_coredump(p, vp, cred, &core);
     85  1.1  thorpej 	if (error)
     86  1.1  thorpej 		return (error);
     87  1.1  thorpej 
     88  1.1  thorpej #if 0
     89  1.1  thorpej 	/*
     90  1.1  thorpej 	 * XXX
     91  1.1  thorpej 	 * It would be nice if we at least dumped the signal state (and made it
     92  1.1  thorpej 	 * available at run time to the debugger, as well), but this code
     93  1.1  thorpej 	 * hasn't actually had any effect for a long time, since we don't dump
     94  1.1  thorpej 	 * the user area.  For now, it's dead.
     95  1.1  thorpej 	 */
     96  1.1  thorpej 	memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
     97  1.1  thorpej 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
     98  1.1  thorpej #endif
     99  1.1  thorpej 
    100  1.1  thorpej 	offset = core.c_hdrsize + core.c_seghdrsize + core.c_cpusize;
    101  1.1  thorpej 	maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
    102  1.1  thorpej 
    103  1.1  thorpej 	for (entry = map->header.next; entry != &map->header;
    104  1.1  thorpej 	     entry = entry->next) {
    105  1.1  thorpej 		/* Should never happen for a user process. */
    106  1.1  thorpej 		if (UVM_ET_ISSUBMAP(entry))
    107  1.1  thorpej 			panic("coredump_netbsd: user process with submap?");
    108  1.1  thorpej 
    109  1.1  thorpej 		if ((entry->protection & VM_PROT_WRITE) == 0)
    110  1.1  thorpej 			continue;
    111  1.1  thorpej 
    112  1.1  thorpej 		start = entry->start;
    113  1.1  thorpej 		end = entry->end;
    114  1.1  thorpej 
    115  1.1  thorpej 		if (start >= VM_MAXUSER_ADDRESS)
    116  1.1  thorpej 			continue;
    117  1.1  thorpej 
    118  1.1  thorpej 		if (end > VM_MAXUSER_ADDRESS)
    119  1.1  thorpej 			end = VM_MAXUSER_ADDRESS;
    120  1.1  thorpej 
    121  1.1  thorpej 		if (start >= (vaddr_t)vm->vm_maxsaddr) {
    122  1.1  thorpej 			if (end <= maxstack)
    123  1.1  thorpej 				continue;
    124  1.1  thorpej 			if (start < maxstack)
    125  1.1  thorpej 				start = maxstack;
    126  1.1  thorpej 			flag = CORE_STACK;
    127  1.1  thorpej 		} else
    128  1.1  thorpej 			flag = CORE_DATA;
    129  1.1  thorpej 
    130  1.1  thorpej 		/*
    131  1.1  thorpej 		 * Set up a new core file segment.
    132  1.1  thorpej 		 */
    133  1.1  thorpej 		CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(core), flag);
    134  1.1  thorpej 		cseg.c_addr = start;
    135  1.1  thorpej 		cseg.c_size = end - start;
    136  1.1  thorpej 
    137  1.1  thorpej 		error = vn_rdwr(UIO_WRITE, vp,
    138  1.1  thorpej 		    (caddr_t)&cseg, core.c_seghdrsize,
    139  1.1  thorpej 		    offset, UIO_SYSSPACE,
    140  1.1  thorpej 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    141  1.1  thorpej 		if (error)
    142  1.1  thorpej 			return (error);
    143  1.1  thorpej 
    144  1.1  thorpej 		offset += core.c_seghdrsize;
    145  1.1  thorpej 		error = vn_rdwr(UIO_WRITE, vp,
    146  1.1  thorpej 		    (caddr_t)cseg.c_addr, (int)cseg.c_size,
    147  1.1  thorpej 		    offset, UIO_USERSPACE,
    148  1.1  thorpej 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    149  1.1  thorpej 		if (error)
    150  1.1  thorpej 			return (error);
    151  1.1  thorpej 
    152  1.1  thorpej 		offset += cseg.c_size;
    153  1.1  thorpej 		core.c_nseg++;
    154  1.1  thorpej 	}
    155  1.1  thorpej 
    156  1.1  thorpej 	/* Now write out the core header. */
    157  1.1  thorpej 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
    158  1.1  thorpej 	    (int)core.c_hdrsize, (off_t)0,
    159  1.1  thorpej 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    160  1.1  thorpej 
    161  1.1  thorpej 	return (error);
    162  1.1  thorpej }
    163