btrace: Enable event tracing on Linux for Intel PT.

Event tracing allows GDB to show information about interesting asynchronous
events when tracing with Intel PT.  Subsequent patches will add support for
displaying each type of event.

Enabling event-tracing unconditionally would result in rather noisy output, as
breakpoints themselves result in interrupt events.  Which is why this patch adds
a set/show command to allow the user to enable/disable event-tracing before
starting a recording. The event-tracing setting has no effect on an already
active recording.  The default setting is off.   As event tracing will use the
auxiliary infrastructure added by ptwrite, the user can still disable printing
events, even when event-tracing was enabled, by using the /a switch for the
record instruction-history/function-call-history commands.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Markus Metzger <markus.t.metzger@intel.com>
This commit is contained in:
Felix Willgerodt 2023-06-28 10:15:04 +02:00
parent 48bc2f1c65
commit 13b3a89bc2
9 changed files with 165 additions and 6 deletions

View File

@ -3,6 +3,11 @@
*** Changes since GDB 15
* GDB now supports printing of asynchronous events from the Intel Processor
Trace during 'record instruction-history', 'record function-call-history'
and all stepping commands. This can be controlled with the new
"set record btrace pt event-tracing" command.
* GDB now supports printing of ptwrite payloads from the Intel Processor
Trace during 'record instruction-history', 'record function-call-history'
and all stepping commands. The payload is also accessible in Python as a

View File

@ -8116,6 +8116,16 @@ also need longer to process the branch trace data before it can be used.
Show the current setting of the requested ring buffer size for branch
tracing in Intel Processor Trace format.
@item set record btrace pt event-tracing
Enable or disable event tracing for branch tracing in Intel Processor
Trace format. When enabled, events are recorded during execution as
auxiliary information and will be printed during stepping commands and
commands displaying the execution history. Changing this setting has no
effect on an active recording. The default is off.
@item show record btrace pt event-tracing
Show the current setting of Intel Processor Trace event tracing.
@kindex info record
@item info record
Show various statistics about the recording depending on the recording
@ -45007,6 +45017,11 @@ These are the currently defined stub features and their properties:
@tab @samp{-}
@tab Yes
@item @samp{Qbtrace-conf:pt:event-tracing}
@tab Yes
@tab @samp{-}
@tab Yes
@item @samp{QNonStop}
@tab No
@tab @samp{-}
@ -45331,6 +45346,9 @@ The remote stub understands the @samp{Qbtrace-conf:pt:size} packet.
@item Qbtrace-conf:pt:ptwrite
The remote stub understands the @samp{Qbtrace-conf:pt:ptwrite} packet.
@item Qbtrace-conf:pt:event-tracing
The remote stub understands the @samp{Qbtrace-conf:pt:event-tracing} packet.
@item swbreak
The remote stub reports the @samp{swbreak} stop reason for memory
breakpoints.
@ -45832,6 +45850,18 @@ The ptwrite config parameter has been set.
A badly formed request or an error was encountered.
@end table
@item Qbtrace-conf:pt:event-tracing=@var{(yes|no)}
Indicate support for event-tracing packets. This allows for backwards
compatibility.
Reply:
@table @samp
@item OK
The event-tracing config parameter has been set.
@item E.errtext
A badly formed request or an error was encountered.
@end table
@end table
@node Architecture-Specific Protocol Details
@ -48465,15 +48495,16 @@ branch trace configuration discovery. @xref{Expat}.
The formal DTD for the branch trace configuration format is given below:
@smallexample
<!ELEMENT btrace-conf (bts?, pt?)>
<!ATTLIST btrace-conf version CDATA #FIXED "1.0">
<!ELEMENT btrace-conf (bts?, pt?)>
<!ATTLIST btrace-conf version CDATA #FIXED "1.0">
<!ELEMENT bts EMPTY>
<!ATTLIST bts size CDATA #IMPLIED>
<!ATTLIST bts size CDATA #IMPLIED>
<!ELEMENT pt EMPTY>
<!ATTLIST pt size CDATA #IMPLIED>
<!ATTLIST pt ptwrite (yes | no) #IMPLIED>
<!ATTLIST pt size CDATA #IMPLIED>
<!ATTLIST pt ptwrite (yes | no) #IMPLIED>
<!ATTLIST pt event-tracing (yes | no) #IMPLIED>
@end smallexample
@include agentexpr.texi

View File

@ -13,3 +13,4 @@
<!ELEMENT pt EMPTY>
<!ATTLIST pt size CDATA #IMPLIED>
<!ATTLIST pt ptwrite (yes | no) #IMPLIED>
<!ATTLIST pt event-tracing (yes | no) #IMPLIED>

View File

@ -686,6 +686,17 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
tinfo->conf.pt.ptwrite = true;
}
if (conf->event_tracing)
{
if (linux_supports_pt_feature ("event_trace"))
{
tinfo->attr.config |= linux_read_pt_config_bitmask ("event");
tinfo->conf.pt.event_tracing = true;
}
else
error (_("Event tracing for record btrace pt is not supported."));
}
errno = 0;
scoped_fd fd (syscall (SYS_perf_event_open, &tinfo->attr, pid, -1, -1, 0));
if (fd.get () < 0)

View File

@ -3180,6 +3180,36 @@ show_record_pt_buffer_size_value (struct ui_file *file, int from_tty,
value);
}
static bool event_tracing = false;
/* The "record pt event-tracing" show value function. */
static void
show_record_pt_event_tracing_value (struct ui_file *file, int from_tty,
struct cmd_list_element *c,
const char *value)
{
#if (LIBIPT_VERSION >= 0x201)
gdb_printf (file, _("record pt event-tracing is %s.\n"), value);
#else
gdb_printf (_("Event-tracing is not supported by GDB.\n"));
#endif /* defined (LIBIPT_VERSION >= 0x201) */
}
/* The "record pt event-tracing" set value function. */
static void
set_record_pt_event_tracing_value (const char *args, int from_tty,
cmd_list_element *c)
{
#if (LIBIPT_VERSION >= 0x201)
record_btrace_conf.pt.event_tracing = event_tracing;
#else
gdb_printf (_("Event-tracing is not supported by GDB.\n"));
#endif /* defined (LIBIPT_VERSION >= 0x201) */
}
/* Initialize btrace commands. */
void _initialize_record_btrace ();
@ -3299,6 +3329,19 @@ to see the actual buffer size."), NULL, show_record_pt_buffer_size_value,
&set_record_btrace_pt_cmdlist,
&show_record_btrace_pt_cmdlist);
add_setshow_boolean_cmd ("event-tracing", no_class, &event_tracing,
_("Set event-tracing for record pt."),
_("Show event-tracing for record pt."),
_("\
Use \"on\" to enable event tracing for recordings with Intel Processor Trace, \
and \"off\" to disable it.\n\
Without an argument, event tracing is enabled. Changing this setting has no\
effect on an active recording."),
set_record_pt_event_tracing_value,
show_record_pt_event_tracing_value,
&set_record_btrace_pt_cmdlist,
&show_record_btrace_pt_cmdlist);
add_target (record_btrace_target_info, record_btrace_target_open);
bfcache = htab_create_alloc (50, bfcache_hash, bfcache_eq, NULL,
@ -3311,4 +3354,5 @@ to see the actual buffer size."), NULL, show_record_pt_buffer_size_value,
#else
record_btrace_conf.pt.ptwrite = false;
#endif
record_btrace_conf.pt.event_tracing = false;
}

View File

@ -364,6 +364,9 @@ enum {
/* Support for the Qbtrace-conf:pt:ptwrite packet. */
PACKET_Qbtrace_conf_pt_ptwrite,
/* Support for the Qbtrace-conf:pt:event-tracing packet. */
PACKET_Qbtrace_conf_pt_event_tracing,
/* Support for exec events. */
PACKET_exec_event_feature,
@ -5815,6 +5818,8 @@ static const struct protocol_feature remote_protocol_features[] = {
PACKET_Qbtrace_conf_pt_size },
{ "Qbtrace-conf:pt:ptwrite", PACKET_DISABLE, remote_supported_packet,
PACKET_Qbtrace_conf_pt_ptwrite },
{ "Qbtrace-conf:pt:event-tracing", PACKET_DISABLE, remote_supported_packet,
PACKET_Qbtrace_conf_pt_event_tracing },
{ "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
{ "QThreadEvents", PACKET_DISABLE, remote_supported_packet, PACKET_QThreadEvents },
{ "QThreadOptions", PACKET_DISABLE, remote_supported_thread_options,
@ -14741,7 +14746,7 @@ parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
std::vector<gdb_xml_value> &attributes)
{
struct btrace_config *conf;
struct gdb_xml_value *size, *ptwrite;
struct gdb_xml_value *size, *ptwrite, *event_tracing;
conf = (struct btrace_config *) user_data;
conf->format = BTRACE_FORMAT_PT;
@ -14754,12 +14759,18 @@ parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
ptwrite = xml_find_attribute (attributes, "ptwrite");
if (ptwrite != nullptr)
conf->pt.ptwrite = (bool) *(ULONGEST *) ptwrite->value.get ();
event_tracing = xml_find_attribute (attributes, "event-tracing");
if (event_tracing != nullptr)
conf->pt.event_tracing = (bool) *(ULONGEST *) event_tracing->value.get ();
}
static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
{ "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
{ "ptwrite", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_enum,
gdb_xml_enums_boolean },
{ "event-tracing", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_enum,
gdb_xml_enums_boolean },
{ NULL, GDB_XML_AF_NONE, NULL, NULL }
};
@ -14891,6 +14902,39 @@ remote_target::btrace_sync_conf (const btrace_config *conf)
rs->btrace_config.pt.ptwrite = conf->pt.ptwrite;
}
/* Event tracing is a user setting, warn if it is set but the target
doesn't support it. */
if ((m_features.packet_support (PACKET_Qbtrace_conf_pt_event_tracing)
!= PACKET_ENABLE)
&& conf->pt.event_tracing)
warning (_("Target does not support event-tracing."));
if ((m_features.packet_support (PACKET_Qbtrace_conf_pt_event_tracing)
== PACKET_ENABLE)
&& conf->pt.event_tracing != rs->btrace_config.pt.event_tracing)
{
pos = buf;
const char *event_tracing = conf->pt.event_tracing ? "yes" : "no";
const char *name
= packets_descriptions[PACKET_Qbtrace_conf_pt_event_tracing].name;
pos += xsnprintf (pos, endbuf - pos, "%s=\"%s\"", name, event_tracing);
putpkt (buf);
getpkt (&rs->buf, 0);
packet_result result
= m_features.packet_ok (buf, PACKET_Qbtrace_conf_pt_event_tracing);
if (result.status () == PACKET_ERROR)
{
if (buf[0] == 'E' && buf[1] == '.')
error (_("Failed to sync event-tracing config: %s"), buf + 2);
else
error (_("Failed to sync event-tracing config."));
}
rs->btrace_config.pt.event_tracing = conf->pt.event_tracing;
}
}
/* Read TP's btrace configuration from the target and store it into CONF. */
@ -16348,6 +16392,10 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
add_packet_config_cmd (PACKET_Qbtrace_conf_pt_ptwrite, "Qbtrace-conf:pt:ptwrite",
"btrace-conf-pt-ptwrite", 0);
add_packet_config_cmd (PACKET_Qbtrace_conf_pt_event_tracing,
"Qbtrace-conf:pt:event-tracing",
"btrace-conf-pt-event-tracing", 0);
add_packet_config_cmd (PACKET_vContSupported, "vContSupported",
"verbose-resume-supported", 0);

View File

@ -6970,6 +6970,8 @@ linux_process_target::read_btrace_conf (const btrace_target_info *tinfo,
string_xml_appendf (*buffer, "/>\n");
string_xml_appendf (*buffer, " ptwrite=\"%s\"",
conf->pt.ptwrite ? "yes" : "no");
string_xml_appendf (*buffer, " event-tracing=\"%s\"",
conf->pt.event_tracing ? "yes" : "no");
string_xml_appendf (*buffer, "/>\n");
break;
}

View File

@ -550,6 +550,19 @@ handle_btrace_conf_general_set (char *own_buf)
return -1;
}
}
else if (strncmp (op, "pt:event-tracing=", strlen ("pt:event-tracing=")) == 0)
{
op += strlen ("pt:event-tracing=");
if (strncmp (op, "\"yes\"", strlen ("\"yes\"")) == 0)
current_btrace_conf.pt.event_tracing = true;
else if (strncmp (op, "\"no\"", strlen ("\"no\"")) == 0)
current_btrace_conf.pt.event_tracing = false;
else
{
strcpy (own_buf, "E.Bad event-tracing value.");
return -1;
}
}
else
{
strcpy (own_buf, "E.Bad Qbtrace configuration option.");
@ -2498,6 +2511,7 @@ supported_btrace_packets (char *buf)
strcat (buf, ";Qbtrace:pt+");
strcat (buf, ";Qbtrace-conf:pt:size+");
strcat (buf, ";Qbtrace-conf:pt:ptwrite+");
strcat (buf, ";Qbtrace-conf:pt:event-tracing+");
strcat (buf, ";Qbtrace:off+");
strcat (buf, ";qXfer:btrace:read+");
strcat (buf, ";qXfer:btrace-conf:read+");

View File

@ -123,6 +123,9 @@ struct btrace_config_pt
If both gdb and gdbserver support this, gdb will try to enable ptwrite
packets when tracing is started. */
bool ptwrite;
/* Event tracing setting. */
bool event_tracing;
};
/* A branch tracing configuration.