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 /* Almost all of this file can be skipped if we're not supporting LDB. */
27 #if defined(LISP_FEATURE_SB_LDB)
37 #include "interrupt.h"
39 #include "genesis/static-symbols.h"
40 #include "genesis/primitive-objects.h"
44 /* When we need to do command input, we use this stream, which is not
45 * in general stdin, so that things will "work" (as well as being
46 * thrown into ldb can be considered "working":-) even in a process
47 * where standard input has been redirected to a file or pipe.
49 * (We could set up output to go to a special ldb_out stream for the
50 * same reason, but there's been no pressure for that so far.)
52 * The enter-the-ldb-monitor function is responsible for setting up
54 static FILE *ldb_in = 0;
55 static int ldb_in_fd = -1;
57 typedef void cmd(char **ptr);
59 static cmd dump_cmd, print_cmd, quit_cmd, help_cmd;
60 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
61 static cmd print_context_cmd;
62 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
63 static cmd grab_sigs_cmd;
68 void (*fn)(char **ptr);
69 } supported_cmds[] = {
70 {"help", "Display this help information.", help_cmd},
71 {"?", "(an alias for help)", help_cmd},
72 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
73 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
74 {"context", "Print interrupt context number I.", print_context_cmd},
75 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
76 {"d", "(an alias for dump)", dump_cmd},
77 {"exit", "Exit this instance of the monitor.", exit_cmd},
78 {"flush", "Flush all temp variables.", flush_cmd},
79 /* (Classic CMU CL had a "gc" command here, which seems like a
80 * reasonable idea, but the code was stale (incompatible with
81 * gencgc) so I just flushed it. -- WHN 20000814 */
82 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
83 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
85 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
86 {"print", "Print object at ADDRESS.", print_cmd},
87 {"p", "(an alias for print)", print_cmd},
88 {"quit", "Quit.", quit_cmd},
89 {"regs", "Display current Lisp registers.", regs_cmd},
90 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
91 {"s", "(an alias for search)", search_cmd},
95 static jmp_buf curbuf;
98 visible(unsigned char c)
100 if (c < ' ' || c > '~')
109 static char *lastaddr = 0;
110 static int lastcount = 20;
112 char *addr = lastaddr;
113 int count = lastcount, displacement;
116 addr = parse_addr(ptr);
119 count = parse_number(ptr);
123 printf("COUNT must be non-zero.\n");
136 while (count-- > 0) {
137 #ifndef LISP_FEATURE_ALPHA
138 printf("0x%08lX: ", (unsigned long) addr);
140 printf("0x%08X: ", (u32) addr);
142 if (is_valid_lisp_addr((os_vm_address_t)addr)) {
143 #ifndef LISP_FEATURE_ALPHA
144 unsigned long *lptr = (unsigned long *)addr;
146 u32 *lptr = (u32 *)addr;
148 unsigned short *sptr = (unsigned short *)addr;
149 unsigned char *cptr = (unsigned char *)addr;
151 printf("0x%08lx 0x%04x 0x%04x "
152 "0x%02x 0x%02x 0x%02x 0x%02x "
155 lptr[0], sptr[0], sptr[1],
156 cptr[0], cptr[1], cptr[2], cptr[3],
157 visible(cptr[0]), visible(cptr[1]),
158 visible(cptr[2]), visible(cptr[3]));
161 printf("invalid Lisp-level address\n");
163 addr += displacement;
170 print_cmd(char **ptr)
172 lispobj obj = parse_lispobj(ptr);
180 kill(getpid(), parse_number(ptr));
186 printf("CSP\t=\t0x%08lx\n", (unsigned long)current_control_stack_pointer);
187 printf("CFP\t=\t0x%08lx\n", (unsigned long)current_control_frame_pointer);
190 printf("BSP\t=\t0x%08lx\n", (unsigned long)current_binding_stack_pointer);
192 /* printf("BSP\t=\t0x%08lx\n",
193 (unsigned long)SymbolValue(BINDING_STACK_POINTER)); */
196 #ifdef LISP_FEATURE_GENCGC
197 /* printf("DYNAMIC\t=\t0x%08lx\n", DYNAMIC_SPACE_START); */
199 printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)current_dynamic_space);
203 printf("ALLOC\t=\t0x%08lx\n", (unsigned long)dynamic_space_free_pointer);
205 printf("ALLOC\t=\t0x%08lx\n",
206 (unsigned long)SymbolValue(ALLOCATION_POINTER,
207 arch_os_get_current_thread()));
210 #ifndef LISP_FEATURE_GENCGC
211 printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
215 printf("STATIC\t=\t0x%08lx\n",
216 (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER));
217 printf("RDONLY\t=\t0x%08lx\n",
218 (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
223 search_cmd(char **ptr)
225 static int lastval = 0, lastcount = 0;
226 static lispobj *start = 0, *end = 0;
231 val = parse_number(ptr);
232 if (val < 0 || val > 0xff) {
233 printf("can only search for single bytes\n");
237 addr = (lispobj *)native_pointer((long)parse_addr(ptr));
239 count = parse_number(ptr);
242 /* Specified value and address, but no count. Only one. */
247 /* Specified a value, but no address, so search same range. */
253 /* Specified nothing, search again for val. */
263 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
265 while (search_for_type(val, &end, &count)) {
266 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
270 if (widetag_of(obj) == SIMPLE_FUN_HEADER_WIDETAG) {
271 print((long)addr | FUN_POINTER_LOWTAG);
272 } else if (lowtag_of(obj) == OTHER_IMMEDIATE_0_LOWTAG ||
273 lowtag_of(obj) == OTHER_IMMEDIATE_1_LOWTAG) {
274 print((lispobj)addr | OTHER_POINTER_LOWTAG);
276 print((lispobj)addr);
283 /* (There used to be call_cmd() here, to call known-at-cold-init-time
284 * Lisp functions from ldb, but it bitrotted and was deleted in
285 * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
289 flush_cmd(char **ptr)
299 printf("Really quit? [y] ");
301 fgets(buf, sizeof(buf), ldb_in);
302 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
311 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
312 if (cmd->help != NULL)
313 printf("%s\t%s\n", cmd->cmd, cmd->help);
325 purify_cmd(char **ptr)
331 print_context(os_context_t *context)
335 for (i = 0; i < NREGS; i++) {
336 printf("%s:\t", lisp_register_names[i]);
337 #ifdef LISP_FEATURE_X86
338 brief_print((lispobj)(*os_context_register_addr(context,
341 brief_print((lispobj)(*os_context_register_addr(context,i)));
344 #ifdef LISP_FEATURE_DARWIN
345 printf("DAR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context, 41)));
346 printf("DSISR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context, 42)));
348 printf("PC:\t\t 0x%08lx\n",
349 (unsigned long)(*os_context_pc_addr(context)));
353 print_context_cmd(char **ptr)
356 struct thread *thread=arch_os_get_current_thread();
358 free_ici = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
363 index = parse_number(ptr);
365 if ((index >= 0) && (index < free_ici)) {
366 printf("There are %d interrupt contexts.\n", free_ici);
367 printf("printing context %d\n", index);
368 print_context(thread->interrupt_contexts[index]);
370 printf("There aren't that many/few contexts.\n");
371 printf("There are %d interrupt contexts.\n", free_ici);
375 printf("There are no interrupt contexts!\n");
377 printf("There are %d interrupt contexts.\n", free_ici);
378 printf("printing context %d\n", free_ici - 1);
379 print_context(thread->interrupt_contexts[free_ici - 1]);
385 backtrace_cmd(char **ptr)
387 void backtrace(int frames);
391 n = parse_number(ptr);
395 printf("Backtrace:\n");
400 catchers_cmd(char **ptr)
402 struct catch_block *catch;
403 struct thread *thread=arch_os_get_current_thread();
405 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK,thread);
408 printf("There are no active catchers!\n");
410 while (catch != NULL) {
411 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\t"
412 "code: 0x%08lX\n\tentry: 0x%08lX\n\ttag: ",
413 (unsigned long)catch,
414 (unsigned long)(catch->current_uwp),
415 (unsigned long)(catch->current_cont),
416 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
417 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc)
418 + OTHER_POINTER_LOWTAG,
420 (unsigned long)(catch->current_code),
422 (unsigned long)(catch->entry_pc));
423 brief_print((lispobj)catch->tag);
424 catch = catch->previous_catch;
430 grab_sigs_cmd(char **ptr)
432 extern void sigint_init(void);
434 printf("Grabbing signals.\n");
441 struct cmd *cmd, *found;
443 char *line, *ptr, *token;
447 ldb_in = fopen("/dev/tty","r+");
448 ldb_in_fd = fileno(ldb_in);
454 line = fgets(buf, sizeof(buf), ldb_in);
456 if (isatty(ldb_in_fd)) {
461 fprintf(stderr, "\nEOF on something other than a tty.\n");
466 if ((token = parse_token(&ptr)) == NULL)
470 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
471 if (strcmp(token, cmd->cmd) == 0) {
476 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
484 printf("``%s'' is ambiguous.\n", token);
485 else if (found == NULL)
486 printf("unknown command: ``%s''\n", token);
499 bcopy(curbuf, oldbuf, sizeof(oldbuf));
501 printf("LDB monitor\n");
509 bcopy(oldbuf, curbuf, sizeof(curbuf));
518 #endif /* defined(LISP_FEATURE_SB_LDB) */
520 /* what we do when things go badly wrong at a low level */
522 monitor_or_something()
524 #if defined(LISP_FEATURE_SB_LDB)
528 "The system is too badly corrupted or confused to continue at the Lisp\n\
529 level. If the system had been compiled with the SB-LDB feature, we'd drop\n\
530 into the LDB low-level debugger now. But there's no LDB in this build, so\n\
531 we can't really do anything but just exit, sorry.\n");