core_elf32.c revision 1.54 1 1.54 christos /* $NetBSD: core_elf32.c,v 1.54 2017/03/30 20:17:11 christos Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.1 thorpej /*
39 1.1 thorpej * core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
40 1.1 thorpej */
41 1.1 thorpej
42 1.1 thorpej #include <sys/cdefs.h>
43 1.54 christos __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.54 2017/03/30 20:17:11 christos Exp $");
44 1.33 ad
45 1.33 ad #ifdef _KERNEL_OPT
46 1.33 ad #include "opt_coredump.h"
47 1.47 christos #include "opt_compat_netbsd32.h"
48 1.33 ad #endif
49 1.1 thorpej
50 1.1 thorpej #ifndef ELFSIZE
51 1.1 thorpej #define ELFSIZE 32
52 1.1 thorpej #endif
53 1.1 thorpej
54 1.1 thorpej #include <sys/param.h>
55 1.1 thorpej #include <sys/systm.h>
56 1.1 thorpej #include <sys/proc.h>
57 1.1 thorpej #include <sys/vnode.h>
58 1.18 matt #include <sys/exec.h>
59 1.1 thorpej #include <sys/exec_elf.h>
60 1.1 thorpej #include <sys/ptrace.h>
61 1.36 para #include <sys/kmem.h>
62 1.25 elad #include <sys/kauth.h>
63 1.1 thorpej
64 1.1 thorpej #include <machine/reg.h>
65 1.1 thorpej
66 1.2 thorpej #include <uvm/uvm_extern.h>
67 1.2 thorpej
68 1.33 ad #ifdef COREDUMP
69 1.33 ad
70 1.2 thorpej struct writesegs_state {
71 1.16 matt Elf_Phdr *psections;
72 1.40 dsl proc_t *p;
73 1.40 dsl off_t secoff;
74 1.40 dsl size_t npsections;
75 1.39 dsl };
76 1.39 dsl
77 1.39 dsl /*
78 1.39 dsl * We need to know how big the 'notes' are before we write the main header.
79 1.39 dsl * To avoid problems with double-processing we save the data.
80 1.39 dsl */
81 1.39 dsl struct note_buf {
82 1.39 dsl struct note_buf *nb_next;
83 1.39 dsl unsigned char nb_data[4096 - sizeof (void *)];
84 1.39 dsl };
85 1.39 dsl
86 1.39 dsl struct note_state {
87 1.39 dsl struct note_buf *ns_first;
88 1.39 dsl struct note_buf *ns_last;
89 1.39 dsl unsigned int ns_count; /* Of full buffers */
90 1.39 dsl unsigned int ns_offset; /* Write point in last buffer */
91 1.2 thorpej };
92 1.2 thorpej
93 1.40 dsl static int ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *);
94 1.22 thorpej
95 1.39 dsl static int ELFNAMEEND(coredump_notes)(struct lwp *, struct note_state *);
96 1.39 dsl static int ELFNAMEEND(coredump_note)(struct lwp *, struct note_state *);
97 1.1 thorpej
98 1.39 dsl /* The 'note' section names and data are always 4-byte aligned. */
99 1.1 thorpej #define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */
100 1.1 thorpej
101 1.23 cube #define elf_process_read_regs CONCAT(process_read_regs, ELFSIZE)
102 1.23 cube #define elf_process_read_fpregs CONCAT(process_read_fpregs, ELFSIZE)
103 1.23 cube #define elf_reg CONCAT(process_reg, ELFSIZE)
104 1.23 cube #define elf_fpreg CONCAT(process_fpreg, ELFSIZE)
105 1.23 cube
106 1.1 thorpej int
107 1.37 dsl ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
108 1.1 thorpej {
109 1.1 thorpej Elf_Ehdr ehdr;
110 1.45 matt Elf_Shdr shdr;
111 1.39 dsl Elf_Phdr *psections;
112 1.36 para size_t psectionssize;
113 1.38 dsl int npsections;
114 1.2 thorpej struct writesegs_state ws;
115 1.39 dsl off_t notestart;
116 1.6 matt size_t notesize;
117 1.16 matt int error, i;
118 1.1 thorpej
119 1.39 dsl struct note_state ns;
120 1.39 dsl struct note_buf *nb;
121 1.39 dsl
122 1.16 matt psections = NULL;
123 1.39 dsl
124 1.39 dsl /* Get all of the notes (mostly all the registers). */
125 1.39 dsl ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
126 1.39 dsl ns.ns_last = ns.ns_first;
127 1.39 dsl ns.ns_count = 0;
128 1.39 dsl ns.ns_offset = 0;
129 1.39 dsl error = ELFNAMEEND(coredump_notes)(l, &ns);
130 1.39 dsl ns.ns_last->nb_next = NULL;
131 1.39 dsl if (error)
132 1.39 dsl goto out;
133 1.39 dsl notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
134 1.39 dsl
135 1.1 thorpej /*
136 1.1 thorpej * We have to make a total of 3 passes across the map:
137 1.1 thorpej *
138 1.1 thorpej * 1. Count the number of map entries (the number of
139 1.38 dsl * PT_LOAD sections in the dump).
140 1.1 thorpej *
141 1.1 thorpej * 2. Write the P-section headers.
142 1.1 thorpej *
143 1.1 thorpej * 3. Write the P-sections.
144 1.1 thorpej */
145 1.1 thorpej
146 1.1 thorpej /* Pass 1: count the entries. */
147 1.39 dsl npsections = uvm_coredump_count_segs(l->l_proc);
148 1.39 dsl /* Allow for the PT_NOTE section. */
149 1.38 dsl npsections++;
150 1.1 thorpej
151 1.39 dsl /* Build the main elf header */
152 1.18 matt memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
153 1.1 thorpej memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
154 1.1 thorpej #if ELFSIZE == 32
155 1.1 thorpej ehdr.e_ident[EI_CLASS] = ELFCLASS32;
156 1.1 thorpej #elif ELFSIZE == 64
157 1.1 thorpej ehdr.e_ident[EI_CLASS] = ELFCLASS64;
158 1.1 thorpej #endif
159 1.1 thorpej ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
160 1.1 thorpej ehdr.e_ident[EI_VERSION] = EV_CURRENT;
161 1.1 thorpej /* XXX Should be the OSABI/ABI version of the executable. */
162 1.1 thorpej ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
163 1.2 thorpej ehdr.e_ident[EI_ABIVERSION] = 0;
164 1.1 thorpej
165 1.1 thorpej ehdr.e_type = ET_CORE;
166 1.1 thorpej /* XXX This should be the e_machine of the executable. */
167 1.1 thorpej ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
168 1.1 thorpej ehdr.e_version = EV_CURRENT;
169 1.1 thorpej ehdr.e_entry = 0;
170 1.1 thorpej ehdr.e_flags = 0;
171 1.1 thorpej ehdr.e_ehsize = sizeof(ehdr);
172 1.1 thorpej ehdr.e_phentsize = sizeof(Elf_Phdr);
173 1.45 matt if (npsections < PN_XNUM) {
174 1.45 matt ehdr.e_phnum = npsections;
175 1.45 matt ehdr.e_shentsize = 0;
176 1.45 matt ehdr.e_shnum = 0;
177 1.45 matt ehdr.e_shoff = 0;
178 1.45 matt ehdr.e_phoff = sizeof(ehdr);
179 1.45 matt } else {
180 1.45 matt ehdr.e_phnum = PN_XNUM;
181 1.45 matt ehdr.e_shentsize = sizeof(Elf_Shdr);
182 1.45 matt ehdr.e_shnum = 1;
183 1.45 matt ehdr.e_shoff = sizeof(ehdr);
184 1.45 matt ehdr.e_phoff = sizeof(ehdr) + sizeof(shdr);
185 1.45 matt }
186 1.1 thorpej ehdr.e_shstrndx = 0;
187 1.1 thorpej
188 1.35 matt #ifdef ELF_MD_COREDUMP_SETUP
189 1.35 matt ELF_MD_COREDUMP_SETUP(l, &ehdr);
190 1.35 matt #endif
191 1.35 matt
192 1.1 thorpej /* Write out the ELF header. */
193 1.18 matt error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
194 1.16 matt if (error)
195 1.16 matt goto out;
196 1.16 matt
197 1.45 matt /* Write out sections, if needed */
198 1.45 matt if (npsections >= PN_XNUM) {
199 1.45 matt memset(&shdr, 0, sizeof(shdr));
200 1.45 matt shdr.sh_type = SHT_NULL;
201 1.45 matt shdr.sh_info = npsections;
202 1.45 matt error = coredump_write(cookie, UIO_SYSSPACE, &shdr,
203 1.45 matt sizeof(shdr));
204 1.45 matt if (error)
205 1.45 matt goto out;
206 1.45 matt }
207 1.45 matt
208 1.39 dsl psectionssize = npsections * sizeof(*psections);
209 1.45 matt notestart = ehdr.e_phoff + psectionssize;
210 1.1 thorpej
211 1.36 para psections = kmem_zalloc(psectionssize, KM_SLEEP);
212 1.1 thorpej
213 1.38 dsl /* Pass 2: now find the P-section headers. */
214 1.39 dsl ws.secoff = notestart + notesize;
215 1.16 matt ws.psections = psections;
216 1.39 dsl ws.npsections = npsections - 1;
217 1.41 dsl ws.p = l->l_proc;
218 1.39 dsl error = uvm_coredump_walkmap(l->l_proc, ELFNAMEEND(coredump_getseghdrs),
219 1.39 dsl &ws);
220 1.2 thorpej if (error)
221 1.16 matt goto out;
222 1.39 dsl if (ws.npsections != 0) {
223 1.39 dsl /* A section went away */
224 1.39 dsl error = ENOMEM;
225 1.39 dsl goto out;
226 1.39 dsl }
227 1.1 thorpej
228 1.38 dsl /* Add the PT_NOTE header after the P-section headers. */
229 1.16 matt ws.psections->p_type = PT_NOTE;
230 1.16 matt ws.psections->p_offset = notestart;
231 1.16 matt ws.psections->p_vaddr = 0;
232 1.16 matt ws.psections->p_paddr = 0;
233 1.16 matt ws.psections->p_filesz = notesize;
234 1.16 matt ws.psections->p_memsz = 0;
235 1.16 matt ws.psections->p_flags = PF_R;
236 1.16 matt ws.psections->p_align = ELFROUNDSIZE;
237 1.1 thorpej
238 1.39 dsl /* Write the P-section headers followed by the PT_NOTE header */
239 1.39 dsl error = coredump_write(cookie, UIO_SYSSPACE, psections, psectionssize);
240 1.1 thorpej if (error)
241 1.16 matt goto out;
242 1.1 thorpej
243 1.18 matt #ifdef DIAGNOSTIC
244 1.39 dsl if (coredump_offset(cookie) != notestart)
245 1.2 thorpej panic("coredump: offset %lld != notestart %lld",
246 1.39 dsl (long long) coredump_offset(cookie),
247 1.39 dsl (long long) notestart);
248 1.2 thorpej #endif
249 1.1 thorpej
250 1.1 thorpej /* Write out the notes. */
251 1.39 dsl for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
252 1.39 dsl error = coredump_write(cookie, UIO_SYSSPACE, nb->nb_data,
253 1.39 dsl nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data);
254 1.39 dsl if (error)
255 1.39 dsl goto out;
256 1.39 dsl }
257 1.2 thorpej
258 1.39 dsl /* Finally, write the sections themselves. */
259 1.38 dsl for (i = 0; i < npsections - 1; i++) {
260 1.16 matt if (psections[i].p_filesz == 0)
261 1.16 matt continue;
262 1.16 matt
263 1.16 matt #ifdef DIAGNOSTIC
264 1.39 dsl if (coredump_offset(cookie) != psections[i].p_offset)
265 1.16 matt panic("coredump: offset %lld != p_offset[%d] %lld",
266 1.39 dsl (long long) coredump_offset(cookie), i,
267 1.16 matt (long long) psections[i].p_filesz);
268 1.16 matt #endif
269 1.16 matt
270 1.18 matt error = coredump_write(cookie, UIO_USERSPACE,
271 1.18 matt (void *)(vaddr_t)psections[i].p_vaddr,
272 1.18 matt psections[i].p_filesz);
273 1.16 matt if (error)
274 1.16 matt goto out;
275 1.16 matt }
276 1.1 thorpej
277 1.16 matt out:
278 1.16 matt if (psections)
279 1.36 para kmem_free(psections, psectionssize);
280 1.44 dsl while ((nb = ns.ns_first) != NULL) {
281 1.44 dsl ns.ns_first = nb->nb_next;
282 1.39 dsl kmem_free(nb, sizeof *nb);
283 1.43 mrg }
284 1.2 thorpej return (error);
285 1.2 thorpej }
286 1.2 thorpej
287 1.22 thorpej static int
288 1.40 dsl ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *us)
289 1.2 thorpej {
290 1.2 thorpej struct writesegs_state *ws = us->cookie;
291 1.2 thorpej Elf_Phdr phdr;
292 1.16 matt vsize_t size, realsize;
293 1.16 matt vaddr_t end;
294 1.2 thorpej int error;
295 1.2 thorpej
296 1.39 dsl /* Don't overrun if there are more sections */
297 1.39 dsl if (ws->npsections == 0)
298 1.39 dsl return ENOMEM;
299 1.39 dsl ws->npsections--;
300 1.39 dsl
301 1.2 thorpej size = us->end - us->start;
302 1.16 matt realsize = us->realend - us->start;
303 1.16 matt end = us->realend;
304 1.16 matt
305 1.38 dsl /* Don't bother writing out trailing zeros */
306 1.16 matt while (realsize > 0) {
307 1.16 matt long buf[1024 / sizeof(long)];
308 1.16 matt size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
309 1.16 matt const long *ep;
310 1.16 matt int i;
311 1.16 matt
312 1.16 matt end -= slen;
313 1.40 dsl if ((error = copyin_proc(ws->p, (void *)end, buf, slen)) != 0)
314 1.21 christos return error;
315 1.16 matt
316 1.16 matt ep = (const long *) &buf[slen / sizeof(buf[0])];
317 1.16 matt for (i = 0, ep--; buf <= ep; ep--, i++) {
318 1.16 matt if (*ep)
319 1.16 matt break;
320 1.16 matt }
321 1.16 matt realsize -= i * sizeof(buf[0]);
322 1.16 matt if (i * sizeof(buf[0]) < slen)
323 1.16 matt break;
324 1.16 matt }
325 1.2 thorpej
326 1.2 thorpej phdr.p_type = PT_LOAD;
327 1.2 thorpej phdr.p_offset = ws->secoff;
328 1.2 thorpej phdr.p_vaddr = us->start;
329 1.2 thorpej phdr.p_paddr = 0;
330 1.16 matt phdr.p_filesz = realsize;
331 1.2 thorpej phdr.p_memsz = size;
332 1.2 thorpej phdr.p_flags = 0;
333 1.2 thorpej if (us->prot & VM_PROT_READ)
334 1.2 thorpej phdr.p_flags |= PF_R;
335 1.2 thorpej if (us->prot & VM_PROT_WRITE)
336 1.2 thorpej phdr.p_flags |= PF_W;
337 1.2 thorpej if (us->prot & VM_PROT_EXECUTE)
338 1.2 thorpej phdr.p_flags |= PF_X;
339 1.2 thorpej phdr.p_align = PAGE_SIZE;
340 1.2 thorpej
341 1.2 thorpej ws->secoff += phdr.p_filesz;
342 1.16 matt *ws->psections++ = phdr;
343 1.1 thorpej
344 1.1 thorpej return (0);
345 1.1 thorpej }
346 1.1 thorpej
347 1.46 christos static void
348 1.46 christos coredump_note_procinfo(struct lwp *l, struct note_state *ns)
349 1.1 thorpej {
350 1.39 dsl struct proc *p;
351 1.1 thorpej struct netbsd_elfcore_procinfo cpi;
352 1.6 matt struct lwp *l0;
353 1.29 ad sigset_t ss1, ss2;
354 1.1 thorpej
355 1.39 dsl p = l->l_proc;
356 1.1 thorpej
357 1.1 thorpej /* First, write an elfcore_procinfo. */
358 1.39 dsl cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
359 1.39 dsl cpi.cpi_cpisize = sizeof(cpi);
360 1.50 kamil cpi.cpi_signo = p->p_sigctx.ps_info._signo;
361 1.50 kamil cpi.cpi_sigcode = p->p_sigctx.ps_info._code;
362 1.39 dsl cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
363 1.1 thorpej
364 1.39 dsl /*
365 1.39 dsl * XXX This should be per-LWP.
366 1.39 dsl */
367 1.39 dsl ss1 = p->p_sigpend.sp_set;
368 1.39 dsl sigemptyset(&ss2);
369 1.39 dsl LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
370 1.39 dsl sigplusset(&l0->l_sigpend.sp_set, &ss1);
371 1.39 dsl sigplusset(&l0->l_sigmask, &ss2);
372 1.1 thorpej }
373 1.39 dsl memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
374 1.39 dsl memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
375 1.39 dsl memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
376 1.39 dsl sizeof(cpi.cpi_sigignore));
377 1.39 dsl memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
378 1.39 dsl sizeof(cpi.cpi_sigcatch));
379 1.39 dsl
380 1.39 dsl cpi.cpi_pid = p->p_pid;
381 1.39 dsl mutex_enter(proc_lock);
382 1.39 dsl cpi.cpi_ppid = p->p_pptr->p_pid;
383 1.39 dsl cpi.cpi_pgrp = p->p_pgid;
384 1.39 dsl cpi.cpi_sid = p->p_session->s_sid;
385 1.39 dsl mutex_exit(proc_lock);
386 1.39 dsl
387 1.39 dsl cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
388 1.39 dsl cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
389 1.39 dsl cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
390 1.39 dsl
391 1.39 dsl cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
392 1.39 dsl cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
393 1.39 dsl cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
394 1.39 dsl
395 1.39 dsl cpi.cpi_nlwps = p->p_nlwps;
396 1.39 dsl (void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
397 1.39 dsl cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
398 1.39 dsl
399 1.41 dsl ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
400 1.41 dsl ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
401 1.46 christos }
402 1.46 christos
403 1.46 christos static int
404 1.46 christos coredump_note_auxv(struct lwp *l, struct note_state *ns)
405 1.46 christos {
406 1.46 christos int error;
407 1.54 christos size_t len;
408 1.54 christos void *kauxv;
409 1.46 christos
410 1.54 christos if ((error = proc_getauxv(l->l_proc, &kauxv, &len)) != 0)
411 1.46 christos return error;
412 1.46 christos
413 1.54 christos ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_AUXV,
414 1.54 christos ELF_NOTE_NETBSD_CORE_NAME, kauxv, len);
415 1.46 christos
416 1.46 christos kmem_free(kauxv, len);
417 1.54 christos return 0;
418 1.46 christos }
419 1.46 christos
420 1.46 christos static int
421 1.46 christos ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
422 1.46 christos {
423 1.46 christos int error;
424 1.46 christos struct lwp *l0;
425 1.46 christos struct proc *p = l->l_proc;
426 1.46 christos
427 1.46 christos coredump_note_procinfo(l, ns);
428 1.46 christos error = coredump_note_auxv(l, ns);
429 1.46 christos if (error)
430 1.46 christos return error;
431 1.1 thorpej
432 1.1 thorpej /* XXX Add hook for machdep per-proc notes. */
433 1.1 thorpej
434 1.1 thorpej /*
435 1.6 matt * Now write the register info for the thread that caused the
436 1.6 matt * coredump.
437 1.6 matt */
438 1.39 dsl error = ELFNAMEEND(coredump_note)(l, ns);
439 1.6 matt if (error)
440 1.46 christos return error;
441 1.6 matt
442 1.6 matt /*
443 1.1 thorpej * Now, for each LWP, write the register info and any other
444 1.40 dsl * per-LWP notes.
445 1.40 dsl * Lock in case this is a gcore requested dump.
446 1.1 thorpej */
447 1.40 dsl mutex_enter(p->p_lock);
448 1.6 matt LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
449 1.6 matt if (l0 == l) /* we've taken care of this thread */
450 1.6 matt continue;
451 1.39 dsl error = ELFNAMEEND(coredump_note)(l0, ns);
452 1.6 matt if (error)
453 1.40 dsl break;
454 1.6 matt }
455 1.40 dsl mutex_exit(p->p_lock);
456 1.6 matt
457 1.40 dsl return error;
458 1.6 matt }
459 1.1 thorpej
460 1.22 thorpej static int
461 1.39 dsl ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
462 1.6 matt {
463 1.39 dsl int error;
464 1.39 dsl char name[64];
465 1.23 cube elf_reg intreg;
466 1.1 thorpej #ifdef PT_GETFPREGS
467 1.23 cube elf_fpreg freg;
468 1.39 dsl size_t freglen;
469 1.6 matt #endif
470 1.6 matt
471 1.39 dsl snprintf(name, sizeof(name), "%s@%d",
472 1.18 matt ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
473 1.6 matt
474 1.39 dsl error = elf_process_read_regs(l, &intreg);
475 1.39 dsl if (error)
476 1.39 dsl return (error);
477 1.6 matt
478 1.41 dsl ELFNAMEEND(coredump_savenote)(ns, PT_GETREGS, name, &intreg,
479 1.41 dsl sizeof(intreg));
480 1.6 matt
481 1.6 matt #ifdef PT_GETFPREGS
482 1.39 dsl freglen = sizeof(freg);
483 1.42 dsl error = elf_process_read_fpregs(l, &freg, &freglen);
484 1.39 dsl if (error)
485 1.39 dsl return (error);
486 1.6 matt
487 1.41 dsl ELFNAMEEND(coredump_savenote)(ns, PT_GETFPREGS, name, &freg, freglen);
488 1.6 matt #endif
489 1.6 matt /* XXX Add hook for machdep per-LWP notes. */
490 1.1 thorpej return (0);
491 1.1 thorpej }
492 1.1 thorpej
493 1.39 dsl static void
494 1.39 dsl save_note_bytes(struct note_state *ns, const void *data, size_t len)
495 1.1 thorpej {
496 1.39 dsl struct note_buf *nb = ns->ns_last;
497 1.39 dsl size_t copylen;
498 1.39 dsl unsigned char *wp;
499 1.39 dsl
500 1.39 dsl /*
501 1.39 dsl * Just copy the data into a buffer list.
502 1.39 dsl * All but the last buffer is full.
503 1.39 dsl */
504 1.39 dsl for (;;) {
505 1.39 dsl copylen = min(len, sizeof nb->nb_data - ns->ns_offset);
506 1.39 dsl wp = nb->nb_data + ns->ns_offset;
507 1.39 dsl memcpy(wp, data, copylen);
508 1.39 dsl if (copylen == len)
509 1.39 dsl break;
510 1.39 dsl nb->nb_next = kmem_alloc(sizeof *nb->nb_next, KM_SLEEP);
511 1.39 dsl nb = nb->nb_next;
512 1.39 dsl ns->ns_last = nb;
513 1.39 dsl ns->ns_count++;
514 1.39 dsl ns->ns_offset = 0;
515 1.39 dsl len -= copylen;
516 1.39 dsl data = (const unsigned char *)data + copylen;
517 1.39 dsl }
518 1.1 thorpej
519 1.39 dsl while (copylen & (ELFROUNDSIZE - 1))
520 1.39 dsl wp[copylen++] = 0;
521 1.1 thorpej
522 1.39 dsl ns->ns_offset += copylen;
523 1.39 dsl }
524 1.1 thorpej
525 1.39 dsl void
526 1.41 dsl ELFNAMEEND(coredump_savenote)(struct note_state *ns, unsigned int type,
527 1.41 dsl const char *name, void *data, size_t data_len)
528 1.39 dsl {
529 1.41 dsl Elf_Nhdr nhdr;
530 1.41 dsl
531 1.41 dsl nhdr.n_namesz = strlen(name) + 1;
532 1.41 dsl nhdr.n_descsz = data_len;
533 1.41 dsl nhdr.n_type = type;
534 1.41 dsl
535 1.41 dsl save_note_bytes(ns, &nhdr, sizeof (nhdr));
536 1.41 dsl save_note_bytes(ns, name, nhdr.n_namesz);
537 1.41 dsl save_note_bytes(ns, data, data_len);
538 1.1 thorpej }
539 1.33 ad
540 1.33 ad #else /* COREDUMP */
541 1.33 ad
542 1.33 ad int
543 1.48 dholland ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
544 1.33 ad {
545 1.33 ad
546 1.33 ad return ENOSYS;
547 1.33 ad }
548 1.33 ad
549 1.33 ad #endif /* COREDUMP */
550