Home | History | Annotate | Line # | Download | only in kern
core_netbsd.c revision 1.16
      1  1.16        ad /*	$NetBSD: core_netbsd.c,v 1.16 2008/11/19 18:36:06 ad 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.16        ad __KERNEL_RCSID(0, "$NetBSD: core_netbsd.c,v 1.16 2008/11/19 18:36:06 ad Exp $");
     54  1.16        ad 
     55  1.16        ad #ifdef _KERNEL_OPT
     56  1.16        ad #include "opt_coredump.h"
     57  1.16        ad #endif
     58   1.1   thorpej 
     59   1.1   thorpej #include <sys/param.h>
     60   1.1   thorpej #include <sys/systm.h>
     61  1.11      matt #include <sys/exec.h>
     62   1.1   thorpej #include <sys/proc.h>
     63   1.1   thorpej #include <sys/vnode.h>
     64   1.1   thorpej #include <sys/core.h>
     65   1.1   thorpej 
     66   1.2   thorpej #include <uvm/uvm_extern.h>
     67   1.2   thorpej 
     68  1.11      matt #ifndef CORENAME
     69  1.11      matt #define	CORENAME(x)	x
     70  1.11      matt #endif
     71  1.11      matt #ifdef COREINC
     72  1.11      matt #include COREINC
     73  1.11      matt #endif
     74  1.11      matt 
     75  1.16        ad #ifdef COREDUMP
     76  1.16        ad 
     77   1.2   thorpej struct coredump_state {
     78  1.11      matt 	struct CORENAME(core) core;
     79   1.2   thorpej };
     80   1.2   thorpej 
     81  1.12   thorpej static int	CORENAME(coredump_countsegs_netbsd)(struct proc *, void *,
     82  1.12   thorpej 		    struct uvm_coredump_state *);
     83  1.12   thorpej static int	CORENAME(coredump_writesegs_netbsd)(struct proc *, void *,
     84  1.12   thorpej 		    struct uvm_coredump_state *);
     85   1.1   thorpej 
     86   1.1   thorpej int
     87  1.11      matt CORENAME(coredump_netbsd)(struct lwp *l, void *iocookie)
     88   1.1   thorpej {
     89   1.2   thorpej 	struct coredump_state cs;
     90  1.11      matt 	struct proc *p = l->l_proc;
     91  1.11      matt 	struct vmspace *vm = p->p_vmspace;
     92   1.2   thorpej 	int error;
     93   1.9     perry 
     94   1.1   thorpej 
     95   1.2   thorpej 	cs.core.c_midmag = 0;
     96   1.2   thorpej 	strncpy(cs.core.c_name, p->p_comm, MAXCOMLEN);
     97   1.2   thorpej 	cs.core.c_nseg = 0;
     98   1.7  christos 	cs.core.c_signo = p->p_sigctx.ps_signo;
     99   1.7  christos 	cs.core.c_ucode = p->p_sigctx.ps_code;
    100   1.2   thorpej 	cs.core.c_cpusize = 0;
    101   1.2   thorpej 	cs.core.c_tsize = (u_long)ctob(vm->vm_tsize);
    102   1.2   thorpej 	cs.core.c_dsize = (u_long)ctob(vm->vm_dsize);
    103   1.2   thorpej 	cs.core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
    104   1.1   thorpej 
    105   1.1   thorpej #if 0
    106   1.1   thorpej 	/*
    107   1.1   thorpej 	 * XXX
    108   1.1   thorpej 	 * It would be nice if we at least dumped the signal state (and made it
    109   1.1   thorpej 	 * available at run time to the debugger, as well), but this code
    110   1.1   thorpej 	 * hasn't actually had any effect for a long time, since we don't dump
    111   1.1   thorpej 	 * the user area.  For now, it's dead.
    112   1.1   thorpej 	 */
    113   1.1   thorpej 	memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
    114  1.15        ad 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc, false);
    115   1.1   thorpej #endif
    116  1.11      matt 	error = CORENAME(cpu_coredump)(l, NULL, &cs.core);
    117  1.11      matt 	if (error)
    118  1.11      matt 		return (error);
    119  1.11      matt 	error = uvm_coredump_walkmap(p, NULL,
    120  1.11      matt 	    CORENAME(coredump_countsegs_netbsd), &cs);
    121  1.11      matt 	if (error)
    122  1.11      matt 		return (error);
    123   1.1   thorpej 
    124  1.11      matt 	/* First write out the core header. */
    125  1.11      matt 	error = coredump_write(iocookie, UIO_SYSSPACE, &cs.core,
    126  1.11      matt 	    cs.core.c_hdrsize);
    127   1.2   thorpej 	if (error)
    128   1.2   thorpej 		return (error);
    129   1.1   thorpej 
    130  1.11      matt 	/* Then the CPU specific stuff */
    131  1.11      matt 	error = CORENAME(cpu_coredump)(l, iocookie, &cs.core);
    132  1.11      matt 	if (error)
    133  1.11      matt 		return (error);
    134   1.1   thorpej 
    135  1.11      matt 	/* Finally, the address space dump */
    136  1.11      matt 	return uvm_coredump_walkmap(p, iocookie,
    137  1.11      matt 	    CORENAME(coredump_writesegs_netbsd), &cs);
    138   1.2   thorpej }
    139   1.2   thorpej 
    140  1.12   thorpej static int
    141  1.14      yamt CORENAME(coredump_countsegs_netbsd)(struct proc *p, void *iocookie,
    142  1.14      yamt     struct uvm_coredump_state *us)
    143   1.2   thorpej {
    144   1.2   thorpej 	struct coredump_state *cs = us->cookie;
    145  1.11      matt 
    146  1.11      matt 	if (us->start != us->realend)
    147  1.11      matt 		cs->core.c_nseg++;
    148  1.11      matt 
    149  1.11      matt 	return (0);
    150  1.11      matt }
    151  1.11      matt 
    152  1.12   thorpej static int
    153  1.14      yamt CORENAME(coredump_writesegs_netbsd)(struct proc *p, void *iocookie,
    154  1.11      matt     struct uvm_coredump_state *us)
    155  1.11      matt {
    156  1.11      matt 	struct coredump_state *cs = us->cookie;
    157  1.11      matt 	struct CORENAME(coreseg) cseg;
    158   1.2   thorpej 	int flag, error;
    159   1.2   thorpej 
    160  1.10      matt 	if (us->start == us->realend)
    161   1.2   thorpej 		return (0);
    162   1.2   thorpej 
    163   1.2   thorpej 	if (us->flags & UVM_COREDUMP_STACK)
    164   1.2   thorpej 		flag = CORE_STACK;
    165   1.2   thorpej 	else
    166   1.2   thorpej 		flag = CORE_DATA;
    167   1.2   thorpej 
    168   1.2   thorpej 	/*
    169   1.2   thorpej 	 * Set up a new core file segment.
    170   1.2   thorpej 	 */
    171   1.2   thorpej 	CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(cs->core), flag);
    172   1.2   thorpej 	cseg.c_addr = us->start;
    173   1.2   thorpej 	cseg.c_size = us->end - us->start;
    174   1.2   thorpej 
    175  1.11      matt 	error = coredump_write(iocookie, UIO_SYSSPACE,
    176  1.11      matt 	    &cseg, cs->core.c_seghdrsize);
    177   1.2   thorpej 	if (error)
    178   1.2   thorpej 		return (error);
    179   1.2   thorpej 
    180  1.11      matt 	return coredump_write(iocookie, UIO_USERSPACE,
    181  1.11      matt 	    (void *)(vaddr_t)us->start, cseg.c_size);
    182   1.1   thorpej }
    183  1.16        ad 
    184  1.16        ad #else	/* COREDUMP */
    185  1.16        ad 
    186  1.16        ad int
    187  1.16        ad CORENAME(coredump_netbsd)(struct lwp *l, void *cookie)
    188  1.16        ad {
    189  1.16        ad 
    190  1.16        ad 	return ENOSYS;
    191  1.16        ad }
    192  1.16        ad 
    193  1.16        ad #endif	/* COREDUMP */
    194