Use INTERNAL_SYSCALL.
[glibc.git] / nptl_db / td_ta_thr_iter.c
blob68b5427a7d1d7cab7ddf4aeff5edeeca5bd4904b
1 /* Iterate over a process's threads.
2 Copyright (C) 1999, 2000, 2001, 2002 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"
22 #include <nptl/descr.h>
25 static td_err_e
26 iterate_thread_list (const td_thragent_t *ta, td_thr_iter_f *callback,
27 void *cbdata_p, td_thr_state_e state, int ti_pri,
28 psaddr_t head)
30 list_t list;
31 td_err_e result = TD_OK;
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 if (ps_pdread (ta->ph, head, &list, sizeof (list_t)) != PS_OK)
39 return TD_ERR; /* XXX Other error value? */
41 if (list.next == 0 && list.prev == 0 && head == ta->stack_user)
43 /* __pthread_initialize_minimal has not run.
44 There is just the main thread to return. */
45 td_thrhandle_t th;
46 td_err_e err = td_ta_map_lwp2thr (ta, ps_getpid (ta->ph), &th);
47 return (err != TD_OK ? err
48 : callback (&th, cbdata_p) != 0 ? TD_DBERR : TD_OK);
51 while (list.next != head)
53 psaddr_t addr = ((psaddr_t) list.next
54 - offsetof (struct pthread, header.data.list));
56 int schedpolicy;
57 if (ps_pdread (ta->ph, &((struct pthread *) addr)->schedpolicy,
58 &schedpolicy, sizeof (int)) != PS_OK)
60 result = TD_ERR; /* XXX Other error value? */
61 break;
64 struct sched_param schedparam;
65 if (ps_pdread (ta->ph, &((struct pthread *) addr)->schedparam,
66 &schedparam, sizeof (struct sched_param)) != PS_OK)
68 result = TD_ERR; /* XXX Other error value? */
69 break;
72 /* Now test whether this thread matches the specified
73 conditions. */
75 /* Only if the priority level is as high or higher. */
76 int descr_pri = (schedpolicy == SCHED_OTHER
77 ? 0 : schedparam.sched_priority);
78 if (descr_pri >= ti_pri)
80 /* XXX For now we ignore threads which are not running anymore.
81 The reason is that gdb tries to get the registers and fails.
82 In future we should have a special mode of the thread library
83 in which we keep the process around until the actual join
84 operation happened. */
85 int cancelhandling;
86 if (ps_pdread (ta->ph, &((struct pthread *) addr)->cancelhandling,
87 &cancelhandling, sizeof (int)) != PS_OK)
89 result = TD_ERR; /* XXX Other error value? */
90 break;
93 if ((cancelhandling & TERMINATED_BIT) == 0)
95 /* Yep, it matches. Call the callback function. */
96 td_thrhandle_t th;
97 th.th_ta_p = (td_thragent_t *) ta;
98 th.th_unique = addr;
99 if (callback (&th, cbdata_p) != 0)
100 return TD_DBERR;
104 /* Get the pointer to the next element. */
105 if (ps_pdread (ta->ph, &((struct pthread *) addr)->header.data.list,
106 &list, sizeof (list_t)) != PS_OK)
107 return TD_ERR; /* XXX Other error value? */
110 return result;
114 td_err_e
115 td_ta_thr_iter (const td_thragent_t *ta, td_thr_iter_f *callback,
116 void *cbdata_p, td_thr_state_e state, int ti_pri,
117 sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
119 LOG ("td_ta_thr_iter");
121 /* Test whether the TA parameter is ok. */
122 if (! ta_ok (ta))
123 return TD_BADTA;
125 /* The thread library keeps two lists for the running threads. One
126 list contains the thread which are using user-provided stacks
127 (this includes the main thread) and the other includes the
128 threads for which the thread library allocated the stacks. We
129 have to iterate over both lists separately. We start with the
130 list of threads with user-defined stacks. */
131 td_err_e result = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri,
132 ta->stack_user);
134 /* And the threads with stacks allocated by the implementation. */
135 if (result == TD_OK)
136 result = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri,
137 ta->stack_used);
139 return result;