td_ta_thr_iter.c 5.3 KB

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