[obv] Code cleanup: Unify linux_nat_core_of_thread_1 with common/

Jan Kratochvil jan.kratochvil@redhat.com
Mon Mar 5 21:08:00 GMT 2012


Hi,

just unification of code (for my later patch), linux-nat -> common was
differing by:

## -1,8 +1,9 @@
 int
-linux_nat_core_of_thread_1 (ptid_t ptid)
+linux_common_core_of_thread (ptid_t ptid)
 {
-  struct cleanup *back_to;
-  char *filename;
+  char filename[sizeof ("/proc//task//stat")
+		 + 2 * 20 /* decimal digits for 2 numbers, max 2^64 bit each */
+		 + 1];
   FILE *f;
   char *content = NULL;
   char *p;
## -11,23 +12,15 @@ linux_nat_core_of_thread_1 (ptid_t ptid)
   int i;
   int core;
 
-  filename = xstrprintf ("/proc/%d/task/%ld/stat",
-			 GET_PID (ptid), GET_LWP (ptid));
-  back_to = make_cleanup (xfree, filename);
-
+  sprintf (filename, "/proc/%d/task/%ld/stat",
+	   ptid_get_pid (ptid), ptid_get_lwp (ptid));
   f = fopen (filename, "r");
   if (!f)
-    {
-      do_cleanups (back_to);
-      return -1;
-    }
-
-  make_cleanup_fclose (f);
+    return -1;
 
   for (;;)
     {
       int n;
-
       content = xrealloc (content, content_read + 1024);
       n = fread (content + content_read, 1, 1024, f);
       content_read += n;
## -38,8 +31,6 @@ linux_nat_core_of_thread_1 (ptid_t ptid)
 	}
     }
 
-  make_cleanup (xfree, content);
-
   p = strchr (content, '(');
 
   /* Skip ")".  */
## -58,7 +49,8 @@ linux_nat_core_of_thread_1 (ptid_t ptid)
   if (p == NULL || sscanf (p, "%d", &core) == 0)
     core = -1;
 
-  do_cleanups (back_to);
+  xfree (content);
+  fclose (f);
 
   return core;
 }

The common/ implementation has been introduced by:
	[PATCH] Unify implementation of 'info os' commands between gdb and gdbserver
	http://sourceware.org/ml/gdb-patches/2011-07/msg00304.html

No regressions on {x86_64,x86_64-m32,i686}-fedora17-linux-gnu.

Checked in.


Thanks,
Jan


http://sourceware.org/ml/gdb-cvs/2012-03/msg00103.html

--- src/gdb/ChangeLog	2012/03/05 20:53:15	1.13963
+++ src/gdb/ChangeLog	2012/03/05 21:07:46	1.13964
@@ -1,3 +1,16 @@
+2012-03-05  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	Code cleanup.
+	* common/linux-osdata.c (linux_common_core_of_thread): New function
+	comment.
+	* linux-nat.c (linux_nat_wait_1): Replace linux_nat_core_of_thread_1
+	call by linux_common_core_of_thread.
+	(linux_nat_core_of_thread_1): Remove.
+	* linux-nat.h (linux_nat_core_of_thread_1): Remove declaration.
+	* linux-thread-db.c: Include linux-osdata.h.
+	(update_thread_core): Replace linux_nat_core_of_thread_1 call by
+	linux_common_core_of_thread.
+
 2012-03-05  Tom Tromey  <tromey@redhat.com>
 
 	* value.c (value_primitive_field): Don't fetch contents for
--- src/gdb/linux-nat.c	2012/03/03 09:51:28	1.242
+++ src/gdb/linux-nat.c	2012/03/05 21:07:46	1.243
@@ -4000,7 +4000,7 @@
       || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
     lp->core = -1;
   else
-    lp->core = linux_nat_core_of_thread_1 (lp->ptid);
+    lp->core = linux_common_core_of_thread (lp->ptid);
 
   return lp->ptid;
 }
@@ -5189,71 +5189,6 @@
   return inf->aspace;
 }
 
-int
-linux_nat_core_of_thread_1 (ptid_t ptid)
-{
-  struct cleanup *back_to;
-  char *filename;
-  FILE *f;
-  char *content = NULL;
-  char *p;
-  char *ts = 0;
-  int content_read = 0;
-  int i;
-  int core;
-
-  filename = xstrprintf ("/proc/%d/task/%ld/stat",
-			 GET_PID (ptid), GET_LWP (ptid));
-  back_to = make_cleanup (xfree, filename);
-
-  f = fopen (filename, "r");
-  if (!f)
-    {
-      do_cleanups (back_to);
-      return -1;
-    }
-
-  make_cleanup_fclose (f);
-
-  for (;;)
-    {
-      int n;
-
-      content = xrealloc (content, content_read + 1024);
-      n = fread (content + content_read, 1, 1024, f);
-      content_read += n;
-      if (n < 1024)
-	{
-	  content[content_read] = '\0';
-	  break;
-	}
-    }
-
-  make_cleanup (xfree, content);
-
-  p = strchr (content, '(');
-
-  /* Skip ")".  */
-  if (p != NULL)
-    p = strchr (p, ')');
-  if (p != NULL)
-    p++;
-
-  /* If the first field after program name has index 0, then core number is
-     the field with index 36.  There's no constant for that anywhere.  */
-  if (p != NULL)
-    p = strtok_r (p, " ", &ts);
-  for (i = 0; p != NULL && i != 36; ++i)
-    p = strtok_r (NULL, " ", &ts);
-
-  if (p == NULL || sscanf (p, "%d", &core) == 0)
-    core = -1;
-
-  do_cleanups (back_to);
-
-  return core;
-}
-
 /* Return the cached value of the processor core for thread PTID.  */
 
 static int
--- src/gdb/linux-nat.h	2012/01/24 13:46:54	1.44
+++ src/gdb/linux-nat.h	2012/03/05 21:07:46	1.45
@@ -200,9 +200,6 @@
 /* Return the saved siginfo associated with PTID.  */
 struct siginfo *linux_nat_get_siginfo (ptid_t ptid);
 
-/* Compute and return the processor core of a given thread.  */
-int linux_nat_core_of_thread_1 (ptid_t ptid);
-
 /* Set alternative SIGTRAP-like events recognizer.  */
 void linux_nat_set_status_is_event (struct target_ops *t,
 				    int (*status_is_event) (int status));
--- src/gdb/linux-thread-db.c	2012/03/02 16:54:59	1.94
+++ src/gdb/linux-thread-db.c	2012/03/05 21:07:46	1.95
@@ -40,6 +40,7 @@
 #include "observer.h"
 #include "linux-nat.h"
 #include "linux-procfs.h"
+#include "linux-osdata.h"
 
 #include <signal.h>
 
@@ -1603,7 +1604,7 @@
 static int
 update_thread_core (struct lwp_info *info, void *closure)
 {
-  info->core = linux_nat_core_of_thread_1 (info->ptid);
+  info->core = linux_common_core_of_thread (info->ptid);
   return 0;
 }
 
--- src/gdb/common/linux-osdata.c	2012/01/04 08:17:18	1.3
+++ src/gdb/common/linux-osdata.c	2012/03/05 21:07:46	1.4
@@ -45,6 +45,8 @@
 #include "gdb_assert.h"
 #include "gdb_dirent.h"
 
+/* Compute and return the processor core of a given thread.  */
+
 int
 linux_common_core_of_thread (ptid_t ptid)
 {



More information about the Gdb-patches mailing list