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>
27 #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 *)PTR((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 (TypeOf(obj) == type_FunctionHeader)
253 print((long)addr | type_FunctionPointer);
254 else if (LowtagOf(obj) == type_OtherImmediate0 || LowtagOf(obj) == type_OtherImmediate1)
255 print((lispobj)addr | type_OtherPointer);
257 print((lispobj)addr);
266 lispobj thing = parse_lispobj(ptr), function, result = 0, cons, args[3];
269 if (LowtagOf(thing) == type_OtherPointer) {
270 switch (TypeOf(*(lispobj *)(thing-type_OtherPointer))) {
271 case type_SymbolHeader:
272 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
274 cons = CONS(cons)->cdr) {
275 if (FDEFN(CONS(cons)->car)->name == thing) {
276 thing = CONS(cons)->car;
280 printf("Symbol 0x%08lx is undefined.\n", (long unsigned)thing);
285 function = FDEFN(thing)->function;
286 if (function == NIL) {
287 printf("Fdefn 0x%08lx is undefined.\n", (long unsigned)thing);
292 printf("0x%08lx is not a function pointer, symbol, "
293 "or fdefn object.\n",
294 (long unsigned)thing);
298 else if (LowtagOf(thing) != type_FunctionPointer) {
299 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
300 (long unsigned)thing);
307 while (more_p(ptr)) {
309 printf("too many arguments (no more than 3 supported)\n");
312 args[numargs++] = parse_lispobj(ptr);
317 result = funcall0(function);
320 result = funcall1(function, args[0]);
323 result = funcall2(function, args[0], args[1]);
326 result = funcall3(function, args[0], args[1], args[2]);
329 lose("unsupported arg count made it past validity check?!");
336 flush_cmd(char **ptr)
346 printf("Really quit? [y] ");
348 fgets(buf, sizeof(buf), ldb_in);
349 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
358 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
359 if (cmd->help != NULL)
360 printf("%s\t%s\n", cmd->cmd, cmd->help);
372 purify_cmd(char **ptr)
378 print_context(os_context_t *context)
382 for (i = 0; i < NREGS; i++) {
383 printf("%s:\t", lisp_register_names[i]);
385 brief_print((lispobj)(*os_context_register_addr(context,
388 brief_print((lispobj)(*os_context_register_addr(context,i)));
391 printf("PC:\t\t 0x%08lx\n",
392 (unsigned long)(*os_context_pc_addr(context)));
396 print_context_cmd(char **ptr)
400 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
405 index = parse_number(ptr);
407 if ((index >= 0) && (index < free)) {
408 printf("There are %d interrupt contexts.\n", free);
409 printf("printing context %d\n", index);
410 print_context(lisp_interrupt_contexts[index]);
412 printf("There aren't that many/few contexts.\n");
413 printf("There are %d interrupt contexts.\n", free);
417 printf("There are no interrupt contexts!\n");
419 printf("There are %d interrupt contexts.\n", free);
420 printf("printing context %d\n", free - 1);
421 print_context(lisp_interrupt_contexts[free - 1]);
427 backtrace_cmd(char **ptr)
429 void backtrace(int frames);
433 n = parse_number(ptr);
437 printf("Backtrace:\n");
442 catchers_cmd(char **ptr)
444 struct catch_block *catch;
446 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
449 printf("There are no active catchers!\n");
451 while (catch != NULL) {
453 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
454 (unsigned long)catch, (unsigned long)(catch->current_uwp),
455 (unsigned long)(catch->current_cont),
459 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
460 (unsigned long)catch, (unsigned long)(catch->current_uwp),
461 (unsigned long)(catch->current_cont),
462 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
464 (unsigned long)catch->entry_pc);
466 brief_print((lispobj)catch->tag);
467 catch = catch->previous_catch;
473 grab_sigs_cmd(char **ptr)
475 extern void sigint_init(void);
477 printf("Grabbing signals.\n");
484 struct cmd *cmd, *found;
486 char *line, *ptr, *token;
490 ldb_in = fopen("/dev/tty","r+");
491 ldb_in_fd = fileno(ldb_in);
497 line = fgets(buf, sizeof(buf), ldb_in);
499 if (isatty(ldb_in_fd)) {
504 fprintf(stderr, "\nEOF on something other than a tty.\n");
509 if ((token = parse_token(&ptr)) == NULL)
513 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
514 if (strcmp(token, cmd->cmd) == 0) {
519 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
527 printf("``%s'' is ambiguous.\n", token);
528 else if (found == NULL)
529 printf("unknown command: ``%s''\n", token);
542 bcopy(curbuf, oldbuf, sizeof(oldbuf));
544 printf("LDB monitor\n");
552 bcopy(oldbuf, curbuf, sizeof(curbuf));