xref: /netbsd-src/sys/kern/core_netbsd.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: core_netbsd.c,v 1.17 2011/01/14 02:06:34 rmind 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. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *      This product includes software developed by Charles D. Cranor,
25  *	Washington University, the University of California, Berkeley and
26  *	its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
44  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
45  * from: NetBSD: uvm_unix.c,v 1.25 2001/11/10 07:37:01 lukem Exp
46  */
47 
48 /*
49  * core_netbsd.c: Support for the historic NetBSD core file format.
50  */
51 
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: core_netbsd.c,v 1.17 2011/01/14 02:06:34 rmind Exp $");
54 
55 #ifdef _KERNEL_OPT
56 #include "opt_coredump.h"
57 #endif
58 
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/exec.h>
62 #include <sys/proc.h>
63 #include <sys/vnode.h>
64 #include <sys/core.h>
65 
66 #include <uvm/uvm_extern.h>
67 
68 #ifndef CORENAME
69 #define	CORENAME(x)	x
70 #endif
71 #ifdef COREINC
72 #include COREINC
73 #endif
74 
75 #ifdef COREDUMP
76 
77 struct coredump_state {
78 	struct CORENAME(core) core;
79 };
80 
81 static int	CORENAME(coredump_countsegs_netbsd)(struct proc *, void *,
82 		    struct uvm_coredump_state *);
83 static int	CORENAME(coredump_writesegs_netbsd)(struct proc *, void *,
84 		    struct uvm_coredump_state *);
85 
86 int
87 CORENAME(coredump_netbsd)(struct lwp *l, void *iocookie)
88 {
89 	struct coredump_state cs;
90 	struct proc *p = l->l_proc;
91 	struct vmspace *vm = p->p_vmspace;
92 	int error;
93 
94 	cs.core.c_midmag = 0;
95 	strncpy(cs.core.c_name, p->p_comm, MAXCOMLEN);
96 	cs.core.c_nseg = 0;
97 	cs.core.c_signo = p->p_sigctx.ps_signo;
98 	cs.core.c_ucode = p->p_sigctx.ps_code;
99 	cs.core.c_cpusize = 0;
100 	cs.core.c_tsize = (u_long)ctob(vm->vm_tsize);
101 	cs.core.c_dsize = (u_long)ctob(vm->vm_dsize);
102 	cs.core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
103 
104 	error = CORENAME(cpu_coredump)(l, NULL, &cs.core);
105 	if (error)
106 		return (error);
107 	error = uvm_coredump_walkmap(p, NULL,
108 	    CORENAME(coredump_countsegs_netbsd), &cs);
109 	if (error)
110 		return (error);
111 
112 	/* First write out the core header. */
113 	error = coredump_write(iocookie, UIO_SYSSPACE, &cs.core,
114 	    cs.core.c_hdrsize);
115 	if (error)
116 		return (error);
117 
118 	/* Then the CPU specific stuff */
119 	error = CORENAME(cpu_coredump)(l, iocookie, &cs.core);
120 	if (error)
121 		return (error);
122 
123 	/* Finally, the address space dump */
124 	return uvm_coredump_walkmap(p, iocookie,
125 	    CORENAME(coredump_writesegs_netbsd), &cs);
126 }
127 
128 static int
129 CORENAME(coredump_countsegs_netbsd)(struct proc *p, void *iocookie,
130     struct uvm_coredump_state *us)
131 {
132 	struct coredump_state *cs = us->cookie;
133 
134 	if (us->start != us->realend)
135 		cs->core.c_nseg++;
136 
137 	return (0);
138 }
139 
140 static int
141 CORENAME(coredump_writesegs_netbsd)(struct proc *p, void *iocookie,
142     struct uvm_coredump_state *us)
143 {
144 	struct coredump_state *cs = us->cookie;
145 	struct CORENAME(coreseg) cseg;
146 	int flag, error;
147 
148 	if (us->start == us->realend)
149 		return (0);
150 
151 	if (us->flags & UVM_COREDUMP_STACK)
152 		flag = CORE_STACK;
153 	else
154 		flag = CORE_DATA;
155 
156 	/*
157 	 * Set up a new core file segment.
158 	 */
159 	CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(cs->core), flag);
160 	cseg.c_addr = us->start;
161 	cseg.c_size = us->end - us->start;
162 
163 	error = coredump_write(iocookie, UIO_SYSSPACE,
164 	    &cseg, cs->core.c_seghdrsize);
165 	if (error)
166 		return (error);
167 
168 	return coredump_write(iocookie, UIO_USERSPACE,
169 	    (void *)(vaddr_t)us->start, cseg.c_size);
170 }
171 
172 #else	/* COREDUMP */
173 
174 int
175 CORENAME(coredump_netbsd)(struct lwp *l, void *cookie)
176 {
177 
178 	return ENOSYS;
179 }
180 
181 #endif	/* COREDUMP */
182