From patchwork Wed Jul 11 12:35:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Ser X-Patchwork-Id: 28309 Received: (qmail 9520 invoked by alias); 11 Jul 2018 12:35:57 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 9498 invoked by uid 89); 11 Jul 2018 12:35:56 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_HELO_PASS, SPF_PASS, UNWANTED_LANGUAGE_BODY autolearn=ham version=3.3.2 spammy=sk:contact, U*contact X-HELO: mail2.protonmail.ch Received: from mail2.protonmail.ch (HELO mail2.protonmail.ch) (185.70.40.22) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 11 Jul 2018 12:35:54 +0000 Date: Wed, 11 Jul 2018 08:35:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emersion.fr; s=protonmail; t=1531312551; bh=vGSXS8yATGsRtqK14aXD4jVlwk277L6+Pnq7En7QKHI=; h=Date:To:From:Cc:Reply-To:Subject:Feedback-ID:From; b=nbgCW3o6H4FElvMdMocY74u2DiG32j2nL5rH1GaNjhp1vDpqUc12KhjUgkukS3RJk Ezz+MUaP7ked7OC2guaMjo5zjYBGp2Wr6+kAPbS7g8EnVQd7BpFaJcB4lWnEJpZXCh fffXB4M1810rWe+Gdl9FPHias3FOgEgkVNGECeMk= To: gdb-patches@sourceware.org From: Simon Ser Cc: Simon Ser , jhb@FreeBSD.org Reply-To: Simon Ser Subject: [PATCH] Generate NT_PROCSTAT_{AUXV,VMMAP} in FreeBSD coredumps Message-ID: MIME-Version: 1.0 gcore generates NT_AUXV and NT_FILE notes for Linux targets. On FreeBSD auxv is stored in a NT_PROCSTAT_AUXV section, file mappings are stored in a NT_PROCSTAT_VMMAP and both are prefixed with the struct size. 2018-07-11 Simon Ser * fbsd-tdep.c (fbsd_make_corefile_notes): write NT_PROCSTAT_AUXV and NT_PROCSTAT_VMMAP notes --- This is an improvement of my v2 patch [1]. Thanks John for your review! Changes from v2 to v3: - Use NT_FREEBSD_PROCSTAT_* enums instead or re-defining these - Simplify Elf_Auxinfo struct size expression - Also write NT_PROCSTAT_VMMAP notes - Directly use sysctl as this will allow to support more notes in the future (all of these work in the same way) - Use gdb::unique_xmalloc_ptr() [1]: https://sourceware.org/ml/gdb-patches/2018-07/msg00267.html gdb/fbsd-tdep.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c index 9cea0098..e335ee6f 100644 --- a/gdb/fbsd-tdep.c +++ b/gdb/fbsd-tdep.c @@ -25,6 +25,9 @@ #include "regset.h" #include "gdbthread.h" #include "xml-syscall.h" +#include +#include +#include #include "elf-bfd.h" #include "fbsd-tdep.h" @@ -512,6 +515,32 @@ fbsd_corefile_thread (struct thread_info *info, args->note_size, args->stop_signal); } +static gdb::unique_xmalloc_ptr +procstat_sysctl (pid_t pid, int what, size_t structsz, size_t *sizep) +{ + int name[4]; + name[0] = CTL_KERN; + name[1] = KERN_PROC; + name[2] = what; + name[3] = pid; + size_t len = 0; + if (sysctl (name, 4, NULL, &len, NULL, 0) == -1) + return NULL; + + int structsize = structsz; + gdb::unique_xmalloc_ptr buf + ((char *) xmalloc (sizeof (structsize) + len)); + if (buf == NULL) + return NULL; + memcpy (buf.get (), &structsize, sizeof (structsize)); + void *p = buf.get () + sizeof (structsize); + if (sysctl (name, 4, p, &len, NULL, 0) == -1) + return NULL; + + *sizep = sizeof (structsize) + len; + return buf; +} + /* Create appropriate note sections for a corefile, returning them in allocated memory. */ @@ -586,6 +615,35 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size) note_data = thread_args.note_data; + pid_t pid = inferior_ptid.pid (); + + /* Auxillary vector. */ + size_t auxinfo_size = gdbarch_addr_bit (gdbarch) / 4; /* Elf_Auxinfo */ + size_t note_desc_size; + gdb::unique_xmalloc_ptr note_desc; + note_desc = procstat_sysctl (pid, KERN_PROC_AUXV, auxinfo_size, + ¬e_desc_size); + if (note_desc != NULL) + { + note_data = elfcore_write_note (obfd, note_data, note_size, + "FreeBSD", NT_FREEBSD_PROCSTAT_AUXV, + note_desc.get (), note_desc_size); + if (!note_data) + return NULL; + } + + /* File mappings */ + note_desc = procstat_sysctl (pid, KERN_PROC_VMMAP, + sizeof(struct kinfo_vmentry), ¬e_desc_size); + if (note_desc != NULL) + { + note_data = elfcore_write_note (obfd, note_data, note_size, + "FreeBSD", NT_FREEBSD_PROCSTAT_VMMAP, + note_desc.get (), note_desc_size); + if (!note_data) + return NULL; + } + return note_data; }