xref: /netbsd-src/external/gpl3/gdb/dist/gdb/testsuite/gdb.threads/step-over-exec.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2020-2023 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 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <pthread.h>
23 
24 #include "../lib/my-syscalls.h"
25 
26 #if (!defined(LEADER_DOES_EXEC) && !defined(OTHER_DOES_EXEC) \
27      || defined(LEADER_DOES_EXEC) && defined(OTHER_DOES_EXEC))
28 # error "Exactly one of LEADER_DOES_EXEC and OTHER_DOES_EXEC must be defined."
29 #endif
30 
31 
32 static char *argv0;
33 static pthread_barrier_t barrier;
34 
35 static void
36 do_the_exec (void)
37 {
38   char *execd_path = (char *) malloc (strlen (argv0) + sizeof ("-execd"));
39   sprintf (execd_path, "%s-execd", argv0);
40   char *argv[] = { execd_path, NULL };
41 
42   printf ("Exec-ing %s\n", execd_path);
43 
44   extern char **environ;
45   my_execve (execd_path, argv, environ);
46 
47   printf ("Exec failed :(\n");
48   abort ();
49 }
50 
51 static void *
52 thread_func (void *arg)
53 {
54   pthread_barrier_wait (&barrier);
55 #ifdef OTHER_DOES_EXEC
56   printf ("Other going in exec.\n");
57   do_the_exec ();
58 #endif
59 
60   /* Just make sure the thread does not exit when the leader does the exec.  */
61   pthread_barrier_wait (&barrier);
62 
63   return NULL;
64 }
65 
66 int
67 main (int argc, char *argv[])
68 {
69   argv0 = argv[0];
70 
71   int ret = pthread_barrier_init (&barrier, NULL, 2);
72   if (ret != 0)
73     abort ();
74 
75   pthread_t thread;
76   ret = pthread_create (&thread, NULL, thread_func, argv[0]);
77   if (ret != 0)
78     abort ();
79 
80   pthread_barrier_wait (&barrier);
81 
82 #ifdef LEADER_DOES_EXEC
83   printf ("Leader going in exec.\n");
84   do_the_exec ();
85 #endif
86 
87   pthread_join (thread, NULL);
88 
89   return 0;
90 }
91