td_ta_thr_iter.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Iterate over a process's threads.
  2. Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@redhat.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. static td_err_e
  18. iterate_thread_list (td_thragent_t *ta, td_thr_iter_f *callback,
  19. void *cbdata_p, td_thr_state_e state, int ti_pri,
  20. psaddr_t head, int fake_empty)
  21. {
  22. td_err_e err;
  23. psaddr_t next, ofs;
  24. void *copy;
  25. /* Test the state.
  26. XXX This is incomplete. Normally this test should be in the loop. */
  27. if (state != TD_THR_ANY_STATE)
  28. return TD_OK;
  29. err = DB_GET_FIELD (next, ta, head, list_t, next, 0);
  30. if (err != TD_OK)
  31. return err;
  32. if (next == 0 && fake_empty)
  33. {
  34. /* __pthread_initialize_minimal has not run.
  35. There is just the main thread to return. */
  36. td_thrhandle_t th;
  37. err = td_ta_map_lwp2thr (ta, ps_getpid (ta->ph), &th);
  38. if (err == TD_OK)
  39. err = callback (&th, cbdata_p) != 0 ? TD_DBERR : TD_OK;
  40. return err;
  41. }
  42. /* Cache the offset from struct pthread to its list_t member. */
  43. err = DB_GET_FIELD_ADDRESS (ofs, ta, 0, pthread, list, 0);
  44. if (err != TD_OK)
  45. return err;
  46. if (ta->ta_sizeof_pthread == 0)
  47. {
  48. err = _td_check_sizeof (ta, &ta->ta_sizeof_pthread, SYM_SIZEOF_pthread);
  49. if (err != TD_OK)
  50. return err;
  51. }
  52. copy = __alloca (ta->ta_sizeof_pthread);
  53. while (next != head)
  54. {
  55. psaddr_t addr, schedpolicy, schedprio;
  56. addr = next - (ofs - (psaddr_t) 0);
  57. if (next == 0 || addr == 0) /* Sanity check. */
  58. return TD_DBERR;
  59. /* Copy the whole descriptor in once so we can access the several
  60. fields locally. Excess copying in one go is much better than
  61. multiple ps_pdread calls. */
  62. if (ps_pdread (ta->ph, addr, copy, ta->ta_sizeof_pthread) != PS_OK)
  63. return TD_ERR;
  64. err = DB_GET_FIELD_LOCAL (schedpolicy, ta, copy, pthread,
  65. schedpolicy, 0);
  66. if (err != TD_OK)
  67. break;
  68. err = DB_GET_FIELD_LOCAL (schedprio, ta, copy, pthread,
  69. schedparam_sched_priority, 0);
  70. if (err != TD_OK)
  71. break;
  72. /* Now test whether this thread matches the specified conditions. */
  73. /* Only if the priority level is as high or higher. */
  74. int descr_pri = ((uintptr_t) schedpolicy == SCHED_OTHER
  75. ? 0 : (uintptr_t) schedprio);
  76. if (descr_pri >= ti_pri)
  77. {
  78. /* Yep, it matches. Call the callback function. */
  79. td_thrhandle_t th;
  80. th.th_ta_p = (td_thragent_t *) ta;
  81. th.th_unique = addr;
  82. if (callback (&th, cbdata_p) != 0)
  83. return TD_DBERR;
  84. }
  85. /* Get the pointer to the next element. */
  86. err = DB_GET_FIELD_LOCAL (next, ta, copy + (ofs - (psaddr_t) 0), list_t,
  87. next, 0);
  88. if (err != TD_OK)
  89. break;
  90. }
  91. return err;
  92. }
  93. td_err_e
  94. td_ta_thr_iter (const td_thragent_t *ta_arg, td_thr_iter_f *callback,
  95. void *cbdata_p, td_thr_state_e state, int ti_pri,
  96. sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
  97. {
  98. td_thragent_t *const ta = (td_thragent_t *) ta_arg;
  99. td_err_e err;
  100. psaddr_t list = 0;
  101. LOG ("td_ta_thr_iter");
  102. /* Test whether the TA parameter is ok. */
  103. if (! ta_ok (ta))
  104. return TD_BADTA;
  105. /* The thread library keeps two lists for the running threads. One
  106. list contains the thread which are using user-provided stacks
  107. (this includes the main thread) and the other includes the
  108. threads for which the thread library allocated the stacks. We
  109. have to iterate over both lists separately. We start with the
  110. list of threads with user-defined stacks. */
  111. err = DB_GET_SYMBOL (list, ta, __stack_user);
  112. if (err == TD_OK)
  113. err = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri, list, 1);
  114. /* And the threads with stacks allocated by the implementation. */
  115. if (err == TD_OK)
  116. err = DB_GET_SYMBOL (list, ta, stack_used);
  117. if (err == TD_OK)
  118. err = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri, list, 0);
  119. return err;
  120. }