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.
13 #include <sys/types.h>
17 #include <sys/resource.h>
26 #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 print_context_cmd;
42 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
43 static cmd grab_sigs_cmd;
47 void (*fn)(char **ptr);
49 {"help", "Display this help information.", help},
51 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
52 {"call", "Call FUNCTION with ARG1, ARG2, ...", call_cmd},
53 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
54 {"context", "Print interrupt context number I.", print_context_cmd},
55 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
56 {"d", "dump", dump_cmd},
57 {"exit", "Exit this instance of the monitor.", exit_cmd},
58 {"flush", "Flush all temp variables.", flush_cmd},
59 /* (Classic CMU CL had a "gc" command here, which seems like a
60 * reasonable idea, but the code was stale (incompatible with
61 * gencgc) so I just flushed it. -- WHN 20000814 */
62 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
63 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
64 {"print", "Print object at ADDRESS.", print_cmd},
65 {"p", "print", print_cmd},
66 {"quit", "Quit.", quit},
67 {"regs", "Display current Lisp regs.", regs_cmd},
68 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
69 {"s", "search", search_cmd},
73 static jmp_buf curbuf;
75 static int visible(unsigned char c)
77 if (c < ' ' || c > '~')
83 static void dump_cmd(char **ptr)
85 static char *lastaddr = 0;
86 static int lastcount = 20;
88 char *addr = lastaddr;
89 int count = lastcount, displacement;
92 addr = parse_addr(ptr);
95 count = parse_number(ptr);
99 printf("COUNT must be non-zero.\n");
112 while (count-- > 0) {
114 printf("0x%08lX: ", (unsigned long) addr);
116 printf("0x%08X: ", (u32) addr);
118 if (is_valid_lisp_addr((os_vm_address_t)addr)) {
120 unsigned long *lptr = (unsigned long *)addr;
122 u32 *lptr = (unsigned long *)addr;
124 unsigned short *sptr = (unsigned short *)addr;
125 unsigned char *cptr = (unsigned char *)addr;
127 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]));
130 printf("invalid Lisp-level address\n");
132 addr += displacement;
138 static void print_cmd(char **ptr)
140 lispobj obj = parse_lispobj(ptr);
145 static void regs_cmd(char **ptr)
147 printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer);
148 printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer);
149 #if !defined(ibmrt) && !defined(__i386__)
150 printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
153 printf("BSP\t=\t0x%08X\n", SymbolValue(BINDING_STACK_POINTER));
156 printf("DYNAMIC\t=\t0x%08lX\n", (unsigned long)DYNAMIC_SPACE_START);
157 #if defined(ibmrt) || defined(__i386__)
158 printf("ALLOC\t=\t0x%08lX\n", SymbolValue(ALLOCATION_POINTER));
159 printf("TRIGGER\t=\t0x%08lX\n", SymbolValue(INTERNAL_GC_TRIGGER));
161 printf("ALLOC\t=\t0x%08X\n",
162 (unsigned long)dynamic_space_free_pointer);
163 printf("TRIGGER\t=\t0x%08X\n", (unsigned long)current_auto_gc_trigger);
165 printf("STATIC\t=\t0x%08lX\n", SymbolValue(STATIC_SPACE_FREE_POINTER));
166 printf("RDONLY\t=\t0x%08lX\n", SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
169 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
173 static void search_cmd(char **ptr)
175 static int lastval = 0, lastcount = 0;
176 static lispobj *start = 0, *end = 0;
181 val = parse_number(ptr);
182 if (val < 0 || val > 0xff) {
183 printf("Can only search for single bytes.\n");
187 addr = (lispobj *)PTR((long)parse_addr(ptr));
189 count = parse_number(ptr);
192 /* Speced value and address, but no count. Only one. */
197 /* Speced a value, but no address, so search same range. */
203 /* Speced nothing, search again for val. */
213 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
215 while (search_for_type(val, &end, &count)) {
216 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
220 if (TypeOf(obj) == type_FunctionHeader)
221 print((long)addr | type_FunctionPointer);
222 else if (LowtagOf(obj) == type_OtherImmediate0 || LowtagOf(obj) == type_OtherImmediate1)
223 print((lispobj)addr | type_OtherPointer);
225 print((lispobj)addr);
231 static void call_cmd(char **ptr)
233 lispobj thing = parse_lispobj(ptr), function, result, cons, args[3];
236 if (LowtagOf(thing) == type_OtherPointer) {
237 switch (TypeOf(*(lispobj *)(thing-type_OtherPointer))) {
238 case type_SymbolHeader:
239 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
241 cons = CONS(cons)->cdr) {
242 if (FDEFN(CONS(cons)->car)->name == thing) {
243 thing = CONS(cons)->car;
247 printf("symbol 0x%08lx is undefined.\n", thing);
252 function = FDEFN(thing)->function;
253 if (function == NIL) {
254 printf("fdefn 0x%08lx is undefined.\n", thing);
260 "0x%08lx is not a function pointer, symbol, or fdefn object.\n",
265 else if (LowtagOf(thing) != type_FunctionPointer) {
266 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
274 while (more_p(ptr)) {
276 printf("Too many arguments. (3 at most)\n");
279 args[numargs++] = parse_lispobj(ptr);
284 result = funcall0(function);
287 result = funcall1(function, args[0]);
290 result = funcall2(function, args[0], args[1]);
293 result = funcall3(function, args[0], args[1], args[2]);
300 static void flush_cmd(char **ptr)
305 static void quit(char **ptr)
309 printf("Really quit? [y] ");
311 fgets(buf, sizeof(buf), stdin);
312 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
316 static void help(char **ptr)
320 for (cmd = Cmds; cmd->cmd != NULL; cmd++)
321 if (cmd->help != NULL)
322 printf("%s\t%s\n", cmd->cmd, cmd->help);
327 static void exit_cmd(char **ptr)
332 static void purify_cmd(char **ptr)
337 static void print_context(os_context_t *context)
341 for (i = 0; i < NREGS; i++) {
342 printf("%s:\t", lisp_register_names[i]);
344 brief_print((lispobj)(*os_context_register_addr(context,
347 brief_print((lispobj)(*os_context_register_addr(context,
351 printf("PC:\t\t 0x%08lx\n", *os_context_pc_addr(context));
354 static void print_context_cmd(char **ptr)
358 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
363 index = parse_number(ptr);
365 if ((index >= 0) && (index < free)) {
366 printf("There are %d interrupt contexts.\n", free);
367 printf("Printing context %d\n", index);
368 print_context(lisp_interrupt_contexts[index]);
370 printf("There aren't that many/few contexts.\n");
371 printf("There are %d interrupt contexts.\n", free);
375 printf("There are no interrupt contexts!\n");
377 printf("There are %d interrupt contexts.\n", free);
378 printf("Printing context %d\n", free - 1);
379 print_context(lisp_interrupt_contexts[free - 1]);
384 static void backtrace_cmd(char **ptr)
386 void backtrace(int frames);
390 n = parse_number(ptr);
394 printf("Backtrace:\n");
398 static void catchers_cmd(char **ptr)
400 struct catch_block *catch;
402 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
405 printf("There are no active catchers!\n");
407 while (catch != NULL) {
409 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
410 (unsigned long)catch, (unsigned long)(catch->current_uwp),
411 (unsigned long)(catch->current_cont),
415 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
416 (unsigned long)catch, (unsigned long)(catch->current_uwp),
417 (unsigned long)(catch->current_cont),
418 component_ptr_from_pc(catch->entry_pc) + type_OtherPointer,
421 brief_print((lispobj)catch->tag);
422 catch = catch->previous_catch;
427 static void grab_sigs_cmd(char **ptr)
429 extern void sigint_init(void);
431 printf("Grabbing signals.\n");
435 static void sub_monitor(void)
437 struct cmd *cmd, *found;
439 char *line, *ptr, *token;
445 line = fgets(buf, sizeof(buf), stdin);
452 fprintf(stderr, "\nEOF on something other than a tty.\n");
457 if ((token = parse_token(&ptr)) == NULL)
461 for (cmd = Cmds; cmd->cmd != NULL; cmd++) {
462 if (strcmp(token, cmd->cmd) == 0) {
467 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
475 printf("``%s'' is ambiguous.\n", token);
476 else if (found == NULL)
477 printf("unknown command: ``%s''\n", token);
489 bcopy(curbuf, oldbuf, sizeof(oldbuf));
491 printf("LDB monitor\n");
499 bcopy(oldbuf, curbuf, sizeof(curbuf));
502 void throw_to_monitor()