23f87d2077999b1ca427a8a3399c0ad281271094
[sbcl.git] / src / runtime / monitor.c
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
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.
10  */
11
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>
20
21 #include "runtime.h"
22 #include "sbcl.h"
23
24 /* Almost all of this file can be skipped if we're not supporting LDB. */
25 #if defined(LISP_FEATURE_SB_LDB)
26
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
34 /* When we need to do command input, we use this stream, which is not
35  * in general stdin, so that things will "work" (as well as being
36  * thrown into ldb can be considered "working":-) even in a process
37  * where standard input has been redirected to a file or pipe.
38  *
39  * (We could set up output to go to a special ldb_out stream for the
40  * same reason, but there's been no pressure for that so far.)
41  * 
42  * The enter-the-ldb-monitor function is responsible for setting up
43  * this stream. */
44 static FILE *ldb_in = 0;
45 static int ldb_in_fd = -1;
46
47 typedef void cmd(char **ptr);
48
49 static cmd call_cmd, dump_cmd, print_cmd, quit_cmd, help_cmd;
50 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
51 static cmd print_context_cmd;
52 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
53 static cmd grab_sigs_cmd;
54 static cmd kill_cmd;
55
56 static struct cmd {
57     char *cmd, *help;
58     void (*fn)(char **ptr);
59 } supported_cmds[] = {
60     {"help", "Display this help information.", help_cmd},
61     {"?", "(an alias for help)", help_cmd},
62     {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
63     {"call", "Call FUNCTION with ARG1, ARG2, ...", call_cmd},
64     {"catchers", "Print a list of all the active catchers.", catchers_cmd},
65     {"context", "Print interrupt context number I.", print_context_cmd},
66     {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
67     {"d", "(an alias for dump)", dump_cmd},
68     {"exit", "Exit this instance of the monitor.", exit_cmd},
69     {"flush", "Flush all temp variables.", flush_cmd},
70     /* (Classic CMU CL had a "gc" command here, which seems like a
71      * reasonable idea, but the code was stale (incompatible with
72      * gencgc) so I just flushed it. -- WHN 20000814 */
73     {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
74     {"kill", "Kill ourself with signal number N (useful if running under gdb)",
75      kill_cmd},
76     {"purify", "Purify. (Caveat purifier!)", purify_cmd},
77     {"print", "Print object at ADDRESS.", print_cmd},
78     {"p", "(an alias for print)", print_cmd},
79     {"quit", "Quit.", quit_cmd},
80     {"regs", "Display current Lisp registers.", regs_cmd},
81     {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
82     {"s", "(an alias for search)", search_cmd},
83     {NULL, NULL, NULL}
84 };
85
86 static jmp_buf curbuf;
87
88 static int
89 visible(unsigned char c)
90 {
91     if (c < ' ' || c > '~')
92         return ' ';
93     else
94         return c;
95 }
96
97 static void
98 dump_cmd(char **ptr)
99 {
100     static char *lastaddr = 0;
101     static int lastcount = 20;
102
103     char *addr = lastaddr;
104     int count = lastcount, displacement;
105
106     if (more_p(ptr)) {
107         addr = parse_addr(ptr);
108
109         if (more_p(ptr))
110             count = parse_number(ptr);
111     }
112
113     if (count == 0) {
114         printf("COUNT must be non-zero.\n");
115         return;
116     }
117
118     lastcount = count;
119
120     if (count > 0)
121         displacement = 4;
122     else {
123         displacement = -4;
124         count = -count;
125     }
126
127     while (count-- > 0) {
128 #ifndef alpha
129         printf("0x%08lX: ", (unsigned long) addr);
130 #else
131         printf("0x%08X: ", (u32) addr);
132 #endif
133         if (is_valid_lisp_addr((os_vm_address_t)addr)) {
134 #ifndef alpha
135             unsigned long *lptr = (unsigned long *)addr;
136 #else
137             u32 *lptr = (u32 *)addr;
138 #endif
139             unsigned short *sptr = (unsigned short *)addr;
140             unsigned char *cptr = (unsigned char *)addr;
141
142             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]));
143         }
144         else
145             printf("invalid Lisp-level address\n");
146
147         addr += displacement;
148     }
149
150     lastaddr = addr;
151 }
152
153 static void
154 print_cmd(char **ptr)
155 {
156     lispobj obj = parse_lispobj(ptr);
157
158     print(obj);
159 }
160
161 static void
162 kill_cmd(char **ptr)
163 {
164     kill(getpid(), parse_number(ptr));
165 }
166
167 static void
168 regs_cmd(char **ptr)
169 {
170     printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer);
171     printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer);
172 #if !defined(__i386__)
173     printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
174 #endif
175 #ifdef __i386__
176     printf("BSP\t=\t0x%08lx\n",
177            (unsigned long)SymbolValue(BINDING_STACK_POINTER));
178 #endif
179
180     printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)DYNAMIC_SPACE_START);
181 #if defined(__i386__)
182     printf("ALLOC\t=\t0x%08lx\n",
183            (unsigned long)SymbolValue(ALLOCATION_POINTER));
184     printf("TRIGGER\t=\t0x%08lx\n",
185            (unsigned long)SymbolValue(INTERNAL_GC_TRIGGER));
186 #else
187     printf("ALLOC\t=\t0x%08X\n",
188            (unsigned long)dynamic_space_free_pointer);
189     printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
190 #endif
191     printf("STATIC\t=\t0x%08lx\n",
192            (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER));
193     printf("RDONLY\t=\t0x%08lx\n",
194            (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
195
196 #ifdef MIPS
197     printf("FLAGS\t=\t0x%08x\n", current_flags_register);
198 #endif
199 }
200
201 static void
202 search_cmd(char **ptr)
203 {
204     static int lastval = 0, lastcount = 0;
205     static lispobj *start = 0, *end = 0;
206     int val, count;
207     lispobj *addr, obj;
208
209     if (more_p(ptr)) {
210         val = parse_number(ptr);
211         if (val < 0 || val > 0xff) {
212             printf("can only search for single bytes\n");
213             return;
214         }
215         if (more_p(ptr)) {
216             addr = (lispobj *)native_pointer((long)parse_addr(ptr));
217             if (more_p(ptr)) {
218                 count = parse_number(ptr);
219             }
220             else {
221                 /* Specified value and address, but no count. Only one. */
222                 count = -1;
223             }
224         }
225         else {
226             /* Specified a value, but no address, so search same range. */
227             addr = start;
228             count = lastcount;
229         }
230     }
231     else {
232         /* Specified nothing, search again for val. */
233         val = lastval;
234         addr = end;
235         count = lastcount;
236     }
237
238     lastval = val;
239     start = end = addr;
240     lastcount = count;
241
242     printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
243
244     while (search_for_type(val, &end, &count)) {
245         printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
246         obj = *end;
247         addr = end;
248         end += 2;
249         if (widetag_of(obj) == SIMPLE_FUN_HEADER_WIDETAG) {
250             print((long)addr | FUN_POINTER_LOWTAG);
251         } else if (lowtag_of(obj) == OTHER_IMMEDIATE_0_LOWTAG ||
252                    lowtag_of(obj) == OTHER_IMMEDIATE_1_LOWTAG) {
253             print((lispobj)addr | OTHER_POINTER_LOWTAG);
254         } else {
255             print((lispobj)addr);
256         } if (count == -1) {
257             return;
258         }
259     }
260 }
261
262 static void
263 call_cmd(char **ptr)
264 {
265     lispobj thing = parse_lispobj(ptr), function, result = 0, cons, args[3];
266     int numargs;
267
268     if (lowtag_of(thing) == OTHER_POINTER_LOWTAG) {
269         switch (widetag_of(*(lispobj *)(thing-OTHER_POINTER_LOWTAG))) {
270           case SYMBOL_HEADER_WIDETAG:
271             for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
272                  cons != NIL;
273                  cons = CONS(cons)->cdr) {
274                 if (FDEFN(CONS(cons)->car)->name == thing) {
275                     thing = CONS(cons)->car;
276                     goto fdefn;
277                 }
278             }
279             printf("Symbol 0x%08lx is undefined.\n", (long unsigned)thing);
280             return;
281
282           case FDEFN_WIDETAG:
283           fdefn:
284             function = FDEFN(thing)->fun;
285             if (function == NIL) {
286                 printf("Fdefn 0x%08lx is undefined.\n", (long unsigned)thing);
287                 return;
288             }
289             break;
290           default:
291             printf("0x%08lx is not a function pointer, symbol, "
292                    "or fdefn object.\n",
293                    (long unsigned)thing);
294             return;
295         }
296     }
297     else if (lowtag_of(thing) != FUN_POINTER_LOWTAG) {
298         printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
299                (long unsigned)thing);
300         return;
301     }
302     else
303         function = thing;
304
305     numargs = 0;
306     while (more_p(ptr)) {
307         if (numargs >= 3) {
308             printf("too many arguments (no more than 3 supported)\n");
309             return;
310         }
311         args[numargs++] = parse_lispobj(ptr);
312     }
313
314     switch (numargs) {
315     case 0:
316         result = funcall0(function);
317         break;
318     case 1:
319         result = funcall1(function, args[0]);
320         break;
321     case 2:
322         result = funcall2(function, args[0], args[1]);
323         break;
324     case 3:
325         result = funcall3(function, args[0], args[1], args[2]);
326         break;
327     default:
328         lose("unsupported arg count made it past validity check?!");
329     }
330
331     print(result);
332 }
333
334 static void
335 flush_cmd(char **ptr)
336 {
337     flush_vars();
338 }
339
340 static void
341 quit_cmd(char **ptr)
342 {
343     char buf[10];
344
345     printf("Really quit? [y] ");
346     fflush(stdout);
347     fgets(buf, sizeof(buf), ldb_in);
348     if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
349         exit(0);
350 }
351
352 static void
353 help_cmd(char **ptr)
354 {
355     struct cmd *cmd;
356
357     for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
358         if (cmd->help != NULL)
359             printf("%s\t%s\n", cmd->cmd, cmd->help);
360 }
361
362 static int done;
363
364 static void
365 exit_cmd(char **ptr)
366 {
367     done = 1;
368 }
369
370 static void
371 purify_cmd(char **ptr)
372 {
373     purify(NIL, NIL);
374 }
375
376 static void
377 print_context(os_context_t *context)
378 {
379         int i;
380
381         for (i = 0; i < NREGS; i++) {
382                 printf("%s:\t", lisp_register_names[i]);
383 #ifdef __i386__
384                 brief_print((lispobj)(*os_context_register_addr(context,
385                                                                 i*2)));
386 #else
387                 brief_print((lispobj)(*os_context_register_addr(context,i)));
388 #endif
389         }
390         printf("PC:\t\t  0x%08lx\n",
391                (unsigned long)(*os_context_pc_addr(context)));
392 }
393
394 static void
395 print_context_cmd(char **ptr)
396 {
397         int free;
398
399         free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
400         
401         if (more_p(ptr)) {
402                 int index;
403
404                 index = parse_number(ptr);
405
406                 if ((index >= 0) && (index < free)) {
407                         printf("There are %d interrupt contexts.\n", free);
408                         printf("printing context %d\n", index);
409                         print_context(lisp_interrupt_contexts[index]);
410                 } else {
411                         printf("There aren't that many/few contexts.\n");
412                         printf("There are %d interrupt contexts.\n", free);
413                 }
414         } else {
415                 if (free == 0)
416                         printf("There are no interrupt contexts!\n");
417                 else {
418                         printf("There are %d interrupt contexts.\n", free);
419                         printf("printing context %d\n", free - 1);
420                         print_context(lisp_interrupt_contexts[free - 1]);
421                 }
422         }
423 }
424
425 static void
426 backtrace_cmd(char **ptr)
427 {
428     void backtrace(int frames);
429     int n;
430
431     if (more_p(ptr))
432         n = parse_number(ptr);
433     else
434         n = 100;
435
436     printf("Backtrace:\n");
437     backtrace(n);
438 }
439
440 static void
441 catchers_cmd(char **ptr)
442 {
443     struct catch_block *catch;
444
445     catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
446
447     if (catch == NULL)
448         printf("There are no active catchers!\n");
449     else {
450         while (catch != NULL) {
451 #ifndef __i386__
452             printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
453                    (unsigned long)catch, (unsigned long)(catch->current_uwp),
454                    (unsigned long)(catch->current_cont),
455                    catch->current_code,
456                    catch->entry_pc);
457 #else
458             printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
459                    (unsigned long)catch, (unsigned long)(catch->current_uwp),
460                    (unsigned long)(catch->current_cont),
461                    (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
462                    OTHER_POINTER_LOWTAG,
463                    (unsigned long)catch->entry_pc);
464 #endif
465             brief_print((lispobj)catch->tag);
466             catch = catch->previous_catch;
467         }
468     }
469 }
470
471 static void
472 grab_sigs_cmd(char **ptr)
473 {
474     extern void sigint_init(void);
475
476     printf("Grabbing signals.\n");
477     sigint_init();
478 }
479
480 static void
481 sub_monitor(void)
482 {
483     struct cmd *cmd, *found;
484     char buf[256];
485     char *line, *ptr, *token;
486     int ambig;
487
488     if (!ldb_in) {
489         ldb_in = fopen("/dev/tty","r+");
490         ldb_in_fd = fileno(ldb_in);
491     }
492
493     while (!done) {
494         printf("ldb> ");
495         fflush(stdout);
496         line = fgets(buf, sizeof(buf), ldb_in);
497         if (line == NULL) {
498             if (isatty(ldb_in_fd)) {
499                 putchar('\n');
500                 continue;
501             }
502             else {
503                 fprintf(stderr, "\nEOF on something other than a tty.\n");
504                 exit(0);
505             }
506         }
507         ptr = line;
508         if ((token = parse_token(&ptr)) == NULL)
509             continue;
510         ambig = 0;
511         found = NULL;
512         for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
513             if (strcmp(token, cmd->cmd) == 0) {
514                 found = cmd;
515                 ambig = 0;
516                 break;
517             }
518             else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
519                 if (found)
520                     ambig = 1;
521                 else
522                     found = cmd;
523             }
524         }
525         if (ambig)
526             printf("``%s'' is ambiguous.\n", token);
527         else if (found == NULL)
528             printf("unknown command: ``%s''\n", token);
529         else {
530             reset_printer();
531             (*found->fn)(&ptr);
532         }
533     }
534 }
535
536 void
537 ldb_monitor()
538 {
539     jmp_buf oldbuf;
540
541     bcopy(curbuf, oldbuf, sizeof(oldbuf));
542
543     printf("LDB monitor\n");
544
545     setjmp(curbuf);
546
547     sub_monitor();
548
549     done = 0;
550
551     bcopy(oldbuf, curbuf, sizeof(curbuf));
552 }
553
554 void
555 throw_to_monitor()
556 {
557     longjmp(curbuf, 1);
558 }
559
560 #endif /* defined(LISP_FEATURE_SB_LDB) */
561
562 /* what we do when things go badly wrong at a low level */
563 void
564 monitor_or_something()
565 {
566 #if defined(LISP_FEATURE_SB_LDB)
567     ldb_monitor();
568 #else
569     fprintf(stderr, "There's no LDB in this build; exiting.\n");
570     exit(1);
571 #endif
572 }