xref: /netbsd-src/sys/kern/core_netbsd.c (revision 1d577fe3790e662ec592913bd86905a501cfc612)
1*1d577fe3Spgoyette /*	$NetBSD: core_netbsd.c,v 1.24 2019/11/20 19:37:53 pgoyette Exp $	*/
2205c159fSthorpej 
3205c159fSthorpej /*
4205c159fSthorpej  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5205c159fSthorpej  * Copyright (c) 1991, 1993 The Regents of the University of California.
6205c159fSthorpej  * Copyright (c) 1988 University of Utah.
7205c159fSthorpej  *
8205c159fSthorpej  * All rights reserved.
9205c159fSthorpej  *
10205c159fSthorpej  * This code is derived from software contributed to Berkeley by
11205c159fSthorpej  * the Systems Programming Group of the University of Utah Computer
12205c159fSthorpej  * Science Department.
13205c159fSthorpej  *
14205c159fSthorpej  * Redistribution and use in source and binary forms, with or without
15205c159fSthorpej  * modification, are permitted provided that the following conditions
16205c159fSthorpej  * are met:
17205c159fSthorpej  * 1. Redistributions of source code must retain the above copyright
18205c159fSthorpej  *    notice, this list of conditions and the following disclaimer.
19205c159fSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
20205c159fSthorpej  *    notice, this list of conditions and the following disclaimer in the
21205c159fSthorpej  *    documentation and/or other materials provided with the distribution.
22afca0358Schuck  * 3. Neither the name of the University nor the names of its contributors
23205c159fSthorpej  *    may be used to endorse or promote products derived from this software
24205c159fSthorpej  *    without specific prior written permission.
25205c159fSthorpej  *
26205c159fSthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27205c159fSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28205c159fSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29205c159fSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30205c159fSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31205c159fSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32205c159fSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33205c159fSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34205c159fSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35205c159fSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36205c159fSthorpej  * SUCH DAMAGE.
37205c159fSthorpej  *
38205c159fSthorpej  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
39205c159fSthorpej  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
40205c159fSthorpej  * from: NetBSD: uvm_unix.c,v 1.25 2001/11/10 07:37:01 lukem Exp
41205c159fSthorpej  */
42205c159fSthorpej 
43205c159fSthorpej /*
44205c159fSthorpej  * core_netbsd.c: Support for the historic NetBSD core file format.
45205c159fSthorpej  */
46205c159fSthorpej 
47205c159fSthorpej #include <sys/cdefs.h>
48*1d577fe3Spgoyette __KERNEL_RCSID(0, "$NetBSD: core_netbsd.c,v 1.24 2019/11/20 19:37:53 pgoyette Exp $");
49205c159fSthorpej 
50205c159fSthorpej #include <sys/param.h>
51205c159fSthorpej #include <sys/systm.h>
52e1245a3cSmatt #include <sys/exec.h>
53205c159fSthorpej #include <sys/proc.h>
54205c159fSthorpej #include <sys/vnode.h>
55205c159fSthorpej #include <sys/core.h>
56*1d577fe3Spgoyette #include <sys/module.h>
57*1d577fe3Spgoyette #include <sys/compat_stub.h>
58*1d577fe3Spgoyette #include <sys/signalvar.h>
59205c159fSthorpej 
6006920aefSthorpej #include <uvm/uvm_extern.h>
6106920aefSthorpej 
62e1245a3cSmatt #ifndef CORENAME
63e1245a3cSmatt #define	CORENAME(x)	x
64e1245a3cSmatt #endif
65e1245a3cSmatt #ifdef COREINC
66e1245a3cSmatt #include COREINC
67e1245a3cSmatt #endif
68e1245a3cSmatt 
6906920aefSthorpej struct coredump_state {
707a743f3dSdsl 	struct coredump_iostate *iocookie;
71e1245a3cSmatt 	struct CORENAME(core) core;
7206920aefSthorpej };
7306920aefSthorpej 
74825d1920Sdsl static int	CORENAME(coredump_writesegs_netbsd)(struct uvm_coredump_state *);
75205c159fSthorpej 
76205c159fSthorpej int
CORENAME(real_coredump_netbsd)77*1d577fe3Spgoyette CORENAME(real_coredump_netbsd)(struct lwp *l, struct coredump_iostate *iocookie)
78205c159fSthorpej {
7906920aefSthorpej 	struct coredump_state cs;
80e1245a3cSmatt 	struct proc *p = l->l_proc;
81e1245a3cSmatt 	struct vmspace *vm = p->p_vmspace;
8206920aefSthorpej 	int error;
83205c159fSthorpej 
847a743f3dSdsl 	cs.iocookie = iocookie;
8506920aefSthorpej 	cs.core.c_midmag = 0;
8606920aefSthorpej 	strncpy(cs.core.c_name, p->p_comm, MAXCOMLEN);
8706920aefSthorpej 	cs.core.c_nseg = 0;
88e4281b20Skamil 	cs.core.c_signo = p->p_sigctx.ps_info._signo;
89e4281b20Skamil 	cs.core.c_ucode = p->p_sigctx.ps_info._code;
9006920aefSthorpej 	cs.core.c_cpusize = 0;
9106920aefSthorpej 	cs.core.c_tsize = (u_long)ctob(vm->vm_tsize);
9206920aefSthorpej 	cs.core.c_dsize = (u_long)ctob(vm->vm_dsize);
9306920aefSthorpej 	cs.core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
94205c159fSthorpej 
95e1245a3cSmatt 	error = CORENAME(cpu_coredump)(l, NULL, &cs.core);
96e1245a3cSmatt 	if (error)
97e1245a3cSmatt 		return (error);
98*1d577fe3Spgoyette 	MODULE_HOOK_CALL(uvm_coredump_count_segs_hook, (p), 0, cs.core.c_nseg);
99205c159fSthorpej 
100e1245a3cSmatt 	/* First write out the core header. */
101*1d577fe3Spgoyette 	MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &cs.core,
102*1d577fe3Spgoyette 	    cs.core.c_hdrsize), ENOSYS, error);
103e1245a3cSmatt 	if (error)
10406920aefSthorpej 		return (error);
105e1245a3cSmatt 
106e1245a3cSmatt 	/* Then the CPU specific stuff */
107e1245a3cSmatt 	error = CORENAME(cpu_coredump)(l, iocookie, &cs.core);
108e1245a3cSmatt 	if (error)
109e1245a3cSmatt 		return (error);
110e1245a3cSmatt 
111e1245a3cSmatt 	/* Finally, the address space dump */
112*1d577fe3Spgoyette 	MODULE_HOOK_CALL(uvm_coredump_walkmap_hook,
113*1d577fe3Spgoyette 	    (p, CORENAME(coredump_writesegs_netbsd), &cs), ENOSYS, error);
114*1d577fe3Spgoyette 
115*1d577fe3Spgoyette 	return error;
116e1245a3cSmatt }
117e1245a3cSmatt 
11859466612Sthorpej static int
CORENAME(coredump_writesegs_netbsd)119825d1920Sdsl CORENAME(coredump_writesegs_netbsd)(struct uvm_coredump_state *us)
120e1245a3cSmatt {
121e1245a3cSmatt 	struct coredump_state *cs = us->cookie;
122e1245a3cSmatt 	struct CORENAME(coreseg) cseg;
12306920aefSthorpej 	int flag, error;
124205c159fSthorpej 
12506920aefSthorpej 	if (us->flags & UVM_COREDUMP_STACK)
126205c159fSthorpej 		flag = CORE_STACK;
12706920aefSthorpej 	else
128205c159fSthorpej 		flag = CORE_DATA;
129205c159fSthorpej 
130205c159fSthorpej 	/*
131205c159fSthorpej 	 * Set up a new core file segment.
132205c159fSthorpej 	 */
13306920aefSthorpej 	CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(cs->core), flag);
13406920aefSthorpej 	cseg.c_addr = us->start;
135ff295547Sdsl 
136ff295547Sdsl 	if (us->start == us->realend)
137ff295547Sdsl 		/* Not really wanted, but counted... */
138ff295547Sdsl 		cseg.c_size = 0;
139ff295547Sdsl 	else
14006920aefSthorpej 		cseg.c_size = us->end - us->start;
141205c159fSthorpej 
142*1d577fe3Spgoyette 	MODULE_HOOK_CALL(coredump_write_hook, (cs->iocookie, UIO_SYSSPACE,
143*1d577fe3Spgoyette 	    &cseg, cs->core.c_seghdrsize), ENOSYS, error);
144205c159fSthorpej 	if (error)
145205c159fSthorpej 		return (error);
146205c159fSthorpej 
147*1d577fe3Spgoyette 	MODULE_HOOK_CALL(coredump_write_hook, (cs->iocookie, UIO_USERSPACE,
148*1d577fe3Spgoyette 	    (void *)(vaddr_t)us->start, cseg.c_size), ENOSYS, error);
149*1d577fe3Spgoyette 
150*1d577fe3Spgoyette 	return error;
151205c159fSthorpej }
152