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