aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/step-over-exec.c
blob: a917507c84a9d20430ddf990e51c8488668a26c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* This testcase is part of GDB, the GNU debugger.

   Copyright 2020-2023 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include "../lib/my-syscalls.h"

#if (!defined(LEADER_DOES_EXEC) && !defined(OTHER_DOES_EXEC) \
     || defined(LEADER_DOES_EXEC) && defined(OTHER_DOES_EXEC))
# error "Exactly one of LEADER_DOES_EXEC and OTHER_DOES_EXEC must be defined."
#endif


static char *argv0;
static pthread_barrier_t barrier;

static void
do_the_exec (void)
{
  char *execd_path = (char *) malloc (strlen (argv0) + sizeof ("-execd"));
  sprintf (execd_path, "%s-execd", argv0);
  char *argv[] = { execd_path, NULL };

  printf ("Exec-ing %s\n", execd_path);

  extern char **environ;
  my_execve (execd_path, argv, environ);

  printf ("Exec failed :(\n");
  abort ();
}

static void *
thread_func (void *arg)
{
  pthread_barrier_wait (&barrier);
#ifdef OTHER_DOES_EXEC
  printf ("Other going in exec.\n");
  do_the_exec ();
#endif

  /* Just make sure the thread does not exit when the leader does the exec.  */
  pthread_barrier_wait (&barrier);

  return NULL;
}

int
main (int argc, char *argv[])
{
  argv0 = argv[0];

  int ret = pthread_barrier_init (&barrier, NULL, 2);
  if (ret != 0)
    abort ();

  pthread_t thread;
  ret = pthread_create (&thread, NULL, thread_func, argv[0]);
  if (ret != 0)
    abort ();

  pthread_barrier_wait (&barrier);

#ifdef LEADER_DOES_EXEC
  printf ("Leader going in exec.\n");
  do_the_exec ();
#endif

  pthread_join (thread, NULL);

  return 0;
}