core_elf32.c revision 1.36.6.2 1 1.36.6.1 tls /* $NetBSD: core_elf32.c,v 1.36.6.2 2017/12/03 11:38:44 jdolecek 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.36.6.1 tls __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.36.6.2 2017/12/03 11:38:44 jdolecek Exp $");
44 1.33 ad
45 1.33 ad #ifdef _KERNEL_OPT
46 1.33 ad #include "opt_coredump.h"
47 1.36.6.2 jdolecek #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.36.6.1 tls struct writesegs_state {
71 1.36.6.1 tls Elf_Phdr *psections;
72 1.36.6.1 tls proc_t *p;
73 1.36.6.1 tls off_t secoff;
74 1.36.6.1 tls size_t npsections;
75 1.2 thorpej };
76 1.2 thorpej
77 1.36.6.1 tls /*
78 1.36.6.1 tls * We need to know how big the 'notes' are before we write the main header.
79 1.36.6.1 tls * To avoid problems with double-processing we save the data.
80 1.36.6.1 tls */
81 1.36.6.1 tls struct note_buf {
82 1.36.6.1 tls struct note_buf *nb_next;
83 1.36.6.1 tls unsigned char nb_data[4096 - sizeof (void *)];
84 1.36.6.1 tls };
85 1.2 thorpej
86 1.36.6.1 tls struct note_state {
87 1.36.6.1 tls struct note_buf *ns_first;
88 1.36.6.1 tls struct note_buf *ns_last;
89 1.36.6.1 tls unsigned int ns_count; /* Of full buffers */
90 1.36.6.1 tls unsigned int ns_offset; /* Write point in last buffer */
91 1.2 thorpej };
92 1.2 thorpej
93 1.36.6.1 tls static int ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *);
94 1.22 thorpej
95 1.36.6.1 tls static int ELFNAMEEND(coredump_notes)(struct lwp *, struct note_state *);
96 1.36.6.1 tls static int ELFNAMEEND(coredump_note)(struct lwp *, struct note_state *);
97 1.1 thorpej
98 1.36.6.1 tls /* 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.36.6.1 tls ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
108 1.1 thorpej {
109 1.1 thorpej Elf_Ehdr ehdr;
110 1.36.6.1 tls Elf_Shdr shdr;
111 1.36.6.1 tls Elf_Phdr *psections;
112 1.36 para size_t psectionssize;
113 1.36.6.1 tls int npsections;
114 1.2 thorpej struct writesegs_state ws;
115 1.36.6.1 tls off_t notestart;
116 1.6 matt size_t notesize;
117 1.16 matt int error, i;
118 1.1 thorpej
119 1.36.6.1 tls struct note_state ns;
120 1.36.6.1 tls struct note_buf *nb;
121 1.36.6.1 tls
122 1.16 matt psections = NULL;
123 1.36.6.1 tls
124 1.36.6.1 tls /* Get all of the notes (mostly all the registers). */
125 1.36.6.1 tls ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
126 1.36.6.1 tls ns.ns_last = ns.ns_first;
127 1.36.6.1 tls ns.ns_count = 0;
128 1.36.6.1 tls ns.ns_offset = 0;
129 1.36.6.1 tls error = ELFNAMEEND(coredump_notes)(l, &ns);
130 1.36.6.1 tls ns.ns_last->nb_next = NULL;
131 1.36.6.1 tls if (error)
132 1.36.6.1 tls goto out;
133 1.36.6.1 tls notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
134 1.36.6.1 tls
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.36.6.1 tls * 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.36.6.1 tls npsections = uvm_coredump_count_segs(l->l_proc);
148 1.36.6.1 tls /* Allow for the PT_NOTE section. */
149 1.36.6.1 tls npsections++;
150 1.1 thorpej
151 1.36.6.1 tls /* 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.36.6.2 jdolecek /*
162 1.36.6.2 jdolecek * NetBSD sets generic SYSV OSABI and ABI version 0
163 1.36.6.2 jdolecek * Native ELF files are distinguishable with NetBSD specific notes
164 1.36.6.2 jdolecek */
165 1.1 thorpej ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
166 1.2 thorpej ehdr.e_ident[EI_ABIVERSION] = 0;
167 1.1 thorpej
168 1.1 thorpej ehdr.e_type = ET_CORE;
169 1.1 thorpej /* XXX This should be the e_machine of the executable. */
170 1.1 thorpej ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
171 1.1 thorpej ehdr.e_version = EV_CURRENT;
172 1.1 thorpej ehdr.e_entry = 0;
173 1.1 thorpej ehdr.e_flags = 0;
174 1.1 thorpej ehdr.e_ehsize = sizeof(ehdr);
175 1.1 thorpej ehdr.e_phentsize = sizeof(Elf_Phdr);
176 1.36.6.1 tls if (npsections < PN_XNUM) {
177 1.36.6.1 tls ehdr.e_phnum = npsections;
178 1.36.6.1 tls ehdr.e_shentsize = 0;
179 1.36.6.1 tls ehdr.e_shnum = 0;
180 1.36.6.1 tls ehdr.e_shoff = 0;
181 1.36.6.1 tls ehdr.e_phoff = sizeof(ehdr);
182 1.36.6.1 tls } else {
183 1.36.6.1 tls ehdr.e_phnum = PN_XNUM;
184 1.36.6.1 tls ehdr.e_shentsize = sizeof(Elf_Shdr);
185 1.36.6.1 tls ehdr.e_shnum = 1;
186 1.36.6.1 tls ehdr.e_shoff = sizeof(ehdr);
187 1.36.6.1 tls ehdr.e_phoff = sizeof(ehdr) + sizeof(shdr);
188 1.36.6.1 tls }
189 1.1 thorpej ehdr.e_shstrndx = 0;
190 1.1 thorpej
191 1.35 matt #ifdef ELF_MD_COREDUMP_SETUP
192 1.35 matt ELF_MD_COREDUMP_SETUP(l, &ehdr);
193 1.35 matt #endif
194 1.35 matt
195 1.1 thorpej /* Write out the ELF header. */
196 1.18 matt error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
197 1.16 matt if (error)
198 1.16 matt goto out;
199 1.16 matt
200 1.36.6.1 tls /* Write out sections, if needed */
201 1.36.6.1 tls if (npsections >= PN_XNUM) {
202 1.36.6.1 tls memset(&shdr, 0, sizeof(shdr));
203 1.36.6.1 tls shdr.sh_type = SHT_NULL;
204 1.36.6.1 tls shdr.sh_info = npsections;
205 1.36.6.1 tls error = coredump_write(cookie, UIO_SYSSPACE, &shdr,
206 1.36.6.1 tls sizeof(shdr));
207 1.36.6.1 tls if (error)
208 1.36.6.1 tls goto out;
209 1.36.6.1 tls }
210 1.1 thorpej
211 1.36.6.1 tls psectionssize = npsections * sizeof(*psections);
212 1.36.6.1 tls notestart = ehdr.e_phoff + psectionssize;
213 1.16 matt
214 1.36 para psections = kmem_zalloc(psectionssize, KM_SLEEP);
215 1.1 thorpej
216 1.36.6.1 tls /* Pass 2: now find the P-section headers. */
217 1.36.6.1 tls ws.secoff = notestart + notesize;
218 1.16 matt ws.psections = psections;
219 1.36.6.1 tls ws.npsections = npsections - 1;
220 1.36.6.1 tls ws.p = l->l_proc;
221 1.36.6.1 tls error = uvm_coredump_walkmap(l->l_proc, ELFNAMEEND(coredump_getseghdrs),
222 1.36.6.1 tls &ws);
223 1.2 thorpej if (error)
224 1.16 matt goto out;
225 1.36.6.1 tls if (ws.npsections != 0) {
226 1.36.6.1 tls /* A section went away */
227 1.36.6.1 tls error = ENOMEM;
228 1.36.6.1 tls goto out;
229 1.36.6.1 tls }
230 1.1 thorpej
231 1.36.6.1 tls /* Add the PT_NOTE header after the P-section headers. */
232 1.16 matt ws.psections->p_type = PT_NOTE;
233 1.16 matt ws.psections->p_offset = notestart;
234 1.16 matt ws.psections->p_vaddr = 0;
235 1.16 matt ws.psections->p_paddr = 0;
236 1.16 matt ws.psections->p_filesz = notesize;
237 1.16 matt ws.psections->p_memsz = 0;
238 1.16 matt ws.psections->p_flags = PF_R;
239 1.16 matt ws.psections->p_align = ELFROUNDSIZE;
240 1.1 thorpej
241 1.36.6.1 tls /* Write the P-section headers followed by the PT_NOTE header */
242 1.36.6.1 tls error = coredump_write(cookie, UIO_SYSSPACE, psections, psectionssize);
243 1.1 thorpej if (error)
244 1.16 matt goto out;
245 1.1 thorpej
246 1.18 matt #ifdef DIAGNOSTIC
247 1.36.6.1 tls if (coredump_offset(cookie) != notestart)
248 1.2 thorpej panic("coredump: offset %lld != notestart %lld",
249 1.36.6.1 tls (long long) coredump_offset(cookie),
250 1.36.6.1 tls (long long) notestart);
251 1.2 thorpej #endif
252 1.1 thorpej
253 1.1 thorpej /* Write out the notes. */
254 1.36.6.1 tls for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
255 1.36.6.1 tls error = coredump_write(cookie, UIO_SYSSPACE, nb->nb_data,
256 1.36.6.1 tls nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data);
257 1.36.6.1 tls if (error)
258 1.36.6.1 tls goto out;
259 1.36.6.1 tls }
260 1.2 thorpej
261 1.36.6.1 tls /* Finally, write the sections themselves. */
262 1.36.6.1 tls for (i = 0; i < npsections - 1; i++) {
263 1.16 matt if (psections[i].p_filesz == 0)
264 1.16 matt continue;
265 1.16 matt
266 1.16 matt #ifdef DIAGNOSTIC
267 1.36.6.1 tls if (coredump_offset(cookie) != psections[i].p_offset)
268 1.16 matt panic("coredump: offset %lld != p_offset[%d] %lld",
269 1.36.6.1 tls (long long) coredump_offset(cookie), i,
270 1.16 matt (long long) psections[i].p_filesz);
271 1.16 matt #endif
272 1.16 matt
273 1.18 matt error = coredump_write(cookie, UIO_USERSPACE,
274 1.18 matt (void *)(vaddr_t)psections[i].p_vaddr,
275 1.18 matt psections[i].p_filesz);
276 1.16 matt if (error)
277 1.16 matt goto out;
278 1.16 matt }
279 1.1 thorpej
280 1.16 matt out:
281 1.16 matt if (psections)
282 1.36 para kmem_free(psections, psectionssize);
283 1.36.6.1 tls while ((nb = ns.ns_first) != NULL) {
284 1.36.6.1 tls ns.ns_first = nb->nb_next;
285 1.36.6.1 tls kmem_free(nb, sizeof *nb);
286 1.36.6.1 tls }
287 1.2 thorpej return (error);
288 1.2 thorpej }
289 1.2 thorpej
290 1.22 thorpej static int
291 1.36.6.1 tls ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *us)
292 1.2 thorpej {
293 1.2 thorpej struct writesegs_state *ws = us->cookie;
294 1.2 thorpej Elf_Phdr phdr;
295 1.16 matt vsize_t size, realsize;
296 1.16 matt vaddr_t end;
297 1.2 thorpej int error;
298 1.2 thorpej
299 1.36.6.1 tls /* Don't overrun if there are more sections */
300 1.36.6.1 tls if (ws->npsections == 0)
301 1.36.6.1 tls return ENOMEM;
302 1.36.6.1 tls ws->npsections--;
303 1.36.6.1 tls
304 1.2 thorpej size = us->end - us->start;
305 1.16 matt realsize = us->realend - us->start;
306 1.16 matt end = us->realend;
307 1.16 matt
308 1.36.6.1 tls /* Don't bother writing out trailing zeros */
309 1.16 matt while (realsize > 0) {
310 1.16 matt long buf[1024 / sizeof(long)];
311 1.16 matt size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
312 1.16 matt const long *ep;
313 1.16 matt int i;
314 1.16 matt
315 1.16 matt end -= slen;
316 1.36.6.1 tls if ((error = copyin_proc(ws->p, (void *)end, buf, slen)) != 0)
317 1.21 christos return error;
318 1.16 matt
319 1.16 matt ep = (const long *) &buf[slen / sizeof(buf[0])];
320 1.16 matt for (i = 0, ep--; buf <= ep; ep--, i++) {
321 1.16 matt if (*ep)
322 1.16 matt break;
323 1.16 matt }
324 1.16 matt realsize -= i * sizeof(buf[0]);
325 1.16 matt if (i * sizeof(buf[0]) < slen)
326 1.16 matt break;
327 1.16 matt }
328 1.2 thorpej
329 1.2 thorpej phdr.p_type = PT_LOAD;
330 1.2 thorpej phdr.p_offset = ws->secoff;
331 1.2 thorpej phdr.p_vaddr = us->start;
332 1.2 thorpej phdr.p_paddr = 0;
333 1.16 matt phdr.p_filesz = realsize;
334 1.2 thorpej phdr.p_memsz = size;
335 1.2 thorpej phdr.p_flags = 0;
336 1.2 thorpej if (us->prot & VM_PROT_READ)
337 1.2 thorpej phdr.p_flags |= PF_R;
338 1.2 thorpej if (us->prot & VM_PROT_WRITE)
339 1.2 thorpej phdr.p_flags |= PF_W;
340 1.2 thorpej if (us->prot & VM_PROT_EXECUTE)
341 1.2 thorpej phdr.p_flags |= PF_X;
342 1.2 thorpej phdr.p_align = PAGE_SIZE;
343 1.2 thorpej
344 1.2 thorpej ws->secoff += phdr.p_filesz;
345 1.16 matt *ws->psections++ = phdr;
346 1.1 thorpej
347 1.1 thorpej return (0);
348 1.1 thorpej }
349 1.1 thorpej
350 1.36.6.2 jdolecek static void
351 1.36.6.2 jdolecek coredump_note_procinfo(struct lwp *l, struct note_state *ns)
352 1.1 thorpej {
353 1.36.6.1 tls struct proc *p;
354 1.1 thorpej struct netbsd_elfcore_procinfo cpi;
355 1.6 matt struct lwp *l0;
356 1.29 ad sigset_t ss1, ss2;
357 1.1 thorpej
358 1.36.6.1 tls p = l->l_proc;
359 1.1 thorpej
360 1.1 thorpej /* First, write an elfcore_procinfo. */
361 1.36.6.1 tls cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
362 1.36.6.1 tls cpi.cpi_cpisize = sizeof(cpi);
363 1.36.6.2 jdolecek cpi.cpi_signo = p->p_sigctx.ps_info._signo;
364 1.36.6.2 jdolecek cpi.cpi_sigcode = p->p_sigctx.ps_info._code;
365 1.36.6.1 tls cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
366 1.1 thorpej
367 1.36.6.1 tls /*
368 1.36.6.1 tls * XXX This should be per-LWP.
369 1.36.6.1 tls */
370 1.36.6.1 tls ss1 = p->p_sigpend.sp_set;
371 1.36.6.1 tls sigemptyset(&ss2);
372 1.36.6.1 tls LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
373 1.36.6.1 tls sigplusset(&l0->l_sigpend.sp_set, &ss1);
374 1.36.6.1 tls sigplusset(&l0->l_sigmask, &ss2);
375 1.1 thorpej }
376 1.36.6.1 tls memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
377 1.36.6.1 tls memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
378 1.36.6.1 tls memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
379 1.36.6.1 tls sizeof(cpi.cpi_sigignore));
380 1.36.6.1 tls memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
381 1.36.6.1 tls sizeof(cpi.cpi_sigcatch));
382 1.36.6.1 tls
383 1.36.6.1 tls cpi.cpi_pid = p->p_pid;
384 1.36.6.1 tls mutex_enter(proc_lock);
385 1.36.6.1 tls cpi.cpi_ppid = p->p_pptr->p_pid;
386 1.36.6.1 tls cpi.cpi_pgrp = p->p_pgid;
387 1.36.6.1 tls cpi.cpi_sid = p->p_session->s_sid;
388 1.36.6.1 tls mutex_exit(proc_lock);
389 1.36.6.1 tls
390 1.36.6.1 tls cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
391 1.36.6.1 tls cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
392 1.36.6.1 tls cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
393 1.36.6.1 tls
394 1.36.6.1 tls cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
395 1.36.6.1 tls cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
396 1.36.6.1 tls cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
397 1.36.6.1 tls
398 1.36.6.1 tls cpi.cpi_nlwps = p->p_nlwps;
399 1.36.6.1 tls (void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
400 1.36.6.1 tls cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
401 1.1 thorpej
402 1.36.6.1 tls ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
403 1.36.6.1 tls ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
404 1.36.6.2 jdolecek }
405 1.36.6.2 jdolecek
406 1.36.6.2 jdolecek static int
407 1.36.6.2 jdolecek coredump_note_auxv(struct lwp *l, struct note_state *ns)
408 1.36.6.2 jdolecek {
409 1.36.6.2 jdolecek int error;
410 1.36.6.2 jdolecek size_t len;
411 1.36.6.2 jdolecek void *kauxv;
412 1.36.6.2 jdolecek
413 1.36.6.2 jdolecek if ((error = proc_getauxv(l->l_proc, &kauxv, &len)) != 0)
414 1.36.6.2 jdolecek return error;
415 1.36.6.2 jdolecek
416 1.36.6.2 jdolecek ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_AUXV,
417 1.36.6.2 jdolecek ELF_NOTE_NETBSD_CORE_NAME, kauxv, len);
418 1.36.6.2 jdolecek
419 1.36.6.2 jdolecek kmem_free(kauxv, len);
420 1.36.6.2 jdolecek return 0;
421 1.36.6.2 jdolecek }
422 1.36.6.2 jdolecek
423 1.36.6.2 jdolecek static int
424 1.36.6.2 jdolecek ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
425 1.36.6.2 jdolecek {
426 1.36.6.2 jdolecek int error;
427 1.36.6.2 jdolecek struct lwp *l0;
428 1.36.6.2 jdolecek struct proc *p = l->l_proc;
429 1.36.6.2 jdolecek
430 1.36.6.2 jdolecek coredump_note_procinfo(l, ns);
431 1.36.6.2 jdolecek error = coredump_note_auxv(l, ns);
432 1.36.6.2 jdolecek if (error)
433 1.36.6.2 jdolecek return error;
434 1.1 thorpej
435 1.1 thorpej /* XXX Add hook for machdep per-proc notes. */
436 1.1 thorpej
437 1.1 thorpej /*
438 1.6 matt * Now write the register info for the thread that caused the
439 1.6 matt * coredump.
440 1.6 matt */
441 1.36.6.1 tls error = ELFNAMEEND(coredump_note)(l, ns);
442 1.6 matt if (error)
443 1.36.6.2 jdolecek return error;
444 1.6 matt
445 1.6 matt /*
446 1.1 thorpej * Now, for each LWP, write the register info and any other
447 1.36.6.1 tls * per-LWP notes.
448 1.36.6.1 tls * Lock in case this is a gcore requested dump.
449 1.1 thorpej */
450 1.36.6.1 tls mutex_enter(p->p_lock);
451 1.6 matt LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
452 1.6 matt if (l0 == l) /* we've taken care of this thread */
453 1.6 matt continue;
454 1.36.6.1 tls error = ELFNAMEEND(coredump_note)(l0, ns);
455 1.6 matt if (error)
456 1.36.6.1 tls break;
457 1.6 matt }
458 1.36.6.1 tls mutex_exit(p->p_lock);
459 1.6 matt
460 1.36.6.1 tls return error;
461 1.6 matt }
462 1.1 thorpej
463 1.22 thorpej static int
464 1.36.6.1 tls ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
465 1.6 matt {
466 1.36.6.1 tls int error;
467 1.36.6.1 tls char name[64];
468 1.23 cube elf_reg intreg;
469 1.1 thorpej #ifdef PT_GETFPREGS
470 1.23 cube elf_fpreg freg;
471 1.36.6.1 tls size_t freglen;
472 1.6 matt #endif
473 1.6 matt
474 1.36.6.1 tls snprintf(name, sizeof(name), "%s@%d",
475 1.18 matt ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
476 1.6 matt
477 1.36.6.1 tls error = elf_process_read_regs(l, &intreg);
478 1.36.6.1 tls if (error)
479 1.36.6.1 tls return (error);
480 1.1 thorpej
481 1.36.6.1 tls ELFNAMEEND(coredump_savenote)(ns, PT_GETREGS, name, &intreg,
482 1.36.6.1 tls sizeof(intreg));
483 1.6 matt
484 1.6 matt #ifdef PT_GETFPREGS
485 1.36.6.1 tls freglen = sizeof(freg);
486 1.36.6.1 tls error = elf_process_read_fpregs(l, &freg, &freglen);
487 1.36.6.1 tls if (error)
488 1.36.6.1 tls return (error);
489 1.1 thorpej
490 1.36.6.1 tls ELFNAMEEND(coredump_savenote)(ns, PT_GETFPREGS, name, &freg, freglen);
491 1.6 matt #endif
492 1.6 matt /* XXX Add hook for machdep per-LWP notes. */
493 1.1 thorpej return (0);
494 1.1 thorpej }
495 1.1 thorpej
496 1.36.6.1 tls static void
497 1.36.6.1 tls save_note_bytes(struct note_state *ns, const void *data, size_t len)
498 1.1 thorpej {
499 1.36.6.1 tls struct note_buf *nb = ns->ns_last;
500 1.36.6.1 tls size_t copylen;
501 1.36.6.1 tls unsigned char *wp;
502 1.1 thorpej
503 1.36.6.1 tls /*
504 1.36.6.1 tls * Just copy the data into a buffer list.
505 1.36.6.1 tls * All but the last buffer is full.
506 1.36.6.1 tls */
507 1.36.6.1 tls for (;;) {
508 1.36.6.1 tls copylen = min(len, sizeof nb->nb_data - ns->ns_offset);
509 1.36.6.1 tls wp = nb->nb_data + ns->ns_offset;
510 1.36.6.1 tls memcpy(wp, data, copylen);
511 1.36.6.1 tls if (copylen == len)
512 1.36.6.1 tls break;
513 1.36.6.1 tls nb->nb_next = kmem_alloc(sizeof *nb->nb_next, KM_SLEEP);
514 1.36.6.1 tls nb = nb->nb_next;
515 1.36.6.1 tls ns->ns_last = nb;
516 1.36.6.1 tls ns->ns_count++;
517 1.36.6.1 tls ns->ns_offset = 0;
518 1.36.6.1 tls len -= copylen;
519 1.36.6.1 tls data = (const unsigned char *)data + copylen;
520 1.36.6.1 tls }
521 1.1 thorpej
522 1.36.6.1 tls while (copylen & (ELFROUNDSIZE - 1))
523 1.36.6.1 tls wp[copylen++] = 0;
524 1.36.6.1 tls
525 1.36.6.1 tls ns->ns_offset += copylen;
526 1.36.6.1 tls }
527 1.36.6.1 tls
528 1.36.6.1 tls void
529 1.36.6.1 tls ELFNAMEEND(coredump_savenote)(struct note_state *ns, unsigned int type,
530 1.36.6.1 tls const char *name, void *data, size_t data_len)
531 1.36.6.1 tls {
532 1.36.6.1 tls Elf_Nhdr nhdr;
533 1.1 thorpej
534 1.36.6.1 tls nhdr.n_namesz = strlen(name) + 1;
535 1.36.6.1 tls nhdr.n_descsz = data_len;
536 1.36.6.1 tls nhdr.n_type = type;
537 1.36.6.1 tls
538 1.36.6.1 tls save_note_bytes(ns, &nhdr, sizeof (nhdr));
539 1.36.6.1 tls save_note_bytes(ns, name, nhdr.n_namesz);
540 1.36.6.1 tls save_note_bytes(ns, data, data_len);
541 1.1 thorpej }
542 1.33 ad
543 1.33 ad #else /* COREDUMP */
544 1.33 ad
545 1.33 ad int
546 1.36.6.2 jdolecek ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
547 1.33 ad {
548 1.33 ad
549 1.33 ad return ENOSYS;
550 1.33 ad }
551 1.33 ad
552 1.33 ad #endif /* COREDUMP */
553