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