64e44f52f1c2bf2028507095b618a5a368d42eef
[projects/cmucl/cmucl.git] / src / lisp / monitor.c
1 /* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/monitor.c,v 1.22 2010/01/26 18:54:18 rtoy Rel $ */
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <stdlib.h>
6 #include <setjmp.h>
7 #include <sys/time.h>
8 #include <sys/resource.h>
9 #include <signal.h>
10 #include <string.h>
11
12 #include "lisp.h"
13 #include "internals.h"
14 #include "globals.h"
15 #include "vars.h"
16 #include "parse.h"
17 #include "os.h"
18 #include "interrupt.h"
19 #include "lispregs.h"
20 #include "monitor.h"
21 #include "print.h"
22 #include "arch.h"
23 #include "gc.h"
24 #include "search.h"
25 #include "purify.h"
26 #if defined GENCGC
27 #include "gencgc.h"
28 #endif
29
30 extern boolean isatty(int fd);
31
32 typedef void cmd(char **ptr);
33
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;
39
40 static struct cmd {
41     char *cmd, *help;
42     void (*fn) (char **ptr);
43 } Cmds[] = {
44     /* *INDENT-OFF* */
45     {"help", "Display this info", help},
46     {"?", NULL, 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},
64     {NULL, NULL, NULL}
65     /* *INDENT-ON* */
66 };
67
68
69 static jmp_buf curbuf;
70
71
72 static int
73 visable(unsigned char c)
74 {
75     if (c < ' ' || c > '~')
76         return ' ';
77     else
78         return c;
79 }
80
81 static void
82 dump_cmd(char **ptr)
83 {
84     static char *lastaddr = 0;
85     static int lastcount = 20;
86
87     char *addr = lastaddr;
88     int count = lastcount, displacement;
89
90     if (more_p(ptr)) {
91         addr = parse_addr(ptr);
92
93         if (more_p(ptr))
94             count = parse_number(ptr);
95     }
96
97     if (count == 0) {
98         printf("COUNT must be non-zero.\n");
99         return;
100     }
101
102     lastcount = count;
103
104     if (count > 0)
105         displacement = 4;
106     else {
107         displacement = -4;
108         count = -count;
109     }
110
111     while (count-- > 0) {
112 #ifndef alpha
113         printf("0x%08lX: ", (unsigned long) addr);
114 #else
115         printf("0x%08X: ", (u32) addr);
116 #endif
117         if (valid_addr((os_vm_address_t) addr)) {
118 #ifndef alpha
119             unsigned long *lptr = (unsigned long *) addr;
120 #else
121             u32 *lptr = (unsigned long *) addr;
122 #endif
123             unsigned short *sptr = (unsigned short *) addr;
124             unsigned char *cptr = (unsigned char *) addr;
125
126             printf
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]),
130                  visable(cptr[3]));
131         } else
132             printf("invalid address\n");
133
134         addr += displacement;
135     }
136
137     lastaddr = addr;
138 }
139
140 static void
141 print_cmd(char **ptr)
142 {
143     lispobj obj = parse_lispobj(ptr);
144
145     print(obj);
146 }
147
148 static void
149 regs_cmd(char **ptr)
150 {
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);
155 #endif
156 #if defined(i386) || defined(__x86_64)
157     printf("BSP\t=\t0x%08lX\n", SymbolValue(BINDING_STACK_POINTER));
158 #endif
159
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));
164 #else
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);
168 #endif
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));
171
172 #ifdef MIPS
173     printf("FLAGS\t=\t0x%08x\n", current_flags_register);
174 #endif
175 }
176
177 static void
178 search_cmd(char **ptr)
179 {
180     static int lastval = 0, lastcount = 0;
181     static lispobj *start = 0, *end = 0;
182     int val, count;
183     lispobj *addr, obj;
184
185     if (more_p(ptr)) {
186         val = parse_number(ptr);
187         if (val < 0 || val > 0xff) {
188             printf("Can only search for single bytes.\n");
189             return;
190         }
191         if (more_p(ptr)) {
192             addr = (lispobj *) PTR((long) parse_addr(ptr));
193             if (more_p(ptr)) {
194                 count = parse_number(ptr);
195             } else {
196                 /* Speced value and address, but no count. Only one. */
197                 count = -1;
198             }
199         } else {
200             /* Speced a value, but no address, so search same range. */
201             addr = start;
202             count = lastcount;
203         }
204     } else {
205         /* Speced nothing, search again for val. */
206         val = lastval;
207         addr = end;
208         count = lastcount;
209     }
210
211     lastval = val;
212     start = end = addr;
213     lastcount = count;
214
215     printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long) end);
216
217     while (search_for_type(val, &end, &count)) {
218         printf("found 0x%x at 0x%08lX:\n", val, (unsigned long) end);
219         obj = *end;
220         addr = end;
221         end += 2;
222         if (TypeOf(obj) == type_FunctionHeader)
223             print((long) addr | type_FunctionPointer);
224         else if (LowtagOf(obj) == type_OtherImmediate0
225                  || LowtagOf(obj) ==
226                  type_OtherImmediate1) print((lispobj) addr |
227                                              type_OtherPointer);
228         else
229             print((lispobj) addr);
230         if (count == -1)
231             return;
232     }
233 }
234
235 static void
236 call_cmd(char **ptr)
237 {
238     lispobj thing = parse_lispobj(ptr);
239     lispobj function, cons, args[3];
240     lispobj result = NIL;
241
242     int numargs;
243
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;
251                       goto fdefn;
252                   }
253               }
254               printf("symbol 0x%08lx is undefined.\n", thing);
255               return;
256
257           case type_Fdefn:
258             fdefn:
259               function = FDEFN(thing)->function;
260               if (function == NIL) {
261                   printf("fdefn 0x%08lx is undefined.\n", thing);
262                   return;
263               }
264               break;
265           default:
266               printf
267                   ("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
268                    thing);
269               return;
270         }
271     } else if (LowtagOf(thing) != type_FunctionPointer) {
272         printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
273                thing);
274         return;
275     } else
276         function = thing;
277
278     numargs = 0;
279     while (more_p(ptr)) {
280         if (numargs >= 3) {
281             printf("Too many arguments.  3 at most.\n");
282             return;
283         }
284         args[numargs++] = parse_lispobj(ptr);
285     }
286
287     switch (numargs) {
288       case 0:
289           result = funcall0(function);
290           break;
291       case 1:
292           result = funcall1(function, args[0]);
293           break;
294       case 2:
295           result = funcall2(function, args[0], args[1]);
296           break;
297       case 3:
298           result = funcall3(function, args[0], args[1], args[2]);
299           break;
300     }
301
302     print(result);
303 }
304
305 static void
306 flush_cmd(char **ptr)
307 {
308     flush_vars();
309 }
310
311 static void
312 quit(char **ptr)
313 {
314     char buf[10];
315     char *result; 
316
317     printf("Really quit? [y] ");
318     fflush(stdout);
319     result = fgets(buf, sizeof(buf), stdin);
320     if (result && (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')) {
321         exit(0);
322     }
323 }
324
325 static void
326 help(char **ptr)
327 {
328     struct cmd *cmd;
329
330     for (cmd = Cmds; cmd->cmd != NULL; cmd++)
331         if (cmd->help != NULL)
332             printf("%s\t%s\n", cmd->cmd, cmd->help);
333 }
334
335 static int done;
336
337 static void
338 exit_cmd(char **ptr)
339 {
340     done = TRUE;
341 }
342
343 static void
344 gc_cmd(char **ptr)
345 {
346     collect_garbage();
347 }
348
349 static void
350 purify_cmd(char **ptr)
351 {
352     purify(NIL, NIL);
353 }
354
355 static void
356 print_context(os_context_t * context)
357 {
358     int i;
359
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));
364 #else
365         brief_print((lispobj) SC_REG(context, i));
366 #endif
367     }
368     printf("PC:\t\t  0x%08lx\n", (unsigned long) SC_PC(context));
369 }
370
371 static void
372 print_context_cmd(char **ptr)
373 {
374     int free;
375
376     free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX) >> 2;
377
378     if (more_p(ptr)) {
379         int index;
380
381         index = parse_number(ptr);
382
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]);
387         } else {
388             printf("There aren't that many/few contexts.\n");
389             printf("There are %d interrupt contexts.\n", free);
390         }
391     } else {
392         if (free == 0)
393             printf("There are no interrupt contexts!\n");
394         else {
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]);
398         }
399     }
400 }
401
402 static void
403 backtrace_cmd(char **ptr)
404 {
405     void backtrace(int frames);
406     int n;
407
408     if (more_p(ptr))
409         n = parse_number(ptr);
410     else
411         n = 100;
412
413     printf("Backtrace:\n");
414     backtrace(n);
415 }
416
417 static void
418 catchers_cmd(char **ptr)
419 {
420     struct catch_block *catch;
421
422     catch = (struct catch_block *) SymbolValue(CURRENT_CATCH_BLOCK);
423
424     if (catch == NULL)
425         printf("There are no active catchers!\n");
426     else {
427         while (catch != NULL) {
428 #if !(defined(i386) || defined(__x86_64))
429             printf
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,
433                  catch->entry_pc);
434 #else
435             printf
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);
441 #endif
442             brief_print((lispobj) catch->tag);
443             catch = catch->previous_catch;
444         }
445     }
446 }
447
448 static void
449 grab_sigs_cmd(char **ptr)
450 {
451     extern void sigint_init(void);
452
453     printf("Grabbing signals.\n");
454     sigint_init();
455 }
456
457 static void
458 sub_monitor(void)
459 {
460     struct cmd *cmd, *found;
461     char buf[256];
462     char *line, *ptr, *token;
463     int ambig;
464
465     while (!done) {
466         printf("ldb> ");
467         fflush(stdout);
468         line = fgets(buf, sizeof(buf), stdin);
469         if (line == NULL) {
470             if (isatty(0)) {
471                 putchar('\n');
472                 /*
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
477                     * unreadable.
478                     */
479                 break;
480             } else {
481                 fprintf(stderr, "\nEOF on something other than a tty.\n");
482                 exit(1);
483             }
484         }
485         ptr = line;
486         if ((token = parse_token(&ptr)) == NULL)
487             continue;
488         ambig = 0;
489         found = NULL;
490         for (cmd = Cmds; cmd->cmd != NULL; cmd++) {
491             if (strcmp(token, cmd->cmd) == 0) {
492                 found = cmd;
493                 ambig = 0;
494                 break;
495             } else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
496                 if (found)
497                     ambig = 1;
498                 else
499                     found = cmd;
500             }
501         }
502         if (ambig)
503             printf("``%s'' is ambiguous.\n", token);
504         else if (found == NULL)
505             printf("unknown command: ``%s''\n", token);
506         else {
507             reset_printer();
508             (*found->fn) (&ptr);
509         }
510     }
511 }
512
513 void
514 ldb_monitor(void)
515 {
516     jmp_buf oldbuf;
517
518     memcpy(oldbuf, curbuf, sizeof(jmp_buf));
519
520     printf("LDB monitor\n");
521
522     setjmp(curbuf);
523
524     sub_monitor();
525
526     done = FALSE;
527
528     memcpy(curbuf, oldbuf, sizeof(jmp_buf));
529 }
530
531 void
532 throw_to_monitor(void)
533 {
534     longjmp(curbuf, 1);
535 }