]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_task_thread.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / osfmk / ddb / db_task_thread.c
1 /*
2 * Copyright (c) 2000-2007 Apple 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 = TASK_NULL; /* default target task */
79 thread_t db_default_act = THREAD_NULL; /* 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
102 task_id = 0;
103 if (queue_first(&tasks) == 0)
104 return(-1);
105 queue_iterate(&tasks, task, task_t, tasks) {
106 if (target_task == task)
107 return(task_id);
108 if (task_id++ >= DB_MAX_TASKID)
109 return(-1);
110 }
111 return(-1);
112 }
113
114 /*
115 * search thread queue of the task, and return the queue position
116 */
117 int
118 db_lookup_task_act(
119 task_t task,
120 thread_t target_act)
121 {
122 register thread_t thr_act;
123 register int act_id;
124
125 act_id = 0;
126 if (queue_first(&task->threads) == 0)
127 return(-1);
128 queue_iterate(&task->threads, thr_act, thread_t, task_threads) {
129 if (target_act == thr_act)
130 return(act_id);
131 if (act_id++ >= DB_MAX_THREADID)
132 return(-1);
133 }
134 return(-1);
135 }
136
137 /*
138 * search thr_act queue of every valid task, and return the queue position
139 * as the thread id.
140 */
141 int
142 db_lookup_act(thread_t target_act)
143 {
144 register int act_id;
145 register task_t task;
146 register int ntask = 0;
147
148 if (queue_first(&tasks) == 0)
149 return(-1);
150 queue_iterate(&tasks, task, task_t, tasks) {
151 if (ntask++ > DB_MAX_TASKID)
152 return(-1);
153 if (task->thread_count == 0)
154 continue;
155 act_id = db_lookup_task_act(task, target_act);
156 if (act_id >= 0)
157 return(act_id);
158 }
159 return(-1);
160 }
161
162 /*
163 * check the address is a valid thread address
164 */
165 int force_act_lookup = 0;
166 boolean_t
167 db_check_act_address_valid(thread_t thr_act)
168 {
169 if (!force_act_lookup && db_lookup_act(thr_act) < 0) {
170 db_printf("Bad thr_act address 0x%x\n", thr_act);
171 db_flush_lex();
172 return(FALSE);
173 } else
174 return(TRUE);
175 }
176
177 /*
178 * convert task_id(queue postion) to task address
179 */
180 task_t
181 db_lookup_task_id(int task_id)
182 {
183 register task_t task;
184
185 if (task_id > DB_MAX_TASKID)
186 return(TASK_NULL);
187 if (queue_first(&tasks) == 0)
188 return(TASK_NULL);
189 queue_iterate(&tasks, task, task_t, tasks) {
190 if (task_id-- <= 0)
191 return(task);
192 }
193 return(TASK_NULL);
194 }
195
196 /*
197 * convert (task_id, act_id) pair to thr_act address
198 */
199 static thread_t
200 db_lookup_act_id(
201 task_t task,
202 register int act_id)
203 {
204 register thread_t thr_act;
205
206
207 if (act_id > DB_MAX_THREADID)
208 return(THREAD_NULL);
209 if (queue_first(&task->threads) == 0)
210 return(THREAD_NULL);
211 queue_iterate(&task->threads, thr_act, thread_t, task_threads) {
212 if (act_id-- <= 0)
213 return(thr_act);
214 }
215 return(THREAD_NULL);
216 }
217
218 /*
219 * get next parameter from a command line, and check it as a valid
220 * thread address
221 */
222 boolean_t
223 db_get_next_act(
224 thread_t *actp,
225 int position)
226 {
227 db_expr_t value;
228 thread_t thr_act;
229
230 *actp = THREAD_NULL;
231 if (db_expression(&value)) {
232 thr_act = (thread_t)(unsigned long)value;
233 if (!db_check_act_address_valid(thr_act)) {
234 db_flush_lex();
235 return(FALSE);
236 }
237 } else if (position <= 0) {
238 thr_act = db_default_act;
239 } else
240 return(FALSE);
241 *actp = thr_act;
242 return(TRUE);
243 }
244
245 /*
246 * check the default thread is still valid
247 * ( it is called in entering DDB session )
248 */
249 void
250 db_init_default_act(void)
251 {
252 if (db_lookup_act(db_default_act) < 0) {
253 db_default_act = THREAD_NULL;
254 db_default_task = TASK_NULL;
255 } else
256 db_default_task = db_default_act->task;
257 }
258
259 /*
260 * set or get default thread which is used when /t or :t option is specified
261 * in the command line
262 */
263 int
264 db_set_default_act(__unused struct db_variable *vp, db_expr_t *valuep,
265 int flag, __unused db_var_aux_param_t ap)
266 {
267 thread_t thr_act;
268 int task_id;
269 int act_id;
270
271 if (flag == DB_VAR_SHOW) {
272 db_printf("%#n", db_default_act);
273 task_id = db_lookup_task(db_default_task);
274 if (task_id != -1) {
275 act_id = db_lookup_act(db_default_act);
276 if (act_id != -1) {
277 db_printf(" (task%d.%d)", task_id, act_id);
278 }
279 }
280 return(0);
281 }
282
283 if (flag != DB_VAR_SET) {
284 *valuep = (db_expr_t)(unsigned long)db_default_act;
285 return(0);
286 }
287 thr_act = (thread_t)(unsigned long)*valuep;
288 if (thr_act != THREAD_NULL && !db_check_act_address_valid(thr_act))
289 db_error(0);
290 /* NOTREACHED */
291 db_default_act = thr_act;
292 if (thr_act)
293 db_default_task = thr_act->task;
294 return(0);
295 }
296
297 /*
298 * convert $taskXXX[.YYY] type DDB variable to task or thread address
299 */
300 int
301 db_get_task_act(__unused struct db_variable *vp, db_expr_t *valuep, int flag,
302 db_var_aux_param_t ap)
303 {
304 task_t task;
305 thread_t thr_act;
306 int task_id;
307
308 if (flag == DB_VAR_SHOW) {
309 db_printf("%#n", db_default_task);
310 task_id = db_lookup_task(db_default_task);
311 if (task_id != -1)
312 db_printf(" (task%d)", task_id);
313 return(0);
314 }
315
316 if (flag != DB_VAR_GET) {
317 db_error("Cannot set to $task variable\n");
318 /* NOTREACHED */
319 }
320 if ((task = db_lookup_task_id(ap->suffix[0])) == TASK_NULL) {
321 db_printf("no such task($task%d)\n", ap->suffix[0]);
322 db_error(0);
323 /* NOTREACHED */
324 }
325 if (ap->level <= 1) {
326 *valuep = (db_expr_t)(unsigned long)task;
327 return(0);
328 }
329 if ((thr_act = db_lookup_act_id(task, ap->suffix[1])) == THREAD_NULL){
330 db_printf("no such thr_act($task%d.%d)\n",
331 ap->suffix[0], ap->suffix[1]);
332 db_error(0);
333 /* NOTREACHED */
334 }
335 *valuep = (db_expr_t)(unsigned long)thr_act;
336 return(0);
337 }