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>
24 /* Almost all of this file can be skipped if we're not supporting LDB. */
25 #if defined(LISP_FEATURE_SB_LDB)
35 #include "interrupt.h"
37 #include "genesis/static-symbols.h"
38 #include "genesis/primitive-objects.h"
42 /* When we need to do command input, we use this stream, which is not
43 * in general stdin, so that things will "work" (as well as being
44 * thrown into ldb can be considered "working":-) even in a process
45 * where standard input has been redirected to a file or pipe.
47 * (We could set up output to go to a special ldb_out stream for the
48 * same reason, but there's been no pressure for that so far.)
50 * The enter-the-ldb-monitor function is responsible for setting up
52 static FILE *ldb_in
= 0;
53 static int ldb_in_fd
= -1;
55 typedef void cmd(char **ptr
);
57 static cmd dump_cmd
, print_cmd
, quit_cmd
, help_cmd
;
58 static cmd flush_cmd
, search_cmd
, regs_cmd
, exit_cmd
;
59 static cmd print_context_cmd
;
60 static cmd backtrace_cmd
, purify_cmd
, catchers_cmd
;
61 static cmd grab_sigs_cmd
;
66 void (*fn
)(char **ptr
);
67 } supported_cmds
[] = {
68 {"help", "Display this help information.", help_cmd
},
69 {"?", "(an alias for help)", help_cmd
},
70 {"backtrace", "Backtrace up to N frames.", backtrace_cmd
},
71 {"catchers", "Print a list of all the active catchers.", catchers_cmd
},
72 {"context", "Print interrupt context number I.", print_context_cmd
},
73 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd
},
74 {"d", "(an alias for dump)", dump_cmd
},
75 {"exit", "Exit this instance of the monitor.", exit_cmd
},
76 {"flush", "Flush all temp variables.", flush_cmd
},
77 /* (Classic CMU CL had a "gc" command here, which seems like a
78 * reasonable idea, but the code was stale (incompatible with
79 * gencgc) so I just flushed it. -- WHN 20000814 */
80 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd
},
81 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
83 {"purify", "Purify. (Caveat purifier!)", purify_cmd
},
84 {"print", "Print object at ADDRESS.", print_cmd
},
85 {"p", "(an alias for print)", print_cmd
},
86 {"quit", "Quit.", quit_cmd
},
87 {"regs", "Display current Lisp registers.", regs_cmd
},
88 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd
},
89 {"s", "(an alias for search)", search_cmd
},
93 static jmp_buf curbuf
;
96 visible(unsigned char c
)
98 if (c
< ' ' || c
> '~')
107 static char *lastaddr
= 0;
108 static int lastcount
= 20;
110 char *addr
= lastaddr
;
111 int count
= lastcount
, displacement
;
114 addr
= parse_addr(ptr
);
117 count
= parse_number(ptr
);
121 printf("COUNT must be non-zero.\n");
134 while (count
-- > 0) {
136 printf("0x%08lX: ", (unsigned long) addr
);
138 printf("0x%08X: ", (u32
) addr
);
140 if (is_valid_lisp_addr((os_vm_address_t
)addr
)) {
142 unsigned long *lptr
= (unsigned long *)addr
;
144 u32
*lptr
= (u32
*)addr
;
146 unsigned short *sptr
= (unsigned short *)addr
;
147 unsigned char *cptr
= (unsigned char *)addr
;
149 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]));
152 printf("invalid Lisp-level address\n");
154 addr
+= displacement
;
161 print_cmd(char **ptr
)
163 lispobj obj
= parse_lispobj(ptr
);
171 kill(getpid(), parse_number(ptr
));
177 printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer
);
178 printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer
);
179 #if !defined(LISP_FEATURE_X86)
180 printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer
);
183 #ifdef LISP_FEATURE_X86
184 printf("BSP\t=\t0x%08lx\n",
185 (unsigned long)SymbolValue(BINDING_STACK_POINTER
));
188 printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)DYNAMIC_SPACE_START
);
189 #if defined(LISP_FEATURE_X86)
190 printf("ALLOC\t=\t0x%08lx\n",
191 (unsigned long)SymbolValue(ALLOCATION_POINTER
));
193 printf("ALLOC\t=\t0x%08X\n",
194 (unsigned long)dynamic_space_free_pointer
);
195 printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger
);
197 printf("STATIC\t=\t0x%08lx\n",
198 (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER
));
199 printf("RDONLY\t=\t0x%08lx\n",
200 (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
));
203 printf("FLAGS\t=\t0x%08x\n", current_flags_register
);
208 search_cmd(char **ptr
)
210 static int lastval
= 0, lastcount
= 0;
211 static lispobj
*start
= 0, *end
= 0;
216 val
= parse_number(ptr
);
217 if (val
< 0 || val
> 0xff) {
218 printf("can only search for single bytes\n");
222 addr
= (lispobj
*)native_pointer((long)parse_addr(ptr
));
224 count
= parse_number(ptr
);
227 /* Specified value and address, but no count. Only one. */
232 /* Specified a value, but no address, so search same range. */
238 /* Specified nothing, search again for val. */
248 printf("searching for 0x%x at 0x%08lX\n", val
, (unsigned long)end
);
250 while (search_for_type(val
, &end
, &count
)) {
251 printf("found 0x%x at 0x%08lX:\n", val
, (unsigned long)end
);
255 if (widetag_of(obj
) == SIMPLE_FUN_HEADER_WIDETAG
) {
256 print((long)addr
| FUN_POINTER_LOWTAG
);
257 } else if (lowtag_of(obj
) == OTHER_IMMEDIATE_0_LOWTAG
||
258 lowtag_of(obj
) == OTHER_IMMEDIATE_1_LOWTAG
) {
259 print((lispobj
)addr
| OTHER_POINTER_LOWTAG
);
261 print((lispobj
)addr
);
268 /* (There used to be call_cmd() here, to call known-at-cold-init-time
269 * Lisp functions from ldb, but it bitrotted and was deleted in
270 * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
274 flush_cmd(char **ptr
)
284 printf("Really quit? [y] ");
286 fgets(buf
, sizeof(buf
), ldb_in
);
287 if (buf
[0] == 'y' || buf
[0] == 'Y' || buf
[0] == '\n')
296 for (cmd
= supported_cmds
; cmd
->cmd
!= NULL
; cmd
++)
297 if (cmd
->help
!= NULL
)
298 printf("%s\t%s\n", cmd
->cmd
, cmd
->help
);
310 purify_cmd(char **ptr
)
316 print_context(os_context_t
*context
)
320 for (i
= 0; i
< NREGS
; i
++) {
321 printf("%s:\t", lisp_register_names
[i
]);
322 #ifdef LISP_FEATURE_X86
323 brief_print((lispobj
)(*os_context_register_addr(context
,
326 brief_print((lispobj
)(*os_context_register_addr(context
,i
)));
329 #ifdef LISP_FEATURE_DARWIN
330 printf("DAR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context
, 41)));
331 printf("DSISR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context
, 42)));
333 printf("PC:\t\t 0x%08lx\n",
334 (unsigned long)(*os_context_pc_addr(context
)));
338 print_context_cmd(char **ptr
)
341 struct thread
*thread
=arch_os_get_current_thread();
343 free
= SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX
,thread
)>>2;
348 index
= parse_number(ptr
);
350 if ((index
>= 0) && (index
< free
)) {
351 printf("There are %d interrupt contexts.\n", free
);
352 printf("printing context %d\n", index
);
353 print_context(thread
->interrupt_contexts
[index
]);
355 printf("There aren't that many/few contexts.\n");
356 printf("There are %d interrupt contexts.\n", free
);
360 printf("There are no interrupt contexts!\n");
362 printf("There are %d interrupt contexts.\n", free
);
363 printf("printing context %d\n", free
- 1);
364 print_context(thread
->interrupt_contexts
[free
- 1]);
370 backtrace_cmd(char **ptr
)
372 void backtrace(int frames
);
376 n
= parse_number(ptr
);
380 printf("Backtrace:\n");
385 catchers_cmd(char **ptr
)
387 struct catch_block
*catch;
388 struct thread
*thread
=arch_os_get_current_thread();
390 catch = (struct catch_block
*)SymbolValue(CURRENT_CATCH_BLOCK
,thread
);
393 printf("There are no active catchers!\n");
395 while (catch != NULL
) {
396 #ifndef LISP_FEATURE_X86
397 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
398 (unsigned long)catch, (unsigned long)(catch->current_uwp
),
399 (unsigned long)(catch->current_cont
),
403 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
404 (unsigned long)catch, (unsigned long)(catch->current_uwp
),
405 (unsigned long)(catch->current_cont
),
406 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc
) +
407 OTHER_POINTER_LOWTAG
,
408 (unsigned long)catch->entry_pc
);
410 brief_print((lispobj
)catch->tag
);
411 catch = catch->previous_catch
;
417 grab_sigs_cmd(char **ptr
)
419 extern void sigint_init(void);
421 printf("Grabbing signals.\n");
428 struct cmd
*cmd
, *found
;
430 char *line
, *ptr
, *token
;
434 ldb_in
= fopen("/dev/tty","r+");
435 ldb_in_fd
= fileno(ldb_in
);
441 line
= fgets(buf
, sizeof(buf
), ldb_in
);
443 if (isatty(ldb_in_fd
)) {
448 fprintf(stderr
, "\nEOF on something other than a tty.\n");
453 if ((token
= parse_token(&ptr
)) == NULL
)
457 for (cmd
= supported_cmds
; cmd
->cmd
!= NULL
; cmd
++) {
458 if (strcmp(token
, cmd
->cmd
) == 0) {
463 else if (strncmp(token
, cmd
->cmd
, strlen(token
)) == 0) {
471 printf("``%s'' is ambiguous.\n", token
);
472 else if (found
== NULL
)
473 printf("unknown command: ``%s''\n", token
);
486 bcopy(curbuf
, oldbuf
, sizeof(oldbuf
));
488 printf("LDB monitor\n");
496 bcopy(oldbuf
, curbuf
, sizeof(curbuf
));
505 #endif /* defined(LISP_FEATURE_SB_LDB) */
507 /* what we do when things go badly wrong at a low level */
509 monitor_or_something()
511 #if defined(LISP_FEATURE_SB_LDB)
514 fprintf(stderr
, "There's no LDB in this build; exiting.\n");