]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl_db/td_ta_thr_iter.c
2009-03-19 Roland McGrath <roland@redhat.com>
[thirdparty/glibc.git] / nptl_db / td_ta_thr_iter.c
CommitLineData
76a50749 1/* Iterate over a process's threads.
d11dc9ec 2 Copyright (C) 1999,2000,2001,2002,2003,2004,2007,2008
7d9d8bd1 3 Free Software Foundation, Inc.
76a50749
UD
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@redhat.com>, 1999.
6
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.
11
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.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
21
22#include "thread_dbP.h"
76a50749
UD
23
24
25static td_err_e
7f08f55a 26iterate_thread_list (td_thragent_t *ta, td_thr_iter_f *callback,
76a50749 27 void *cbdata_p, td_thr_state_e state, int ti_pri,
d11dc9ec 28 psaddr_t head, bool fake_empty, pid_t match_pid)
76a50749 29{
7f08f55a
RM
30 td_err_e err;
31 psaddr_t next, ofs;
32 void *copy;
76a50749
UD
33
34 /* Test the state.
35 XXX This is incomplete. Normally this test should be in the loop. */
36 if (state != TD_THR_ANY_STATE)
37 return TD_OK;
38
7f08f55a
RM
39 err = DB_GET_FIELD (next, ta, head, list_t, next, 0);
40 if (err != TD_OK)
41 return err;
76a50749 42
7f08f55a 43 if (next == 0 && fake_empty)
f9958079 44 {
7d9d8bd1
RM
45 /* __pthread_initialize_minimal has not run. There is just the main
46 thread to return. We cannot rely on its thread register. They
47 sometimes contain garbage that would confuse us, left by the
48 kernel at exec. So if it looks like initialization is incomplete,
49 we only fake a special descriptor for the initial thread. */
50 td_thrhandle_t th = { ta, 0 };
51 return callback (&th, cbdata_p) != 0 ? TD_DBERR : TD_OK;
f9958079
RM
52 }
53
7f08f55a
RM
54 /* Cache the offset from struct pthread to its list_t member. */
55 err = DB_GET_FIELD_ADDRESS (ofs, ta, 0, pthread, list, 0);
56 if (err != TD_OK)
57 return err;
58
59 if (ta->ta_sizeof_pthread == 0)
76a50749 60 {
7f08f55a
RM
61 err = _td_check_sizeof (ta, &ta->ta_sizeof_pthread, SYM_SIZEOF_pthread);
62 if (err != TD_OK)
63 return err;
64 }
65 copy = __alloca (ta->ta_sizeof_pthread);
76a50749 66
7f08f55a
RM
67 while (next != head)
68 {
69 psaddr_t addr, schedpolicy, schedprio;
76a50749 70
7f08f55a
RM
71 addr = next - (ofs - (psaddr_t) 0);
72 if (next == 0 || addr == 0) /* Sanity check. */
73 return TD_DBERR;
74
75 /* Copy the whole descriptor in once so we can access the several
76 fields locally. Excess copying in one go is much better than
77 multiple ps_pdread calls. */
78 if (ps_pdread (ta->ph, addr, copy, ta->ta_sizeof_pthread) != PS_OK)
79 return TD_ERR;
80
d11dc9ec
RM
81 /* Verify that this thread's pid field matches the child PID.
82 If its pid field is negative, it's about to do a fork or it
83 is the sole thread in a fork child. */
84 psaddr_t pid;
85 err = DB_GET_FIELD_LOCAL (pid, ta, copy, pthread, pid, 0);
86 if (err == TD_OK && (pid_t) (uintptr_t) pid < 0)
87 {
88 if (-(pid_t) (uintptr_t) pid == match_pid)
89 /* It is about to do a fork, but is really still the parent PID. */
90 pid = (psaddr_t) (uintptr_t) match_pid;
91 else
92 /* It must be a fork child, whose new PID is in the tid field. */
93 err = DB_GET_FIELD_LOCAL (pid, ta, copy, pthread, tid, 0);
94 }
7f08f55a
RM
95 if (err != TD_OK)
96 break;
76a50749 97
d11dc9ec 98 if ((pid_t) (uintptr_t) pid == match_pid)
76a50749 99 {
d11dc9ec
RM
100 err = DB_GET_FIELD_LOCAL (schedpolicy, ta, copy, pthread,
101 schedpolicy, 0);
102 if (err != TD_OK)
103 break;
104 err = DB_GET_FIELD_LOCAL (schedprio, ta, copy, pthread,
105 schedparam_sched_priority, 0);
106 if (err != TD_OK)
107 break;
108
109 /* Now test whether this thread matches the specified conditions. */
110
111 /* Only if the priority level is as high or higher. */
112 int descr_pri = ((uintptr_t) schedpolicy == SCHED_OTHER
113 ? 0 : (uintptr_t) schedprio);
114 if (descr_pri >= ti_pri)
115 {
116 /* Yep, it matches. Call the callback function. */
117 td_thrhandle_t th;
118 th.th_ta_p = (td_thragent_t *) ta;
119 th.th_unique = addr;
120 if (callback (&th, cbdata_p) != 0)
121 return TD_DBERR;
122 }
76a50749 123 }
abbd6175
UD
124
125 /* Get the pointer to the next element. */
7f08f55a
RM
126 err = DB_GET_FIELD_LOCAL (next, ta, copy + (ofs - (psaddr_t) 0), list_t,
127 next, 0);
128 if (err != TD_OK)
129 break;
76a50749
UD
130 }
131
7f08f55a 132 return err;
76a50749
UD
133}
134
135
136td_err_e
7f08f55a 137td_ta_thr_iter (const td_thragent_t *ta_arg, td_thr_iter_f *callback,
76a50749
UD
138 void *cbdata_p, td_thr_state_e state, int ti_pri,
139 sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
140{
7f08f55a
RM
141 td_thragent_t *const ta = (td_thragent_t *) ta_arg;
142 td_err_e err;
abca9f7f 143 psaddr_t list = 0;
7f08f55a 144
76a50749
UD
145 LOG ("td_ta_thr_iter");
146
147 /* Test whether the TA parameter is ok. */
148 if (! ta_ok (ta))
149 return TD_BADTA;
150
151 /* The thread library keeps two lists for the running threads. One
152 list contains the thread which are using user-provided stacks
153 (this includes the main thread) and the other includes the
154 threads for which the thread library allocated the stacks. We
155 have to iterate over both lists separately. We start with the
156 list of threads with user-defined stacks. */
7f08f55a 157
d11dc9ec 158 pid_t pid = ps_getpid (ta->ph);
7f08f55a
RM
159 err = DB_GET_SYMBOL (list, ta, __stack_user);
160 if (err == TD_OK)
7d9d8bd1 161 err = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri,
d11dc9ec 162 list, true, pid);
76a50749
UD
163
164 /* And the threads with stacks allocated by the implementation. */
7f08f55a
RM
165 if (err == TD_OK)
166 err = DB_GET_SYMBOL (list, ta, stack_used);
167 if (err == TD_OK)
7d9d8bd1 168 err = iterate_thread_list (ta, callback, cbdata_p, state, ti_pri,
d11dc9ec 169 list, false, pid);
76a50749 170
7f08f55a 171 return err;
76a50749 172}