netbsd32_core.c revision 1.3
11.3Sthorpej/*	$NetBSD: netbsd32_core.c,v 1.3 2001/12/10 01:56:46 thorpej Exp $	*/
21.1Sthorpej
31.1Sthorpej/*
41.1Sthorpej * Copyright (c) 1997 Charles D. Cranor and Washington University.
51.1Sthorpej * Copyright (c) 1991, 1993 The Regents of the University of California.
61.1Sthorpej * Copyright (c) 1988 University of Utah.
71.1Sthorpej *
81.1Sthorpej * All rights reserved.
91.1Sthorpej *
101.1Sthorpej * This code is derived from software contributed to Berkeley by
111.1Sthorpej * the Systems Programming Group of the University of Utah Computer
121.1Sthorpej * Science Department.
131.1Sthorpej *
141.1Sthorpej * Redistribution and use in source and binary forms, with or without
151.1Sthorpej * modification, are permitted provided that the following conditions
161.1Sthorpej * are met:
171.1Sthorpej * 1. Redistributions of source code must retain the above copyright
181.1Sthorpej *    notice, this list of conditions and the following disclaimer.
191.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
201.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
211.1Sthorpej *    documentation and/or other materials provided with the distribution.
221.1Sthorpej * 3. All advertising materials mentioning features or use of this software
231.1Sthorpej *    must display the following acknowledgement:
241.1Sthorpej *      This product includes software developed by Charles D. Cranor,
251.1Sthorpej *	Washington University, the University of California, Berkeley and
261.1Sthorpej *	its contributors.
271.1Sthorpej * 4. Neither the name of the University nor the names of its contributors
281.1Sthorpej *    may be used to endorse or promote products derived from this software
291.1Sthorpej *    without specific prior written permission.
301.1Sthorpej *
311.1Sthorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
321.1Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
331.1Sthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
341.1Sthorpej * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
351.1Sthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
361.1Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
371.1Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
381.1Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
391.1Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
401.1Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
411.1Sthorpej * SUCH DAMAGE.
421.1Sthorpej *
431.1Sthorpej * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
441.1Sthorpej *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
451.1Sthorpej * from: NetBSD: uvm_unix.c,v 1.25 2001/11/10 07:37:01 lukem Exp
461.1Sthorpej */
471.1Sthorpej
481.1Sthorpej/*
491.3Sthorpej * core_netbsd.c: Support for the historic NetBSD core file format.
501.1Sthorpej */
511.1Sthorpej
521.1Sthorpej#include <sys/cdefs.h>
531.3Sthorpej__KERNEL_RCSID(0, "$NetBSD: netbsd32_core.c,v 1.3 2001/12/10 01:56:46 thorpej Exp $");
541.1Sthorpej
551.1Sthorpej#include <sys/param.h>
561.1Sthorpej#include <sys/systm.h>
571.1Sthorpej#include <sys/proc.h>
581.1Sthorpej#include <sys/vnode.h>
591.1Sthorpej#include <sys/core.h>
601.1Sthorpej
611.3Sthorpej#include <uvm/uvm_extern.h>
621.1Sthorpej
631.3Sthorpej#include <compat/netbsd32/netbsd32.h>
641.3Sthorpej
651.3Sthorpejstruct coredump_state {
661.3Sthorpej	struct core32 core;
671.3Sthorpej	off_t offset;
681.3Sthorpej};
691.3Sthorpej
701.3Sthorpejint	coredump_writesegs_netbsd(struct proc *, struct vnode *,
711.3Sthorpej	    struct ucred *, struct uvm_coredump_state *);
721.2Sthorpej
731.1Sthorpejint
741.3Sthorpejcoredump_netbsd(struct proc *p, struct vnode *vp, struct ucred *cred)
751.1Sthorpej{
761.3Sthorpej	struct coredump_state cs;
771.1Sthorpej	struct vmspace *vm = p->p_vmspace;
781.3Sthorpej	int error;
791.1Sthorpej
801.3Sthorpej	cs.core.c_midmag = 0;
811.3Sthorpej	strncpy(cs.core.c_name, p->p_comm, MAXCOMLEN);
821.3Sthorpej	cs.core.c_nseg = 0;
831.3Sthorpej	cs.core.c_signo = p->p_sigctx.ps_sig;
841.3Sthorpej	cs.core.c_ucode = p->p_sigctx.ps_code;
851.3Sthorpej	cs.core.c_cpusize = 0;
861.3Sthorpej	cs.core.c_tsize = (u_long)ctob(vm->vm_tsize);
871.3Sthorpej	cs.core.c_dsize = (u_long)ctob(vm->vm_dsize);
881.3Sthorpej	cs.core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
891.3Sthorpej	error = cpu_coredump32(p, vp, cred, &cs.core);
901.1Sthorpej	if (error)
911.1Sthorpej		return (error);
921.1Sthorpej
931.1Sthorpej#if 0
941.1Sthorpej	/*
951.1Sthorpej	 * XXX
961.1Sthorpej	 * It would be nice if we at least dumped the signal state (and made it
971.1Sthorpej	 * available at run time to the debugger, as well), but this code
981.1Sthorpej	 * hasn't actually had any effect for a long time, since we don't dump
991.1Sthorpej	 * the user area.  For now, it's dead.
1001.1Sthorpej	 */
1011.3Sthorpej	memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
1021.3Sthorpej	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1031.1Sthorpej#endif
1041.1Sthorpej
1051.3Sthorpej	cs.offset = cs.core.c_hdrsize + cs.core.c_seghdrsize +
1061.3Sthorpej	    cs.core.c_cpusize;
1071.3Sthorpej	error = uvm_coredump_walkmap(p, vp, cred, coredump_writesegs_netbsd,
1081.3Sthorpej	    &cs);
1091.3Sthorpej	if (error)
1101.3Sthorpej		return (error);
1111.1Sthorpej
1121.1Sthorpej	/* Now write out the core header. */
1131.3Sthorpej	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&cs.core,
1141.3Sthorpej	    (int)cs.core.c_hdrsize, (off_t)0,
1151.1Sthorpej	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1161.1Sthorpej
1171.1Sthorpej	return (error);
1181.3Sthorpej}
1191.3Sthorpej
1201.3Sthorpejint
1211.3Sthorpejcoredump_writesegs_netbsd(struct proc *p, struct vnode *vp, struct ucred *cred,
1221.3Sthorpej    struct uvm_coredump_state *us)
1231.3Sthorpej{
1241.3Sthorpej	struct coredump_state *cs = us->cookie;
1251.3Sthorpej	struct coreseg32 cseg;
1261.3Sthorpej	int flag, error;
1271.3Sthorpej
1281.3Sthorpej	if (us->flags & UVM_COREDUMP_NODUMP)
1291.3Sthorpej		return (0);
1301.3Sthorpej
1311.3Sthorpej	if (us->flags & UVM_COREDUMP_STACK)
1321.3Sthorpej		flag = CORE_STACK;
1331.3Sthorpej	else
1341.3Sthorpej		flag = CORE_DATA;
1351.3Sthorpej
1361.3Sthorpej	/*
1371.3Sthorpej	 * Set up a new core file segment.
1381.3Sthorpej	 */
1391.3Sthorpej	CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(cs->core), flag);
1401.3Sthorpej	cseg.c_addr = us->start;
1411.3Sthorpej	cseg.c_size = us->end - us->start;
1421.3Sthorpej
1431.3Sthorpej	error = vn_rdwr(UIO_WRITE, vp,
1441.3Sthorpej	    (caddr_t)&cseg, cs->core.c_seghdrsize,
1451.3Sthorpej	    cs->offset, UIO_SYSSPACE,
1461.3Sthorpej	    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1471.3Sthorpej	if (error)
1481.3Sthorpej		return (error);
1491.3Sthorpej
1501.3Sthorpej	cs->offset += cs->core.c_seghdrsize;
1511.3Sthorpej	error = vn_rdwr(UIO_WRITE, vp,
1521.3Sthorpej	    (caddr_t) us->start, (int) cseg.c_size,
1531.3Sthorpej	    cs->offset, UIO_USERSPACE,
1541.3Sthorpej	    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1551.3Sthorpej	if (error)
1561.3Sthorpej		return (error);
1571.3Sthorpej
1581.3Sthorpej	cs->offset += cseg.c_size;
1591.3Sthorpej	cs->core.c_nseg++;
1601.3Sthorpej
1611.3Sthorpej	return (0);
1621.1Sthorpej}
163