+/* This type describes each known response to the qSupported
+ packet. */
+struct protocol_feature
+{
+ /* The name of this protocol feature. */
+ const char *name;
+
+ /* The default for this protocol feature. */
+ enum packet_support default_support;
+
+ /* The function to call when this feature is reported, or after
+ qSupported processing if the feature is not supported.
+ The first argument points to this structure. The second
+ argument indicates whether the packet requested support be
+ enabled, disabled, or probed (or the default, if this function
+ is being called at the end of processing and this feature was
+ not reported). The third argument may be NULL; if not NULL, it
+ is a NUL-terminated string taken from the packet following
+ this feature's name and an equals sign. */
+ void (*func) (const struct protocol_feature *, enum packet_support,
+ const char *);
+
+ /* The corresponding packet for this feature. Only used if
+ FUNC is remote_supported_packet. */
+ int packet;
+};
+
+static void
+remote_supported_packet (const struct protocol_feature *feature,
+ enum packet_support support,
+ const char *argument)
+{
+ if (argument)
+ {
+ warning (_("Remote qSupported response supplied an unexpected value for"
+ " \"%s\"."), feature->name);
+ return;
+ }
+
+ if (remote_protocol_packets[feature->packet].support
+ == PACKET_SUPPORT_UNKNOWN)
+ remote_protocol_packets[feature->packet].support = support;
+}
+
+static void
+remote_packet_size (const struct protocol_feature *feature,
+ enum packet_support support, const char *value)
+{
+ struct remote_state *rs = get_remote_state ();
+
+ int packet_size;
+ char *value_end;
+
+ if (support != PACKET_ENABLE)
+ return;
+
+ if (value == NULL || *value == '\0')
+ {
+ warning (_("Remote target reported \"%s\" without a size."),
+ feature->name);
+ return;
+ }
+
+ errno = 0;
+ packet_size = strtol (value, &value_end, 16);
+ if (errno != 0 || *value_end != '\0' || packet_size < 0)
+ {
+ warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
+ feature->name, value);
+ return;
+ }
+
+ if (packet_size > MAX_REMOTE_PACKET_SIZE)
+ {
+ warning (_("limiting remote suggested packet size (%d bytes) to %d"),
+ packet_size, MAX_REMOTE_PACKET_SIZE);
+ packet_size = MAX_REMOTE_PACKET_SIZE;
+ }
+
+ /* Record the new maximum packet size. */
+ rs->explicit_packet_size = packet_size;
+}
+
+static struct protocol_feature remote_protocol_features[] = {
+ { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
+ { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
+ PACKET_qXfer_auxv },
+ { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
+ PACKET_qXfer_features },
+ { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
+ PACKET_qXfer_libraries },
+ { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
+ PACKET_qXfer_memory_map },
+ { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
+ PACKET_qXfer_spu_read },
+ { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
+ PACKET_qXfer_spu_write },
+ { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QPassSignals },
+};
+
+static void
+remote_query_supported (void)
+{
+ struct remote_state *rs = get_remote_state ();
+ char *next;
+ int i;
+ unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
+
+ /* The packet support flags are handled differently for this packet
+ than for most others. We treat an error, a disabled packet, and
+ an empty response identically: any features which must be reported
+ to be used will be automatically disabled. An empty buffer
+ accomplishes this, since that is also the representation for a list
+ containing no features. */
+
+ rs->buf[0] = 0;
+ if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE)
+ {
+ putpkt ("qSupported");
+ getpkt (&rs->buf, &rs->buf_size, 0);
+
+ /* If an error occured, warn, but do not return - just reset the
+ buffer to empty and go on to disable features. */
+ if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
+ == PACKET_ERROR)
+ {
+ warning (_("Remote failure reply: %s"), rs->buf);
+ rs->buf[0] = 0;
+ }
+ }
+
+ memset (seen, 0, sizeof (seen));
+
+ next = rs->buf;
+ while (*next)
+ {
+ enum packet_support is_supported;
+ char *p, *end, *name_end, *value;
+
+ /* First separate out this item from the rest of the packet. If
+ there's another item after this, we overwrite the separator
+ (terminated strings are much easier to work with). */
+ p = next;
+ end = strchr (p, ';');
+ if (end == NULL)
+ {
+ end = p + strlen (p);
+ next = end;
+ }
+ else
+ {
+ *end = '\0';
+ next = end + 1;
+
+ if (end == p)
+ {
+ warning (_("empty item in \"qSupported\" response"));
+ continue;
+ }
+ }
+
+ name_end = strchr (p, '=');
+ if (name_end)
+ {
+ /* This is a name=value entry. */
+ is_supported = PACKET_ENABLE;
+ value = name_end + 1;
+ *name_end = '\0';
+ }
+ else
+ {
+ value = NULL;
+ switch (end[-1])
+ {
+ case '+':
+ is_supported = PACKET_ENABLE;
+ break;
+
+ case '-':
+ is_supported = PACKET_DISABLE;
+ break;
+
+ case '?':
+ is_supported = PACKET_SUPPORT_UNKNOWN;
+ break;
+
+ default:
+ warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p);
+ continue;
+ }
+ end[-1] = '\0';
+ }
+
+ for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
+ if (strcmp (remote_protocol_features[i].name, p) == 0)
+ {
+ const struct protocol_feature *feature;
+
+ seen[i] = 1;
+ feature = &remote_protocol_features[i];
+ feature->func (feature, is_supported, value);
+ break;
+ }
+ }
+
+ /* If we increased the packet size, make sure to increase the global
+ buffer size also. We delay this until after parsing the entire
+ qSupported packet, because this is the same buffer we were
+ parsing. */
+ if (rs->buf_size < rs->explicit_packet_size)
+ {
+ rs->buf_size = rs->explicit_packet_size;
+ rs->buf = xrealloc (rs->buf, rs->buf_size);
+ }
+
+ /* Handle the defaults for unmentioned features. */
+ for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
+ if (!seen[i])
+ {
+ const struct protocol_feature *feature;
+
+ feature = &remote_protocol_features[i];
+ feature->func (feature, feature->default_support, NULL);
+ }
+}
+
+