Try to make the :lurking-threads test more robust.
[sbcl.git] / src / runtime / monitor.c
blob382ee701fc609e8bd838d76578dadab23b0f524a
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
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.
12 #include "sbcl.h"
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>
25 #include "runtime.h"
26 #include "parse.h"
27 #include "vars.h"
29 #include "print.h"
30 #include "arch.h"
31 #include "interr.h"
32 #include "search.h"
33 #include "purify.h"
34 #include "globals.h"
35 #include "lispregs.h"
36 #include "interrupt.h"
37 #include "thread.h"
38 #include "genesis/static-symbols.h"
39 #include "genesis/primitive-objects.h"
43 /* When we need to do command input, we use this stream, which is not
44 * in general stdin, so that things will "work" (as well as being
45 * thrown into ldb can be considered "working":-) even in a process
46 * where standard input has been redirected to a file or pipe.
48 * (We could set up output to go to a special ldb_out stream for the
49 * same reason, but there's been no pressure for that so far.)
51 * The enter-the-ldb-monitor function is responsible for setting up
52 * this stream. */
53 static FILE *ldb_in = 0;
54 static int ldb_in_fd = -1;
56 typedef void cmd(char **ptr);
58 static cmd dump_cmd, print_cmd, quit_cmd, help_cmd;
59 static cmd flush_cmd, search_cmd, regs_cmd, exit_cmd;
60 static cmd print_context_cmd;
61 static cmd backtrace_cmd, purify_cmd, catchers_cmd;
62 static cmd grab_sigs_cmd;
63 static cmd kill_cmd;
65 static struct cmd {
66 char *cmd, *help;
67 void (*fn)(char **ptr);
68 } supported_cmds[] = {
69 {"help", "Display this help information.", help_cmd},
70 {"?", "(an alias for help)", help_cmd},
71 {"backtrace", "Backtrace up to N frames.", backtrace_cmd},
72 {"catchers", "Print a list of all the active catchers.", catchers_cmd},
73 {"context", "Print interrupt context number I.", print_context_cmd},
74 {"dump", "Dump memory starting at ADDRESS for COUNT words.", dump_cmd},
75 {"d", "(an alias for dump)", dump_cmd},
76 {"exit", "Exit this instance of the monitor.", exit_cmd},
77 {"flush", "Flush all temp variables.", flush_cmd},
78 /* (Classic CMU CL had a "gc" command here, which seems like a
79 * reasonable idea, but the code was stale (incompatible with
80 * gencgc) so I just flushed it. -- WHN 20000814 */
81 {"grab-signals", "Set the signal handlers to call LDB.", grab_sigs_cmd},
82 {"kill", "Kill ourself with signal number N (useful if running under gdb)",
83 kill_cmd},
84 {"purify", "Purify. (Caveat purifier!)", purify_cmd},
85 {"print", "Print object at ADDRESS.", print_cmd},
86 {"p", "(an alias for print)", print_cmd},
87 {"quit", "Quit.", quit_cmd},
88 {"regs", "Display current Lisp registers.", regs_cmd},
89 {"search", "Search for TYPE starting at ADDRESS for a max of COUNT words.", search_cmd},
90 {"s", "(an alias for search)", search_cmd},
91 {NULL, NULL, NULL}
94 static jmp_buf curbuf;
96 static int
97 visible(unsigned char c)
99 if (c < ' ' || c > '~')
100 return ' ';
101 else
102 return c;
105 static void
106 dump_cmd(char **ptr)
108 static char *lastaddr = 0;
109 static int lastcount = 20;
111 char *addr = lastaddr;
112 int count = lastcount, displacement;
113 int force = 0;
115 if (more_p(ptr)) {
116 if (!strncmp(*ptr, "-f ", 3)) {
117 force = 1;
118 *ptr += 3;
120 addr = parse_addr(ptr, !force);
122 if (more_p(ptr))
123 count = parse_number(ptr);
126 if (count == 0) {
127 printf("COUNT must be non-zero.\n");
128 return;
131 lastcount = count;
133 if (count > 0)
134 displacement = N_WORD_BYTES;
135 else {
136 displacement = -N_WORD_BYTES;
137 count = -count;
140 while (count-- > 0) {
141 #ifndef LISP_FEATURE_ALPHA
142 printf("%p: ", (os_vm_address_t) addr);
143 #else
144 printf("0x%08X: ", (u32) addr);
145 #endif
146 if (force || gc_managed_addr_p((lispobj)addr)) {
147 #ifndef LISP_FEATURE_ALPHA
148 unsigned long *lptr = (unsigned long *)addr;
149 #else
150 u32 *lptr = (u32 *)addr;
151 #endif
152 unsigned char *cptr = (unsigned char *)addr;
154 #if N_WORD_BYTES == 8
155 printf("0x%016lx | %c%c%c%c%c%c%c%c\n",
156 lptr[0],
157 visible(cptr[0]), visible(cptr[1]),
158 visible(cptr[2]), visible(cptr[3]),
159 visible(cptr[4]), visible(cptr[5]),
160 visible(cptr[6]), visible(cptr[7]));
161 #else
162 unsigned short *sptr = (unsigned short *)addr;
163 printf("0x%08lx 0x%04x 0x%04x "
164 "0x%02x 0x%02x 0x%02x 0x%02x "
165 "%c%c"
166 "%c%c\n",
167 lptr[0], sptr[0], sptr[1],
168 cptr[0], cptr[1], cptr[2], cptr[3],
169 visible(cptr[0]), visible(cptr[1]),
170 visible(cptr[2]), visible(cptr[3]));
171 #endif
173 else
174 printf("invalid Lisp-level address\n");
176 addr += displacement;
179 lastaddr = addr;
182 static void
183 print_cmd(char **ptr)
185 lispobj obj = parse_lispobj(ptr);
187 print(obj);
190 static void
191 kill_cmd(char **ptr)
193 #ifndef LISP_FEATURE_WIN32
194 kill(getpid(), parse_number(ptr));
195 #endif
198 static void
199 regs_cmd(char **ptr)
201 struct thread __attribute__((unused)) *thread=arch_os_get_current_thread();
203 printf("CSP\t=\t%p ", access_control_stack_pointer(thread));
204 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
205 printf("CFP\t=\t%p ", access_control_frame_pointer(thread));
206 #endif
208 #ifdef reg_BSP
209 printf("BSP\t=\t%p\n", get_binding_stack_pointer(thread));
210 #else
211 /* printf("BSP\t=\t%p\n", (void*)SymbolValue(BINDING_STACK_POINTER)); */
212 printf("\n");
213 #endif
215 #ifdef LISP_FEATURE_GENCGC
216 /* printf("DYNAMIC\t=\t%p\n", (void*)DYNAMIC_SPACE_START); */
217 #else
218 printf("STATIC\t=\t%p ", static_space_free_pointer);
219 printf("RDONLY\t=\t%p ", read_only_space_free_pointer);
220 printf("DYNAMIC\t=\t%p\n", (void*)current_dynamic_space);
221 #endif
223 #ifndef ALLOCATION_POINTER
224 printf("ALLOC\t=\t%p\n", (void*)dynamic_space_free_pointer);
225 #else
226 printf("ALLOC\t=\t%p\n", (void*)SymbolValue(ALLOCATION_POINTER, thread));
227 #endif
229 #ifndef LISP_FEATURE_GENCGC
230 printf("TRIGGER\t=\t%p\n", (void*)current_auto_gc_trigger);
231 #endif
234 static void
235 search_cmd(char **ptr)
237 static int lastval = 0, lastcount = 0;
238 static lispobj *start = 0, *end = 0;
239 int val, count;
240 lispobj *addr, obj;
242 if (more_p(ptr)) {
243 val = parse_number(ptr);
244 if (val < 0 || val > 0xff) {
245 printf("can only search for single bytes\n");
246 return;
248 if (more_p(ptr)) {
249 addr = native_pointer((uword_t)parse_addr(ptr, 1));
250 if (more_p(ptr)) {
251 count = parse_number(ptr);
253 else {
254 /* Specified value and address, but no count. Only one. */
255 count = -1;
258 else {
259 /* Specified a value, but no address, so search same range. */
260 addr = start;
261 count = lastcount;
264 else {
265 /* Specified nothing, search again for val. */
266 val = lastval;
267 addr = end;
268 count = lastcount;
271 lastval = val;
272 start = end = addr;
273 lastcount = count;
275 printf("searching for 0x%x at %p\n", val, (void*)(uword_t)end);
277 while (search_for_type(val, &end, &count)) {
278 printf("found 0x%x at %p:\n", val, (void*)(uword_t)end);
279 obj = *end;
280 addr = end;
281 end += 2;
282 if (widetag_of(obj) == SIMPLE_FUN_WIDETAG) {
283 print((uword_t)addr | FUN_POINTER_LOWTAG);
284 } else if (other_immediate_lowtag_p(obj)) {
285 print((lispobj)addr | OTHER_POINTER_LOWTAG);
286 } else {
287 print((lispobj)addr);
288 } if (count == -1) {
289 return;
294 /* (There used to be call_cmd() here, to call known-at-cold-init-time
295 * Lisp functions from ldb, but it bitrotted and was deleted in
296 * sbcl-0.7.5.1. See older CVS versions if you want to resuscitate
297 * it.) */
299 static void
300 flush_cmd(char **ptr)
302 flush_vars();
305 static void
306 quit_cmd(char **ptr)
308 char buf[10];
310 printf("Really quit? [y] ");
311 fflush(stdout);
312 if (fgets(buf, sizeof(buf), ldb_in)) {
313 if (buf[0] == 'y' || buf[0] == 'Y' || buf[0] == '\n')
314 exit(1);
315 } else {
316 printf("\nUnable to read response, assuming y.\n");
317 exit(1);
321 static void
322 help_cmd(char **ptr)
324 struct cmd *cmd;
326 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++)
327 if (cmd->help != NULL)
328 printf("%s\t%s\n", cmd->cmd, cmd->help);
331 static int done;
333 static void
334 exit_cmd(char **ptr)
336 done = 1;
339 static void
340 purify_cmd(char **ptr)
342 purify(NIL, NIL);
345 static void
346 print_context(os_context_t *context)
348 int i;
350 for (i = 0; i < NREGS; i++) {
351 printf("%s:\t", lisp_register_names[i]);
352 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
353 brief_print((lispobj)(*os_context_register_addr(context, i*2)));
354 #else
355 brief_print((lispobj)(*os_context_register_addr(context,i)));
356 #endif
358 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_PPC)
359 printf("DAR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context, 41)));
360 printf("DSISR:\t\t 0x%08lx\n", (unsigned long)(*os_context_register_addr(context, 42)));
361 #endif
362 printf("PC:\t\t 0x%08lx\n",
363 (unsigned long)(*os_context_pc_addr(context)));
366 static void
367 print_context_cmd(char **ptr)
369 int free_ici;
370 struct thread *thread=arch_os_get_current_thread();
372 free_ici = fixnum_value(read_TLS(FREE_INTERRUPT_CONTEXT_INDEX,thread));
374 if (more_p(ptr)) {
375 int index;
377 index = parse_number(ptr);
379 if ((index >= 0) && (index < free_ici)) {
380 printf("There are %d interrupt contexts.\n", free_ici);
381 printf("printing context %d\n", index);
382 print_context(thread->interrupt_contexts[index]);
383 } else {
384 printf("There aren't that many/few contexts.\n");
385 printf("There are %d interrupt contexts.\n", free_ici);
387 } else {
388 if (free_ici == 0)
389 printf("There are no interrupt contexts!\n");
390 else {
391 printf("There are %d interrupt contexts.\n", free_ici);
392 printf("printing context %d\n", free_ici - 1);
393 print_context(thread->interrupt_contexts[free_ici - 1]);
398 static void
399 backtrace_cmd(char **ptr)
401 void lisp_backtrace(int frames);
402 int n;
404 if (more_p(ptr))
405 n = parse_number(ptr);
406 else
407 n = 100;
409 printf("Backtrace:\n");
410 lisp_backtrace(n);
413 static void
414 catchers_cmd(char **ptr)
416 struct catch_block *catch = (struct catch_block *)
417 read_TLS(CURRENT_CATCH_BLOCK, arch_os_get_current_thread());
419 if (catch == NULL)
420 printf("There are no active catchers!\n");
421 else {
422 while (catch != NULL) {
423 printf("0x%08lX:\n\tuwp: 0x%08lX\n\tfp: 0x%08lX\n\t"
424 "code: 0x%08lX\n\tentry: 0x%08lX\n\ttag: ",
425 (long unsigned)catch,
426 (long unsigned)(catch->uwp),
427 (long unsigned)(catch->cfp),
428 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
429 (long unsigned)component_ptr_from_pc((void*)catch->entry_pc)
430 + OTHER_POINTER_LOWTAG,
431 #else
432 (long unsigned)(catch->code),
433 #endif
434 (long unsigned)(catch->entry_pc));
435 brief_print((lispobj)catch->tag);
436 catch = catch->previous_catch;
441 static void
442 grab_sigs_cmd(char **ptr)
444 extern void sigint_init(void);
446 printf("Grabbing signals.\n");
447 sigint_init();
450 static void
451 sub_monitor(void)
453 struct cmd *cmd, *found;
454 char buf[256];
455 char *line, *ptr, *token;
456 int ambig;
458 if (!ldb_in) {
459 #ifndef LISP_FEATURE_WIN32
460 ldb_in = fopen("/dev/tty","r+");
461 if (ldb_in == NULL) {
462 perror("Error opening /dev/tty");
463 ldb_in = stdin;
465 #else
466 ldb_in = stdin;
467 #endif
468 ldb_in_fd = fileno(ldb_in);
471 while (!done) {
472 printf("ldb> ");
473 fflush(stdout);
474 line = fgets(buf, sizeof(buf), ldb_in);
475 if (line == NULL) {
476 exit(1);
478 ptr = line;
479 if ((token = parse_token(&ptr)) == NULL)
480 continue;
481 ambig = 0;
482 found = NULL;
483 for (cmd = supported_cmds; cmd->cmd != NULL; cmd++) {
484 if (strcmp(token, cmd->cmd) == 0) {
485 found = cmd;
486 ambig = 0;
487 break;
489 else if (strncmp(token, cmd->cmd, strlen(token)) == 0) {
490 if (found)
491 ambig = 1;
492 else
493 found = cmd;
496 if (ambig)
497 printf("``%s'' is ambiguous.\n", token);
498 else if (found == NULL)
499 printf("unknown command: ``%s''\n", token);
500 else {
501 reset_printer();
502 (*found->fn)(&ptr);
507 void
508 ldb_monitor()
510 jmp_buf oldbuf;
512 bcopy(curbuf, oldbuf, sizeof(oldbuf));
514 printf("Welcome to LDB, a low-level debugger for the Lisp runtime environment.\n");
516 setjmp(curbuf);
518 sub_monitor();
520 done = 0;
522 bcopy(oldbuf, curbuf, sizeof(curbuf));
525 void
526 throw_to_monitor()
528 longjmp(curbuf, 1);
531 /* what we do when things go badly wrong at a low level */
532 void
533 monitor_or_something()
535 ldb_monitor();