Blame SOURCES/gdb-rhbz1186918-gdbserver-in-container-2of8.patch

0b42f8
Implement qXfer:exec-file:read in gdbserver
0b42f8
0b42f8
From: Gary Benson <gbenson@redhat.com>
0b42f8
0b42f8
This commit implements the "qXfer:exec-file:read" packet in gdbserver.
0b42f8
0b42f8
gdb/gdbserver/ChangeLog:
0b42f8
0b42f8
	* target.h (struct target_ops) <pid_to_exec_file>: New field.
0b42f8
	* linux-low.c (linux_target_ops): Initialize pid_to_exec_file.
0b42f8
	* server.c (handle_qxfer_exec_file): New function.
0b42f8
	(qxfer_packets): Add exec-file entry.
0b42f8
	(handle_query): Report qXfer:exec-file:read as supported packet.
0b42f8
---
0b42f8
 gdb/gdbserver/linux-low.c |   19 +++++++++++++++++++
0b42f8
 gdb/gdbserver/server.c    |   40 ++++++++++++++++++++++++++++++++++++++++
0b42f8
 gdb/gdbserver/target.h    |    7 +++++++
0b42f8
 3 files changed, 66 insertions(+)
0b42f8
0b42f8
Index: gdb-7.6.1/gdb/gdbserver/linux-low.c
0b42f8
===================================================================
0b42f8
--- gdb-7.6.1.orig/gdb/gdbserver/linux-low.c	2016-03-15 19:04:39.260193747 +0100
0b42f8
+++ gdb-7.6.1/gdb/gdbserver/linux-low.c	2016-03-15 19:04:48.841256052 +0100
0b42f8
@@ -5993,6 +5993,24 @@
0b42f8
 }
0b42f8
 #endif /* HAVE_LINUX_BTRACE */
0b42f8
 
0b42f8
+// gdb/nat/linux-procfs.c
0b42f8
+static char *
0b42f8
+linux_proc_pid_to_exec_file (int pid)
0b42f8
+{
0b42f8
+  static char buf[PATH_MAX];
0b42f8
+  char name[PATH_MAX];
0b42f8
+  ssize_t len;
0b42f8
+
0b42f8
+  xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
0b42f8
+  len = readlink (name, buf, PATH_MAX - 1);
0b42f8
+  if (len <= 0)
0b42f8
+    strcpy (buf, name);
0b42f8
+  else
0b42f8
+    buf[len] = '\0';
0b42f8
+
0b42f8
+  return buf;
0b42f8
+}
0b42f8
+
0b42f8
 static struct target_ops linux_target_ops = {
0b42f8
   linux_create_inferior,
0b42f8
   linux_attach,
0b42f8
@@ -6070,6 +6088,7 @@
0b42f8
   NULL,
0b42f8
   NULL,
0b42f8
 #endif
0b42f8
+  linux_proc_pid_to_exec_file,
0b42f8
 };
0b42f8
 
0b42f8
 static void
0b42f8
Index: gdb-7.6.1/gdb/gdbserver/server.c
0b42f8
===================================================================
0b42f8
--- gdb-7.6.1.orig/gdb/gdbserver/server.c	2016-03-15 19:04:39.262193760 +0100
0b42f8
+++ gdb-7.6.1/gdb/gdbserver/server.c	2016-03-15 19:04:48.842256059 +0100
0b42f8
@@ -1006,6 +1006,42 @@
0b42f8
   return (*the_target->read_auxv) (offset, readbuf, len);
0b42f8
 }
0b42f8
 
0b42f8
+/* Handle qXfer:exec-file:read.  */
0b42f8
+
0b42f8
+static int
0b42f8
+handle_qxfer_exec_file (const char *const_annex,
0b42f8
+			gdb_byte *readbuf, const gdb_byte *writebuf,
0b42f8
+			ULONGEST offset, LONGEST len)
0b42f8
+{
0b42f8
+  char *annex, *file;
0b42f8
+  ULONGEST pid;
0b42f8
+  int total_len;
0b42f8
+
0b42f8
+  if (the_target->pid_to_exec_file == NULL || writebuf != NULL)
0b42f8
+    return -2;
0b42f8
+
0b42f8
+  annex = alloca (strlen (const_annex) + 1);
0b42f8
+  strcpy (annex, const_annex);
0b42f8
+  annex = unpack_varlen_hex (annex, &pid;;
0b42f8
+  if (annex[0] != '\0' || pid == 0)
0b42f8
+    return -1;
0b42f8
+
0b42f8
+  file = (*the_target->pid_to_exec_file) (pid);
0b42f8
+  if (file == NULL)
0b42f8
+    return -1;
0b42f8
+
0b42f8
+  total_len = strlen (file);
0b42f8
+
0b42f8
+  if (offset > total_len)
0b42f8
+    return -1;
0b42f8
+
0b42f8
+  if (offset + len > total_len)
0b42f8
+    len = total_len - offset;
0b42f8
+
0b42f8
+  memcpy (readbuf, file + offset, len);
0b42f8
+  return len;
0b42f8
+}
0b42f8
+
0b42f8
 /* Handle qXfer:features:read.  */
0b42f8
 
0b42f8
 static int
0b42f8
@@ -1408,6 +1444,7 @@
0b42f8
   {
0b42f8
     { "auxv", handle_qxfer_auxv },
0b42f8
     { "btrace", handle_qxfer_btrace },
0b42f8
+    { "exec-file", handle_qxfer_exec_file},
0b42f8
     { "fdpic", handle_qxfer_fdpic},
0b42f8
     { "features", handle_qxfer_features },
0b42f8
     { "libraries", handle_qxfer_libraries },
0b42f8
@@ -1828,6 +1865,9 @@
0b42f8
       if (target_supports_stopped_by_hw_breakpoint ())
0b42f8
 	strcat (own_buf, ";hwbreak+");
0b42f8
 
0b42f8
+      if (the_target->pid_to_exec_file != NULL)
0b42f8
+	strcat (own_buf, ";qXfer:exec-file:read+");
0b42f8
+
0b42f8
       return;
0b42f8
     }
0b42f8
 
0b42f8
Index: gdb-7.6.1/gdb/gdbserver/target.h
0b42f8
===================================================================
0b42f8
--- gdb-7.6.1.orig/gdb/gdbserver/target.h	2016-03-15 19:04:39.262193760 +0100
0b42f8
+++ gdb-7.6.1/gdb/gdbserver/target.h	2016-03-15 19:04:48.842256059 +0100
0b42f8
@@ -421,6 +421,13 @@
0b42f8
      to break a cyclic dependency.  */
0b42f8
   void (*read_btrace) (struct btrace_target_info *, struct buffer *, int type);
0b42f8
 
0b42f8
+  /* Return the full absolute name of the executable file that was
0b42f8
+     run to create the process PID.  If the executable file cannot
0b42f8
+     be determined, NULL is returned.  Otherwise, a pointer to a
0b42f8
+     character string containing the pathname is returned.  This
0b42f8
+     string should be copied into a buffer by the client if the string
0b42f8
+     will not be immediately used, or if it must persist.  */
0b42f8
+  char *(*pid_to_exec_file) (int pid);
0b42f8
 };
0b42f8
 
0b42f8
 extern struct target_ops *the_target;