(outside-patch): redhat -> fedora
[glibc.git] / linuxthreads_db / td_ta_thr_iter.c
blob9ab04e14e3f3e266cddca47ed247a00bafcbaf72
1 /* Iterate over a process's threads.
2 Copyright (C) 1999,2000,2001,2002,2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.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 <linuxthreads/internals.h>
23 #include <alloca.h>
25 static int
26 handle_descr (const td_thragent_t *ta, td_thr_iter_f *callback,
27 void *cbdata_p, td_thr_state_e state, int ti_pri,
28 size_t cnt, pthread_descr descr)
30 struct _pthread_descr_struct pds;
31 size_t sizeof_descr = ta->sizeof_descr;
32 td_thrhandle_t th;
34 if (descr == NULL)
36 /* No descriptor (yet). */
37 if (cnt == 0)
39 /* This is the main thread. Create a fake descriptor. */
40 memset (&pds, '\0', sizeof (pds));
42 /* Empty thread descriptor the thread library would create. */
43 #if !defined USE_TLS || !TLS_DTV_AT_TP
44 pds.p_header.data.self = &pds;
45 #endif
46 pds.p_nextlive = pds.p_prevlive = &pds;
47 pds.p_tid = PTHREAD_THREADS_MAX;
48 /* The init code also sets up p_lock, p_errnop, p_herrnop, and
49 p_userstack but this should not be necessary here. */
51 th.th_ta_p = (td_thragent_t *) ta;
52 th.th_unique = NULL;
53 if (callback (&th, cbdata_p) != 0)
54 return TD_DBERR;
56 /* All done successfully. */
57 return TD_OK;
59 else if (cnt == 1)
60 /* The manager is not yet started. No big deal. */
61 return TD_OK;
62 else
63 /* For every other thread this should not happen. */
64 return TD_ERR;
67 if (ps_pdread (ta->ph, descr, &pds, sizeof_descr) != PS_OK)
68 return TD_ERR; /* XXX Other error value? */
70 /* The manager thread must be handled special. The descriptor
71 exists but the thread only gets created when the first
72 `pthread_create' call is issued. A clear indication that this
73 happened is when the p_pid field is non-zero. */
74 if (cnt == 1 && pds.p_pid == 0)
75 return TD_OK;
77 /* Now test whether this thread matches the specified
78 conditions. */
80 /* Only if the priority level is as high or higher. */
81 if (pds.p_priority < ti_pri)
82 return TD_OK;
84 /* Test the state.
85 XXX This is incomplete. */
86 if (state != TD_THR_ANY_STATE)
87 return TD_OK;
89 /* XXX For now we ignore threads which are not running anymore.
90 The reason is that gdb tries to get the registers and fails.
91 In future we should have a special mode of the thread library
92 in which we keep the process around until the actual join
93 operation happened. */
94 if (pds.p_exited != 0)
95 return TD_OK;
97 /* Yep, it matches. Call the callback function. */
98 th.th_ta_p = (td_thragent_t *) ta;
99 th.th_unique = descr;
100 if (callback (&th, cbdata_p) != 0)
101 return TD_DBERR;
103 /* All done successfully. */
104 return TD_OK;
108 td_err_e
109 td_ta_thr_iter (const td_thragent_t *ta, td_thr_iter_f *callback,
110 void *cbdata_p, td_thr_state_e state, int ti_pri,
111 sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
113 int pthread_threads_max;
114 struct pthread_handle_struct *phc;
115 td_err_e result = TD_OK;
116 int cnt;
117 #ifdef ALL_THREADS_STOPPED
118 int num;
119 #else
120 # define num 1
121 #endif
123 LOG ("td_ta_thr_iter");
125 /* Test whether the TA parameter is ok. */
126 if (! ta_ok (ta))
127 return TD_BADTA;
129 pthread_threads_max = ta->pthread_threads_max;
130 phc = (struct pthread_handle_struct *) alloca (sizeof (phc[0])
131 * pthread_threads_max);
133 /* First read only the main thread and manager thread information. */
134 if (ps_pdread (ta->ph, ta->handles, phc,
135 sizeof (struct pthread_handle_struct) * 2) != PS_OK)
136 return TD_ERR; /* XXX Other error value? */
138 /* Now handle these descriptors. */
139 result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 0,
140 phc[0].h_descr);
141 if (result != TD_OK)
142 return result;
143 result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 1,
144 phc[1].h_descr);
145 if (result != TD_OK)
146 return result;
148 /* Read all the descriptors. */
149 if (ps_pdread (ta->ph, ta->handles + 2, &phc[2],
150 (sizeof (struct pthread_handle_struct)
151 * (pthread_threads_max - 2))) != PS_OK)
152 return TD_ERR; /* XXX Other error value? */
154 #ifdef ALL_THREADS_STOPPED
155 /* Read the number of currently active threads. */
156 if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
157 return TD_ERR; /* XXX Other error value? */
158 #endif
160 /* Now get all descriptors, one after the other. */
161 for (cnt = 2; cnt < pthread_threads_max && num > 0; ++cnt)
162 if (phc[cnt].h_descr != NULL)
164 #ifdef ALL_THREADS_STOPPED
165 /* First count this active thread. */
166 --num;
167 #endif
169 result = handle_descr (ta, callback, cbdata_p, state, ti_pri, cnt,
170 phc[cnt].h_descr);
171 if (result != TD_OK)
172 break;
175 return result;