source: trunk/minix/commands/ibm/sdump.c@ 9

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

Minix 3.1.2a

File size: 5.1 KB
Line 
1/* sdump - dump memory */
2
3#include <minix/config.h>
4#include <sys/types.h>
5#include <minix/const.h>
6#include <minix/type.h>
7#include <minix/ipc.h>
8#include <timers.h>
9#include <signal.h>
10
11#undef EXTERN
12#define EXTERN
13#include "../../servers/pm/const.h"
14#include "../../servers/pm/type.h"
15#include "../../servers/pm/mproc.h"
16#include "../../kernel/const.h"
17#include "../../kernel/type.h"
18#include "../../kernel/proc.h"
19#undef printf /* printf was misdefined by the sys headers */
20
21#include <ctype.h>
22#include <fcntl.h>
23#include <limits.h>
24#include <stdarg.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdio.h>
29
30#define STACK_BYTES 30000
31
32char *default_core = "core";
33int stack[STACK_BYTES / sizeof (int)];
34
35_PROTOTYPE(int main, (int argc, char **argv));
36_PROTOTYPE(void read_segmap, (int fd, struct mproc *mp, long *seg_size));
37_PROTOTYPE(void read_registers, (int fd, struct proc *p));
38_PROTOTYPE(void dump_stack, (int fd, struct mproc *mp, long *seg_size));
39_PROTOTYPE(void error, (char *s1, char *s2));
40
41int main(argc, argv)
42int argc;
43char *argv[];
44{
45 int fd;
46 long seg_size[NR_LOCAL_SEGS]; /* segment sizes in bytes */
47 struct mproc *mp;
48 struct proc *p;
49 char *file;
50
51 if (argc > 2) error("Usage: rdump [core_file]\n", "");
52
53 if (argc == 1)
54 file = default_core;
55 else
56 file = argv[1];
57
58 if ( (fd = open(file, O_RDONLY)) < 0) error("Cannot open", file);
59
60 mp = &mproc[0];
61 p = &proc[0];
62 read_segmap(fd, mp, seg_size);
63 read_registers(fd, p);
64 dump_stack(fd, mp, seg_size);
65 return(0);
66}
67
68void read_segmap(fd, mp, seg_size)
69int fd;
70struct mproc *mp;
71long seg_size[NR_LOCAL_SEGS];
72{
73 int i, segmap_size;
74
75 /* Read in the segment map. */
76 segmap_size = sizeof mp->mp_seg;
77 if (read(fd, (char *) mp, segmap_size) != segmap_size)
78 error("Cannot read segmap map from core image file", "");
79
80 for (i = 0; i < NR_LOCAL_SEGS; i++)
81 seg_size[i] = (long) mp->mp_seg[i].mem_len << CLICK_SHIFT;
82 printf("Seg sizes in bytes: Text: %ld Data: %ld, Stack: %ld\n",
83 seg_size[T], seg_size[D], seg_size[S]);
84}
85
86void read_registers(fd, p)
87int fd;
88struct proc *p;
89{
90 int proctblsize;
91 struct stackframe_s r;
92
93 proctblsize = sizeof (struct proc);
94 if (read(fd, (char *) p, proctblsize) != proctblsize)
95 error("Cannot read process table from core image file", "");
96 r = p->p_reg;
97
98 /* Print proc table. */
99 printf("\n");
100#if (CHIP == INTEL)
101#if _WORD_SIZE == 4
102 printf("eax=%8lX ebx=%8lX ecx=%8lX edx=%8lX\n",
103 r.retreg, r.bx, r.cx, r.dx);
104 printf("esi=%8lX edi=%8lX ebp=%8lX esp=%8lX eip=%8lX\n",
105 r.si, r.di, r.fp, r.sp, r.pc);
106 printf(" ds=%8lX es=%8lX ss=%8lX cs=%8lX\n",
107 r.ds, r.es, r.ss, r.cs);
108 printf(" fs=%8lX gs=%8lX ef=%8lX\n",
109 r.fs, r.gs, r.psw);
110#else
111 printf(
112"ax=%4X bx=%4X cx=%4X dx=%4X si=%4X di=%4X bp=%4X sp=%4X ip=%4X\n",
113 r.retreg, r.bx, r.cx, r.dx, r.si, r.di, r.fp, r.sp, r.pc);
114 printf(
115" f=%4X ds=%4X es=%4X ss=%4X cs=%4X\n",
116 r.psw, r.ds, r.es, r.ss, r.cs);
117#endif
118#endif /* (CHIP == INTEL) */
119#if (CHIP == M68000)
120 printf("pc=%8lx psw=%4x\n", r.pc, r.psw);
121 printf("d0=%8lx d1=%8lx d2=%8lx d3=%8lx\n", r.retreg, r.d1, r.d2, r.d3);
122 printf("d4=%8lx d5=%8lx d6=%8lx d7=%8lx\n", r.d4, r.d5, r.d6, r.d7);
123 printf("a0=%8lx a1=%8lx a2=%8lx a3=%8lx\n", r.a0, r.a1, r.a2, r.a3);
124 printf("a4=%8lx a5=%8lx a6=%8lx a7=%8lx\n", r.a4, r.a5, r.a6, r.sp);
125#endif
126 printf("\n");
127}
128
129void dump_stack(fd, mp, seg_size)
130int fd;
131struct mproc *mp;
132long seg_size[NR_LOCAL_SEGS];
133{
134 unsigned char ch;
135 char format[32];
136 int word, i, stack_size, j;
137 vir_bytes v, vi;
138
139 /* Seek past text and data segments. */
140 lseek(fd, seg_size[T] + seg_size[D], SEEK_CUR);
141 v = mp->mp_seg[S].mem_vir << CLICK_SHIFT;
142
143 stack_size = (int) seg_size[S];
144 if (stack_size != seg_size[S] || stack_size < 0 || stack_size > STACK_BYTES)
145 error("Stack too large", "");
146
147 /* Dump the stack. */
148 if (read(fd, (char *) stack, stack_size) != stack_size)
149 error("Error reading stack from core file", "");
150
151#define BYTES(num) ((unsigned) sizeof (num))
152#define DEC_DIGITS(num) (sizeof (num) <= 2 ? 6 : 11) /* for 16/32 bit num */
153#define HEX_DIGITS(num) (((unsigned) sizeof (num) * CHAR_BIT + 3) / 4)
154
155 printf("%*s %*s %*s %*s\n",
156 HEX_DIGITS(vi), "Addr",
157 HEX_DIGITS(word), "Hex",
158 DEC_DIGITS(word), "Dec",
159 BYTES(word), "Char");
160
161 /* The format string depends messily on the size of various things. */
162 strcpy(format, "%*");
163 if (sizeof v > sizeof (int)) strcat(format, "l");
164 strcat(format, "X: %*");
165 if (sizeof word > sizeof (int)) strcat(format, "l");
166 strcat(format, "X %*");
167 if (sizeof word > sizeof (int)) strcat(format, "l");
168 strcat(format, "d ");
169
170 for (i = stack_size / sizeof (int) - 1, vi = v + stack_size - sizeof (int);
171 i >= 0; --i, vi -= sizeof (int)) {
172 word = stack[i];
173 printf(format,
174 HEX_DIGITS(vi), vi,
175 HEX_DIGITS(word), word,
176 DEC_DIGITS(word), word);
177 for (j = 0; j < BYTES(word); ++j, word >>= CHAR_BIT) {
178 ch = (unsigned char) word;
179 if (!isprint(ch)) ch = '.';
180 putchar(ch);
181 }
182 putchar('\n');
183 }
184}
185
186void error(s1, s2)
187char *s1, *s2;
188{
189 printf("%s %s\n", s1, s2);
190 exit(1);
191}
Note: See TracBrowser for help on using the repository browser.