]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_command.c
xnu-1504.9.26.tar.gz
[apple/xnu.git] / osfmk / ddb / db_command.c
1 /*
2 * Copyright (c) 2000-2008 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 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 * Author: David B. Golub, Carnegie Mellon University
60 * Date: 7/90
61 */
62
63 /*
64 * Command dispatcher.
65 */
66 #include <norma_vm.h>
67 #ifdef AT386
68 #include <norma_scsi.h>
69 #endif /* AT386 */
70
71 #include <mach/boolean.h>
72 #include <string.h>
73 #include <machine/db_machdep.h>
74
75 #if defined(__alpha)
76 # include <kdebug.h>
77 # if KDEBUG
78 # include <machine/kdebug.h>
79 # endif
80 #endif /* defined(__alpha) */
81
82 #include <ddb/db_lex.h>
83 #include <ddb/db_output.h>
84 #include <ddb/db_break.h>
85 #include <ddb/db_command.h>
86 #include <ddb/db_cond.h>
87 #include <ddb/db_examine.h>
88 #include <ddb/db_expr.h>
89 #if defined(__ppc__)
90 #include <ppc/db_low_trace.h>
91 #endif
92 #include <ddb/db_macro.h>
93 #include <ddb/db_print.h>
94 #include <ddb/db_run.h>
95 #include <ddb/db_task_thread.h>
96 #include <ddb/db_variables.h>
97 #include <ddb/db_watch.h>
98 #include <ddb/db_write_cmd.h>
99
100 #include <machine/setjmp.h>
101 #include <kern/thread.h>
102
103 #include <kern/misc_protos.h>
104 #include <vm/vm_print.h>
105 #include <ipc/ipc_print.h>
106 #include <kern/kern_print.h>
107 #include <machine/db_machdep.h> /* For db_stack_trace_cmd(). */
108 #include <kern/zalloc.h> /* For db_show_one_zone, db_show_all_zones. */
109 #include <kern/lock.h> /* For db_show_all_slocks(). */
110
111 #if NORMA_VM
112 #include <xmm/xmm_obj.h>
113 #endif /* NORMA_VM */
114
115 /*
116 * Exported global variables
117 */
118 boolean_t db_cmd_loop_done;
119 jmp_buf_t *db_recover = 0;
120 db_addr_t db_dot;
121 db_addr_t db_last_addr;
122 db_addr_t db_prev;
123 db_addr_t db_next;
124
125 /*
126 * if 'ed' style: 'dot' is set at start of last item printed,
127 * and '+' points to next line.
128 * Otherwise: 'dot' points to next item, '..' points to last.
129 */
130 boolean_t db_ed_style = TRUE;
131
132 /*
133 * Results of command search.
134 */
135 #define CMD_UNIQUE 0
136 #define CMD_FOUND 1
137 #define CMD_NONE 2
138 #define CMD_AMBIGUOUS 3
139 #define CMD_HELP 4
140
141 /* Prototypes for functions local to this file. XXX -- should be static!
142 */
143
144 void db_command(
145 struct db_command **last_cmdp, /* IN_OUT */
146 db_expr_t *last_countp, /* IN_OUT */
147 char *last_modifp, /* IN_OUT */
148 struct db_command *cmd_table);
149
150 void db_help_cmd(void);
151
152 void db_fncall(void);
153
154 void db_cmd_list(struct db_command *table);
155
156 int db_cmd_search(
157 char * name,
158 struct db_command * table,
159 struct db_command ** cmdp); /* out */
160
161 void db_command_list(
162 struct db_command **last_cmdp, /* IN_OUT */
163 db_expr_t *last_countp, /* IN_OUT */
164 char *last_modifp, /* IN_OUT */
165 struct db_command *cmd_table);
166
167 /*
168 * Search for command prefix.
169 */
170 int
171 db_cmd_search(
172 char * name,
173 struct db_command * table,
174 struct db_command ** cmdp) /* out */
175 {
176 struct db_command *cmd;
177 int result = CMD_NONE;
178
179 for (cmd = table; cmd->name != 0; cmd++) {
180 register char *lp;
181 const char *rp;
182 register int c;
183
184 lp = name;
185 rp = cmd->name;
186 while ((c = *lp) == *rp) {
187 if (c == 0) {
188 /* complete match */
189 *cmdp = cmd;
190 return (CMD_UNIQUE);
191 }
192 lp++;
193 rp++;
194 }
195 if (c == 0) {
196 /* end of name, not end of command -
197 partial match */
198 if (result == CMD_FOUND) {
199 result = CMD_AMBIGUOUS;
200 /* but keep looking for a full match -
201 this lets us match single letters */
202 }
203 else {
204 *cmdp = cmd;
205 result = CMD_FOUND;
206 }
207 }
208 }
209 if (result == CMD_NONE) {
210 /* check for 'help' */
211 if (!strncmp(name, "help", strlen(name)))
212 result = CMD_HELP;
213 }
214 return (result);
215 }
216
217 void
218 db_cmd_list(struct db_command *table)
219 {
220 struct db_command *new;
221 struct db_command *old;
222 struct db_command *cur;
223 unsigned int l;
224 unsigned int len;
225
226 len = 1;
227 for (cur = table; cur->name != 0; cur++)
228 if ((l = strlen(cur->name)) >= len)
229 len = l + 1;
230
231 old = (struct db_command *)0;
232 for (;;) {
233 new = (struct db_command *)0;
234 for (cur = table; cur->name != 0; cur++)
235 if ((new == (struct db_command *)0 ||
236 strncmp(cur->name, new->name, strlen(cur->name)) < 0) &&
237 (old == (struct db_command *)0 ||
238 strncmp(cur->name, old->name, strlen(cur->name)) > 0))
239 new = cur;
240 if (new == (struct db_command *)0)
241 return;
242 db_reserve_output_position(len);
243 db_printf("%-*s", len, new->name);
244 old = new;
245 }
246 }
247
248 void
249 db_command(
250 struct db_command **last_cmdp, /* IN_OUT */
251 db_expr_t *last_countp, /* IN_OUT */
252 char *last_modifp, /* IN_OUT */
253 struct db_command *cmd_table)
254 {
255 struct db_command *cmd;
256 int t;
257 char modif[TOK_STRING_SIZE];
258 char *modifp = &modif[0];
259 db_expr_t addr, count;
260 boolean_t have_addr = FALSE;
261 int result;
262
263 t = db_read_token();
264 if (t == tEOL || t == tSEMI_COLON) {
265 /* empty line repeats last command, at 'next' */
266 cmd = *last_cmdp;
267 count = *last_countp;
268 modifp = last_modifp;
269 addr = (db_expr_t)db_next;
270 have_addr = FALSE;
271 if (t == tSEMI_COLON)
272 db_unread_token(t);
273 }
274 else if (t == tEXCL) {
275 db_fncall();
276 return;
277 }
278 else if (t != tIDENT) {
279 db_printf("?\n");
280 db_flush_lex();
281 return;
282 }
283 else {
284 /*
285 * Search for command
286 */
287 while (cmd_table) {
288 result = db_cmd_search(db_tok_string,
289 cmd_table,
290 &cmd);
291 switch (result) {
292 case CMD_NONE:
293 if (db_exec_macro(db_tok_string) == 0)
294 return;
295 db_printf("No such command \"%s\"\n", db_tok_string);
296 db_flush_lex();
297 return;
298 case CMD_AMBIGUOUS:
299 db_printf("Ambiguous\n");
300 db_flush_lex();
301 return;
302 case CMD_HELP:
303 db_cmd_list(cmd_table);
304 db_flush_lex();
305 return;
306 default:
307 break;
308 }
309 if ((cmd_table = cmd->more) != 0) {
310 t = db_read_token();
311 if (t != tIDENT) {
312 db_cmd_list(cmd_table);
313 db_flush_lex();
314 return;
315 }
316 }
317 }
318
319 if ((cmd->flag & CS_OWN) == 0) {
320 /*
321 * Standard syntax:
322 * command [/modifier] [addr] [,count]
323 */
324 t = db_read_token();
325 if (t == tSLASH) {
326 t = db_read_token();
327 if (t != tIDENT) {
328 db_printf("Bad modifier \"/%s\"\n", db_tok_string);
329 db_flush_lex();
330 return;
331 }
332 strlcpy(modif, db_tok_string, TOK_STRING_SIZE);
333 }
334 else {
335 db_unread_token(t);
336 modif[0] = '\0';
337 }
338
339 if (db_expression(&addr)) {
340 db_dot = (db_addr_t) addr;
341 db_last_addr = db_dot;
342 have_addr = TRUE;
343 }
344 else {
345 addr = (db_expr_t) db_dot;
346 have_addr = FALSE;
347 }
348 t = db_read_token();
349 if (t == tCOMMA) {
350 if (!db_expression(&count)) {
351 db_printf("Count missing after ','\n");
352 db_flush_lex();
353 return;
354 }
355 }
356 else {
357 db_unread_token(t);
358 count = -1;
359 }
360 }
361 }
362 if (cmd != 0) {
363 /*
364 * Execute the command.
365 */
366 (*cmd->fcn)(addr, have_addr, count, modifp);
367
368 if (cmd->flag & CS_SET_DOT) {
369 /*
370 * If command changes dot, set dot to
371 * previous address displayed (if 'ed' style).
372 */
373 if (db_ed_style) {
374 db_dot = db_prev;
375 }
376 else {
377 db_dot = db_next;
378 }
379 }
380 else {
381 /*
382 * If command does not change dot,
383 * set 'next' location to be the same.
384 */
385 db_next = db_dot;
386 }
387 }
388 *last_cmdp = cmd;
389 *last_countp = count;
390 strlcpy(last_modifp, modifp, TOK_STRING_SIZE);
391 }
392
393 void
394 db_command_list(
395 struct db_command **last_cmdp, /* IN_OUT */
396 db_expr_t *last_countp, /* IN_OUT */
397 char *last_modifp, /* IN_OUT */
398 struct db_command *cmd_table)
399 {
400 do {
401 db_command(last_cmdp, last_countp, last_modifp, cmd_table);
402 db_skip_to_eol();
403 } while (db_read_token() == tSEMI_COLON && db_cmd_loop_done == 0);
404 }
405
406
407 extern void db_system_stats(void);
408
409 struct db_command db_show_all_cmds[] = {
410 {
411 .name = "acts",
412 .fcn = db_show_all_acts,
413 },
414 {
415 .name = "spaces",
416 .fcn = db_show_all_spaces,
417 },
418 {
419 .name = "tasks",
420 .fcn = db_show_all_acts,
421 },
422 /* temporary alias for sanity preservation */
423 {
424 .name ="threads",
425 db_show_all_acts,
426 },
427 {
428 .name = "zones",
429 .fcn = db_show_all_zones,
430 },
431 {
432 .name = "vmtask",
433 .fcn = db_show_all_task_vm,
434 },
435 {
436 .name = (const char *)NULL,
437 },
438 };
439
440 /* XXX */
441
442 extern void db_show_thread_log(void);
443 extern void db_show_etap_log(db_expr_t, int, db_expr_t, char *);
444
445 struct db_command db_show_cmds[] = {
446 {
447 .name = "all",
448 .more = db_show_all_cmds
449 },
450 {
451 .name = "registers",
452 .fcn = db_show_regs,
453 },
454 {
455 .name = "variables",
456 .fcn = db_show_variable,
457 .flag = CS_OWN,
458 },
459 {
460 .name = "breaks",
461 .fcn = db_listbreak_cmd,
462 },
463 {
464 .name = "watches",
465 .fcn = db_listwatch_cmd,
466 },
467 {
468 .name = "task",
469 .fcn = db_show_one_task,
470 },
471 {
472 .name = "act",
473 .fcn = db_show_one_act,
474 },
475 {
476 .name = "shuttle",
477 .fcn = db_show_shuttle,
478 },
479 #if 0
480 {
481 .name = "thread",
482 .fcn = db_show_one_thread,
483 },
484 #endif
485 {
486 .name = "vmtask",
487 .fcn = db_show_one_task_vm,
488 },
489 {
490 .name = "macro",
491 .fcn = (db_func)db_show_macro,
492 .flag = CS_OWN,
493 },
494 {
495 .name = "runq",
496 .fcn = (db_func)db_show_runq,
497 },
498 {
499 .name = "map",
500 .fcn = (db_func)vm_map_print,
501 },
502 {
503 .name = "object",
504 .fcn = vm_object_print,
505 },
506 {
507 .name = "page",
508 .fcn = (db_func)vm_page_print,
509 },
510 {
511 .name = "copy",
512 .fcn = (db_func)vm_map_copy_print,
513 },
514 {
515 .name = "port",
516 .fcn = (db_func)ipc_port_print,
517 },
518 {
519 .name = "pset",
520 .fcn = (db_func)ipc_pset_print,
521 },
522 {
523 .name = "kmsg",
524 .fcn = (db_func)ipc_kmsg_print,
525 },
526 {
527 .name = "msg",
528 .fcn = (db_func)ipc_msg_print,
529 },
530 {
531 .name = "ipc_port",
532 .fcn = db_show_port_id,
533 },
534 #if NORMA_VM
535 {
536 .name = "xmm_obj",
537 .fcn = (db_func)xmm_obj_print,
538 },
539 {
540 .name = "xmm_reply",
541 .fcn = (db_func)xmm_reply_print,
542 },
543 #endif /* NORMA_VM */
544 {
545 .name = "space",
546 .fcn = db_show_one_space,
547 },
548 {
549 .name = "system",
550 .fcn = (db_func)db_system_stats,
551 },
552 {
553 .name = "zone",
554 .fcn = db_show_one_zone,
555 },
556 {
557 .name = "lock",
558 .fcn = (db_func)db_show_one_lock,
559 },
560 {
561 .name = "simple_lock",
562 .fcn = (db_func)db_show_one_simple_lock,
563 },
564 {
565 .name = "thread_log",
566 (db_func)db_show_thread_log,
567 },
568 {
569 .name = "shuttle",
570 .fcn = db_show_shuttle,
571 },
572 {
573 .name = (const char *)NULL,
574 },
575 };
576
577 #define db_switch_cpu kdb_on
578
579 struct db_command db_command_table[] = {
580 #if DB_MACHINE_COMMANDS
581 /* this must be the first entry, if it exists */
582 {
583 .name = "machine",
584 },
585 #endif /* DB_MACHINE_COMMANDS */
586 {
587 .name = "print",
588 .fcn = (db_func)db_print_cmd,
589 .flag = CS_OWN,
590 },
591 {
592 .name = "examine",
593 .fcn = db_examine_cmd,
594 .flag = CS_MORE|CS_SET_DOT,
595 },
596 {
597 .name = "x",
598 .fcn = db_examine_cmd,
599 .flag = CS_MORE|CS_SET_DOT,
600 },
601 {
602 .name = "xf",
603 .fcn = db_examine_forward,
604 .flag = CS_SET_DOT,
605 },
606 {
607 .name = "xb",
608 .fcn = db_examine_backward,
609 .flag = CS_SET_DOT,
610 },
611 {
612 .name = "search",
613 .fcn = (db_func)db_search_cmd,
614 .flag = CS_OWN|CS_SET_DOT,
615 },
616 {
617 .name = "set",
618 .fcn = (db_func)db_set_cmd,
619 .flag = CS_OWN,
620 },
621 {
622 .name = "write",
623 .fcn = db_write_cmd,
624 .flag = CS_MORE|CS_SET_DOT,
625 },
626 {
627 .name = "w",
628 .fcn = db_write_cmd,
629 .flag = CS_MORE|CS_SET_DOT,
630 },
631 {
632 .name = "delete",
633 .fcn = (db_func)db_delete_cmd,
634 .flag = CS_OWN,
635 },
636 {
637 .name = "d",
638 .fcn = (db_func)db_delete_cmd,
639 .flag = CS_OWN,
640 },
641 {
642 .name = "break",
643 .fcn = db_breakpoint_cmd,
644 .flag = CS_MORE,
645 },
646 {
647 .name = "dwatch",
648 .fcn = db_deletewatch_cmd,
649 .flag = CS_MORE,
650 },
651 {
652 .name = "watch",
653 .fcn = db_watchpoint_cmd,
654 .flag = CS_MORE,
655 },
656 {
657 .name = "step",
658 .fcn = db_single_step_cmd,
659 },
660 {
661 .name = "s",
662 .fcn = db_single_step_cmd,
663 },
664 {
665 .name = "continue",
666 .fcn = db_continue_cmd,
667 },
668 {
669 .name = "c",
670 .fcn = db_continue_cmd,
671 },
672 {
673 .name = "gdb",
674 .fcn = db_continue_gdb,
675 },
676 {
677 .name = "until",
678 .fcn = db_trace_until_call_cmd,
679 },
680
681 /* As per request of DNoveck, CR1550, leave this disabled */
682 #if 0 /* until CR1440 is fixed, to avoid toe-stubbing */
683 {
684 .name = "next",
685 .fcn = db_trace_until_matching_cmd,
686 },
687 #endif
688 {
689 .name = "match",
690 .fcn = db_trace_until_matching_cmd,
691 },
692 {
693 .name = "trace",
694 .fcn = db_stack_trace_cmd,
695 },
696 {
697 .name = "cond",
698 .fcn = (db_func)db_cond_cmd,
699 .flag = CS_OWN,
700 },
701 {
702 .name = "call",
703 .fcn = (db_func)db_fncall,
704 .flag = CS_OWN,
705 },
706 {
707 .name = "macro",
708 .fcn = (db_func)db_def_macro_cmd,
709 .flag = CS_OWN,
710 },
711 {
712 .name = "dmacro",
713 .fcn = (db_func)db_del_macro_cmd,
714 .flag = CS_OWN,
715 },
716 {
717 .name = "show",
718 .more = db_show_cmds
719 },
720 {
721 .name = "cpu",
722 .fcn = (db_func)db_switch_cpu,
723 },
724 {
725 .name = "dr",
726 .fcn = db_display_real,
727 .flag = CS_MORE|CS_SET_DOT,
728 },
729 {
730 .name = "di",
731 .fcn = db_display_iokit,
732 .flag = CS_MORE,
733 },
734 {
735 .name = "dk",
736 .fcn = db_display_kmod,
737 .flag = CS_MORE,
738 },
739
740 {
741 .name = "reboot",
742 (db_func)db_reboot,
743 },
744 #if !defined(__ppc__)
745 {
746 .name = "ms",
747 .fcn = db_msr,
748 .flag = CS_MORE,
749 },
750 {
751 .name = "cp",
752 .fcn = db_cpuid,
753 .flag = CS_MORE,
754 },
755 {
756 .name = "da",
757 .fcn = db_apic,
758 .flag = CS_MORE,
759 },
760 #endif /* !__ppc__ */
761 #if defined(__ppc__)
762 {
763 .name = "lt",
764 .fcn = db_low_trace,
765 .flag = CS_MORE|CS_SET_DOT,
766 },
767 {
768 .name = "dl",
769 .fcn = db_display_long,
770 .flag = CS_MORE|CS_SET_DOT,
771 },
772 {
773 .name = "dc",
774 .fcn = db_display_char,
775 .flag = CS_MORE|CS_SET_DOT,
776 },
777 {
778 .name = "dv",
779 .fcn = db_display_virtual,
780 .flag = CS_MORE|CS_SET_DOT,
781 },
782 {
783 .name = "dm",
784 .fcn = db_display_mappings,
785 .flag = CS_MORE|CS_SET_DOT,
786 },
787 {
788 .name = "dh",
789 .fcn = db_display_hash,
790 .flag = CS_MORE|CS_SET_DOT,
791 },
792 {
793 .name = "dp",
794 .fcn = db_display_pmap,
795 .flag = CS_MORE,
796 },
797 {
798 .name = "ds",
799 .fcn = db_display_save,
800 .flag = CS_MORE|CS_SET_DOT,
801 },
802 {
803 .name = "dx",
804 .fcn = db_display_xregs,
805 .flag = CS_MORE|CS_SET_DOT,
806 },
807 {
808 .name = "gs",
809 .fcn = db_gsnoop,
810 .flag = CS_MORE,
811 },
812 {
813 .name = "cm",
814 .fcn = db_check_mappings,
815 .flag = CS_MORE,
816 },
817 {
818 .name = "cp",
819 .fcn = db_check_pmaps,
820 .flag = CS_MORE,
821 },
822 #endif /* __ppc__ */
823 {
824 .name = (const char *)NULL,
825 },
826 };
827
828 /* this function should be called to install the machine dependent
829 commands. It should be called before the debugger is enabled */
830 void db_machine_commands_install(struct db_command *ptr)
831 {
832 db_command_table[0].more = ptr;
833 return;
834 }
835
836
837 struct db_command *db_last_command = 0;
838 db_expr_t db_last_count = 0;
839 char db_last_modifier[TOK_STRING_SIZE] = { '\0' };
840
841 void
842 db_help_cmd(void)
843 {
844 struct db_command *cmd = db_command_table;
845
846 while (cmd->name != 0) {
847 db_printf("%-12s", cmd->name);
848 db_end_line();
849 cmd++;
850 }
851 }
852
853 int (*ddb_display)(void);
854
855 extern int db_output_line;
856 extern int db_macro_level;
857
858 void
859 db_command_loop(void)
860 {
861 jmp_buf_t db_jmpbuf;
862 jmp_buf_t *prev = db_recover;
863
864 /*
865 * Initialize 'prev' and 'next' to dot.
866 */
867 db_prev = db_dot;
868 db_next = db_dot;
869
870 if (ddb_display)
871 (*ddb_display)();
872
873 db_cmd_loop_done = 0;
874 while (!db_cmd_loop_done) {
875 (void) _setjmp(db_recover = &db_jmpbuf);
876 db_macro_level = 0;
877 if (db_print_position() != 0)
878 db_printf("\n");
879 db_output_line = 0;
880 db_indent = 0;
881 db_reset_more();
882 db_output_prompt();
883
884 (void) db_read_line("!!");
885 db_command_list(&db_last_command, &db_last_count,
886 db_last_modifier, db_command_table);
887 }
888
889 db_recover = prev;
890 }
891
892 boolean_t
893 db_exec_cmd_nest(
894 const char *cmd,
895 int size)
896 {
897 struct db_lex_context lex_context;
898
899 db_cmd_loop_done = 0;
900 if (cmd) {
901 db_save_lex_context(&lex_context);
902 db_switch_input(cmd, size);
903 }
904 db_command_list(&db_last_command, &db_last_count,
905 db_last_modifier, db_command_table);
906 if (cmd)
907 db_restore_lex_context(&lex_context);
908 return(db_cmd_loop_done == 0);
909 }
910
911 void
912 db_error(const char *s)
913 {
914 db_macro_level = 0;
915 if (db_recover) {
916 if (s > (char *)1)
917 db_printf(s);
918 db_flush_lex();
919 _longjmp(db_recover, (s == (char *)1) ? 2 : 1);
920 }
921 else
922 {
923 if (s > (char *)1)
924 db_printf(s);
925 panic("db_error");
926 }
927 }
928
929
930 /*
931 * Call random function:
932 * !expr(arg,arg,arg)
933 */
934 void
935 db_fncall(void)
936 {
937 db_expr_t fn_addr;
938 #define MAXARGS 11
939 uint32_t args[MAXARGS];
940 db_expr_t argwork;
941 int nargs = 0;
942 uint32_t retval;
943 uint32_t (*func)(uint32_t, ...);
944 int t;
945
946 if (!db_expression(&fn_addr)) {
947 db_printf("Bad function \"%s\"\n", db_tok_string);
948 db_flush_lex();
949 return;
950 }
951 func = (uint32_t (*) (uint32_t, ...))(unsigned long)fn_addr;
952
953 t = db_read_token();
954 if (t == tLPAREN) {
955 if (db_expression(&argwork)) {
956 args[nargs] = (uint32_t)argwork;
957 nargs++;
958 while ((t = db_read_token()) == tCOMMA) {
959 if (nargs == MAXARGS) {
960 db_printf("Too many arguments\n");
961 db_flush_lex();
962 return;
963 }
964 if (!db_expression(&argwork)) {
965 db_printf("Argument missing\n");
966 db_flush_lex();
967 return;
968 }
969 args[nargs] = (uint32_t)argwork;
970 nargs++;
971 }
972 db_unread_token(t);
973 }
974 if (db_read_token() != tRPAREN) {
975 db_printf("?\n");
976 db_flush_lex();
977 return;
978 }
979 }
980 while (nargs < MAXARGS) {
981 args[nargs++] = 0;
982 }
983
984 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
985 args[5], args[6], args[7], args[8], args[9] );
986 db_printf(" %#n\n", retval);
987 }
988
989 boolean_t
990 db_option(
991 const char *modif,
992 int option)
993 {
994 const char *p;
995
996 for (p = modif; *p; p++)
997 if (*p == option)
998 return(TRUE);
999 return(FALSE);
1000 }