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