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