0.7.13.5
[sbcl/lichteblau.git] / src / runtime / monitor.c
blobc37c2d58367bf8a1b3f8ca282288d1a86fb68ec9
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
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.
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <stdlib.h>
15 #include <setjmp.h>
16 #include <sys/time.h>
17 #include <sys/resource.h>
18 #include <signal.h>
19 #include <unistd.h>
21 #include "runtime.h"
22 #include "sbcl.h"
24 /* Almost all of this file can be skipped if we're not supporting LDB. */
25 #if defined(LISP_FEATURE_SB_LDB)
27 #include "print.h"
28 #include "arch.h"
29 #include "interr.h"
30 #include "gc.h"
31 #include "search.h"
32 #include "purify.h"
33 #include "globals.h"
34 #include "lispregs.h"
35 #include "interrupt.h"
36 #include "genesis/static-symbols.h"
37 #include "primitive-objects.h"
41 /* When we need to do command input, we use this stream, which is not
42 * in general stdin, so that things will "work" (as well as being
43 * thrown into ldb can be considered "working":-) even in a process
44 * where standard input has been redirected to a file or pipe.
46 * (We could set up output to go to a special ldb_out stream for the
47 * same reason, but there's been no pressure for that so far.)
49 * The enter-the-ldb-monitor function is responsible for setting up
50 * this stream. */
51 static FILE *ldb_in = 0;
52 static int ldb_in_fd = -1;
54 typedef void cmd(char **ptr);
56 static cmd dump_cmd, print_cmd, quit_cmd, help_cmd;
57 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
58 static cmd print_context_cmd;
59 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
60 static cmd grab_sigs_cmd;
61 static cmd kill_cmd;
63 static struct cmd {
64 char *cmd, *help;
65 void (*fn)(char **ptr);
66 } supported_cmds[] = {
67 {"help", "Display this help information.", help_cmd},
68 {"?", "(an alias for help)", help_cmd},
69 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
70 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
71 {"context", "Print interrupt context number I.", print_context_cmd},
72 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
73 {"d", "(an alias for dump)", dump_cmd},
74 {"exit", "Exit this instance of the monitor.", exit_cmd},
75 {"flush", "Flush all temp variables.", flush_cmd},
76 /* (Classic CMU CL had a "gc" command here, which seems like a
77 * reasonable idea, but the code was stale (incompatible with
78 * gencgc) so I just flushed it. -- WHN 20000814 */
79 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
80 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
81 kill_cmd},
82 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
83 {"print", "Print object at ADDRESS.", print_cmd},
84 {"p", "(an alias for print)", print_cmd},
85 {"quit", "Quit.", quit_cmd},
86 {"regs", "Display current Lisp registers.", regs_cmd},
87 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
88 {"s", "(an alias for search)", search_cmd},
89 {NULL, NULL, NULL}
92 static jmp_buf curbuf;
94 static int
95 visible(unsigned char c)
97 if (c < ' ' || c > '~')
98 return ' ';
99 else
100 return c;
103 static void
104 dump_cmd(char **ptr)
106 static char *lastaddr = 0;
107 static int lastcount = 20;
109 char *addr = lastaddr;
110 int count = lastcount, displacement;
112 if (more_p(ptr)) {
113 addr = parse_addr(ptr);
115 if (more_p(ptr))
116 count = parse_number(ptr);
119 if (count == 0) {
120 printf("COUNT must be non-zero.\n");
121 return;
124 lastcount = count;
126 if (count > 0)
127 displacement = 4;
128 else {
129 displacement = -4;
130 count = -count;
133 while (count-- > 0) {
134 #ifndef alpha
135 printf("0x%08lX: ", (unsigned long) addr);
136 #else
137 printf("0x%08X: ", (u32) addr);
138 #endif
139 if (is_valid_lisp_addr((os_vm_address_t)addr)) {
140 #ifndef alpha
141 unsigned long *lptr = (unsigned long *)addr;
142 #else
143 u32 *lptr = (u32 *)addr;
144 #endif
145 unsigned short *sptr = (unsigned short *)addr;
146 unsigned char *cptr = (unsigned char *)addr;
148 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]));
150 else
151 printf("invalid Lisp-level address\n");
153 addr += displacement;
156 lastaddr = addr;
159 static void
160 print_cmd(char **ptr)
162 lispobj obj = parse_lispobj(ptr);
164 print(obj);
167 static void
168 kill_cmd(char **ptr)
170 kill(getpid(), parse_number(ptr));
173 static void
174 regs_cmd(char **ptr)
176 printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer);
177 printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer);
178 #if !defined(__i386__)
179 printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
180 #endif
181 #ifdef __i386__
182 printf("BSP\t=\t0x%08lx\n",
183 (unsigned long)SymbolValue(BINDING_STACK_POINTER));
184 #endif
186 printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)DYNAMIC_SPACE_START);
187 #if defined(__i386__)
188 printf("ALLOC\t=\t0x%08lx\n",
189 (unsigned long)SymbolValue(ALLOCATION_POINTER));
190 #else
191 printf("ALLOC\t=\t0x%08X\n",
192 (unsigned long)dynamic_space_free_pointer);
193 printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
194 #endif
195 printf("STATIC\t=\t0x%08lx\n",
196 (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER));
197 printf("RDONLY\t=\t0x%08lx\n",
198 (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
200 #ifdef MIPS
201 printf("FLAGS\t=\t0x%08x\n", current_flags_register);
202 #endif
205 static void
206 search_cmd(char **ptr)
208 static int lastval = 0, lastcount = 0;
209 static lispobj *start = 0, *end = 0;
210 int val, count;
211 lispobj *addr, obj;
213 if (more_p(ptr)) {
214 val = parse_number(ptr);
215 if (val < 0 || val > 0xff) {
216 printf("can only search for single bytes\n");
217 return;
219 if (more_p(ptr)) {
220 addr = (lispobj *)native_pointer((long)parse_addr(ptr));
221 if (more_p(ptr)) {
222 count = parse_number(ptr);
224 else {
225 /* Specified value and address, but no count. Only one. */
226 count = -1;
229 else {
230 /* Specified a value, but no address, so search same range. */
231 addr = start;
232 count = lastcount;
235 else {
236 /* Specified nothing, search again for val. */
237 val = lastval;
238 addr = end;
239 count = lastcount;
242 lastval = val;
243 start = end = addr;
244 lastcount = count;
246 printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
248 while (search_for_type(val, &end, &count)) {
249 printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
250 obj = *end;
251 addr = end;
252 end += 2;
253 if (widetag_of(obj) == SIMPLE_FUN_HEADER_WIDETAG) {
254 print((long)addr | FUN_POINTER_LOWTAG);
255 } else if (lowtag_of(obj) == OTHER_IMMEDIATE_0_LOWTAG ||
256 lowtag_of(obj) == OTHER_IMMEDIATE_1_LOWTAG) {
257 print((lispobj)addr | OTHER_POINTER_LOWTAG);
258 } else {
259 print((lispobj)addr);
260 } if (count == -1) {
261 return;
266 /* (There used to be call_cmd() here, to call known-at-cold-init-time
267 * Lisp functions from ldb, but it bitrotted and was deleted in
268 * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
269 * it.) */
271 static void
272 flush_cmd(char **ptr)
274 flush_vars();
277 static void
278 quit_cmd(char **ptr)
280 char buf[10];
282 printf("Really quit? [y] ");
283 fflush(stdout);
284 fgets(buf, sizeof(buf), ldb_in);
285 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
286 exit(0);
289 static void
290 help_cmd(char **ptr)
292 struct cmd *cmd;
294 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
295 if (cmd->help != NULL)
296 printf("%s\t%s\n", cmd->cmd, cmd->help);
299 static int done;
301 static void
302 exit_cmd(char **ptr)
304 done = 1;
307 static void
308 purify_cmd(char **ptr)
310 purify(NIL, NIL);
313 static void
314 print_context(os_context_t *context)
316 int i;
318 for (i = 0; i < NREGS; i++) {
319 printf("%s:\t", lisp_register_names[i]);
320 #ifdef __i386__
321 brief_print((lispobj)(*os_context_register_addr(context,
322 i*2)));
323 #else
324 brief_print((lispobj)(*os_context_register_addr(context,i)));
325 #endif
327 printf("PC:\t\t 0x%08lx\n",
328 (unsigned long)(*os_context_pc_addr(context)));
331 static void
332 print_context_cmd(char **ptr)
334 int free;
336 free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
338 if (more_p(ptr)) {
339 int index;
341 index = parse_number(ptr);
343 if ((index >= 0) && (index < free)) {
344 printf("There are %d interrupt contexts.\n", free);
345 printf("printing context %d\n", index);
346 print_context(lisp_interrupt_contexts[index]);
347 } else {
348 printf("There aren't that many/few contexts.\n");
349 printf("There are %d interrupt contexts.\n", free);
351 } else {
352 if (free == 0)
353 printf("There are no interrupt contexts!\n");
354 else {
355 printf("There are %d interrupt contexts.\n", free);
356 printf("printing context %d\n", free - 1);
357 print_context(lisp_interrupt_contexts[free - 1]);
362 static void
363 backtrace_cmd(char **ptr)
365 void backtrace(int frames);
366 int n;
368 if (more_p(ptr))
369 n = parse_number(ptr);
370 else
371 n = 100;
373 printf("Backtrace:\n");
374 backtrace(n);
377 static void
378 catchers_cmd(char **ptr)
380 struct catch_block *catch;
382 catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
384 if (catch == NULL)
385 printf("There are no active catchers!\n");
386 else {
387 while (catch != NULL) {
388 #ifndef __i386__
389 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
390 (unsigned long)catch, (unsigned long)(catch->current_uwp),
391 (unsigned long)(catch->current_cont),
392 catch->current_code,
393 catch->entry_pc);
394 #else
395 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
396 (unsigned long)catch, (unsigned long)(catch->current_uwp),
397 (unsigned long)(catch->current_cont),
398 (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
399 OTHER_POINTER_LOWTAG,
400 (unsigned long)catch->entry_pc);
401 #endif
402 brief_print((lispobj)catch->tag);
403 catch = catch->previous_catch;
408 static void
409 grab_sigs_cmd(char **ptr)
411 extern void sigint_init(void);
413 printf("Grabbing signals.\n");
414 sigint_init();
417 static void
418 sub_monitor(void)
420 struct cmd *cmd, *found;
421 char buf[256];
422 char *line, *ptr, *token;
423 int ambig;
425 if (!ldb_in) {
426 ldb_in = fopen("/dev/tty","r+");
427 ldb_in_fd = fileno(ldb_in);
430 while (!done) {
431 printf("ldb> ");
432 fflush(stdout);
433 line = fgets(buf, sizeof(buf), ldb_in);
434 if (line == NULL) {
435 if (isatty(ldb_in_fd)) {
436 putchar('\n');
437 continue;
439 else {
440 fprintf(stderr, "\nEOF on something other than a tty.\n");
441 exit(0);
444 ptr = line;
445 if ((token = parse_token(&ptr)) == NULL)
446 continue;
447 ambig = 0;
448 found = NULL;
449 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
450 if (strcmp(token, cmd->cmd) == 0) {
451 found = cmd;
452 ambig = 0;
453 break;
455 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
456 if (found)
457 ambig = 1;
458 else
459 found = cmd;
462 if (ambig)
463 printf("``%s'' is ambiguous.\n", token);
464 else if (found == NULL)
465 printf("unknown command: ``%s''\n", token);
466 else {
467 reset_printer();
468 (*found->fn)(&ptr);
473 void
474 ldb_monitor()
476 jmp_buf oldbuf;
478 bcopy(curbuf, oldbuf, sizeof(oldbuf));
480 printf("LDB monitor\n");
482 setjmp(curbuf);
484 sub_monitor();
486 done = 0;
488 bcopy(oldbuf, curbuf, sizeof(curbuf));
491 void
492 throw_to_monitor()
494 longjmp(curbuf, 1);
497 #endif /* defined(LISP_FEATURE_SB_LDB) */
499 /* what we do when things go badly wrong at a low level */
500 void
501 monitor_or_something()
503 #if defined(LISP_FEATURE_SB_LDB)
504 ldb_monitor();
505 #else
506 fprintf(stderr, "There's no LDB in this build; exiting.\n");
507 exit(1);
508 #endif