xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.threads/create-fail.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2012-2017 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 #include <unistd.h>
31 
32 /* Count the number of tasks/threads in the PID thread group.  */
33 
34 static int
35 count_tasks (pid_t pid)
36 {
37   char path[100];
38   int count;
39   DIR *d;
40 
41   snprintf (path, sizeof (path), "/proc/%d/task/", (int) pid);
42   d = opendir (path);
43   if (d == NULL)
44     return -1;
45 
46   for (count = 0; readdir (d) != NULL; count++)
47     ;
48   closedir (d);
49 
50   /* Account for '.' and '..'.  */
51   assert (count > 2);
52   return count - 2;
53 }
54 
55 pthread_attr_t attr[CPU_SETSIZE];
56 pthread_t thr[CPU_SETSIZE];
57 
58 static void *
59 mythread (void *_arg)
60 {
61   return NULL;
62 }
63 
64 int
65 main ()
66 {
67   int i;
68 
69   for (i = 0; i < CPU_SETSIZE; i++)
70     {
71       cpu_set_t set;
72       int ret;
73 
74       pthread_attr_init (&attr[i]);
75       CPU_ZERO_S (sizeof (set), &set);
76       CPU_SET_S (i, sizeof (set), &set);
77 
78       ret = pthread_attr_setaffinity_np (&attr[i], sizeof (set), &set);
79       if (ret != 0)
80 	{
81 	  fprintf (stderr, "set_affinity: %d: %s\n", ret, strerror (ret));
82 	  exit (3);
83 	}
84       ret = pthread_create (&thr[i], &attr[i], mythread, NULL);
85       /* Should fail with EINVAL at some point.  */
86       if (ret != 0)
87 	{
88 	  unsigned long t;
89 
90 	  fprintf (stderr, "pthread_create: %d: %s\n", ret, strerror (ret));
91 
92 	  /* Wait for all threads to exit.  pthread_create spawns a
93 	     clone thread even in the failing case, as it can only try
94 	     to set the affinity after creating the thread.  That new
95 	     thread is immediately canceled (because setting the
96 	     affinity fails), by killing it with a SIGCANCEL signal,
97 	     which may end up in pthread_cancel/unwind paths, which
98 	     may trigger a libgcc_s.so load, making the thread hit the
99 	     solib-event breakpoint.  Now, if we would let the program
100 	     exit without waiting, sometimes it would happen that the
101 	     inferior exits just while we're handling the solib-event,
102 	     resulting in errors being thrown due to failing ptrace
103 	     call fails (with ESCHR), breaking the test.  */
104 	  t = 16;
105 	  while (count_tasks (getpid ()) > 1)
106 	    {
107 	      usleep (t);
108 
109 	      if (t < 256)
110 		t *= 2;
111 	    }
112 
113 	  /* Normal exit, because this is what we are expecting.  */
114 	  exit (0);
115 	}
116     }
117 
118   /* Should not normally be reached.  */
119   exit (1);
120 }
121