0f827240f42016d240c68f900f8a59ba55c92045
[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   "
152                    "0x%02x 0x%02x 0x%02x 0x%02x    "
153                    "%c%c"
154                    "%c%c\n",
155                    lptr[0], sptr[0], sptr[1],
156                    cptr[0], cptr[1], cptr[2], cptr[3],
157                    visible(cptr[0]), visible(cptr[1]),
158                    visible(cptr[2]), visible(cptr[3]));
159         }
160         else
161             printf("invalid Lisp-level address\n");
162
163         addr += displacement;
164     }
165
166     lastaddr = addr;
167 }
168
169 static void
170 print_cmd(char **ptr)
171 {
172     lispobj obj = parse_lispobj(ptr);
173
174     print(obj);
175 }
176
177 static void
178 kill_cmd(char **ptr)
179 {
180     kill(getpid(), parse_number(ptr));
181 }
182
183 static void
184 regs_cmd(char **ptr)
185 {
186     printf("CSP\t=\t0x%08lx\n", (unsigned long)current_control_stack_pointer);
187     printf("CFP\t=\t0x%08lx\n", (unsigned long)current_control_frame_pointer);
188
189 #ifdef reg_BSP
190     printf("BSP\t=\t0x%08lx\n", (unsigned long)current_binding_stack_pointer);
191 #else
192     /* printf("BSP\t=\t0x%08lx\n",
193            (unsigned long)SymbolValue(BINDING_STACK_POINTER)); */
194 #endif
195
196 #ifdef LISP_FEATURE_GENCGC
197     /* printf("DYNAMIC\t=\t0x%08lx\n", DYNAMIC_SPACE_START); */
198 #else
199     printf("DYNAMIC\t=\t0x%08lx\n", (unsigned long)current_dynamic_space);
200 #endif
201
202 #ifdef reg_ALLOC
203     printf("ALLOC\t=\t0x%08lx\n", (unsigned long)dynamic_space_free_pointer);
204 #else
205     printf("ALLOC\t=\t0x%08lx\n",
206            (unsigned long)SymbolValue(ALLOCATION_POINTER));
207 #endif
208
209 #ifndef LISP_FEATURE_GENCGC
210     printf("TRIGGER\t=\t0x%08lx\n", (unsigned long)current_auto_gc_trigger);
211 #endif
212
213 #if 0
214     printf("STATIC\t=\t0x%08lx\n",
215            (unsigned long)SymbolValue(STATIC_SPACE_FREE_POINTER));
216     printf("RDONLY\t=\t0x%08lx\n",
217            (unsigned long)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
218 #endif
219 }
220
221 static void
222 search_cmd(char **ptr)
223 {
224     static int lastval = 0, lastcount = 0;
225     static lispobj *start = 0, *end = 0;
226     int val, count;
227     lispobj *addr, obj;
228
229     if (more_p(ptr)) {
230         val = parse_number(ptr);
231         if (val < 0 || val > 0xff) {
232             printf("can only search for single bytes\n");
233             return;
234         }
235         if (more_p(ptr)) {
236             addr = (lispobj *)native_pointer((long)parse_addr(ptr));
237             if (more_p(ptr)) {
238                 count = parse_number(ptr);
239             }
240             else {
241                 /* Specified value and address, but no count. Only one. */
242                 count = -1;
243             }
244         }
245         else {
246             /* Specified a value, but no address, so search same range. */
247             addr = start;
248             count = lastcount;
249         }
250     }
251     else {
252         /* Specified nothing, search again for val. */
253         val = lastval;
254         addr = end;
255         count = lastcount;
256     }
257
258     lastval = val;
259     start = end = addr;
260     lastcount = count;
261
262     printf("searching for 0x%x at 0x%08lX\n", val, (unsigned long)end);
263
264     while (search_for_type(val, &end, &count)) {
265         printf("found 0x%x at 0x%08lX:\n", val, (unsigned long)end);
266         obj = *end;
267         addr = end;
268         end += 2;
269         if (widetag_of(obj) == SIMPLE_FUN_HEADER_WIDETAG) {
270             print((long)addr | FUN_POINTER_LOWTAG);
271         } else if (lowtag_of(obj) == OTHER_IMMEDIATE_0_LOWTAG ||
272                    lowtag_of(obj) == OTHER_IMMEDIATE_1_LOWTAG) {
273             print((lispobj)addr | OTHER_POINTER_LOWTAG);
274         } else {
275             print((lispobj)addr);
276         } if (count == -1) {
277             return;
278         }
279     }
280 }
281
282 /* (There used to be call_cmd() here, to call known-at-cold-init-time
283  * Lisp functions from ldb, but it bitrotted and was deleted in
284  * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
285  * it.) */
286
287 static void
288 flush_cmd(char **ptr)
289 {
290     flush_vars();
291 }
292
293 static void
294 quit_cmd(char **ptr)
295 {
296     char buf[10];
297
298     printf("Really quit? [y] ");
299     fflush(stdout);
300     fgets(buf, sizeof(buf), ldb_in);
301     if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
302         exit(1);
303 }
304
305 static void
306 help_cmd(char **ptr)
307 {
308     struct cmd *cmd;
309
310     for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
311         if (cmd->help != NULL)
312             printf("%s\t%s\n", cmd->cmd, cmd->help);
313 }
314
315 static int done;
316
317 static void
318 exit_cmd(char **ptr)
319 {
320     done = 1;
321 }
322
323 static void
324 purify_cmd(char **ptr)
325 {
326     purify(NIL, NIL);
327 }
328
329 static void
330 print_context(os_context_t *context)
331 {
332     int i;
333
334     for (i = 0; i < NREGS; i++) {
335         printf("%s:\t", lisp_register_names[i]);
336 #ifdef LISP_FEATURE_X86
337         brief_print((lispobj)(*os_context_register_addr(context,
338                                                         i*2)));
339 #else
340         brief_print((lispobj)(*os_context_register_addr(context,i)));
341 #endif
342     }
343 #ifdef LISP_FEATURE_DARWIN
344     printf("DAR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context, 41)));
345     printf("DSISR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context, 42)));
346 #endif
347     printf("PC:\t\t  0x%08lx\n",
348            (unsigned long)(*os_context_pc_addr(context)));
349 }
350
351 static void
352 print_context_cmd(char **ptr)
353 {
354     int free;
355     struct thread *thread=arch_os_get_current_thread();
356
357     free = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)>>2;
358
359     if (more_p(ptr)) {
360         int index;
361
362         index = parse_number(ptr);
363
364         if ((index >= 0) && (index < free)) {
365             printf("There are %d interrupt contexts.\n", free);
366             printf("printing context %d\n", index);
367             print_context(thread->interrupt_contexts[index]);
368         } else {
369             printf("There aren't that many/few contexts.\n");
370             printf("There are %d interrupt contexts.\n", free);
371         }
372     } else {
373         if (free == 0)
374             printf("There are no interrupt contexts!\n");
375         else {
376             printf("There are %d interrupt contexts.\n", free);
377             printf("printing context %d\n", free - 1);
378             print_context(thread->interrupt_contexts[free - 1]);
379         }
380     }
381 }
382
383 static void
384 backtrace_cmd(char **ptr)
385 {
386     void backtrace(int frames);
387     int n;
388
389     if (more_p(ptr))
390         n = parse_number(ptr);
391     else
392         n = 100;
393
394     printf("Backtrace:\n");
395     backtrace(n);
396 }
397
398 static void
399 catchers_cmd(char **ptr)
400 {
401     struct catch_block *catch;
402     struct thread *thread=arch_os_get_current_thread();
403
404     catch = (struct catch_block *)SymbolValue(CURRENT_CATCH_BLOCK,thread);
405
406     if (catch == NULL)
407         printf("There are no active catchers!\n");
408     else {
409         while (catch != NULL) {
410             printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\t"
411                    "code: 0x%08lX\n\tentry: 0x%08lX\n\ttag: ",
412                    (unsigned long)catch,
413                    (unsigned long)(catch->current_uwp),
414                    (unsigned long)(catch->current_cont),
415 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
416                    (unsigned long)component_ptr_from_pc((void*)catch->entry_pc)
417                        + OTHER_POINTER_LOWTAG,
418 #else
419                    (unsigned long)(catch->current_code),
420 #endif
421                    (unsigned long)(catch->entry_pc));
422             brief_print((lispobj)catch->tag);
423             catch = catch->previous_catch;
424         }
425     }
426 }
427
428 static void
429 grab_sigs_cmd(char **ptr)
430 {
431     extern void sigint_init(void);
432
433     printf("Grabbing signals.\n");
434     sigint_init();
435 }
436
437 static void
438 sub_monitor(void)
439 {
440     struct cmd *cmd, *found;
441     char buf[256];
442     char *line, *ptr, *token;
443     int ambig;
444
445     if (!ldb_in) {
446         ldb_in = fopen("/dev/tty","r+");
447         ldb_in_fd = fileno(ldb_in);
448     }
449
450     while (!done) {
451         printf("ldb> ");
452         fflush(stdout);
453         line = fgets(buf, sizeof(buf), ldb_in);
454         if (line == NULL) {
455             if (isatty(ldb_in_fd)) {
456                 putchar('\n');
457                 continue;
458             }
459             else {
460                 fprintf(stderr, "\nEOF on something other than a tty.\n");
461                 exit(0);
462             }
463         }
464         ptr = line;
465         if ((token = parse_token(&ptr)) == NULL)
466             continue;
467         ambig = 0;
468         found = NULL;
469         for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
470             if (strcmp(token, cmd->cmd) == 0) {
471                 found = cmd;
472                 ambig = 0;
473                 break;
474             }
475             else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
476                 if (found)
477                     ambig = 1;
478                 else
479                     found = cmd;
480             }
481         }
482         if (ambig)
483             printf("``%s'' is ambiguous.\n", token);
484         else if (found == NULL)
485             printf("unknown command: ``%s''\n", token);
486         else {
487             reset_printer();
488             (*found->fn)(&ptr);
489         }
490     }
491 }
492
493 void
494 ldb_monitor()
495 {
496     jmp_buf oldbuf;
497
498     bcopy(curbuf, oldbuf, sizeof(oldbuf));
499
500     printf("LDB monitor\n");
501
502     setjmp(curbuf);
503
504     sub_monitor();
505
506     done = 0;
507
508     bcopy(oldbuf, curbuf, sizeof(curbuf));
509 }
510
511 void
512 throw_to_monitor()
513 {
514     longjmp(curbuf, 1);
515 }
516
517 #endif /* defined(LISP_FEATURE_SB_LDB) */
518
519 /* what we do when things go badly wrong at a low level */
520 void
521 monitor_or_something()
522 {
523 #if defined(LISP_FEATURE_SB_LDB)
524     ldb_monitor();
525 #else
526      fprintf(stderr,
527 "The system is too badly corrupted or confused to continue at the Lisp\n\
528 level. If the system had been compiled with the SB-LDB feature, we'd drop\n\
529 into the LDB low-level debugger now. But there's no LDB in this build, so\n\
530 we can't really do anything but just exit, sorry.\n");
531     exit(1);
532 #endif
533 }