td_ta_thr_iter.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Iterate over a process's threads.
  2. Copyright (C) 1999, 2000 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 (ps_pdread (ta->ph, descr, &pds, sizeof_descr) != PS_OK)
  28. return TD_ERR; /* XXX Other error value? */
  29. /* The manager thread must be handled special. The descriptor
  30. exists but the thread only gets created when the first
  31. `pthread_create' call is issued. A clear indication that this
  32. happened is when the p_pid field is non-zero. */
  33. if (cnt == 1 && pds.p_pid == 0)
  34. return TD_OK;
  35. /* Now test whether this thread matches the specified
  36. conditions. */
  37. /* Only if the priority level is as high or higher. */
  38. if (pds.p_priority < ti_pri)
  39. return TD_OK;
  40. /* Test the state.
  41. XXX This is incomplete. */
  42. if (state != TD_THR_ANY_STATE)
  43. return TD_OK;
  44. /* XXX For now we ignore threads which are not running anymore.
  45. The reason is that gdb tries to get the registers and fails.
  46. In future we should have a special mode of the thread library
  47. in which we keep the process around until the actual join
  48. operation happened. */
  49. if (pds.p_exited != 0)
  50. return TD_OK;
  51. /* Yep, it matches. Call the callback function. */
  52. th.th_ta_p = (td_thragent_t *) ta;
  53. th.th_unique = descr;
  54. if (callback (&th, cbdata_p) != 0)
  55. return TD_DBERR;
  56. /* All done successfully. */
  57. return TD_OK;
  58. }
  59. td_err_e
  60. td_ta_thr_iter (const td_thragent_t *ta, td_thr_iter_f *callback,
  61. void *cbdata_p, td_thr_state_e state, int ti_pri,
  62. sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
  63. {
  64. int pthread_threads_max;
  65. struct pthread_handle_struct *phc;
  66. td_err_e result = TD_OK;
  67. int cnt;
  68. #ifdef ALL_THREADS_STOPPED
  69. int num;
  70. #else
  71. # define num 1
  72. #endif
  73. LOG (__FUNCTION__);
  74. /* Test whether the TA parameter is ok. */
  75. if (! ta_ok (ta))
  76. return TD_BADTA;
  77. pthread_threads_max = ta->pthread_threads_max;
  78. phc = (struct pthread_handle_struct *) alloca (sizeof (phc[0])
  79. * pthread_threads_max);
  80. /* First read only the main thread and manager thread information. */
  81. if (ps_pdread (ta->ph, ta->handles, phc,
  82. sizeof (struct pthread_handle_struct) * 2) != PS_OK)
  83. return TD_ERR; /* XXX Other error value? */
  84. /* Now handle these descriptors. */
  85. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 0,
  86. phc[0].h_descr);
  87. if (result != TD_OK)
  88. return result;
  89. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 1,
  90. phc[1].h_descr);
  91. if (result != TD_OK)
  92. return result;
  93. /* Read all the descriptors. */
  94. if (ps_pdread (ta->ph, ta->handles + 2, &phc[2],
  95. (sizeof (struct pthread_handle_struct)
  96. * (pthread_threads_max - 2))) != PS_OK)
  97. return TD_ERR; /* XXX Other error value? */
  98. #ifdef ALL_THREADS_STOPPED
  99. /* Read the number of currently active threads. */
  100. if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
  101. return TD_ERR; /* XXX Other error value? */
  102. #endif
  103. /* Now get all descriptors, one after the other. */
  104. for (cnt = 2; cnt < pthread_threads_max && num > 0; ++cnt)
  105. if (phc[cnt].h_descr != NULL)
  106. {
  107. #ifdef ALL_THREADS_STOPPED
  108. /* First count this active thread. */
  109. --num;
  110. #endif
  111. result = handle_descr (ta, callback, cbdata_p, state, ti_pri, cnt,
  112. phc[cnt].h_descr);
  113. if (result != TD_OK)
  114. break;
  115. }
  116. return result;
  117. }