core_netbsd.c revision 1.2.2.3 1 1.2.2.3 nathanw /* $NetBSD: core_netbsd.c,v 1.2.2.3 2002/01/09 02:58:56 nathanw Exp $ */
2 1.2.2.2 nathanw
3 1.2.2.2 nathanw /*
4 1.2.2.2 nathanw * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.2.2.2 nathanw * Copyright (c) 1991, 1993 The Regents of the University of California.
6 1.2.2.2 nathanw * Copyright (c) 1988 University of Utah.
7 1.2.2.2 nathanw *
8 1.2.2.2 nathanw * All rights reserved.
9 1.2.2.2 nathanw *
10 1.2.2.2 nathanw * This code is derived from software contributed to Berkeley by
11 1.2.2.2 nathanw * the Systems Programming Group of the University of Utah Computer
12 1.2.2.2 nathanw * Science Department.
13 1.2.2.2 nathanw *
14 1.2.2.2 nathanw * Redistribution and use in source and binary forms, with or without
15 1.2.2.2 nathanw * modification, are permitted provided that the following conditions
16 1.2.2.2 nathanw * are met:
17 1.2.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
18 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer.
19 1.2.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
20 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
21 1.2.2.2 nathanw * documentation and/or other materials provided with the distribution.
22 1.2.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
23 1.2.2.2 nathanw * must display the following acknowledgement:
24 1.2.2.2 nathanw * This product includes software developed by Charles D. Cranor,
25 1.2.2.2 nathanw * Washington University, the University of California, Berkeley and
26 1.2.2.2 nathanw * its contributors.
27 1.2.2.2 nathanw * 4. Neither the name of the University nor the names of its contributors
28 1.2.2.2 nathanw * may be used to endorse or promote products derived from this software
29 1.2.2.2 nathanw * without specific prior written permission.
30 1.2.2.2 nathanw *
31 1.2.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 1.2.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 1.2.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 1.2.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 1.2.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.2.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.2.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.2.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 1.2.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 1.2.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 1.2.2.2 nathanw * SUCH DAMAGE.
42 1.2.2.2 nathanw *
43 1.2.2.2 nathanw * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
44 1.2.2.2 nathanw * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93
45 1.2.2.2 nathanw * from: NetBSD: uvm_unix.c,v 1.25 2001/11/10 07:37:01 lukem Exp
46 1.2.2.2 nathanw */
47 1.2.2.2 nathanw
48 1.2.2.2 nathanw /*
49 1.2.2.2 nathanw * core_netbsd.c: Support for the historic NetBSD core file format.
50 1.2.2.2 nathanw */
51 1.2.2.2 nathanw
52 1.2.2.2 nathanw #include <sys/cdefs.h>
53 1.2.2.3 nathanw __KERNEL_RCSID(0, "$NetBSD: core_netbsd.c,v 1.2.2.3 2002/01/09 02:58:56 nathanw Exp $");
54 1.2.2.2 nathanw
55 1.2.2.2 nathanw #include <sys/param.h>
56 1.2.2.2 nathanw #include <sys/systm.h>
57 1.2.2.2 nathanw #include <sys/proc.h>
58 1.2.2.2 nathanw #include <sys/vnode.h>
59 1.2.2.2 nathanw #include <sys/core.h>
60 1.2.2.2 nathanw
61 1.2.2.2 nathanw #include <uvm/uvm_extern.h>
62 1.2.2.2 nathanw
63 1.2.2.2 nathanw struct coredump_state {
64 1.2.2.2 nathanw struct core core;
65 1.2.2.2 nathanw off_t offset;
66 1.2.2.2 nathanw };
67 1.2.2.2 nathanw
68 1.2.2.2 nathanw int coredump_writesegs_netbsd(struct proc *, struct vnode *,
69 1.2.2.2 nathanw struct ucred *, struct uvm_coredump_state *);
70 1.2.2.2 nathanw
71 1.2.2.2 nathanw int
72 1.2.2.3 nathanw coredump_netbsd(struct lwp *l, struct vnode *vp, struct ucred *cred)
73 1.2.2.2 nathanw {
74 1.2.2.2 nathanw struct coredump_state cs;
75 1.2.2.3 nathanw struct proc *p;
76 1.2.2.3 nathanw struct vmspace *vm;
77 1.2.2.2 nathanw int error;
78 1.2.2.3 nathanw
79 1.2.2.3 nathanw p = l->l_proc;
80 1.2.2.3 nathanw vm = p->p_vmspace;
81 1.2.2.2 nathanw
82 1.2.2.2 nathanw cs.core.c_midmag = 0;
83 1.2.2.2 nathanw strncpy(cs.core.c_name, p->p_comm, MAXCOMLEN);
84 1.2.2.2 nathanw cs.core.c_nseg = 0;
85 1.2.2.2 nathanw cs.core.c_signo = p->p_sigctx.ps_sig;
86 1.2.2.2 nathanw cs.core.c_ucode = p->p_sigctx.ps_code;
87 1.2.2.2 nathanw cs.core.c_cpusize = 0;
88 1.2.2.2 nathanw cs.core.c_tsize = (u_long)ctob(vm->vm_tsize);
89 1.2.2.2 nathanw cs.core.c_dsize = (u_long)ctob(vm->vm_dsize);
90 1.2.2.2 nathanw cs.core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
91 1.2.2.3 nathanw error = cpu_coredump(l, vp, cred, &cs.core);
92 1.2.2.2 nathanw if (error)
93 1.2.2.2 nathanw return (error);
94 1.2.2.2 nathanw
95 1.2.2.2 nathanw #if 0
96 1.2.2.2 nathanw /*
97 1.2.2.2 nathanw * XXX
98 1.2.2.2 nathanw * It would be nice if we at least dumped the signal state (and made it
99 1.2.2.2 nathanw * available at run time to the debugger, as well), but this code
100 1.2.2.2 nathanw * hasn't actually had any effect for a long time, since we don't dump
101 1.2.2.2 nathanw * the user area. For now, it's dead.
102 1.2.2.2 nathanw */
103 1.2.2.2 nathanw memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
104 1.2.2.2 nathanw fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
105 1.2.2.2 nathanw #endif
106 1.2.2.2 nathanw
107 1.2.2.2 nathanw cs.offset = cs.core.c_hdrsize + cs.core.c_seghdrsize +
108 1.2.2.2 nathanw cs.core.c_cpusize;
109 1.2.2.2 nathanw error = uvm_coredump_walkmap(p, vp, cred, coredump_writesegs_netbsd,
110 1.2.2.2 nathanw &cs);
111 1.2.2.2 nathanw if (error)
112 1.2.2.2 nathanw return (error);
113 1.2.2.2 nathanw
114 1.2.2.2 nathanw /* Now write out the core header. */
115 1.2.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&cs.core,
116 1.2.2.2 nathanw (int)cs.core.c_hdrsize, (off_t)0,
117 1.2.2.2 nathanw UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
118 1.2.2.2 nathanw
119 1.2.2.2 nathanw return (error);
120 1.2.2.2 nathanw }
121 1.2.2.2 nathanw
122 1.2.2.2 nathanw int
123 1.2.2.2 nathanw coredump_writesegs_netbsd(struct proc *p, struct vnode *vp, struct ucred *cred,
124 1.2.2.2 nathanw struct uvm_coredump_state *us)
125 1.2.2.2 nathanw {
126 1.2.2.2 nathanw struct coredump_state *cs = us->cookie;
127 1.2.2.2 nathanw struct coreseg cseg;
128 1.2.2.2 nathanw int flag, error;
129 1.2.2.2 nathanw
130 1.2.2.2 nathanw if (us->flags & UVM_COREDUMP_NODUMP)
131 1.2.2.2 nathanw return (0);
132 1.2.2.2 nathanw
133 1.2.2.2 nathanw if (us->flags & UVM_COREDUMP_STACK)
134 1.2.2.2 nathanw flag = CORE_STACK;
135 1.2.2.2 nathanw else
136 1.2.2.2 nathanw flag = CORE_DATA;
137 1.2.2.2 nathanw
138 1.2.2.2 nathanw /*
139 1.2.2.2 nathanw * Set up a new core file segment.
140 1.2.2.2 nathanw */
141 1.2.2.2 nathanw CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(cs->core), flag);
142 1.2.2.2 nathanw cseg.c_addr = us->start;
143 1.2.2.2 nathanw cseg.c_size = us->end - us->start;
144 1.2.2.2 nathanw
145 1.2.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp,
146 1.2.2.2 nathanw (caddr_t)&cseg, cs->core.c_seghdrsize,
147 1.2.2.2 nathanw cs->offset, UIO_SYSSPACE,
148 1.2.2.2 nathanw IO_NODELOCKED|IO_UNIT, cred, NULL, p);
149 1.2.2.2 nathanw if (error)
150 1.2.2.2 nathanw return (error);
151 1.2.2.2 nathanw
152 1.2.2.2 nathanw cs->offset += cs->core.c_seghdrsize;
153 1.2.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp,
154 1.2.2.2 nathanw (caddr_t) us->start, (int) cseg.c_size,
155 1.2.2.2 nathanw cs->offset, UIO_USERSPACE,
156 1.2.2.2 nathanw IO_NODELOCKED|IO_UNIT, cred, NULL, p);
157 1.2.2.2 nathanw if (error)
158 1.2.2.2 nathanw return (error);
159 1.2.2.2 nathanw
160 1.2.2.2 nathanw cs->offset += cseg.c_size;
161 1.2.2.2 nathanw cs->core.c_nseg++;
162 1.2.2.2 nathanw
163 1.2.2.2 nathanw return (0);
164 1.2.2.2 nathanw }
165