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

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