OSDN Git Service

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