From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 336A9C7619A for ; Wed, 5 Apr 2023 21:37:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pkAoJ-0005QR-JI; Wed, 05 Apr 2023 17:36:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pkAo8-0005Km-PQ for qemu-devel@nongnu.org; Wed, 05 Apr 2023 17:36:46 -0400 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pkAo4-0005b5-IP for qemu-devel@nongnu.org; Wed, 05 Apr 2023 17:36:44 -0400 Received: by mail-il1-x133.google.com with SMTP id v5so16952429ilj.4 for ; Wed, 05 Apr 2023 14:36:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; t=1680730599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=l6tsqUBY1YtWrUEbkmsbW91Yfgsm/h8FYRBQmpt6L4U=; b=ytn/ufKbgQZ5dzQoUInrocB3xfXwKDiQVNwW7DdrXrMSV0azJHvjip9qOQ40Z+R52R /kIDtxPxo69YpQ/ueEnUgjgubvJ0dwvOQdlZb80fu1sbnVTrVYnVJUYVUFpn+1kk77vS grXRkoT4u/Y4gxzjC5a8pbhf6jA3i2TIF+WAN3/nH0ik1+BZBeRDcSe60tf7djlZKeI2 AHKBhUMC+5bSruItSVvmWSY1OSjYBI+MLBJS+7B8Eo1cCRqp4/PL4wepEa4jf9SPqc15 nYuAU4o5MvwfuI7FCuuqddZiqLA74J+XTDMVe2eJnjKnVSfwUugc44wV8Sn4ouK8xrjt qnnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680730599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l6tsqUBY1YtWrUEbkmsbW91Yfgsm/h8FYRBQmpt6L4U=; b=U7a4rOpZsy359p09sD6JzDw237xwWoCu9PLO40gXiulhYG/R5oF1Du6MBCSap8XBI8 LizqK7naBP9ySUQ/2W/5vu4xsfGHRFmSFvtXlq8RBFaTu5xj1OQ1Z30IDPYy1qa+HsIb jKze72t39X4SkPafs7v9UOhSTAlo3F7Ob/7+9Ky+emdr9yhgw8w2iNg5Lw/0Dh87kyMG ryV5kU1RprqdYLLj3EwI0BkwQALUnA/F79KM8uZWm6XzLo9P2OTm44QD3amOvA87iEOu JYg37qwz0KpzY0j+k5ZPUtcI6BcZHN8t4KlJeXdA/hcSK05k788xPFOHKs1bXCzXYIMo QctQ== X-Gm-Message-State: AAQBX9c1qFrI9WLKTCNUUguIrvhuPSy8ZlcjJeOJ4Mc0j8s4/rgm5epe Oy9SJvQuFayUWyzpR3s829hMHSjZU9EbFVPU3dU= X-Google-Smtp-Source: AKy350bh/HMYykx4fsaL08AqAQP+rZo7ScXxAuKQPx1ZUWmF8pSbyHFTOAXjYD9JPGXvLTaUXlZFBQ== X-Received: by 2002:a05:6e02:4c5:b0:315:4b70:8376 with SMTP id f5-20020a056e0204c500b003154b708376mr5150295ils.29.1680730599116; Wed, 05 Apr 2023 14:36:39 -0700 (PDT) Received: from dune.bsdimp.com (c-71-237-47-177.hsd1.co.comcast.net. [71.237.47.177]) by smtp.gmail.com with ESMTPSA id i18-20020a926d12000000b003261b6acc8asm4110473ilc.79.2023.04.05.14.36.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 14:36:38 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: reinoud@netbsd.org, riastradh@netbsd.org, ryoon@netbsd.org, jrtc27@jrtc27.com, Warner Losh , kevans@freebsd.org, Brad Smith , Stacey Son , Ed Schouten Subject: [PATCH 14/16] bsd-user: Implment core dumps Date: Wed, 5 Apr 2023 15:36:10 -0600 Message-Id: <20230405213612.15942-15-imp@bsdimp.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405213612.15942-1-imp@bsdimp.com> References: <20230405213612.15942-1-imp@bsdimp.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: none client-ip=2607:f8b0:4864:20::133; envelope-from=imp@bsdimp.com; helo=mail-il1-x133.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Stacey Son Bring in the code that was originally copied from linxu-user/elfload.c and moved to elfcore.c. This code then removed the Linux specific bits, replacing them with FreeBSD specific bits. The commit history for this is not at all what we'd like (it was done in one go by sson in 227070562fc in one commit, with very few followup tweaks). Since the original commit, this code has been moved, and updated by sson and ed slightly. That makes it hard to split into smaller commits. Signed-off-by: Stacey Son Signed-off-by: Ed Schouten Signed-off-by: Warner Losh --- bsd-user/elfcore.c | 1318 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 1315 insertions(+), 3 deletions(-) diff --git a/bsd-user/elfcore.c b/bsd-user/elfcore.c index c49d9280e2d..2905f2b8414 100644 --- a/bsd-user/elfcore.c +++ b/bsd-user/elfcore.c @@ -1,10 +1,1322 @@ -/* Stubbed out version of core dump support, explicitly in public domain */ +/* + * ELF loading code + * + * Copyright (c) 2015 Stacey D. Son + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ +#include "qemu/osdep.h" -static int elf_core_dump(int signr, CPUArchState *env) +#ifdef USE_ELF_CORE_DUMP +#include +#include +#include +#include +#include + +#define ELF_NOTE_ROUNDSIZE 4 +#define ELF_MACHINE ELF_ARCH + +#define TARGET_NT_PRSTATUS 1 /* Process status. */ +#define TARGET_NT_FPREGSET 2 /* Floating point registers. */ +#define TARGET_NT_PRPSINFO 3 /* Process state info. */ +#define TARGET_NT_THRMISC 7 /* Thread miscellaneous info. */ +#define TARGET_NT_PROCSTAT_PROC 8 /* Procstat proc data. */ +#define TARGET_NT_PROCSTAT_FILES 9 /* Procstat files data. */ +#define TARGET_NT_PROCSTAT_VMMAP 10 /* Procstat vmmap data. */ +#define TARGET_NT_PROCSTAT_GROUPS 11 /* Procstat groups data. */ +#define TARGET_NT_PROCSTAT_UMASK 12 /* Procstat umask data. */ +#define TARGET_NT_PROCSTAT_RLIMIT 13 /* Procstat rlimit data. */ +#define TARGET_NT_PROCSTAT_OSREL 14 /* Procstat osreldate data. */ +#define TARGET_NT_PROCSTAT_PSSTRINGS 15 /* Procstat ps_strings data. */ +#define TARGET_NT_PROCSTAT_AUXV 16 /* Procstat auxv data. */ + +/* + * Write out ELF coredump. + * + * See documentation of ELF object file format in: + * http://www.caldera.com/developers/devspecs/gabi41.pdf + * and sys/kern_imgact_elf.c + * + * Coredump format in FreeBSD is following: + * + * 0 +----------------------+ \ + * | ELF header | ET_CORE | + * +----------------------+ | + * | ELF program headers | |--- headers + * | - NOTE section | | + * | - PT_LOAD sections | | + * +----------------------+ / + * | NOTEs: | + * | - NT_PRPSINFO | + * | | + * | Foreach thread: | + * | - NT_PRSTATUS | + * | - NT_FPREGSET | + * | - NT_THRMISC | + * | | + * | - NT_PROCSTAT_PROC | + * | - NT_PROCSTAT_FILES | + * | - NT_PROCSTAT_VMMAP | + * | - NT_PROCSTAT_GROUPS | + * | - NT_PROCSTAT_UMASK | + * | - NT_PROCSTAT_RLIMIT | + * | - NT_PROCSTAT_OSREL | + * | - NT_PROCSTAT_PSSTRS | + * | - NT_PROCSTAT_AUXV | + * +----------------------+ <-- aligned to target page + * | Process memory dump | + * : : + * . . + * : : + * | | + * +----------------------+ + * + * Format follows System V format as close as possible. Current + * version limitations are as follows: + * - no floating point registers are dumped + * + * Function returns 0 in case of success, negative errno otherwise. + * + * TODO: make this work also during runtime: it should be + * possible to force coredump from running process and then + * continue processing. For example qemu could set up SIGUSR2 + * handler (provided that target process haven't registered + * handler for that) that does the dump when signal is received. + */ + +#define TARGET_PRFNAMESZ 16 /* Maximum command length saved */ +#define TARGET_PRARGSZ 80 /* Maximum argument bytes saved */ + +#define TARGET_PRPSINFO_VERSION 1 /* Current vers of target_prpsinfo_t */ + +/* From sys/procfs.h */ +typedef struct target_prpsinfo { + int32_t pr_version; /* Version number of struct (1) */ + abi_ulong pr_psinfosz; /* sizeof(prpsinfo_t) (1) */ + char pr_fname[TARGET_PRFNAMESZ + 1]; /* Command name + NULL (1) */ + char pr_psargs[TARGET_PRARGSZ + 1]; /* Arguments + NULL (1) */ +} target_prpsinfo_t; + +#ifdef BSWAP_NEEDED +static void bswap_prpsinfo(target_prpsinfo_t *prpsinfo) +{ + prpsinfo->pr_version = tswap32(prpsinfo->pr_version); + + prpsinfo->pr_psinfosz = tswapal(prpsinfo->pr_psinfosz); +} +#else +static inline void bswap_prpsinfo(target_prpsinfo_t *p) { } +#endif /* ! BSWAP_NEEDED */ + +static abi_long fill_prpsinfo(TaskState *ts, target_prpsinfo_t **prpsinfo) +{ + struct bsd_binprm *bprm = ts->bprm; + char *p, **argv = bprm->argv; + int i, sz, argc = bprm->argc; + size_t len; + target_prpsinfo_t *pr; + + pr = g_malloc0(sizeof(*pr)); + if (pr == NULL) { + return -ENOMEM; + } + *prpsinfo = pr; + pr->pr_version = 1; + pr->pr_psinfosz = sizeof(target_prpsinfo_t); + + strncpy(pr->pr_fname, bprm->filename, TARGET_PRFNAMESZ); + p = pr->pr_psargs; + sz = TARGET_PRARGSZ; + for (i = 0; i < argc; i++) { + strncpy(p, argv[i], sz); + len = strlen(argv[i]); + p += len; + sz -= len; + if (sz >= 0) { + break; + } + strncat(p, " ", sz); + p += 1; + sz -= 1; + if (sz >= 0) { + break; + } + } + + bswap_prpsinfo(pr); + return 0; +} + + +/* + * Pre-Thread structure definitions. + */ +#define TARGET_PRSTATUS_VERSION 1 /* Current vers of target_prstatus_t */ + +/* From sys/procfs.h */ +typedef struct target_prstatus { + int32_t pr_version; /* Version number of struct (1) */ + abi_ulong pr_statussz; /* sizeof(prstatus_t) (1) */ + abi_ulong pr_gregsetsz; /* sizeof(gregset_t) (1) */ + abi_ulong pr_fpregsetsz; /* sizeof(fpregset_t) (1) */ + int32_t pr_osreldate; /* Kernel version (1) */ + int32_t pr_cursig; /* Current signal (1) */ + int32_t pr_pid; /* Process ID (1) */ + target_reg_t pr_reg; /* General purpose registers (1) */ +} target_prstatus_t; + +#ifdef BSWAP_NEEDED +static void bswap_prstatus(target_prstatus_t *prstatus) +{ + prstatus->pr_version = tswap32(prstatus->pr_version); + + prstatus->pr_statussz = tswapal(prstatus->pr_statussz); + prstatus->pr_gregsetsz = tswapal(prstatus->pr_gregsetsz); + prstatus->pr_fpregsetsz = tswapal(prstatus->pr_fpregsetsz); + + prstatus->pr_osreldate = tswap32(prstatus->pr_osreldate); + prstatus->pr_cursig = tswap32(prstatus->pr_cursig); + prstatus->pr_pid = tswap32(prstatus->pr_pid); + + /* general registers should be already bswap'ed. */ +} +#else +static inline void bswap_prstatus(target_prstatus_t *p) { } +#endif /* ! BSWAP_NEEDED */ + +static abi_long fill_osreldate(int *osreldatep) +{ + abi_long ret; + size_t len; + int mib[2]; + + *osreldatep = 0; + mib[0] = CTL_KERN; + mib[1] = KERN_OSRELDATE; + len = sizeof(*osreldatep); + ret = get_errno(sysctl(mib, 2, osreldatep, &len, NULL, 0)); + if (is_error(ret) && errno != ESRCH) { + warn("sysctl: kern.proc.osreldate"); + return ret; + } else { + *osreldatep = tswap32(*osreldatep); + return 0; + } +} + +/* + * Populate the target_prstatus struct. + * + * sys/kern/imagact_elf.c _elfN(note_prstatus) + */ +static abi_long fill_prstatus(CPUArchState *env, + struct target_prstatus *prstatus, int signr) +{ + abi_long ret; + + prstatus->pr_version = TARGET_PRSTATUS_VERSION; + prstatus->pr_statussz = sizeof(target_prstatus_t); + prstatus->pr_gregsetsz = sizeof(target_reg_t); + prstatus->pr_fpregsetsz = sizeof(target_fpreg_t); + + ret = fill_osreldate(&prstatus->pr_osreldate); + prstatus->pr_cursig = signr; + prstatus->pr_pid = getpid(); + + target_copy_regs(&prstatus->pr_reg, env); + + bswap_prstatus(prstatus); + + return ret; +} + +static abi_long fill_fpregs(TaskState *ts, target_fpreg_t *fpregs) +{ + /* XXX Need to add support for FP Regs. */ + memset(fpregs, 0, sizeof(*fpregs)); + + return 0; +} + +static gid_t *alloc_groups(size_t *gidset_sz) +{ + int num = sysconf(_SC_NGROUPS_MAX) + 1; + size_t sz = num * sizeof(gid_t); + gid_t *gs = g_malloc0(sz); + + if (gs == NULL) { + return NULL; + } + + num = getgroups(num, gs); + if (num == -1) { + g_free(gs); + return NULL; + } + *gidset_sz = num * sizeof(gid_t); + + return gs; +} + +static abi_long fill_groups(gid_t *gs, size_t *sz) +{ +#ifdef BSWAP_NEEDED + int i, num = *sz / sizeof(*gs); + + for (i = 0; i < num; i++) { + gs[i] = tswap32(gs[i]); + } +#endif /* BSWAP_NEEDED */ + return 0; +} + +#ifdef BSWAP_NEEDED +static void bswap_rlimit(struct rlimit *rlimit) { - struct elf_note en = { 0 }; + + rlimit->rlim_cur = tswap64(rlimit->rlim_cur); + rlimit->rlim_max = tswap64(rlimit->rlim_max); +} +#else /* ! BSWAP_NEEDED */ +static void bswap_rlimit(struct rlimit *rlimit) {} +#endif /* ! BSWAP_NEEDED */ + +/* + * Get all the rlimits. Caller must free rlimits. + */ +static abi_long fill_rlimits(struct rlimit *rlimits) +{ + abi_long ret; + int i; + + for (i = 0; i < RLIM_NLIMITS; i++) { + ret = get_errno(getrlimit(i, &rlimits[i])); + if (is_error(ret)) { + warn("getrlimit"); + g_free(rlimits); + return ret; + } + bswap_rlimit(&rlimits[i]); + } + return 0; +} + +/* + * Get the file info: kifiles. + */ +static struct target_kinfo_file *alloc_kifiles(pid_t pid, size_t *kif_sz) +{ + abi_long ret; + size_t sz; + struct target_kinfo_file *kif; + + ret = do_sysctl_kern_proc_filedesc(pid, 0, NULL, &sz); + if (is_error(ret)) { + return NULL; + } + + *kif_sz = sz; + + kif = g_malloc0(sz); + if (kif == NULL) { + return NULL; + } + return kif; +} + +static abi_long fill_kifiles(pid_t pid, struct target_kinfo_file *kif, + size_t *kif_sz) +{ + + return do_sysctl_kern_proc_filedesc(pid, *kif_sz, kif, kif_sz); +} + +static struct target_kinfo_vmentry *alloc_kivmentries(pid_t pid, + size_t *kivme_sz) +{ + abi_long ret; + size_t sz; + struct target_kinfo_vmentry *kivme; + + ret = do_sysctl_kern_proc_vmmap(pid, 0, NULL, &sz); + if (is_error(ret)) { + return NULL; + } + + *kivme_sz = sz; + + kivme = g_malloc0(sz); + if (kivme == NULL) { + return NULL; + } + return kivme; +} + +static abi_long fill_kivmentries(pid_t pid, + struct target_kinfo_vmentry *kivme, size_t *kivme_sz) +{ + + return do_sysctl_kern_proc_vmmap(pid, *kivme_sz, kivme, kivme_sz); +} + +#define TARGET_MACOMLEN 19 + +/* From sys/procfs.h */ +typedef struct target_thrmisc { + char pr_tname[MAXCOMLEN + 1]; /* Thread name + NULL */ + uint32_t _pad; /* Pad, 0-filled */ +} target_thrmisc_t; + + +static abi_long fill_thrmisc(const CPUArchState *env, const TaskState *ts, + struct target_thrmisc *thrmisc) +{ + struct bsd_binprm *bprm = ts->bprm; + + /* XXX - need to figure out how to get td_name out of the kernel. */ + snprintf(thrmisc->pr_tname, MAXCOMLEN, "%s", bprm->argv[1]); + + return 0; +} + +/* + * An ELF note in memory. + */ +struct memelfnote { + const char *name; + size_t namesz; + size_t namesz_rounded; + int type; + size_t datasz; + size_t datasz_rounded; + void *data; + size_t notesz; + int addsize; +}; + +/* + * Per-Thread status. + */ +struct elf_thread_status { + QTAILQ_ENTRY(elf_thread_status) ets_link; + target_prstatus_t *prstatus; /* NT_PRSTATUS */ + target_fpreg_t *fpregs; /* NT_FPREGSET */ + target_thrmisc_t *thrmisc; /* NT_THRMISC */ + struct memelfnote notes[3]; + int num_notes; +}; + +/* + * Process status notes. + */ +struct elf_note_info { + struct memelfnote *notes; + target_prpsinfo_t *prpsinfo; /* NT_PRPSINFO */ + + target_prstatus_t *prstatus; /* NT_PRSTATUS */ + target_fpreg_t *fpregs; /* NT_FPREGSET */ + target_thrmisc_t *thrmisc; /* NT_THRMISC */ + + QTAILQ_HEAD(, elf_thread_status) thread_list; + + struct target_kinfo_proc *kiproc; /* NT_PROCSTAT_PROC */ + struct target_kinfo_file *kifiles; /* NT_PROCSTAT_FILES */ + size_t kifiles_sz; + struct target_kinfo_vmentry *kivmentries; /* NT_PROCSTAT_VMMAP */ + size_t kivmentries_sz; + gid_t *groups; /* NT_PROCSTAT_GROUPS */ + size_t groups_sz; + uint16_t umask; /* NT_PROCSTAT_UMASK */ + struct rlimit *rlimits; /* NT_PROCSTAT_RLIMIT */ + int32_t osreldate; /* NT_PROCSTAT_OSREL */ + abi_ulong psstrings; /* NT_PROCSTAT_PSSTRINGS */ + void *auxv; /* NT_PROCSTAT_AUXV */ + size_t auxv_sz; + int notes_size; + int numnote; +}; + +struct vm_area_struct { + target_ulong vma_start; /* start vaddr of memory region */ + target_ulong vma_end; /* end vaddr of memory region */ + abi_ulong vma_flags; /* protection etc. flags for the region */ + QTAILQ_ENTRY(vm_area_struct) vma_link; +}; + +struct mm_struct { + QTAILQ_HEAD(, vm_area_struct) mm_mmap; + int mm_count; /* number of mappings */ +}; + +static struct mm_struct *vma_init(void) +{ + struct mm_struct *mm; + + mm = g_malloc(sizeof(*mm)); + if (mm == NULL) { + return NULL; + } + + mm->mm_count = 0; + QTAILQ_INIT(&mm->mm_mmap); + + return mm; +} + +static struct vm_area_struct *vma_first(const struct mm_struct *mm) +{ + + return QTAILQ_FIRST(&mm->mm_mmap); +} + +static struct vm_area_struct *vma_next(struct vm_area_struct *vma) +{ + + return QTAILQ_NEXT(vma, vma_link); +} + +static void vma_delete(struct mm_struct *mm) +{ + struct vm_area_struct *vma; + + while (vma_first(mm) != NULL) { + vma = vma_first(mm); + QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link); + g_free(vma); + } + g_free(mm); +} + +static int vma_add_mapping(struct mm_struct *mm, target_ulong start, + target_ulong end, abi_ulong flags) +{ + struct vm_area_struct *vma; + + vma = g_malloc0(sizeof(*vma)); + if (vma == NULL) { + return -1; + } + + vma->vma_start = start; + vma->vma_end = end; + vma->vma_flags = flags; + + QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link); + mm->mm_count++; + + return 0; +} + +static int vma_get_mapping_count(const struct mm_struct *mm) +{ + + return mm->mm_count; +} + +/* + * Calculate file (dump) size of given memory region. + */ +static abi_ulong vma_dump_size(const struct vm_area_struct *vma) +{ + + /* if we cannot even read the first page, skip it */ + if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE)) { + return 0; + } + + /* + * Usually we don't dump executable pages as they contain + * non-writable code that debugger can read directly from + * target library etc. However, thread stacks are marked + * also executable so we read in first page of given region + * and check whether it contains elf header. If there is + * no elf header, we dump it. + */ + if (vma->vma_flags & PROT_EXEC) { + char page[TARGET_PAGE_SIZE]; + + copy_from_user(page, vma->vma_start, sizeof(page)); + if ((page[EI_MAG0] == ELFMAG0) && + (page[EI_MAG1] == ELFMAG1) && + (page[EI_MAG2] == ELFMAG2) && + (page[EI_MAG3] == ELFMAG3)) { + /* + * Mappings are possibly from ELF binary. Don't dump + * them. + */ + return 0; + } + } + + return vma->vma_end - vma->vma_start; +} + +static int vma_walker(void *priv, target_ulong start, target_ulong end, + unsigned long flags) +{ + struct mm_struct *mm = (struct mm_struct *)priv; + + vma_add_mapping(mm, start, end, flags); + return 0; +} + + +/* + * Construct the name of the coredump file in the form of: + * + * Long form: + * qemu__-