xref: /netbsd-src/sys/kern/core_netbsd.c (revision 1d577fe3790e662ec592913bd86905a501cfc612)
1 /*	$NetBSD: core_netbsd.c,v 1.24 2019/11/20 19:37:53 pgoyette Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * Copyright (c) 1991, 1993 The Regents of the University of California.
6  * Copyright (c) 1988 University of Utah.
7  *
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * the Systems Programming Group of the University of Utah Computer
12  * Science Department.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
39  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
40  * from: NetBSD: uvm_unix.c,v 1.25 2001/11/10 07:37:01 lukem Exp
41  */
42 
43 /*
44  * core_netbsd.c: Support for the historic NetBSD core file format.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: core_netbsd.c,v 1.24 2019/11/20 19:37:53 pgoyette Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/exec.h>
53 #include <sys/proc.h>
54 #include <sys/vnode.h>
55 #include <sys/core.h>
56 #include <sys/module.h>
57 #include <sys/compat_stub.h>
58 #include <sys/signalvar.h>
59 
60 #include <uvm/uvm_extern.h>
61 
62 #ifndef CORENAME
63 #define	CORENAME(x)	x
64 #endif
65 #ifdef COREINC
66 #include COREINC
67 #endif
68 
69 struct coredump_state {
70 	struct coredump_iostate *iocookie;
71 	struct CORENAME(core) core;
72 };
73 
74 static int	CORENAME(coredump_writesegs_netbsd)(struct uvm_coredump_state *);
75 
76 int
CORENAME(real_coredump_netbsd)77 CORENAME(real_coredump_netbsd)(struct lwp *l, struct coredump_iostate *iocookie)
78 {
79 	struct coredump_state cs;
80 	struct proc *p = l->l_proc;
81 	struct vmspace *vm = p->p_vmspace;
82 	int error;
83 
84 	cs.iocookie = iocookie;
85 	cs.core.c_midmag = 0;
86 	strncpy(cs.core.c_name, p->p_comm, MAXCOMLEN);
87 	cs.core.c_nseg = 0;
88 	cs.core.c_signo = p->p_sigctx.ps_info._signo;
89 	cs.core.c_ucode = p->p_sigctx.ps_info._code;
90 	cs.core.c_cpusize = 0;
91 	cs.core.c_tsize = (u_long)ctob(vm->vm_tsize);
92 	cs.core.c_dsize = (u_long)ctob(vm->vm_dsize);
93 	cs.core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
94 
95 	error = CORENAME(cpu_coredump)(l, NULL, &cs.core);
96 	if (error)
97 		return (error);
98 	MODULE_HOOK_CALL(uvm_coredump_count_segs_hook, (p), 0, cs.core.c_nseg);
99 
100 	/* First write out the core header. */
101 	MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &cs.core,
102 	    cs.core.c_hdrsize), ENOSYS, error);
103 	if (error)
104 		return (error);
105 
106 	/* Then the CPU specific stuff */
107 	error = CORENAME(cpu_coredump)(l, iocookie, &cs.core);
108 	if (error)
109 		return (error);
110 
111 	/* Finally, the address space dump */
112 	MODULE_HOOK_CALL(uvm_coredump_walkmap_hook,
113 	    (p, CORENAME(coredump_writesegs_netbsd), &cs), ENOSYS, error);
114 
115 	return error;
116 }
117 
118 static int
CORENAME(coredump_writesegs_netbsd)119 CORENAME(coredump_writesegs_netbsd)(struct uvm_coredump_state *us)
120 {
121 	struct coredump_state *cs = us->cookie;
122 	struct CORENAME(coreseg) cseg;
123 	int flag, error;
124 
125 	if (us->flags & UVM_COREDUMP_STACK)
126 		flag = CORE_STACK;
127 	else
128 		flag = CORE_DATA;
129 
130 	/*
131 	 * Set up a new core file segment.
132 	 */
133 	CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(cs->core), flag);
134 	cseg.c_addr = us->start;
135 
136 	if (us->start == us->realend)
137 		/* Not really wanted, but counted... */
138 		cseg.c_size = 0;
139 	else
140 		cseg.c_size = us->end - us->start;
141 
142 	MODULE_HOOK_CALL(coredump_write_hook, (cs->iocookie, UIO_SYSSPACE,
143 	    &cseg, cs->core.c_seghdrsize), ENOSYS, error);
144 	if (error)
145 		return (error);
146 
147 	MODULE_HOOK_CALL(coredump_write_hook, (cs->iocookie, UIO_USERSPACE,
148 	    (void *)(vaddr_t)us->start, cseg.c_size), ENOSYS, error);
149 
150 	return error;
151 }
152