source: trunk/minix/commands/ibm/postmort.c@ 21

Last change on this file since 21 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 16.7 KB
Line 
1/* postmort - post mortem dump Author: C. W. Rose */
2
3/* Postmort: perform post-mortem on PC Minix 1.7 core files.
4 *
5 */
6
7 /* The 1.5 core file structure is a struct mem_map, the segment memory map,
8 * followed by a struct proc, the process table, followed by a dump of the
9 * text, data, and stack segments.
10 *
11 * This is the 8086/Intel version; 386 and 68K will differ. It defaults to
12 * using the name 'core' for the core file, and 'a.out' for the symbol file.
13 * If there is no 'a.out', it will try and read the symbol table from
14 * 'symbol.out', then give up. A non-existant symbol table is not a fatal
15 * error unless the -s option was used.
16 *
17 * The PC 1.5 kernel dump routines are odd - they dump the memory maps twice,
18 * the second time as part of the kernel process table, and the kernel
19 * process table size must be a multiple of 4. Should a core file have a
20 * header with a magic number in future?
21 *
22 * The kernel include file paths need to be edited for each machine. */
23
24#include <sys/types.h>
25#include <minix/config.h>
26#include <minix/const.h>
27#include <minix/type.h>
28#include <minix/ipc.h>
29#include <limits.h>
30#include <timers.h>
31#include <signal.h>
32#include <stdlib.h>
33
34#undef EXTERN /* <minix/const.h> defined this */
35#define EXTERN /* so we get proc & mproc */
36#include "../../kernel/const.h"
37#include "../../kernel/type.h"
38#include "../../kernel/proc.h"
39#undef printf /* kernel's const.h defined this */
40#include "../../servers/pm/mproc.h"
41
42#include <a.out.h>
43#include <ctype.h>
44#include <errno.h>
45#include <fcntl.h>
46#include <stdio.h>
47#undef NULL
48#include <string.h>
49#include <unistd.h>
50
51#define FALSE 0
52#undef TRUE
53#define TRUE ~FALSE
54#define OK 1
55#define FAILED -1
56
57#define CORE "core"
58#define AOUT "a.out"
59#define SYMB "symbol.out"
60#define LINE_LEN 16
61#define MAXSYM 200
62#define SYMLEN 8
63
64/* Global variables */
65int opt_c = FALSE; /* name of core file */
66int opt_d = FALSE; /* dump raw data and stack segments */
67int opt_p = FALSE; /* dump the kernel process table */
68int opt_s = FALSE; /* name of symbol file */
69int opt_t = FALSE; /* trace back the stack */
70int opt_x = FALSE; /* debugging flag */
71
72char progname[20]; /* program name */
73char *segment_name[] = { /* array of segment names */
74 "Text",
75 "Data",
76 "Stack"
77};
78
79int dbglvl = 0; /* debugging level */
80int maxsym; /* maximum symbol number */
81unsigned int baseptr; /* reference copy of stack base pointer */
82unsigned int stackptr; /* reference copy of stack pointer */
83long int lengths[NR_LOCAL_SEGS]; /* segment lengths */
84long int bases[NR_LOCAL_SEGS]; /* segment base addresses */
85
86struct sym { /* symbol table addresses and labels */
87 unsigned int addr;
88 char label[SYMLEN + 1];
89} symtab[MAXSYM];
90
91/* Used by getopt(3) package */
92extern int optind, opterr, optopt;
93extern char *optarg;
94
95_PROTOTYPE(int binary, (int uc, char *sp));
96_PROTOTYPE(void dump_all_segs, (int fd));
97_PROTOTYPE(void dump_maps, (struct mem_map * mp));
98_PROTOTYPE(void dump_one_seg, (int fd, int segindex));
99_PROTOTYPE(void dump_proc_table, (struct proc * pt));
100_PROTOTYPE(void dump_registers, (struct proc * pt));
101_PROTOTYPE(void dump_sym_tab, (struct sym *st));
102_PROTOTYPE(void dump_stack, (struct stackframe_s * sp));
103_PROTOTYPE(int main, (int argc, char *argv[]));
104_PROTOTYPE(int parse_line, (char *ps));
105_PROTOTYPE(int read_symbol, (int fd));
106_PROTOTYPE(void stack_trace, (int fd));
107_PROTOTYPE(void usage, (void));
108
109
110/* B i n a r y
111 *
112 * Produce a binary representation of an 8-bit number.
113 */
114int binary(ucc, sp)
115int ucc;
116char *sp;
117{
118 int j;
119 unsigned char k, uc;
120
121 uc = (unsigned char) ucc;
122 for (k = 0x80, j = 0; j < 8; j++) {
123 if ((uc & k) == 0)
124 *sp++ = '0';
125 else
126 *sp++ = '1';
127 if (j == 3) *sp++ = '$';
128 k >>= 1;
129 }
130 *sp = '\0';
131
132 return(0);
133}
134
135
136/* D u m p _ a l l _ s e g s
137 *
138 * Dump all the segments except for text
139 */
140void dump_all_segs(fd)
141int fd;
142{
143 int j;
144 long int start;
145
146 start = (long) (NR_LOCAL_SEGS * sizeof(struct mem_map)) + sizeof(struct proc);
147 for (j = 1; j < NR_LOCAL_SEGS; j++) {
148 start += lengths[j - 1];
149 (void) lseek(fd, start, 0);
150 printf("\n");
151 dump_one_seg(fd, j);
152 }
153}
154
155
156/* D u m p _ m a p s
157 *
158 * Dump the memory maps
159 */
160void dump_maps(mp)
161struct mem_map *mp;
162{
163 int j;
164 long int vir, phy, len;
165
166 printf("\t Virtual\t Physical\tLength\n");
167 printf("\t address\t address\n");
168 for (j = 0; j < NR_LOCAL_SEGS; j++) {
169 vir = (long) mp[j].mem_vir << CLICK_SHIFT;
170 phy = (long) mp[j].mem_phys << CLICK_SHIFT;
171 len = (long) mp[j].mem_len << CLICK_SHIFT;
172 printf("%s:\t0x%08.8lx\t0x%08.8lx\t%8ld (0x%08.8lx)\n",
173 segment_name[j], vir, phy, len, len);
174 lengths[j] = len;
175 bases[j] = vir;
176 }
177}
178
179
180/* D u m p _ o n e _ s e g
181 *
182 * Dump a single segment
183 */
184void dump_one_seg(fd, segindex)
185int fd, segindex;
186{
187 unsigned char dlen[LINE_LEN];
188 int i, amt, amt_read;
189 long int len, offset;
190
191 printf("%s segment\n\n", segment_name[segindex]);
192 len = lengths[segindex];
193 amt = LINE_LEN;
194 for (offset = 0; offset < len; offset += amt) {
195 if ((len - offset) < LINE_LEN) amt = (int) (len - offset);
196 if (dbglvl > 0)
197 printf("Length %ld, offset %ld, amt %d\n", len, offset, amt);
198 if ((amt_read = read(fd, (char *) dlen, (unsigned int) amt)) == -1) {
199 printf("Unexpected end of file\n");
200 exit(1);
201 }
202 printf("%08.8lx: ", bases[segindex] + offset);
203 for (i = 0; i < amt_read; i++) {
204 if (i == LINE_LEN / 2) printf("- ");
205 printf("%02.2x ", dlen[i]);
206 }
207 printf(" ");
208 for (i = 0; i < amt_read; i++) {
209 if (isprint(dlen[i]))
210 (void) putchar((char) dlen[i]);
211 else
212 (void) putchar('.');
213 }
214 (void) putchar('\n');
215 if (dbglvl > 0 && amt_read != amt)
216 printf("wanted = %d, got = %d, offset = %ld\n",
217 amt, amt_read, offset);
218 }
219}
220
221
222/* D u m p _ p r o c _ t a b l e
223 *
224 * Dump the entire kernel proc table
225 */
226void dump_proc_table(pt)
227struct proc *pt;
228{
229 printf("Kernel process table entries:\n\n");
230#if 0
231 printf("Process' registers: 0x%04.4x\n", pt->p_reg); /* struct stackframe_s */
232 printf("Selector in gdt: 0x%04.4x\n", pt->p_ldt_sel); /* reg_t */
233 printf("Descriptors for code and data: 0x%04.4x\n", pt->p_ldt[2]); /* struct segdesc_s */
234#endif
235 printf("Number of this process: 0x%04.4x\n", pt->p_nr); /* int */
236#if 0
237 printf("Nonzero if blocked by busy task: 0x%04.4x\n", pt->p_ntf_blocked); /* int */
238 printf("Nonzero if held by busy syscall: 0x%04.4x\n", pt->p_ntf_held); /* int */
239 printf("Next in chain of held-up processes: 0x%04.4x\n", pt->p_ntf_nextheld); /* struct proc * */
240#endif
241 printf("SENDING, RECEIVING, etc.: 0x%04.4x\n", pt->p_rts_flags); /* int */
242#if 0
243 printf("Memory map: 0x%04.4x\n", pt->p_map[NR_LOCAL_SEGS]); /* struct mem_map */
244#endif
245#if DEAD_CODE
246 printf("Process id passed in from MM: 0x%04.4x\n", pt->p_pid); /* int */
247#endif
248#if 0
249 printf("User time in ticks: %ld\n", pt->user_time); /* time_t */
250 printf("Sys time in ticks: %ld\n", pt->sys_time); /* time_t */
251 printf("Cumulative user time of children: %ld\n", pt->child_utime); /* time_t */
252 printf("Cumulative sys time of children: %ld\n", pt->child_stime); /* time_t */
253 printf("Ticks used in current quantum: %d\n", pt->quantum_time); /* int */
254 printf("Ticks used in last quantum: %d\n", pt->quantum_last); /* int */
255 printf("Current priority of the process: %d\n", pt->curr_prio); /* int */
256 printf("Base priority of the process: %d\n", pt->base_prio); /* int */
257 printf("Scale for profiling, 0 = none: %u\n", pt->p_pscale); /* unsigned */
258 printf("Profiling pc lower boundary: %d\n", pt->p_plow); /* vir_bytes */
259 printf("Profiling pc upper boundary: %d\n", pt->p_phigh); /* vir_bytes */
260 printf("Profiling buffer: %d\n", pt->p_pbuf); /* vir_bytes */
261 printf("Profiling buffer size: %d\n", pt->p_psiz); /* vir_bytes */
262#endif
263#if 0
264 printf("First proc wishing to send: 0x%04.4x\n", pt->p_callerq); /* struct proc * */
265 printf("Link to next proc wishing to send: 0x%04.4x\n", pt->p_sendlink); /* struct proc * */
266 printf("Pointer to message buffer: 0x%04.4x\n", pt->p_messbuf); /* message * */
267#endif
268 printf("Expecting message from: 0x%04.4x\n", pt->p_getfrom_e); /* int */
269#if 0
270 printf("Pointer to next ready process: 0x%04.4x\n", pt->p_nextready); /* struct proc * */
271#endif
272 printf("Bit map for pending signals 1-16: 0x%04.4x\n", pt->p_pending); /* int */
273#if 0
274 printf("Count of pending/unfinished signals: 0x%04.4x\n", pt->p_pendcount); /* unsigned */
275#endif
276}
277
278
279/* D u m p _ r e g i s t e r s
280 *
281 * Dump the registers from the proc table
282 */
283void dump_registers(pt)
284struct proc *pt;
285{
286 char buff[32];
287 unsigned char uc;
288
289 /* Print the registers */
290 dump_stack(&pt->p_reg);
291
292 /* Build up a binary representation of the signal flags */
293 uc = (pt->p_pending >> 8) & 0xff;
294 (void) binary((int) uc, buff);
295 buff[9] = '$';
296 uc = pt->p_pending & 0xff;
297 (void) binary((int) uc, buff + 10);
298 printf("Pending signals = %s\n", buff);
299}
300
301
302/* D u m p _ s y m _ t a b
303 *
304 * Dump the symbol table
305 */
306void dump_sym_tab(st)
307struct sym *st;
308{
309 int j;
310
311 printf("Symbol table entries (text):\n\n");
312 for (j = 0; j < maxsym; j++)
313 printf("0x%08.8x T %s\n", symtab[j].addr, symtab[j].label);
314}
315
316
317/* D u m p _ s t a c k
318 *
319 * Dump the stack frame
320 */
321void dump_stack(sp)
322struct stackframe_s *sp;
323{
324 char buff[32];
325 unsigned char uc;
326
327 /* Build up the binary PSW representation */
328 uc = (sp->psw >> 8) & 0xff;
329 (void) binary((int) uc, buff);
330 uc = sp->psw & 0xff;
331 buff[9] = '$';
332 (void) binary((int) uc, buff + 10);
333
334 /* Print all the information */
335 printf("Stack Frame:\tPC = %04.4x\t\t PSW = %s\n",
336 sp->pc, buff);
337 printf("\t\t\t\t\tStatus = ____ ODIT SZ_A _P_C\n");
338
339 printf(" ax bx cx dx di si\n");
340 printf(" %04.4x\t%04.4x\t%04.4x\t%04.4x\t%04.4x\t%04.4x\n",
341 sp->retreg, sp->bx, sp->cx, sp->dx, sp->di, sp->si);
342 printf(" sp bp ss\n");
343 printf(" %04.4x\t%04.4x\t%04.4x\n",
344 sp->sp, sp->fp, sp->ss);
345 printf(" cs ds es\n");
346 printf(" %04.4x\t%04.4x\t%04.4x\n",
347 sp->cs, sp->ds, sp->es);
348
349 /* Store for future reference */
350 stackptr = sp->sp;
351 baseptr = sp->fp;
352 if (dbglvl > 0)
353 printf("\nStack pointer 0x%x, Base pointer 0x%x\n", stackptr, baseptr);
354}
355
356
357/* M a i n
358 *
359 * Main program
360 */
361main(argc, argv)
362int argc;
363char *argv[];
364{
365 int j, fdc, fds;
366 char *cp, corefile[132], symbfile[132];
367 struct proc proc_entry;
368 struct mem_map mp_segs[NR_LOCAL_SEGS];
369
370 /* Initial set up */
371 if ((cp = strrchr(argv[0], '/')) == (char *) NULL)
372 cp = argv[0];
373 else
374 cp++;
375 strncpy(progname, cp, 19);
376 strncpy(corefile, CORE, 131);
377 strncpy(symbfile, AOUT, 131);
378
379 /* Parse arguments */
380 opterr = 0;
381 while ((j = getopt(argc, argv, "c:dps:tx:")) != EOF) {
382 switch (j & 0177) {
383 case 'c':
384 opt_c = TRUE;
385 strncpy(corefile, optarg, 131);
386 break;
387 case 'd': opt_d = TRUE; break;
388 case 'p': opt_p = TRUE; break;
389 case 's':
390 opt_s = TRUE;
391 strncpy(symbfile, optarg, 131);
392 break;
393 case 't': opt_t = TRUE; break;
394 case 'x':
395 dbglvl = atoi(optarg);
396 opt_x = TRUE;
397 break;
398 case '?':
399 default:
400 usage();
401 exit(1);
402 break;
403 }
404 }
405
406 /* We must have a core file */
407 if ((fdc = open(corefile, O_RDONLY)) == -1) {
408 fprintf(stderr, "Cannot open %s\n", corefile);
409 exit(1);
410 }
411
412 /* We'd like an a.out file or a symbol table */
413 if ((fds = open(symbfile, O_RDONLY)) == -1) {
414 if (opt_s)
415 j = FAILED;
416 else {
417 strncpy(symbfile, AOUT, 131);
418 if ((fds = open(symbfile, O_RDONLY)) == -1)
419 j = FAILED;
420 else
421 j = read_symbol(fds);
422 }
423 } else
424 j = read_symbol(fds);
425
426 /* Only fatal if we insisted */
427 if (opt_s && j == FAILED) {
428 fprintf(stderr, "Cannot find symbols in %s\n", symbfile);
429 exit(1);
430 }
431
432 /* Read the process table */
433 if (dbglvl > 0) {
434 printf("\n");
435 printf("Size of mproc entry %d\n", NR_LOCAL_SEGS * sizeof(struct mem_map));
436 printf("Size of process table %d\n", sizeof(proc_entry));
437 }
438 if (read(fdc, (char *) mp_segs, sizeof(mp_segs)) != sizeof(mp_segs) ||
439 read(fdc, (char *) &proc_entry,
440 sizeof(struct proc)) != sizeof(struct proc)) {
441 fprintf(stderr, "Cannot open %s\n", corefile);
442 exit(1);
443 }
444
445 /* Do the work */
446#if 0
447 dump_maps(mp_segs); /* duplicated in the kernel */
448 printf("\n");
449 /* XXX broken */
450 dump_maps(proc_entry.p_map);
451#endif
452 printf("\n");
453 dump_registers(&proc_entry);
454 if (opt_t) {
455 printf("\n");
456 stack_trace(fdc);
457 }
458 if (opt_p) {
459 printf("\n");
460 dump_proc_table(&proc_entry);
461 }
462 if (opt_d) {
463 printf("\n");
464 dump_sym_tab(symtab);
465 dump_all_segs(fdc);
466 }
467
468 /* Wrap up */
469 (void) close(fdc);
470 if (fds != -1) (void) close(fds);
471
472 exit(0);
473 /* NOTREACHED */
474}
475
476
477/* P a r s e _ l i n e
478 *
479 * Parse a line of the symbol table
480 */
481int parse_line(ps)
482char *ps;
483{
484 char c, s[80];
485 int j, k;
486 unsigned int u;
487
488 /* We must have space in the table */
489 if (maxsym == MAXSYM) return(FAILED);
490
491 /* Lines must be a minimum length to contain information */
492 if (strlen(ps) < 8) return(FAILED);
493
494 /* Lines must have a definite structure */
495 if (ps[1] != ' ' || ps[6] != ' ') return(FAILED);
496 for (j = 2; j < 6; j++)
497 if (!isxdigit(ps[j])) return(FAILED);
498 if (sscanf(ps, "%c %x %s", &c, &u, s) != 3) return (FAILED);
499
500 if (dbglvl > 0) printf("Address 0x%04.4x, label %s\n", u, s);
501
502 /* Load the symbol table in sorted order */
503 for (j = 0; j < maxsym; j++) {
504 if (u < symtab[j].addr) {
505 for (k = maxsym; k > j; k--) symtab[k] = symtab[k - 1];
506 break;
507 }
508 }
509 symtab[j].addr = u;
510 strncpy(symtab[j].label, s, SYMLEN);
511 maxsym++;
512
513 return(OK);
514}
515
516
517/* R e a d _ s y m b o l
518 *
519 * Read the symbol table
520 */
521int read_symbol(fd)
522int fd;
523{
524 char sym[80], buff[BUFSIZ];
525 int j, k, m;
526 long int offset;
527 struct exec *ep;
528 struct nlist *np;
529
530 /* We collect only text symbols, since that's all that's needed here */
531
532 /* Initialise the buffer */
533 if ((j = read(fd, buff, BUFSIZ)) == 0 || j == -1) return(FAILED);
534
535 k = maxsym = 0;
536
537 /* Find out what we've got */
538 ep = (struct exec *) buff;
539 np = (struct nlist *) buff;
540 if (BADMAG(*ep)) {
541 /* Must be a separate symbol table */
542 while (TRUE) {
543 if (buff[k] == 'T') {
544 for (m = 0; m < 78; m++) {
545 sym[m] = buff[k];
546 if (++k == j) {
547 if ((j = read(fd, buff, BUFSIZ)) == 0 || j == -1)
548 break;
549 k = 0;
550 }
551 if (buff[k] == '\n') break;
552 }
553 sym[m + 1] = '\0';
554 (void) parse_line(sym);
555 }
556 if (++k == j) {
557 if ((j = read(fd, buff, BUFSIZ)) == 0 || j == -1)
558 break;
559 k = 0;
560 }
561 }
562 } else if (ep->a_syms != 0L) {
563 /* There's symbols in them thar hills */
564 offset = 8 * sizeof(long) + ep->a_text + ep->a_data;
565 if (lseek(fd, offset, 0) == -1L) return(FAILED);
566 /* Symbols are in an unsorted list */
567 while (read(fd, buff, sizeof(struct nlist)) == sizeof(struct nlist)) {
568 if (np->n_sclass == (N_TEXT + C_EXT)) { /* external text symbols */
569 for (j = 0; j < maxsym; j++) {
570 if (np->n_value < symtab[j].addr) {
571 for (k = maxsym; k > j; k--)
572 symtab[k] = symtab[k - 1];
573 break;
574 }
575 }
576 symtab[j].addr = np->n_value;
577 strncpy(symtab[j].label, np->n_name, SYMLEN);
578 if (maxsym++ == MAXSYM) break;
579 }
580 }
581 } else if (opt_s)
582 return(FAILED);
583
584 if (dbglvl > 0) {
585 for (m = 0; m < maxsym; m++) printf("Addr 0x%04.4x, label %s\n",
586 symtab[m].addr, symtab[m].label);
587 printf("Maxsym %d\n", maxsym);
588 }
589 return(OK);
590}
591
592
593/* S t a c k _ t r a c e
594 *
595 * Trace back down the stack frames.
596 *
597 * WARNING: very, very, non-portable code
598 */
599void stack_trace(fd)
600int fd;
601{
602 int j;
603 unsigned int framepointer, lastpointer, returnvalue, end;
604 long int offset, bp;
605
606 /* Bp actually gives the offset from the base of the data segment */
607 bp = (long) (NR_LOCAL_SEGS * sizeof(struct mem_map)) + sizeof(struct proc)
608 + lengths[0] + lengths[1] - bases[2];
609 if ((offset = lseek(fd, bp + (long int) baseptr, 0)) == -1L) return;
610 end = (bases[2] + lengths[2] - 1) & 0xffff;
611
612 if (dbglvl > 0)
613 printf("Baseptr %x, End %x, Bp %ld, Offset %ld\n", baseptr, end, bp, offset);
614
615 /* Print the header, then try to backtrace */
616 printf("Stack back trace:\n\n");
617 printf("Frame address. Contents. Return address.");
618 if (maxsym != 0) printf(" Previous label.");
619 printf("\n");
620
621 lastpointer = baseptr;
622 while (TRUE) {
623 /* Read the frame pointer and return address values */
624 if (read(fd, (char *) &framepointer, sizeof(int)) == -1 ||
625 read(fd, (char *) &returnvalue, sizeof(int)) == -1)
626 break;
627
628 /* Look up the return address - ignored if maxsym == 0 */
629 for (j = 0; j < maxsym; j++) {
630 if (symtab[j].addr >= returnvalue) break;
631 }
632 if (j > 0) j--;
633 printf(" 0x%04.4x 0x%04.4x 0x%04.4x %s\n",
634 lastpointer, framepointer, returnvalue,
635 (maxsym == 0) ? "" : symtab[j].label);
636
637 /* If the result is clearly invalid, quit */
638 if (framepointer == 0 || framepointer >= end || framepointer <= lastpointer)
639 break;
640
641 /* Otherwise try to move to the next frame base */
642 lastpointer = framepointer;
643 if ((offset = lseek(fd, bp + (long int) framepointer, 0)) == -1L || offset == 0L)
644 break;
645 }
646}
647
648
649/* U s a g e
650 *
651 * Usage message
652 */
653void usage()
654{
655 fprintf(stderr, "Usage: %s [-dpt] [-c corefile] [-s symbfile]\n", progname);
656}
Note: See TracBrowser for help on using the repository browser.