td_ta_thr_iter.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include "thread_dbP.h"
  18. #include <alloca.h>
  19. static int
  20. handle_descr (const td_thragent_t *ta, td_thr_iter_f *callback,
  21. void *cbdata_p, td_thr_state_e state, int ti_pri,
  22. size_t cnt, pthread_descr descr)
  23. {
  24. struct _pthread_descr_struct pds;
  25. size_t sizeof_descr = ta->sizeof_descr;
  26. td_thrhandle_t th;
  27. if (descr == NULL)
  28. {
  29. /* No descriptor (yet). */
  30. if (cnt == 0)
  31. {
  32. /* This is the main thread. Create a fake descriptor. */
  33. memset (&pds, '\0', sizeof (pds));
  34. /* Empty thread descriptor the thread library would create. */
  35. pds.p_self = &pds;
  36. pds.p_nextlive = pds.p_prevlive = &pds;
  37. pds.p_tid = PTHREAD_THREADS_MAX;
  38. /* The init code also sets up p_lock, p_errnop, p_herrnop, and
  39. p_userstack but this should not be necessary here. */
  40. th.th_ta_p = (td_thragent_t *) ta;
  41. th.th_unique = NULL;
  42. if (callback (&th, cbdata_p) != 0)
  43. return TD_DBERR;
  44. /* All done successfully. */
  45. return TD_OK;
  46. }
  47. else if (cnt == 1)
  48. /* The manager is not yet started. No big deal. */
  49. return TD_OK;
  50. else
  51. /* For every other thread this should not happen. */
  52. return TD_ERR;
  53. }
  54. if (ps_pdread (ta->ph, descr, &pds, sizeof_descr) != PS_OK)
  55. return TD_ERR; /* XXX Other error value? */
  56. /* The manager thread must be handled special. The descriptor
  57. exists but the thread only gets created when the first
  58. `pthread_create' call is issued. A clear indication that this
  59. happened is when the p_pid field is non-zero. */
  60. if (cnt == 1 && pds.p_pid == 0)
  61. return TD_OK;
  62. /* Now test whether this thread matches the specified
  63. conditions. */
  64. /* Only if the priority level is as high or higher. */
  65. if (pds.p_priority < ti_pri)
  66. return TD_OK;
  67. /* Test the state.
  68. XXX This is incomplete. */
  69. if (state != TD_THR_ANY_STATE)
  70. return TD_OK;
  71. /* XXX For now we ignore threads which are not running anymore.
  72. The reason is that gdb tries to get the registers and fails.
  73. In future we should have a special mode of the thread library
  74. in which we keep the process around until the actual join
  75. operation happened. */
  76. if (pds.p_exited != 0)
  77. return TD_OK;
  78. /* Yep, it matches. Call the callback function. */
  79. th.th_ta_p = (td_thragent_t *) ta;
  80. th.th_unique = descr;
  81. if (callback (&th, cbdata_p) != 0)
  82. return TD_DBERR;
  83. /* All done successfully. */
  84. return TD_OK;
  85. }
  86. td_err_e
  87. td_ta_thr_iter (const td_thragent_t *ta, td_thr_iter_f *callback,
  88. void *cbdata_p, td_thr_state_e state, int ti_pri,
  89. sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
  90. {
  91. int pthread_threads_max;
  92. struct pthread_handle_struct *phc;
  93. td_err_e result = TD_OK;
  94. int cnt;
  95. #ifdef ALL_THREADS_STOPPED
  96. int num;
  97. #else
  98. # define num 1
  99. #endif
  100. LOG ("td_ta_thr_iter");
  101. /* Test whether the TA parameter is ok. */
  102. if (! ta_ok (ta))
  103. return TD_BADTA;
  104. pthread_threads_max = ta->pthread_threads_max;
  105. phc = (struct pthread_handle_struct *) alloca (sizeof (phc[0])
  106. * pthread_threads_max);
  107. /* First read only the main thread and manager thread information. */
  108. if (ps_pdread (ta->ph, ta->handles, phc,
  109. sizeof (struct pthread_handle_struct) * 2) != PS_OK)
  110. return TD_ERR; /* XXX Other error value? */
  111. /* Now handle these descriptors. */
  112. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 0,
  113. phc[0].h_descr);
  114. if (result != TD_OK)
  115. return result;
  116. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 1,
  117. phc[1].h_descr);
  118. if (result != TD_OK)
  119. return result;
  120. /* Read all the descriptors. */
  121. if (ps_pdread (ta->ph, ta->handles + 2, &phc[2],
  122. (sizeof (struct pthread_handle_struct)
  123. * (pthread_threads_max - 2))) != PS_OK)
  124. return TD_ERR; /* XXX Other error value? */
  125. #ifdef ALL_THREADS_STOPPED
  126. /* Read the number of currently active threads. */
  127. if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
  128. return TD_ERR; /* XXX Other error value? */
  129. #endif
  130. /* Now get all descriptors, one after the other. */
  131. for (cnt = 2; cnt < pthread_threads_max && num > 0; ++cnt)
  132. if (phc[cnt].h_descr != NULL)
  133. {
  134. #ifdef ALL_THREADS_STOPPED
  135. /* First count this active thread. */
  136. --num;
  137. #endif
  138. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, cnt,
  139. phc[cnt].h_descr);
  140. if (result != TD_OK)
  141. break;
  142. }
  143. return result;
  144. }