4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
44 static caddr_t g_file_base
; /* base address of file */
45 static char *g_cmdname
; /* name of this command */
46 static int g_raw
= B_FALSE
; /* output format */
47 static int g_status
= EXIT_SUCCESS
; /* exit status (from stdlib.h) */
48 static const char *print_unsigned
= "%u";
49 static const char *print_unsigned64
= "%llu";
51 #define OFF(p) (p - g_file_base)
53 static void describe_array (tnf_datum_t
);
54 static void describe_brief (tnf_datum_t
);
55 static void describe_record (tnf_datum_t
);
56 static void describe_struct (tnf_datum_t
);
57 static void describe_type (tnf_datum_t
);
58 static void read_tnf_file (int, char *);
59 static void usage (void);
60 static void scanargs (int, char **, int *, char ***);
63 main(int ac
, char *av
[])
65 int numfiles
; /* number of files to be printed */
66 char **filenames
; /* start of file names list */
69 /* internationalization stuff */
70 (void) setlocale(LC_ALL
, "");
71 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
72 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
74 (void) textdomain(TEXT_DOMAIN
);
77 scanargs(ac
, av
, &numfiles
, &filenames
);
78 for (i
= 0; i
< numfiles
; i
++) {
79 read_tnf_file(g_raw
, filenames
[i
]);
83 if (table_get_num_elements() > 0) {
85 print_sorted_events();
95 scanargs(int argc
, char **argv
, int *nfiles
, char ***files
)
101 while ((c
= getopt(argc
, argv
, optstr
)) != EOF
) {
107 print_unsigned
= "0x%x";
108 print_unsigned64
= "0x%llx";
115 *files
= &argv
[optind
];
116 *nfiles
= argc
- optind
;
127 read_tnf_file(int raw
, char *path
)
131 caddr_t p
, curr_p
, end_p
;
135 void (*desc_func
)(tnf_datum_t
) = describe_c_record
;
137 if ((fd
= open(path
, O_RDONLY
, 0777)) == -1) {
138 (void) fprintf(stderr
, gettext("%s: cannot open %s\n"),
140 g_status
= EXIT_FAILURE
;
143 if (fstat(fd
, &st
) != 0) {
144 (void) fprintf(stderr
, gettext("%s: fstat error on %s\n"),
147 g_status
= EXIT_FAILURE
;
150 if (st
.st_size
== 0) {
151 (void) fprintf(stderr
, gettext("%s: %s is empty\n"),
154 g_status
= EXIT_FAILURE
;
157 if ((p
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0))
159 (void) fprintf(stderr
, gettext("%s: mmap error on %s\n"),
162 g_status
= EXIT_FAILURE
;
167 g_file_base
= p
; /* for OFF() */
171 * magic word is 0 - catch the error if entire file is zero.
172 * tnf_reader_begin() will catch the "not a TNF file" error.
175 end_p
= p
+ st
.st_size
;
176 while ((curr_p
< end_p
) && (*curr_p
== 0))
178 if (curr_p
== end_p
) {
179 (void) fprintf(stderr
,
180 gettext("%s: %s is an empty TNF file\n"),
182 (void) munmap(p
, st
.st_size
);
188 if ((err
= tnf_reader_begin(p
, st
.st_size
, &tnf
)) != TNF_ERR_NONE
) {
189 (void) fprintf(stderr
, gettext("%s: error in %s: %s\n"),
190 g_cmdname
, path
, tnf_error_message(err
));
191 (void) munmap(p
, st
.st_size
);
193 g_status
= EXIT_FAILURE
;
197 /* Describe file header */
198 record
= tnf_get_file_header(tnf
);
200 describe_record(record
);
201 desc_func
= describe_record
;
204 /* Describe all other records */
205 while ((record
= tnf_get_next_record(record
)) != TNF_DATUM_NULL
)
208 /* Don't munmap for cooked output because we access records later */
210 (void) munmap(p
, st
.st_size
);
215 describe_record(tnf_datum_t datum
)
217 (void) printf("0x%-8x: {\n", OFF(tnf_get_raw(datum
)));
219 switch (tnf_get_kind(datum
)) {
222 describe_struct(datum
);
226 describe_array(datum
);
229 describe_type(datum
);
232 fail(0, gettext("illegal record at %x (%d)"),
233 tnf_get_raw(datum
), tnf_get_kind(datum
));
237 (void) printf("\t}\n");
241 describe_scalar(tnf_datum_t datum
)
243 switch (tnf_get_kind(datum
)) {
246 (void) printf("%c", tnf_get_char(datum
));
249 (void) printf("%d", tnf_get_int8(datum
));
252 (void) printf(print_unsigned
, (tnf_uint8_t
)tnf_get_int8(datum
));
255 (void) printf("%d", tnf_get_int16(datum
));
258 (void) printf(print_unsigned
,
259 (tnf_uint16_t
)tnf_get_int16(datum
));
262 (void) printf("%d", (int)tnf_get_int32(datum
));
265 if ((tnf_type_get_property(tnf_get_type(datum
), TNF_N_OPAQUE
))
268 (void) printf("0x%x",
269 (tnf_uint32_t
)tnf_get_int32(datum
));
271 (void) printf(print_unsigned
,
272 (tnf_uint32_t
)tnf_get_int32(datum
));
276 /* lint not updated, it complains: malformed format string */
277 (void) printf("%lld", tnf_get_int64(datum
));
280 if ((tnf_type_get_property(tnf_get_type(datum
), TNF_N_OPAQUE
))
282 (void) printf("0x%llx",
283 (tnf_uint64_t
)tnf_get_int64(datum
));
285 /* lint not updated, it complains: malformed format string */
286 (void) printf(print_unsigned64
,
287 (tnf_uint64_t
)tnf_get_int64(datum
));
291 (void) printf("%f", tnf_get_float32(datum
));
294 (void) printf("%f", tnf_get_float64(datum
));
297 (void) printf("unhandled scalar");
300 fail(0, gettext("not a scalar"));
306 describe_struct(tnf_datum_t datum
)
311 n
= tnf_get_slot_count(datum
);
312 for (i
= 0; i
< n
; i
++) {
313 slotname
= tnf_get_slot_name(datum
, i
);
314 (void) printf("%24s ", slotname
);
315 describe_brief(tnf_get_slot_indexed(datum
, i
));
317 /* tag_arg heuristic */
318 if ((i
== 0) && tnf_is_record(datum
)) {
321 if ((tag_arg
= tnf_get_tag_arg(datum
))
323 (void) printf("%24s ", TNF_N_TAG_ARG
);
324 describe_brief(tag_arg
);
332 describe_array(tnf_datum_t datum
)
336 describe_struct(datum
); /* XXX */
338 if (tnf_is_string(datum
))
339 (void) printf("%24s \"%s\"\n", "chars", tnf_get_chars(datum
));
341 n
= tnf_get_element_count(datum
);
342 for (i
= 0; i
< n
; i
++) {
343 (void) printf("%24d ", i
);
344 describe_brief(tnf_get_element(datum
, i
));
351 describe_type(tnf_datum_t datum
)
353 describe_struct(datum
);
357 describe_brief(tnf_datum_t datum
)
359 if (datum
== TNF_DATUM_NULL
) /* allowed */
360 (void) printf("0x%-8x <NULL>", 0);
362 else if (tnf_is_scalar(datum
))
363 describe_scalar(datum
);
365 else if (tnf_is_record(datum
)) {
367 (void) printf("0x%-8x ",
368 OFF(tnf_get_raw(datum
))); /* common */
370 switch (tnf_get_kind(datum
)) {
372 (void) printf("%s", tnf_type_get_name(datum
));
375 (void) printf("\"%s\"", tnf_get_chars(datum
));
378 (void) printf("<%s>", tnf_get_type_name(datum
));
381 fail(0, gettext("inline aggregate slots/elements unhandled"));
385 fail(int do_perror
, char *message
, ...)
389 va_start(args
, message
);
390 (void) fprintf(stderr
, gettext("%s: "), g_cmdname
);
391 (void) vfprintf(stderr
, message
, args
);
394 (void) fprintf(stderr
, gettext(": %s"), strerror(errno
));
395 (void) fprintf(stderr
, gettext("\n"));
402 (void) fprintf(stderr
,
403 gettext("Usage: %s [-r] <tnf_file> [<tnf_file> ...]\n"),