From patchwork Thu Jan 25 09:12:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Metzger, Markus T" X-Patchwork-Id: 25530 Received: (qmail 26305 invoked by alias); 25 Jan 2018 09:12:35 -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 25569 invoked by uid 89); 25 Jan 2018 09:12:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-23.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=5606, Trace, UD:pt X-HELO: mga09.intel.com Received: from mga09.intel.com (HELO mga09.intel.com) (134.134.136.24) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 25 Jan 2018 09:12:11 +0000 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Jan 2018 01:12:09 -0800 X-ExtLoop1: 1 Received: from irvmail001.ir.intel.com ([163.33.26.43]) by fmsmga008.fm.intel.com with ESMTP; 25 Jan 2018 01:12:08 -0800 Received: from ulvlx001.iul.intel.com (ulvlx001.iul.intel.com [172.28.207.17]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id w0P9C7A6026717; Thu, 25 Jan 2018 09:12:08 GMT Received: from ulvlx001.iul.intel.com (localhost [127.0.0.1]) by ulvlx001.iul.intel.com with ESMTP id w0P9C7HE032500; Thu, 25 Jan 2018 10:12:07 +0100 Received: (from mmetzger@localhost) by ulvlx001.iul.intel.com with LOCAL id w0P9C7gZ032496; Thu, 25 Jan 2018 10:12:07 +0100 From: Markus Metzger To: gdb-patches@sourceware.org Subject: [PATCH 4/5] btrace: improve enable error messages Date: Thu, 25 Jan 2018 10:12:05 +0100 Message-Id: <1516871526-32129-5-git-send-email-markus.t.metzger@intel.com> In-Reply-To: <1516871526-32129-1-git-send-email-markus.t.metzger@intel.com> References: <1516871526-32129-1-git-send-email-markus.t.metzger@intel.com> X-IsSubscribed: yes Improve the error message when GDB fails to start recording branch trace. This patch also removes a zero buffer size check for PT to align with BTS. The buffer size can not be configured to be zero. 2018-01-25 Markus Metzger gdb/ * nat/linux-btrace.c (perf_event_pt_event_type): Improve error message. Return type. Update callers. (linux_enable_bts, linux_enable_pt): Improve error message. (linux_enable_pt): Remove zero buffer size check. (linux_enable_btrace): Improve error messages. Remove NULL return check. --- gdb/nat/linux-btrace.c | 69 +++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c index 08a9716..db93739 100644 --- a/gdb/nat/linux-btrace.c +++ b/gdb/nat/linux-btrace.c @@ -524,7 +524,7 @@ linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf) scoped_close_fd fd (syscall (SYS_perf_event_open, &bts->attr, pid, -1, -1, 0)); if (fd < 0) - return nullptr; + error (_("Failed to start recording: %s"), safe_strerror (errno)); /* Convert the requested size in bytes to pages (rounding up). */ pages = ((size_t) conf->size / PAGE_SIZE @@ -560,6 +560,7 @@ linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf) if ((__u64) length != data_size + PAGE_SIZE) continue; + errno = 0; /* The number of pages we request needs to be a power of two. */ header.reset (NULL, length, PROT_READ, MAP_SHARED, fd, 0); if (header != MAP_FAILED) @@ -567,7 +568,7 @@ linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf) } if (pages == 0) - return nullptr; + error (_("Failed to map trace buffer: %s."), safe_strerror (errno)); data_offset = PAGE_SIZE; @@ -583,7 +584,7 @@ linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf) /* Check for overflows. */ if ((__u64) size != data_size) - return nullptr; + error (_("Failed to determine trace buffer size.")); } #endif /* defined (PERF_ATTR_SIZE_VER5) */ @@ -600,21 +601,23 @@ linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf) #if defined (PERF_ATTR_SIZE_VER5) -/* Determine the event type. - Returns zero on success and fills in TYPE; returns -1 otherwise. */ +/* Determine the event type. */ static int -perf_event_pt_event_type (int *type) +perf_event_pt_event_type (void) { - gdb_file_up file = - gdb_fopen_cloexec ("/sys/bus/event_source/devices/intel_pt/type", "r"); + const char *filename = "/sys/bus/event_source/devices/intel_pt/type"; + + errno = 0; + gdb_file_up file = gdb_fopen_cloexec (filename, "r"); if (file.get () == nullptr) - return -1; + error (_("Failed to open %s: %s."), filename, safe_strerror (errno)); - int found = fscanf (file.get (), "%d", type); - if (found == 1) - return 0; - return -1; + int type, found = fscanf (file.get (), "%d", &type); + if (found != 1) + error (_("Failed to read the PT event type from %s."), filename); + + return type; } /* Enable branch tracing in Intel Processor Trace format. */ @@ -624,14 +627,7 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) { struct btrace_tinfo_pt *pt; size_t pages; - int pid, pg, errcode, type; - - if (conf->size == 0) - return NULL; - - errcode = perf_event_pt_event_type (&type); - if (errcode != 0) - return NULL; + int pid, pg; pid = ptid_get_lwp (ptid); if (pid == 0) @@ -645,7 +641,7 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) pt = &tinfo->variant.pt; pt->attr.size = sizeof (pt->attr); - pt->attr.type = type; + pt->attr.type = perf_event_pt_event_type (); pt->attr.exclude_kernel = 1; pt->attr.exclude_hv = 1; @@ -654,14 +650,14 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) errno = 0; scoped_close_fd fd (syscall (SYS_perf_event_open, &pt->attr, pid, -1, -1, 0)); if (fd < 0) - return nullptr; + error (_("Failed to start recording: %s"), safe_strerror (errno)); /* Allocate the configuration page. */ scoped_mmap header (NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (header == MAP_FAILED) - return nullptr; + error (_("Failed to map trace user page: %s."), safe_strerror (errno)); header->aux_offset = header->data_offset + header->data_size; @@ -700,13 +696,14 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) header->aux_size = data_size; + errno = 0; aux.reset (NULL, length, PROT_READ, MAP_SHARED, fd, header->aux_offset); if (aux != MAP_FAILED) break; } if (pages == 0) - return nullptr; + error (_("Failed to map trace buffer: %s."), safe_strerror (errno)); pt->pt.size = aux.size (); pt->pt.mem = aux.release (); @@ -723,8 +720,7 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) static struct btrace_target_info * linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) { - errno = EOPNOTSUPP; - return NULL; + error (_("GDB does not support Intel PT.")); } #endif /* !defined (PERF_ATTR_SIZE_VER5) */ @@ -734,27 +730,20 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) struct btrace_target_info * linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf) { - struct btrace_target_info *tinfo; - - tinfo = NULL; switch (conf->format) { case BTRACE_FORMAT_NONE: - break; + error (_("Bad branch trace format.")); + + default: + error (_("Unknown branch trace format.")); case BTRACE_FORMAT_BTS: - tinfo = linux_enable_bts (ptid, &conf->bts); - break; + return linux_enable_bts (ptid, &conf->bts); case BTRACE_FORMAT_PT: - tinfo = linux_enable_pt (ptid, &conf->pt); - break; + return linux_enable_pt (ptid, &conf->pt); } - - if (tinfo == NULL) - error (_("Unknown error.")); - - return tinfo; } /* Disable BTS tracing. */