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