td_ta_thr_iter.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Iterate over a process's threads.
  2. Copyright (C) 1999,2000,2001,2002,2003,2004,2007
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Ulrich Drepper <drepper@redhat.com>, 1999.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "thread_dbP.h"
  18. static td_err_e
  19. iterate_thread_list (td_thragent_t *ta, td_thr_iter_f *callback,
  20. void *cbdata_p, td_thr_state_e state, int ti_pri,
  21. psaddr_t head, bool fake_empty)
  22. {
  23. td_err_e err;
  24. psaddr_t next, ofs;
  25. void *copy;
  26. int descr_pri;
  27. /* Test the state.
  28. XXX This is incomplete. Normally this test should be in the loop. */
  29. if (state != TD_THR_ANY_STATE)
  30. return TD_OK;
  31. err = DB_GET_FIELD (next, ta, head, list_t, next, 0);
  32. if (err != TD_OK)
  33. return err;
  34. if (next == 0 && fake_empty)
  35. {
  36. /* __pthread_initialize_minimal has not run. There is just the main
  37. thread to return. We cannot rely on its thread register. They
  38. sometimes contain garbage that would confuse us, left by the
  39. kernel at exec. So if it looks like initialization is incomplete,
  40. we only fake a special descriptor for the initial thread. */
  41. td_thrhandle_t th = { ta, 0 };
  42. return callback (&th, cbdata_p) != 0 ? TD_DBERR : TD_OK;
  43. }
  44. /* Cache the offset from struct pthread to its list_t member. */
  45. err = DB_GET_FIELD_ADDRESS (ofs, ta, 0, pthread, list, 0);
  46. if (err != TD_OK)
  47. return err;
  48. if (ta->ta_sizeof_pthread == 0)
  49. {
  50. err = _td_check_sizeof (ta, &ta->ta_sizeof_pthread, SYM_SIZEOF_pthread);
  51. if (err != TD_OK)
  52. return err;
  53. }
  54. copy = __alloca (ta->ta_sizeof_pthread);
  55. while (next != head)
  56. {
  57. psaddr_t addr, schedpolicy, schedprio;
  58. addr = next - (ofs - (psaddr_t) 0);
  59. if (next == 0 || addr == 0) /* Sanity check. */
  60. return TD_DBERR;
  61. /* Copy the whole descriptor in once so we can access the several
  62. fields locally. Excess copying in one go is much better than
  63. multiple ps_pdread calls. */
  64. if (ps_pdread (ta->ph, addr, copy, ta->ta_sizeof_pthread) != PS_OK)
  65. return TD_ERR;
  66. err = DB_GET_FIELD_LOCAL (schedpolicy, ta, copy, pthread,
  67. schedpolicy, 0);
  68. if (err != TD_OK)
  69. break;
  70. err = DB_GET_FIELD_LOCAL (schedprio, ta, copy, pthread,
  71. schedparam_sched_priority, 0);
  72. if (err != TD_OK)
  73. break;
  74. /* Now test whether this thread matches the specified conditions. */
  75. /* Only if the priority level is as high or higher. */
  76. descr_pri = ((uintptr_t) schedpolicy == SCHED_OTHER
  77. ? 0 : (uintptr_t) schedprio);
  78. if (descr_pri >= ti_pri)
  79. {
  80. /* Yep, it matches. Call the callback function. */
  81. td_thrhandle_t th;
  82. th.th_ta_p = (td_thragent_t *) ta;
  83. th.th_unique = addr;
  84. if (callback (&th, cbdata_p) != 0)
  85. return TD_DBERR;
  86. }
  87. /* Get the pointer to the next element. */
  88. err = DB_GET_FIELD_LOCAL (next, ta, copy + (ofs - (psaddr_t) 0), list_t,
  89. next, 0);
  90. if (err != TD_OK)
  91. break;
  92. }
  93. return err;
  94. }
  95. td_err_e
  96. td_ta_thr_iter (const td_thragent_t *ta_arg, td_thr_iter_f *callback,
  97. void *cbdata_p, td_thr_state_e state, int ti_pri,
  98. sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
  99. {
  100. td_thragent_t *const ta = (td_thragent_t *) ta_arg;
  101. td_err_e err;
  102. psaddr_t list = 0;
  103. LOG ("td_ta_thr_iter");
  104. /* Test whether the TA parameter is ok. */
  105. if (! ta_ok (ta))
  106. return TD_BADTA;
  107. /* The thread library keeps two lists for the running threads. One
  108. list contains the thread which are using user-provided stacks
  109. (this includes the main thread) and the other includes the
  110. threads for which the thread library allocated the stacks. We
  111. have to iterate over both lists separately. We start with the
  112. list of threads with user-defined stacks. */
  113. err = DB_GET_SYMBOL (list, ta, __stack_user);
  114. if (err == TD_OK)
  115. err = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri,
  116. list, true);
  117. /* And the threads with stacks allocated by the implementation. */
  118. if (err == TD_OK)
  119. err = DB_GET_SYMBOL (list, ta, stack_used);
  120. if (err == TD_OK)
  121. err = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri,
  122. list, false);
  123. return err;
  124. }