This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[rfc][1/4] Consolidate TARGET_OBJECT_SPU handling


Hello,

this patch merges the TARGET_OBJECT_SPU xfer handlers from linux-nat.c
and gdbserver/linux-low.c and moves the merged handler to common/.

While this is not directly related to the "info proc" rework, this not
only removes some redundancy in existing code, but the new handler can
also serve as prototype for the TARGET_OBJECT_PROC handlers that the
next patch is going to introduce.

Bye,
Ulrich


ChangeLog:

	* common/linux-procfs.c: Include <sys/types.h>, <sys/stat.h>,
	<fcntl.h>, <unistd.h>, <dirent.h>, <sys/stat.h>, and <sys/vfs.h>.
	(SPUFS_MAGIC): Define if necessary.
	(spu_enumerate_spu_ids): New function, merged from linux-nat.c
	and gdbserver/linux-low.c versions.
	(linux_common_xfer_spu): Likewise.
	* common/linux-procfs.h (linux_common_xfer_spu): Add prototype.

	* linux-nat.c: Include "linux-procfs.h".
	(spu_enumerate_spu_ids): Remove.
	(linux_proc_xfer_spu): Remove implementation, replace by call
	to linux_common_xfer_spu.

gdbserver/ChangeLog:

	* linux-low.c: Do not include <sys/stat.h> and <sys/vfs.h>.
	(SPUFS_MAGIC): Remove.
	(spu_enumerate_spu_ids): Remove.
	(linux_qxfer_spu): Remove implementation, replace by call
	to linux_common_xfer_spu.


Index: gdb-head/gdb/common/linux-procfs.c
===================================================================
--- gdb-head.orig/gdb/common/linux-procfs.c
+++ gdb-head/gdb/common/linux-procfs.c
@@ -23,6 +23,18 @@
 #include "gdb_string.h"
 #endif
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <sys/vfs.h>
+
+#ifndef SPUFS_MAGIC
+#define SPUFS_MAGIC 0x23c9b64e
+#endif
+
 #include "linux-procfs.h"
 
 /* Return the TGID of LWPID from /proc/pid/status.  Returns -1 if not
@@ -53,3 +65,96 @@ linux_proc_get_tgid (int lwpid)
 
   return tgid;
 }
+
+
+/* Enumerate spufs IDs for process PID.  */
+static LONGEST
+spu_enumerate_spu_ids (long pid, gdb_byte *buf,
+		       ULONGEST offset, LONGEST len)
+{
+  ULONGEST pos = 0;
+  int written = 0;
+  char path[128];
+  DIR *dir;
+  struct dirent *entry;
+
+  snprintf (path, sizeof path, "/proc/%ld/fd", pid);
+  dir = opendir (path);
+  if (!dir)
+    return -1;
+
+  rewinddir (dir);
+  while ((entry = readdir (dir)) != NULL)
+    {
+      struct stat st;
+      struct statfs stfs;
+      int fd;
+
+      fd = atoi (entry->d_name);
+      if (!fd)
+        continue;
+
+      snprintf (path, sizeof path, "/proc/%ld/fd/%d", pid, fd);
+      if (stat (path, &st) != 0)
+        continue;
+      if (!S_ISDIR (st.st_mode))
+        continue;
+
+      if (statfs (path, &stfs) != 0)
+        continue;
+      if (stfs.f_type != SPUFS_MAGIC)
+        continue;
+
+      if (pos >= offset && pos + 4 <= offset + len)
+        {
+          *(unsigned int *)(buf + pos - offset) = fd;
+          written += 4;
+        }
+      pos += 4;
+    }
+
+  closedir (dir);
+  return written;
+}
+
+/* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
+   object type, using the /proc file system.  */
+LONGEST
+linux_common_xfer_spu (long pid, const char *annex, gdb_byte *readbuf,
+		       const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
+{
+  char buf[128];
+  int fd = 0;
+  LONGEST ret = 0;
+
+  if (!writebuf && !readbuf)
+    return -1;
+
+  if (!annex || !*annex)
+    {
+      if (!readbuf)
+	return -1;
+      else
+	return spu_enumerate_spu_ids (pid, readbuf, offset, len);
+    }
+
+  snprintf (buf, sizeof buf, "/proc/%ld/fd/%s", pid, annex);
+  fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
+  if (fd <= 0)
+    return -1;
+
+  if (offset != 0
+      && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
+    {
+      close (fd);
+      return 0;
+    }
+
+  if (writebuf)
+    ret = write (fd, writebuf, (size_t) len);
+  else
+    ret = read (fd, readbuf, (size_t) len);
+
+  close (fd);
+  return ret;
+}
Index: gdb-head/gdb/common/linux-procfs.h
===================================================================
--- gdb-head.orig/gdb/common/linux-procfs.h
+++ gdb-head/gdb/common/linux-procfs.h
@@ -26,4 +26,9 @@
 
 extern int linux_proc_get_tgid (int lwpid);
 
+extern LONGEST linux_common_xfer_spu (long pid, const char *annex,
+				      gdb_byte *readbuf,
+				      const gdb_byte *writebuf,
+				      ULONGEST offset, LONGEST len);
+
 #endif /* COMMON_LINUX_PROCFS_H */
Index: gdb-head/gdb/gdbserver/linux-low.c
===================================================================
--- gdb-head.orig/gdb/gdbserver/linux-low.c
+++ gdb-head/gdb/gdbserver/linux-low.c
@@ -40,8 +40,6 @@
 #include <pwd.h>
 #include <sys/types.h>
 #include <dirent.h>
-#include <sys/stat.h>
-#include <sys/vfs.h>
 #include <sys/uio.h>
 #ifndef ELFMAG0
 /* Don't include <linux/elf.h> here.  If it got included by gdb_proc_service.h
@@ -51,10 +49,6 @@
 #include <elf.h>
 #endif
 
-#ifndef SPUFS_MAGIC
-#define SPUFS_MAGIC 0x23c9b64e
-#endif
-
 #ifdef HAVE_PERSONALITY
 # include <sys/personality.h>
 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
@@ -4681,55 +4675,6 @@ linux_supports_disable_randomization (vo
 #endif
 }
 
-/* Enumerate spufs IDs for process PID.  */
-static int
-spu_enumerate_spu_ids (long pid, unsigned char *buf, CORE_ADDR offset, int len)
-{
-  int pos = 0;
-  int written = 0;
-  char path[128];
-  DIR *dir;
-  struct dirent *entry;
-
-  sprintf (path, "/proc/%ld/fd", pid);
-  dir = opendir (path);
-  if (!dir)
-    return -1;
-
-  rewinddir (dir);
-  while ((entry = readdir (dir)) != NULL)
-    {
-      struct stat st;
-      struct statfs stfs;
-      int fd;
-
-      fd = atoi (entry->d_name);
-      if (!fd)
-        continue;
-
-      sprintf (path, "/proc/%ld/fd/%d", pid, fd);
-      if (stat (path, &st) != 0)
-        continue;
-      if (!S_ISDIR (st.st_mode))
-        continue;
-
-      if (statfs (path, &stfs) != 0)
-        continue;
-      if (stfs.f_type != SPUFS_MAGIC)
-        continue;
-
-      if (pos >= offset && pos + 4 <= offset + len)
-        {
-          *(unsigned int *)(buf + pos - offset) = fd;
-          written += 4;
-        }
-      pos += 4;
-    }
-
-  closedir (dir);
-  return written;
-}
-
 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
    object type, using the /proc file system.  */
 static int
@@ -4738,40 +4683,7 @@ linux_qxfer_spu (const char *annex, unsi
 		 CORE_ADDR offset, int len)
 {
   long pid = lwpid_of (get_thread_lwp (current_inferior));
-  char buf[128];
-  int fd = 0;
-  int ret = 0;
-
-  if (!writebuf && !readbuf)
-    return -1;
-
-  if (!*annex)
-    {
-      if (!readbuf)
-	return -1;
-      else
-	return spu_enumerate_spu_ids (pid, readbuf, offset, len);
-    }
-
-  sprintf (buf, "/proc/%ld/fd/%s", pid, annex);
-  fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
-  if (fd <= 0)
-    return -1;
-
-  if (offset != 0
-      && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
-    {
-      close (fd);
-      return 0;
-    }
-
-  if (writebuf)
-    ret = write (fd, writebuf, (size_t) len);
-  else
-    ret = read (fd, readbuf, (size_t) len);
-
-  close (fd);
-  return ret;
+  return linux_common_xfer_spu (pid, annex, readbuf, writebuf, offset, len);
 }
 
 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
Index: gdb-head/gdb/linux-nat.c
===================================================================
--- gdb-head.orig/gdb/linux-nat.c
+++ gdb-head/gdb/linux-nat.c
@@ -59,6 +59,7 @@
 #include <sys/vfs.h>
 #include "solib.h"
 #include "linux-osdata.h"
+#include "linux-procfs.h"
 #include "cli/cli-utils.h"
 
 #ifndef SPUFS_MAGIC
@@ -5127,57 +5128,6 @@ linux_proc_xfer_partial (struct target_o
   return ret;
 }
 
-
-/* Enumerate spufs IDs for process PID.  */
-static LONGEST
-spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, LONGEST len)
-{
-  enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
-  LONGEST pos = 0;
-  LONGEST written = 0;
-  char path[128];
-  DIR *dir;
-  struct dirent *entry;
-
-  xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
-  dir = opendir (path);
-  if (!dir)
-    return -1;
-
-  rewinddir (dir);
-  while ((entry = readdir (dir)) != NULL)
-    {
-      struct stat st;
-      struct statfs stfs;
-      int fd;
-
-      fd = atoi (entry->d_name);
-      if (!fd)
-	continue;
-
-      xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
-      if (stat (path, &st) != 0)
-	continue;
-      if (!S_ISDIR (st.st_mode))
-	continue;
-
-      if (statfs (path, &stfs) != 0)
-	continue;
-      if (stfs.f_type != SPUFS_MAGIC)
-	continue;
-
-      if (pos >= offset && pos + 4 <= offset + len)
-	{
-	  store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
-	  written += 4;
-	}
-      pos += 4;
-    }
-
-  closedir (dir);
-  return written;
-}
-
 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
    object type, using the /proc file system.  */
 static LONGEST
@@ -5186,38 +5136,10 @@ linux_proc_xfer_spu (struct target_ops *
 		     const gdb_byte *writebuf,
 		     ULONGEST offset, LONGEST len)
 {
-  char buf[128];
-  int fd = 0;
-  int ret = -1;
-  int pid = PIDGET (inferior_ptid);
-
-  if (!annex)
-    {
-      if (!readbuf)
-	return -1;
-      else
-	return spu_enumerate_spu_ids (pid, readbuf, offset, len);
-    }
-
-  xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
-  fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
-  if (fd <= 0)
-    return -1;
-
-  if (offset != 0
-      && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
-    {
-      close (fd);
-      return 0;
-    }
-
-  if (writebuf)
-    ret = write (fd, writebuf, (size_t) len);
-  else if (readbuf)
-    ret = read (fd, readbuf, (size_t) len);
+  gdb_assert (object == TARGET_OBJECT_SPU);
 
-  close (fd);
-  return ret;
+  return linux_common_xfer_spu (PIDGET (inferior_ptid), annex,
+				readbuf, writebuf, offset, len);
 }
 
 
-- 
  Dr. Ulrich Weigand
  GNU Toolchain for Linux on System z and Cell BE
  Ulrich.Weigand@de.ibm.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]