2 *------------------------------------------------------------------
3 * Copyright (c) 2006-2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <netinet/in.h>
21 #include <sys/types.h>
24 #include <sys/fcntl.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/mem.h>
37 char *time_format = "%.03d:%.02d:%.02d:%.03d:%.03d ";
38 static char version[] = "cpeldump 2.0";
40 #define USEC_PER_MS 1000LL
41 #define USEC_PER_SECOND (1000*USEC_PER_MS)
42 #define USEC_PER_MINUTE (60*USEC_PER_SECOND)
43 #define USEC_PER_HOUR (60*USEC_PER_MINUTE)
45 uword *the_strtab_hash; /* (name, base-VA) hash of all string tables */
46 uword *the_evtdef_hash; /* (event-id, event-definition) hash */
47 uword *the_trackdef_hash; /* (track-id, track-definition) hash */
49 int widest_name_format=5;
50 int widest_track_format=5;
52 typedef struct bound_event_ {
59 bound_event_t *bound_events;
61 typedef struct bound_track_ {
66 bound_track_t *bound_tracks;
70 fprintf(stderr, "%s", s);
80 int (*pass1)(cpel_section_header_t *, int, FILE *);
81 int (*pass2)(cpel_section_header_t *, int, FILE *);
82 } section_processor_t;
84 int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
86 fprintf(ofp, "Bad (type 0) section, skipped...\n");
90 int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
95 int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
98 u8 *strtab_data_area = (u8 *)(sh+1);
100 /* Multiple string tables with the same name are Bad... */
101 p = hash_get_mem(the_strtab_hash, strtab_data_area);
103 fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
106 * Looks funny, but we really do want key = first string in the
107 * table, value = address(first string in the table)
109 hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
111 fprintf(stderr, "String Table %s\n", strtab_data_area);
116 int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
119 event_definition_section_header_t *edh;
120 event_definition_t *ep;
127 edh = (event_definition_section_header_t *)(sh+1);
128 nevents = ntohl(edh->number_of_event_definitions);
131 fprintf(stderr, "Event Definition Section: %d definitions\n",
135 p = hash_get_mem(the_strtab_hash, edh->string_table_name);
137 fprintf(ofp, "Fatal: couldn't find string table\n");
140 this_strtab = (u8 *)p[0];
142 ep = (event_definition_t *)(edh+1);
144 for (i = 0; i < nevents; i++) {
145 event_code = ntohl(ep->event);
146 p = hash_get(the_evtdef_hash, event_code);
148 fprintf(ofp, "Event %d redefined, retain first definition\n",
152 vec_add2(bound_events, bp, 1);
153 bp->event_code = event_code;
154 bp->event_str = this_strtab + ntohl(ep->event_format);
155 bp->datum_str = this_strtab + ntohl(ep->datum_format);
156 bp->is_strtab_ref = 0;
157 /* Decide if the datum format is a %s format => strtab reference */
162 for (j = 0; j < strlen((char *)bp->datum_str); j++) {
163 if (bp->datum_str[j] == '%'){
167 if (seen_percent && bp->datum_str[j] == 's') {
168 bp->is_strtab_ref = 1;
173 hash_set(the_evtdef_hash, event_code, bp - bound_events);
175 thislen = strlen((char *)bp->event_str);
176 if (thislen > widest_name_format)
177 widest_name_format = thislen;
184 int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
187 track_definition_section_header_t *tdh;
188 track_definition_t *tp;
195 tdh = (track_definition_section_header_t *)(sh+1);
196 nevents = ntohl(tdh->number_of_track_definitions);
199 fprintf(stderr, "Track Definition Section: %d definitions\n",
203 p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
205 fprintf(ofp, "Fatal: couldn't find string table\n");
208 this_strtab = (u8 *)p[0];
210 tp = (track_definition_t *)(tdh+1);
212 for (i = 0; i < nevents; i++) {
213 track_code = ntohl(tp->track);
214 p = hash_get(the_trackdef_hash, track_code);
216 fprintf(ofp, "track %d redefined, retain first definition\n",
220 vec_add2(bound_tracks, btp, 1);
221 btp->track = track_code;
222 btp->track_str = this_strtab + ntohl(tp->track_format);
223 hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
225 thislen = strlen((char *)btp->track_str);
226 if (thislen > widest_track_format)
227 widest_track_format = thislen;
233 int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
236 fprintf(stderr, "Unsupported type %d section\n",
237 ntohl(sh->section_type));
242 int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
244 event_section_header_t *eh;
246 u32 event_code, track_code;
247 u64 starttime = 0xFFFFFFFFFFFFFFFFULL;
254 u32 hours, minutes, seconds, msec, usec;
258 bound_event_t generic_event;
260 bound_track_t generic_track;
262 u8 *s, *evtpad, *trackpad;
265 generic_event.event_str = (u8 *)"%d";
266 generic_event.datum_str = (u8 *)"0x%08x";
267 generic_event.is_strtab_ref = 0;
269 generic_track.track_str = (u8 *)"%d";
270 last_track_code = 0xdeadbeef;
272 eh = (event_section_header_t *)(sh+1);
273 nevents = ntohl(eh->number_of_events);
274 ticks_per_us = ((double)ntohl(eh->clock_ticks_per_second)) / 1e6;
277 fprintf(stderr, "Event section: %d events, %.3f ticks_per_us\n",
278 nevents, ticks_per_us);
281 ep = (event_entry_t *)(eh+1);
283 p = hash_get_mem(the_strtab_hash, eh->string_table_name);
285 fprintf(ofp, "Fatal: couldn't find string table\n");
288 this_strtab = (u8 *)p[0];
290 evtpad = format(0, "%%-%ds ", widest_name_format);
292 trackpad = format(0, "%%-%ds ", widest_track_format);
293 vec_add1(trackpad, 0);
295 for (i = 0; i < nevents; i++) {
296 time0 = ntohl (ep->time[0]);
297 time1 = ntohl (ep->time[1]);
299 now = (((u64) time0)<<32) | time1;
301 /* Convert from bus ticks to usec */
307 if (starttime == 0xFFFFFFFFFFFFFFFFULL)
310 delta = now - starttime;
312 /* Delta = time since first event, in usec */
314 hours = delta / USEC_PER_HOUR;
316 delta -= ((u64) hours * USEC_PER_HOUR);
317 minutes = delta / USEC_PER_MINUTE;
319 delta -= ((u64) minutes * USEC_PER_MINUTE);
320 seconds = delta / USEC_PER_SECOND;
322 delta -= ((u64) seconds * USEC_PER_SECOND);
323 msec = delta / USEC_PER_MS;
325 delta -= ((u64) msec * USEC_PER_MS);
329 /* Output the timestamp */
330 fprintf(ofp, time_format, hours, minutes, seconds, msec, usec);
332 /* output the track */
333 track_code = ntohl(ep->track);
335 if (track_code != last_track_code) {
336 p = hash_get(the_trackdef_hash, track_code);
338 tp = &bound_tracks[p[0]];
343 s = format(0, (char *)tp->track_str, track_code);
345 fprintf(ofp, (char *)trackpad, s);
348 /* output the event and datum */
350 fprintf(stderr, "raw event code %d, raw event datum 0x%x\n",
351 ntohl(ep->event_code), ntohl(ep->event_datum));
354 event_code = ntohl(ep->event_code);
355 p = hash_get(the_evtdef_hash, event_code);
357 bp = &bound_events[p[0]];
361 s = format(0, (char *)bp->event_str, ntohl(ep->event_code));
363 fprintf(ofp, (char *)evtpad, s);
365 if (bp->is_strtab_ref) {
366 fprintf(ofp, (char *) bp->datum_str,
367 &this_strtab[ntohl(ep->event_datum)]);
369 fprintf(ofp, (char *) bp->datum_str, ntohl(ep->event_datum));
380 * Note: If necessary, add passes / columns to this table to
381 * handle section order dependencies.
384 section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
386 {bad_section, noop_pass}, /* type 0 -- f**ked */
387 {strtab_pass1, noop_pass}, /* type 1 -- STRTAB */
388 {unsupported_pass, noop_pass}, /* type 2 -- SYMTAB */
389 {evtdef_pass1, noop_pass}, /* type 3 -- EVTDEF */
390 {trackdef_pass1, noop_pass}, /* type 4 -- TRACKDEF */
391 {noop_pass, event_pass2}, /* type 5 -- EVENTS */
395 int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
399 type = ntohl(sh->section_type);
401 int (*fp)(cpel_section_header_t *, int, FILE *);
403 if (type > CPEL_NUM_SECTION_TYPES) {
404 fprintf(stderr, "Unknown section type %d\n", type);
409 fp = processors[type].pass1;
413 fp = processors[type].pass2;
417 fprintf(stderr, "Unknown pass %d\n", pass);
421 rv = (*fp)(sh, verbose, ofp);
426 int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
431 fprintf(stderr, "CPEL file: %s-endian, version %d\n",
432 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ?
434 fh->endian_version & CPEL_FILE_VERSION_MASK);
436 file_time = ntohl(fh->file_date);
438 fprintf(stderr, "File created %s", ctime(&file_time));
439 fprintf(stderr, "File has %d sections\n",
440 ntohs(fh->nsections));
447 int cpel_dump(u8 *cpel, int verbose, FILE *ofp)
449 cpel_file_header_t *fh;
450 cpel_section_header_t *sh;
455 /* First, the file header */
456 fh = (cpel_file_header_t *)cpel;
457 if (fh->endian_version != CPEL_FILE_VERSION) {
458 if (fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) {
459 fprintf(stderr, "Little endian data format not supported\n");
462 fprintf(stderr, "Unsupported file version 0x%x\n",
466 cpel_dump_file_header(fh, verbose, ofp);
467 nsections = ntohs(fh->nsections);
470 * Take two passes through the file. PASS1 builds
471 * data structures, PASS2 actually dumps the file.
472 * Just in case the sections are in an unobvious order.
474 sh = (cpel_section_header_t *)(fh+1);
475 for (i = 0; i < nsections; i++) {
476 section_size = ntohl(sh->data_length);
480 "Section type %d, size %d\n", ntohl(sh->section_type),
484 if(process_section(sh, verbose, ofp, PASS1))
488 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
491 sh = (cpel_section_header_t *)(fh+1);
492 for (i = 0; i < nsections; i++) {
493 if(process_section(sh, verbose, ofp, PASS2))
495 section_size = ntohl(sh->data_length);
497 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
503 char *mapfile (char *file)
510 maphfile = open (file, O_RDONLY);
514 fprintf (stderr, "Couldn't read %s, skipping it...\n", file);
518 if (fstat (maphfile, &statb) < 0)
520 fprintf (stderr, "Couldn't get size of %s, skipping it...\n", file);
524 /* Don't try to mmap directories, FIFOs, semaphores, etc. */
525 if (! (statb.st_mode & S_IFREG)) {
526 fprintf (stderr, "%s is not a regular file, skipping it...\n", file);
530 mapfsize = statb.st_size;
534 fprintf (stderr, "%s zero-length, skipping it...\n", file);
539 rv = mmap (0, mapfsize, PROT_READ, MAP_SHARED, maphfile, 0);
543 fprintf (stderr, "%s problem mapping, I quit...\n", file);
553 int main (int argc, char **argv)
556 char *outputfile = 0;
562 while (curarg < argc) {
563 if (!strncmp(argv[curarg], "--input-file", 3)) {
566 cpel_file = argv[curarg];
570 fatal("Missing filename after --input-file\n");
572 if (!strncmp(argv[curarg], "--output-file", 3)) {
575 outputfile = argv[curarg];
579 fatal("Missing filename after --output-file\n");
581 if (!strncmp(argv[curarg], "--verbose", 3)) {
589 "cpeldump --input-file <filename> [--output-file <filename>]\n");
590 fprintf(stderr, "%s\n", version);
597 clib_mem_init (0, ((uword)3<<30));
599 cpel = mapfile(cpel_file);
601 fprintf(stderr, "Couldn't map %s...\n", cpel_file);
606 ofp = fdopen(1, "w");
608 fprintf(stderr, "Couldn't fdopen(1)?\n");
612 ofp = fopen(outputfile, "w");
614 fprintf(stderr, "Couldn't create %s...\n", outputfile);
619 the_strtab_hash = hash_create_string (0, sizeof (uword));
620 the_evtdef_hash = hash_create (0, sizeof (uword));
621 the_trackdef_hash = hash_create (0, sizeof (uword));
623 #ifdef TEST_TRACK_INFO
626 vec_add2(bound_tracks, btp, 1);
628 btp->track_str = "cpu %d";
629 hash_set(the_trackdef_hash, 0, btp - bound_tracks);
630 hash_set(the_trackdef_hash, 1, btp - bound_tracks);
634 if (cpel_dump((u8 *)cpel, verbose, ofp)) {