td_ta_thr_iter.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Iterate over a process's threads.
  2. Copyright (C) 1999,2000,2001,2002,2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "thread_dbP.h"
  17. #include <alloca.h>
  18. static int
  19. handle_descr (const td_thragent_t *ta, td_thr_iter_f *callback,
  20. void *cbdata_p, td_thr_state_e state, int ti_pri,
  21. size_t cnt, pthread_descr descr)
  22. {
  23. struct _pthread_descr_struct pds;
  24. size_t sizeof_descr = ta->sizeof_descr;
  25. td_thrhandle_t th;
  26. if (descr == NULL)
  27. {
  28. /* No descriptor (yet). */
  29. if (cnt == 0)
  30. {
  31. /* This is the main thread. Create a fake descriptor. */
  32. memset (&pds, '\0', sizeof (pds));
  33. /* Empty thread descriptor the thread library would create. */
  34. pds.p_self = &pds;
  35. pds.p_nextlive = pds.p_prevlive = &pds;
  36. pds.p_tid = PTHREAD_THREADS_MAX;
  37. /* The init code also sets up p_lock, p_errnop, p_herrnop, and
  38. p_userstack but this should not be necessary here. */
  39. th.th_ta_p = (td_thragent_t *) ta;
  40. th.th_unique = NULL;
  41. if (callback (&th, cbdata_p) != 0)
  42. return TD_DBERR;
  43. /* All done successfully. */
  44. return TD_OK;
  45. }
  46. else if (cnt == 1)
  47. /* The manager is not yet started. No big deal. */
  48. return TD_OK;
  49. else
  50. /* For every other thread this should not happen. */
  51. return TD_ERR;
  52. }
  53. if (ps_pdread (ta->ph, descr, &pds, sizeof_descr) != PS_OK)
  54. return TD_ERR; /* XXX Other error value? */
  55. /* The manager thread must be handled special. The descriptor
  56. exists but the thread only gets created when the first
  57. `pthread_create' call is issued. A clear indication that this
  58. happened is when the p_pid field is non-zero. */
  59. if (cnt == 1 && pds.p_pid == 0)
  60. return TD_OK;
  61. /* Now test whether this thread matches the specified
  62. conditions. */
  63. /* Only if the priority level is as high or higher. */
  64. if (pds.p_priority < ti_pri)
  65. return TD_OK;
  66. /* Test the state.
  67. XXX This is incomplete. */
  68. if (state != TD_THR_ANY_STATE)
  69. return TD_OK;
  70. /* XXX For now we ignore threads which are not running anymore.
  71. The reason is that gdb tries to get the registers and fails.
  72. In future we should have a special mode of the thread library
  73. in which we keep the process around until the actual join
  74. operation happened. */
  75. if (pds.p_exited != 0)
  76. return TD_OK;
  77. /* Yep, it matches. Call the callback function. */
  78. th.th_ta_p = (td_thragent_t *) ta;
  79. th.th_unique = descr;
  80. if (callback (&th, cbdata_p) != 0)
  81. return TD_DBERR;
  82. /* All done successfully. */
  83. return TD_OK;
  84. }
  85. td_err_e
  86. td_ta_thr_iter (const td_thragent_t *ta, td_thr_iter_f *callback,
  87. void *cbdata_p, td_thr_state_e state, int ti_pri,
  88. sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
  89. {
  90. int pthread_threads_max;
  91. struct pthread_handle_struct *phc;
  92. td_err_e result = TD_OK;
  93. int cnt;
  94. #ifdef ALL_THREADS_STOPPED
  95. int num;
  96. #else
  97. # define num 1
  98. #endif
  99. LOG ("td_ta_thr_iter");
  100. /* Test whether the TA parameter is ok. */
  101. if (! ta_ok (ta))
  102. return TD_BADTA;
  103. pthread_threads_max = ta->pthread_threads_max;
  104. phc = (struct pthread_handle_struct *) alloca (sizeof (phc[0])
  105. * pthread_threads_max);
  106. /* First read only the main thread and manager thread information. */
  107. if (ps_pdread (ta->ph, ta->handles, phc,
  108. sizeof (struct pthread_handle_struct) * 2) != PS_OK)
  109. return TD_ERR; /* XXX Other error value? */
  110. /* Now handle these descriptors. */
  111. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 0,
  112. phc[0].h_descr);
  113. if (result != TD_OK)
  114. return result;
  115. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 1,
  116. phc[1].h_descr);
  117. if (result != TD_OK)
  118. return result;
  119. /* Read all the descriptors. */
  120. if (ps_pdread (ta->ph, ta->handles + 2, &phc[2],
  121. (sizeof (struct pthread_handle_struct)
  122. * (pthread_threads_max - 2))) != PS_OK)
  123. return TD_ERR; /* XXX Other error value? */
  124. #ifdef ALL_THREADS_STOPPED
  125. /* Read the number of currently active threads. */
  126. if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
  127. return TD_ERR; /* XXX Other error value? */
  128. #endif
  129. /* Now get all descriptors, one after the other. */
  130. for (cnt = 2; cnt < pthread_threads_max && num > 0; ++cnt)
  131. if (phc[cnt].h_descr != NULL)
  132. {
  133. #ifdef ALL_THREADS_STOPPED
  134. /* First count this active thread. */
  135. --num;
  136. #endif
  137. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, cnt,
  138. phc[cnt].h_descr);
  139. if (result != TD_OK)
  140. break;
  141. }
  142. return result;
  143. }