]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.mi/non-stop.c
d329df53a13f7cb46d317945cfe0e78ac79d6206
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.mi / non-stop.c
1 /* Test program for non-stop debugging.
2 Copyright 1996-2019 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <pthread.h>
22 #include <unistd.h>
23
24 int exit_first_thread = 0;
25
26 void break_at_me (int id, int i)
27 {
28 }
29
30 void *
31 worker (void *arg)
32 {
33 int id = *(int *)arg;
34 int i = 0;
35
36 /* When gdb is running, it sets hidden breakpoints in the thread
37 library. The signals caused by these hidden breakpoints can
38 cause system calls such as 'sleep' to return early. Pay attention
39 to the return value from 'sleep' to get the full sleep. */
40 for (;;++i)
41 {
42 int unslept = 1;
43 while (unslept > 0)
44 unslept = sleep (unslept);
45
46 if (exit_first_thread && id == 0)
47 return NULL;
48
49 break_at_me (id, i);
50 }
51 }
52
53 pthread_t
54 create_thread (int id)
55 {
56 pthread_t tid;
57 /* This memory will be leaked, we don't care for a test. */
58 int *id2 = malloc (sizeof (int));
59 *id2 = id;
60
61 if (pthread_create (&tid, NULL, worker, (void *) id2))
62 {
63 perror ("pthread_create 1");
64 exit (1);
65 }
66 return tid;
67 }
68
69 int
70 main (int argc, char *argv[])
71 {
72 pthread_t tid;
73 create_thread (0);
74 sleep (1);
75 tid = create_thread (1);
76 pthread_join (tid, NULL);
77
78 return 0;
79 }
80