~ubuntu-branches/debian/jessie/gdb/jessie

« back to all changes in this revision

Viewing changes to gdb/testsuite/gdb.threads/watchthreads-reorder.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2010-03-20 01:21:29 UTC
  • mfrom: (1.3.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100320012129-t7h25y8zgr8c2369
Tags: 7.1-1
* New upstream release, including:
  - PIE support (Closes: #346409).
  - C++ improvements, including static_cast<> et al, namespace imports,
    and bug fixes in printing virtual base classes.
  - Multi-program debugging.  One GDB can now debug multiple programs
    at the same time.
  - Python scripting improvements, including gdb.parse_and_eval.
  - Updated MIPS Linux signal frame layout (Closes: #570875).
  - No internal error stepping over _dl_debug_state (Closes: #569551).
* Update to Standards-Version: 3.8.4 (no changes required).
* Include more relevant (and smaller) docs in the gdbserver package
  (Closes: #571132).
* Do not duplicate documentation in gdb64, gdb-source, and libgdb-dev.
* Fix crash when switching into TUI mode (Closes: #568489).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This testcase is part of GDB, the GNU debugger.
 
2
 
 
3
   Copyright 2009, 2010 Free Software Foundation, Inc.
 
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
#include <pthread.h>
 
20
#include <stdio.h>
 
21
#include <limits.h>
 
22
#include <errno.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <assert.h>
 
26
#include <sys/types.h>
 
27
#include <signal.h>
 
28
#include <unistd.h>
 
29
#include <asm/unistd.h>
 
30
 
 
31
#define gettid() syscall (__NR_gettid)
 
32
 
 
33
/* Terminate always in the main task, it can lock up with SIGSTOPped GDB
 
34
   otherwise.  */
 
35
#define TIMEOUT (gettid () == getpid() ? 10 : 15)
 
36
 
 
37
static pid_t thread1_tid;
 
38
static pthread_cond_t thread1_tid_cond = PTHREAD_COND_INITIALIZER;
 
39
static pthread_mutex_t thread1_tid_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
 
40
 
 
41
static pid_t thread2_tid;
 
42
static pthread_cond_t thread2_tid_cond = PTHREAD_COND_INITIALIZER;
 
43
static pthread_mutex_t thread2_tid_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
 
44
 
 
45
static pthread_mutex_t terminate_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
 
46
 
 
47
/* These variables must have lower in-memory addresses than thread1_rwatch and
 
48
   thread2_rwatch so that they take their watchpoint slots.  */
 
49
 
 
50
static int unused1_rwatch;
 
51
static int unused2_rwatch;
 
52
 
 
53
static volatile int thread1_rwatch;
 
54
static volatile int thread2_rwatch;
 
55
 
 
56
/* Do not use alarm as it would create a ptrace event which would hang up us if
 
57
   we are being traced by GDB which we stopped ourselves.  */
 
58
 
 
59
static void timed_mutex_lock (pthread_mutex_t *mutex)
 
60
{
 
61
  int i;
 
62
  struct timespec start, now;
 
63
 
 
64
  i = clock_gettime (CLOCK_MONOTONIC, &start);
 
65
  assert (i == 0);
 
66
 
 
67
  do
 
68
    {
 
69
      i = pthread_mutex_trylock (mutex);
 
70
      if (i == 0)
 
71
        return;
 
72
      assert (i == EBUSY);
 
73
 
 
74
      i = clock_gettime (CLOCK_MONOTONIC, &now);
 
75
      assert (i == 0);
 
76
      assert (now.tv_sec >= start.tv_sec);
 
77
    }
 
78
  while (now.tv_sec - start.tv_sec < TIMEOUT);
 
79
 
 
80
  fprintf (stderr, "Timed out waiting for internal lock!\n");
 
81
  exit (EXIT_FAILURE);
 
82
}
 
83
 
 
84
static void *
 
85
thread1_func (void *unused)
 
86
{
 
87
  int i;
 
88
  volatile int rwatch_store;
 
89
 
 
90
  timed_mutex_lock (&thread1_tid_mutex);
 
91
 
 
92
  /* THREAD1_TID_MUTEX must be already locked to avoid race.  */
 
93
  thread1_tid = gettid ();
 
94
 
 
95
  i = pthread_cond_signal (&thread1_tid_cond);
 
96
  assert (i == 0);
 
97
  i = pthread_mutex_unlock (&thread1_tid_mutex);
 
98
  assert (i == 0);
 
99
 
 
100
  rwatch_store = thread1_rwatch;
 
101
 
 
102
  /* Be sure the "T (tracing stop)" test can proceed for both threads.  */
 
103
  timed_mutex_lock (&terminate_mutex);
 
104
  i = pthread_mutex_unlock (&terminate_mutex);
 
105
  assert (i == 0);
 
106
 
 
107
  return NULL;
 
108
}
 
109
 
 
110
static void *
 
111
thread2_func (void *unused)
 
112
{
 
113
  int i;
 
114
  volatile int rwatch_store;
 
115
 
 
116
  timed_mutex_lock (&thread2_tid_mutex);
 
117
 
 
118
  /* THREAD2_TID_MUTEX must be already locked to avoid race.  */
 
119
  thread2_tid = gettid ();
 
120
 
 
121
  i = pthread_cond_signal (&thread2_tid_cond);
 
122
  assert (i == 0);
 
123
  i = pthread_mutex_unlock (&thread2_tid_mutex);
 
124
  assert (i == 0);
 
125
 
 
126
  rwatch_store = thread2_rwatch;
 
127
 
 
128
  /* Be sure the "T (tracing stop)" test can proceed for both threads.  */
 
129
  timed_mutex_lock (&terminate_mutex);
 
130
  i = pthread_mutex_unlock (&terminate_mutex);
 
131
  assert (i == 0);
 
132
 
 
133
  return NULL;
 
134
}
 
135
 
 
136
static const char *
 
137
proc_string (const char *filename, const char *line)
 
138
{
 
139
  FILE *f;
 
140
  static char buf[LINE_MAX];
 
141
  size_t line_len = strlen (line);
 
142
 
 
143
  f = fopen (filename, "r");
 
144
  if (f == NULL)
 
145
    {
 
146
      fprintf (stderr, "fopen (\"%s\") for \"%s\": %s\n", filename, line,
 
147
               strerror (errno));
 
148
      exit (EXIT_FAILURE);
 
149
    }
 
150
  while (errno = 0, fgets (buf, sizeof (buf), f))
 
151
    {
 
152
      char *s;
 
153
 
 
154
      s = strchr (buf, '\n');
 
155
      assert (s != NULL);
 
156
      *s = 0;
 
157
 
 
158
      if (strncmp (buf, line, line_len) != 0)
 
159
        continue;
 
160
 
 
161
      if (fclose (f))
 
162
        {
 
163
          fprintf (stderr, "fclose (\"%s\") for \"%s\": %s\n", filename, line,
 
164
                   strerror (errno));
 
165
          exit (EXIT_FAILURE);
 
166
        }
 
167
 
 
168
      return &buf[line_len];
 
169
    }
 
170
  if (errno != 0)
 
171
    {
 
172
      fprintf (stderr, "fgets (\"%s\": %s\n", filename, strerror (errno));
 
173
      exit (EXIT_FAILURE);
 
174
    }
 
175
  fprintf (stderr, "\"%s\": No line \"%s\" found.\n", filename, line);
 
176
  exit (EXIT_FAILURE);
 
177
}
 
178
 
 
179
static unsigned long
 
180
proc_ulong (const char *filename, const char *line)
 
181
{
 
182
  const char *s = proc_string (filename, line);
 
183
  long retval;
 
184
  char *end;
 
185
 
 
186
  errno = 0;
 
187
  retval = strtol (s, &end, 10);
 
188
  if (retval < 0 || retval >= LONG_MAX || (end && *end))
 
189
    {
 
190
      fprintf (stderr, "\"%s\":\"%s\": %ld, %s\n", filename, line, retval,
 
191
               strerror (errno));
 
192
      exit (EXIT_FAILURE);
 
193
    }
 
194
  return retval;
 
195
}
 
196
 
 
197
static void
 
198
state_wait (pid_t process, const char *wanted)
 
199
{
 
200
  char *filename;
 
201
  int i;
 
202
  struct timespec start, now;
 
203
  const char *state;
 
204
 
 
205
  i = asprintf (&filename, "/proc/%lu/status", (unsigned long) process);
 
206
  assert (i > 0);
 
207
 
 
208
  i = clock_gettime (CLOCK_MONOTONIC, &start);
 
209
  assert (i == 0);
 
210
 
 
211
  do
 
212
    {
 
213
      state = proc_string (filename, "State:\t");
 
214
      if (strcmp (state, wanted) == 0)
 
215
        {
 
216
          free (filename);
 
217
          return;
 
218
        }
 
219
 
 
220
      if (sched_yield ())
 
221
        {
 
222
          perror ("sched_yield()");
 
223
          exit (EXIT_FAILURE);
 
224
        }
 
225
 
 
226
      i = clock_gettime (CLOCK_MONOTONIC, &now);
 
227
      assert (i == 0);
 
228
      assert (now.tv_sec >= start.tv_sec);
 
229
    }
 
230
  while (now.tv_sec - start.tv_sec < TIMEOUT);
 
231
 
 
232
  fprintf (stderr, "Timed out waiting for PID %lu \"%s\" (now it is \"%s\")!\n",
 
233
           (unsigned long) process, wanted, state);
 
234
  exit (EXIT_FAILURE);
 
235
}
 
236
 
 
237
static volatile pid_t tracer = 0;
 
238
static pthread_t thread1, thread2;
 
239
 
 
240
static void
 
241
cleanup (void)
 
242
{
 
243
  printf ("Resuming GDB PID %lu.\n", (unsigned long) tracer);
 
244
 
 
245
  if (tracer)
 
246
    {
 
247
      int i;
 
248
      int tracer_save = tracer;
 
249
 
 
250
      tracer = 0;
 
251
 
 
252
      i = kill (tracer_save, SIGCONT);
 
253
      assert (i == 0);
 
254
    }
 
255
}
 
256
 
 
257
int
 
258
main (int argc, char **argv)
 
259
{
 
260
  int i;
 
261
  int standalone = 0;
 
262
 
 
263
  if (argc == 2 && strcmp (argv[1], "-s") == 0)
 
264
    standalone = 1;
 
265
  else
 
266
    assert (argc == 1);
 
267
 
 
268
  setbuf (stdout, NULL);
 
269
 
 
270
  timed_mutex_lock (&thread1_tid_mutex);
 
271
  timed_mutex_lock (&thread2_tid_mutex);
 
272
 
 
273
  timed_mutex_lock (&terminate_mutex);
 
274
 
 
275
  i = pthread_create (&thread1, NULL, thread1_func, NULL);
 
276
  assert (i == 0);
 
277
 
 
278
  i = pthread_create (&thread2, NULL, thread2_func, NULL);
 
279
  assert (i == 0);
 
280
 
 
281
  if (!standalone)
 
282
    {
 
283
      tracer = proc_ulong ("/proc/self/status", "TracerPid:\t");
 
284
      if (tracer == 0)
 
285
        {
 
286
          fprintf (stderr, "The testcase must be run by GDB!\n");
 
287
          exit (EXIT_FAILURE);
 
288
        }
 
289
      if (tracer != getppid ())
 
290
        {
 
291
          fprintf (stderr, "The testcase parent must be our GDB tracer!\n");
 
292
          exit (EXIT_FAILURE);
 
293
        }
 
294
    }
 
295
 
 
296
  /* SIGCONT our debugger in the case of our crash as we would deadlock
 
297
     otherwise.  */
 
298
 
 
299
  atexit (cleanup);
 
300
 
 
301
  printf ("Stopping GDB PID %lu.\n", (unsigned long) tracer);
 
302
 
 
303
  if (tracer)
 
304
    {
 
305
      i = kill (tracer, SIGSTOP);
 
306
      assert (i == 0);
 
307
      state_wait (tracer, "T (stopped)");
 
308
    }
 
309
 
 
310
  /* Threads are now waiting at timed_mutex_lock (thread1_tid_mutex) and so
 
311
     they could not trigger the watchpoints before GDB gets unstopped later.
 
312
     Threads get resumed at pthread_cond_wait below.  Use `while' loops for
 
313
     protection against spurious pthread_cond_wait wakeups.  */
 
314
 
 
315
  printf ("Waiting till the threads initialize their TIDs.\n");
 
316
 
 
317
  while (thread1_tid == 0)
 
318
    {
 
319
      i = pthread_cond_wait (&thread1_tid_cond, &thread1_tid_mutex);
 
320
      assert (i == 0);
 
321
    }
 
322
 
 
323
  while (thread2_tid == 0)
 
324
    {
 
325
      i = pthread_cond_wait (&thread2_tid_cond, &thread2_tid_mutex);
 
326
      assert (i == 0);
 
327
    }
 
328
 
 
329
  printf ("Thread 1 TID = %lu, thread 2 TID = %lu, PID = %lu.\n",
 
330
          (unsigned long) thread1_tid, (unsigned long) thread2_tid,
 
331
          (unsigned long) getpid ());
 
332
 
 
333
  printf ("Waiting till the threads get trapped by the watchpoints.\n");
 
334
 
 
335
  if (tracer)
 
336
    {
 
337
      /* s390x-unknown-linux-gnu will fail with "R (running)".  */
 
338
 
 
339
      state_wait (thread1_tid, "T (tracing stop)");
 
340
 
 
341
      state_wait (thread2_tid, "T (tracing stop)");
 
342
    }
 
343
 
 
344
  cleanup ();
 
345
 
 
346
  printf ("Joining the threads.\n");
 
347
 
 
348
  i = pthread_mutex_unlock (&terminate_mutex);
 
349
  assert (i == 0);
 
350
 
 
351
  i = pthread_join (thread1, NULL);
 
352
  assert (i == 0);
 
353
 
 
354
  i = pthread_join (thread2, NULL);
 
355
  assert (i == 0);
 
356
 
 
357
  printf ("Exiting.\n");        /* break-at-exit */
 
358
 
 
359
  /* Just prevent compiler `warning: unusedX_rwatch defined but not used'.  */
 
360
  unused1_rwatch = 1;
 
361
  unused2_rwatch = 2;
 
362
 
 
363
  return EXIT_SUCCESS;
 
364
}