cbd92872a5073b4632214666d022512f58fe7f8a
[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 #include "globals.h"
24 #include "vars.h"
25 #include "parse.h"
26 #include "os.h"
27 #include "interrupt.h"
28 #include "lispregs.h"
29 #include "monitor.h"
30 #include "print.h"
31 #include "arch.h"
32 #include "interr.h"
33 #include "gc.h"
34 #include "search.h"
35 #include "purify.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 *)PTR((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 (TypeOf(obj) == type_FunctionHeader)
253             print((long)addr | type_FunctionPointer);
254         else if (LowtagOf(obj) == type_OtherImmediate0 || LowtagOf(obj) == type_OtherImmediate1)
255             print((lispobj)addr | type_OtherPointer);
256         else
257             print((lispobj)addr);
258         if (count == -1)
259             return;
260     }
261 }
262
263 static void
264 call_cmd(char **ptr)
265 {
266     lispobj thing = parse_lispobj(ptr), function, result = 0, cons, args[3];
267     int numargs;
268
269     if (LowtagOf(thing) == type_OtherPointer) {
270         switch (TypeOf(*(lispobj *)(thing-type_OtherPointer))) {
271           case type_SymbolHeader:
272             for (cons = SymbolValue(INITIAL_FDEFN_OBJECTS);
273                  cons != NIL;
274                  cons = CONS(cons)->cdr) {
275                 if (FDEFN(CONS(cons)->car)->name == thing) {
276                     thing = CONS(cons)->car;
277                     goto fdefn;
278                 }
279             }
280             printf("Symbol 0x%08lx is undefined.\n", (long unsigned)thing);
281             return;
282
283           case type_Fdefn:
284           fdefn:
285             function = FDEFN(thing)->function;
286             if (function == NIL) {
287                 printf("Fdefn 0x%08lx is undefined.\n", (long unsigned)thing);
288                 return;
289             }
290             break;
291           default:
292             printf("0x%08lx is not a function pointer, symbol, "
293                    "or fdefn object.\n",
294                    (long unsigned)thing);
295             return;
296         }
297     }
298     else if (LowtagOf(thing) != type_FunctionPointer) {
299         printf("0x%08lx is not a function pointer, symbol, or fdefn object.\n",
300                (long unsigned)thing);
301         return;
302     }
303     else
304         function = thing;
305
306     numargs = 0;
307     while (more_p(ptr)) {
308         if (numargs >= 3) {
309             printf("too many arguments (no more than 3 supported)\n");
310             return;
311         }
312         args[numargs++] = parse_lispobj(ptr);
313     }
314
315     switch (numargs) {
316     case 0:
317         result = funcall0(function);
318         break;
319     case 1:
320         result = funcall1(function, args[0]);
321         break;
322     case 2:
323         result = funcall2(function, args[0], args[1]);
324         break;
325     case 3:
326         result = funcall3(function, args[0], args[1], args[2]);
327         break;
328     default:
329         lose("unsupported arg count made it past validity check?!");
330     }
331
332     print(result);
333 }
334
335 static void
336 flush_cmd(char **ptr)
337 {
338     flush_vars();
339 }
340
341 static void
342 quit_cmd(char **ptr)
343 {
344     char buf[10];
345
346     printf("Really quit? [y] ");
347     fflush(stdout);
348     fgets(buf, sizeof(buf), ldb_in);
349     if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
350         exit(0);
351 }
352
353 static void
354 help_cmd(char **ptr)
355 {
356     struct cmd *cmd;
357
358     for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
359         if (cmd->help != NULL)
360             printf("%s\t%s\n", cmd->cmd, cmd->help);
361 }
362
363 static int done;
364
365 static void
366 exit_cmd(char **ptr)
367 {
368     done = 1;
369 }
370
371 static void
372 purify_cmd(char **ptr)
373 {
374     purify(NIL, NIL);
375 }
376
377 static void
378 print_context(os_context_t *context)
379 {
380         int i;
381
382         for (i = 0; i < NREGS; i++) {
383                 printf("%s:\t", lisp_register_names[i]);
384 #ifdef __i386__
385                 brief_print((lispobj)(*os_context_register_addr(context,
386                                                                 i*2)));
387 #else
388                 brief_print((lispobj)(*os_context_register_addr(context,i)));
389 #endif
390         }
391         printf("PC:\t\t  0x%08lx\n",
392                (unsigned long)(*os_context_pc_addr(context)));
393 }
394
395 static void
396 print_context_cmd(char **ptr)
397 {
398         int free;
399
400         free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
401         
402         if (more_p(ptr)) {
403                 int index;
404
405                 index = parse_number(ptr);
406
407                 if ((index >= 0) && (index < free)) {
408                         printf("There are %d interrupt contexts.\n", free);
409                         printf("printing context %d\n", index);
410                         print_context(lisp_interrupt_contexts[index]);
411                 } else {
412                         printf("There aren't that many/few contexts.\n");
413                         printf("There are %d interrupt contexts.\n", free);
414                 }
415         } else {
416                 if (free == 0)
417                         printf("There are no interrupt contexts!\n");
418                 else {
419                         printf("There are %d interrupt contexts.\n", free);
420                         printf("printing context %d\n", free - 1);
421                         print_context(lisp_interrupt_contexts[free - 1]);
422                 }
423         }
424 }
425
426 static void
427 backtrace_cmd(char **ptr)
428 {
429     void backtrace(int frames);
430     int n;
431
432     if (more_p(ptr))
433         n = parse_number(ptr);
434     else
435         n = 100;
436
437     printf("Backtrace:\n");
438     backtrace(n);
439 }
440
441 static void
442 catchers_cmd(char **ptr)
443 {
444     struct catch_block *catch;
445
446     catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
447
448     if (catch == NULL)
449         printf("There are no active catchers!\n");
450     else {
451         while (catch != NULL) {
452 #ifndef __i386__
453             printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
454                    (unsigned long)catch, (unsigned long)(catch->current_uwp),
455                    (unsigned long)(catch->current_cont),
456                    catch->current_code,
457                    catch->entry_pc);
458 #else
459             printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
460                    (unsigned long)catch, (unsigned long)(catch->current_uwp),
461                    (unsigned long)(catch->current_cont),
462                    (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
463                    type_OtherPointer,
464                    (unsigned long)catch->entry_pc);
465 #endif
466             brief_print((lispobj)catch->tag);
467             catch = catch->previous_catch;
468         }
469     }
470 }
471
472 static void
473 grab_sigs_cmd(char **ptr)
474 {
475     extern void sigint_init(void);
476
477     printf("Grabbing signals.\n");
478     sigint_init();
479 }
480
481 static void
482 sub_monitor(void)
483 {
484     struct cmd *cmd, *found;
485     char buf[256];
486     char *line, *ptr, *token;
487     int ambig;
488
489     if (!ldb_in) {
490         ldb_in = fopen("/dev/tty","r+");
491         ldb_in_fd = fileno(ldb_in);
492     }
493
494     while (!done) {
495         printf("ldb> ");
496         fflush(stdout);
497         line = fgets(buf, sizeof(buf), ldb_in);
498         if (line == NULL) {
499             if (isatty(ldb_in_fd)) {
500                 putchar('\n');
501                 continue;
502             }
503             else {
504                 fprintf(stderr, "\nEOF on something other than a tty.\n");
505                 exit(0);
506             }
507         }
508         ptr = line;
509         if ((token = parse_token(&ptr)) == NULL)
510             continue;
511         ambig = 0;
512         found = NULL;
513         for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
514             if (strcmp(token, cmd->cmd) == 0) {
515                 found = cmd;
516                 ambig = 0;
517                 break;
518             }
519             else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
520                 if (found)
521                     ambig = 1;
522                 else
523                     found = cmd;
524             }
525         }
526         if (ambig)
527             printf("``%s'' is ambiguous.\n", token);
528         else if (found == NULL)
529             printf("unknown command: ``%s''\n", token);
530         else {
531             reset_printer();
532             (*found->fn)(&ptr);
533         }
534     }
535 }
536
537 void
538 ldb_monitor()
539 {
540     jmp_buf oldbuf;
541
542     bcopy(curbuf, oldbuf, sizeof(oldbuf));
543
544     printf("LDB monitor\n");
545
546     setjmp(curbuf);
547
548     sub_monitor();
549
550     done = 0;
551
552     bcopy(oldbuf, curbuf, sizeof(curbuf));
553 }
554
555 void
556 throw_to_monitor()
557 {
558     longjmp(curbuf, 1);
559 }