From patchwork Tue Feb 27 10:39:45 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: 26104 Received: (qmail 40903 invoked by alias); 27 Feb 2018 10:39:51 -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 40888 invoked by uid 89); 27 Feb 2018 10:39:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.2 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= X-HELO: mga17.intel.com Received: from mga17.intel.com (HELO mga17.intel.com) (192.55.52.151) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 27 Feb 2018 10:39:48 +0000 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 Feb 2018 02:39:47 -0800 X-ExtLoop1: 1 Received: from irvmail001.ir.intel.com ([163.33.26.43]) by fmsmga002.fm.intel.com with ESMTP; 27 Feb 2018 02:39:46 -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 w1RAdkxx006798; Tue, 27 Feb 2018 10:39:46 GMT Received: from ulvlx001.iul.intel.com (localhost [127.0.0.1]) by ulvlx001.iul.intel.com with ESMTP id w1RAdjrA018203; Tue, 27 Feb 2018 11:39:45 +0100 Received: (from mmetzger@localhost) by ulvlx001.iul.intel.com with LOCAL id w1RAdju4018199; Tue, 27 Feb 2018 11:39:45 +0100 From: Markus Metzger To: gdb-patches@sourceware.org Subject: [PATCH] btrace, gdbserver: check btrace target pointers Date: Tue, 27 Feb 2018 11:39:45 +0100 Message-Id: <1519727985-17914-1-git-send-email-markus.t.metzger@intel.com> X-IsSubscribed: yes By removing the supports_btrace gdbserver target method we relied on GDB trying to enable branch tracing and failing on the attempt. For targets that do not provide the btrace methods, however, an initial request from GDB for the branch trace configuration to detect whether gdbserver is already recording resulted in a protocol error. Have the btrace target methods throw a "Target does not suppor branch tracing" error and be prepared to handle exceptions in all functions that call btrace target methods. We therefore turn the target_* macros into static inline functions. Also remove the additional btrace target method checks that resulted in the above protocol error. Thanks to Maciej W. Rozycki for reporting this. 2018-02-27 Markus Metzger gdbserver/ * target.h (target_enable_btrace, target_disable_btrace, target_read_btrace, target_read_btrace_conf): Turn macro into inline function. Throw error if target method not defined. * server.c (handle_qxfer_btrace, handle_qxfer_btrace_conf): Remove check for btrace target method. Be prepared to handle exceptions from btrace target methods. --- gdb/gdbserver/server.c | 38 ++++++++++++++++++++++++++++---------- gdb/gdbserver/target.h | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c index cb02b58..4fd274f 100644 --- a/gdb/gdbserver/server.c +++ b/gdb/gdbserver/server.c @@ -1818,7 +1818,7 @@ handle_qxfer_btrace (const char *annex, enum btrace_read_type type; int result; - if (the_target->read_btrace == NULL || writebuf != NULL) + if (writebuf != NULL) return -2; if (ptid_equal (general_thread, null_ptid) @@ -1857,12 +1857,21 @@ handle_qxfer_btrace (const char *annex, { buffer_free (&cache); - result = target_read_btrace (thread->btrace, &cache, type); - if (result != 0) + TRY { - memcpy (own_buf, cache.buffer, cache.used_size); - return -3; + result = target_read_btrace (thread->btrace, &cache, type); + if (result != 0) + memcpy (own_buf, cache.buffer, cache.used_size); } + CATCH (exception, RETURN_MASK_ERROR) + { + sprintf (own_buf, "E.%s", exception.message); + result = -1; + } + END_CATCH + + if (result != 0) + return -3; } else if (offset > cache.used_size) { @@ -1889,7 +1898,7 @@ handle_qxfer_btrace_conf (const char *annex, struct thread_info *thread; int result; - if (the_target->read_btrace_conf == NULL || writebuf != NULL) + if (writebuf != NULL) return -2; if (annex[0] != '\0') @@ -1919,12 +1928,21 @@ handle_qxfer_btrace_conf (const char *annex, { buffer_free (&cache); - result = target_read_btrace_conf (thread->btrace, &cache); - if (result != 0) + TRY { - memcpy (own_buf, cache.buffer, cache.used_size); - return -3; + result = target_read_btrace_conf (thread->btrace, &cache); + if (result != 0) + memcpy (own_buf, cache.buffer, cache.used_size); } + CATCH (exception, RETURN_MASK_ERROR) + { + sprintf (own_buf, "E.%s", exception.message); + result = -1; + } + END_CATCH + + if (result != 0) + return -3; } else if (offset > cache.used_size) { diff --git a/gdb/gdbserver/target.h b/gdb/gdbserver/target.h index dcefe1a..ff38c70 100644 --- a/gdb/gdbserver/target.h +++ b/gdb/gdbserver/target.h @@ -620,17 +620,43 @@ int kill_inferior (int); (the_target->supports_agent ? \ (*the_target->supports_agent) () : 0) -#define target_enable_btrace(ptid, conf) \ - (*the_target->enable_btrace) (ptid, conf) +static inline struct btrace_target_info * +target_enable_btrace (ptid_t ptid, const struct btrace_config *conf) +{ + if (the_target->enable_btrace == nullptr) + error ("Target does not support branch tracing."); + + return (*the_target->enable_btrace) (ptid, conf); +} + +static inline int +target_disable_btrace (struct btrace_target_info *tinfo) +{ + if (the_target->disable_btrace == nullptr) + error ("Target does not support branch tracing."); -#define target_disable_btrace(tinfo) \ - (*the_target->disable_btrace) (tinfo) + return (*the_target->disable_btrace) (tinfo); +} -#define target_read_btrace(tinfo, buffer, type) \ - (*the_target->read_btrace) (tinfo, buffer, type) +static inline int +target_read_btrace (struct btrace_target_info *tinfo, struct buffer *buffer, + enum btrace_read_type type) +{ + if (the_target->read_btrace == nullptr) + error ("Target does not support branch tracing."); + + return (*the_target->read_btrace) (tinfo, buffer, type); +} + +static inline int +target_read_btrace_conf (struct btrace_target_info *tinfo, + struct buffer *buffer) +{ + if (the_target->read_btrace_conf == nullptr) + error ("Target does not support branch tracing."); -#define target_read_btrace_conf(tinfo, buffer) \ - (*the_target->read_btrace_conf) (tinfo, buffer) + return (*the_target->read_btrace_conf) (tinfo, buffer); +} #define target_supports_range_stepping() \ (the_target->supports_range_stepping ? \