td_ta_thr_iter.c 5.3 KB

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