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