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>
24 /* Almost all of this file can be skipped if we're not supporting LDB. */
25 #if defined(LISP_FEATURE_SB_LDB)
34 /* When we need to do command input, we use this stream, which is not
35 * in general stdin, so that things will "work" (as well as being
36 * thrown into ldb can be considered "working":-) even in a process
37 * where standard input has been redirected to a file or pipe.
39 * (We could set up output to go to a special ldb_out stream for the
40 * same reason, but there's been no pressure for that so far.)
42 * The enter-the-ldb-monitor function is responsible for setting up
44 static FILE *ldb_in = 0;
45 static int ldb_in_fd = -1;
47 typedef void cmd(char **ptr);
49 static cmd call_cmd, dump_cmd, print_cmd, quit_cmd, help_cmd;
50 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
51 static cmd print_context_cmd;
52 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
53 static cmd grab_sigs_cmd;
58 void (*fn)(char **ptr);
59 } supported_cmds[] = {
60 {"help", "Display this help information.", help_cmd},
61 {"?", "(an alias for help)", help_cmd},
62 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
63 {"call", "Call FUNCTION with ARG1, ARG2, ...", call_cmd},
64 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
65 {"context", "Print interrupt context number I.", print_context_cmd},
66 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
67 {"d", "(an alias for dump)", dump_cmd},
68 {"exit", "Exit this instance of the monitor.", exit_cmd},
69 {"flush", "Flush all temp variables.", flush_cmd},
70 /* (Classic CMU CL had a "gc" command here, which seems like a
71 * reasonable idea, but the code was stale (incompatible with
72 * gencgc) so I just flushed it. -- WHN 20000814 */
73 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
74 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
76 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
77 {"print", "Print object at ADDRESS.", print_cmd},
78 {"p", "(an alias for print)", print_cmd},
79 {"quit", "Quit.", quit_cmd},
80 {"regs", "Display current Lisp registers.", regs_cmd},
81 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
82 {"s", "(an alias for search)", search_cmd},
86 static jmp_buf curbuf;
89 visible(unsigned char c)
91 if (c < ' ' || c > '~')
100 static char *lastaddr = 0;
101 static int lastcount = 20;
103 char *addr = lastaddr;
104 int count = lastcount, displacement;
107 addr = parse_addr(ptr);
110 count = parse_number(ptr);
114 printf("COUNT must be non-zero.\n");
127 while (count-- > 0) {
129 printf("0x%08lX: ", (unsigned long) addr);
131 printf("0x%08X: ", (u32) addr);
133 if (is_valid_lisp_addr((os_vm_address_t)addr)) {
135 unsigned long *lptr = (unsigned long *)addr;
137 u32 *lptr = (u32 *)addr;
139 unsigned short *sptr = (unsigned short *)addr;
140 unsigned char *cptr = (unsigned char *)addr;
142 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]));
145 printf("invalid Lisp-level address\n");
147 addr += displacement;
154 print_cmd(char **ptr)
156 lispobj obj = parse_lispobj(ptr);
164 kill(getpid(), parse_number(ptr));
170 printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer);
171 printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer);
172 #if !defined(__i386__)
173 printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
176 printf("BSP\t=\t0x%08lx\n",
177 (unsigned long)SymbolValue(BINDING_STACK_POINTER));
180 printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)DYNAMIC_SPACE_START);
181 #if defined(__i386__)
182 printf("ALLOC\t=\t0x%08lx\n",
183 (unsigned long)SymbolValue(ALLOCATION_POINTER));
184 printf("TRIGGER\t=\t0x%08lx\n",
185 (unsigned long)SymbolValue(INTERNAL_GC_TRIGGER));
187 printf("ALLOC\t=\t0x%08X\n",
188 (unsigned long)dynamic_space_free_pointer);
189 printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
191 printf("STATIC\t=\t0x%08lx\n",
192 (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER));
193 printf("RDONLY\t=\t0x%08lx\n",
194 (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
197 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
202 search_cmd(char **ptr)
204 static int lastval = 0, lastcount = 0;
205 static lispobj *start = 0, *end = 0;
210 val = parse_number(ptr);
211 if (val < 0 || val > 0xff) {
212 printf("can only search for single bytes\n");
216 addr = (lispobj *)native_pointer((long)parse_addr(ptr));
218 count = parse_number(ptr);
221 /* Specified value and address, but no count. Only one. */
226 /* Specified a value, but no address, so search same range. */
232 /* Specified nothing, search again for val. */
242 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
244 while (search_for_type(val, &end, &count)) {
245 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
249 if (TypeOf(obj) == type_FunctionHeader)
250 print((long)addr | type_FunctionPointer);
251 else if (LowtagOf(obj) == type_OtherImmediate0 || LowtagOf(obj) == type_OtherImmediate1)
252 print((lispobj)addr | type_OtherPointer);
254 print((lispobj)addr);
263 lispobj thing = parse_lispobj(ptr), function, result = 0, cons, args[3];
266 if (LowtagOf(thing) == type_OtherPointer) {
267 switch (TypeOf(*(lispobj *)(thing-type_OtherPointer))) {
268 case type_SymbolHeader:
269 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
271 cons = CONS(cons)->cdr) {
272 if (FDEFN(CONS(cons)->car)->name == thing) {
273 thing = CONS(cons)->car;
277 printf("Symbol 0x%08lx is undefined.\n", (long unsigned)thing);
282 function = FDEFN(thing)->function;
283 if (function == NIL) {
284 printf("Fdefn 0x%08lx is undefined.\n", (long unsigned)thing);
289 printf("0x%08lx is not a function pointer, symbol, "
290 "or fdefn object.\n",
291 (long unsigned)thing);
295 else if (LowtagOf(thing) != type_FunctionPointer) {
296 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
297 (long unsigned)thing);
304 while (more_p(ptr)) {
306 printf("too many arguments (no more than 3 supported)\n");
309 args[numargs++] = parse_lispobj(ptr);
314 result = funcall0(function);
317 result = funcall1(function, args[0]);
320 result = funcall2(function, args[0], args[1]);
323 result = funcall3(function, args[0], args[1], args[2]);
326 lose("unsupported arg count made it past validity check?!");
333 flush_cmd(char **ptr)
343 printf("Really quit? [y] ");
345 fgets(buf, sizeof(buf), ldb_in);
346 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
355 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
356 if (cmd->help != NULL)
357 printf("%s\t%s\n", cmd->cmd, cmd->help);
369 purify_cmd(char **ptr)
375 print_context(os_context_t *context)
379 for (i = 0; i < NREGS; i++) {
380 printf("%s:\t", lisp_register_names[i]);
382 brief_print((lispobj)(*os_context_register_addr(context,
385 brief_print((lispobj)(*os_context_register_addr(context,i)));
388 printf("PC:\t\t 0x%08lx\n",
389 (unsigned long)(*os_context_pc_addr(context)));
393 print_context_cmd(char **ptr)
397 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
402 index = parse_number(ptr);
404 if ((index >= 0) && (index < free)) {
405 printf("There are %d interrupt contexts.\n", free);
406 printf("printing context %d\n", index);
407 print_context(lisp_interrupt_contexts[index]);
409 printf("There aren't that many/few contexts.\n");
410 printf("There are %d interrupt contexts.\n", free);
414 printf("There are no interrupt contexts!\n");
416 printf("There are %d interrupt contexts.\n", free);
417 printf("printing context %d\n", free - 1);
418 print_context(lisp_interrupt_contexts[free - 1]);
424 backtrace_cmd(char **ptr)
426 void backtrace(int frames);
430 n = parse_number(ptr);
434 printf("Backtrace:\n");
439 catchers_cmd(char **ptr)
441 struct catch_block *catch;
443 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
446 printf("There are no active catchers!\n");
448 while (catch != NULL) {
450 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
451 (unsigned long)catch, (unsigned long)(catch->current_uwp),
452 (unsigned long)(catch->current_cont),
456 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
457 (unsigned long)catch, (unsigned long)(catch->current_uwp),
458 (unsigned long)(catch->current_cont),
459 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
461 (unsigned long)catch->entry_pc);
463 brief_print((lispobj)catch->tag);
464 catch = catch->previous_catch;
470 grab_sigs_cmd(char **ptr)
472 extern void sigint_init(void);
474 printf("Grabbing signals.\n");
481 struct cmd *cmd, *found;
483 char *line, *ptr, *token;
487 ldb_in = fopen("/dev/tty","r+");
488 ldb_in_fd = fileno(ldb_in);
494 line = fgets(buf, sizeof(buf), ldb_in);
496 if (isatty(ldb_in_fd)) {
501 fprintf(stderr, "\nEOF on something other than a tty.\n");
506 if ((token = parse_token(&ptr)) == NULL)
510 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
511 if (strcmp(token, cmd->cmd) == 0) {
516 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
524 printf("``%s'' is ambiguous.\n", token);
525 else if (found == NULL)
526 printf("unknown command: ``%s''\n", token);
539 bcopy(curbuf, oldbuf, sizeof(oldbuf));
541 printf("LDB monitor\n");
549 bcopy(oldbuf, curbuf, sizeof(curbuf));
558 #endif /* defined(LISP_FEATURE_SB_LDB) */
560 /* what we do when things go badly wrong at a low level */
562 monitor_or_something()
564 #if defined(LISP_FEATURE_SB_LDB)
567 fprintf(stderr, "There's no LDB in this build; exiting.\n");