3 This code was written as part of the CMU Common Lisp project at
4 Carnegie Mellon University, and has been placed in the public domain.
13 #include <sys/resource.h>
18 #include "internals.h"
23 #include "interrupt.h"
35 extern boolean isatty(int fd);
37 typedef void cmd(char **ptr);
39 static cmd call_cmd, dump_cmd, print_cmd, quit, help;
40 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
41 static cmd gc_cmd, print_context_cmd;
42 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
43 static cmd grab_sigs_cmd;
47 void (*fn) (char **ptr);
50 {"help", "Display this info", help},
52 {"backtrace", "backtrace up to N frames", backtrace_cmd},
53 {"call", "call FUNCTION with ARG1, ARG2, ...", call_cmd},
54 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
55 {"context", "print interrupt context number I.", print_context_cmd},
56 {"dump", "dump memory starting at ADDRESS for COUNT words.", dump_cmd},
57 {"d", NULL, dump_cmd},
58 {"exit", "Exit this instance of the monitor.", exit_cmd},
59 {"flush", "flush all temp variables.", flush_cmd},
60 {"gc", "collect garbage (caveat collector).", gc_cmd},
61 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
62 {"purify", "purify (caveat purifier).", purify_cmd},
63 {"print", "print object at ADDRESS.", print_cmd},
64 {"p", NULL, print_cmd},
65 {"quit", "quit.", quit},
66 {"regs", "display current lisp regs.", regs_cmd},
67 {"search", "search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
68 {"s", NULL, search_cmd},
74 static jmp_buf curbuf;
78 visable(unsigned char c)
80 if (c < ' ' || c > '~')
89 static char *lastaddr = 0;
90 static int lastcount = 20;
92 char *addr = lastaddr;
93 int count = lastcount, displacement;
96 addr = parse_addr(ptr);
99 count = parse_number(ptr);
103 printf("COUNT must be non-zero.\n");
116 while (count-- > 0) {
118 printf("0x%08lX: ", (unsigned long) addr);
120 printf("0x%08X: ", (u32) addr);
122 if (valid_addr((os_vm_address_t) addr)) {
124 unsigned long *lptr = (unsigned long *) addr;
126 u32 *lptr = (unsigned long *) addr;
128 unsigned short *sptr = (unsigned short *) addr;
129 unsigned char *cptr = (unsigned char *) addr;
132 ("0x%08lx 0x%04x 0x%04x 0x%02x 0x%02x 0x%02x 0x%02x %c%c%c%c\n",
133 lptr[0], sptr[0], sptr[1], cptr[0], cptr[1], cptr[2], cptr[3],
134 visable(cptr[0]), visable(cptr[1]), visable(cptr[2]),
137 printf("invalid address\n");
139 addr += displacement;
146 print_cmd(char **ptr)
148 lispobj obj = parse_lispobj(ptr);
156 printf("CSP\t=\t0x%08lX\n", (unsigned long) current_control_stack_pointer);
157 printf("FP\t=\t0x%08lX\n", (unsigned long) current_control_frame_pointer);
158 #if !defined(ibmrt) && !defined(i386) && !defined(__x86_64)
159 printf("BSP\t=\t0x%08lX\n", (unsigned long) current_binding_stack_pointer);
161 #if defined(i386) || defined(__x86_64)
162 printf("BSP\t=\t0x%08lX\n", SymbolValue(BINDING_STACK_POINTER));
165 printf("DYNAMIC\t=\t0x%08lX\n", (unsigned long) current_dynamic_space);
166 #if defined(ibmrt) || defined(i386) || defined(__x86_64)
167 printf("ALLOC\t=\t0x%08lX\n", SymbolValue(ALLOCATION_POINTER));
168 printf("TRIGGER\t=\t0x%08lX\n", SymbolValue(INTERNAL_GC_TRIGGER));
170 printf("ALLOC\t=\t0x%08lX\n",
171 (unsigned long) current_dynamic_space_free_pointer);
172 printf("TRIGGER\t=\t0x%08lX\n", (unsigned long) current_auto_gc_trigger);
174 printf("STATIC\t=\t0x%08lX\n", SymbolValue(STATIC_SPACE_FREE_POINTER));
175 printf("RDONLY\t=\t0x%08lX\n", SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
178 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
183 search_cmd(char **ptr)
185 static int lastval = 0, lastcount = 0;
186 static lispobj *start = 0, *end = 0;
191 val = parse_number(ptr);
192 if (val < 0 || val > 0xff) {
193 printf("Can only search for single bytes.\n");
197 addr = (lispobj *) PTR((long) parse_addr(ptr));
199 count = parse_number(ptr);
201 /* Speced value and address, but no count. Only one. */
205 /* Speced a value, but no address, so search same range. */
210 /* Speced nothing, search again for val. */
220 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long) end);
222 while (search_for_type(val, &end, &count)) {
223 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long) end);
227 if (TypeOf(obj) == type_FunctionHeader)
228 print((long) addr | type_FunctionPointer);
229 else if (LowtagOf(obj) == type_OtherImmediate0
231 type_OtherImmediate1) print((lispobj) addr |
234 print((lispobj) addr);
243 lispobj thing = parse_lispobj(ptr);
244 lispobj function, cons, args[3];
245 lispobj result = NIL;
249 if (LowtagOf(thing) == type_OtherPointer) {
250 switch (TypeOf(*(lispobj *) (thing - type_OtherPointer))) {
251 case type_SymbolHeader:
252 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
253 cons != NIL; cons = CONS(cons)->cdr) {
254 if (FDEFN(CONS(cons)->car)->name == thing) {
255 thing = CONS(cons)->car;
259 printf("symbol 0x%08lx is undefined.\n", thing);
264 function = FDEFN(thing)->function;
265 if (function == NIL) {
266 printf("fdefn 0x%08lx is undefined.\n", thing);
272 ("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
276 } else if (LowtagOf(thing) != type_FunctionPointer) {
277 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
284 while (more_p(ptr)) {
286 printf("Too many arguments. 3 at most.\n");
289 args[numargs++] = parse_lispobj(ptr);
294 result = funcall0(function);
297 result = funcall1(function, args[0]);
300 result = funcall2(function, args[0], args[1]);
303 result = funcall3(function, args[0], args[1], args[2]);
311 flush_cmd(char **ptr)
322 printf("Really quit? [y] ");
324 result = fgets(buf, sizeof(buf), stdin);
325 if (result && (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')) {
335 for (cmd = Cmds; cmd->cmd != NULL; cmd++)
336 if (cmd->help != NULL)
337 printf("%s\t%s\n", cmd->cmd, cmd->help);
355 purify_cmd(char **ptr)
361 print_context(os_context_t * context)
365 for (i = 0; i < NREGS; i++) {
366 printf("%s:\t", lisp_register_names[i]);
367 #if defined(i386) || defined(__x86_64)
368 brief_print((lispobj) SC_REG(context, i * 2));
370 brief_print((lispobj) SC_REG(context, i));
373 printf("PC:\t\t 0x%08lx\n", (unsigned long) SC_PC(context));
377 print_context_cmd(char **ptr)
381 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX) >> 2;
386 index = parse_number(ptr);
388 if ((index >= 0) && (index < free)) {
389 printf("There are %d interrupt contexts.\n", free);
390 printf("Printing context %d\n", index);
391 print_context(lisp_interrupt_contexts[index]);
393 printf("There aren't that many/few contexts.\n");
394 printf("There are %d interrupt contexts.\n", free);
398 printf("There are no interrupt contexts!\n");
400 printf("There are %d interrupt contexts.\n", free);
401 printf("Printing context %d\n", free - 1);
402 print_context(lisp_interrupt_contexts[free - 1]);
408 backtrace_cmd(char **ptr)
410 void backtrace(int frames);
414 n = parse_number(ptr);
418 printf("Backtrace:\n");
423 catchers_cmd(char **ptr)
425 struct catch_block *catch;
427 catch = (struct catch_block *) SymbolValue(CURRENT_CATCH_BLOCK);
430 printf("There are no active catchers!\n");
432 while (catch != NULL) {
433 #if !(defined(i386) || defined(__x86_64))
435 ("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
436 (unsigned long) catch, (unsigned long) (catch->current_uwp),
437 (unsigned long) (catch->current_cont), catch->current_code,
441 ("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%p\n\tentry: 0x%08lx\n\ttag: ",
442 (unsigned long) catch, (unsigned long) (catch->current_uwp),
443 (unsigned long) (catch->current_cont),
444 component_ptr_from_pc((lispobj *) catch->entry_pc) +
445 type_OtherPointer, catch->entry_pc);
447 brief_print((lispobj) catch->tag);
448 catch = catch->previous_catch;
454 grab_sigs_cmd(char **ptr)
456 extern void sigint_init(void);
458 printf("Grabbing signals.\n");
465 struct cmd *cmd, *found;
467 char *line, *ptr, *token;
473 line = fgets(buf, sizeof(buf), stdin);
478 * We can no longer read anything from stdin, so
479 * just exit this loop instead of spewing an
480 * endless stream of prompts. This also means we
481 * can't use ldb anymore because stdin is
486 fprintf(stderr, "\nEOF on something other than a tty.\n");
491 if ((token = parse_token(&ptr)) == NULL)
495 for (cmd = Cmds; cmd->cmd != NULL; cmd++) {
496 if (strcmp(token, cmd->cmd) == 0) {
500 } else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
508 printf("``%s'' is ambiguous.\n", token);
509 else if (found == NULL)
510 printf("unknown command: ``%s''\n", token);
523 memcpy(oldbuf, curbuf, sizeof(jmp_buf));
525 printf("LDB monitor\n");
533 memcpy(curbuf, oldbuf, sizeof(jmp_buf));
537 throw_to_monitor(void)