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.
15 #include <sys/types.h>
19 #ifndef LISP_FEATURE_WIN32
20 #include <sys/resource.h>
29 /* Almost all of this file can be skipped if we're not supporting LDB. */
30 #if defined(LISP_FEATURE_SB_LDB)
40 #include "interrupt.h"
42 #include "genesis/static-symbols.h"
43 #include "genesis/primitive-objects.h"
47 /* When we need to do command input, we use this stream, which is not
48 * in general stdin, so that things will "work" (as well as being
49 * thrown into ldb can be considered "working":-) even in a process
50 * where standard input has been redirected to a file or pipe.
52 * (We could set up output to go to a special ldb_out stream for the
53 * same reason, but there's been no pressure for that so far.)
55 * The enter-the-ldb-monitor function is responsible for setting up
57 static FILE *ldb_in = 0;
58 static int ldb_in_fd = -1;
60 typedef void cmd(char **ptr);
62 static cmd dump_cmd, print_cmd, quit_cmd, help_cmd;
63 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
64 static cmd print_context_cmd;
65 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
66 static cmd grab_sigs_cmd;
71 void (*fn)(char **ptr);
72 } supported_cmds[] = {
73 {"help", "Display this help information.", help_cmd},
74 {"?", "(an alias for help)", help_cmd},
75 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
76 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
77 {"context", "Print interrupt context number I.", print_context_cmd},
78 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
79 {"d", "(an alias for dump)", dump_cmd},
80 {"exit", "Exit this instance of the monitor.", exit_cmd},
81 {"flush", "Flush all temp variables.", flush_cmd},
82 /* (Classic CMU CL had a "gc" command here, which seems like a
83 * reasonable idea, but the code was stale (incompatible with
84 * gencgc) so I just flushed it. -- WHN 20000814 */
85 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
86 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
88 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
89 {"print", "Print object at ADDRESS.", print_cmd},
90 {"p", "(an alias for print)", print_cmd},
91 {"quit", "Quit.", quit_cmd},
92 {"regs", "Display current Lisp registers.", regs_cmd},
93 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
94 {"s", "(an alias for search)", search_cmd},
98 static jmp_buf curbuf;
101 visible(unsigned char c)
103 if (c < ' ' || c > '~')
112 static char *lastaddr = 0;
113 static int lastcount = 20;
115 char *addr = lastaddr;
116 int count = lastcount, displacement;
119 addr = parse_addr(ptr);
122 count = parse_number(ptr);
126 printf("COUNT must be non-zero.\n");
139 while (count-- > 0) {
140 #ifndef LISP_FEATURE_ALPHA
141 printf("0x%08lX: ", (unsigned long) addr);
143 printf("0x%08X: ", (u32) addr);
145 if (is_valid_lisp_addr((os_vm_address_t)addr)) {
146 #ifndef LISP_FEATURE_ALPHA
147 unsigned long *lptr = (unsigned long *)addr;
149 u32 *lptr = (u32 *)addr;
151 unsigned short *sptr = (unsigned short *)addr;
152 unsigned char *cptr = (unsigned char *)addr;
154 printf("0x%08lx 0x%04x 0x%04x "
155 "0x%02x 0x%02x 0x%02x 0x%02x "
158 lptr[0], sptr[0], sptr[1],
159 cptr[0], cptr[1], cptr[2], cptr[3],
160 visible(cptr[0]), visible(cptr[1]),
161 visible(cptr[2]), visible(cptr[3]));
164 printf("invalid Lisp-level address\n");
166 addr += displacement;
173 print_cmd(char **ptr)
175 lispobj obj = parse_lispobj(ptr);
183 #ifndef LISP_FEATURE_WIN32
184 kill(getpid(), parse_number(ptr));
191 struct thread *thread=arch_os_get_current_thread();
193 printf("CSP\t=\t0x%08lx ", (unsigned long)current_control_stack_pointer);
194 printf("CFP\t=\t0x%08lx ", (unsigned long)current_control_frame_pointer);
197 printf("BSP\t=\t0x%08lx\n", (unsigned long)current_binding_stack_pointer);
199 /* printf("BSP\t=\t0x%08lx\n",
200 (unsigned long)SymbolValue(BINDING_STACK_POINTER)); */
204 #ifdef LISP_FEATURE_GENCGC
205 /* printf("DYNAMIC\t=\t0x%08lx\n", DYNAMIC_SPACE_START); */
207 printf("STATIC\t=\t0x%08lx ",
208 (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER, thread));
209 printf("RDONLY\t=\t0x%08lx ",
210 (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER, thread));
211 printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)current_dynamic_space);
215 printf("ALLOC\t=\t0x%08lx\n", (unsigned long)dynamic_space_free_pointer);
217 printf("ALLOC\t=\t0x%08lx\n",
218 (unsigned long)SymbolValue(ALLOCATION_POINTER, thread));
221 #ifndef LISP_FEATURE_GENCGC
222 printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
227 search_cmd(char **ptr)
229 static int lastval = 0, lastcount = 0;
230 static lispobj *start = 0, *end = 0;
235 val = parse_number(ptr);
236 if (val < 0 || val > 0xff) {
237 printf("can only search for single bytes\n");
241 addr = (lispobj *)native_pointer((long)parse_addr(ptr));
243 count = parse_number(ptr);
246 /* Specified value and address, but no count. Only one. */
251 /* Specified a value, but no address, so search same range. */
257 /* Specified nothing, search again for val. */
267 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
269 while (search_for_type(val, &end, &count)) {
270 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
274 if (widetag_of(obj) == SIMPLE_FUN_HEADER_WIDETAG) {
275 print((long)addr | FUN_POINTER_LOWTAG);
276 } else if (other_immediate_lowtag_p(obj)) {
277 print((lispobj)addr | OTHER_POINTER_LOWTAG);
279 print((lispobj)addr);
286 /* (There used to be call_cmd() here, to call known-at-cold-init-time
287 * Lisp functions from ldb, but it bitrotted and was deleted in
288 * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
292 flush_cmd(char **ptr)
302 printf("Really quit? [y] ");
304 if (fgets(buf, sizeof(buf), ldb_in)) {
305 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
308 printf("\nUnable to read response, assuming y.\n");
318 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
319 if (cmd->help != NULL)
320 printf("%s\t%s\n", cmd->cmd, cmd->help);
332 purify_cmd(char **ptr)
338 print_context(os_context_t *context)
342 for (i = 0; i < NREGS; i++) {
343 printf("%s:\t", lisp_register_names[i]);
344 #ifdef LISP_FEATURE_X86
345 brief_print((lispobj)(*os_context_register_addr(context,
348 brief_print((lispobj)(*os_context_register_addr(context,i)));
351 #ifdef LISP_FEATURE_DARWIN
352 printf("DAR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context, 41)));
353 printf("DSISR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context, 42)));
355 printf("PC:\t\t 0x%08lx\n",
356 (unsigned long)(*os_context_pc_addr(context)));
360 print_context_cmd(char **ptr)
363 struct thread *thread=arch_os_get_current_thread();
365 free_ici = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
370 index = parse_number(ptr);
372 if ((index >= 0) && (index < free_ici)) {
373 printf("There are %d interrupt contexts.\n", free_ici);
374 printf("printing context %d\n", index);
375 print_context(thread->interrupt_contexts[index]);
377 printf("There aren't that many/few contexts.\n");
378 printf("There are %d interrupt contexts.\n", free_ici);
382 printf("There are no interrupt contexts!\n");
384 printf("There are %d interrupt contexts.\n", free_ici);
385 printf("printing context %d\n", free_ici - 1);
386 print_context(thread->interrupt_contexts[free_ici - 1]);
392 backtrace_cmd(char **ptr)
394 void backtrace(int frames);
398 n = parse_number(ptr);
402 printf("Backtrace:\n");
407 catchers_cmd(char **ptr)
409 struct catch_block *catch;
410 struct thread *thread=arch_os_get_current_thread();
412 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK,thread);
415 printf("There are no active catchers!\n");
417 while (catch != NULL) {
418 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\t"
419 "code: 0x%08lX\n\tentry: 0x%08lX\n\ttag: ",
420 (unsigned long)catch,
421 (unsigned long)(catch->current_uwp),
422 (unsigned long)(catch->current_cont),
423 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
424 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc)
425 + OTHER_POINTER_LOWTAG,
427 (unsigned long)(catch->current_code),
429 (unsigned long)(catch->entry_pc));
430 brief_print((lispobj)catch->tag);
431 catch = catch->previous_catch;
437 grab_sigs_cmd(char **ptr)
439 extern void sigint_init(void);
441 printf("Grabbing signals.\n");
448 struct cmd *cmd, *found;
450 char *line, *ptr, *token;
454 #ifndef LISP_FEATURE_WIN32
455 ldb_in = fopen("/dev/tty","r+");
456 if (ldb_in == NULL) {
457 perror("Error opening /dev/tty");
463 ldb_in_fd = fileno(ldb_in);
469 line = fgets(buf, sizeof(buf), ldb_in);
474 if ((token = parse_token(&ptr)) == NULL)
478 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
479 if (strcmp(token, cmd->cmd) == 0) {
484 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
492 printf("``%s'' is ambiguous.\n", token);
493 else if (found == NULL)
494 printf("unknown command: ``%s''\n", token);
507 bcopy(curbuf, oldbuf, sizeof(oldbuf));
509 printf("Welcome to LDB, a low-level debugger for the Lisp runtime environment.\n");
517 bcopy(oldbuf, curbuf, sizeof(curbuf));
526 #endif /* defined(LISP_FEATURE_SB_LDB) */
528 /* what we do when things go badly wrong at a low level */
530 monitor_or_something()
532 #if defined(LISP_FEATURE_SB_LDB)
536 "The system is too badly corrupted or confused to continue at the Lisp\n\
537 level. If the system had been compiled with the SB-LDB feature, we'd drop\n\
538 into the LDB low-level debugger now. But there's no LDB in this build, so\n\
539 we can't really do anything but just exit, sorry.\n");