socketcall: fix compile issue with older Linux kernel
[uclibc-ng.git] / libpthread / linuxthreads_db / td_ta_thr_iter.c
blobda230096f4a0b76ddda06cb11a63b9de20ae8fc1
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, see
18 <http://www.gnu.org/licenses/>. */
20 #include "thread_dbP.h"
21 #include <alloca.h>
23 static int
24 handle_descr (const td_thragent_t *ta, td_thr_iter_f *callback,
25 void *cbdata_p, td_thr_state_e state, int ti_pri,
26 size_t cnt, pthread_descr descr)
28 struct _pthread_descr_struct pds;
29 size_t sizeof_descr = ta->sizeof_descr;
30 td_thrhandle_t th;
32 if (descr == NULL)
34 /* No descriptor (yet). */
35 if (cnt == 0)
37 /* This is the main thread. Create a fake descriptor. */
38 memset (&pds, '\0', sizeof (pds));
40 /* Empty thread descriptor the thread library would create. */
41 pds.p_self = &pds;
42 pds.p_nextlive = pds.p_prevlive = &pds;
43 pds.p_tid = PTHREAD_THREADS_MAX;
44 /* The init code also sets up p_lock, p_errnop, p_herrnop, and
45 p_userstack but this should not be necessary here. */
47 th.th_ta_p = (td_thragent_t *) ta;
48 th.th_unique = NULL;
49 if (callback (&th, cbdata_p) != 0)
50 return TD_DBERR;
52 /* All done successfully. */
53 return TD_OK;
55 else if (cnt == 1)
56 /* The manager is not yet started. No big deal. */
57 return TD_OK;
58 else
59 /* For every other thread this should not happen. */
60 return TD_ERR;
63 if (ps_pdread (ta->ph, descr, &pds, sizeof_descr) != PS_OK)
64 return TD_ERR; /* XXX Other error value? */
66 /* The manager thread must be handled special. The descriptor
67 exists but the thread only gets created when the first
68 `pthread_create' call is issued. A clear indication that this
69 happened is when the p_pid field is non-zero. */
70 if (cnt == 1 && pds.p_pid == 0)
71 return TD_OK;
73 /* Now test whether this thread matches the specified
74 conditions. */
76 /* Only if the priority level is as high or higher. */
77 if (pds.p_priority < ti_pri)
78 return TD_OK;
80 /* Test the state.
81 XXX This is incomplete. */
82 if (state != TD_THR_ANY_STATE)
83 return TD_OK;
85 /* XXX For now we ignore threads which are not running anymore.
86 The reason is that gdb tries to get the registers and fails.
87 In future we should have a special mode of the thread library
88 in which we keep the process around until the actual join
89 operation happened. */
90 if (pds.p_exited != 0)
91 return TD_OK;
93 /* Yep, it matches. Call the callback function. */
94 th.th_ta_p = (td_thragent_t *) ta;
95 th.th_unique = descr;
96 if (callback (&th, cbdata_p) != 0)
97 return TD_DBERR;
99 /* All done successfully. */
100 return TD_OK;
104 td_err_e
105 td_ta_thr_iter (const td_thragent_t *ta, td_thr_iter_f *callback,
106 void *cbdata_p, td_thr_state_e state, int ti_pri,
107 sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
109 int pthread_threads_max;
110 struct pthread_handle_struct *phc;
111 td_err_e result = TD_OK;
112 int cnt;
113 #ifdef ALL_THREADS_STOPPED
114 int num;
115 #else
116 # define num 1
117 #endif
119 LOG ("td_ta_thr_iter");
121 /* Test whether the TA parameter is ok. */
122 if (! ta_ok (ta))
123 return TD_BADTA;
125 pthread_threads_max = ta->pthread_threads_max;
126 phc = (struct pthread_handle_struct *) alloca (sizeof (phc[0])
127 * pthread_threads_max);
129 /* First read only the main thread and manager thread information. */
130 if (ps_pdread (ta->ph, ta->handles, phc,
131 sizeof (struct pthread_handle_struct) * 2) != PS_OK)
132 return TD_ERR; /* XXX Other error value? */
134 /* Now handle these descriptors. */
135 result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 0,
136 phc[0].h_descr);
137 if (result != TD_OK)
138 return result;
139 result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 1,
140 phc[1].h_descr);
141 if (result != TD_OK)
142 return result;
144 /* Read all the descriptors. */
145 if (ps_pdread (ta->ph, ta->handles + 2, &phc[2],
146 (sizeof (struct pthread_handle_struct)
147 * (pthread_threads_max - 2))) != PS_OK)
148 return TD_ERR; /* XXX Other error value? */
150 #ifdef ALL_THREADS_STOPPED
151 /* Read the number of currently active threads. */
152 if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
153 return TD_ERR; /* XXX Other error value? */
154 #endif
156 /* Now get all descriptors, one after the other. */
157 for (cnt = 2; cnt < pthread_threads_max && num > 0; ++cnt)
158 if (phc[cnt].h_descr != NULL)
160 #ifdef ALL_THREADS_STOPPED
161 /* First count this active thread. */
162 --num;
163 #endif
165 result = handle_descr (ta, callback, cbdata_p, state, ti_pri, cnt,
166 phc[cnt].h_descr);
167 if (result != TD_OK)
168 break;
171 return result;