]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/traced-thread.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / traced-thread.c
CommitLineData
c6f7f9c8
TT
1/* This testcase is part of GDB, the GNU debugger.
2
1d506c26 3 Copyright 2023-2024 Free Software Foundation, Inc.
c6f7f9c8
TT
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
20#include <assert.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <sys/ptrace.h>
24#include <pthread.h>
25#include <stdlib.h>
26#include <errno.h>
27
28int fds[2];
29
30_Atomic pid_t bg_tid = 0;
31
32pthread_barrier_t barrier;
33
34#define FIVE_MINUTES (5 * 60)
35
36/* One thread of the child process. This is traced by the parent
37 process. */
38void *
39block (void *ignore)
40{
41 bg_tid = gettid ();
42 pthread_barrier_wait (&barrier);
43 sleep (FIVE_MINUTES);
44 return 0;
45}
46
47/* The parent process blocks in this function. */
48void
49parent_stop (pid_t child_thread_pid)
50{
51 sleep (FIVE_MINUTES);
52}
53
54int
55main ()
56{
57 int result;
58
59 pthread_barrier_init (&barrier, NULL, 2);
60
61 result = pipe (fds);
62 assert (result != -1);
63
64 pid_t child = fork ();
65 if (child != 0)
66 {
67 /* Parent. */
68 close (fds[1]);
69
70 pid_t the_tid;
71 result = read (fds[0], &the_tid, sizeof (the_tid));
72 assert (result == sizeof (the_tid));
73
74 /* Trace a single, non-main thread of the child. This should
75 prevent gdb from attaching to the child at all. The bug here
76 was that gdb would get into an infinite loop repeatedly
77 trying to attach to this thread. */
78 result = ptrace (PTRACE_SEIZE, the_tid, (void *) 0, (void *) 0);
79 if (result == -1)
80 perror ("ptrace");
81
82 parent_stop (child);
83 }
84 else
85 {
86 /* Child. */
87
88 close (fds[0]);
89
90 pthread_t thr;
91 result = pthread_create (&thr, 0, block, 0);
92 assert (result == 0);
93
94 /* Wait until the TID has been assigned. */
95 pthread_barrier_wait (&barrier);
96 assert (bg_tid != 0);
97
98 result = write (fds[1], &bg_tid, sizeof (bg_tid));
99 assert (result == sizeof (bg_tid));
100
101 sleep (FIVE_MINUTES);
102 }
103
104 exit (0);
105}