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