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