wp-replication.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* This testcase is part of GDB, the GNU debugger.
  2. Copyright 2009-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. Check that hardware watchpoints get correctly replicated to all
  14. existing threads when hardware watchpoints are created. This test
  15. creates one hardware watchpoint per thread until a maximum is
  16. reached. It originally addresses a deficiency seen on embedded
  17. powerpc targets with slotted hardware *point designs.
  18. */
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <pthread.h>
  24. #ifndef NR_THREADS
  25. #define NR_THREADS 4 /* Set by the testcase. */
  26. #endif
  27. #ifndef X_INCR_COUNT
  28. #define X_INCR_COUNT 10 /* Set by the testcase. */
  29. #endif
  30. void *thread_function (void *arg); /* Function executed by each thread. */
  31. /* Used to hold threads back until wp-replication.exp is ready. */
  32. int test_ready = 0;
  33. /* Used to hold threads back until every thread has had a chance of causing
  34. a watchpoint trigger. This prevents a situation in GDB where it may miss
  35. watchpoint triggers when threads exit while other threads are causing
  36. watchpoint triggers. */
  37. int can_terminate = 0;
  38. /* Number of watchpoints GDB is capable of using (this is provided
  39. by GDB during the test run). */
  40. int hw_watch_count = 0;
  41. /* Array with elements we can create watchpoints for. */
  42. static int watched_data[NR_THREADS];
  43. pthread_mutex_t data_mutex;
  44. int
  45. main ()
  46. {
  47. int res;
  48. pthread_t threads[NR_THREADS];
  49. int i;
  50. pthread_mutex_init (&data_mutex, NULL);
  51. for (i = 0; i < NR_THREADS; i++)
  52. {
  53. res = pthread_create (&threads[i],
  54. NULL, thread_function,
  55. (void *) (intptr_t) i);
  56. if (res != 0)
  57. {
  58. fprintf (stderr, "error in thread %d create\n", i);
  59. abort ();
  60. }
  61. }
  62. for (i = 0; i < NR_THREADS; ++i)
  63. {
  64. res = pthread_join (threads[i], NULL);
  65. if (res != 0)
  66. {
  67. fprintf (stderr, "error in thread %d join\n", i);
  68. abort ();
  69. }
  70. }
  71. exit (EXIT_SUCCESS);
  72. }
  73. /* Easy place for a breakpoint.
  74. wp-replication.exp uses this to track when all threads are running
  75. instead of, for example, the program keeping track
  76. because we don't need the program to know when all threads are running,
  77. instead we need gdb to know when all threads are running.
  78. There is a delay between when a thread has started and when the thread
  79. has been registered with gdb. */
  80. void
  81. thread_started (void)
  82. {
  83. }
  84. void *
  85. thread_function (void *arg)
  86. {
  87. int i, j;
  88. long thread_number = (long) arg;
  89. thread_started ();
  90. /* Don't start incrementing X until wp-replication.exp is ready. */
  91. while (!test_ready)
  92. usleep (1);
  93. pthread_mutex_lock (&data_mutex);
  94. for (i = 0; i < NR_TRIGGERS_PER_THREAD; i++)
  95. {
  96. for (j = 0; j < hw_watch_count; j++)
  97. {
  98. /* For debugging. */
  99. printf ("Thread %ld changing watch_thread[%d] data"
  100. " from %d -> %d\n", thread_number, j,
  101. watched_data[j], watched_data[j] + 1);
  102. /* Increment the watched data field. */
  103. watched_data[j]++;
  104. }
  105. }
  106. pthread_mutex_unlock (&data_mutex);
  107. /* Hold the threads here to work around a problem GDB has evaluating
  108. watchpoints right when a DSO event shows up (PR breakpoints/10116).
  109. Sleep a little longer (than, say, 1, 5 or 10) to avoid consuming
  110. lots of cycles while the other threads are trying to execute the
  111. loop. */
  112. while (!can_terminate)
  113. usleep (100);
  114. pthread_exit (NULL);
  115. }