xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.threads/create-fail.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2012-2015 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #define _GNU_SOURCE
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sched.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <assert.h>
30 
31 /* Count the number of tasks/threads in the PID thread group.  */
32 
33 static int
34 count_tasks (pid_t pid)
35 {
36   char path[100];
37   int count;
38   DIR *d;
39 
40   snprintf (path, sizeof (path), "/proc/%d/task/", (int) pid);
41   d = opendir (path);
42   if (d == NULL)
43     return -1;
44 
45   for (count = 0; readdir (d) != NULL; count++)
46     ;
47   closedir (d);
48 
49   /* Account for '.' and '..'.  */
50   assert (count > 2);
51   return count - 2;
52 }
53 
54 pthread_attr_t attr[CPU_SETSIZE];
55 pthread_t thr[CPU_SETSIZE];
56 
57 static void *
58 mythread (void *_arg)
59 {
60   return NULL;
61 }
62 
63 int
64 main ()
65 {
66   int i;
67 
68   for (i = 0; i < CPU_SETSIZE; i++)
69     {
70       cpu_set_t set;
71       int ret;
72 
73       pthread_attr_init (&attr[i]);
74       CPU_ZERO_S (sizeof (set), &set);
75       CPU_SET_S (i, sizeof (set), &set);
76 
77       ret = pthread_attr_setaffinity_np (&attr[i], sizeof (set), &set);
78       if (ret != 0)
79 	{
80 	  fprintf (stderr, "set_affinity: %d: %s\n", ret, strerror (ret));
81 	  exit (3);
82 	}
83       ret = pthread_create (&thr[i], &attr[i], mythread, NULL);
84       /* Should fail with EINVAL at some point.  */
85       if (ret != 0)
86 	{
87 	  unsigned long t;
88 
89 	  fprintf (stderr, "pthread_create: %d: %s\n", ret, strerror (ret));
90 
91 	  /* Wait for all threads to exit.  pthread_create spawns a
92 	     clone thread even in the failing case, as it can only try
93 	     to set the affinity after creating the thread.  That new
94 	     thread is immediately canceled (because setting the
95 	     affinity fails), by killing it with a SIGCANCEL signal,
96 	     which may end up in pthread_cancel/unwind paths, which
97 	     may trigger a libgcc_s.so load, making the thread hit the
98 	     solib-event breakpoint.  Now, if we would let the program
99 	     exit without waiting, sometimes it would happen that the
100 	     inferior exits just while we're handling the solib-event,
101 	     resulting in errors being thrown due to failing ptrace
102 	     call fails (with ESCHR), breaking the test.  */
103 	  t = 16;
104 	  while (count_tasks (getpid ()) > 1)
105 	    {
106 	      usleep (t);
107 
108 	      if (t < 256)
109 		t *= 2;
110 	    }
111 
112 	  /* Normal exit, because this is what we are expecting.  */
113 	  exit (0);
114 	}
115     }
116 
117   /* Should not normally be reached.  */
118   exit (1);
119 }
120