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)
35 #include "interrupt.h"
37 /* When we need to do command input, we use this stream, which is not
38 * in general stdin, so that things will "work" (as well as being
39 * thrown into ldb can be considered "working":-) even in a process
40 * where standard input has been redirected to a file or pipe.
42 * (We could set up output to go to a special ldb_out stream for the
43 * same reason, but there's been no pressure for that so far.)
45 * The enter-the-ldb-monitor function is responsible for setting up
47 static FILE *ldb_in = 0;
48 static int ldb_in_fd = -1;
50 typedef void cmd(char **ptr);
52 static cmd call_cmd, dump_cmd, print_cmd, quit_cmd, help_cmd;
53 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
54 static cmd print_context_cmd;
55 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
56 static cmd grab_sigs_cmd;
61 void (*fn)(char **ptr);
62 } supported_cmds[] = {
63 {"help", "Display this help information.", help_cmd},
64 {"?", "(an alias for help)", help_cmd},
65 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
66 {"call", "Call FUNCTION with ARG1, ARG2, ...", call_cmd},
67 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
68 {"context", "Print interrupt context number I.", print_context_cmd},
69 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
70 {"d", "(an alias for dump)", dump_cmd},
71 {"exit", "Exit this instance of the monitor.", exit_cmd},
72 {"flush", "Flush all temp variables.", flush_cmd},
73 /* (Classic CMU CL had a "gc" command here, which seems like a
74 * reasonable idea, but the code was stale (incompatible with
75 * gencgc) so I just flushed it. -- WHN 20000814 */
76 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
77 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
79 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
80 {"print", "Print object at ADDRESS.", print_cmd},
81 {"p", "(an alias for print)", print_cmd},
82 {"quit", "Quit.", quit_cmd},
83 {"regs", "Display current Lisp registers.", regs_cmd},
84 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
85 {"s", "(an alias for search)", search_cmd},
89 static jmp_buf curbuf;
92 visible(unsigned char c)
94 if (c < ' ' || c > '~')
103 static char *lastaddr = 0;
104 static int lastcount = 20;
106 char *addr = lastaddr;
107 int count = lastcount, displacement;
110 addr = parse_addr(ptr);
113 count = parse_number(ptr);
117 printf("COUNT must be non-zero.\n");
130 while (count-- > 0) {
132 printf("0x%08lX: ", (unsigned long) addr);
134 printf("0x%08X: ", (u32) addr);
136 if (is_valid_lisp_addr((os_vm_address_t)addr)) {
138 unsigned long *lptr = (unsigned long *)addr;
140 u32 *lptr = (u32 *)addr;
142 unsigned short *sptr = (unsigned short *)addr;
143 unsigned char *cptr = (unsigned char *)addr;
145 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]));
148 printf("invalid Lisp-level address\n");
150 addr += displacement;
157 print_cmd(char **ptr)
159 lispobj obj = parse_lispobj(ptr);
167 kill(getpid(), parse_number(ptr));
173 printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer);
174 printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer);
175 #if !defined(__i386__)
176 printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
179 printf("BSP\t=\t0x%08lx\n",
180 (unsigned long)SymbolValue(BINDING_STACK_POINTER));
183 printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)DYNAMIC_SPACE_START);
184 #if defined(__i386__)
185 printf("ALLOC\t=\t0x%08lx\n",
186 (unsigned long)SymbolValue(ALLOCATION_POINTER));
187 printf("TRIGGER\t=\t0x%08lx\n",
188 (unsigned long)SymbolValue(INTERNAL_GC_TRIGGER));
190 printf("ALLOC\t=\t0x%08X\n",
191 (unsigned long)dynamic_space_free_pointer);
192 printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
194 printf("STATIC\t=\t0x%08lx\n",
195 (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER));
196 printf("RDONLY\t=\t0x%08lx\n",
197 (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
200 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
205 search_cmd(char **ptr)
207 static int lastval = 0, lastcount = 0;
208 static lispobj *start = 0, *end = 0;
213 val = parse_number(ptr);
214 if (val < 0 || val > 0xff) {
215 printf("can only search for single bytes\n");
219 addr = (lispobj *)native_pointer((long)parse_addr(ptr));
221 count = parse_number(ptr);
224 /* Specified value and address, but no count. Only one. */
229 /* Specified a value, but no address, so search same range. */
235 /* Specified nothing, search again for val. */
245 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
247 while (search_for_type(val, &end, &count)) {
248 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
252 if (widetag_of(obj) == SIMPLE_FUN_HEADER_WIDETAG) {
253 print((long)addr | FUN_POINTER_LOWTAG);
254 } else if (lowtag_of(obj) == OTHER_IMMEDIATE_0_LOWTAG ||
255 lowtag_of(obj) == OTHER_IMMEDIATE_1_LOWTAG) {
256 print((lispobj)addr | OTHER_POINTER_LOWTAG);
258 print((lispobj)addr);
268 lispobj thing = parse_lispobj(ptr), function, result = 0, cons, args[3];
271 if (lowtag_of(thing) == OTHER_POINTER_LOWTAG) {
272 switch (widetag_of(*(lispobj *)(thing-OTHER_POINTER_LOWTAG))) {
273 case SYMBOL_HEADER_WIDETAG:
274 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
276 cons = CONS(cons)->cdr) {
277 if (FDEFN(CONS(cons)->car)->name == thing) {
278 thing = CONS(cons)->car;
282 printf("Symbol 0x%08lx is undefined.\n", (long unsigned)thing);
287 function = FDEFN(thing)->fun;
288 if (function == NIL) {
289 printf("Fdefn 0x%08lx is undefined.\n", (long unsigned)thing);
294 printf("0x%08lx is not a function pointer, symbol, "
295 "or fdefn object.\n",
296 (long unsigned)thing);
300 else if (lowtag_of(thing) != FUN_POINTER_LOWTAG) {
301 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
302 (long unsigned)thing);
309 while (more_p(ptr)) {
311 printf("too many arguments (no more than 3 supported)\n");
314 args[numargs++] = parse_lispobj(ptr);
319 result = funcall0(function);
322 result = funcall1(function, args[0]);
325 result = funcall2(function, args[0], args[1]);
328 result = funcall3(function, args[0], args[1], args[2]);
331 lose("unsupported arg count made it past validity check?!");
338 flush_cmd(char **ptr)
348 printf("Really quit? [y] ");
350 fgets(buf, sizeof(buf), ldb_in);
351 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
360 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
361 if (cmd->help != NULL)
362 printf("%s\t%s\n", cmd->cmd, cmd->help);
374 purify_cmd(char **ptr)
380 print_context(os_context_t *context)
384 for (i = 0; i < NREGS; i++) {
385 printf("%s:\t", lisp_register_names[i]);
387 brief_print((lispobj)(*os_context_register_addr(context,
390 brief_print((lispobj)(*os_context_register_addr(context,i)));
393 printf("PC:\t\t 0x%08lx\n",
394 (unsigned long)(*os_context_pc_addr(context)));
398 print_context_cmd(char **ptr)
402 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
407 index = parse_number(ptr);
409 if ((index >= 0) && (index < free)) {
410 printf("There are %d interrupt contexts.\n", free);
411 printf("printing context %d\n", index);
412 print_context(lisp_interrupt_contexts[index]);
414 printf("There aren't that many/few contexts.\n");
415 printf("There are %d interrupt contexts.\n", free);
419 printf("There are no interrupt contexts!\n");
421 printf("There are %d interrupt contexts.\n", free);
422 printf("printing context %d\n", free - 1);
423 print_context(lisp_interrupt_contexts[free - 1]);
429 backtrace_cmd(char **ptr)
431 void backtrace(int frames);
435 n = parse_number(ptr);
439 printf("Backtrace:\n");
444 catchers_cmd(char **ptr)
446 struct catch_block *catch;
448 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
451 printf("There are no active catchers!\n");
453 while (catch != NULL) {
455 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
456 (unsigned long)catch, (unsigned long)(catch->current_uwp),
457 (unsigned long)(catch->current_cont),
461 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
462 (unsigned long)catch, (unsigned long)(catch->current_uwp),
463 (unsigned long)(catch->current_cont),
464 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
465 OTHER_POINTER_LOWTAG,
466 (unsigned long)catch->entry_pc);
468 brief_print((lispobj)catch->tag);
469 catch = catch->previous_catch;
475 grab_sigs_cmd(char **ptr)
477 extern void sigint_init(void);
479 printf("Grabbing signals.\n");
486 struct cmd *cmd, *found;
488 char *line, *ptr, *token;
492 ldb_in = fopen("/dev/tty","r+");
493 ldb_in_fd = fileno(ldb_in);
499 line = fgets(buf, sizeof(buf), ldb_in);
501 if (isatty(ldb_in_fd)) {
506 fprintf(stderr, "\nEOF on something other than a tty.\n");
511 if ((token = parse_token(&ptr)) == NULL)
515 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
516 if (strcmp(token, cmd->cmd) == 0) {
521 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
529 printf("``%s'' is ambiguous.\n", token);
530 else if (found == NULL)
531 printf("unknown command: ``%s''\n", token);
544 bcopy(curbuf, oldbuf, sizeof(oldbuf));
546 printf("LDB monitor\n");
554 bcopy(oldbuf, curbuf, sizeof(curbuf));
563 #endif /* defined(LISP_FEATURE_SB_LDB) */
565 /* what we do when things go badly wrong at a low level */
567 monitor_or_something()
569 #if defined(LISP_FEATURE_SB_LDB)
572 fprintf(stderr, "There's no LDB in this build; exiting.\n");