2 * This software is part of the SBCL system. See the README file for
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
17 #include <sys/types.h>
21 #include <sys/resource.h>
30 #include "interrupt.h"
39 extern boolean isatty(int fd);
41 typedef void cmd(char **ptr);
43 static cmd call_cmd, dump_cmd, print_cmd, quit, help;
44 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
45 static cmd print_context_cmd;
46 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
47 static cmd grab_sigs_cmd;
51 void (*fn)(char **ptr);
53 {"help", "Display this help information.", help},
55 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
56 {"call", "Call FUNCTION with ARG1, ARG2, ...", call_cmd},
57 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
58 {"context", "Print interrupt context number I.", print_context_cmd},
59 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
60 {"d", "dump", dump_cmd},
61 {"exit", "Exit this instance of the monitor.", exit_cmd},
62 {"flush", "Flush all temp variables.", flush_cmd},
63 /* (Classic CMU CL had a "gc" command here, which seems like a
64 * reasonable idea, but the code was stale (incompatible with
65 * gencgc) so I just flushed it. -- WHN 20000814 */
66 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
67 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
68 {"print", "Print object at ADDRESS.", print_cmd},
69 {"p", "print", print_cmd},
70 {"quit", "Quit.", quit},
71 {"regs", "Display current Lisp regs.", regs_cmd},
72 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
73 {"s", "search", search_cmd},
77 static jmp_buf curbuf;
79 static int visible(unsigned char c)
81 if (c < ' ' || c > '~')
87 static void dump_cmd(char **ptr)
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 (is_valid_lisp_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;
131 printf("0x%08lx 0x%04x 0x%04x 0x%02x 0x%02x 0x%02x 0x%02x %c%c%c%c\n", lptr[0], sptr[0], sptr[1], cptr[0], cptr[1], cptr[2], cptr[3], visible(cptr[0]), visible(cptr[1]), visible(cptr[2]), visible(cptr[3]));
134 printf("invalid Lisp-level address\n");
136 addr += displacement;
142 static void print_cmd(char **ptr)
144 lispobj obj = parse_lispobj(ptr);
149 static void regs_cmd(char **ptr)
151 printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer);
152 printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer);
153 #if !defined(ibmrt) && !defined(__i386__)
154 printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
157 printf("BSP\t=\t0x%08X\n", SymbolValue(BINDING_STACK_POINTER));
160 printf("DYNAMIC\t=\t0x%08lX\n", (unsigned long)DYNAMIC_SPACE_START);
161 #if defined(ibmrt) || defined(__i386__)
162 printf("ALLOC\t=\t0x%08lX\n", SymbolValue(ALLOCATION_POINTER));
163 printf("TRIGGER\t=\t0x%08lX\n", SymbolValue(INTERNAL_GC_TRIGGER));
165 printf("ALLOC\t=\t0x%08X\n",
166 (unsigned long)dynamic_space_free_pointer);
167 printf("TRIGGER\t=\t0x%08X\n", (unsigned long)current_auto_gc_trigger);
169 printf("STATIC\t=\t0x%08lX\n", SymbolValue(STATIC_SPACE_FREE_POINTER));
170 printf("RDONLY\t=\t0x%08lX\n", SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
173 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
177 static void search_cmd(char **ptr)
179 static int lastval = 0, lastcount = 0;
180 static lispobj *start = 0, *end = 0;
185 val = parse_number(ptr);
186 if (val < 0 || val > 0xff) {
187 printf("Can only search for single bytes.\n");
191 addr = (lispobj *)PTR((long)parse_addr(ptr));
193 count = parse_number(ptr);
196 /* Speced value and address, but no count. Only one. */
201 /* Speced a value, but no address, so search same range. */
207 /* Speced nothing, search again for val. */
217 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
219 while (search_for_type(val, &end, &count)) {
220 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
224 if (TypeOf(obj) == type_FunctionHeader)
225 print((long)addr | type_FunctionPointer);
226 else if (LowtagOf(obj) == type_OtherImmediate0 || LowtagOf(obj) == type_OtherImmediate1)
227 print((lispobj)addr | type_OtherPointer);
229 print((lispobj)addr);
235 static void call_cmd(char **ptr)
237 lispobj thing = parse_lispobj(ptr), function, result, cons, args[3];
240 if (LowtagOf(thing) == type_OtherPointer) {
241 switch (TypeOf(*(lispobj *)(thing-type_OtherPointer))) {
242 case type_SymbolHeader:
243 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
245 cons = CONS(cons)->cdr) {
246 if (FDEFN(CONS(cons)->car)->name == thing) {
247 thing = CONS(cons)->car;
251 printf("symbol 0x%08lx is undefined.\n", thing);
256 function = FDEFN(thing)->function;
257 if (function == NIL) {
258 printf("fdefn 0x%08lx is undefined.\n", thing);
264 "0x%08lx is not a function pointer, symbol, or fdefn object.\n",
269 else if (LowtagOf(thing) != type_FunctionPointer) {
270 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
278 while (more_p(ptr)) {
280 printf("Too many arguments. (3 at most)\n");
283 args[numargs++] = parse_lispobj(ptr);
288 result = funcall0(function);
291 result = funcall1(function, args[0]);
294 result = funcall2(function, args[0], args[1]);
297 result = funcall3(function, args[0], args[1], args[2]);
304 static void flush_cmd(char **ptr)
309 static void quit(char **ptr)
313 printf("Really quit? [y] ");
315 fgets(buf, sizeof(buf), stdin);
316 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
320 static void help(char **ptr)
324 for (cmd = Cmds; cmd->cmd != NULL; cmd++)
325 if (cmd->help != NULL)
326 printf("%s\t%s\n", cmd->cmd, cmd->help);
331 static void exit_cmd(char **ptr)
336 static void purify_cmd(char **ptr)
341 static void print_context(os_context_t *context)
345 for (i = 0; i < NREGS; i++) {
346 printf("%s:\t", lisp_register_names[i]);
348 brief_print((lispobj)(*os_context_register_addr(context,
351 brief_print((lispobj)(*os_context_register_addr(context,
355 printf("PC:\t\t 0x%08lx\n", *os_context_pc_addr(context));
358 static void print_context_cmd(char **ptr)
362 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
367 index = parse_number(ptr);
369 if ((index >= 0) && (index < free)) {
370 printf("There are %d interrupt contexts.\n", free);
371 printf("Printing context %d\n", index);
372 print_context(lisp_interrupt_contexts[index]);
374 printf("There aren't that many/few contexts.\n");
375 printf("There are %d interrupt contexts.\n", free);
379 printf("There are no interrupt contexts!\n");
381 printf("There are %d interrupt contexts.\n", free);
382 printf("Printing context %d\n", free - 1);
383 print_context(lisp_interrupt_contexts[free - 1]);
388 static void backtrace_cmd(char **ptr)
390 void backtrace(int frames);
394 n = parse_number(ptr);
398 printf("Backtrace:\n");
402 static void catchers_cmd(char **ptr)
404 struct catch_block *catch;
406 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
409 printf("There are no active catchers!\n");
411 while (catch != NULL) {
413 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
414 (unsigned long)catch, (unsigned long)(catch->current_uwp),
415 (unsigned long)(catch->current_cont),
419 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
420 (unsigned long)catch, (unsigned long)(catch->current_uwp),
421 (unsigned long)(catch->current_cont),
422 component_ptr_from_pc(catch->entry_pc) + type_OtherPointer,
425 brief_print((lispobj)catch->tag);
426 catch = catch->previous_catch;
431 static void grab_sigs_cmd(char **ptr)
433 extern void sigint_init(void);
435 printf("Grabbing signals.\n");
439 static void sub_monitor(void)
441 struct cmd *cmd, *found;
443 char *line, *ptr, *token;
449 line = fgets(buf, sizeof(buf), stdin);
456 fprintf(stderr, "\nEOF on something other than a tty.\n");
461 if ((token = parse_token(&ptr)) == NULL)
465 for (cmd = Cmds; cmd->cmd != NULL; cmd++) {
466 if (strcmp(token, cmd->cmd) == 0) {
471 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
479 printf("``%s'' is ambiguous.\n", token);
480 else if (found == NULL)
481 printf("unknown command: ``%s''\n", token);
493 bcopy(curbuf, oldbuf, sizeof(oldbuf));
495 printf("LDB monitor\n");
503 bcopy(oldbuf, curbuf, sizeof(curbuf));
506 void throw_to_monitor()