ffefafac23fd326cac09622f312ab2832321a903
[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 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     {"catchers", "Print a list of all the active catchers.", catchers_cmd},
67     {"context", "Print interrupt context number I.", print_context_cmd},
68     {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
69     {"d", "(an alias for dump)", dump_cmd},
70     {"exit", "Exit this instance of the monitor.", exit_cmd},
71     {"flush", "Flush all temp variables.", flush_cmd},
72     /* (Classic CMU CL had a "gc" command here, which seems like a
73      * reasonable idea, but the code was stale (incompatible with
74      * gencgc) so I just flushed it. -- WHN 20000814 */
75     {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
76     {"kill", "Kill ourself with signal number N (useful if running under gdb)",
77      kill_cmd},
78     {"purify", "Purify. (Caveat purifier!)", purify_cmd},
79     {"print", "Print object at ADDRESS.", print_cmd},
80     {"p", "(an alias for print)", print_cmd},
81     {"quit", "Quit.", quit_cmd},
82     {"regs", "Display current Lisp registers.", regs_cmd},
83     {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
84     {"s", "(an alias for search)", search_cmd},
85     {NULL, NULL, NULL}
86 };
87
88 static jmp_buf curbuf;
89
90 static int
91 visible(unsigned char c)
92 {
93     if (c < ' ' || c > '~')
94         return ' ';
95     else
96         return c;
97 }
98
99 static void
100 dump_cmd(char **ptr)
101 {
102     static char *lastaddr = 0;
103     static int lastcount = 20;
104
105     char *addr = lastaddr;
106     int count = lastcount, displacement;
107
108     if (more_p(ptr)) {
109         addr = parse_addr(ptr);
110
111         if (more_p(ptr))
112             count = parse_number(ptr);
113     }
114
115     if (count == 0) {
116         printf("COUNT must be non-zero.\n");
117         return;
118     }
119
120     lastcount = count;
121
122     if (count > 0)
123         displacement = 4;
124     else {
125         displacement = -4;
126         count = -count;
127     }
128
129     while (count-- > 0) {
130 #ifndef alpha
131         printf("0x%08lX: ", (unsigned long) addr);
132 #else
133         printf("0x%08X: ", (u32) addr);
134 #endif
135         if (is_valid_lisp_addr((os_vm_address_t)addr)) {
136 #ifndef alpha
137             unsigned long *lptr = (unsigned long *)addr;
138 #else
139             u32 *lptr = (u32 *)addr;
140 #endif
141             unsigned short *sptr = (unsigned short *)addr;
142             unsigned char *cptr = (unsigned char *)addr;
143
144             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]));
145         }
146         else
147             printf("invalid Lisp-level address\n");
148
149         addr += displacement;
150     }
151
152     lastaddr = addr;
153 }
154
155 static void
156 print_cmd(char **ptr)
157 {
158     lispobj obj = parse_lispobj(ptr);
159
160     print(obj);
161 }
162
163 static void
164 kill_cmd(char **ptr)
165 {
166     kill(getpid(), parse_number(ptr));
167 }
168
169 static void
170 regs_cmd(char **ptr)
171 {
172     printf("CSP\t=\t0x%08lX\n", (unsigned long)current_control_stack_pointer);
173     printf("FP\t=\t0x%08lX\n", (unsigned long)current_control_frame_pointer);
174 #if !defined(__i386__)
175     printf("BSP\t=\t0x%08X\n", (unsigned long)current_binding_stack_pointer);
176 #endif
177 #ifdef __i386__
178     printf("BSP\t=\t0x%08lx\n",
179            (unsigned long)SymbolValue(BINDING_STACK_POINTER));
180 #endif
181
182     printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)DYNAMIC_SPACE_START);
183 #if defined(__i386__)
184     printf("ALLOC\t=\t0x%08lx\n",
185            (unsigned long)SymbolValue(ALLOCATION_POINTER));
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 /* (There used to be call_cmd() here, to call known-at-cold-init-time
263  * Lisp functions from ldb, but it bitrotted and was deleted in
264  * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
265  * it.) */
266
267 static void
268 flush_cmd(char **ptr)
269 {
270     flush_vars();
271 }
272
273 static void
274 quit_cmd(char **ptr)
275 {
276     char buf[10];
277
278     printf("Really quit? [y] ");
279     fflush(stdout);
280     fgets(buf, sizeof(buf), ldb_in);
281     if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
282         exit(0);
283 }
284
285 static void
286 help_cmd(char **ptr)
287 {
288     struct cmd *cmd;
289
290     for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
291         if (cmd->help != NULL)
292             printf("%s\t%s\n", cmd->cmd, cmd->help);
293 }
294
295 static int done;
296
297 static void
298 exit_cmd(char **ptr)
299 {
300     done = 1;
301 }
302
303 static void
304 purify_cmd(char **ptr)
305 {
306     purify(NIL, NIL);
307 }
308
309 static void
310 print_context(os_context_t *context)
311 {
312     int i;
313
314     for (i = 0; i < NREGS; i++) {
315         printf("%s:\t", lisp_register_names[i]);
316 #ifdef __i386__
317         brief_print((lispobj)(*os_context_register_addr(context,
318                                                         i*2)));
319 #else
320         brief_print((lispobj)(*os_context_register_addr(context,i)));
321 #endif
322     }
323     printf("PC:\t\t  0x%08lx\n",
324            (unsigned long)(*os_context_pc_addr(context)));
325 }
326
327 static void
328 print_context_cmd(char **ptr)
329 {
330     int free;
331
332     free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
333         
334     if (more_p(ptr)) {
335         int index;
336
337         index = parse_number(ptr);
338
339         if ((index >= 0) && (index < free)) {
340             printf("There are %d interrupt contexts.\n", free);
341             printf("printing context %d\n", index);
342             print_context(lisp_interrupt_contexts[index]);
343         } else {
344             printf("There aren't that many/few contexts.\n");
345             printf("There are %d interrupt contexts.\n", free);
346         }
347     } else {
348         if (free == 0)
349             printf("There are no interrupt contexts!\n");
350         else {
351             printf("There are %d interrupt contexts.\n", free);
352             printf("printing context %d\n", free - 1);
353             print_context(lisp_interrupt_contexts[free - 1]);
354         }
355     }
356 }
357
358 static void
359 backtrace_cmd(char **ptr)
360 {
361     void backtrace(int frames);
362     int n;
363
364     if (more_p(ptr))
365         n = parse_number(ptr);
366     else
367         n = 100;
368
369     printf("Backtrace:\n");
370     backtrace(n);
371 }
372
373 static void
374 catchers_cmd(char **ptr)
375 {
376     struct catch_block *catch;
377
378     catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK);
379
380     if (catch == NULL)
381         printf("There are no active catchers!\n");
382     else {
383         while (catch != NULL) {
384 #ifndef __i386__
385             printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
386                    (unsigned long)catch, (unsigned long)(catch->current_uwp),
387                    (unsigned long)(catch->current_cont),
388                    catch->current_code,
389                    catch->entry_pc);
390 #else
391             printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\tcode: 0x%08lx\n\tentry: 0x%08lx\n\ttag: ",
392                    (unsigned long)catch, (unsigned long)(catch->current_uwp),
393                    (unsigned long)(catch->current_cont),
394                    (unsigned long)component_ptr_from_pc((void*)catch->entry_pc) +
395                    OTHER_POINTER_LOWTAG,
396                    (unsigned long)catch->entry_pc);
397 #endif
398             brief_print((lispobj)catch->tag);
399             catch = catch->previous_catch;
400         }
401     }
402 }
403
404 static void
405 grab_sigs_cmd(char **ptr)
406 {
407     extern void sigint_init(void);
408
409     printf("Grabbing signals.\n");
410     sigint_init();
411 }
412
413 static void
414 sub_monitor(void)
415 {
416     struct cmd *cmd, *found;
417     char buf[256];
418     char *line, *ptr, *token;
419     int ambig;
420
421     if (!ldb_in) {
422         ldb_in = fopen("/dev/tty","r+");
423         ldb_in_fd = fileno(ldb_in);
424     }
425
426     while (!done) {
427         printf("ldb> ");
428         fflush(stdout);
429         line = fgets(buf, sizeof(buf), ldb_in);
430         if (line == NULL) {
431             if (isatty(ldb_in_fd)) {
432                 putchar('\n');
433                 continue;
434             }
435             else {
436                 fprintf(stderr, "\nEOF on something other than a tty.\n");
437                 exit(0);
438             }
439         }
440         ptr = line;
441         if ((token = parse_token(&ptr)) == NULL)
442             continue;
443         ambig = 0;
444         found = NULL;
445         for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
446             if (strcmp(token, cmd->cmd) == 0) {
447                 found = cmd;
448                 ambig = 0;
449                 break;
450             }
451             else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
452                 if (found)
453                     ambig = 1;
454                 else
455                     found = cmd;
456             }
457         }
458         if (ambig)
459             printf("``%s'' is ambiguous.\n", token);
460         else if (found == NULL)
461             printf("unknown command: ``%s''\n", token);
462         else {
463             reset_printer();
464             (*found->fn)(&ptr);
465         }
466     }
467 }
468
469 void
470 ldb_monitor()
471 {
472     jmp_buf oldbuf;
473
474     bcopy(curbuf, oldbuf, sizeof(oldbuf));
475
476     printf("LDB monitor\n");
477
478     setjmp(curbuf);
479
480     sub_monitor();
481
482     done = 0;
483
484     bcopy(oldbuf, curbuf, sizeof(curbuf));
485 }
486
487 void
488 throw_to_monitor()
489 {
490     longjmp(curbuf, 1);
491 }
492
493 #endif /* defined(LISP_FEATURE_SB_LDB) */
494
495 /* what we do when things go badly wrong at a low level */
496 void
497 monitor_or_something()
498 {
499 #if defined(LISP_FEATURE_SB_LDB)
500     ldb_monitor();
501 #else
502     fprintf(stderr, "There's no LDB in this build; exiting.\n");
503     exit(1);
504 #endif
505 }