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;
49 void (*fn)(char **ptr);
51 {"help", "Display this help information.", help},
53 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
54 {"call", "Call FUNCTION with ARG1, ARG2, ...", call_cmd},
55 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
56 {"context", "Print interrupt context number I.", print_context_cmd},
57 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
58 {"d", "Alias for dump", dump_cmd},
59 {"exit", "Exit this instance of the monitor.", exit_cmd},
60 {"flush", "Flush all temp variables.", flush_cmd},
61 /* (Classic CMU CL had a "gc" command here, which seems like a
62 * reasonable idea, but the code was stale (incompatible with
63 * gencgc) so I just flushed it. -- WHN 20000814 */
64 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
65 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
67 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
68 {"print", "Print object at ADDRESS.", print_cmd},
69 {"p", "Alias for 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 = (u32 *)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 kill_cmd(char **ptr)
151 kill(getpid(), parse_number(ptr));
154 static void regs_cmd(char **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__)
159 printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
162 printf("BSP\t=\t0x%08lx\n",
163 (unsigned long)SymbolValue(BINDING_STACK_POINTER));
166 printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)DYNAMIC_SPACE_START);
167 #if defined(ibmrt) || defined(__i386__)
168 printf("ALLOC\t=\t0x%08lx\n",
169 (unsigned long)SymbolValue(ALLOCATION_POINTER));
170 printf("TRIGGER\t=\t0x%08lx\n",
171 (unsigned long)SymbolValue(INTERNAL_GC_TRIGGER));
173 printf("ALLOC\t=\t0x%08X\n",
174 (unsigned long)dynamic_space_free_pointer);
175 printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
177 printf("STATIC\t=\t0x%08lx\n",
178 (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER));
179 printf("RDONLY\t=\t0x%08lx\n",
180 (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
183 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
187 static void search_cmd(char **ptr)
189 static int lastval = 0, lastcount = 0;
190 static lispobj *start = 0, *end = 0;
195 val = parse_number(ptr);
196 if (val < 0 || val > 0xff) {
197 printf("Can only search for single bytes.\n");
201 addr = (lispobj *)PTR((long)parse_addr(ptr));
203 count = parse_number(ptr);
206 /* Speced value and address, but no count. Only one. */
211 /* Speced a value, but no address, so search same range. */
217 /* Speced nothing, search again for val. */
227 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
229 while (search_for_type(val, &end, &count)) {
230 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
234 if (TypeOf(obj) == type_FunctionHeader)
235 print((long)addr | type_FunctionPointer);
236 else if (LowtagOf(obj) == type_OtherImmediate0 || LowtagOf(obj) == type_OtherImmediate1)
237 print((lispobj)addr | type_OtherPointer);
239 print((lispobj)addr);
245 static void call_cmd(char **ptr)
247 lispobj thing = parse_lispobj(ptr), function, result = 0, cons, args[3];
250 if (LowtagOf(thing) == type_OtherPointer) {
251 switch (TypeOf(*(lispobj *)(thing-type_OtherPointer))) {
252 case type_SymbolHeader:
253 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
255 cons = CONS(cons)->cdr) {
256 if (FDEFN(CONS(cons)->car)->name == thing) {
257 thing = CONS(cons)->car;
261 printf("symbol 0x%08lx is undefined.\n", (long unsigned)thing);
266 function = FDEFN(thing)->function;
267 if (function == NIL) {
268 printf("fdefn 0x%08lx is undefined.\n", (long unsigned)thing);
273 printf("0x%08lx is not a function pointer, symbol, "
274 "or fdefn object.\n",
275 (long unsigned)thing);
279 else if (LowtagOf(thing) != type_FunctionPointer) {
280 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
281 (long unsigned)thing);
288 while (more_p(ptr)) {
290 printf("too many arguments (no more than 3 allowed)\n");
293 args[numargs++] = parse_lispobj(ptr);
298 result = funcall0(function);
301 result = funcall1(function, args[0]);
304 result = funcall2(function, args[0], args[1]);
307 result = funcall3(function, args[0], args[1], args[2]);
310 lose("unsupported argument count");
316 static void flush_cmd(char **ptr)
321 static void quit(char **ptr)
325 printf("Really quit? [y] ");
327 fgets(buf, sizeof(buf), stdin);
328 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
332 static void help(char **ptr)
336 for (cmd = Cmds; cmd->cmd != NULL; cmd++)
337 if (cmd->help != NULL)
338 printf("%s\t%s\n", cmd->cmd, cmd->help);
343 static void exit_cmd(char **ptr)
348 static void purify_cmd(char **ptr)
353 static void print_context(os_context_t *context)
357 for (i = 0; i < NREGS; i++) {
358 printf("%s:\t", lisp_register_names[i]);
360 brief_print((lispobj)(*os_context_register_addr(context,
363 brief_print((lispobj)(*os_context_register_addr(context,i)));
366 printf("PC:\t\t 0x%08lx\n",
367 (unsigned long)(*os_context_pc_addr(context)));
370 static void print_context_cmd(char **ptr)
374 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
379 index = parse_number(ptr);
381 if ((index >= 0) && (index < free)) {
382 printf("There are %d interrupt contexts.\n", free);
383 printf("Printing context %d\n", index);
384 print_context(lisp_interrupt_contexts[index]);
386 printf("There aren't that many/few contexts.\n");
387 printf("There are %d interrupt contexts.\n", free);
391 printf("There are no interrupt contexts!\n");
393 printf("There are %d interrupt contexts.\n", free);
394 printf("Printing context %d\n", free - 1);
395 print_context(lisp_interrupt_contexts[free - 1]);
400 static void backtrace_cmd(char **ptr)
402 void backtrace(int frames);
406 n = parse_number(ptr);
410 printf("Backtrace:\n");
414 static void catchers_cmd(char **ptr)
416 struct catch_block *catch;
418 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
421 printf("There are no active catchers!\n");
423 while (catch != NULL) {
425 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
426 (unsigned long)catch, (unsigned long)(catch->current_uwp),
427 (unsigned long)(catch->current_cont),
431 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
432 (unsigned long)catch, (unsigned long)(catch->current_uwp),
433 (unsigned long)(catch->current_cont),
434 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
436 (unsigned long)catch->entry_pc);
438 brief_print((lispobj)catch->tag);
439 catch = catch->previous_catch;
444 static void grab_sigs_cmd(char **ptr)
446 extern void sigint_init(void);
448 printf("Grabbing signals.\n");
453 static int devttyfd=-1;
455 static void sub_monitor(void)
457 struct cmd *cmd, *found;
459 char *line, *ptr, *token;
463 devtty=fopen("/dev/tty","r+");
464 devttyfd=fileno(devtty);
469 line = fgets(buf, sizeof(buf), devtty);
471 if (isatty(devttyfd)) {
476 fprintf(stderr, "\nEOF on something other than a tty.\n");
481 if ((token = parse_token(&ptr)) == NULL)
485 for (cmd = Cmds; cmd->cmd != NULL; cmd++) {
486 if (strcmp(token, cmd->cmd) == 0) {
491 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
499 printf("``%s'' is ambiguous.\n", token);
500 else if (found == NULL)
501 printf("unknown command: ``%s''\n", token);
513 bcopy(curbuf, oldbuf, sizeof(oldbuf));
515 printf("LDB monitor\n");
523 bcopy(oldbuf, curbuf, sizeof(curbuf));
526 void throw_to_monitor()