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"
36 extern boolean isatty(int fd);
38 typedef void cmd(char **ptr);
40 static cmd call_cmd, dump_cmd, print_cmd, quit, help;
41 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
42 static cmd print_context_cmd;
43 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
44 static cmd grab_sigs_cmd;
48 void (*fn)(char **ptr);
50 {"help", "Display this help information.", 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", "dump", dump_cmd},
58 {"exit", "Exit this instance of the monitor.", exit_cmd},
59 {"flush", "Flush all temp variables.", flush_cmd},
60 /* (Classic CMU CL had a "gc" command here, which seems like a
61 * reasonable idea, but the code was stale (incompatible with
62 * gencgc) so I just flushed it. -- WHN 20000814 */
63 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
64 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
65 {"print", "Print object at ADDRESS.", print_cmd},
66 {"p", "print", print_cmd},
67 {"quit", "Quit.", quit},
68 {"regs", "Display current Lisp regs.", regs_cmd},
69 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
70 {"s", "search", search_cmd},
74 static jmp_buf curbuf;
76 static int visible(unsigned char c)
78 if (c < ' ' || c > '~')
84 static void dump_cmd(char **ptr)
86 static char *lastaddr = 0;
87 static int lastcount = 20;
89 char *addr = lastaddr;
90 int count = lastcount, displacement;
93 addr = parse_addr(ptr);
96 count = parse_number(ptr);
100 printf("COUNT must be non-zero.\n");
113 while (count-- > 0) {
115 printf("0x%08lX: ", (unsigned long) addr);
117 printf("0x%08X: ", (u32) addr);
119 if (is_valid_lisp_addr((os_vm_address_t)addr)) {
121 unsigned long *lptr = (unsigned long *)addr;
123 u32 *lptr = (unsigned long *)addr;
125 unsigned short *sptr = (unsigned short *)addr;
126 unsigned char *cptr = (unsigned char *)addr;
128 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]));
131 printf("invalid Lisp-level address\n");
133 addr += displacement;
139 static void print_cmd(char **ptr)
141 lispobj obj = parse_lispobj(ptr);
146 static void regs_cmd(char **ptr)
148 printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer);
149 printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer);
150 #if !defined(ibmrt) && !defined(__i386__)
151 printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
154 printf("BSP\t=\t0x%08lx\n",
155 (unsigned long)SymbolValue(BINDING_STACK_POINTER));
158 printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)DYNAMIC_SPACE_START);
159 #if defined(ibmrt) || defined(__i386__)
160 printf("ALLOC\t=\t0x%08lx\n",
161 (unsigned long)SymbolValue(ALLOCATION_POINTER));
162 printf("TRIGGER\t=\t0x%08lx\n",
163 (unsigned long)SymbolValue(INTERNAL_GC_TRIGGER));
165 printf("ALLOC\t=\t0x%08X\n",
166 (unsigned long)dynamic_space_free_pointer);
167 printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
169 printf("STATIC\t=\t0x%08lx\n",
170 (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER));
171 printf("RDONLY\t=\t0x%08lx\n",
172 (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
175 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
179 static void search_cmd(char **ptr)
181 static int lastval = 0, lastcount = 0;
182 static lispobj *start = 0, *end = 0;
187 val = parse_number(ptr);
188 if (val < 0 || val > 0xff) {
189 printf("Can only search for single bytes.\n");
193 addr = (lispobj *)PTR((long)parse_addr(ptr));
195 count = parse_number(ptr);
198 /* Speced value and address, but no count. Only one. */
203 /* Speced a value, but no address, so search same range. */
209 /* Speced nothing, search again for val. */
219 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
221 while (search_for_type(val, &end, &count)) {
222 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
226 if (TypeOf(obj) == type_FunctionHeader)
227 print((long)addr | type_FunctionPointer);
228 else if (LowtagOf(obj) == type_OtherImmediate0 || LowtagOf(obj) == type_OtherImmediate1)
229 print((lispobj)addr | type_OtherPointer);
231 print((lispobj)addr);
237 static void call_cmd(char **ptr)
239 lispobj thing = parse_lispobj(ptr), function, result, cons, args[3];
242 if (LowtagOf(thing) == type_OtherPointer) {
243 switch (TypeOf(*(lispobj *)(thing-type_OtherPointer))) {
244 case type_SymbolHeader:
245 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
247 cons = CONS(cons)->cdr) {
248 if (FDEFN(CONS(cons)->car)->name == thing) {
249 thing = CONS(cons)->car;
253 printf("symbol 0x%08lx is undefined.\n", (long unsigned)thing);
258 function = FDEFN(thing)->function;
259 if (function == NIL) {
260 printf("fdefn 0x%08lx is undefined.\n", (long unsigned)thing);
265 printf("0x%08lx is not a function pointer, symbol, "
266 "or fdefn object.\n",
267 (long unsigned)thing);
271 else if (LowtagOf(thing) != type_FunctionPointer) {
272 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
273 (long unsigned)thing);
280 while (more_p(ptr)) {
282 printf("too many arguments (no more than 3 allowed)\n");
285 args[numargs++] = parse_lispobj(ptr);
290 result = funcall0(function);
293 result = funcall1(function, args[0]);
296 result = funcall2(function, args[0], args[1]);
299 result = funcall3(function, args[0], args[1], args[2]);
302 lose("unsupported argument count");
308 static void flush_cmd(char **ptr)
313 static void quit(char **ptr)
317 printf("Really quit? [y] ");
319 fgets(buf, sizeof(buf), stdin);
320 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
324 static void help(char **ptr)
328 for (cmd = Cmds; cmd->cmd != NULL; cmd++)
329 if (cmd->help != NULL)
330 printf("%s\t%s\n", cmd->cmd, cmd->help);
335 static void exit_cmd(char **ptr)
340 static void purify_cmd(char **ptr)
345 static void print_context(os_context_t *context)
349 for (i = 0; i < NREGS; i++) {
350 printf("%s:\t", lisp_register_names[i]);
352 brief_print((lispobj)(*os_context_register_addr(context,
355 brief_print((lispobj)(*os_context_register_addr(context,
359 printf("PC:\t\t 0x%08lx\n",
360 (unsigned long)(*os_context_pc_addr(context)));
363 static void print_context_cmd(char **ptr)
367 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
372 index = parse_number(ptr);
374 if ((index >= 0) && (index < free)) {
375 printf("There are %d interrupt contexts.\n", free);
376 printf("Printing context %d\n", index);
377 print_context(lisp_interrupt_contexts[index]);
379 printf("There aren't that many/few contexts.\n");
380 printf("There are %d interrupt contexts.\n", free);
384 printf("There are no interrupt contexts!\n");
386 printf("There are %d interrupt contexts.\n", free);
387 printf("Printing context %d\n", free - 1);
388 print_context(lisp_interrupt_contexts[free - 1]);
393 static void backtrace_cmd(char **ptr)
395 void backtrace(int frames);
399 n = parse_number(ptr);
403 printf("Backtrace:\n");
407 static void catchers_cmd(char **ptr)
409 struct catch_block *catch;
411 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
414 printf("There are no active catchers!\n");
416 while (catch != NULL) {
418 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
419 (unsigned long)catch, (unsigned long)(catch->current_uwp),
420 (unsigned long)(catch->current_cont),
424 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
425 (unsigned long)catch, (unsigned long)(catch->current_uwp),
426 (unsigned long)(catch->current_cont),
427 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
429 (unsigned long)catch->entry_pc);
431 brief_print((lispobj)catch->tag);
432 catch = catch->previous_catch;
437 static void grab_sigs_cmd(char **ptr)
439 extern void sigint_init(void);
441 printf("Grabbing signals.\n");
445 static void sub_monitor(void)
447 struct cmd *cmd, *found;
449 char *line, *ptr, *token;
455 line = fgets(buf, sizeof(buf), stdin);
462 fprintf(stderr, "\nEOF on something other than a tty.\n");
467 if ((token = parse_token(&ptr)) == NULL)
471 for (cmd = Cmds; cmd->cmd != NULL; cmd++) {
472 if (strcmp(token, cmd->cmd) == 0) {
477 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
485 printf("``%s'' is ambiguous.\n", token);
486 else if (found == NULL)
487 printf("unknown command: ``%s''\n", token);
499 bcopy(curbuf, oldbuf, sizeof(oldbuf));
501 printf("LDB monitor\n");
509 bcopy(oldbuf, curbuf, sizeof(curbuf));
512 void throw_to_monitor()