[v6,6/7] add support for coresight btrace via remote protocol

Message ID 20210531213307.275079-7-zied.guermazi@trande.de
State New
Headers show
Series
  • extend branch tracing to use ARM CoreSight traces
Related show

Commit Message

Zied Guermazi May 31, 2021, 9:33 p.m.
This patch extends the remote protocol to use ARM CoreSight traces for btrace.
This patch adds the capabilities enumeration as well as the required protocol
extentions for configuring traces collection, starting and stopping tracing,
and transferring the traces as well as the parameters needed for decoding them.

gdb/ChangeLog

	* NEWS: list new remote packets for extending btrace
	to support using ARM CoreSight Traces.
	* btrace.c (check_xml_btrace_version): add version 1.1
	(parse_xml_btrace_etm_config_source_config_cpu_etmv4_config): New.
	(parse_xml_btrace_etm_config_source_config_cpu_etmv3_config): New.
	(parse_xml_btrace_etm_config_source_config_cpu_etm_config): New.
	(parse_xml_btrace_etm_config_source_config): New.
	(parse_xml_btrace_etm_config_source_config_end): New
	(parse_xml_btrace_etm_config_sink_config): New.
	(parse_xml_btrace_etm_raw): New.
	(parse_xml_btrace_etm): New.
	(parse_xml_btrace_conf_etm): New.
	* btrace.dtd: add etm data and decoding parameters.
	* btrace-conf.dtd: add etm configuration.
	* remote.c (remote_target::btrace_sync_conf): add etm configuration.
	(remote_target::remote_btrace_maybe_reopen): warn if ETM was disabled.
	(remote_target::enable_btrace): add coresight etm.
	(_initialize_remote): add etm related packets.

gdb/doc/ChangeLog

	* gdb.texinfo (General Query Packets) Document extending GDB remote
	protocol packets to support using ARM CoreSight traces for btrace.

gdbserver/ChangeLog

	* configure.srv: add btrace for aarch64*-*-linux* and arm*-*-linux*.
	* linux-low.c (linux_low_encode_etm_config): New.
	(linux_process_target::read_btrace): encode CoreSight traces
	and related decoding parameters.
	(linux_process_target::read_btrace_conf): encode CoreSight
	configuration.
	* server.cc (handle_btrace_enable_etm): New.
	(handle_btrace_general_set): add etm handling.
	(handle_btrace_conf_general_set): add etm handling.
	(supported_btrace_packets): add etm related packets.
---
 gdb/NEWS                     |  11 ++
 gdb/btrace.c                 | 368 ++++++++++++++++++++++++++++++++++-
 gdb/doc/gdb.texinfo          |  63 +++++-
 gdb/features/btrace-conf.dtd |  10 +-
 gdb/features/btrace.dtd      |  38 +++-
 gdb/remote.c                 |  66 ++++++-
 gdbserver/configure.srv      |   4 +
 gdbserver/linux-low.cc       | 100 +++++++++-
 gdbserver/server.cc          |  34 +++-
 9 files changed, 685 insertions(+), 9 deletions(-)

-- 
2.25.1

Comments

Weimin Pan via Gdb-patches June 1, 2021, 12:08 p.m. | #1
> From: Zied Guermazi <zied.guermazi@trande.de>

> Date: Mon, 31 May 2021 23:33:06 +0200

> Cc: Zied Guermazi <zied.guermazi@trande.de>

> 

> This patch extends the remote protocol to use ARM CoreSight traces for btrace.

> This patch adds the capabilities enumeration as well as the required protocol

> extentions for configuring traces collection, starting and stopping tracing,

> and transferring the traces as well as the parameters needed for decoding them.

> 

> gdb/ChangeLog

> 

> 	* NEWS: list new remote packets for extending btrace

> 	to support using ARM CoreSight Traces.

> 	* btrace.c (check_xml_btrace_version): add version 1.1

> 	(parse_xml_btrace_etm_config_source_config_cpu_etmv4_config): New.

> 	(parse_xml_btrace_etm_config_source_config_cpu_etmv3_config): New.

> 	(parse_xml_btrace_etm_config_source_config_cpu_etm_config): New.

> 	(parse_xml_btrace_etm_config_source_config): New.

> 	(parse_xml_btrace_etm_config_source_config_end): New

> 	(parse_xml_btrace_etm_config_sink_config): New.

> 	(parse_xml_btrace_etm_raw): New.

> 	(parse_xml_btrace_etm): New.

> 	(parse_xml_btrace_conf_etm): New.

> 	* btrace.dtd: add etm data and decoding parameters.

> 	* btrace-conf.dtd: add etm configuration.

> 	* remote.c (remote_target::btrace_sync_conf): add etm configuration.

> 	(remote_target::remote_btrace_maybe_reopen): warn if ETM was disabled.

> 	(remote_target::enable_btrace): add coresight etm.

> 	(_initialize_remote): add etm related packets.

> 

> gdb/doc/ChangeLog

> 

> 	* gdb.texinfo (General Query Packets) Document extending GDB remote

> 	protocol packets to support using ARM CoreSight traces for btrace.

> 

> gdbserver/ChangeLog

> 

> 	* configure.srv: add btrace for aarch64*-*-linux* and arm*-*-linux*.

> 	* linux-low.c (linux_low_encode_etm_config): New.

> 	(linux_process_target::read_btrace): encode CoreSight traces

> 	and related decoding parameters.

> 	(linux_process_target::read_btrace_conf): encode CoreSight

> 	configuration.

> 	* server.cc (handle_btrace_enable_etm): New.

> 	(handle_btrace_general_set): add etm handling.

> 	(handle_btrace_conf_general_set): add etm handling.

> 	(supported_btrace_packets): add etm related packets.


Likewise here: if there were no changes since v5 in the documentation
parts, I believe I already approved that.

Thanks.
Weimin Pan via Gdb-patches June 23, 2021, 10:59 a.m. | #2
Hello Zied,

>+/* Parse a btrace etm "cpu-etm-config-etmv4_config" xml record.  */

>+

>+static void

>+parse_xml_btrace_etm_config_source_config_cpu_etmv4_config (


Isn't the element just called "etmv4-config"?


>+				struct gdb_xml_parser *parser,

>+				const struct gdb_xml_element *element,

>+				void *user_data,

>+				std::vector<gdb_xml_value> &attributes)

>+{

>+  struct btrace_data *btrace;

>+  cs_etm_trace_params *etm_trace_params;


No forward declarations; etm_trace_params can become a reference.

>+

>+  DEBUG ("parse_xml_btrace_etm_config_source_config_cpu_etmv4_config");

>+

>+  btrace = (struct btrace_data *) user_data;

>+  etm_trace_params = & (btrace->variant.etm.config.etm_trace_params->back

>());

>+

>+  struct gdb_xml_value *reg_idr0;

>+  reg_idr0

>+    = xml_find_attribute (attributes, "reg_idr0");


Please combine declaration and initialization.

>+

>+  struct gdb_xml_value *reg_idr1;

>+  reg_idr1

>+    = xml_find_attribute (attributes, "reg_idr1");

>+

>+  struct gdb_xml_value *reg_idr2;

>+  reg_idr2

>+    = xml_find_attribute (attributes, "reg_idr2");

>+

>+  struct gdb_xml_value *reg_idr8;

>+  reg_idr8

>+    = xml_find_attribute (attributes, "reg_idr8");

>+

>+  struct gdb_xml_value *reg_configr;

>+  reg_configr

>+    = xml_find_attribute (attributes, "reg_configr");

>+

>+  struct gdb_xml_value *reg_traceidr;

>+  reg_traceidr

>+    = xml_find_attribute (attributes, "reg_traceidr");

>+

>+  etm_trace_params->etmv4.reg_idr0

>+    = (unsigned int) *(ULONGEST *) reg_idr0->value.get ();


We should check those pointers before dereferencing.  If the XML
doesn't contain that attribute, the pointer would be nullptr.


>+/* Parse a btrace etm "cpu-etm-config" xml record.  */

>+

>+static void

>+parse_xml_btrace_etm_config_source_config_cpu_etm_config (


How would we arrive at that function name?  Wouldn't it simply be

parse_xml_btrace_cpu_etm_config?

More below.

>+  trace_id = xml_find_attribute (attributes, "trace_id");

>+  if (trace_id != NULL)

>+    btrace->variant.etm.trace_id

>+      = (uint8_t) *(ULONGEST *) trace_id->value.get ();


Shouldn't this be an error?


>+static const struct gdb_xml_attribute

>+btrace_etm_config_source_config_cpu_config_etmv3_config_attributes[] = {

>+  { "reg_ctrl", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },


NULL is spelled nullptr in new code.


>+  struct gdb_xml_value *sink;

>+  sink = xml_find_attribute (attributes, "sink");

>+  if (sink != nullptr)

>+    conf->etm.sink = (char*) sink->value.get ();


I don't think this memory is alive after parsing the XML.


>+static const struct gdb_xml_attribute btrace_conf_etm_attributes[] = {

>+  { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },

>+  { "sink", GDB_XML_AF_OPTIONAL, NULL, NULL },

>+  { NULL, GDB_XML_AF_NONE, NULL, NULL }

>+};


Shouldn't there be entries for filter and config, too?


> These are the currently defined stub features, in more detail:

>@@ -42248,9 +42262,11 @@ The remote stub understands the

>@samp{Qbtrace:off} packet.

>

> @item Qbtrace:bts

> The remote stub understands the @samp{Qbtrace:bts} packet.

>+(@pxref{bts}).

>

> @item Qbtrace:pt

> The remote stub understands the @samp{Qbtrace:pt} packet.

>+(@pxref{pt}).

>

> @item Qbtrace-conf:bts:size

> The remote stub understands the @samp{Qbtrace-conf:bts:size} packet.

>@@ -42286,7 +42302,6 @@ The remote stub understands the

>@samp{QThreadEvents} packet.

> @item no-resumed

> The remote stub reports the @samp{N} stop reply.

>

>-

> @item memory-tagging

> The remote stub supports and implements the required memory tagging

> functionality and understands the @samp{qMemTags} (@pxref{qMemTags}) and

>@@ -42296,6 +42311,15 @@ For AArch64 GNU/Linux systems, this feature also

>requires access to the

> @file{/proc/@var{pid}/smaps} file so memory mapping page flags can be

>inspected.

> This is done via the @samp{vFile} requests.

>

>+@item Qbtrace:etm

>+The remote stub understands the @samp{Qbtrace:etm} packet.

>+(@pxref{etm}).


Shouldn't this command be documented together with Qbtrace:bts and
Qbtrace:pt above?


>+

>+@item Qbtrace-conf:etm:size

>+The remote stub understands the @samp{Qbtrace-conf:etm:size} packet.

>+

>+@item Qbtrace-conf:etm:sink

>+The remote stub understands the @samp{Qbtrace-conf:etm:sink} packet.

> @end table


Same here.  They would go to the Qbtrace-conf:... block.

This comment is for all new packets that are really extensions of existing
btrace packets for a new format.


>diff --git a/gdb/features/btrace-conf.dtd b/gdb/features/btrace-conf.dtd

>index 4b060bb408c..7334d035f34 100644

>--- a/gdb/features/btrace-conf.dtd

>+++ b/gdb/features/btrace-conf.dtd

>@@ -4,11 +4,17 @@

>      are permitted in any medium without royalty provided the copyright

>      notice and this notice are preserved.  -->

>

>-<!ELEMENT btrace-conf	(bts?, pt?)>

>-<!ATTLIST btrace-conf	version	CDATA	#FIXED "1.0">

>+<!ELEMENT btrace-conf	(bts?, pt?, etm?)>

>+<!ATTLIST btrace-conf	version	(1.0|1.1) #REQUIRED>


I don't know how this would be handled.  We'd need someone else to
review this part.


>diff --git a/gdb/remote.c b/gdb/remote.c

>index 9b465d77343..2defaa4503e 100644

>--- a/gdb/remote.c

>+++ b/gdb/remote.c

>@@ -2184,6 +2184,15 @@ enum {

>      packets and the tag violation stop replies.  */

>   PACKET_memory_tagging_feature,

>

>+  /* Support for the Qbtrace-etm packet.  */

>+  PACKET_Qbtrace_etm,

>+

>+  /* Support for the Qbtrace-conf:etm:size packet.  */

>+  PACKET_Qbtrace_conf_etm_size,

>+

>+  /* Support for the Qbtrace-conf:etm:sink packet.  */

>+  PACKET_Qbtrace_conf_etm_sink,


Packets for filter and config are missing.


>+static void

>+linux_low_encode_etm_config (struct buffer *buffer,

>+			    const struct btrace_data_etm_config *config)

>+{

>+  int architecture;

>+  buffer_grow_str (buffer, "<etm-config>\n");

>+  buffer_grow_str (buffer, "<source-config>\n");

>+  for (int i=0; i< config->cpu_count;i++)

>+  {

>+    if ((config->etm_trace_params->at (i).protocol == OCSD_PROTOCOL_ETMV3)

>+	||(config->etm_trace_params->at (i).protocol == OCSD_PROTOCOL_PTM))


space before (

>+      {

>+	architecture = ARCH_V7;

>+      }


No {} for a single statement.

>+    else if (config->etm_trace_params->at (i).protocol ==

>OCSD_PROTOCOL_ETMV4I)

>+      {

>+	architecture = ARCH_V8;

>+      }

>+    else

>+      {

>+	architecture = ARCH_UNKNOWN;

>+      }

>+

>+    buffer_xml_printf (buffer,"<cpu-etm-config arch_ver=\"0x%x\" "

>+	"core_prof=\"0x%x\" cpu_id=\"0x%x\" protocol=\"0x%x\">\n",


Indentation of the split string is off.  More below.

>+			  architecture, profile_CortexA,

>+			  i, config->etm_trace_params->at (i).protocol);

>+    if (architecture == ARCH_V7)

>+    {

>+      buffer_xml_printf (buffer,

>+	"<etmv3-config reg_idr=\"0x%x\" reg_ctrl=\"0x%x\" "

>+	"reg_ccer=\"0x%x\" reg_trc_id=\"0x%x\"/>\n",

>+			  config->etm_trace_params->at (i).etmv3.reg_idr,

>+			  config->etm_trace_params->at (i).etmv3.reg_ctrl,

>+			  config->etm_trace_params->at (i).etmv3.reg_ccer,

>+			  config->etm_trace_params->at (i).etmv3.reg_trc_id);


It might help to stick to the order used in the corresponding schema.


>@@ -7077,7 +7161,7 @@ linux_process_target::read_btrace_conf (const

>btrace_target_info *tinfo,

>   const struct btrace_config *conf;

>

>   buffer_grow_str (buffer, "<!DOCTYPE btrace-conf SYSTEM \"btrace-

>conf.dtd\">\n");

>-  buffer_grow_str (buffer, "<btrace-conf version=\"1.0\">\n");

>+  buffer_grow_str (buffer, "<btrace-conf version=\"1.1\">\n");


We only need 1.1 for the new features.

If we built a new gdbserver from this it wouldn't be able to use PT or BTS
with an old GDB, although we didn't change anything, there.


>   conf = linux_btrace_conf (tinfo);

>   if (conf != NULL)

>@@ -7098,6 +7182,20 @@ linux_process_target::read_btrace_conf (const

>btrace_target_info *tinfo,

> 	  buffer_xml_printf (buffer, " size=\"0x%x\"", conf->pt.size);

> 	  buffer_xml_printf (buffer, "/>\n");

> 	  break;

>+

>+	case BTRACE_FORMAT_ETM:

>+	  buffer_xml_printf (buffer, "<etm");

>+	  buffer_xml_printf (buffer, " size=\"0x%x\"", conf->etm.size);

>+	  if (conf->etm.sink !=NULL)


spaces around != and nullptr instead of NULL

>+	    {

>+	      buffer_xml_printf (buffer, " sink=\"%s\"", conf->etm.sink);

>+	    }

>+	  else

>+	    {

>+	      buffer_xml_printf (buffer, " sink=\"default\"");


We treat nullptr as default above, yet we pass "default", here.  The parser
just installs that string.

Where do we handle the "default" sink?


Regards,
Markus.

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 02035901f6e..ad93def71af 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -209,6 +209,17 @@  ptype[/FLAGS] TYPE | EXPRESSION
 ARM Symbian			arm*-*-symbianelf*
 
 * New remote packets
+Qbtrace:etm
+  Enable ARM CoreSight ETM Trace-based branch tracing for the current
+  process.  The remote stub reports support for this packet to GDB's
+  qSupported query.
+
+Qbtrace-conf:etm:size
+  Set the requested ring buffer size for branch tracing in
+  ARM CoreSight ETM Trace format.
+
+Qbtrace-conf:etm:sink
+  Set the trace sink for branch tracing in ARM CoreSight ETM Trace format.
 
 qMemTags
   Request the remote to send allocation tags for a particular memory range.
diff --git a/gdb/btrace.c b/gdb/btrace.c
index 2676389b63e..c10da71b7cd 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -2631,7 +2631,7 @@  check_xml_btrace_version (struct gdb_xml_parser *parser,
   const char *version
     = (const char *) xml_find_attribute (attributes, "version")->value.get ();
 
-  if (strcmp (version, "1.0") != 0)
+  if ((strcmp (version, "1.0") != 0) && (strcmp (version, "1.1") != 0))
     gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
 }
 
@@ -2766,6 +2766,253 @@  parse_xml_btrace_pt (struct gdb_xml_parser *parser,
   btrace->variant.pt.size = 0;
 }
 
+/* Parse a btrace etm "cpu-etm-config-etmv4_config" xml record.  */
+
+static void
+parse_xml_btrace_etm_config_source_config_cpu_etmv4_config (
+				struct gdb_xml_parser *parser,
+				const struct gdb_xml_element *element,
+				void *user_data,
+				std::vector<gdb_xml_value> &attributes)
+{
+  struct btrace_data *btrace;
+  cs_etm_trace_params *etm_trace_params;
+
+  DEBUG ("parse_xml_btrace_etm_config_source_config_cpu_etmv4_config");
+
+  btrace = (struct btrace_data *) user_data;
+  etm_trace_params = & (btrace->variant.etm.config.etm_trace_params->back ());
+
+  struct gdb_xml_value *reg_idr0;
+  reg_idr0
+    = xml_find_attribute (attributes, "reg_idr0");
+
+  struct gdb_xml_value *reg_idr1;
+  reg_idr1
+    = xml_find_attribute (attributes, "reg_idr1");
+
+  struct gdb_xml_value *reg_idr2;
+  reg_idr2
+    = xml_find_attribute (attributes, "reg_idr2");
+
+  struct gdb_xml_value *reg_idr8;
+  reg_idr8
+    = xml_find_attribute (attributes, "reg_idr8");
+
+  struct gdb_xml_value *reg_configr;
+  reg_configr
+    = xml_find_attribute (attributes, "reg_configr");
+
+  struct gdb_xml_value *reg_traceidr;
+  reg_traceidr
+    = xml_find_attribute (attributes, "reg_traceidr");
+
+  etm_trace_params->etmv4.reg_idr0
+    = (unsigned int) *(ULONGEST *) reg_idr0->value.get ();
+  etm_trace_params->etmv4.reg_idr1
+    = (unsigned int) *(ULONGEST *) reg_idr1->value.get ();
+  etm_trace_params->etmv4.reg_idr2
+    = (unsigned int) *(ULONGEST *) reg_idr2->value.get ();
+  etm_trace_params->etmv4.reg_idr8
+    = (unsigned int) *(ULONGEST *) reg_idr8->value.get ();
+  etm_trace_params->etmv4.reg_configr
+    = (unsigned int) *(ULONGEST *) reg_configr->value.get ();
+  etm_trace_params->etmv4.reg_traceidr
+    = (unsigned int) *(ULONGEST *) reg_traceidr->value.get ();
+
+}
+
+/* Parse a btrace etm "cpu-etm-config-etmv3_config" xml record.  */
+
+static void
+parse_xml_btrace_etm_config_source_config_cpu_etmv3_config (
+				struct gdb_xml_parser *parser,
+				const struct gdb_xml_element *element,
+				void *user_data,
+				std::vector<gdb_xml_value> &attributes)
+{
+  struct btrace_data *btrace;
+  cs_etm_trace_params *etm_trace_params;
+
+  DEBUG ("parse_xml_btrace_etm_config_source_config_cpu_etmv3_config");
+
+  btrace = (struct btrace_data *) user_data;
+  etm_trace_params = & (btrace->variant.etm.config.etm_trace_params->back ());
+
+  struct gdb_xml_value *reg_ctrl;
+  reg_ctrl
+    = xml_find_attribute (attributes, "reg_ctrl");
+
+  struct gdb_xml_value *reg_trc_id;
+  reg_trc_id
+    = xml_find_attribute (attributes, "reg_trc_id");
+
+  struct gdb_xml_value *reg_ccer;
+  reg_ccer
+    = xml_find_attribute (attributes, "reg_ccer");
+
+  struct gdb_xml_value *reg_idr;
+  reg_idr
+    = xml_find_attribute (attributes, "reg_idr");
+
+  etm_trace_params->etmv3.reg_ctrl
+    = (unsigned int) *(ULONGEST *) reg_ctrl->value.get ();
+  etm_trace_params->etmv3.reg_trc_id
+    = (unsigned int) *(ULONGEST *) reg_trc_id->value.get ();
+  etm_trace_params->etmv3.reg_ccer
+    = (unsigned int) *(ULONGEST *) reg_ccer->value.get ();
+  etm_trace_params->etmv3.reg_idr
+    = (unsigned int) *(ULONGEST *) reg_idr->value.get ();
+}
+
+
+/* Parse a btrace etm "cpu-etm-config" xml record.  */
+
+static void
+parse_xml_btrace_etm_config_source_config_cpu_etm_config (
+				struct gdb_xml_parser *parser,
+				const struct gdb_xml_element *element,
+				void *user_data,
+				std::vector<gdb_xml_value> &attributes)
+{
+  struct btrace_data *btrace;
+  cs_etm_trace_params etm_trace_params;
+
+  DEBUG ("parse_xml_btrace_etm_config_source_config_cpu_etm_config");
+
+  btrace = (struct btrace_data *) user_data;
+
+  struct gdb_xml_value *arch_ver;
+  arch_ver
+    = xml_find_attribute (attributes, "arch_ver");
+
+  struct gdb_xml_value *core_prof;
+  core_prof
+    = xml_find_attribute (attributes, "core_prof");
+
+  struct gdb_xml_value *protocol;
+  protocol
+    = xml_find_attribute (attributes, "protocol");
+
+  etm_trace_params.arch_ver = (int) *(ULONGEST *) arch_ver->value.get ();
+  etm_trace_params.core_profile = (int) *(ULONGEST *) core_prof->value.get ();
+  etm_trace_params.protocol = (int) *(ULONGEST *) protocol->value.get ();
+
+  btrace->variant.etm.config.etm_trace_params->push_back (etm_trace_params);
+}
+
+/* Parse a btrace etm "source-config" xml record.  */
+
+static void
+parse_xml_btrace_etm_config_source_config (struct gdb_xml_parser *parser,
+				const struct gdb_xml_element *element,
+				void *user_data,
+				std::vector<gdb_xml_value> &attributes)
+{
+  struct btrace_data *btrace;
+  struct gdb_xml_value *trace_id;
+
+  DEBUG ("parse_xml_btrace_etm_config_source_config");
+  btrace = (struct btrace_data *) user_data;
+
+  trace_id = xml_find_attribute (attributes, "trace_id");
+  if (trace_id != NULL)
+    btrace->variant.etm.trace_id
+      = (uint8_t) *(ULONGEST *) trace_id->value.get ();
+  else
+    btrace->variant.etm.trace_id = 0xFF;
+  btrace->variant.etm.config.etm_trace_params
+    = new std::vector<cs_etm_trace_params>;
+}
+
+/* Get the number of cpus.  */
+
+static void
+parse_xml_btrace_etm_config_source_config_end (struct gdb_xml_parser *parser,
+				const struct gdb_xml_element *element,
+				void *user_data, const char *body_text)
+{
+  struct btrace_data *btrace;
+
+  DEBUG ("parse_xml_btrace_etm_config_source_config_end");
+  btrace = (struct btrace_data *) user_data;
+
+  btrace->variant.etm.config.cpu_count
+    = btrace->variant.etm.config.etm_trace_params->size ();
+}
+
+/* Parse a btrace etm "sink-config" xml record.  */
+
+static void
+parse_xml_btrace_etm_config_sink_config (struct gdb_xml_parser *parser,
+				const struct gdb_xml_element *element,
+				void *user_data,
+				std::vector<gdb_xml_value> &attributes)
+{
+  struct btrace_data *btrace;
+
+  DEBUG ("parse_xml_btrace_etm_config_sink_config");
+
+  btrace = (struct btrace_data *) user_data;
+
+  struct gdb_xml_value *formatted;
+  formatted = xml_find_attribute (attributes, "formatted");
+
+  struct gdb_xml_value *fsyncs;
+  fsyncs = xml_find_attribute (attributes, "fsyncs");
+
+  struct gdb_xml_value *hsyncs;
+  hsyncs = xml_find_attribute (attributes, "hsyncs");
+
+  struct gdb_xml_value *frame_aligned;
+  frame_aligned = xml_find_attribute (attributes, "frame_aligned");
+
+  struct gdb_xml_value *reset_on_4x_sync;
+  reset_on_4x_sync = xml_find_attribute (attributes, "reset_on_4x_sync");
+
+  btrace->variant.etm.config.etm_decoder_params.formatted
+    = (uint8_t) *(ULONGEST *) formatted->value.get ();
+  btrace->variant.etm.config.etm_decoder_params.fsyncs
+    = (uint8_t) *(ULONGEST *) fsyncs->value.get ();
+  btrace->variant.etm.config.etm_decoder_params.hsyncs
+    = (uint8_t) *(ULONGEST *) hsyncs->value.get ();
+  btrace->variant.etm.config.etm_decoder_params.frame_aligned
+    = (uint8_t) *(ULONGEST *) frame_aligned->value.get ();
+  btrace->variant.etm.config.etm_decoder_params.reset_on_4x_sync
+    = (uint8_t) *(ULONGEST *) reset_on_4x_sync->value.get ();
+}
+
+
+/* Parse a btrace etm "raw" xml record.  */
+
+static void
+parse_xml_btrace_etm_raw (struct gdb_xml_parser *parser,
+			  const struct gdb_xml_element *element,
+			  void *user_data, const char *body_text)
+{
+  struct btrace_data *btrace;
+  DEBUG ("parse_xml_btrace_etm_raw");
+  btrace = (struct btrace_data *) user_data;
+  parse_xml_raw (parser, body_text, &btrace->variant.etm.data,
+		 &btrace->variant.etm.size);
+}
+
+/* Parse a btrace "etm" xml record.  */
+
+static void
+parse_xml_btrace_etm (struct gdb_xml_parser *parser,
+		      const struct gdb_xml_element *element,
+		      void *user_data,
+		      std::vector<gdb_xml_value> &attributes)
+{
+  struct btrace_data *btrace;
+  DEBUG ("parse_xml_btrace_etm");
+  btrace = (struct btrace_data *) user_data;
+  btrace->format = BTRACE_FORMAT_ETM;
+  btrace->variant.etm.data = NULL;
+  btrace->variant.etm.size = 0;
+}
+
 static const struct gdb_xml_attribute block_attributes[] = {
   { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
   { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
@@ -2793,6 +3040,91 @@  static const struct gdb_xml_element btrace_pt_children[] = {
   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
 };
 
+static const struct gdb_xml_attribute
+btrace_etm_config_source_config_cpu_config_etmv3_config_attributes[] = {
+  { "reg_ctrl", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reg_trc_id", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reg_ccer", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reg_idr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { NULL, GDB_XML_AF_NONE, NULL, NULL }
+};
+
+static const struct gdb_xml_attribute
+btrace_etm_config_source_config_cpu_config_etmv4_config_attributes[] = {
+  { "reg_idr0", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reg_idr1", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reg_idr2", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reg_idr8", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reg_configr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reg_traceidr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { NULL, GDB_XML_AF_NONE, NULL, NULL }
+};
+
+static const struct gdb_xml_element
+btrace_etm_config_source_config_cpu_etm_config_children[] = {
+  { "etmv3-config",
+      btrace_etm_config_source_config_cpu_config_etmv3_config_attributes,
+      NULL, GDB_XML_EF_OPTIONAL,
+      parse_xml_btrace_etm_config_source_config_cpu_etmv3_config, NULL },
+  { "etmv4-config",
+      btrace_etm_config_source_config_cpu_config_etmv4_config_attributes,
+      NULL, GDB_XML_EF_OPTIONAL,
+      parse_xml_btrace_etm_config_source_config_cpu_etmv4_config, NULL },
+  { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
+};
+
+static const struct gdb_xml_attribute
+btrace_etm_config_source_config_etm_config_attributes[] = {
+  { "cpu_id", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "arch_ver", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "core_prof", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "protocol", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { NULL, GDB_XML_AF_NONE, NULL, NULL }
+};
+
+static const struct gdb_xml_element
+btrace_etm_config_source_config_children[] = {
+  { "cpu-etm-config", btrace_etm_config_source_config_etm_config_attributes,
+      btrace_etm_config_source_config_cpu_etm_config_children,
+      GDB_XML_EF_REPEATABLE,
+      parse_xml_btrace_etm_config_source_config_cpu_etm_config, NULL },
+  { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
+};
+
+static const struct gdb_xml_attribute
+btrace_etm_config_sink_config_attributes[] = {
+  { "formatted", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "fsyncs", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "hsyncs", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "frame_aligned", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "reset_on_4x_sync", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { NULL, GDB_XML_AF_NONE, NULL, NULL }
+};
+
+static const struct gdb_xml_attribute
+btrace_etm_config_source_config_attributes[] = {
+  { "trace_id", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
+  { NULL, GDB_XML_AF_NONE, NULL, NULL }
+};
+
+static const struct gdb_xml_element btrace_etm_config_children[] = {
+  { "source-config", btrace_etm_config_source_config_attributes,
+      btrace_etm_config_source_config_children, GDB_XML_EF_NONE,
+      parse_xml_btrace_etm_config_source_config,
+      parse_xml_btrace_etm_config_source_config_end },
+  { "sink-config", btrace_etm_config_sink_config_attributes, NULL,
+      GDB_XML_EF_NONE, parse_xml_btrace_etm_config_sink_config, NULL },
+  { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
+};
+
+static const struct gdb_xml_element btrace_etm_children[] = {
+  { "etm-config", NULL, btrace_etm_config_children, GDB_XML_EF_NONE, NULL,
+      NULL },
+  { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL,
+      parse_xml_btrace_etm_raw },
+  { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
+};
+
 static const struct gdb_xml_attribute btrace_attributes[] = {
   { "version", GDB_XML_AF_NONE, NULL, NULL },
   { NULL, GDB_XML_AF_NONE, NULL, NULL }
@@ -2803,6 +3135,8 @@  static const struct gdb_xml_element btrace_children[] = {
     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
   { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
     NULL },
+  { "etm", NULL, btrace_etm_children, GDB_XML_EF_OPTIONAL,
+    parse_xml_btrace_etm, NULL },
   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
 };
 
@@ -2883,6 +3217,30 @@  parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
     conf->pt.size = (unsigned int) *(ULONGEST *) size->value.get ();
 }
 
+/* Parse a btrace-conf "etm" xml record.  */
+
+static void
+parse_xml_btrace_conf_etm (struct gdb_xml_parser *parser,
+			   const struct gdb_xml_element *element,
+			   void *user_data,
+			   std::vector<gdb_xml_value> &attributes)
+{
+  DEBUG ("parse_xml_btrace_conf_etm");
+
+  struct btrace_config *conf = (struct btrace_config *) user_data;
+  conf->format = BTRACE_FORMAT_ETM;
+  conf->etm.size = 0;
+
+  struct gdb_xml_value *size = xml_find_attribute (attributes, "size");
+  if (size != nullptr)
+    conf->etm.size = (unsigned int) *(ULONGEST *) size->value.get ();
+
+  struct gdb_xml_value *sink;
+  sink = xml_find_attribute (attributes, "sink");
+  if (sink != nullptr)
+    conf->etm.sink = (char*) sink->value.get ();
+}
+
 static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
   { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
   { NULL, GDB_XML_AF_NONE, NULL, NULL }
@@ -2893,11 +3251,19 @@  static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = {
   { NULL, GDB_XML_AF_NONE, NULL, NULL }
 };
 
+static const struct gdb_xml_attribute btrace_conf_etm_attributes[] = {
+  { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
+  { "sink", GDB_XML_AF_OPTIONAL, NULL, NULL },
+  { NULL, GDB_XML_AF_NONE, NULL, NULL }
+};
+
 static const struct gdb_xml_element btrace_conf_children[] = {
   { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
     parse_xml_btrace_conf_bts, NULL },
   { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
     parse_xml_btrace_conf_pt, NULL },
+  { "etm", btrace_conf_etm_attributes, NULL, GDB_XML_EF_OPTIONAL,
+    parse_xml_btrace_conf_etm, NULL },
   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
 };
 
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index b9b09ff97f2..83ddbc28aa5 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -42072,6 +42072,20 @@  These are the currently defined stub features and their properties:
 @tab @samp{-}
 @tab No
 
+@item @samp{Qbtrace:etm}
+@tab Yes
+@tab @samp{-}
+@tab Yes
+
+@item @samp{Qbtrace-conf:etm:size}
+@tab Yes
+@tab @samp{-}
+@tab Yes
+
+@item @samp{Qbtrace-conf:etm:sink}
+@tab Yes
+@tab @samp{-}
+@tab Yes
 @end multitable
 
 These are the currently defined stub features, in more detail:
@@ -42248,9 +42262,11 @@  The remote stub understands the @samp{Qbtrace:off} packet.
 
 @item Qbtrace:bts
 The remote stub understands the @samp{Qbtrace:bts} packet.
+(@pxref{bts}).
 
 @item Qbtrace:pt
 The remote stub understands the @samp{Qbtrace:pt} packet.
+(@pxref{pt}).
 
 @item Qbtrace-conf:bts:size
 The remote stub understands the @samp{Qbtrace-conf:bts:size} packet.
@@ -42286,7 +42302,6 @@  The remote stub understands the @samp{QThreadEvents} packet.
 @item no-resumed
 The remote stub reports the @samp{N} stop reply.
 
-
 @item memory-tagging
 The remote stub supports and implements the required memory tagging
 functionality and understands the @samp{qMemTags} (@pxref{qMemTags}) and
@@ -42296,6 +42311,15 @@  For AArch64 GNU/Linux systems, this feature also requires access to the
 @file{/proc/@var{pid}/smaps} file so memory mapping page flags can be inspected.
 This is done via the @samp{vFile} requests.
 
+@item Qbtrace:etm
+The remote stub understands the @samp{Qbtrace:etm} packet.
+(@pxref{etm}).
+
+@item Qbtrace-conf:etm:size
+The remote stub understands the @samp{Qbtrace-conf:etm:size} packet.
+
+@item Qbtrace-conf:etm:sink
+The remote stub understands the @samp{Qbtrace-conf:etm:sink} packet.
 @end table
 
 @item qSymbol::
@@ -42702,6 +42726,7 @@  A badly formed request or an error was encountered.
 @end table
 
 @item Qbtrace:bts
+@anchor{bts}
 Enable branch tracing for the current thread using Branch Trace Store.
 
 Reply:
@@ -42713,6 +42738,7 @@  A badly formed request or an error was encountered.
 @end table
 
 @item Qbtrace:pt
+@anchor{pt}
 Enable branch tracing for the current thread using Intel Processor Trace.
 
 Reply:
@@ -42758,6 +42784,41 @@  The ring buffer size has been set.
 A badly formed request or an error was encountered.
 @end table
 
+@item Qbtrace:etm
+@anchor{etm}
+Enable branch tracing for the current thread using ARM CoreSight ETM Trace.
+
+Reply:
+@table @samp
+@item OK
+Branch tracing has been enabled.
+@item E.errtext
+A badly formed request or an error was encountered.
+@end table
+
+@item Qbtrace-conf:etm:size=@var{value}
+Set the requested ring buffer size for new threads that use the
+btrace recording method in etm format.
+
+Reply:
+@table @samp
+@item OK
+The ring buffer size has been set.
+@item E.errtext
+A badly formed request or an error was encountered.
+@end table
+
+@item Qbtrace-conf:etm:sink=@var{value}
+Set the requested trace sink for new threads that use the
+btrace recording method in etm format.
+
+Reply:
+@table @samp
+@item OK
+The ring buffer size has been set.
+@item E.errtext
+A badly formed request or an error was encountered.
+@end table
 @end table
 
 @node Architecture-Specific Protocol Details
diff --git a/gdb/features/btrace-conf.dtd b/gdb/features/btrace-conf.dtd
index 4b060bb408c..7334d035f34 100644
--- a/gdb/features/btrace-conf.dtd
+++ b/gdb/features/btrace-conf.dtd
@@ -4,11 +4,17 @@ 
      are permitted in any medium without royalty provided the copyright
      notice and this notice are preserved.  -->
 
-<!ELEMENT btrace-conf	(bts?, pt?)>
-<!ATTLIST btrace-conf	version	CDATA	#FIXED "1.0">
+<!ELEMENT btrace-conf	(bts?, pt?, etm?)>
+<!ATTLIST btrace-conf	version	(1.0|1.1) #REQUIRED>
 
 <!ELEMENT bts	EMPTY>
 <!ATTLIST bts	size	CDATA	#IMPLIED>
 
 <!ELEMENT pt	EMPTY>
 <!ATTLIST pt	size	CDATA	#IMPLIED>
+
+<!ELEMENT etm	EMPTY>
+<!ATTLIST etm   size    CDATA   #IMPLIED
+                sink	CDATA   #IMPLIED
+                filter  CDATA   #IMPLIED
+                config  CDATA   #IMPLIED>
diff --git a/gdb/features/btrace.dtd b/gdb/features/btrace.dtd
index 9bb930d7d3b..001b77f35d0 100644
--- a/gdb/features/btrace.dtd
+++ b/gdb/features/btrace.dtd
@@ -4,8 +4,8 @@ 
      are permitted in any medium without royalty provided the copyright
      notice and this notice are preserved.  -->
 
-<!ELEMENT btrace  (block* | pt)>
-<!ATTLIST btrace  version CDATA   #FIXED "1.0">
+<!ELEMENT btrace  (block* | pt | etm)>
+<!ATTLIST btrace  version (1.0|1.1) #REQUIRED>
 
 <!ELEMENT block        EMPTY>
 <!ATTLIST block        begin  CDATA   #REQUIRED
@@ -21,4 +21,38 @@ 
               model    CDATA #REQUIRED
               stepping CDATA #REQUIRED>
 
+<!ELEMENT etm (etm-config?, raw?)>
+
+<!ELEMENT etm-config (source-config, sink-config)>
+
+<!ELEMENT source-config   (cpu-etm-config+)>
+<!ATTLIST source-config    trace_id         CDATA #IMPLIED>
+
+<!ELEMENT cpu-etm-config  (etmv3-config|etmv4-config)>
+<!ATTLIST cpu-etm-config   cpu_id           CDATA #REQUIRED
+                           arch_ver         CDATA #REQUIRED
+                           core_prof        CDATA #REQUIRED
+                           protocol         CDATA #REQUIRED>
+
+<!ELEMENT etmv3-config     EMPTY>
+<!ATTLIST etmv3-config     reg_ctrl         CDATA #REQUIRED
+                           reg_trc_id       CDATA #REQUIRED
+                           reg_ccer         CDATA #REQUIRED
+                           reg_idr          CDATA #REQUIRED>
+
+<!ELEMENT etmv4-config     EMPTY>
+<!ATTLIST etmv4-config     reg_idr0         CDATA #REQUIRED
+                           reg_idr1         CDATA #REQUIRED
+                           reg_idr2         CDATA #REQUIRED
+                           reg_idr8         CDATA #REQUIRED
+                           reg_configr      CDATA #REQUIRED
+                           reg_traceidr     CDATA #REQUIRED>
+
+<!ELEMENT sink-config      EMPTY>
+<!ATTLIST sink-config      formatted         CDATA #REQUIRED
+                           fsyncs            CDATA #REQUIRED
+                           hsyncs            CDATA #REQUIRED
+                           frame_aligned     CDATA #REQUIRED
+                           reset_on_4x_sync  CDATA #REQUIRED>
+
 <!ELEMENT raw (#PCDATA)>
diff --git a/gdb/remote.c b/gdb/remote.c
index 9b465d77343..2defaa4503e 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -2184,6 +2184,15 @@  enum {
      packets and the tag violation stop replies.  */
   PACKET_memory_tagging_feature,
 
+  /* Support for the Qbtrace-etm packet.  */
+  PACKET_Qbtrace_etm,
+
+  /* Support for the Qbtrace-conf:etm:size packet.  */
+  PACKET_Qbtrace_conf_etm_size,
+
+  /* Support for the Qbtrace-conf:etm:sink packet.  */
+  PACKET_Qbtrace_conf_etm_sink,
+
   PACKET_MAX
 };
 
@@ -5333,6 +5342,12 @@  static const struct protocol_feature remote_protocol_features[] = {
   { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed },
   { "memory-tagging", PACKET_DISABLE, remote_supported_packet,
     PACKET_memory_tagging_feature },
+  { "Qbtrace:etm", PACKET_DISABLE, remote_supported_packet,
+    PACKET_Qbtrace_etm },
+  { "Qbtrace-conf:etm:size", PACKET_DISABLE, remote_supported_packet,
+    PACKET_Qbtrace_conf_etm_size },
+  { "Qbtrace-conf:etm:sink", PACKET_DISABLE, remote_supported_packet,
+    PACKET_Qbtrace_conf_etm_sink },
 };
 
 static char *remote_support_xml;
@@ -14020,6 +14035,28 @@  remote_target::btrace_sync_conf (const btrace_config *conf)
 
       rs->btrace_config.pt.size = conf->pt.size;
     }
+
+  packet = &remote_protocol_packets[PACKET_Qbtrace_conf_etm_size];
+  if (packet_config_support (packet) == PACKET_ENABLE
+      && conf->etm.size != rs->btrace_config.etm.size)
+    {
+      pos = buf;
+      pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
+                        conf->etm.size);
+
+      putpkt (buf);
+      getpkt (&rs->buf, 0);
+
+      if (packet_ok (buf, packet) == PACKET_ERROR)
+	{
+	  if (buf[0] == 'E' && buf[1] == '.')
+	    error (_("Failed to configure the trace buffer size: %s"), buf + 2);
+	  else
+	    error (_("Failed to configure the trace buffer size."));
+	}
+
+      rs->btrace_config.etm.size = conf->etm.size;
+    }
 }
 
 /* Read the current thread's btrace configuration from the target and
@@ -14042,7 +14079,7 @@  remote_target::remote_btrace_maybe_reopen ()
 {
   struct remote_state *rs = get_remote_state ();
   int btrace_target_pushed = 0;
-#if !defined (HAVE_LIBIPT)
+#if !defined (HAVE_LIBIPT) || !defined (HAVE_LIBOPENCSD_C_API)
   int warned = 0;
 #endif
 
@@ -14077,6 +14114,20 @@  remote_target::remote_btrace_maybe_reopen ()
 	}
 #endif /* !defined (HAVE_LIBIPT) */
 
+#if !defined (HAVE_LIBOPENCSD_C_API)
+      if (rs->btrace_config.format == BTRACE_FORMAT_ETM)
+	{
+	  if (!warned)
+	    {
+	      warned = 1;
+	      warning (_("Target is recording using ARM CoreSight Processor "
+			 "Trace but support was disabled at compile time."));
+	    }
+
+	  continue;
+	}
+#endif /* !defined (HAVE_LIBOPENCSD_C_API) */
+
       /* Push target, once, but before anything else happens.  This way our
 	 changes to the threads will be cleaned up by unpushing the target
 	 in case btrace_read_config () throws.  */
@@ -14114,6 +14165,10 @@  remote_target::enable_btrace (ptid_t ptid, const struct btrace_config *conf)
       case BTRACE_FORMAT_PT:
 	packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
 	break;
+
+      case BTRACE_FORMAT_ETM:
+	packet = &remote_protocol_packets[PACKET_Qbtrace_etm];
+	break;
     }
 
   if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
@@ -15222,6 +15277,15 @@  Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_memory_tagging_feature],
 			 "memory-tagging-feature", "memory-tagging-feature", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_etm],
+			 "Qbtrace:etm", "enable-btrace-etm", 0);
+
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_etm_size],
+			 "Qbtrace-conf:etm:size", "btrace-conf-etm-size", 0);
+
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_etm_sink],
+			 "Qbtrace-conf:etm:sink", "btrace-conf-etm-sink", 0);
+
   /* Assert that we've registered "set remote foo-packet" commands
      for all packet configs.  */
   {
diff --git a/gdbserver/configure.srv b/gdbserver/configure.srv
index 971f537bffa..0c3a33a6975 100644
--- a/gdbserver/configure.srv
+++ b/gdbserver/configure.srv
@@ -51,12 +51,14 @@  case "${gdbserver_host}" in
 			srv_tgtobj="$srv_tgtobj linux-aarch64-tdesc.o"
 			srv_tgtobj="$srv_tgtobj nat/aarch64-mte-linux-ptrace.o"
 			srv_tgtobj="$srv_tgtobj nat/aarch64-sve-linux-ptrace.o"
+			srv_tgtobj="${srv_tgtobj} nat/linux-btrace.o"
 			srv_tgtobj="${srv_tgtobj} $srv_linux_obj"
 			srv_linux_regsets=yes
 			srv_linux_thread_db=yes
 			ipa_obj="linux-aarch64-ipa.o"
 			ipa_obj="${ipa_obj} linux-aarch64-tdesc-ipa.o"
 			ipa_obj="${ipa_obj} arch/aarch64-ipa.o"
+			srv_linux_btrace=yes
 			;;
   aarch64*-*-netbsd*)	srv_regobj=""
 			srv_tgtobj="netbsd-low.o netbsd-aarch64-low.o fork-child.o"
@@ -79,6 +81,7 @@  case "${gdbserver_host}" in
 			srv_tgtobj="$srv_tgtobj linux-arm-tdesc.o"
 			srv_tgtobj="$srv_tgtobj linux-aarch32-low.o"
 			srv_tgtobj="$srv_tgtobj linux-aarch32-tdesc.o"
+			srv_tgtobj="${srv_tgtobj} nat/linux-btrace.o"
 			srv_tgtobj="${srv_tgtobj} arch/aarch32.o"
 			srv_tgtobj="${srv_tgtobj} arch/arm.o"
 			srv_tgtobj="${srv_tgtobj} arch/arm-linux.o"
@@ -86,6 +89,7 @@  case "${gdbserver_host}" in
 			srv_linux_usrregs=yes
 			srv_linux_regsets=yes
 			srv_linux_thread_db=yes
+			srv_linux_btrace=yes
 			;;
   i[34567]86-*-cygwin*)	srv_regobj=""
 			srv_tgtobj="x86-low.o nat/x86-dregs.o win32-low.o"
diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc
index 5c6191d941c..6c4989bdb2c 100644
--- a/gdbserver/linux-low.cc
+++ b/gdbserver/linux-low.cc
@@ -51,6 +51,9 @@ 
 #include "gdbsupport/environ.h"
 #include "gdbsupport/gdb-sigmask.h"
 #include "gdbsupport/scoped_restore.h"
+#if defined (HAVE_LIBOPENCSD_C_API)
+#  include <opencsd/ocsd_if_types.h>
+#endif
 #ifndef ELFMAG0
 /* Don't include <linux/elf.h> here.  If it got included by gdb_proc_service.h
    then ELFMAG0 will have been defined.  If it didn't get included by
@@ -6983,6 +6986,73 @@  linux_low_encode_pt_config (struct buffer *buffer,
   buffer_grow_str (buffer, "</pt-config>\n");
 }
 
+/* Encode ARM CoreSight Processor Trace configuration.  */
+
+static void
+linux_low_encode_etm_config (struct buffer *buffer,
+			    const struct btrace_data_etm_config *config)
+{
+  int architecture;
+  buffer_grow_str (buffer, "<etm-config>\n");
+  buffer_grow_str (buffer, "<source-config>\n");
+  for (int i=0; i< config->cpu_count;i++)
+  {
+    if ((config->etm_trace_params->at (i).protocol == OCSD_PROTOCOL_ETMV3)
+	||(config->etm_trace_params->at (i).protocol == OCSD_PROTOCOL_PTM))
+      {
+	architecture = ARCH_V7;
+      }
+    else if (config->etm_trace_params->at (i).protocol == OCSD_PROTOCOL_ETMV4I)
+      {
+	architecture = ARCH_V8;
+      }
+    else
+      {
+	architecture = ARCH_UNKNOWN;
+      }
+
+    buffer_xml_printf (buffer,"<cpu-etm-config arch_ver=\"0x%x\" "
+	"core_prof=\"0x%x\" cpu_id=\"0x%x\" protocol=\"0x%x\">\n",
+			  architecture, profile_CortexA,
+			  i, config->etm_trace_params->at (i).protocol);
+    if (architecture == ARCH_V7)
+    {
+      buffer_xml_printf (buffer,
+	"<etmv3-config reg_idr=\"0x%x\" reg_ctrl=\"0x%x\" "
+	"reg_ccer=\"0x%x\" reg_trc_id=\"0x%x\"/>\n",
+			  config->etm_trace_params->at (i).etmv3.reg_idr,
+			  config->etm_trace_params->at (i).etmv3.reg_ctrl,
+			  config->etm_trace_params->at (i).etmv3.reg_ccer,
+			  config->etm_trace_params->at (i).etmv3.reg_trc_id);
+    }
+    if (architecture == ARCH_V8)
+    {
+      buffer_xml_printf (buffer,
+	"<etmv4-config reg_idr0=\"0x%x\" reg_idr1=\"0x%x\" "
+	"reg_idr2=\"0x%x\" reg_idr8=\"0x%x\" "
+			  "reg_configr=\"0x%x\" reg_traceidr=\"0x%x\"/>\n",
+			  config->etm_trace_params->at (i).etmv4.reg_idr0,
+			  config->etm_trace_params->at (i).etmv4.reg_idr1,
+			  config->etm_trace_params->at (i).etmv4.reg_idr2,
+			  config->etm_trace_params->at (i).etmv4.reg_idr8,
+			  config->etm_trace_params->at (i).etmv4.reg_configr,
+			  config->etm_trace_params->at (i).etmv4.reg_traceidr);
+    }
+    buffer_xml_printf (buffer,"</cpu-etm-config>\n");
+  }
+  buffer_grow_str (buffer,"</source-config>\n");
+  buffer_xml_printf (buffer,
+    "<sink-config  formatted=\"0x%x\" "
+		"fsyncs=\"0x%x\" hsyncs=\"0x%x\" "
+		"frame_aligned=\"0x%x\" reset_on_4x_sync=\"0x%x\"/>\n",
+		config->etm_decoder_params.formatted,
+		config->etm_decoder_params.fsyncs,
+		config->etm_decoder_params.hsyncs,
+		config->etm_decoder_params.frame_aligned,
+		config->etm_decoder_params.reset_on_4x_sync);
+  buffer_grow_str (buffer,"</etm-config>\n");
+}
+
 /* Encode a raw buffer.  */
 
 static void
@@ -7060,6 +7130,20 @@  linux_process_target::read_btrace (btrace_target_info *tinfo,
       buffer_grow_str0 (buffer, "</btrace>\n");
       break;
 
+    case BTRACE_FORMAT_ETM:
+      buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
+      buffer_grow_str (buffer, "<btrace version=\"1.1\">\n");
+      buffer_grow_str (buffer, "<etm>\n");
+
+      linux_low_encode_etm_config (buffer, &btrace.variant.etm.config);
+
+      linux_low_encode_raw (buffer, btrace.variant.etm.data,
+			    btrace.variant.etm.size);
+
+      buffer_grow_str (buffer, "</etm>\n");
+      buffer_grow_str0 (buffer, "</btrace>\n");
+      break;
+
     default:
       buffer_grow_str0 (buffer, "E.Unsupported Trace Format.");
       return -1;
@@ -7077,7 +7161,7 @@  linux_process_target::read_btrace_conf (const btrace_target_info *tinfo,
   const struct btrace_config *conf;
 
   buffer_grow_str (buffer, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
-  buffer_grow_str (buffer, "<btrace-conf version=\"1.0\">\n");
+  buffer_grow_str (buffer, "<btrace-conf version=\"1.1\">\n");
 
   conf = linux_btrace_conf (tinfo);
   if (conf != NULL)
@@ -7098,6 +7182,20 @@  linux_process_target::read_btrace_conf (const btrace_target_info *tinfo,
 	  buffer_xml_printf (buffer, " size=\"0x%x\"", conf->pt.size);
 	  buffer_xml_printf (buffer, "/>\n");
 	  break;
+
+	case BTRACE_FORMAT_ETM:
+	  buffer_xml_printf (buffer, "<etm");
+	  buffer_xml_printf (buffer, " size=\"0x%x\"", conf->etm.size);
+	  if (conf->etm.sink !=NULL)
+	    {
+	      buffer_xml_printf (buffer, " sink=\"%s\"", conf->etm.sink);
+	    }
+	  else
+	    {
+	      buffer_xml_printf (buffer, " sink=\"default\"");
+	    }
+	  buffer_xml_printf (buffer, "/>\n");
+	  break;
 	}
     }
 
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 32dcc05924e..278978421bc 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -424,6 +424,18 @@  handle_btrace_enable_pt (struct thread_info *thread)
   thread->btrace = target_enable_btrace (thread->id, &current_btrace_conf);
 }
 
+/* Handle btrace enabling in ARM CoreSight Trace format.  */
+
+static void
+handle_btrace_enable_etm (struct thread_info *thread)
+{
+  if (thread->btrace != NULL)
+    error (_("Btrace already enabled."));
+
+  current_btrace_conf.format = BTRACE_FORMAT_ETM;
+  thread->btrace = target_enable_btrace (thread->id, &current_btrace_conf);
+}
+
 /* Handle btrace disabling.  */
 
 static void
@@ -473,10 +485,12 @@  handle_btrace_general_set (char *own_buf)
 	handle_btrace_enable_bts (thread);
       else if (strcmp (op, "pt") == 0)
 	handle_btrace_enable_pt (thread);
+      else if (strcmp (op, "etm") == 0)
+	handle_btrace_enable_etm (thread);
       else if (strcmp (op, "off") == 0)
 	handle_btrace_disable (thread);
       else
-	error (_("Bad Qbtrace operation.  Use bts, pt, or off."));
+	error (_("Bad Qbtrace operation.  Use bts, pt, etm, or off."));
 
       write_ok (own_buf);
     }
@@ -546,6 +560,21 @@  handle_btrace_conf_general_set (char *own_buf)
 
       current_btrace_conf.pt.size = (unsigned int) size;
     }
+  else if (strncmp (op, "etm:size=", strlen ("etm:size=")) == 0)
+    {
+      unsigned long size;
+      char *endp = NULL;
+
+      errno = 0;
+      size = strtoul (op + strlen ("etm:size="), &endp, 16);
+      if (endp == NULL || *endp != 0 || errno != 0 || size > UINT_MAX)
+	{
+	  strcpy (own_buf, "E.Bad size value.");
+	  return -1;
+	}
+
+      current_btrace_conf.etm.size = (unsigned int) size;
+    }
   else
     {
       strcpy (own_buf, "E.Bad Qbtrace configuration option.");
@@ -2189,6 +2218,9 @@  supported_btrace_packets (char *buf)
   strcat (buf, ";Qbtrace-conf:bts:size+");
   strcat (buf, ";Qbtrace:pt+");
   strcat (buf, ";Qbtrace-conf:pt:size+");
+  strcat (buf, ";Qbtrace:etm+");
+  strcat (buf, ";Qbtrace-conf:etm:size+");
+  strcat (buf, ";Qbtrace-conf:etm:sink+");
   strcat (buf, ";Qbtrace:off+");
   strcat (buf, ";qXfer:btrace:read+");
   strcat (buf, ";qXfer:btrace-conf:read+");