db_command.c revision 139747
1189251Ssam/*-
2214734Srpaulo * Mach Operating System
3252726Srpaulo * Copyright (c) 1991,1990 Carnegie Mellon University
4189251Ssam * All Rights Reserved.
5252726Srpaulo *
6252726Srpaulo * Permission to use, copy, modify and distribute this software and its
7189251Ssam * documentation is hereby granted, provided that both the copyright
8189251Ssam * notice and this permission notice appear in all copies of the
9189251Ssam * software, derivative works or modified versions, and any portions
10189251Ssam * thereof, and that both notices appear in supporting documentation.
11189251Ssam *
12189251Ssam * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13252726Srpaulo * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14214734Srpaulo * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15214734Srpaulo *
16189251Ssam * Carnegie Mellon requests users of this software to return to
17189251Ssam *
18189251Ssam *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19189251Ssam *  School of Computer Science
20189251Ssam *  Carnegie Mellon University
21189251Ssam *  Pittsburgh PA 15213-3890
22252726Srpaulo *
23252726Srpaulo * any improvements or extensions that they make and grant Carnegie the
24252726Srpaulo * rights to redistribute these changes.
25189251Ssam */
26189251Ssam/*
27189251Ssam *	Author: David B. Golub, Carnegie Mellon University
28189251Ssam *	Date:	7/90
29189251Ssam */
30189251Ssam/*
31189251Ssam * Command dispatcher.
32189251Ssam */
33189251Ssam
34189251Ssam#include <sys/cdefs.h>
35189251Ssam__FBSDID("$FreeBSD: head/sys/ddb/db_command.c 139747 2005-01-06 01:34:41Z imp $");
36252726Srpaulo
37189251Ssam#include <sys/param.h>
38189251Ssam#include <sys/linker_set.h>
39189251Ssam#include <sys/lock.h>
40189251Ssam#include <sys/kdb.h>
41189251Ssam#include <sys/mutex.h>
42189251Ssam#include <sys/proc.h>
43189251Ssam#include <sys/reboot.h>
44189251Ssam#include <sys/signalvar.h>
45189251Ssam#include <sys/systm.h>
46189251Ssam#include <sys/cons.h>
47189251Ssam#include <sys/watchdog.h>
48189251Ssam
49189251Ssam#include <ddb/ddb.h>
50189251Ssam#include <ddb/db_command.h>
51189251Ssam#include <ddb/db_lex.h>
52252726Srpaulo#include <ddb/db_output.h>
53252726Srpaulo
54252726Srpaulo#include <machine/cpu.h>
55252726Srpaulo#include <machine/setjmp.h>
56252726Srpaulo
57189251Ssam/*
58189251Ssam * Exported global variables
59189251Ssam */
60189251Ssamboolean_t	db_cmd_loop_done;
61189251Ssamdb_addr_t	db_dot;
62189251Ssamdb_addr_t	db_last_addr;
63189251Ssamdb_addr_t	db_prev;
64189251Ssamdb_addr_t	db_next;
65189251Ssam
66189251SsamSET_DECLARE(db_cmd_set, struct command);
67189251SsamSET_DECLARE(db_show_cmd_set, struct command);
68189251Ssam
69252726Srpaulostatic db_cmdfcn_t	db_fncall;
70252726Srpaulostatic db_cmdfcn_t	db_gdb;
71189251Ssamstatic db_cmdfcn_t	db_kill;
72189251Ssamstatic db_cmdfcn_t	db_reset;
73189251Ssamstatic db_cmdfcn_t	db_stack_trace;
74189251Ssamstatic db_cmdfcn_t	db_watchdog;
75189251Ssam
76189251Ssam/* XXX this is actually forward-static. */
77189251Ssamextern struct command	db_show_cmds[];
78189251Ssam
79189251Ssam/*
80189251Ssam * if 'ed' style: 'dot' is set at start of last item printed,
81189251Ssam * and '+' points to next line.
82189251Ssam * Otherwise: 'dot' points to next item, '..' points to last.
83189251Ssam */
84189251Ssamstatic boolean_t	db_ed_style = TRUE;
85189251Ssam
86189251Ssam/*
87189251Ssam * Utility routine - discard tokens through end-of-line.
88252726Srpaulo */
89252726Srpaulovoid
90189251Ssamdb_skip_to_eol()
91189251Ssam{
92189251Ssam	int	t;
93189251Ssam	do {
94189251Ssam	    t = db_read_token();
95252726Srpaulo	} while (t != tEOL);
96252726Srpaulo}
97252726Srpaulo
98252726Srpaulo/*
99252726Srpaulo * Results of command search.
100252726Srpaulo */
101252726Srpaulo#define	CMD_UNIQUE	0
102252726Srpaulo#define	CMD_FOUND	1
103252726Srpaulo#define	CMD_NONE	2
104252726Srpaulo#define	CMD_AMBIGUOUS	3
105252726Srpaulo#define	CMD_HELP	4
106252726Srpaulo
107252726Srpaulostatic void	db_cmd_list(struct command *table, struct command **aux_tablep,
108252726Srpaulo		    struct command **aux_tablep_end);
109252726Srpaulostatic int	db_cmd_search(char *name, struct command *table,
110252726Srpaulo		    struct command **aux_tablep,
111252726Srpaulo		    struct command **aux_tablep_end, struct command **cmdp);
112252726Srpaulostatic void	db_command(struct command **last_cmdp,
113252726Srpaulo		    struct command *cmd_table, struct command **aux_cmd_tablep,
114252726Srpaulo		    struct command **aux_cmd_tablep_end);
115252726Srpaulo
116189251Ssam/*
117189251Ssam * Search for command prefix.
118189251Ssam */
119189251Ssamstatic int
120189251Ssamdb_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
121189251Ssam	char *		name;
122189251Ssam	struct command	*table;
123189251Ssam	struct command	**aux_tablep;
124189251Ssam	struct command	**aux_tablep_end;
125189251Ssam	struct command	**cmdp;	/* out */
126189251Ssam{
127189251Ssam	struct command	*cmd;
128189251Ssam	struct command	**aux_cmdp;
129189251Ssam	int		result = CMD_NONE;
130189251Ssam
131189251Ssam	for (cmd = table; cmd->name != 0; cmd++) {
132189251Ssam	    register char *lp;
133189251Ssam	    register char *rp;
134189251Ssam	    register int  c;
135189251Ssam
136189251Ssam	    lp = name;
137189251Ssam	    rp = cmd->name;
138189251Ssam	    while ((c = *lp) == *rp) {
139252726Srpaulo		if (c == 0) {
140252726Srpaulo		    /* complete match */
141252726Srpaulo		    *cmdp = cmd;
142252726Srpaulo		    return (CMD_UNIQUE);
143252726Srpaulo		}
144252726Srpaulo		lp++;
145252726Srpaulo		rp++;
146252726Srpaulo	    }
147189251Ssam	    if (c == 0) {
148189251Ssam		/* end of name, not end of command -
149189251Ssam		   partial match */
150189251Ssam		if (result == CMD_FOUND) {
151189251Ssam		    result = CMD_AMBIGUOUS;
152189251Ssam		    /* but keep looking for a full match -
153189251Ssam		       this lets us match single letters */
154189251Ssam		}
155189251Ssam		else {
156189251Ssam		    *cmdp = cmd;
157189251Ssam		    result = CMD_FOUND;
158189251Ssam		}
159189251Ssam	    }
160189251Ssam	}
161189251Ssam	if (result == CMD_NONE && aux_tablep != 0)
162189251Ssam	    /* XXX repeat too much code. */
163189251Ssam	    for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
164189251Ssam		register char *lp;
165189251Ssam		register char *rp;
166189251Ssam		register int  c;
167189251Ssam
168189251Ssam		lp = name;
169189251Ssam		rp = (*aux_cmdp)->name;
170189251Ssam		while ((c = *lp) == *rp) {
171189251Ssam		    if (c == 0) {
172189251Ssam			/* complete match */
173189251Ssam			*cmdp = *aux_cmdp;
174189251Ssam			return (CMD_UNIQUE);
175189251Ssam		    }
176189251Ssam		    lp++;
177189251Ssam		    rp++;
178189251Ssam		}
179189251Ssam		if (c == 0) {
180189251Ssam		    /* end of name, not end of command -
181189251Ssam		       partial match */
182189251Ssam		    if (result == CMD_FOUND) {
183252726Srpaulo			result = CMD_AMBIGUOUS;
184252726Srpaulo			/* but keep looking for a full match -
185252726Srpaulo			   this lets us match single letters */
186252726Srpaulo		    }
187252726Srpaulo		    else {
188252726Srpaulo			*cmdp = *aux_cmdp;
189252726Srpaulo			result = CMD_FOUND;
190252726Srpaulo		    }
191252726Srpaulo		}
192252726Srpaulo	    }
193252726Srpaulo	if (result == CMD_NONE) {
194189251Ssam	    /* check for 'help' */
195189251Ssam		if (name[0] == 'h' && name[1] == 'e'
196189251Ssam		    && name[2] == 'l' && name[3] == 'p')
197189251Ssam			result = CMD_HELP;
198189251Ssam	}
199189251Ssam	return (result);
200189251Ssam}
201189251Ssam
202189251Ssamstatic void
203189251Ssamdb_cmd_list(table, aux_tablep, aux_tablep_end)
204189251Ssam	struct command *table;
205189251Ssam	struct command **aux_tablep;
206189251Ssam	struct command **aux_tablep_end;
207189251Ssam{
208189251Ssam	register struct command *cmd;
209189251Ssam	register struct command **aux_cmdp;
210189251Ssam
211189251Ssam	for (cmd = table; cmd->name != 0; cmd++) {
212189251Ssam	    db_printf("%-12s", cmd->name);
213189251Ssam	    db_end_line();
214189251Ssam	}
215189251Ssam	if (aux_tablep == 0)
216189251Ssam	    return;
217189251Ssam	for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
218189251Ssam	    db_printf("%-12s", (*aux_cmdp)->name);
219189251Ssam	    db_end_line();
220189251Ssam	}
221189251Ssam}
222189251Ssam
223189251Ssamstatic void
224189251Ssamdb_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
225189251Ssam	struct command	**last_cmdp;	/* IN_OUT */
226189251Ssam	struct command	*cmd_table;
227189251Ssam	struct command	**aux_cmd_tablep;
228189251Ssam	struct command	**aux_cmd_tablep_end;
229189251Ssam{
230189251Ssam	struct command	*cmd;
231189251Ssam	int		t;
232189251Ssam	char		modif[TOK_STRING_SIZE];
233189251Ssam	db_expr_t	addr, count;
234252726Srpaulo	boolean_t	have_addr = FALSE;
235252726Srpaulo	int		result;
236252726Srpaulo
237252726Srpaulo	t = db_read_token();
238252726Srpaulo	if (t == tEOL) {
239252726Srpaulo	    /* empty line repeats last command, at 'next' */
240252726Srpaulo	    cmd = *last_cmdp;
241252726Srpaulo	    addr = (db_expr_t)db_next;
242252726Srpaulo	    have_addr = FALSE;
243252726Srpaulo	    count = 1;
244252726Srpaulo	    modif[0] = '\0';
245252726Srpaulo	}
246252726Srpaulo	else if (t == tEXCL) {
247252726Srpaulo	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
248252726Srpaulo	    return;
249252726Srpaulo	}
250252726Srpaulo	else if (t != tIDENT) {
251252726Srpaulo	    db_printf("?\n");
252252726Srpaulo	    db_flush_lex();
253252726Srpaulo	    return;
254252726Srpaulo	}
255252726Srpaulo	else {
256252726Srpaulo	    /*
257252726Srpaulo	     * Search for command
258252726Srpaulo	     */
259189251Ssam	    while (cmd_table) {
260189251Ssam		result = db_cmd_search(db_tok_string,
261189251Ssam				       cmd_table,
262189251Ssam				       aux_cmd_tablep,
263189251Ssam				       aux_cmd_tablep_end,
264189251Ssam				       &cmd);
265189251Ssam		switch (result) {
266189251Ssam		    case CMD_NONE:
267189251Ssam			db_printf("No such command\n");
268189251Ssam			db_flush_lex();
269189251Ssam			return;
270189251Ssam		    case CMD_AMBIGUOUS:
271189251Ssam			db_printf("Ambiguous\n");
272189251Ssam			db_flush_lex();
273189251Ssam			return;
274189251Ssam		    case CMD_HELP:
275189251Ssam			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
276			db_flush_lex();
277			return;
278		    default:
279			break;
280		}
281		if ((cmd_table = cmd->more) != 0) {
282		    /* XXX usually no more aux's. */
283		    aux_cmd_tablep = 0;
284		    if (cmd_table == db_show_cmds) {
285			aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
286			aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
287		    }
288
289		    t = db_read_token();
290		    if (t != tIDENT) {
291			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
292			db_flush_lex();
293			return;
294		    }
295		}
296	    }
297
298	    if ((cmd->flag & CS_OWN) == 0) {
299		/*
300		 * Standard syntax:
301		 * command [/modifier] [addr] [,count]
302		 */
303		t = db_read_token();
304		if (t == tSLASH) {
305		    t = db_read_token();
306		    if (t != tIDENT) {
307			db_printf("Bad modifier\n");
308			db_flush_lex();
309			return;
310		    }
311		    db_strcpy(modif, db_tok_string);
312		}
313		else {
314		    db_unread_token(t);
315		    modif[0] = '\0';
316		}
317
318		if (db_expression(&addr)) {
319		    db_dot = (db_addr_t) addr;
320		    db_last_addr = db_dot;
321		    have_addr = TRUE;
322		}
323		else {
324		    addr = (db_expr_t) db_dot;
325		    have_addr = FALSE;
326		}
327		t = db_read_token();
328		if (t == tCOMMA) {
329		    if (!db_expression(&count)) {
330			db_printf("Count missing\n");
331			db_flush_lex();
332			return;
333		    }
334		}
335		else {
336		    db_unread_token(t);
337		    count = -1;
338		}
339		if ((cmd->flag & CS_MORE) == 0) {
340		    db_skip_to_eol();
341		}
342	    }
343	}
344	*last_cmdp = cmd;
345	if (cmd != 0) {
346	    /*
347	     * Execute the command.
348	     */
349	    (*cmd->fcn)(addr, have_addr, count, modif);
350	    db_setup_paging(NULL, NULL, -1);
351
352	    if (cmd->flag & CS_SET_DOT) {
353		/*
354		 * If command changes dot, set dot to
355		 * previous address displayed (if 'ed' style).
356		 */
357		if (db_ed_style) {
358		    db_dot = db_prev;
359		}
360		else {
361		    db_dot = db_next;
362		}
363	    }
364	    else {
365		/*
366		 * If command does not change dot,
367		 * set 'next' location to be the same.
368		 */
369		db_next = db_dot;
370	    }
371	}
372}
373
374/*
375 * 'show' commands
376 */
377
378static struct command db_show_all_cmds[] = {
379	{ "procs",	db_ps,			0,	0 },
380	{ (char *)0 }
381};
382
383static struct command db_show_cmds[] = {
384	{ "all",	0,			0,	db_show_all_cmds },
385	{ "registers",	db_show_regs,		0,	0 },
386	{ "breaks",	db_listbreak_cmd, 	0,	0 },
387	{ "threads",	db_show_threads,	0,	0 },
388	{ (char *)0, }
389};
390
391static struct command db_command_table[] = {
392	{ "print",	db_print_cmd,		0,	0 },
393	{ "p",		db_print_cmd,		0,	0 },
394	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
395	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
396	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
397	{ "set",	db_set_cmd,		CS_OWN,	0 },
398	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
399	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
400	{ "delete",	db_delete_cmd,		0,	0 },
401	{ "d",		db_delete_cmd,		0,	0 },
402	{ "break",	db_breakpoint_cmd,	0,	0 },
403	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
404	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
405	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
406	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
407	{ "step",	db_single_step_cmd,	0,	0 },
408	{ "s",		db_single_step_cmd,	0,	0 },
409	{ "continue",	db_continue_cmd,	0,	0 },
410	{ "c",		db_continue_cmd,	0,	0 },
411	{ "until",	db_trace_until_call_cmd,0,	0 },
412	{ "next",	db_trace_until_matching_cmd,0,	0 },
413	{ "match",	db_trace_until_matching_cmd,0,	0 },
414	{ "trace",	db_stack_trace,		CS_OWN,	0 },
415	{ "where",	db_stack_trace,		CS_OWN,	0 },
416	{ "call",	db_fncall,		CS_OWN,	0 },
417	{ "show",	0,			0,	db_show_cmds },
418	{ "ps",		db_ps,			0,	0 },
419	{ "gdb",	db_gdb,			0,	0 },
420	{ "reset",	db_reset,		0,	0 },
421	{ "kill",	db_kill,		CS_OWN,	0 },
422	{ "watchdog",	db_watchdog,		0,	0 },
423	{ "thread",	db_set_thread,		CS_OWN,	0 },
424	{ (char *)0, }
425};
426
427static struct command	*db_last_command = 0;
428
429/*
430 * At least one non-optional command must be implemented using
431 * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
432 */
433DB_COMMAND(panic, db_panic)
434{
435	panic("from debugger");
436}
437
438void
439db_command_loop()
440{
441	/*
442	 * Initialize 'prev' and 'next' to dot.
443	 */
444	db_prev = db_dot;
445	db_next = db_dot;
446
447	db_cmd_loop_done = 0;
448	while (!db_cmd_loop_done) {
449	    if (db_print_position() != 0)
450		db_printf("\n");
451
452	    db_printf("db> ");
453	    (void) db_read_line();
454
455	    db_command(&db_last_command, db_command_table,
456		       SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
457	}
458}
459
460void
461db_error(s)
462	const char *s;
463{
464	if (s)
465	    db_printf("%s", s);
466	db_flush_lex();
467	kdb_reenter();
468}
469
470
471/*
472 * Call random function:
473 * !expr(arg,arg,arg)
474 */
475static void
476db_fncall(dummy1, dummy2, dummy3, dummy4)
477	db_expr_t	dummy1;
478	boolean_t	dummy2;
479	db_expr_t	dummy3;
480	char *		dummy4;
481{
482	db_expr_t	fn_addr;
483#define	MAXARGS		11	/* XXX only 10 are passed */
484	db_expr_t	args[MAXARGS];
485	int		nargs = 0;
486	db_expr_t	retval;
487	typedef db_expr_t fcn_10args_t(db_expr_t, db_expr_t, db_expr_t,
488			    db_expr_t, db_expr_t, db_expr_t, db_expr_t,
489			    db_expr_t, db_expr_t, db_expr_t);
490	fcn_10args_t	*func;
491	int		t;
492
493	if (!db_expression(&fn_addr)) {
494	    db_printf("Bad function\n");
495	    db_flush_lex();
496	    return;
497	}
498	func = (fcn_10args_t *)fn_addr;	/* XXX */
499
500	t = db_read_token();
501	if (t == tLPAREN) {
502	    if (db_expression(&args[0])) {
503		nargs++;
504		while ((t = db_read_token()) == tCOMMA) {
505		    if (nargs == MAXARGS) {
506			db_printf("Too many arguments\n");
507			db_flush_lex();
508			return;
509		    }
510		    if (!db_expression(&args[nargs])) {
511			db_printf("Argument missing\n");
512			db_flush_lex();
513			return;
514		    }
515		    nargs++;
516		}
517		db_unread_token(t);
518	    }
519	    if (db_read_token() != tRPAREN) {
520		db_printf("?\n");
521		db_flush_lex();
522		return;
523	    }
524	}
525	db_skip_to_eol();
526
527	while (nargs < MAXARGS) {
528	    args[nargs++] = 0;
529	}
530
531	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
532			 args[5], args[6], args[7], args[8], args[9] );
533	db_printf("%#lr\n", (long)retval);
534}
535
536static void
537db_kill(dummy1, dummy2, dummy3, dummy4)
538	db_expr_t	dummy1;
539	boolean_t	dummy2;
540	db_expr_t	dummy3;
541	char *		dummy4;
542{
543	db_expr_t old_radix, pid, sig;
544	struct proc *p;
545
546#define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
547
548	/*
549	 * PIDs and signal numbers are typically represented in base
550	 * 10, so make that the default here.  It can, of course, be
551	 * overridden by specifying a prefix.
552	 */
553	old_radix = db_radix;
554	db_radix = 10;
555	/* Retrieve arguments. */
556	if (!db_expression(&sig))
557		DB_ERROR(("Missing signal number\n"));
558	if (!db_expression(&pid))
559		DB_ERROR(("Missing process ID\n"));
560	db_skip_to_eol();
561	if (sig < 0 || sig > _SIG_MAXSIG)
562		DB_ERROR(("Signal number out of range\n"));
563
564	/*
565	 * Find the process in question.  allproc_lock is not needed
566	 * since we're in DDB.
567	 */
568	/* sx_slock(&allproc_lock); */
569	LIST_FOREACH(p, &allproc, p_list)
570	    if (p->p_pid == pid)
571		    break;
572	/* sx_sunlock(&allproc_lock); */
573	if (p == NULL)
574		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
575
576	/* If it's already locked, bail; otherwise, do the deed. */
577	if (PROC_TRYLOCK(p) == 0)
578		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
579	else {
580		psignal(p, sig);
581		PROC_UNLOCK(p);
582	}
583
584out:
585	db_radix = old_radix;
586#undef DB_ERROR
587}
588
589static void
590db_reset(dummy1, dummy2, dummy3, dummy4)
591	db_expr_t	dummy1;
592	boolean_t	dummy2;
593	db_expr_t	dummy3;
594	char *		dummy4;
595{
596
597	cpu_reset();
598}
599
600static void
601db_watchdog(dummy1, dummy2, dummy3, dummy4)
602	db_expr_t	dummy1;
603	boolean_t	dummy2;
604	db_expr_t	dummy3;
605	char *		dummy4;
606{
607	int i;
608
609	/*
610	 * XXX: It might make sense to be able to set the watchdog to a
611	 * XXX: timeout here so that failure or hang as a result of subsequent
612	 * XXX: ddb commands could be recovered by a reset.
613	 */
614
615	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
616}
617
618static void
619db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
620{
621
622	if (kdb_dbbe_select("gdb") != 0)
623		db_printf("The remote GDB backend could not be selected.\n");
624	else
625		db_printf("Step to enter the remote GDB backend.\n");
626}
627
628static void
629db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
630{
631	struct thread *td;
632	db_expr_t radix;
633	pid_t pid;
634	int t;
635
636	/*
637	 * We parse our own arguments. We don't like the default radix.
638	 */
639	radix = db_radix;
640	db_radix = 10;
641	hastid = db_expression(&tid);
642	t = db_read_token();
643	if (t == tCOMMA) {
644		if (!db_expression(&count)) {
645			db_printf("Count missing\n");
646			db_flush_lex();
647			return;
648		}
649	} else {
650		db_unread_token(t);
651		count = -1;
652	}
653	db_skip_to_eol();
654	db_radix = radix;
655
656	if (hastid) {
657		td = kdb_thr_lookup((lwpid_t)tid);
658		if (td == NULL)
659			td = kdb_thr_from_pid((pid_t)tid);
660		if (td == NULL) {
661			db_printf("Thread %d not found\n", (int)tid);
662			return;
663		}
664	} else
665		td = kdb_thread;
666	if (td->td_proc != NULL)
667		pid = td->td_proc->p_pid;
668	else
669		pid = -1;
670	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
671	db_trace_thread(td, count);
672}
673