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