aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/current-lwp-dead.c
blob: 3a817c3cdd4f43294bf5a8ca07f1cb0a25208e1b (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* This testcase is part of GDB, the GNU debugger.

   Copyright 2009-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/>.


   The original issue we're trying to test is described in this
   thread:

     https://sourceware.org/legacy-ml/gdb-patches/2009-06/msg00802.html

   The NEW_THREAD_EVENT code the comments below refer to no longer
   exists in GDB, so the following comments are kept for historical
   reasons, and to guide future updates to the testcase.

   ---

   Do not use threads as we need to exploit a bug in LWP code masked by the
   threads code otherwise.

   INFERIOR_PTID must point to exited LWP.  Here we use the initial LWP as it
   is automatically INFERIOR_PTID for GDB.

   Finally we need to call target_resume (RESUME_ALL, ...) which we invoke by
   NEW_THREAD_EVENT (called from the new LWP as initial LWP is exited now).  */

#define _GNU_SOURCE
#include <sched.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

#define STACK_SIZE 0x1000

/* True if the 'fn_return' thread has been reached at the point after
   its parent is already gone.  */
volatile int fn_return_reached = 0;

/* True if the 'fn' thread has exited.  */
volatile int fn_exited = 0;

/* Wrapper around clone.  */

static int
do_clone (int (*fn)(void *))
{
  unsigned char *stack;
  int new_pid;

  stack = malloc (STACK_SIZE);
  assert (stack != NULL);

  new_pid = clone (fn, stack + STACK_SIZE, CLONE_FILES | CLONE_VM,
		   NULL, NULL, NULL, NULL);
  assert (new_pid > 0);

  return new_pid;
}

static int
fn_return (void *unused)
{
  /* Wait until our direct parent exits.  We want the breakpoint set a
     couple lines below to hit with the previously-selected thread
     gone.  */
  while (!fn_exited)
    usleep (1);

  fn_return_reached = 1; /* at-fn_return */
  return 0;
}

static int
fn (void *unused)
{
  do_clone (fn_return);
  return 0;
}

int
main (int argc, char **argv)
{
  int new_pid, status, ret;

  new_pid = do_clone (fn);

  /* Note the clone call above didn't use CLONE_THREAD, so it actually
     put the new child in a new thread group.  However, the new clone
     is still reported with PTRACE_EVENT_CLONE to GDB, since we didn't
     use CLONE_VFORK (results in PTRACE_EVENT_VFORK) nor set the
     termination signal to SIGCHLD (results in PTRACE_EVENT_FORK), so
     GDB thinks of it as a new thread of the same inferior.  It's a
     bit of an odd setup, but it's not important for what we're
     testing, and, it let's us conveniently use waitpid to wait for
     the child, which you can't with CLONE_THREAD.  */
  ret = waitpid (new_pid, &status, __WALL);
  assert (ret == new_pid);
  assert (WIFEXITED (status) && WEXITSTATUS (status) == 0);

  fn_exited = 1;

  /* Don't exit before the breakpoint at fn_return triggers.  */
  while (!fn_return_reached)
    usleep (1);

  return 0;
}