Blame SOURCES/gdb-rhbz1225569-oom-killer-aarch64-frame-same-id-7of8.patch

01917d
commit 1b5d0ab34c53d6e896d2c0958b1176e324bb7878
01917d
Author: Pedro Alves <palves@redhat.com>
01917d
Date:   Wed Jul 16 19:25:41 2014 +0100
01917d
01917d
    gdb.trace/tfile.c: Remove Thumb bit in one more more, general cleanup
01917d
    
01917d
    I noticed that the existing code casts a function's address to 'long',
01917d
    but that doesn't work correctly on some ABIs, like Win64, where long
01917d
    is 32-bit and while pointers are 64-bit:
01917d
    
01917d
      func_addr = (long) &write_basic_trace_file;
01917d
    
01917d
    Fixing that showed there's actually another place in the file that
01917d
    writes a function address to file, and therefore should clear the
01917d
    Thumb bit.  This commit adds a macro+function pair to centralize the
01917d
    Thumb bit handling, and uses it in both places.
01917d
    
01917d
    The rest is just enough changes to make the file build without
01917d
    warnings with "-Wall -Wextra" with x86_64-w64-mingw32-gcc and
01917d
    i686-w64-mingw32-gcc cross compilers, and with -m32/-m64 on x86_64
01917d
    GNU/Linux.  Currently with x86_64-w64-mingw32-gcc we get:
01917d
    
01917d
      $ x86_64-w64-mingw32-gcc tfile.c  -Wall -DTFILE_DIR=\"\"
01917d
      tfile.c: In function 'start_trace_file':
01917d
      tfile.c:51:23: error: 'S_IRGRP' undeclared (first use in this function)
01917d
    	 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
01917d
    			 ^
01917d
      tfile.c:51:23: note: each undeclared identifier is reported only once for each function it appears in
01917d
      tfile.c:51:31: error: 'S_IROTH' undeclared (first use in this function)
01917d
    	 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
01917d
    				 ^
01917d
      tfile.c: In function 'add_memory_block':
01917d
      tfile.c:79:10: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
01917d
         ll_x = (unsigned long) addr;
01917d
    	    ^
01917d
      tfile.c: In function 'write_basic_trace_file':
01917d
      tfile.c:113:15: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
01917d
         func_addr = (long) &write_basic_trace_file;
01917d
    		 ^
01917d
      tfile.c:137:3: warning: passing argument 1 of 'add_memory_block' from incompatible pointer type [enabled by default]
01917d
         add_memory_block (&testglob, sizeof (testglob));
01917d
         ^
01917d
      tfile.c:72:1: note: expected 'char *' but argument is of type 'int *'
01917d
       add_memory_block (char *addr, int size)
01917d
       ^
01917d
      tfile.c:139:3: warning: passing argument 1 of 'add_memory_block' from incompatible pointer type [enabled by default]
01917d
         add_memory_block (&testglob2, 1);
01917d
         ^
01917d
      tfile.c:72:1: note: expected 'char *' but argument is of type 'int *'
01917d
       add_memory_block (char *addr, int size)
01917d
       ^
01917d
      tfile.c: In function 'write_error_trace_file':
01917d
      tfile.c:185:3: warning: implicit declaration of function 'alloca' [-Wimplicit-function-declaration]
01917d
         char *hex = alloca (len * 2 + 1);
01917d
         ^
01917d
      tfile.c:185:15: warning: incompatible implicit declaration of built-in function 'alloca' [enabled by default]
01917d
         char *hex = alloca (len * 2 + 1);
01917d
    		 ^
01917d
      tfile.c:211:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
01917d
    	(long) &write_basic_trace_file);
01917d
          ^
01917d
    
01917d
    Tested on x86_64 Fedora 20, -m64 and -m32.
01917d
    Tested by Yao on arm targets.
01917d
    
01917d
    gdb/testsuite/
01917d
    2014-07-16  Pedro Alves  <palves@redhat.com>
01917d
    
01917d
    	* gdb.trace/tfile.c: Include unistd.h and stdint.h.
01917d
    	(start_trace_file): Guard S_IRGRP and S_IROTH uses behind #ifdef.
01917d
    	(tfile_write_64, tfile_write_16, tfile_write_8, tfile_write_addr)
01917d
    	(tfile_write_buf): New functions.
01917d
    	(add_memory_block): Rewrite using the above.
01917d
    	(adjust_function_address): New function.
01917d
    	(FUNCTION_ADDRESS): New macro.
01917d
    	(write_basic_trace_file): Remove short_x local, and use
01917d
    	tfile_write_16. Change type of func_addr local to unsigned long
01917d
    	long.  Use FUNCTION_ADDRESS instead of handling the Thumb bit
01917d
    	here.  Cast argument of add_memory_block to char pointer.
01917d
    	(write_error_trace_file): Avoid alloca.  Use FUNCTION_ADDRESS.
01917d
    	(main): Remove parameters.
01917d
    	* gdb.trace/tfile.exp: Remove nowarnings.
01917d
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.trace/tfile.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/testsuite/gdb.trace/tfile.c
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.trace/tfile.c
01917d
@@ -3,9 +3,11 @@
01917d
    GDB.  */
01917d
 
01917d
 #include <stdio.h>
01917d
+#include <unistd.h>
01917d
 #include <string.h>
01917d
 #include <fcntl.h>
01917d
 #include <sys/stat.h>
01917d
+#include <stdint.h>
01917d
 
01917d
 char spbuf[200];
01917d
 
01917d
@@ -23,9 +25,17 @@ int
01917d
 start_trace_file (char *filename)
01917d
 {
01917d
   int fd;
01917d
+  mode_t mode = S_IRUSR | S_IWUSR;
01917d
 
01917d
-  fd = open (filename, O_WRONLY|O_CREAT|O_APPEND,
01917d
-	     S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
01917d
+#ifdef S_IRGRP
01917d
+  mode |= S_IRGRP;
01917d
+#endif
01917d
+
01917d
+#ifdef S_IROTH
01917d
+  mode |= S_IROTH;
01917d
+#endif
01917d
+
01917d
+  fd = open (filename, O_WRONLY|O_CREAT|O_APPEND, mode);
01917d
 
01917d
   if (fd < 0)
01917d
     return fd;
01917d
@@ -44,31 +54,73 @@ finish_trace_file (int fd)
01917d
   close (fd);
01917d
 }
01917d
 
01917d
+static void
01917d
+tfile_write_64 (uint64_t value)
01917d
+{
01917d
+  memcpy (trptr, &value, sizeof (uint64_t));
01917d
+  trptr += sizeof (uint64_t);
01917d
+}
01917d
 
01917d
-void
01917d
-add_memory_block (char *addr, int size)
01917d
+static void
01917d
+tfile_write_16 (uint16_t value)
01917d
+{
01917d
+  memcpy (trptr, &value, sizeof (uint16_t));
01917d
+  trptr += sizeof (uint16_t);
01917d
+}
01917d
+
01917d
+static void
01917d
+tfile_write_8 (uint8_t value)
01917d
 {
01917d
-  short short_x;
01917d
-  unsigned long long ll_x;
01917d
+  memcpy (trptr, &value, sizeof (uint8_t));
01917d
+  trptr += sizeof (uint8_t);
01917d
+}
01917d
 
01917d
-  *((char *) trptr) = 'M';
01917d
-  trptr += 1;
01917d
-  ll_x = (unsigned long) addr;
01917d
-  memcpy (trptr, &ll_x, sizeof (unsigned long long));
01917d
-  trptr += sizeof (unsigned long long);
01917d
-  short_x = size;
01917d
-  memcpy (trptr, &short_x, 2);
01917d
-  trptr += 2;
01917d
+static void
01917d
+tfile_write_addr (char *addr)
01917d
+{
01917d
+  tfile_write_64 ((uint64_t) (uintptr_t) addr);
01917d
+}
01917d
+
01917d
+static void
01917d
+tfile_write_buf (const void *addr, size_t size)
01917d
+{
01917d
   memcpy (trptr, addr, size);
01917d
   trptr += size;
01917d
 }
01917d
 
01917d
 void
01917d
+add_memory_block (char *addr, int size)
01917d
+{
01917d
+  tfile_write_8 ('M');
01917d
+  tfile_write_addr (addr);
01917d
+  tfile_write_16 (size);
01917d
+  tfile_write_buf (addr, size);
01917d
+}
01917d
+
01917d
+/* Adjust a function's address to account for architectural
01917d
+   particularities.  */
01917d
+
01917d
+static uintptr_t
01917d
+adjust_function_address (uintptr_t func_addr)
01917d
+{
01917d
+#if defined(__thumb__) || defined(__thumb2__)
01917d
+  /* Although Thumb functions are two-byte aligned, function
01917d
+     pointers have the Thumb bit set.  Clear it.  */
01917d
+  return func_addr & ~1;
01917d
+#else
01917d
+  return func_addr;
01917d
+#endif
01917d
+}
01917d
+
01917d
+/* Get a function's address as an integer.  */
01917d
+
01917d
+#define FUNCTION_ADDRESS(FUN) adjust_function_address ((uintptr_t) &FUN)
01917d
+
01917d
+void
01917d
 write_basic_trace_file (void)
01917d
 {
01917d
   int fd, int_x;
01917d
-  short short_x;
01917d
-  long func_addr;
01917d
+  unsigned long long func_addr;
01917d
 
01917d
   fd = start_trace_file ("basic.tf");
01917d
 
01917d
@@ -86,15 +138,9 @@ write_basic_trace_file (void)
01917d
 
01917d
   /* Dump tracepoint definitions, in syntax similar to that used
01917d
      for reconnection uploads.  */
01917d
-  /* FIXME need a portable way to print function address in hex */
01917d
-  func_addr = (long) &write_basic_trace_file;
01917d
-#if defined(__thumb__) || defined(__thumb2__)
01917d
-  /* Although Thumb functions are two-byte aligned, function
01917d
-     pointers have the Thumb bit set.  Clear it.  */
01917d
-  func_addr &= ~1;
01917d
-#endif
01917d
+  func_addr = FUNCTION_ADDRESS (write_basic_trace_file);
01917d
 
01917d
-  snprintf (spbuf, sizeof spbuf, "tp T1:%lx:E:0:0\n", func_addr);
01917d
+  snprintf (spbuf, sizeof spbuf, "tp T1:%llx:E:0:0\n", func_addr);
01917d
   write (fd, spbuf, strlen (spbuf));
01917d
   /* (Note that we would only need actions defined if we wanted to
01917d
      test tdump.) */
01917d
@@ -106,14 +152,13 @@ write_basic_trace_file (void)
01917d
   /* (Encapsulate better if we're going to do lots of this; note that
01917d
      buffer endianness is the target program's enddianness.) */
01917d
   trptr = trbuf;
01917d
-  short_x = 1;
01917d
-  memcpy (trptr, &short_x, 2);
01917d
-  trptr += 2;
01917d
+  tfile_write_16 (1);
01917d
+
01917d
   tfsizeptr = trptr;
01917d
   trptr += 4;
01917d
-  add_memory_block (&testglob, sizeof (testglob));
01917d
+  add_memory_block ((char *) &testglob, sizeof (testglob));
01917d
   /* Divide a variable between two separate memory blocks.  */
01917d
-  add_memory_block (&testglob2, 1);
01917d
+  add_memory_block ((char *) &testglob2, 1);
01917d
   add_memory_block (((char*) &testglob2) + 1, sizeof (testglob2) - 1);
01917d
   /* Go back and patch in the frame size.  */
01917d
   int_x = trptr - tfsizeptr - sizeof (int);
01917d
@@ -158,8 +203,8 @@ write_error_trace_file (void)
01917d
 {
01917d
   int fd;
01917d
   const char made_up[] = "made-up error";
01917d
+  char hex[(sizeof (made_up) - 1) * 2 + 1];
01917d
   int len = sizeof (made_up) - 1;
01917d
-  char *hex = alloca (len * 2 + 1);
01917d
 
01917d
   fd = start_trace_file ("error.tf");
01917d
 
01917d
@@ -183,9 +228,8 @@ write_error_trace_file (void)
01917d
 
01917d
   /* Dump tracepoint definitions, in syntax similar to that used
01917d
      for reconnection uploads.  */
01917d
-  /* FIXME need a portable way to print function address in hex */
01917d
-  snprintf (spbuf, sizeof spbuf, "tp T1:%lx:E:0:0\n",
01917d
-	    (long) &write_basic_trace_file);
01917d
+  snprintf (spbuf, sizeof spbuf, "tp T1:%llx:E:0:0\n",
01917d
+	    (unsigned long long) FUNCTION_ADDRESS (write_basic_trace_file));
01917d
   write (fd, spbuf, strlen (spbuf));
01917d
   /* (Note that we would only need actions defined if we wanted to
01917d
      test tdump.) */
01917d
@@ -210,7 +254,7 @@ done_making_trace_files (void)
01917d
 }
01917d
 
01917d
 int
01917d
-main (int argc, char **argv, char **envp)
01917d
+main (void)
01917d
 {
01917d
   write_basic_trace_file ();
01917d
 
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.trace/tfile.exp
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/testsuite/gdb.trace/tfile.exp
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.trace/tfile.exp
01917d
@@ -26,7 +26,7 @@ gdb_exit
01917d
 gdb_start
01917d
 standard_testfile
01917d
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
01917d
-	  executable {debug nowarnings}] != "" } {
01917d
+	  executable {debug}] != "" } {
01917d
     untested ${testfile}.exp
01917d
     return -1
01917d
 }