core_elf32.c revision 1.3.2.5 1 1.3.2.5 nathanw /* $NetBSD: core_elf32.c,v 1.3.2.5 2002/07/12 01:40:12 nathanw Exp $ */
2 1.3.2.2 nathanw
3 1.3.2.2 nathanw /*
4 1.3.2.2 nathanw * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.3.2.2 nathanw * All rights reserved.
6 1.3.2.2 nathanw *
7 1.3.2.2 nathanw * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.3.2.2 nathanw *
9 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
11 1.3.2.2 nathanw * are met:
12 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.3.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.3.2.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.3.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.3.2.2 nathanw * must display the following acknowledgement:
19 1.3.2.2 nathanw * This product includes software developed for the NetBSD Project by
20 1.3.2.2 nathanw * Wasabi Systems, Inc.
21 1.3.2.2 nathanw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.3.2.2 nathanw * or promote products derived from this software without specific prior
23 1.3.2.2 nathanw * written permission.
24 1.3.2.2 nathanw *
25 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.3.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.3.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.3.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.3.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.3.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.3.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.3.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.3.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.3.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.3.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
36 1.3.2.2 nathanw */
37 1.3.2.2 nathanw
38 1.3.2.2 nathanw /*
39 1.3.2.2 nathanw * core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
40 1.3.2.2 nathanw */
41 1.3.2.2 nathanw
42 1.3.2.2 nathanw #include <sys/cdefs.h>
43 1.3.2.5 nathanw __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.3.2.5 2002/07/12 01:40:12 nathanw Exp $");
44 1.3.2.2 nathanw
45 1.3.2.2 nathanw /* If not included by core_elf64.c, ELFSIZE won't be defined. */
46 1.3.2.2 nathanw #ifndef ELFSIZE
47 1.3.2.2 nathanw #define ELFSIZE 32
48 1.3.2.2 nathanw #endif
49 1.3.2.2 nathanw
50 1.3.2.2 nathanw #include <sys/param.h>
51 1.3.2.2 nathanw #include <sys/systm.h>
52 1.3.2.2 nathanw #include <sys/proc.h>
53 1.3.2.2 nathanw #include <sys/vnode.h>
54 1.3.2.2 nathanw #include <sys/exec_elf.h>
55 1.3.2.2 nathanw #include <sys/ptrace.h>
56 1.3.2.2 nathanw
57 1.3.2.2 nathanw #include <machine/reg.h>
58 1.3.2.2 nathanw
59 1.3.2.2 nathanw #include <uvm/uvm_extern.h>
60 1.3.2.2 nathanw
61 1.3.2.2 nathanw struct countsegs_state {
62 1.3.2.2 nathanw int npsections;
63 1.3.2.2 nathanw };
64 1.3.2.2 nathanw
65 1.3.2.2 nathanw int ELFNAMEEND(coredump_countsegs)(struct proc *, struct vnode *,
66 1.3.2.2 nathanw struct ucred *, struct uvm_coredump_state *);
67 1.3.2.2 nathanw
68 1.3.2.2 nathanw struct writesegs_state {
69 1.3.2.2 nathanw off_t offset;
70 1.3.2.2 nathanw off_t secoff;
71 1.3.2.2 nathanw };
72 1.3.2.2 nathanw
73 1.3.2.2 nathanw int ELFNAMEEND(coredump_writeseghdrs)(struct proc *, struct vnode *,
74 1.3.2.2 nathanw struct ucred *, struct uvm_coredump_state *);
75 1.3.2.2 nathanw int ELFNAMEEND(coredump_writesegs)(struct proc *, struct vnode *,
76 1.3.2.2 nathanw struct ucred *, struct uvm_coredump_state *);
77 1.3.2.2 nathanw
78 1.3.2.2 nathanw int ELFNAMEEND(coredump_notes)(struct proc *, struct vnode *,
79 1.3.2.2 nathanw struct ucred *, int *, off_t);
80 1.3.2.2 nathanw
81 1.3.2.2 nathanw #define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */
82 1.3.2.2 nathanw #define elfround(x) roundup((x), ELFROUNDSIZE)
83 1.3.2.2 nathanw
84 1.3.2.2 nathanw int
85 1.3.2.3 nathanw ELFNAMEEND(coredump)(struct lwp *l, struct vnode *vp, struct ucred *cred)
86 1.3.2.2 nathanw {
87 1.3.2.3 nathanw struct proc *p;
88 1.3.2.2 nathanw Elf_Ehdr ehdr;
89 1.3.2.2 nathanw Elf_Phdr phdr;
90 1.3.2.2 nathanw struct countsegs_state cs;
91 1.3.2.2 nathanw struct writesegs_state ws;
92 1.3.2.2 nathanw off_t notestart, secstart;
93 1.3.2.2 nathanw int notesize, error;
94 1.3.2.2 nathanw
95 1.3.2.3 nathanw p = l->l_proc;
96 1.3.2.2 nathanw /*
97 1.3.2.2 nathanw * We have to make a total of 3 passes across the map:
98 1.3.2.2 nathanw *
99 1.3.2.2 nathanw * 1. Count the number of map entries (the number of
100 1.3.2.2 nathanw * PT_LOAD sections).
101 1.3.2.2 nathanw *
102 1.3.2.2 nathanw * 2. Write the P-section headers.
103 1.3.2.2 nathanw *
104 1.3.2.2 nathanw * 3. Write the P-sections.
105 1.3.2.2 nathanw */
106 1.3.2.2 nathanw
107 1.3.2.2 nathanw /* Pass 1: count the entries. */
108 1.3.2.2 nathanw cs.npsections = 0;
109 1.3.2.2 nathanw error = uvm_coredump_walkmap(p, vp, cred,
110 1.3.2.2 nathanw ELFNAMEEND(coredump_countsegs), &cs);
111 1.3.2.2 nathanw if (error)
112 1.3.2.2 nathanw return (error);
113 1.3.2.2 nathanw
114 1.3.2.2 nathanw /* Get the size of the notes. */
115 1.3.2.2 nathanw error = ELFNAMEEND(coredump_notes)(p, vp, cred, ¬esize, 0);
116 1.3.2.2 nathanw if (error)
117 1.3.2.2 nathanw return (error);
118 1.3.2.2 nathanw
119 1.3.2.2 nathanw /* Count the PT_NOTE section. */
120 1.3.2.2 nathanw cs.npsections++;
121 1.3.2.2 nathanw
122 1.3.2.2 nathanw memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
123 1.3.2.2 nathanw #if ELFSIZE == 32
124 1.3.2.2 nathanw ehdr.e_ident[EI_CLASS] = ELFCLASS32;
125 1.3.2.2 nathanw #elif ELFSIZE == 64
126 1.3.2.2 nathanw ehdr.e_ident[EI_CLASS] = ELFCLASS64;
127 1.3.2.2 nathanw #endif
128 1.3.2.2 nathanw ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
129 1.3.2.2 nathanw ehdr.e_ident[EI_VERSION] = EV_CURRENT;
130 1.3.2.2 nathanw /* XXX Should be the OSABI/ABI version of the executable. */
131 1.3.2.2 nathanw ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
132 1.3.2.2 nathanw ehdr.e_ident[EI_ABIVERSION] = 0;
133 1.3.2.2 nathanw
134 1.3.2.2 nathanw ehdr.e_type = ET_CORE;
135 1.3.2.2 nathanw /* XXX This should be the e_machine of the executable. */
136 1.3.2.2 nathanw ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
137 1.3.2.2 nathanw ehdr.e_version = EV_CURRENT;
138 1.3.2.2 nathanw ehdr.e_entry = 0;
139 1.3.2.2 nathanw ehdr.e_phoff = sizeof(ehdr);
140 1.3.2.2 nathanw ehdr.e_shoff = 0;
141 1.3.2.2 nathanw ehdr.e_flags = 0;
142 1.3.2.2 nathanw ehdr.e_ehsize = sizeof(ehdr);
143 1.3.2.2 nathanw ehdr.e_phentsize = sizeof(Elf_Phdr);
144 1.3.2.2 nathanw ehdr.e_phnum = cs.npsections;
145 1.3.2.2 nathanw ehdr.e_shentsize = 0;
146 1.3.2.2 nathanw ehdr.e_shnum = 0;
147 1.3.2.2 nathanw ehdr.e_shstrndx = 0;
148 1.3.2.2 nathanw
149 1.3.2.2 nathanw /* Write out the ELF header. */
150 1.3.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&ehdr,
151 1.3.2.2 nathanw (int)sizeof(ehdr), (off_t)0,
152 1.3.2.2 nathanw UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
153 1.3.2.2 nathanw
154 1.3.2.2 nathanw ws.offset = ehdr.e_phoff;
155 1.3.2.2 nathanw notestart = ws.offset + (sizeof(phdr) * cs.npsections);
156 1.3.2.2 nathanw secstart = round_page(notestart + notesize);
157 1.3.2.2 nathanw
158 1.3.2.2 nathanw /* Now write the P-section headers. */
159 1.3.2.2 nathanw ws.secoff = secstart;
160 1.3.2.2 nathanw error = uvm_coredump_walkmap(p, vp, cred,
161 1.3.2.2 nathanw ELFNAMEEND(coredump_writeseghdrs), &ws);
162 1.3.2.2 nathanw if (error)
163 1.3.2.2 nathanw return (error);
164 1.3.2.2 nathanw
165 1.3.2.2 nathanw /* Write out the PT_NOTE header. */
166 1.3.2.2 nathanw phdr.p_type = PT_NOTE;
167 1.3.2.2 nathanw phdr.p_offset = notestart;
168 1.3.2.2 nathanw phdr.p_vaddr = 0;
169 1.3.2.2 nathanw phdr.p_paddr = 0;
170 1.3.2.2 nathanw phdr.p_filesz = notesize;
171 1.3.2.2 nathanw phdr.p_memsz = 0;
172 1.3.2.2 nathanw phdr.p_flags = PF_R;
173 1.3.2.2 nathanw phdr.p_align = ELFROUNDSIZE;
174 1.3.2.2 nathanw
175 1.3.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp,
176 1.3.2.2 nathanw (caddr_t)&phdr, sizeof(phdr),
177 1.3.2.2 nathanw ws.offset, UIO_SYSSPACE,
178 1.3.2.2 nathanw IO_NODELOCKED|IO_UNIT, cred, NULL, p);
179 1.3.2.2 nathanw if (error)
180 1.3.2.2 nathanw return (error);
181 1.3.2.2 nathanw
182 1.3.2.2 nathanw ws.offset += sizeof(phdr);
183 1.3.2.2 nathanw
184 1.3.2.2 nathanw #ifdef DIAGNOSTIC
185 1.3.2.2 nathanw if (ws.offset != notestart)
186 1.3.2.2 nathanw panic("coredump: offset %lld != notestart %lld",
187 1.3.2.2 nathanw (long long) ws.offset, (long long) notestart);
188 1.3.2.2 nathanw #endif
189 1.3.2.2 nathanw
190 1.3.2.2 nathanw /* Write out the notes. */
191 1.3.2.2 nathanw error = ELFNAMEEND(coredump_notes)(p, vp, cred, ¬esize, ws.offset);
192 1.3.2.2 nathanw
193 1.3.2.2 nathanw ws.offset += notesize;
194 1.3.2.2 nathanw
195 1.3.2.2 nathanw #ifdef DIAGNOSTIC
196 1.3.2.2 nathanw if (round_page(ws.offset) != secstart)
197 1.3.2.2 nathanw panic("coredump: offset %lld != secstart %lld",
198 1.3.2.2 nathanw (long long) round_page(ws.offset), (long long) secstart);
199 1.3.2.2 nathanw #endif
200 1.3.2.2 nathanw
201 1.3.2.2 nathanw /* ...and finally, the sections themselves. */
202 1.3.2.2 nathanw ws.secoff = secstart;
203 1.3.2.2 nathanw error = uvm_coredump_walkmap(p, vp, cred,
204 1.3.2.2 nathanw ELFNAMEEND(coredump_writesegs), &ws);
205 1.3.2.2 nathanw if (error)
206 1.3.2.2 nathanw return (error);
207 1.3.2.2 nathanw
208 1.3.2.2 nathanw return (error);
209 1.3.2.2 nathanw }
210 1.3.2.2 nathanw
211 1.3.2.2 nathanw int
212 1.3.2.2 nathanw ELFNAMEEND(coredump_countsegs)(struct proc *p, struct vnode *vp,
213 1.3.2.2 nathanw struct ucred *cred, struct uvm_coredump_state *us)
214 1.3.2.2 nathanw {
215 1.3.2.2 nathanw struct countsegs_state *cs = us->cookie;
216 1.3.2.2 nathanw
217 1.3.2.2 nathanw cs->npsections++;
218 1.3.2.2 nathanw return (0);
219 1.3.2.2 nathanw }
220 1.3.2.2 nathanw
221 1.3.2.2 nathanw int
222 1.3.2.2 nathanw ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, struct vnode *vp,
223 1.3.2.2 nathanw struct ucred *cred, struct uvm_coredump_state *us)
224 1.3.2.2 nathanw {
225 1.3.2.2 nathanw struct writesegs_state *ws = us->cookie;
226 1.3.2.2 nathanw Elf_Phdr phdr;
227 1.3.2.2 nathanw vsize_t size;
228 1.3.2.2 nathanw int error;
229 1.3.2.2 nathanw
230 1.3.2.2 nathanw size = us->end - us->start;
231 1.3.2.2 nathanw
232 1.3.2.2 nathanw phdr.p_type = PT_LOAD;
233 1.3.2.2 nathanw phdr.p_offset = ws->secoff;
234 1.3.2.2 nathanw phdr.p_vaddr = us->start;
235 1.3.2.2 nathanw phdr.p_paddr = 0;
236 1.3.2.2 nathanw phdr.p_filesz = (us->flags & UVM_COREDUMP_NODUMP) ? 0 : size;
237 1.3.2.2 nathanw phdr.p_memsz = size;
238 1.3.2.2 nathanw phdr.p_flags = 0;
239 1.3.2.2 nathanw if (us->prot & VM_PROT_READ)
240 1.3.2.2 nathanw phdr.p_flags |= PF_R;
241 1.3.2.2 nathanw if (us->prot & VM_PROT_WRITE)
242 1.3.2.2 nathanw phdr.p_flags |= PF_W;
243 1.3.2.2 nathanw if (us->prot & VM_PROT_EXECUTE)
244 1.3.2.2 nathanw phdr.p_flags |= PF_X;
245 1.3.2.2 nathanw phdr.p_align = PAGE_SIZE;
246 1.3.2.2 nathanw
247 1.3.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp,
248 1.3.2.2 nathanw (caddr_t)&phdr, sizeof(phdr),
249 1.3.2.2 nathanw ws->offset, UIO_SYSSPACE,
250 1.3.2.2 nathanw IO_NODELOCKED|IO_UNIT, cred, NULL, p);
251 1.3.2.2 nathanw if (error)
252 1.3.2.2 nathanw return (error);
253 1.3.2.2 nathanw
254 1.3.2.2 nathanw ws->offset += sizeof(phdr);
255 1.3.2.2 nathanw ws->secoff += phdr.p_filesz;
256 1.3.2.2 nathanw
257 1.3.2.2 nathanw return (0);
258 1.3.2.2 nathanw }
259 1.3.2.2 nathanw
260 1.3.2.2 nathanw int
261 1.3.2.2 nathanw ELFNAMEEND(coredump_writesegs)(struct proc *p, struct vnode *vp,
262 1.3.2.2 nathanw struct ucred *cred, struct uvm_coredump_state *us)
263 1.3.2.2 nathanw {
264 1.3.2.2 nathanw struct writesegs_state *ws = us->cookie;
265 1.3.2.2 nathanw vsize_t size;
266 1.3.2.2 nathanw int error;
267 1.3.2.2 nathanw
268 1.3.2.2 nathanw if (us->flags & UVM_COREDUMP_NODUMP)
269 1.3.2.2 nathanw return (0);
270 1.3.2.2 nathanw
271 1.3.2.2 nathanw size = us->end - us->start;
272 1.3.2.2 nathanw
273 1.3.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp,
274 1.3.2.2 nathanw (caddr_t) us->start, size,
275 1.3.2.2 nathanw ws->secoff, UIO_USERSPACE,
276 1.3.2.2 nathanw IO_NODELOCKED|IO_UNIT, cred, NULL, p);
277 1.3.2.2 nathanw if (error)
278 1.3.2.2 nathanw return (error);
279 1.3.2.2 nathanw
280 1.3.2.2 nathanw ws->secoff += size;
281 1.3.2.2 nathanw
282 1.3.2.2 nathanw return (0);
283 1.3.2.2 nathanw }
284 1.3.2.2 nathanw
285 1.3.2.2 nathanw int
286 1.3.2.2 nathanw ELFNAMEEND(coredump_notes)(struct proc *p, struct vnode *vp,
287 1.3.2.2 nathanw struct ucred *cred, int *sizep, off_t offset)
288 1.3.2.2 nathanw {
289 1.3.2.2 nathanw struct netbsd_elfcore_procinfo cpi;
290 1.3.2.2 nathanw Elf_Nhdr nhdr;
291 1.3.2.2 nathanw int size, notesize, error;
292 1.3.2.2 nathanw char name[64];
293 1.3.2.2 nathanw int namesize;
294 1.3.2.3 nathanw struct lwp *l;
295 1.3.2.2 nathanw struct reg intreg;
296 1.3.2.2 nathanw #ifdef PT_GETFPREGS
297 1.3.2.2 nathanw struct fpreg freg;
298 1.3.2.2 nathanw #endif
299 1.3.2.2 nathanw
300 1.3.2.2 nathanw size = 0;
301 1.3.2.2 nathanw
302 1.3.2.2 nathanw /* First, write an elfcore_procinfo. */
303 1.3.2.2 nathanw notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
304 1.3.2.2 nathanw elfround(sizeof(cpi));
305 1.3.2.2 nathanw if (offset) {
306 1.3.2.2 nathanw cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
307 1.3.2.2 nathanw cpi.cpi_cpisize = sizeof(cpi);
308 1.3.2.2 nathanw cpi.cpi_signo = p->p_sigctx.ps_sig;
309 1.3.2.2 nathanw cpi.cpi_sigcode = p->p_sigctx.ps_code;
310 1.3.2.2 nathanw
311 1.3.2.2 nathanw memcpy(&cpi.cpi_sigpend, &p->p_sigctx.ps_siglist,
312 1.3.2.2 nathanw sizeof(cpi.cpi_sigpend));
313 1.3.2.2 nathanw memcpy(&cpi.cpi_sigmask, &p->p_sigctx.ps_sigmask,
314 1.3.2.2 nathanw sizeof(cpi.cpi_sigmask));
315 1.3.2.2 nathanw memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
316 1.3.2.2 nathanw sizeof(cpi.cpi_sigignore));
317 1.3.2.2 nathanw memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
318 1.3.2.2 nathanw sizeof(cpi.cpi_sigcatch));
319 1.3.2.2 nathanw
320 1.3.2.2 nathanw cpi.cpi_pid = p->p_pid;
321 1.3.2.2 nathanw cpi.cpi_ppid = p->p_pptr->p_pid;
322 1.3.2.2 nathanw cpi.cpi_pgrp = p->p_pgid;
323 1.3.2.2 nathanw cpi.cpi_sid = p->p_session->s_sid;
324 1.3.2.2 nathanw
325 1.3.2.2 nathanw cpi.cpi_ruid = p->p_cred->p_ruid;
326 1.3.2.2 nathanw cpi.cpi_euid = p->p_ucred->cr_uid;
327 1.3.2.2 nathanw cpi.cpi_svuid = p->p_cred->p_svuid;
328 1.3.2.2 nathanw
329 1.3.2.2 nathanw cpi.cpi_rgid = p->p_cred->p_rgid;
330 1.3.2.2 nathanw cpi.cpi_egid = p->p_ucred->cr_gid;
331 1.3.2.2 nathanw cpi.cpi_svgid = p->p_cred->p_svgid;
332 1.3.2.2 nathanw
333 1.3.2.3 nathanw cpi.cpi_nlwps = p->p_nlwps;
334 1.3.2.2 nathanw strcpy(cpi.cpi_name, p->p_comm);
335 1.3.2.2 nathanw
336 1.3.2.2 nathanw nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
337 1.3.2.2 nathanw nhdr.n_descsz = sizeof(cpi);
338 1.3.2.2 nathanw nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
339 1.3.2.2 nathanw
340 1.3.2.2 nathanw error = ELFNAMEEND(coredump_writenote)(p, vp, cred, offset,
341 1.3.2.2 nathanw &nhdr, ELF_NOTE_NETBSD_CORE_NAME, &cpi);
342 1.3.2.2 nathanw if (error)
343 1.3.2.2 nathanw return (error);
344 1.3.2.2 nathanw
345 1.3.2.2 nathanw offset += notesize;
346 1.3.2.2 nathanw }
347 1.3.2.2 nathanw
348 1.3.2.2 nathanw size += notesize;
349 1.3.2.2 nathanw
350 1.3.2.2 nathanw /* XXX Add hook for machdep per-proc notes. */
351 1.3.2.2 nathanw
352 1.3.2.2 nathanw /*
353 1.3.2.2 nathanw * Now, for each LWP, write the register info and any other
354 1.3.2.2 nathanw * per-LWP notes.
355 1.3.2.2 nathanw */
356 1.3.2.3 nathanw LIST_FOREACH(l, &p->p_lwps, l_sibling) {
357 1.3.2.3 nathanw sprintf(name, "%s@%d", ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
358 1.3.2.2 nathanw namesize = strlen(name) + 1;
359 1.3.2.2 nathanw
360 1.3.2.2 nathanw notesize = sizeof(nhdr) + elfround(namesize) +
361 1.3.2.2 nathanw elfround(sizeof(intreg));
362 1.3.2.2 nathanw if (offset) {
363 1.3.2.4 nathanw PHOLD(l);
364 1.3.2.3 nathanw error = process_read_regs(l, &intreg);
365 1.3.2.4 nathanw PRELE(l);
366 1.3.2.2 nathanw if (error)
367 1.3.2.2 nathanw return (error);
368 1.3.2.2 nathanw
369 1.3.2.2 nathanw nhdr.n_namesz = namesize;
370 1.3.2.2 nathanw nhdr.n_descsz = sizeof(intreg);
371 1.3.2.2 nathanw nhdr.n_type = PT_GETREGS;
372 1.3.2.2 nathanw
373 1.3.2.2 nathanw error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
374 1.3.2.2 nathanw offset, &nhdr, name, &intreg);
375 1.3.2.2 nathanw if (error)
376 1.3.2.2 nathanw return (error);
377 1.3.2.2 nathanw
378 1.3.2.2 nathanw offset += notesize;
379 1.3.2.2 nathanw }
380 1.3.2.2 nathanw size += notesize;
381 1.3.2.2 nathanw
382 1.3.2.2 nathanw #ifdef PT_GETFPREGS
383 1.3.2.2 nathanw notesize = sizeof(nhdr) + elfround(namesize) +
384 1.3.2.2 nathanw elfround(sizeof(freg));
385 1.3.2.2 nathanw if (offset) {
386 1.3.2.4 nathanw PHOLD(l);
387 1.3.2.3 nathanw error = process_read_fpregs(l, &freg);
388 1.3.2.4 nathanw PRELE(l);
389 1.3.2.2 nathanw if (error)
390 1.3.2.2 nathanw return (error);
391 1.3.2.2 nathanw
392 1.3.2.2 nathanw nhdr.n_namesz = namesize;
393 1.3.2.2 nathanw nhdr.n_descsz = sizeof(freg);
394 1.3.2.2 nathanw nhdr.n_type = PT_GETFPREGS;
395 1.3.2.2 nathanw
396 1.3.2.2 nathanw error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
397 1.3.2.2 nathanw offset, &nhdr, name, &freg);
398 1.3.2.2 nathanw if (error)
399 1.3.2.2 nathanw return (error);
400 1.3.2.2 nathanw
401 1.3.2.2 nathanw offset += notesize;
402 1.3.2.2 nathanw }
403 1.3.2.2 nathanw size += notesize;
404 1.3.2.2 nathanw #endif
405 1.3.2.2 nathanw /* XXX Add hook for machdep per-LWP notes. */
406 1.3.2.3 nathanw }
407 1.3.2.2 nathanw
408 1.3.2.2 nathanw *sizep = size;
409 1.3.2.2 nathanw return (0);
410 1.3.2.2 nathanw }
411 1.3.2.2 nathanw
412 1.3.2.2 nathanw int
413 1.3.2.2 nathanw ELFNAMEEND(coredump_writenote)(struct proc *p, struct vnode *vp,
414 1.3.2.2 nathanw struct ucred *cred, off_t offset, Elf_Nhdr *nhdr, const char *name,
415 1.3.2.2 nathanw void *data)
416 1.3.2.2 nathanw {
417 1.3.2.2 nathanw int error;
418 1.3.2.2 nathanw
419 1.3.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp,
420 1.3.2.2 nathanw (caddr_t) nhdr, sizeof(*nhdr),
421 1.3.2.2 nathanw offset, UIO_SYSSPACE,
422 1.3.2.2 nathanw IO_NODELOCKED|IO_UNIT, cred, NULL, p);
423 1.3.2.2 nathanw if (error)
424 1.3.2.2 nathanw return (error);
425 1.3.2.2 nathanw
426 1.3.2.2 nathanw offset += sizeof(*nhdr);
427 1.3.2.2 nathanw
428 1.3.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp,
429 1.3.2.2 nathanw (caddr_t)name, nhdr->n_namesz,
430 1.3.2.2 nathanw offset, UIO_SYSSPACE,
431 1.3.2.2 nathanw IO_NODELOCKED|IO_UNIT, cred, NULL, p);
432 1.3.2.2 nathanw if (error)
433 1.3.2.2 nathanw return (error);
434 1.3.2.2 nathanw
435 1.3.2.2 nathanw offset += elfround(nhdr->n_namesz);
436 1.3.2.2 nathanw
437 1.3.2.2 nathanw error = vn_rdwr(UIO_WRITE, vp,
438 1.3.2.2 nathanw data, nhdr->n_descsz,
439 1.3.2.2 nathanw offset, UIO_SYSSPACE,
440 1.3.2.2 nathanw IO_NODELOCKED|IO_UNIT, cred, NULL, p);
441 1.3.2.2 nathanw
442 1.3.2.2 nathanw return (error);
443 1.3.2.2 nathanw }
444