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>
36 #include "interrupt.h"
38 #include "genesis/static-symbols.h"
39 #include "genesis/primitive-objects.h"
43 /* When we need to do command input, we use this stream, which is not
44 * in general stdin, so that things will "work" (as well as being
45 * thrown into ldb can be considered "working":-) even in a process
46 * where standard input has been redirected to a file or pipe.
48 * (We could set up output to go to a special ldb_out stream for the
49 * same reason, but there's been no pressure for that so far.)
51 * The enter-the-ldb-monitor function is responsible for setting up
53 static FILE *ldb_in
= 0;
54 static int ldb_in_fd
= -1;
56 typedef void cmd(char **ptr
);
58 static cmd dump_cmd
, print_cmd
, quit_cmd
, help_cmd
;
59 static cmd flush_cmd
, search_cmd
, regs_cmd
, exit_cmd
;
60 static cmd print_context_cmd
;
61 static cmd backtrace_cmd
, purify_cmd
, catchers_cmd
;
62 static cmd grab_sigs_cmd
;
67 void (*fn
)(char **ptr
);
68 } supported_cmds
[] = {
69 {"help", "Display this help information.", help_cmd
},
70 {"?", "(an alias for help)", help_cmd
},
71 {"backtrace", "Backtrace up to N frames.", backtrace_cmd
},
72 {"catchers", "Print a list of all the active catchers.", catchers_cmd
},
73 {"context", "Print interrupt context number I.", print_context_cmd
},
74 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd
},
75 {"d", "(an alias for dump)", dump_cmd
},
76 {"exit", "Exit this instance of the monitor.", exit_cmd
},
77 {"flush", "Flush all temp variables.", flush_cmd
},
78 /* (Classic CMU CL had a "gc" command here, which seems like a
79 * reasonable idea, but the code was stale (incompatible with
80 * gencgc) so I just flushed it. -- WHN 20000814 */
81 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd
},
82 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
84 {"purify", "Purify. (Caveat purifier!)", purify_cmd
},
85 {"print", "Print object at ADDRESS.", print_cmd
},
86 {"p", "(an alias for print)", print_cmd
},
87 {"quit", "Quit.", quit_cmd
},
88 {"regs", "Display current Lisp registers.", regs_cmd
},
89 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd
},
90 {"s", "(an alias for search)", search_cmd
},
94 static jmp_buf curbuf
;
97 visible(unsigned char c
)
99 if (c
< ' ' || c
> '~')
108 static char *lastaddr
= 0;
109 static int lastcount
= 20;
111 char *addr
= lastaddr
;
112 int count
= lastcount
, displacement
;
116 if (!strncmp(*ptr
, "-f ", 3)) {
120 addr
= parse_addr(ptr
, !force
);
123 count
= parse_number(ptr
);
127 printf("COUNT must be non-zero.\n");
134 displacement
= N_WORD_BYTES
;
136 displacement
= -N_WORD_BYTES
;
140 while (count
-- > 0) {
141 #ifndef LISP_FEATURE_ALPHA
142 printf("%p: ", (os_vm_address_t
) addr
);
144 printf("0x%08X: ", (u32
) addr
);
146 if (force
|| gc_managed_addr_p((lispobj
)addr
)) {
147 #ifndef LISP_FEATURE_ALPHA
148 unsigned long *lptr
= (unsigned long *)addr
;
150 u32
*lptr
= (u32
*)addr
;
152 unsigned char *cptr
= (unsigned char *)addr
;
154 #if N_WORD_BYTES == 8
155 printf("0x%016lx | %c%c%c%c%c%c%c%c\n",
157 visible(cptr
[0]), visible(cptr
[1]),
158 visible(cptr
[2]), visible(cptr
[3]),
159 visible(cptr
[4]), visible(cptr
[5]),
160 visible(cptr
[6]), visible(cptr
[7]));
162 unsigned short *sptr
= (unsigned short *)addr
;
163 printf("0x%08lx 0x%04x 0x%04x "
164 "0x%02x 0x%02x 0x%02x 0x%02x "
167 lptr
[0], sptr
[0], sptr
[1],
168 cptr
[0], cptr
[1], cptr
[2], cptr
[3],
169 visible(cptr
[0]), visible(cptr
[1]),
170 visible(cptr
[2]), visible(cptr
[3]));
174 printf("invalid Lisp-level address\n");
176 addr
+= displacement
;
183 print_cmd(char **ptr
)
185 lispobj obj
= parse_lispobj(ptr
);
193 #ifndef LISP_FEATURE_WIN32
194 kill(getpid(), parse_number(ptr
));
201 struct thread
__attribute__((unused
)) *thread
=arch_os_get_current_thread();
203 printf("CSP\t=\t%p ", access_control_stack_pointer(thread
));
204 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
205 printf("CFP\t=\t%p ", access_control_frame_pointer(thread
));
209 printf("BSP\t=\t%p\n", get_binding_stack_pointer(thread
));
211 /* printf("BSP\t=\t%p\n", (void*)SymbolValue(BINDING_STACK_POINTER)); */
215 #ifdef LISP_FEATURE_GENCGC
216 /* printf("DYNAMIC\t=\t%p\n", (void*)DYNAMIC_SPACE_START); */
218 printf("STATIC\t=\t%p ", static_space_free_pointer
);
219 printf("RDONLY\t=\t%p ", read_only_space_free_pointer
);
220 printf("DYNAMIC\t=\t%p\n", (void*)current_dynamic_space
);
223 #ifndef ALLOCATION_POINTER
224 printf("ALLOC\t=\t%p\n", (void*)dynamic_space_free_pointer
);
226 printf("ALLOC\t=\t%p\n", (void*)SymbolValue(ALLOCATION_POINTER
, thread
));
229 #ifndef LISP_FEATURE_GENCGC
230 printf("TRIGGER\t=\t%p\n", (void*)current_auto_gc_trigger
);
235 search_cmd(char **ptr
)
237 static int lastval
= 0, lastcount
= 0;
238 static lispobj
*start
= 0, *end
= 0;
243 val
= parse_number(ptr
);
244 if (val
< 0 || val
> 0xff) {
245 printf("can only search for single bytes\n");
249 addr
= native_pointer((uword_t
)parse_addr(ptr
, 1));
251 count
= parse_number(ptr
);
254 /* Specified value and address, but no count. Only one. */
259 /* Specified a value, but no address, so search same range. */
265 /* Specified nothing, search again for val. */
275 printf("searching for 0x%x at %p\n", val
, (void*)(uword_t
)end
);
277 while (search_for_type(val
, &end
, &count
)) {
278 printf("found 0x%x at %p:\n", val
, (void*)(uword_t
)end
);
282 if (widetag_of(obj
) == SIMPLE_FUN_WIDETAG
) {
283 print((uword_t
)addr
| FUN_POINTER_LOWTAG
);
284 } else if (other_immediate_lowtag_p(obj
)) {
285 print((lispobj
)addr
| OTHER_POINTER_LOWTAG
);
287 print((lispobj
)addr
);
294 /* (There used to be call_cmd() here, to call known-at-cold-init-time
295 * Lisp functions from ldb, but it bitrotted and was deleted in
296 * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
300 flush_cmd(char **ptr
)
310 printf("Really quit? [y] ");
312 if (fgets(buf
, sizeof(buf
), ldb_in
)) {
313 if (buf
[0] == 'y' || buf
[0] == 'Y' || buf
[0] == '\n')
316 printf("\nUnable to read response, assuming y.\n");
326 for (cmd
= supported_cmds
; cmd
->cmd
!= NULL
; cmd
++)
327 if (cmd
->help
!= NULL
)
328 printf("%s\t%s\n", cmd
->cmd
, cmd
->help
);
340 purify_cmd(char **ptr
)
346 print_context(os_context_t
*context
)
350 for (i
= 0; i
< NREGS
; i
++) {
351 printf("%s:\t", lisp_register_names
[i
]);
352 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
353 brief_print((lispobj
)(*os_context_register_addr(context
, i
*2)));
355 brief_print((lispobj
)(*os_context_register_addr(context
,i
)));
358 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_PPC)
359 printf("DAR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context
, 41)));
360 printf("DSISR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context
, 42)));
362 printf("PC:\t\t 0x%08lx\n",
363 (unsigned long)(*os_context_pc_addr(context
)));
367 print_context_cmd(char **ptr
)
370 struct thread
*thread
=arch_os_get_current_thread();
372 free_ici
= fixnum_value(read_TLS(FREE_INTERRUPT_CONTEXT_INDEX
,thread
));
377 index
= parse_number(ptr
);
379 if ((index
>= 0) && (index
< free_ici
)) {
380 printf("There are %d interrupt contexts.\n", free_ici
);
381 printf("printing context %d\n", index
);
382 print_context(thread
->interrupt_contexts
[index
]);
384 printf("There aren't that many/few contexts.\n");
385 printf("There are %d interrupt contexts.\n", free_ici
);
389 printf("There are no interrupt contexts!\n");
391 printf("There are %d interrupt contexts.\n", free_ici
);
392 printf("printing context %d\n", free_ici
- 1);
393 print_context(thread
->interrupt_contexts
[free_ici
- 1]);
399 backtrace_cmd(char **ptr
)
401 void lisp_backtrace(int frames
);
405 n
= parse_number(ptr
);
409 printf("Backtrace:\n");
414 catchers_cmd(char **ptr
)
416 struct catch_block
*catch = (struct catch_block
*)
417 read_TLS(CURRENT_CATCH_BLOCK
, arch_os_get_current_thread());
420 printf("There are no active catchers!\n");
422 while (catch != NULL
) {
423 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\t"
424 "code: 0x%08lX\n\tentry: 0x%08lX\n\ttag: ",
425 (long unsigned)catch,
426 (long unsigned)(catch->uwp
),
427 (long unsigned)(catch->cfp
),
428 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
429 (long unsigned)component_ptr_from_pc((void*)catch->entry_pc
)
430 + OTHER_POINTER_LOWTAG
,
432 (long unsigned)(catch->code
),
434 (long unsigned)(catch->entry_pc
));
435 brief_print((lispobj
)catch->tag
);
436 catch = catch->previous_catch
;
442 grab_sigs_cmd(char **ptr
)
444 extern void sigint_init(void);
446 printf("Grabbing signals.\n");
453 struct cmd
*cmd
, *found
;
455 char *line
, *ptr
, *token
;
459 #ifndef LISP_FEATURE_WIN32
460 ldb_in
= fopen("/dev/tty","r+");
461 if (ldb_in
== NULL
) {
462 perror("Error opening /dev/tty");
468 ldb_in_fd
= fileno(ldb_in
);
474 line
= fgets(buf
, sizeof(buf
), ldb_in
);
479 if ((token
= parse_token(&ptr
)) == NULL
)
483 for (cmd
= supported_cmds
; cmd
->cmd
!= NULL
; cmd
++) {
484 if (strcmp(token
, cmd
->cmd
) == 0) {
489 else if (strncmp(token
, cmd
->cmd
, strlen(token
)) == 0) {
497 printf("``%s'' is ambiguous.\n", token
);
498 else if (found
== NULL
)
499 printf("unknown command: ``%s''\n", token
);
512 bcopy(curbuf
, oldbuf
, sizeof(oldbuf
));
514 printf("Welcome to LDB, a low-level debugger for the Lisp runtime environment.\n");
522 bcopy(oldbuf
, curbuf
, sizeof(curbuf
));
531 /* what we do when things go badly wrong at a low level */
533 monitor_or_something()