1 /* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/monitor.c,v 1.22 2010/01/26 18:54:18 rtoy Rel $ */
8 #include <sys/resource.h>
13 #include "internals.h"
18 #include "interrupt.h"
30 extern boolean isatty(int fd);
32 typedef void cmd(char **ptr);
34 static cmd call_cmd, dump_cmd, print_cmd, quit, help;
35 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
36 static cmd gc_cmd, print_context_cmd;
37 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
38 static cmd grab_sigs_cmd;
42 void (*fn) (char **ptr);
45 {"help", "Display this info", help},
47 {"backtrace", "backtrace up to N frames", backtrace_cmd},
48 {"call", "call FUNCTION with ARG1, ARG2, ...", call_cmd},
49 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
50 {"context", "print interrupt context number I.", print_context_cmd},
51 {"dump", "dump memory starting at ADDRESS for COUNT words.", dump_cmd},
52 {"d", NULL, dump_cmd},
53 {"exit", "Exit this instance of the monitor.", exit_cmd},
54 {"flush", "flush all temp variables.", flush_cmd},
55 {"gc", "collect garbage (caveat collector).", gc_cmd},
56 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
57 {"purify", "purify (caveat purifier).", purify_cmd},
58 {"print", "print object at ADDRESS.", print_cmd},
59 {"p", NULL, print_cmd},
60 {"quit", "quit.", quit},
61 {"regs", "display current lisp regs.", regs_cmd},
62 {"search", "search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
63 {"s", NULL, search_cmd},
69 static jmp_buf curbuf;
73 visable(unsigned char c)
75 if (c < ' ' || c > '~')
84 static char *lastaddr = 0;
85 static int lastcount = 20;
87 char *addr = lastaddr;
88 int count = lastcount, displacement;
91 addr = parse_addr(ptr);
94 count = parse_number(ptr);
98 printf("COUNT must be non-zero.\n");
111 while (count-- > 0) {
113 printf("0x%08lX: ", (unsigned long) addr);
115 printf("0x%08X: ", (u32) addr);
117 if (valid_addr((os_vm_address_t) addr)) {
119 unsigned long *lptr = (unsigned long *) addr;
121 u32 *lptr = (unsigned long *) addr;
123 unsigned short *sptr = (unsigned short *) addr;
124 unsigned char *cptr = (unsigned char *) addr;
127 ("0x%08lx 0x%04x 0x%04x 0x%02x 0x%02x 0x%02x 0x%02x %c%c%c%c\n",
128 lptr[0], sptr[0], sptr[1], cptr[0], cptr[1], cptr[2], cptr[3],
129 visable(cptr[0]), visable(cptr[1]), visable(cptr[2]),
132 printf("invalid address\n");
134 addr += displacement;
141 print_cmd(char **ptr)
143 lispobj obj = parse_lispobj(ptr);
151 printf("CSP\t=\t0x%08lX\n", (unsigned long) current_control_stack_pointer);
152 printf("FP\t=\t0x%08lX\n", (unsigned long) current_control_frame_pointer);
153 #if !defined(ibmrt) && !defined(i386) && !defined(__x86_64)
154 printf("BSP\t=\t0x%08lX\n", (unsigned long) current_binding_stack_pointer);
156 #if defined(i386) || defined(__x86_64)
157 printf("BSP\t=\t0x%08lX\n", SymbolValue(BINDING_STACK_POINTER));
160 printf("DYNAMIC\t=\t0x%08lX\n", (unsigned long) current_dynamic_space);
161 #if defined(ibmrt) || defined(i386) || defined(__x86_64)
162 printf("ALLOC\t=\t0x%08lX\n", SymbolValue(ALLOCATION_POINTER));
163 printf("TRIGGER\t=\t0x%08lX\n", SymbolValue(INTERNAL_GC_TRIGGER));
165 printf("ALLOC\t=\t0x%08lX\n",
166 (unsigned long) current_dynamic_space_free_pointer);
167 printf("TRIGGER\t=\t0x%08lX\n", (unsigned long) current_auto_gc_trigger);
169 printf("STATIC\t=\t0x%08lX\n", SymbolValue(STATIC_SPACE_FREE_POINTER));
170 printf("RDONLY\t=\t0x%08lX\n", SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
173 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
178 search_cmd(char **ptr)
180 static int lastval = 0, lastcount = 0;
181 static lispobj *start = 0, *end = 0;
186 val = parse_number(ptr);
187 if (val < 0 || val > 0xff) {
188 printf("Can only search for single bytes.\n");
192 addr = (lispobj *) PTR((long) parse_addr(ptr));
194 count = parse_number(ptr);
196 /* Speced value and address, but no count. Only one. */
200 /* Speced a value, but no address, so search same range. */
205 /* Speced nothing, search again for val. */
215 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long) end);
217 while (search_for_type(val, &end, &count)) {
218 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long) end);
222 if (TypeOf(obj) == type_FunctionHeader)
223 print((long) addr | type_FunctionPointer);
224 else if (LowtagOf(obj) == type_OtherImmediate0
226 type_OtherImmediate1) print((lispobj) addr |
229 print((lispobj) addr);
238 lispobj thing = parse_lispobj(ptr);
239 lispobj function, cons, args[3];
240 lispobj result = NIL;
244 if (LowtagOf(thing) == type_OtherPointer) {
245 switch (TypeOf(*(lispobj *) (thing - type_OtherPointer))) {
246 case type_SymbolHeader:
247 for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
248 cons != NIL; cons = CONS(cons)->cdr) {
249 if (FDEFN(CONS(cons)->car)->name == thing) {
250 thing = CONS(cons)->car;
254 printf("symbol 0x%08lx is undefined.\n", thing);
259 function = FDEFN(thing)->function;
260 if (function == NIL) {
261 printf("fdefn 0x%08lx is undefined.\n", thing);
267 ("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
271 } else if (LowtagOf(thing) != type_FunctionPointer) {
272 printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
279 while (more_p(ptr)) {
281 printf("Too many arguments. 3 at most.\n");
284 args[numargs++] = parse_lispobj(ptr);
289 result = funcall0(function);
292 result = funcall1(function, args[0]);
295 result = funcall2(function, args[0], args[1]);
298 result = funcall3(function, args[0], args[1], args[2]);
306 flush_cmd(char **ptr)
317 printf("Really quit? [y] ");
319 result = fgets(buf, sizeof(buf), stdin);
320 if (result && (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')) {
330 for (cmd = Cmds; cmd->cmd != NULL; cmd++)
331 if (cmd->help != NULL)
332 printf("%s\t%s\n", cmd->cmd, cmd->help);
350 purify_cmd(char **ptr)
356 print_context(os_context_t * context)
360 for (i = 0; i < NREGS; i++) {
361 printf("%s:\t", lisp_register_names[i]);
362 #if defined(i386) || defined(__x86_64)
363 brief_print((lispobj) SC_REG(context, i * 2));
365 brief_print((lispobj) SC_REG(context, i));
368 printf("PC:\t\t 0x%08lx\n", (unsigned long) SC_PC(context));
372 print_context_cmd(char **ptr)
376 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX) >> 2;
381 index = parse_number(ptr);
383 if ((index >= 0) && (index < free)) {
384 printf("There are %d interrupt contexts.\n", free);
385 printf("Printing context %d\n", index);
386 print_context(lisp_interrupt_contexts[index]);
388 printf("There aren't that many/few contexts.\n");
389 printf("There are %d interrupt contexts.\n", free);
393 printf("There are no interrupt contexts!\n");
395 printf("There are %d interrupt contexts.\n", free);
396 printf("Printing context %d\n", free - 1);
397 print_context(lisp_interrupt_contexts[free - 1]);
403 backtrace_cmd(char **ptr)
405 void backtrace(int frames);
409 n = parse_number(ptr);
413 printf("Backtrace:\n");
418 catchers_cmd(char **ptr)
420 struct catch_block *catch;
422 catch = (struct catch_block *) SymbolValue(CURRENT_CATCH_BLOCK);
425 printf("There are no active catchers!\n");
427 while (catch != NULL) {
428 #if !(defined(i386) || defined(__x86_64))
430 ("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
431 (unsigned long) catch, (unsigned long) (catch->current_uwp),
432 (unsigned long) (catch->current_cont), catch->current_code,
436 ("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%p\n\tentry: 0x%08lx\n\ttag: ",
437 (unsigned long) catch, (unsigned long) (catch->current_uwp),
438 (unsigned long) (catch->current_cont),
439 component_ptr_from_pc((lispobj *) catch->entry_pc) +
440 type_OtherPointer, catch->entry_pc);
442 brief_print((lispobj) catch->tag);
443 catch = catch->previous_catch;
449 grab_sigs_cmd(char **ptr)
451 extern void sigint_init(void);
453 printf("Grabbing signals.\n");
460 struct cmd *cmd, *found;
462 char *line, *ptr, *token;
468 line = fgets(buf, sizeof(buf), stdin);
473 * We can no longer read anything from stdin, so
474 * just exit this loop instead of spewing an
475 * endless stream of prompts. This also means we
476 * can't use ldb anymore because stdin is
481 fprintf(stderr, "\nEOF on something other than a tty.\n");
486 if ((token = parse_token(&ptr)) == NULL)
490 for (cmd = Cmds; cmd->cmd != NULL; cmd++) {
491 if (strcmp(token, cmd->cmd) == 0) {
495 } else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
503 printf("``%s'' is ambiguous.\n", token);
504 else if (found == NULL)
505 printf("unknown command: ``%s''\n", token);
518 memcpy(oldbuf, curbuf, sizeof(jmp_buf));
520 printf("LDB monitor\n");
528 memcpy(curbuf, oldbuf, sizeof(jmp_buf));
532 throw_to_monitor(void)