]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_task_thread.c
af04e45660dd880069b87e2aa8a0c7add669559c
[apple/xnu.git] / osfmk / ddb / db_task_thread.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 #include <kern/kern_types.h>
60 #include <kern/processor.h>
61 #include <machine/db_machdep.h>
62 #include <ddb/db_task_thread.h>
63 #include <ddb/db_variables.h>
64 #include <ddb/db_command.h>
65 #include <ddb/db_expr.h>
66 #include <ddb/db_lex.h>
67 #include <ddb/db_output.h> /* For db_printf() */
68 #include <ddb/db_sym.h>
69
70 /*
71 * Following constants are used to prevent infinite loop of task
72 * or thread search due to the incorrect list.
73 */
74 #define DB_MAX_TASKID 0x10000 /* max # of tasks */
75 #define DB_MAX_THREADID 0x10000 /* max # of threads in a task */
76 #define DB_MAX_PSETS 0x10000 /* max # of processor sets */
77
78 task_t db_default_task; /* default target task */
79 thread_t db_default_act; /* default target thr_act */
80
81
82
83 /* Prototypes for functions local to this file.
84 */
85 task_t db_lookup_task_id(register int task_id);
86
87 static thread_t db_lookup_act_id(
88 task_t task,
89 register int thread_id);
90
91
92
93 /*
94 * search valid task queue, and return the queue position as the task id
95 */
96 int
97 db_lookup_task(task_t target_task)
98 {
99 register task_t task;
100 register int task_id;
101 register processor_set_t pset = &default_pset;
102 register int npset = 0;
103
104 task_id = 0;
105 if (npset++ >= DB_MAX_PSETS)
106 return(-1);
107 if (queue_first(&pset->tasks) == 0)
108 return(-1);
109 queue_iterate(&pset->tasks, task, task_t, pset_tasks) {
110 if (target_task == task)
111 return(task_id);
112 if (task_id++ >= DB_MAX_TASKID)
113 return(-1);
114 }
115 return(-1);
116 }
117
118 /*
119 * search thread queue of the task, and return the queue position
120 */
121 int
122 db_lookup_task_act(
123 task_t task,
124 thread_t target_act)
125 {
126 register thread_t thr_act;
127 register int act_id;
128
129 act_id = 0;
130 if (queue_first(&task->threads) == 0)
131 return(-1);
132 queue_iterate(&task->threads, thr_act, thread_t, task_threads) {
133 if (target_act == thr_act)
134 return(act_id);
135 if (act_id++ >= DB_MAX_THREADID)
136 return(-1);
137 }
138 return(-1);
139 }
140
141 /*
142 * search thr_act queue of every valid task, and return the queue position
143 * as the thread id.
144 */
145 int
146 db_lookup_act(thread_t target_act)
147 {
148 register int act_id;
149 register task_t task;
150 register processor_set_t pset = &default_pset;
151 register int ntask = 0;
152 register int npset = 0;
153
154 if (npset++ >= DB_MAX_PSETS)
155 return(-1);
156 if (queue_first(&pset->tasks) == 0)
157 return(-1);
158 queue_iterate(&pset->tasks, task, task_t, pset_tasks) {
159 if (ntask++ > DB_MAX_TASKID)
160 return(-1);
161 if (task->thread_count == 0)
162 continue;
163 act_id = db_lookup_task_act(task, target_act);
164 if (act_id >= 0)
165 return(act_id);
166 }
167 return(-1);
168 }
169
170 /*
171 * check the address is a valid thread address
172 */
173 int force_act_lookup = 0;
174 boolean_t
175 db_check_act_address_valid(thread_t thr_act)
176 {
177 if (!force_act_lookup && db_lookup_act(thr_act) < 0) {
178 db_printf("Bad thr_act address 0x%x\n", thr_act);
179 db_flush_lex();
180 return(FALSE);
181 } else
182 return(TRUE);
183 }
184
185 /*
186 * convert task_id(queue postion) to task address
187 */
188 task_t
189 db_lookup_task_id(register task_id)
190 {
191 register task_t task;
192 register processor_set_t pset = &default_pset;
193 register int npset = 0;
194
195 if (task_id > DB_MAX_TASKID)
196 return(TASK_NULL);
197 if (npset++ >= DB_MAX_PSETS)
198 return(TASK_NULL);
199 if (queue_first(&pset->tasks) == 0)
200 return(TASK_NULL);
201 queue_iterate(&pset->tasks, task, task_t, pset_tasks) {
202 if (task_id-- <= 0)
203 return(task);
204 }
205 return(TASK_NULL);
206 }
207
208 /*
209 * convert (task_id, act_id) pair to thr_act address
210 */
211 static thread_t
212 db_lookup_act_id(
213 task_t task,
214 register int act_id)
215 {
216 register thread_t thr_act;
217
218
219 if (act_id > DB_MAX_THREADID)
220 return(THREAD_NULL);
221 if (queue_first(&task->threads) == 0)
222 return(THREAD_NULL);
223 queue_iterate(&task->threads, thr_act, thread_t, task_threads) {
224 if (act_id-- <= 0)
225 return(thr_act);
226 }
227 return(THREAD_NULL);
228 }
229
230 /*
231 * get next parameter from a command line, and check it as a valid
232 * thread address
233 */
234 boolean_t
235 db_get_next_act(
236 thread_t *actp,
237 int position)
238 {
239 db_expr_t value;
240 thread_t thr_act;
241
242 *actp = THREAD_NULL;
243 if (db_expression(&value)) {
244 thr_act = (thread_t) value;
245 if (!db_check_act_address_valid(thr_act)) {
246 db_flush_lex();
247 return(FALSE);
248 }
249 } else if (position <= 0) {
250 thr_act = db_default_act;
251 } else
252 return(FALSE);
253 *actp = thr_act;
254 return(TRUE);
255 }
256
257 /*
258 * check the default thread is still valid
259 * ( it is called in entering DDB session )
260 */
261 void
262 db_init_default_act(void)
263 {
264 if (db_lookup_act(db_default_act) < 0) {
265 db_default_act = THREAD_NULL;
266 db_default_task = TASK_NULL;
267 } else
268 db_default_task = db_default_act->task;
269 }
270
271 /*
272 * set or get default thread which is used when /t or :t option is specified
273 * in the command line
274 */
275 int
276 db_set_default_act(
277 struct db_variable *vp,
278 db_expr_t *valuep,
279 int flag,
280 db_var_aux_param_t ap) /* unused */
281 {
282 thread_t thr_act;
283 int task_id;
284 int act_id;
285
286 if (flag == DB_VAR_SHOW) {
287 db_printf("%#n", db_default_act);
288 task_id = db_lookup_task(db_default_task);
289 if (task_id != -1) {
290 act_id = db_lookup_act(db_default_act);
291 if (act_id != -1) {
292 db_printf(" (task%d.%d)", task_id, act_id);
293 }
294 }
295 return(0);
296 }
297
298 if (flag != DB_VAR_SET) {
299 *valuep = (db_expr_t) db_default_act;
300 return(0);
301 }
302 thr_act = (thread_t) *valuep;
303 if (thr_act != THREAD_NULL && !db_check_act_address_valid(thr_act))
304 db_error(0);
305 /* NOTREACHED */
306 db_default_act = thr_act;
307 if (thr_act)
308 db_default_task = thr_act->task;
309 return(0);
310 }
311
312 /*
313 * convert $taskXXX[.YYY] type DDB variable to task or thread address
314 */
315 int
316 db_get_task_act(
317 struct db_variable *vp,
318 db_expr_t *valuep,
319 int flag,
320 db_var_aux_param_t ap)
321 {
322 task_t task;
323 thread_t thr_act;
324 int task_id;
325
326 if (flag == DB_VAR_SHOW) {
327 db_printf("%#n", db_default_task);
328 task_id = db_lookup_task(db_default_task);
329 if (task_id != -1)
330 db_printf(" (task%d)", task_id);
331 return(0);
332 }
333
334 if (flag != DB_VAR_GET) {
335 db_error("Cannot set to $task variable\n");
336 /* NOTREACHED */
337 }
338 if ((task = db_lookup_task_id(ap->suffix[0])) == TASK_NULL) {
339 db_printf("no such task($task%d)\n", ap->suffix[0]);
340 db_error(0);
341 /* NOTREACHED */
342 }
343 if (ap->level <= 1) {
344 *valuep = (db_expr_t) task;
345 return(0);
346 }
347 if ((thr_act = db_lookup_act_id(task, ap->suffix[1])) == THREAD_NULL){
348 db_printf("no such thr_act($task%d.%d)\n",
349 ap->suffix[0], ap->suffix[1]);
350 db_error(0);
351 /* NOTREACHED */
352 }
353 *valuep = (db_expr_t) thr_act;
354 return(0);
355 }