-
Notifications
You must be signed in to change notification settings - Fork 1
/
range-step-crash.c
159 lines (129 loc) · 3.18 KB
/
range-step-crash.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This testcase is part of wrap-syscall.
Copyright 2002-2014 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/>.
This file is copied from gdb/testsuite/gdb.threads/schedlock.c. */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#define NR_THREADS 2
unsigned int counters[NR_THREADS + 1];
/* Threads advance in a round-robin fashion.
This is used to control the advancement.
A thread advances when x == thread#. */
pthread_mutex_t x_mutex;
int x;
sig_atomic_t sigusr1_seen;
void break_here (void) __attribute ((noinline));
int
break_here_before_inc (void)
{
asm volatile ("");
return 0;
}
void
break_here (void)
{
asm volatile ("");
}
int
inc_x (int thread_nr)
{
int result;
pthread_mutex_lock (&x_mutex);
if (x != thread_nr)
result = -1;
else
{
result = ++x;
if (x >= NR_THREADS)
x = 0;
}
pthread_mutex_unlock (&x_mutex);
return result;
}
void *
thread_function (void *arg)
{
int my_number = (long) arg;
int *myp = (int *) &counters[my_number];
static volatile int t;
/* Don't run forever. Run just short of it :) */
while (*myp > 0)
{
t = break_here_before_inc ();
int y = inc_x (my_number);
if (y >= 0)
{
(*myp) ++; usleep (1); /* Loop increment. */
if (my_number == 0)
break_here ();
}
else
{
usleep (1);
}
}
pthread_exit (NULL);
}
static void
sigusr1_handler (int signo, siginfo_t *siginfo, void *exception)
{
sigusr1_seen = 1;
}
int
main ()
{
int res;
pthread_t threads[NR_THREADS];
void *thread_result;
int i;
struct sigaction sigact;
memset (&sigact, 0, sizeof (sigact));
sigact.sa_sigaction = sigusr1_handler;
sigact.sa_flags = SA_RESTART | SA_SIGINFO;
sigemptyset (&sigact.sa_mask);
sigaction (SIGUSR1, &sigact, NULL);
pthread_mutex_init (&x_mutex, NULL);
for (i = 0; i < NR_THREADS; i++)
{
/* The call to usleep is so that when the watchpoint triggers,
the pc is still on the same line. */
counters[i] = 1; usleep (1); /* Init value. */
}
for (i = 0; i < NR_THREADS; i++)
{
res = pthread_create (&threads[i],
NULL,
thread_function,
(void *) (intptr_t) i);
if (res != 0)
{
fprintf (stderr, "error in thread %d create\n", i);
abort ();
}
}
//counters[i] = 1;
//thread_function ((void *) i);
for (i = 0; i < NR_THREADS; ++i)
{
res = pthread_join (threads[i], NULL);
if (res != 0)
{
fprintf (stderr, "error in thread %d join\n", i);
abort ();
}
}
exit (EXIT_SUCCESS);
}