From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6858 invoked by alias); 7 Dec 2012 10:38:12 -0000 Received: (qmail 6783 invoked by uid 22791); 7 Dec 2012 10:38:11 -0000 X-SWARE-Spam-Status: No, hits=-7.8 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_DNSWL_HI,RCVD_IN_HOSTKARMA_W,RP_MATCHES_RCVD,TW_CP,TW_QX X-Spam-Check-By: sourceware.org Received: from mga14.intel.com (HELO mga14.intel.com) (143.182.124.37) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 07 Dec 2012 10:37:52 +0000 Received: from azsmga002.ch.intel.com ([10.2.17.35]) by azsmga102.ch.intel.com with ESMTP; 07 Dec 2012 02:37:38 -0800 X-ExtLoop1: 1 Received: from swsutil001.isw.intel.com ([10.237.237.11]) by AZSMGA002.ch.intel.com with ESMTP; 07 Dec 2012 02:37:35 -0800 Received: from ulslx001.iul.intel.com (ulslx001.iul.intel.com [172.28.207.63]) by swsutil001.isw.intel.com (8.13.6/8.13.6/MailSET/Hub) with ESMTP id qB7AbYd4003996; Fri, 7 Dec 2012 10:37:34 GMT Received: from ulslx001.iul.intel.com (localhost [127.0.0.1]) by ulslx001.iul.intel.com with ESMTP id qB7AbYWT026041; Fri, 7 Dec 2012 11:37:34 +0100 Received: (from mmetzger@localhost) by ulslx001.iul.intel.com with id qB7AbYa0026037; Fri, 7 Dec 2012 11:37:34 +0100 From: markus.t.metzger@intel.com To: gdb-patches@sourceware.org Cc: markus.t.metzger@gmail.com, jan.kratochvil@redhat.com, palves@redhat.com, tromey@redhat.com, kettenis@gnu.org, Markus Metzger Subject: [patch v5 08/12] gdbserver, btrace: add generic btrace support Date: Fri, 07 Dec 2012 10:38:00 -0000 Message-Id: <1354876644-25749-9-git-send-email-markus.t.metzger@intel.com> In-Reply-To: <1354876644-25749-1-git-send-email-markus.t.metzger@intel.com> References: <1354876644-25749-1-git-send-email-markus.t.metzger@intel.com> X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-12/txt/msg00156.txt.bz2 From: Markus Metzger Add support to gdbserver to understand branch trace related packages. 2012-12-07 Markus Metzger gdbserver/ * target.h (struct target_ops): Add btrace ops. (target_supports_btrace): New macro. (target_btrace_has_changed): New macro. (target_enable_btrace): New macro. (target_disable_btrace): New macro. (target_read_btrace): New macro. * gdbthread.h (struct thread_info): Add btrace field. * server.c (handle_btrace_general_set): New function. (handle_btrace_enable): New function. (handle_btrace_disable): New function. (handle_general_set): Call handle_btrace_general_set. (handle_qxfer_btrace): New function. (struct qxfer qxfer_packets[]): Add btrace entry. (handle_btrace_query): New function. (handle_query): Add btrace to supported query, call handle_btrace_query. * inferiors.c (remove_thread): Disable btrace. --- gdb/gdbserver/gdbthread.h | 5 ++ gdb/gdbserver/inferiors.c | 3 + gdb/gdbserver/server.c | 175 +++++++++++++++++++++++++++++++++++++++++++++ gdb/gdbserver/target.h | 33 +++++++++ 4 files changed, 216 insertions(+), 0 deletions(-) diff --git a/gdb/gdbserver/gdbthread.h b/gdb/gdbserver/gdbthread.h index adc23da..6238ed2 100644 --- a/gdb/gdbserver/gdbthread.h +++ b/gdb/gdbserver/gdbthread.h @@ -22,6 +22,8 @@ #include "server.h" +struct btrace_target_info; + struct thread_info { struct inferior_list_entry entry; @@ -58,6 +60,9 @@ struct thread_info Each item in the list holds the current step of the while-stepping action. */ struct wstep_state *while_stepping; + + /* Branch trace target information for this thread. */ + struct btrace_target_info *btrace; }; extern struct inferior_list all_threads; diff --git a/gdb/gdbserver/inferiors.c b/gdb/gdbserver/inferiors.c index 76abaf5..76f5731 100644 --- a/gdb/gdbserver/inferiors.c +++ b/gdb/gdbserver/inferiors.c @@ -161,6 +161,9 @@ free_one_thread (struct inferior_list_entry *inf) void remove_thread (struct thread_info *thread) { + if (thread->btrace) + target_disable_btrace (thread->btrace); + remove_inferior (&all_threads, (struct inferior_list_entry *) thread); free_one_thread (&thread->entry); } diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c index fae9199..090123e 100644 --- a/gdb/gdbserver/server.c +++ b/gdb/gdbserver/server.c @@ -440,6 +440,88 @@ write_qxfer_response (char *buf, const void *data, int len, int is_more) PBUFSIZ - 2) + 1; } +/* Handle btrace enabling. */ + +static const char * +handle_btrace_enable (struct thread_info *thread) +{ + if (thread->btrace) + return "E.Btrace already enabled."; + + thread->btrace = target_enable_btrace (thread->entry.id); + if (!thread->btrace) + return "E.Could not enable btrace."; + + return NULL; +} + +/* Handle btrace disabling. */ + +static const char * +handle_btrace_disable (struct thread_info *thread) +{ + + if (!thread->btrace) + return "E.Branch tracing not enabled."; + + if (target_disable_btrace (thread->btrace) != 0) + return "E.Could not disable branch tracing."; + + thread->btrace = NULL; + return NULL; +} + +/* Handle the "Qbtrace" packet. */ + +static int +handle_btrace_general_set (char *own_buf) +{ + struct thread_info *thread; + const char *err; + char *op; + + if (strncmp ("Qbtrace:", own_buf, strlen ("Qbtrace:")) != 0) + return 0; + + op = own_buf + strlen ("Qbtrace:"); + + if (!target_supports_btrace ()) + { + strcpy (own_buf, "E.Target does not support branch tracing."); + return 1; + } + + if (ptid_equal (general_thread, null_ptid) + || ptid_equal (general_thread, minus_one_ptid)) + { + strcpy (own_buf, "E.Must select a single thread."); + return -1; + } + + thread = find_thread_ptid (general_thread); + if (!thread) + { + strcpy (own_buf, "E.No such thread."); + return -1; + } + + err = NULL; + + if (strncmp ("on", op, strlen ("on")) == 0) + err = handle_btrace_enable (thread); + else if (strncmp ("off", op, strlen ("off")) == 0) + err = handle_btrace_disable (thread); + else + err = "E.Bad Qbtrace operation. Use on or off."; + + if (err) + strcpy (own_buf, err); + else + write_ok (own_buf); + + return 1; +} + /* Handle all of the extended 'Q' packets. */ static void @@ -596,6 +678,9 @@ handle_general_set (char *own_buf) return; } + if (handle_btrace_general_set (own_buf)) + return; + /* Otherwise we didn't know what packet it was. Say we didn't understand it. */ own_buf[0] = 0; @@ -1295,9 +1380,66 @@ handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf, return (*the_target->read_loadmap) (annex, offset, readbuf, len); } +/* Handle qXfer:btrace:read. */ + +static int +handle_qxfer_btrace (const char *annex, + gdb_byte *readbuf, const gdb_byte *writebuf, + ULONGEST offset, LONGEST len) +{ + static struct buffer cache; + struct thread_info *thread; + + if (!the_target->read_btrace || writebuf) + return -2; + + if (!target_running () || annex[0] != '\0') + return -1; + + if (ptid_equal (general_thread, null_ptid) + || ptid_equal (general_thread, minus_one_ptid)) + { + strcpy (own_buf, "E.Must select a single thread."); + return -1; + } + + thread = find_thread_ptid (general_thread); + if (!thread) + { + strcpy (own_buf, "E.No such thread."); + return -1; + } + + if (!thread->btrace) + { + strcpy (own_buf, "E.Btrace not enabled."); + return -1; + } + + if (!offset) + { + buffer_free (&cache); + + target_read_btrace (thread->btrace, &cache); + } + else if (offset > cache.used_size) + { + buffer_free (&cache); + return -1; + } + + if (len > cache.used_size - offset) + len = cache.used_size - offset; + + memcpy (readbuf, cache.buffer + offset, len); + + return len; +} + static const struct qxfer qxfer_packets[] = { { "auxv", handle_qxfer_auxv }, + { "btrace", handle_qxfer_btrace }, { "fdpic", handle_qxfer_fdpic}, { "features", handle_qxfer_features }, { "libraries", handle_qxfer_libraries }, @@ -1462,6 +1604,29 @@ crc32 (CORE_ADDR base, int len, unsigned int crc) return (unsigned long long) crc; } +/* Handle the "qbtrace" packet. */ + +static int +handle_btrace_query (char *own_buf) +{ + if (strncmp ("qbtrace:", own_buf, strlen ("qbtrace:")) == 0) + { + char *ptid_str = own_buf + strlen ("qbtrace:"); + ptid_t ptid = read_ptid (ptid_str, NULL); + struct thread_info *thread = find_thread_ptid (ptid); + + if (!thread) + strcpy (own_buf, "E.No such thread."); + else if (!thread->btrace) + strcpy (own_buf, "E.Btrace not enabled."); + else + strcpy (own_buf, + (target_btrace_has_changed (thread->btrace) ? "yes" : "no")); + return 1; + } + return 0; +} + /* Handle all of the extended 'q' packets. */ void @@ -1691,6 +1856,13 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) if (target_supports_agent ()) strcat (own_buf, ";QAgent+"); + if (target_supports_btrace ()) + { + strcat (own_buf, ";qbtrace+"); + strcat (own_buf, ";Qbtrace+"); + strcat (own_buf, ";qXfer:btrace:read+"); + } + return; } @@ -1881,6 +2053,9 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) if (target_supports_tracepoints () && handle_tracepoint_query (own_buf)) return; + if (handle_btrace_query (own_buf)) + return; + /* Otherwise we didn't know what packet it was. Say we didn't understand it. */ own_buf[0] = 0; diff --git a/gdb/gdbserver/target.h b/gdb/gdbserver/target.h index 9f96e04..952c973 100644 --- a/gdb/gdbserver/target.h +++ b/gdb/gdbserver/target.h @@ -22,6 +22,8 @@ #define TARGET_H struct emit_ops; +struct btrace_target_info; +struct buffer; /* Ways to "resume" a thread. */ @@ -397,6 +399,22 @@ struct target_ops /* Return true if target supports debugging agent. */ int (*supports_agent) (void); + + /* Check whether the target supports branch tracing. */ + int (*supports_btrace) (void); + + /* Enable branch tracing for @ptid and allocate a branch trace target + information struct for reading and for disabling branch trace. */ + struct btrace_target_info *(*enable_btrace) (ptid_t ptid); + + /* Disable branch tracing. */ + int (*disable_btrace) (struct btrace_target_info *tinfo); + + /* Check whether branch trace changed on the target. */ + int (*btrace_has_changed) (struct btrace_target_info *); + + /* Read branch trace data into buffer. */ + void (*read_btrace) (struct btrace_target_info *, struct buffer *); }; extern struct target_ops *the_target; @@ -520,6 +538,21 @@ int kill_inferior (int); (the_target->supports_agent ? \ (*the_target->supports_agent) () : 0) +#define target_supports_btrace() \ + (the_target->supports_btrace ? (*the_target->supports_btrace) () : 0) + +#define target_enable_btrace(ptid) \ + (*the_target->enable_btrace) (ptid) + +#define target_disable_btrace(tinfo) \ + (*the_target->disable_btrace) (tinfo) + +#define target_btrace_has_changed(tinfo) \ + (*the_target->btrace_has_changed) (tinfo) + +#define target_read_btrace(tinfo, buffer) \ + (*the_target->read_btrace) (tinfo, buffer) + /* Start non-stop mode, returns 0 on success, -1 on failure. */ int start_non_stop (int nonstop); -- 1.7.1