td_ta_thr_iter.c 5.3 KB

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