2007-03-09 17:11:22 +03:00
|
|
|
/* $NetBSD: core_elf32.c,v 1.30 2007/03/09 14:11:23 ad Exp $ */
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Wasabi Systems, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Written by Jason R. Thorpe for Wasabi Systems, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project by
|
|
|
|
* Wasabi Systems, Inc.
|
|
|
|
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2007-03-09 17:11:22 +03:00
|
|
|
__KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.30 2007/03/09 14:11:23 ad Exp $");
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/* If not included by core_elf64.c, ELFSIZE won't be defined. */
|
|
|
|
#ifndef ELFSIZE
|
|
|
|
#define ELFSIZE 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/vnode.h>
|
2005-06-10 09:10:12 +04:00
|
|
|
#include <sys/exec.h>
|
2001-12-10 02:05:56 +03:00
|
|
|
#include <sys/exec_elf.h>
|
|
|
|
#include <sys/ptrace.h>
|
2005-06-02 21:01:43 +04:00
|
|
|
#include <sys/malloc.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
#include <machine/reg.h>
|
|
|
|
|
2001-12-10 04:52:26 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
|
|
|
struct countsegs_state {
|
|
|
|
int npsections;
|
|
|
|
};
|
|
|
|
|
2005-12-08 06:05:40 +03:00
|
|
|
static int ELFNAMEEND(coredump_countsegs)(struct proc *, void *,
|
|
|
|
struct uvm_coredump_state *);
|
2001-12-10 04:52:26 +03:00
|
|
|
|
|
|
|
struct writesegs_state {
|
2005-06-02 21:01:43 +04:00
|
|
|
Elf_Phdr *psections;
|
2001-12-10 04:52:26 +03:00
|
|
|
off_t secoff;
|
|
|
|
};
|
|
|
|
|
2005-12-08 06:05:40 +03:00
|
|
|
static int ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *,
|
|
|
|
struct uvm_coredump_state *);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-12-08 06:05:40 +03:00
|
|
|
static int ELFNAMEEND(coredump_notes)(struct proc *, struct lwp *, void *,
|
|
|
|
size_t *);
|
|
|
|
static int ELFNAMEEND(coredump_note)(struct proc *, struct lwp *, void *,
|
|
|
|
size_t *);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
#define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */
|
|
|
|
#define elfround(x) roundup((x), ELFROUNDSIZE)
|
|
|
|
|
2006-03-12 23:25:25 +03:00
|
|
|
#define elf_process_read_regs CONCAT(process_read_regs, ELFSIZE)
|
|
|
|
#define elf_process_read_fpregs CONCAT(process_read_fpregs, ELFSIZE)
|
|
|
|
#define elf_reg CONCAT(process_reg, ELFSIZE)
|
|
|
|
#define elf_fpreg CONCAT(process_fpreg, ELFSIZE)
|
|
|
|
|
2001-12-10 02:05:56 +03:00
|
|
|
int
|
2005-06-10 09:10:12 +04:00
|
|
|
ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
|
2001-12-10 02:05:56 +03:00
|
|
|
{
|
2003-01-18 13:06:22 +03:00
|
|
|
struct proc *p;
|
2001-12-10 02:05:56 +03:00
|
|
|
Elf_Ehdr ehdr;
|
2005-06-02 21:01:43 +04:00
|
|
|
Elf_Phdr phdr, *psections;
|
2001-12-10 04:52:26 +03:00
|
|
|
struct countsegs_state cs;
|
|
|
|
struct writesegs_state ws;
|
2005-06-02 21:01:43 +04:00
|
|
|
off_t notestart, secstart, offset;
|
2003-05-09 00:26:40 +04:00
|
|
|
size_t notesize;
|
2005-06-02 21:01:43 +04:00
|
|
|
int error, i;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-06-02 21:01:43 +04:00
|
|
|
psections = NULL;
|
2003-01-18 13:06:22 +03:00
|
|
|
p = l->l_proc;
|
2001-12-10 02:05:56 +03:00
|
|
|
/*
|
|
|
|
* We have to make a total of 3 passes across the map:
|
|
|
|
*
|
|
|
|
* 1. Count the number of map entries (the number of
|
|
|
|
* PT_LOAD sections).
|
|
|
|
*
|
|
|
|
* 2. Write the P-section headers.
|
|
|
|
*
|
|
|
|
* 3. Write the P-sections.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Pass 1: count the entries. */
|
2001-12-10 04:52:26 +03:00
|
|
|
cs.npsections = 0;
|
2005-06-10 09:10:12 +04:00
|
|
|
error = uvm_coredump_walkmap(p, NULL,
|
2001-12-10 04:52:26 +03:00
|
|
|
ELFNAMEEND(coredump_countsegs), &cs);
|
|
|
|
if (error)
|
2005-06-02 21:01:43 +04:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Count the PT_NOTE section. */
|
|
|
|
cs.npsections++;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/* Get the size of the notes. */
|
2005-06-10 09:10:12 +04:00
|
|
|
error = ELFNAMEEND(coredump_notes)(p, l, NULL, ¬esize);
|
2001-12-10 02:05:56 +03:00
|
|
|
if (error)
|
2005-06-02 21:01:43 +04:00
|
|
|
goto out;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
|
2001-12-10 02:05:56 +03:00
|
|
|
memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
|
|
|
|
#if ELFSIZE == 32
|
|
|
|
ehdr.e_ident[EI_CLASS] = ELFCLASS32;
|
|
|
|
#elif ELFSIZE == 64
|
|
|
|
ehdr.e_ident[EI_CLASS] = ELFCLASS64;
|
|
|
|
#endif
|
|
|
|
ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
|
|
|
|
ehdr.e_ident[EI_VERSION] = EV_CURRENT;
|
|
|
|
/* XXX Should be the OSABI/ABI version of the executable. */
|
|
|
|
ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
|
2001-12-10 04:52:26 +03:00
|
|
|
ehdr.e_ident[EI_ABIVERSION] = 0;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
ehdr.e_type = ET_CORE;
|
|
|
|
/* XXX This should be the e_machine of the executable. */
|
|
|
|
ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
|
|
|
|
ehdr.e_version = EV_CURRENT;
|
|
|
|
ehdr.e_entry = 0;
|
|
|
|
ehdr.e_phoff = sizeof(ehdr);
|
|
|
|
ehdr.e_shoff = 0;
|
|
|
|
ehdr.e_flags = 0;
|
|
|
|
ehdr.e_ehsize = sizeof(ehdr);
|
|
|
|
ehdr.e_phentsize = sizeof(Elf_Phdr);
|
2001-12-10 04:52:26 +03:00
|
|
|
ehdr.e_phnum = cs.npsections;
|
2001-12-10 02:05:56 +03:00
|
|
|
ehdr.e_shentsize = 0;
|
|
|
|
ehdr.e_shnum = 0;
|
|
|
|
ehdr.e_shstrndx = 0;
|
|
|
|
|
|
|
|
/* Write out the ELF header. */
|
2005-06-10 09:10:12 +04:00
|
|
|
error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
|
2005-06-02 21:01:43 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
offset = sizeof(ehdr);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-06-02 21:01:43 +04:00
|
|
|
notestart = offset + sizeof(phdr) * cs.npsections;
|
|
|
|
secstart = notestart + notesize;
|
|
|
|
|
|
|
|
psections = malloc(cs.npsections * sizeof(Elf_Phdr),
|
|
|
|
M_TEMP, M_WAITOK|M_ZERO);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2003-02-25 08:27:35 +03:00
|
|
|
/* Pass 2: now write the P-section headers. */
|
2001-12-10 04:52:26 +03:00
|
|
|
ws.secoff = secstart;
|
2005-06-02 21:01:43 +04:00
|
|
|
ws.psections = psections;
|
2005-06-10 09:10:12 +04:00
|
|
|
error = uvm_coredump_walkmap(p, cookie,
|
2001-12-10 04:52:26 +03:00
|
|
|
ELFNAMEEND(coredump_writeseghdrs), &ws);
|
|
|
|
if (error)
|
2005-06-02 21:01:43 +04:00
|
|
|
goto out;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/* Write out the PT_NOTE header. */
|
2005-06-02 21:01:43 +04:00
|
|
|
ws.psections->p_type = PT_NOTE;
|
|
|
|
ws.psections->p_offset = notestart;
|
|
|
|
ws.psections->p_vaddr = 0;
|
|
|
|
ws.psections->p_paddr = 0;
|
|
|
|
ws.psections->p_filesz = notesize;
|
|
|
|
ws.psections->p_memsz = 0;
|
|
|
|
ws.psections->p_flags = PF_R;
|
|
|
|
ws.psections->p_align = ELFROUNDSIZE;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
error = coredump_write(cookie, UIO_SYSSPACE, psections,
|
|
|
|
cs.npsections * sizeof(Elf_Phdr));
|
2001-12-10 02:05:56 +03:00
|
|
|
if (error)
|
2005-06-02 21:01:43 +04:00
|
|
|
goto out;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2001-12-10 04:52:26 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
2005-06-10 09:10:12 +04:00
|
|
|
offset += cs.npsections * sizeof(Elf_Phdr);
|
2005-06-02 21:01:43 +04:00
|
|
|
if (offset != notestart)
|
2001-12-10 04:52:26 +03:00
|
|
|
panic("coredump: offset %lld != notestart %lld",
|
2005-06-02 21:01:43 +04:00
|
|
|
(long long) offset, (long long) notestart);
|
2001-12-10 04:52:26 +03:00
|
|
|
#endif
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/* Write out the notes. */
|
2005-06-10 09:10:12 +04:00
|
|
|
error = ELFNAMEEND(coredump_notes)(p, l, cookie, ¬esize);
|
2005-06-02 21:01:43 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
2001-12-10 04:52:26 +03:00
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
2005-06-02 21:01:43 +04:00
|
|
|
offset += notesize;
|
|
|
|
if (offset != secstart)
|
2001-12-10 04:52:26 +03:00
|
|
|
panic("coredump: offset %lld != secstart %lld",
|
2005-06-02 21:01:43 +04:00
|
|
|
(long long) offset, (long long) secstart);
|
2001-12-10 04:52:26 +03:00
|
|
|
#endif
|
|
|
|
|
2003-02-25 08:27:35 +03:00
|
|
|
/* Pass 3: finally, write the sections themselves. */
|
2005-06-02 21:01:43 +04:00
|
|
|
for (i = 0; i < cs.npsections - 1; i++) {
|
|
|
|
if (psections[i].p_filesz == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (offset != psections[i].p_offset)
|
|
|
|
panic("coredump: offset %lld != p_offset[%d] %lld",
|
|
|
|
(long long) offset, i,
|
|
|
|
(long long) psections[i].p_filesz);
|
|
|
|
#endif
|
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
error = coredump_write(cookie, UIO_USERSPACE,
|
|
|
|
(void *)(vaddr_t)psections[i].p_vaddr,
|
|
|
|
psections[i].p_filesz);
|
2005-06-02 21:01:43 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-06-02 21:01:43 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
offset += psections[i].p_filesz;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (psections)
|
|
|
|
free(psections, M_TEMP);
|
2001-12-10 04:52:26 +03:00
|
|
|
return (error);
|
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-12-08 06:05:40 +03:00
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie,
|
2005-06-10 09:10:12 +04:00
|
|
|
struct uvm_coredump_state *us)
|
2001-12-10 04:52:26 +03:00
|
|
|
{
|
|
|
|
struct countsegs_state *cs = us->cookie;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2001-12-10 04:52:26 +03:00
|
|
|
cs->npsections++;
|
|
|
|
return (0);
|
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-12-08 06:05:40 +03:00
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie,
|
2005-06-10 09:10:12 +04:00
|
|
|
struct uvm_coredump_state *us)
|
2001-12-10 04:52:26 +03:00
|
|
|
{
|
|
|
|
struct writesegs_state *ws = us->cookie;
|
|
|
|
Elf_Phdr phdr;
|
2005-06-02 21:01:43 +04:00
|
|
|
vsize_t size, realsize;
|
|
|
|
vaddr_t end;
|
2001-12-10 04:52:26 +03:00
|
|
|
int error;
|
|
|
|
|
|
|
|
size = us->end - us->start;
|
2005-06-02 21:01:43 +04:00
|
|
|
realsize = us->realend - us->start;
|
|
|
|
end = us->realend;
|
|
|
|
|
|
|
|
while (realsize > 0) {
|
|
|
|
long buf[1024 / sizeof(long)];
|
|
|
|
size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
|
|
|
|
const long *ep;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
end -= slen;
|
2005-07-07 02:30:42 +04:00
|
|
|
if ((error = copyin_proc(p, (void *)end, buf, slen)) != 0)
|
|
|
|
return error;
|
2005-06-02 21:01:43 +04:00
|
|
|
|
|
|
|
ep = (const long *) &buf[slen / sizeof(buf[0])];
|
|
|
|
for (i = 0, ep--; buf <= ep; ep--, i++) {
|
|
|
|
if (*ep)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
realsize -= i * sizeof(buf[0]);
|
|
|
|
if (i * sizeof(buf[0]) < slen)
|
|
|
|
break;
|
|
|
|
}
|
2001-12-10 04:52:26 +03:00
|
|
|
|
|
|
|
phdr.p_type = PT_LOAD;
|
|
|
|
phdr.p_offset = ws->secoff;
|
|
|
|
phdr.p_vaddr = us->start;
|
|
|
|
phdr.p_paddr = 0;
|
2005-06-02 21:01:43 +04:00
|
|
|
phdr.p_filesz = realsize;
|
2001-12-10 04:52:26 +03:00
|
|
|
phdr.p_memsz = size;
|
|
|
|
phdr.p_flags = 0;
|
|
|
|
if (us->prot & VM_PROT_READ)
|
|
|
|
phdr.p_flags |= PF_R;
|
|
|
|
if (us->prot & VM_PROT_WRITE)
|
|
|
|
phdr.p_flags |= PF_W;
|
|
|
|
if (us->prot & VM_PROT_EXECUTE)
|
|
|
|
phdr.p_flags |= PF_X;
|
|
|
|
phdr.p_align = PAGE_SIZE;
|
|
|
|
|
|
|
|
ws->secoff += phdr.p_filesz;
|
2005-06-02 21:01:43 +04:00
|
|
|
*ws->psections++ = phdr;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-12-08 06:05:40 +03:00
|
|
|
static int
|
2005-06-10 09:10:12 +04:00
|
|
|
ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l,
|
|
|
|
void *iocookie, size_t *sizep)
|
2001-12-10 02:05:56 +03:00
|
|
|
{
|
|
|
|
struct netbsd_elfcore_procinfo cpi;
|
|
|
|
Elf_Nhdr nhdr;
|
2003-05-09 00:26:40 +04:00
|
|
|
size_t size, notesize;
|
|
|
|
int error;
|
|
|
|
struct lwp *l0;
|
2007-02-10 00:55:00 +03:00
|
|
|
sigset_t ss1, ss2;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
/* First, write an elfcore_procinfo. */
|
|
|
|
notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
|
|
|
|
elfround(sizeof(cpi));
|
2005-06-10 09:10:12 +04:00
|
|
|
if (iocookie) {
|
2001-12-10 02:05:56 +03:00
|
|
|
cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
|
|
|
|
cpi.cpi_cpisize = sizeof(cpi);
|
2003-09-14 10:59:13 +04:00
|
|
|
cpi.cpi_signo = p->p_sigctx.ps_signo;
|
|
|
|
cpi.cpi_sigcode = p->p_sigctx.ps_code;
|
2003-05-20 21:42:51 +04:00
|
|
|
cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* XXX This should be per-LWP.
|
|
|
|
*/
|
|
|
|
ss1 = p->p_sigpend.sp_set;
|
|
|
|
sigemptyset(&ss2);
|
|
|
|
LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
|
|
|
|
sigplusset(&l0->l_sigpend.sp_set, &ss1);
|
|
|
|
sigplusset(&l0->l_sigmask, &ss2);
|
|
|
|
}
|
|
|
|
memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
|
|
|
|
memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
|
2001-12-10 02:05:56 +03:00
|
|
|
memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
|
|
|
|
sizeof(cpi.cpi_sigignore));
|
|
|
|
memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
|
|
|
|
sizeof(cpi.cpi_sigcatch));
|
|
|
|
|
|
|
|
cpi.cpi_pid = p->p_pid;
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_enter(&proclist_lock);
|
2001-12-10 02:05:56 +03:00
|
|
|
cpi.cpi_ppid = p->p_pptr->p_pid;
|
|
|
|
cpi.cpi_pgrp = p->p_pgid;
|
|
|
|
cpi.cpi_sid = p->p_session->s_sid;
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_exit(&proclist_lock);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2006-07-24 02:06:03 +04:00
|
|
|
cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
|
|
|
|
cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
|
|
|
|
cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2006-07-24 02:06:03 +04:00
|
|
|
cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
|
|
|
|
cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
|
|
|
|
cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
cpi.cpi_nlwps = p->p_nlwps;
|
2006-04-18 02:05:23 +04:00
|
|
|
(void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
|
|
|
|
cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
|
|
|
|
nhdr.n_descsz = sizeof(cpi);
|
|
|
|
nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
|
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
|
|
|
|
ELF_NOTE_NETBSD_CORE_NAME "\0\0\0", &cpi);
|
2001-12-10 02:05:56 +03:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
size += notesize;
|
|
|
|
|
|
|
|
/* XXX Add hook for machdep per-proc notes. */
|
|
|
|
|
2003-05-09 00:26:40 +04:00
|
|
|
/*
|
|
|
|
* Now write the register info for the thread that caused the
|
|
|
|
* coredump.
|
|
|
|
*/
|
2005-06-10 09:10:12 +04:00
|
|
|
error = ELFNAMEEND(coredump_note)(p, l, iocookie, ¬esize);
|
2003-05-09 00:26:40 +04:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
size += notesize;
|
|
|
|
|
2001-12-10 02:05:56 +03:00
|
|
|
/*
|
|
|
|
* Now, for each LWP, write the register info and any other
|
2007-02-10 00:55:00 +03:00
|
|
|
* per-LWP notes. Since we're dumping core, we don't bother
|
|
|
|
* locking.
|
2001-12-10 02:05:56 +03:00
|
|
|
*/
|
2003-05-09 00:26:40 +04:00
|
|
|
LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
|
|
|
|
if (l0 == l) /* we've taken care of this thread */
|
|
|
|
continue;
|
2005-06-10 09:10:12 +04:00
|
|
|
error = ELFNAMEEND(coredump_note)(p, l0, iocookie, ¬esize);
|
2003-05-09 00:26:40 +04:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2001-12-10 02:05:56 +03:00
|
|
|
size += notesize;
|
2003-05-09 00:26:40 +04:00
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2003-05-09 00:26:40 +04:00
|
|
|
*sizep = size;
|
|
|
|
return (0);
|
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-12-08 06:05:40 +03:00
|
|
|
static int
|
2005-06-10 09:10:12 +04:00
|
|
|
ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l, void *iocookie,
|
|
|
|
size_t *sizep)
|
2003-05-09 00:26:40 +04:00
|
|
|
{
|
|
|
|
Elf_Nhdr nhdr;
|
|
|
|
int size, notesize, error;
|
|
|
|
int namesize;
|
2005-06-10 09:10:12 +04:00
|
|
|
char name[64+ELFROUNDSIZE];
|
2006-03-12 23:25:25 +03:00
|
|
|
elf_reg intreg;
|
2003-05-09 00:26:40 +04:00
|
|
|
#ifdef PT_GETFPREGS
|
2006-03-12 23:25:25 +03:00
|
|
|
elf_fpreg freg;
|
2001-12-10 02:05:56 +03:00
|
|
|
#endif
|
2003-05-09 00:26:40 +04:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
|
|
|
|
ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
|
2003-05-09 00:26:40 +04:00
|
|
|
namesize = strlen(name) + 1;
|
2005-06-10 09:10:12 +04:00
|
|
|
memset(name + namesize, 0, elfround(namesize) - namesize);
|
2003-05-09 00:26:40 +04:00
|
|
|
|
|
|
|
notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
|
2005-06-10 09:10:12 +04:00
|
|
|
if (iocookie) {
|
2003-05-09 00:26:40 +04:00
|
|
|
PHOLD(l);
|
2006-03-12 23:25:25 +03:00
|
|
|
error = elf_process_read_regs(l, &intreg);
|
2003-05-09 00:26:40 +04:00
|
|
|
PRELE(l);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
nhdr.n_namesz = namesize;
|
|
|
|
nhdr.n_descsz = sizeof(intreg);
|
|
|
|
nhdr.n_type = PT_GETREGS;
|
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
|
|
|
|
name, &intreg);
|
2003-05-09 00:26:40 +04:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
}
|
2003-05-09 00:26:40 +04:00
|
|
|
size += notesize;
|
|
|
|
|
|
|
|
#ifdef PT_GETFPREGS
|
|
|
|
notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
|
2005-06-10 09:10:12 +04:00
|
|
|
if (iocookie) {
|
2003-05-09 00:26:40 +04:00
|
|
|
PHOLD(l);
|
2006-03-12 23:25:25 +03:00
|
|
|
error = elf_process_read_fpregs(l, &freg);
|
2003-05-09 00:26:40 +04:00
|
|
|
PRELE(l);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
nhdr.n_namesz = namesize;
|
|
|
|
nhdr.n_descsz = sizeof(freg);
|
|
|
|
nhdr.n_type = PT_GETFPREGS;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
|
|
|
|
name, &freg);
|
2003-05-09 00:26:40 +04:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
size += notesize;
|
|
|
|
#endif
|
2001-12-10 02:05:56 +03:00
|
|
|
*sizep = size;
|
2003-05-09 00:26:40 +04:00
|
|
|
/* XXX Add hook for machdep per-LWP notes. */
|
2001-12-10 02:05:56 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Nhdr *nhdr,
|
|
|
|
const char *name, void *data)
|
2001-12-10 02:05:56 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
|
2001-12-10 02:05:56 +03:00
|
|
|
if (error)
|
2005-06-10 09:10:12 +04:00
|
|
|
return error;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
error = coredump_write(cookie, UIO_SYSSPACE, name,
|
|
|
|
elfround(nhdr->n_namesz));
|
2001-12-10 02:05:56 +03:00
|
|
|
if (error)
|
2005-06-10 09:10:12 +04:00
|
|
|
return error;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-06-10 09:10:12 +04:00
|
|
|
return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->n_descsz);
|
2001-12-10 02:05:56 +03:00
|
|
|
}
|