1 /* Iterate over a process's threads.
2 Copyright (C) 1999,2000,2001,2002,2003,2004,2007,2008
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@redhat.com>, 1999.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 #include "thread_dbP.h"
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
, bool fake_empty
, pid_t match_pid
)
34 XXX This is incomplete. Normally this test should be in the loop. */
35 if (state
!= TD_THR_ANY_STATE
)
38 err
= DB_GET_FIELD (next
, ta
, head
, list_t
, next
, 0);
42 if (next
== 0 && fake_empty
)
44 /* __pthread_initialize_minimal has not run. There is just the main
45 thread to return. We cannot rely on its thread register. They
46 sometimes contain garbage that would confuse us, left by the
47 kernel at exec. So if it looks like initialization is incomplete,
48 we only fake a special descriptor for the initial thread. */
49 td_thrhandle_t th
= { ta
, 0 };
50 return callback (&th
, cbdata_p
) != 0 ? TD_DBERR
: TD_OK
;
53 /* Cache the offset from struct pthread to its list_t member. */
54 err
= DB_GET_FIELD_ADDRESS (ofs
, ta
, 0, pthread
, list
, 0);
58 if (ta
->ta_sizeof_pthread
== 0)
60 err
= _td_check_sizeof (ta
, &ta
->ta_sizeof_pthread
, SYM_SIZEOF_pthread
);
64 copy
= __alloca (ta
->ta_sizeof_pthread
);
68 psaddr_t addr
, schedpolicy
, schedprio
;
70 addr
= next
- (ofs
- (psaddr_t
) 0);
71 if (next
== 0 || addr
== 0) /* Sanity check. */
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
)
80 /* Verify that this thread's pid field matches the child PID.
81 If its pid field is negative, it's about to do a fork or it
82 is the sole thread in a fork child. */
84 err
= DB_GET_FIELD_LOCAL (pid
, ta
, copy
, pthread
, pid
, 0);
85 if (err
== TD_OK
&& (pid_t
) (uintptr_t) pid
< 0)
87 if (-(pid_t
) (uintptr_t) pid
== match_pid
)
88 /* It is about to do a fork, but is really still the parent PID. */
89 pid
= (psaddr_t
) (uintptr_t) match_pid
;
91 /* It must be a fork child, whose new PID is in the tid field. */
92 err
= DB_GET_FIELD_LOCAL (pid
, ta
, copy
, pthread
, tid
, 0);
97 if ((pid_t
) (uintptr_t) pid
== match_pid
)
99 err
= DB_GET_FIELD_LOCAL (schedpolicy
, ta
, copy
, pthread
,
103 err
= DB_GET_FIELD_LOCAL (schedprio
, ta
, copy
, pthread
,
104 schedparam_sched_priority
, 0);
108 /* Now test whether this thread matches the specified conditions. */
110 /* Only if the priority level is as high or higher. */
111 int descr_pri
= ((uintptr_t) schedpolicy
== SCHED_OTHER
112 ? 0 : (uintptr_t) schedprio
);
113 if (descr_pri
>= ti_pri
)
115 /* Yep, it matches. Call the callback function. */
117 th
.th_ta_p
= (td_thragent_t
*) ta
;
119 if (callback (&th
, cbdata_p
) != 0)
124 /* Get the pointer to the next element. */
125 err
= DB_GET_FIELD_LOCAL (next
, ta
, copy
+ (ofs
- (psaddr_t
) 0), list_t
,
136 td_ta_thr_iter (const td_thragent_t
*ta_arg
, td_thr_iter_f
*callback
,
137 void *cbdata_p
, td_thr_state_e state
, int ti_pri
,
138 sigset_t
*ti_sigmask_p
, unsigned int ti_user_flags
)
140 td_thragent_t
*const ta
= (td_thragent_t
*) ta_arg
;
144 LOG ("td_ta_thr_iter");
146 /* Test whether the TA parameter is ok. */
150 /* The thread library keeps two lists for the running threads. One
151 list contains the thread which are using user-provided stacks
152 (this includes the main thread) and the other includes the
153 threads for which the thread library allocated the stacks. We
154 have to iterate over both lists separately. We start with the
155 list of threads with user-defined stacks. */
157 pid_t pid
= ps_getpid (ta
->ph
);
158 err
= DB_GET_SYMBOL (list
, ta
, __stack_user
);
160 err
= iterate_thread_list (ta
, callback
, cbdata_p
, state
, ti_pri
,
163 /* And the threads with stacks allocated by the implementation. */
165 err
= DB_GET_SYMBOL (list
, ta
, stack_used
);
167 err
= iterate_thread_list (ta
, callback
, cbdata_p
, state
, ti_pri
,