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
;
120 if (!strncmp(*ptr
, "-f ", 3)) {
124 addr
= parse_addr(ptr
, !force
);
127 count
= parse_number(ptr
);
131 printf("COUNT must be non-zero.\n");
138 displacement
= N_WORD_BYTES
;
140 displacement
= -N_WORD_BYTES
;
144 while (count
-- > 0) {
145 #ifndef LISP_FEATURE_ALPHA
146 printf("%p: ", (os_vm_address_t
) addr
);
148 printf("0x%08X: ", (u32
) addr
);
150 if (force
|| is_valid_lisp_addr((os_vm_address_t
)addr
)) {
151 #ifndef LISP_FEATURE_ALPHA
152 unsigned long *lptr
= (unsigned long *)addr
;
154 u32
*lptr
= (u32
*)addr
;
156 unsigned char *cptr
= (unsigned char *)addr
;
158 #if N_WORD_BYTES == 8
159 printf("0x%016lx | %c%c%c%c%c%c%c%c\n",
161 visible(cptr
[0]), visible(cptr
[1]),
162 visible(cptr
[2]), visible(cptr
[3]),
163 visible(cptr
[4]), visible(cptr
[5]),
164 visible(cptr
[6]), visible(cptr
[7]));
166 unsigned short *sptr
= (unsigned short *)addr
;
167 printf("0x%08lx 0x%04x 0x%04x "
168 "0x%02x 0x%02x 0x%02x 0x%02x "
171 lptr
[0], sptr
[0], sptr
[1],
172 cptr
[0], cptr
[1], cptr
[2], cptr
[3],
173 visible(cptr
[0]), visible(cptr
[1]),
174 visible(cptr
[2]), visible(cptr
[3]));
178 printf("invalid Lisp-level address\n");
180 addr
+= displacement
;
187 print_cmd(char **ptr
)
189 lispobj obj
= parse_lispobj(ptr
);
197 #ifndef LISP_FEATURE_WIN32
198 kill(getpid(), parse_number(ptr
));
205 struct thread
__attribute__((unused
)) *thread
=arch_os_get_current_thread();
207 printf("CSP\t=\t%p ", access_control_stack_pointer(thread
));
208 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
209 printf("CFP\t=\t%p ", access_control_frame_pointer(thread
));
213 printf("BSP\t=\t%p\n", get_binding_stack_pointer(thread
));
215 /* printf("BSP\t=\t%p\n", (void*)SymbolValue(BINDING_STACK_POINTER)); */
219 #ifdef LISP_FEATURE_GENCGC
220 /* printf("DYNAMIC\t=\t%p\n", (void*)DYNAMIC_SPACE_START); */
222 printf("STATIC\t=\t%p ",
223 (void*)SymbolValue(STATIC_SPACE_FREE_POINTER
, thread
));
224 printf("RDONLY\t=\t%p ",
225 (void*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
, thread
));
226 printf("DYNAMIC\t=\t%p\n", (void*)current_dynamic_space
);
230 printf("ALLOC\t=\t%p\n", (void*)dynamic_space_free_pointer
);
232 printf("ALLOC\t=\t%p\n", (void*)SymbolValue(ALLOCATION_POINTER
, thread
));
235 #ifndef LISP_FEATURE_GENCGC
236 printf("TRIGGER\t=\t%p\n", (void*)current_auto_gc_trigger
);
241 search_cmd(char **ptr
)
243 static int lastval
= 0, lastcount
= 0;
244 static lispobj
*start
= 0, *end
= 0;
249 val
= parse_number(ptr
);
250 if (val
< 0 || val
> 0xff) {
251 printf("can only search for single bytes\n");
255 addr
= native_pointer((uword_t
)parse_addr(ptr
, 1));
257 count
= parse_number(ptr
);
260 /* Specified value and address, but no count. Only one. */
265 /* Specified a value, but no address, so search same range. */
271 /* Specified nothing, search again for val. */
281 printf("searching for 0x%x at %p\n", val
, (void*)(uword_t
)end
);
283 while (search_for_type(val
, &end
, &count
)) {
284 printf("found 0x%x at %p:\n", val
, (void*)(uword_t
)end
);
288 if (widetag_of(obj
) == SIMPLE_FUN_HEADER_WIDETAG
) {
289 print((uword_t
)addr
| FUN_POINTER_LOWTAG
);
290 } else if (other_immediate_lowtag_p(obj
)) {
291 print((lispobj
)addr
| OTHER_POINTER_LOWTAG
);
293 print((lispobj
)addr
);
300 /* (There used to be call_cmd() here, to call known-at-cold-init-time
301 * Lisp functions from ldb, but it bitrotted and was deleted in
302 * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
306 flush_cmd(char **ptr
)
316 printf("Really quit? [y] ");
318 if (fgets(buf
, sizeof(buf
), ldb_in
)) {
319 if (buf
[0] == 'y' || buf
[0] == 'Y' || buf
[0] == '\n')
322 printf("\nUnable to read response, assuming y.\n");
332 for (cmd
= supported_cmds
; cmd
->cmd
!= NULL
; cmd
++)
333 if (cmd
->help
!= NULL
)
334 printf("%s\t%s\n", cmd
->cmd
, cmd
->help
);
346 purify_cmd(char **ptr
)
352 print_context(os_context_t
*context
)
356 for (i
= 0; i
< NREGS
; i
++) {
357 printf("%s:\t", lisp_register_names
[i
]);
358 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
359 brief_print((lispobj
)(*os_context_register_addr(context
, i
*2)));
361 brief_print((lispobj
)(*os_context_register_addr(context
,i
)));
364 #ifdef LISP_FEATURE_DARWIN
365 printf("DAR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context
, 41)));
366 printf("DSISR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context
, 42)));
368 printf("PC:\t\t 0x%08lx\n",
369 (unsigned long)(*os_context_pc_addr(context
)));
373 print_context_cmd(char **ptr
)
376 struct thread
*thread
=arch_os_get_current_thread();
378 free_ici
= fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX
,thread
));
383 index
= parse_number(ptr
);
385 if ((index
>= 0) && (index
< free_ici
)) {
386 printf("There are %d interrupt contexts.\n", free_ici
);
387 printf("printing context %d\n", index
);
388 print_context(thread
->interrupt_contexts
[index
]);
390 printf("There aren't that many/few contexts.\n");
391 printf("There are %d interrupt contexts.\n", free_ici
);
395 printf("There are no interrupt contexts!\n");
397 printf("There are %d interrupt contexts.\n", free_ici
);
398 printf("printing context %d\n", free_ici
- 1);
399 print_context(thread
->interrupt_contexts
[free_ici
- 1]);
405 backtrace_cmd(char **ptr
)
407 void lisp_backtrace(int frames
);
411 n
= parse_number(ptr
);
415 printf("Backtrace:\n");
420 catchers_cmd(char **ptr
)
422 struct catch_block
*catch;
423 struct thread
*thread
=arch_os_get_current_thread();
425 catch = (struct catch_block
*)SymbolValue(CURRENT_CATCH_BLOCK
,thread
);
428 printf("There are no active catchers!\n");
430 while (catch != NULL
) {
431 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\t"
432 "code: 0x%08lX\n\tentry: 0x%08lX\n\ttag: ",
433 (long unsigned)catch,
434 (long unsigned)(catch->uwp
),
435 (long unsigned)(catch->cfp
),
436 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
437 (long unsigned)component_ptr_from_pc((void*)catch->entry_pc
)
438 + OTHER_POINTER_LOWTAG
,
440 (long unsigned)(catch->code
),
442 (long unsigned)(catch->entry_pc
));
443 brief_print((lispobj
)catch->tag
);
444 catch = catch->previous_catch
;
450 grab_sigs_cmd(char **ptr
)
452 extern void sigint_init(void);
454 printf("Grabbing signals.\n");
461 struct cmd
*cmd
, *found
;
463 char *line
, *ptr
, *token
;
467 #ifndef LISP_FEATURE_WIN32
468 ldb_in
= fopen("/dev/tty","r+");
469 if (ldb_in
== NULL
) {
470 perror("Error opening /dev/tty");
476 ldb_in_fd
= fileno(ldb_in
);
482 line
= fgets(buf
, sizeof(buf
), ldb_in
);
487 if ((token
= parse_token(&ptr
)) == NULL
)
491 for (cmd
= supported_cmds
; cmd
->cmd
!= NULL
; cmd
++) {
492 if (strcmp(token
, cmd
->cmd
) == 0) {
497 else if (strncmp(token
, cmd
->cmd
, strlen(token
)) == 0) {
505 printf("``%s'' is ambiguous.\n", token
);
506 else if (found
== NULL
)
507 printf("unknown command: ``%s''\n", token
);
520 bcopy(curbuf
, oldbuf
, sizeof(oldbuf
));
522 printf("Welcome to LDB, a low-level debugger for the Lisp runtime environment.\n");
530 bcopy(oldbuf
, curbuf
, sizeof(curbuf
));
539 #endif /* defined(LISP_FEATURE_SB_LDB) */
541 /* what we do when things go badly wrong at a low level */
543 monitor_or_something()
545 #if defined(LISP_FEATURE_SB_LDB)
549 "The system is too badly corrupted or confused to continue at the Lisp\n\
550 level. If the system had been compiled with the SB-LDB feature, we'd drop\n\
551 into the LDB low-level debugger now. But there's no LDB in this build, so\n\
552 we can't really do anything but just exit, sorry.\n");