[2/3] gdb/remote.c: support getting thread names with qThreadName

Message ID 1443700496-2936-3-git-send-email-pclouds@gmail.com
State New, archived
Headers

Commit Message

Nguyễn Thái Ngọc Duy Oct. 1, 2015, 11:54 a.m. UTC
  Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 gdb/remote.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
  

Patch

diff --git a/gdb/remote.c b/gdb/remote.c
index f40f791..89c2baf 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1478,6 +1478,9 @@  enum {
   /* Support for query supported vCont actions.  */
   PACKET_vContSupported,
 
+  /* Support for query thread name.  */
+  PACKET_qThreadName,
+
   PACKET_MAX
 };
 
@@ -4392,7 +4395,8 @@  static const struct protocol_feature remote_protocol_features[] = {
     PACKET_exec_event_feature },
   { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
     PACKET_Qbtrace_conf_pt_size },
-  { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported }
+  { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
+  { "qThreadName", PACKET_DISABLE, remote_supported_packet, PACKET_qThreadName }
 };
 
 static char *remote_support_xml;
@@ -4488,6 +4492,9 @@  remote_query_supported (void)
       if (packet_set_cmd_state (PACKET_vContSupported) != AUTO_BOOLEAN_FALSE)
 	q = remote_query_supported_append (q, "vContSupported+");
 
+      if (packet_set_cmd_state (PACKET_qThreadName) != AUTO_BOOLEAN_FALSE)
+	q = remote_query_supported_append (q, "qThreadName+");
+
       q = reconcat (q, "qSupported:", q, (char *) NULL);
       putpkt (q);
 
@@ -10243,6 +10250,52 @@  remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
     }
 }
 
+/* Convert a thread ID to a string.  Returns the string in a static
+   buffer.  */
+
+static char *
+remote_thread_name (struct target_ops *ops, struct thread_info *thr)
+{
+  if (packet_support (PACKET_qThreadName) != PACKET_DISABLE)
+    {
+      struct remote_state *rs = get_remote_state ();
+      char *p = rs->buf;
+      char *endp = rs->buf + get_remote_packet_size ();
+      enum packet_result result;
+
+      strcpy (p, "qThreadName:");
+      p += strlen (p);
+      p = write_ptid (p, endp, thr->ptid);
+      *p++ = '\0';
+
+      putpkt (rs->buf);
+      getpkt (&rs->buf, &rs->buf_size, 0);
+      result = packet_ok (rs->buf,
+			  &remote_protocol_packets[PACKET_qThreadName]);
+      if (result == PACKET_OK)
+	{
+#define COMM_LEN 16		/* from linux-nat.c */
+	  static char name[COMM_LEN + 1];
+	  ULONGEST ignored;
+
+	  p = unpack_varlen_hex (rs->buf, &ignored);
+	  if (*p != ';')
+	    error (_("invalid qThreadName packet: %s"), rs->buf);
+	  strncpy(name, p + 1, COMM_LEN);
+	  name[COMM_LEN] = '\0';
+	  return name;
+	}
+      else if (result == PACKET_UNKNOWN)
+	throw_error (TLS_GENERIC_ERROR,
+		     _("Remote target doesn't support qThreadName packet"));
+      else
+	throw_error (TLS_GENERIC_ERROR,
+		     _("Remote target failed to process qThreadName request"));
+    }
+
+  return NULL;
+}
+
 /* Get the address of the thread local variable in OBJFILE which is
    stored at OFFSET within the thread local storage for thread PTID.  */
 
@@ -12744,6 +12797,7 @@  Specify the serial device it is connected to\n\
   remote_ops.to_thread_alive = remote_thread_alive;
   remote_ops.to_update_thread_list = remote_update_thread_list;
   remote_ops.to_pid_to_str = remote_pid_to_str;
+  remote_ops.to_thread_name = remote_thread_name;
   remote_ops.to_extra_thread_info = remote_threads_extra_info;
   remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
   remote_ops.to_stop = remote_stop;
@@ -13485,6 +13539,9 @@  Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_exec_event_feature],
 			 "exec-event-feature", "exec-event-feature", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_qThreadName],
+			 "qThreadName", "thread-name", 0);
+
   /* Assert that we've registered "set remote foo-packet" commands
      for all packet configs.  */
   {