td_ta_thr_iter.c 4.6 KB

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