Remove advertising header from all userland binaries.
[dragonfly.git] / usr.bin / gcore / md-sparc.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)md-sparc.c       8.1 (Berkeley) 6/6/93
34  *
35  * $DragonFly: src/usr.bin/gcore/md-sparc.c,v 1.5 2005/04/10 20:55:38 drhodus Exp $
36  */
37
38 #include <sys/user.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/sysctl.h>
44 #include <machine/vmparam.h>
45
46 #include <err.h>
47 #include <kvm.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include "extern.h"
52
53 #ifndef offsetof
54 #define offsetof(s, f) ((int)&((s *)0)->f)
55 #endif
56
57 static void
58 shift_page(int fd, off_t off, int ssize)
59 {
60         char buffer[NBPG];
61
62         (void)lseek(fd, (off_t)-NBPG, SEEK_END);
63         for (; ssize > 0; ssize -= NBPG) {
64                 (void)read(fd, buffer, NBPG);
65                 (void)write(fd, buffer, NBPG);
66                 (void)lseek(fd, (off_t)-2 * NBPG, SEEK_CUR);
67         }
68 }
69
70 /*
71  * Fix up the core image for the sparc.  We need to flush any register
72  * windows that are cached in the pcb out to the user stack.
73  * Also, we need to get the trap frame and possible floating point state
74  * from the top of the kernel stack and store it in the pcb.
75  */
76 void
77 md_core(kvm_t *kd, int fd, struct kinfo_proc *ki)
78 {
79         struct rwindow *rw;
80         int nsaved, cc, ssize;
81         off_t off, s;
82         u_long sp;
83         struct pcb pcb;
84         struct trapframe tf;
85
86         /*
87          * Before anything else read the trapframe.  Synchronizing here
88          * is impossible if the process is running.
89          */
90         cc = kvm_read(kd, (u_long)ki->kp_proc.p_md.md_tf,
91                       /* XXX */
92                       (void *)&tf, sizeof(tf));
93         if (cc < 0)
94                 errx(1, "kvm_read: %s (reading kernel trapframe)",
95                       kvm_geterr(kd));
96         if (cc != sizeof(tf))
97                 errx(1, "cannot read kernel trapframe");
98
99         /*
100          * Write out the real trap frame.
101          */
102         off = offsetof(struct user, u_md);
103         off += offsetof(struct md_coredump, md_tf);
104         if (lseek(fd, off, SEEK_SET) == -1)
105                 err(1, "lseek");
106         (void)write(fd, &tf, sizeof(tf));
107
108         if (ki->kp_proc.p_md.md_fpstate != 0) {
109                 /*
110                  * If floating point state is present, write it out too.
111                  * It comes right after the trapframe so we don't need to seek.
112                  */
113                 struct fpstate fs;
114                 cc = kvm_read(kd, (u_long)ki->kp_proc.p_md.md_fpstate,
115                               (void *)&fs, sizeof(fs));
116                 if (cc < 0)
117                         errx(1, "kvm_read: %s (fpu state)", kvm_geterr(kd));
118                 if (cc != sizeof(fs))
119                         errx(1, "cannot read fpu state");
120                 (void)write(fd, (char *)&fs, sizeof(fs));
121         }
122         /*
123          * Read pcb.
124          */
125         if (lseek(fd, (off_t)offsetof(struct user, u_pcb), SEEK_SET) == -1)
126                 err(1, "lseek");
127         cc = read(fd, (char *)&pcb, sizeof(pcb));
128         if (cc != sizeof(pcb)) {
129                 if (cc < 0)
130                         err(1, "read");
131                 errx(1, "couldn't read pcb from core file");
132         }
133
134         /*
135          * Write any unsaved windows to the appropriate stack locations.
136          */
137         nsaved = pcb.pcb_nsaved;
138         if (nsaved == 0)
139                 return;
140
141         rw = &pcb.pcb_rw[0];
142         off = ctob(UPAGES + ki->kp_eproc.e_vm.vm_dsize);
143         ssize = ctob(ki->kp_eproc.e_vm.vm_ssize);
144         sp = tf.tf_out[6];
145         for (; --nsaved >= 0; ++rw) {
146                 /*
147                  * Copy register window into appropriate stack location.
148                  */
149                 s = ssize - (USRSTACK - sp);
150                 if (s < 0) {
151                         if (s < -NBPG)
152                                 errx(1, "cannot copy pcb windows to stack");
153                         /*
154                          * It's possible to be missing the bottomost
155                          * page because a stack page hasn't been allocated
156                          * for the register save area.  Shift over
157                          * the stack segment by a page, and update
158                          * the u-area to reflect the new stack size.  YECH!
159                          */
160                         shift_page(fd, off, ssize);
161                         ssize += NBPG;
162                         s += NBPG;
163                         ++ki->kp_eproc.e_vm.vm_ssize;
164                         (void)lseek(fd,
165                             (off_t)offsetof(struct user, u_kproc.kp_eproc.e_vm),
166                             SEEK_SET);
167                         (void)write(fd,
168                             &ki->kp_eproc.e_vm, sizeof(ki->kp_eproc.e_vm));
169                 }
170                 if (lseek(fd, off + s, SEEK_SET) == -1)
171                         errx(1, "cannot copy pcb windows to stack");
172
173                 (void)write(fd, rw, sizeof(*rw));
174                 sp = rw->rw_in[6];
175         }
176 }