[binutils-gdb] Fix gdb.threads/pthreads.exp error handling/printing

Pedro Alves palves@sourceware.org
Wed Sep 27 14:31:37 GMT 2023


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f3e4716cc534f9521bd97abc400d8e8f0e73ea6a

commit f3e4716cc534f9521bd97abc400d8e8f0e73ea6a
Author: Pedro Alves <pedro@palves.net>
Date:   Fri Sep 15 19:43:55 2023 +0100

    Fix gdb.threads/pthreads.exp error handling/printing
    
    On Cygwin, I noticed:
    
     (gdb) PASS: gdb.threads/pthreads.exp: break thread1
     continue
     Continuing.
     pthread_attr_setscope 1: No error
     [Thread 8732.0x28f8 exited with code 1]
     [Thread 8732.0xb50 exited with code 1]
     [Thread 8732.0x17f8 exited with code 1]
    
     Program terminated with signal SIGHUP, Hangup.
     The program no longer exists.
     (gdb) FAIL: gdb.threads/pthreads.exp: Continue to creation of first thread
    
    Note "No error" in "pthread_attr_setscope 1: No error".  That is a bug
    in the test.  It is using perror, but that prints errno, while the
    pthread functions return the error directly.  Fix all cases of the
    same problem, by adding a new print_error function and using it.
    
    We now get:
    
      ...
      pthread_attr_setscope 1: Not supported (134)
      ...
    
    Approved-By: Tom Tromey <tom@tromey.com>
    Change-Id: I972ebc931b157bc0f9084e6ecd8916a5e39238f5

Diff:
---
 gdb/testsuite/gdb.threads/pthreads.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/gdb/testsuite/gdb.threads/pthreads.c b/gdb/testsuite/gdb.threads/pthreads.c
index e1593e980ea..547bf0fe3f0 100644
--- a/gdb/testsuite/gdb.threads/pthreads.c
+++ b/gdb/testsuite/gdb.threads/pthreads.c
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <pthread.h>
 #include <unistd.h>
+#include <string.h>
 
 static int verbose = 0;
 
@@ -102,6 +103,14 @@ foo (int a, int b, int c)
     printf ("a=%d\n", a);
 }
 
+/* Similar to perror, but use ERR instead of errno.  */
+
+static void
+print_error (const char *ctx, int err)
+{
+  fprintf (stderr, "%s: %s (%d)\n", ctx, strerror (err), err);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -110,38 +119,43 @@ main (int argc, char **argv)
   int t = 0;
   void (*xxx) ();
   pthread_attr_t attr;
+  int res;
 
   if (verbose)
     printf ("pid = %d\n", getpid ());
 
   foo (1, 2, 3);
 
-  if (pthread_attr_init (&attr))
+  res = pthread_attr_init (&attr);
+  if (res != 0)
     {
-      perror ("pthread_attr_init 1");
+      print_error ("pthread_attr_init 1", res);
       exit (1);
     }
 
 #ifdef PTHREAD_SCOPE_SYSTEM
-  if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM))
+  res = pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
+  if (res != 0)
     {
-      perror ("pthread_attr_setscope 1");
+      print_error ("pthread_attr_setscope 1", res);
       exit (1);
     }
 #endif
 
-  if (pthread_create (&tid1, &attr, thread1, (void *) 0xfeedface))
+  res = pthread_create (&tid1, &attr, thread1, (void *) 0xfeedface);
+  if (res != 0)
     {
-      perror ("pthread_create 1");
+      print_error ("pthread_create 1", res);
       exit (1);
     }
   if (verbose)
     printf ("Made thread %ld\n", (long) tid1);
   sleep (1);
 
-  if (pthread_create (&tid2, NULL, thread2, (void *) 0xdeadbeef))
+  res = pthread_create (&tid2, NULL, thread2, (void *) 0xdeadbeef);
+  if (res != 0)
     {
-      perror ("pthread_create 2");
+      print_error ("pthread_create 2", res);
       exit (1);
     }
   if (verbose)


More information about the Gdb-cvs mailing list