1 | /* |
---|
2 | * print_dump |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1994 by Division Incorporated |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * http://www.OARcorp.com/rtems/license.html. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #define GETOPTARGS "v" |
---|
14 | |
---|
15 | char *USAGE = "\ |
---|
16 | usage: print_dump [ -v ] \n\ |
---|
17 | -v -- verbose\n\ |
---|
18 | Reads HP simulator 'memdump' output of 'print_buffer' structure |
---|
19 | on stdin. Dumps it out in vanilla ASCII. |
---|
20 | "; |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <fcntl.h> |
---|
24 | #include <ctype.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <string.h> |
---|
28 | #include <memory.h> |
---|
29 | #include <stdarg.h> |
---|
30 | |
---|
31 | #include "config.h" |
---|
32 | |
---|
33 | #ifndef VMS |
---|
34 | #ifndef HAVE_STRERROR |
---|
35 | #if defined(__linux__) && (__GLIBC__ < 2) |
---|
36 | extern char *sys_errlist[]; |
---|
37 | #endif |
---|
38 | |
---|
39 | #define strerror( _err ) \ |
---|
40 | ((_err) < sys_nerr) ? sys_errlist [(_err)] : "unknown error" |
---|
41 | |
---|
42 | #else /* HAVE_STRERROR */ |
---|
43 | char *strerror (); |
---|
44 | #endif |
---|
45 | #else /* VMS */ |
---|
46 | char *strerror (int,...); |
---|
47 | #endif |
---|
48 | |
---|
49 | #define Failed(x) (((int) (x)) == -1) |
---|
50 | #define TRUE 1 |
---|
51 | #define FALSE 0 |
---|
52 | #define STREQ(a,b) (strcmp(a,b) == 0) |
---|
53 | #define NUMELEMS(arr) (sizeof(arr) / sizeof(arr[0])) |
---|
54 | |
---|
55 | /* |
---|
56 | * Definitions for unsigned "ints"; especially for use in data structures |
---|
57 | * that will be shared among (potentially) different cpu's (we punt on |
---|
58 | * byte ordering problems tho) |
---|
59 | */ |
---|
60 | |
---|
61 | typedef unsigned char u8; |
---|
62 | typedef unsigned short u16; |
---|
63 | typedef unsigned int u32; |
---|
64 | |
---|
65 | typedef union uval { |
---|
66 | u8 uv_chars[4]; |
---|
67 | u16 uv_words[2]; |
---|
68 | u32 uv_long; |
---|
69 | void *uv_ptr[sizeof(long) / sizeof(void *)]; |
---|
70 | } uval_t; |
---|
71 | |
---|
72 | |
---|
73 | /* |
---|
74 | * vars controlled by command line options |
---|
75 | */ |
---|
76 | |
---|
77 | int verbose = FALSE; /* be verbose */ |
---|
78 | |
---|
79 | extern char *optarg; /* getopt(3) control vars */ |
---|
80 | extern int optind, opterr; |
---|
81 | extern int errno; |
---|
82 | |
---|
83 | char *progname; /* for error() */ |
---|
84 | |
---|
85 | void error(int errn, ...); |
---|
86 | |
---|
87 | #define ERR_ERRNO (1<<((sizeof(int) * 8) - 2)) /* hi bit; use 'errno' */ |
---|
88 | #define ERR_FATAL (ERR_ERRNO / 2) /* error is fatal; no return */ |
---|
89 | #define ERR_ABORT (ERR_ERRNO / 4) /* error is fatal; abort */ |
---|
90 | #define ERR_MASK (ERR_ERRNO | ERR_FATAL | ERR_ABORT) /* all */ |
---|
91 | |
---|
92 | int process(void); |
---|
93 | void prchar(unsigned int ch); |
---|
94 | |
---|
95 | |
---|
96 | int |
---|
97 | main(int argc, char **argv, char **env) |
---|
98 | { |
---|
99 | register int c; |
---|
100 | int showusage = FALSE; /* usage error? */ |
---|
101 | |
---|
102 | /* |
---|
103 | * figure out invocation leaf-name |
---|
104 | */ |
---|
105 | |
---|
106 | if ((progname = strrchr(argv[0], '/')) == (char *) NULL) |
---|
107 | progname = argv[0]; |
---|
108 | else |
---|
109 | progname++; |
---|
110 | |
---|
111 | argv[0] = progname; /* for getopt err reporting */ |
---|
112 | |
---|
113 | /* |
---|
114 | * Check options and arguments. |
---|
115 | */ |
---|
116 | |
---|
117 | opterr = 0; /* we'll report all errors */ |
---|
118 | while ((c = getopt(argc, argv, GETOPTARGS)) != EOF) |
---|
119 | switch (c) |
---|
120 | { |
---|
121 | case 'v': /* toggle verbose */ |
---|
122 | verbose = ! verbose; |
---|
123 | break; |
---|
124 | |
---|
125 | case '?': |
---|
126 | showusage = TRUE; |
---|
127 | } |
---|
128 | |
---|
129 | if (showusage) |
---|
130 | { |
---|
131 | (void) fprintf(stderr, "%s", USAGE); |
---|
132 | exit(1); |
---|
133 | } |
---|
134 | |
---|
135 | return process(); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | /* |
---|
140 | * process(arg) |
---|
141 | * |
---|
142 | * Input looks like this |
---|
143 | * |
---|
144 | * Starting address: 00000001.480035a0 |
---|
145 | * ----------------------------------- |
---|
146 | |
---|
147 | +0000 / 0d0a0d0a 2a2a2a20 53454d20 54455354 202d2d20 4e4f4445 2032202a 2a2a0d0a |
---|
148 | +0020 / 73703a20 30783433 30303030 31300d0a 30783438 30613161 38383a20 676f7420 |
---|
149 | .... |
---|
150 | +0b40 / xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx |
---|
151 | |
---|
152 | * |
---|
153 | * The 'xxxxxxxxx' shows up if the page has not been allocated. |
---|
154 | */ |
---|
155 | |
---|
156 | int |
---|
157 | process(void) |
---|
158 | { |
---|
159 | uval_t b[8]; |
---|
160 | u32 ignore; |
---|
161 | char *p; |
---|
162 | int i; |
---|
163 | int failed_once; |
---|
164 | |
---|
165 | char line[1024]; |
---|
166 | |
---|
167 | #define PRINT_BUFFER_SIZE (16 * 1024) |
---|
168 | struct { |
---|
169 | int index; |
---|
170 | int size; |
---|
171 | u8 buffer[PRINT_BUFFER_SIZE]; |
---|
172 | } print_buffer; |
---|
173 | |
---|
174 | /* we stuff the data into print_buffer using memcpy() */ |
---|
175 | p = (char *) &print_buffer; |
---|
176 | |
---|
177 | failed_once = 0; |
---|
178 | |
---|
179 | while (gets(line)) |
---|
180 | { |
---|
181 | char *cp; |
---|
182 | |
---|
183 | /* hack; deal with the 'xxxxxxxx' problem noted above */ |
---|
184 | for (cp=line; *cp; cp++) |
---|
185 | if (*cp == 'x') |
---|
186 | *cp = '0'; |
---|
187 | |
---|
188 | if (*line != '+') |
---|
189 | continue; |
---|
190 | if (sscanf(line, "+%x / %x %x %x %x %x %x %x %x\n", |
---|
191 | &ignore, |
---|
192 | &b[0].uv_long, |
---|
193 | &b[1].uv_long, |
---|
194 | &b[2].uv_long, |
---|
195 | &b[3].uv_long, |
---|
196 | &b[4].uv_long, |
---|
197 | &b[5].uv_long, |
---|
198 | &b[6].uv_long, |
---|
199 | &b[7].uv_long) != 9) |
---|
200 | { |
---|
201 | if (failed_once) |
---|
202 | error(ERR_FATAL, "2nd format problem; giving up"); |
---|
203 | error(0, "format problem in line: `%s`", line); |
---|
204 | failed_once = 1; |
---|
205 | } |
---|
206 | |
---|
207 | memcpy((void *) p, (void *) b, sizeof(b)); |
---|
208 | p += sizeof(b); |
---|
209 | } |
---|
210 | |
---|
211 | if (verbose) |
---|
212 | printf("buffer size: %d\n", print_buffer.size); |
---|
213 | |
---|
214 | if (print_buffer.size < 0) |
---|
215 | error(ERR_FATAL, "size is too small"); |
---|
216 | |
---|
217 | if (print_buffer.size != sizeof(print_buffer.buffer)) |
---|
218 | { |
---|
219 | error(ERR_FATAL, "buffer size mismatch, expected %d", |
---|
220 | sizeof(print_buffer.buffer)); |
---|
221 | /* XXX we really should just dynamically allocate the buffer */ |
---|
222 | } |
---|
223 | |
---|
224 | i = print_buffer.index + 1; |
---|
225 | while (i != print_buffer.index) |
---|
226 | { |
---|
227 | unsigned int c; |
---|
228 | c = print_buffer.buffer[i++]; |
---|
229 | if (c && (c != '\r')) |
---|
230 | prchar(c); |
---|
231 | i %= print_buffer.size; |
---|
232 | } |
---|
233 | printf("\n"); |
---|
234 | return 0; |
---|
235 | } |
---|
236 | |
---|
237 | /* de-controlify */ |
---|
238 | char *de_control[] = { |
---|
239 | "^@", "^A", "^B", "^C", "^D", "^E", "^F", "^G", "^H", "^I", "^J", "^K", |
---|
240 | "^L", "^M", "^N", "^O", "^P", "^Q", "^R", "^S", "^T", "^U", "^V", "^W", |
---|
241 | "^X", "^Y", "^Z", "^[", "^\\", "^]", "^~", "^_", |
---|
242 | " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", |
---|
243 | ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", |
---|
244 | "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", |
---|
245 | "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", |
---|
246 | "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", |
---|
247 | "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", |
---|
248 | "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "^?", |
---|
249 | "M-^@", "M-^A", "M-^B", "M-^C", "M-^D", "M-^E", "M-^F", "M-^G", |
---|
250 | "M-^H", "M-^I", "M-^J", "M-^K", "M-^L", "M-^M", "M-^N", "M-^O", |
---|
251 | "M-^P", "M-^Q", "M-^R", "M-^S", "M-^T", "M-^U", "M-^V", "M-^W", |
---|
252 | "M-^X", "M-^Y", "M-^Z", "M-^[", "M-^\\", "M-^]", "M-^~", "M-^_", |
---|
253 | "M- ", "M-!", "M-\"", "M-#", "M-$", "M-%", "M-&", "M-'", |
---|
254 | "M-(", "M-)", "M-*", "M-+", "M-,", "M--", "M-.", "M-/", |
---|
255 | "M-0", "M-1", "M-2", "M-3", "M-4", "M-5", "M-6", "M-7", |
---|
256 | "M-8", "M-9", "M-:", "M-;", "M-<", "M-=", "M->", "M-?", |
---|
257 | "M-@", "M-A", "M-B", "M-C", "M-D", "M-E", "M-F", "M-G", |
---|
258 | "M-H", "M-I", "M-J", "M-K", "M-L", "M-M", "M-N", "M-O", |
---|
259 | "M-P", "M-Q", "M-R", "M-S", "M-T", "M-U", "M-V", "M-W", |
---|
260 | "M-X", "M-Y", "M-Z", "M-[", "M-\\", "M-]", "M-^", "M-_", |
---|
261 | "M-`", "M-a", "M-b", "M-c", "M-d", "M-e", "M-f", "M-g", |
---|
262 | "M-h", "M-i", "M-j", "M-k", "M-l", "M-m", "M-n", "M-o", |
---|
263 | "M-p", "M-q", "M-r", "M-s", "M-t", "M-u", "M-v", "M-w", |
---|
264 | "M-x", "M-y", "M-z", "M-{", "M-|", "M-}", "M-~", "M-^?" |
---|
265 | }; |
---|
266 | |
---|
267 | /* |
---|
268 | * prchar(ch); print ch in a readable format, ie ^X or X or ~^X or DEL, etc. |
---|
269 | */ |
---|
270 | |
---|
271 | void |
---|
272 | prchar(unsigned int ch) |
---|
273 | { |
---|
274 | if (isprint(ch) || isspace(ch)) |
---|
275 | putchar(ch); |
---|
276 | else |
---|
277 | printf("%s", de_control[ch]); |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | /* |
---|
282 | * error(errn, arglist) |
---|
283 | * report an error to stderr using printf(3) conventions. |
---|
284 | * Any output is preceded by '<progname>: ' |
---|
285 | * |
---|
286 | * Uses ERR_EXIT bit to request exit(errn) |
---|
287 | * ERR_ABORT to request abort() |
---|
288 | * ERR_ERRNO to indicate use of errno instead of argument. |
---|
289 | * |
---|
290 | * If resulting 'errn' is non-zero, it is assumed to be an 'errno' and its |
---|
291 | * associated error message is appended to the output. |
---|
292 | */ |
---|
293 | |
---|
294 | /*VARARGS*/ |
---|
295 | |
---|
296 | void |
---|
297 | error(int error_flag, ...) |
---|
298 | { |
---|
299 | va_list arglist; |
---|
300 | register char *format; |
---|
301 | int local_errno; |
---|
302 | |
---|
303 | extern int errno; |
---|
304 | |
---|
305 | (void) fflush(stdout); /* in case stdout/stderr same */ |
---|
306 | |
---|
307 | local_errno = error_flag & ~ERR_MASK; |
---|
308 | if (error_flag & ERR_ERRNO) /* use errno? */ |
---|
309 | local_errno = errno; |
---|
310 | |
---|
311 | va_start(arglist, error_flag); |
---|
312 | format = va_arg(arglist, char *); |
---|
313 | (void) fprintf(stderr, "%s: ", progname); |
---|
314 | (void) vfprintf(stderr, format, arglist); |
---|
315 | va_end(arglist); |
---|
316 | |
---|
317 | if (local_errno) |
---|
318 | (void) fprintf(stderr, " (%s)\n", strerror(local_errno)); |
---|
319 | else |
---|
320 | (void) fprintf(stderr, "\n"); |
---|
321 | |
---|
322 | (void) fflush(stderr); |
---|
323 | |
---|
324 | if (error_flag & (ERR_FATAL | ERR_ABORT)) |
---|
325 | { |
---|
326 | if (error_flag & ERR_FATAL) |
---|
327 | { |
---|
328 | error(0, local_errno ? "fatal error, exiting" : "exiting"); |
---|
329 | exit(local_errno); |
---|
330 | } |
---|
331 | else |
---|
332 | { |
---|
333 | error(0, "fatal error, aborting"); |
---|
334 | abort(); |
---|
335 | } |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|