Updated to fedora-glibc-20070510T2308
[glibc.git] / nptl_db / td_ta_thr_iter.c
blob66e4376659a17c59725b115f1ee8f92cc1d27171
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.
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.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include "thread_dbP.h"
24 static td_err_e
25 iterate_thread_list (td_thragent_t *ta, td_thr_iter_f *callback,
26 void *cbdata_p, td_thr_state_e state, int ti_pri,
27 psaddr_t head, int fake_empty)
29 td_err_e err;
30 psaddr_t next, ofs;
31 void *copy;
33 /* Test the state.
34 XXX This is incomplete. Normally this test should be in the loop. */
35 if (state != TD_THR_ANY_STATE)
36 return TD_OK;
38 err = DB_GET_FIELD (next, ta, head, list_t, next, 0);
39 if (err != TD_OK)
40 return err;
42 if (next == 0 && fake_empty)
44 /* __pthread_initialize_minimal has not run.
45 There is just the main thread to return. */
46 td_thrhandle_t th;
47 err = td_ta_map_lwp2thr (ta, ps_getpid (ta->ph), &th);
48 if (err == TD_OK)
49 err = callback (&th, cbdata_p) != 0 ? TD_DBERR : TD_OK;
50 return err;
53 /* Cache the offset from struct pthread to its list_t member. */
54 err = DB_GET_FIELD_ADDRESS (ofs, ta, 0, pthread, list, 0);
55 if (err != TD_OK)
56 return err;
58 if (ta->ta_sizeof_pthread == 0)
60 err = _td_check_sizeof (ta, &ta->ta_sizeof_pthread, SYM_SIZEOF_pthread);
61 if (err != TD_OK)
62 return err;
64 copy = __alloca (ta->ta_sizeof_pthread);
66 while (next != head)
68 psaddr_t addr, schedpolicy, schedprio;
70 addr = next - (ofs - (psaddr_t) 0);
71 if (next == 0 || addr == 0) /* Sanity check. */
72 return TD_DBERR;
74 /* Copy the whole descriptor in once so we can access the several
75 fields locally. Excess copying in one go is much better than
76 multiple ps_pdread calls. */
77 if (ps_pdread (ta->ph, addr, copy, ta->ta_sizeof_pthread) != PS_OK)
78 return TD_ERR;
80 err = DB_GET_FIELD_LOCAL (schedpolicy, ta, copy, pthread,
81 schedpolicy, 0);
82 if (err != TD_OK)
83 break;
84 err = DB_GET_FIELD_LOCAL (schedprio, ta, copy, pthread,
85 schedparam_sched_priority, 0);
86 if (err != TD_OK)
87 break;
89 /* Now test whether this thread matches the specified conditions. */
91 /* Only if the priority level is as high or higher. */
92 int descr_pri = ((uintptr_t) schedpolicy == SCHED_OTHER
93 ? 0 : (uintptr_t) schedprio);
94 if (descr_pri >= ti_pri)
96 /* Yep, it matches. Call the callback function. */
97 td_thrhandle_t th;
98 th.th_ta_p = (td_thragent_t *) ta;
99 th.th_unique = addr;
100 if (callback (&th, cbdata_p) != 0)
101 return TD_DBERR;
104 /* Get the pointer to the next element. */
105 err = DB_GET_FIELD_LOCAL (next, ta, copy + (ofs - (psaddr_t) 0), list_t,
106 next, 0);
107 if (err != TD_OK)
108 break;
111 return err;
115 td_err_e
116 td_ta_thr_iter (const td_thragent_t *ta_arg, td_thr_iter_f *callback,
117 void *cbdata_p, td_thr_state_e state, int ti_pri,
118 sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
120 td_thragent_t *const ta = (td_thragent_t *) ta_arg;
121 td_err_e err;
122 psaddr_t list = 0;
124 LOG ("td_ta_thr_iter");
126 /* Test whether the TA parameter is ok. */
127 if (! ta_ok (ta))
128 return TD_BADTA;
130 /* The thread library keeps two lists for the running threads. One
131 list contains the thread which are using user-provided stacks
132 (this includes the main thread) and the other includes the
133 threads for which the thread library allocated the stacks. We
134 have to iterate over both lists separately. We start with the
135 list of threads with user-defined stacks. */
137 err = DB_GET_SYMBOL (list, ta, __stack_user);
138 if (err == TD_OK)
139 err = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri, list, 1);
141 /* And the threads with stacks allocated by the implementation. */
142 if (err == TD_OK)
143 err = DB_GET_SYMBOL (list, ta, stack_used);
144 if (err == TD_OK)
145 err = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri, list, 0);
147 return err;