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>
27 #include <vppinfra/clib.h>
28 #include <vppinfra/vec.h>
29 #include <vppinfra/hash.h>
30 #include <vppinfra/mem.h>
36 char *time_format = "%.03d:%.02d:%.02d:%.03d:%.03d ";
37 static char version[] = "cpeldump 2.0";
39 #define USEC_PER_MS 1000LL
40 #define USEC_PER_SECOND (1000*USEC_PER_MS)
41 #define USEC_PER_MINUTE (60*USEC_PER_SECOND)
42 #define USEC_PER_HOUR (60*USEC_PER_MINUTE)
44 uword *the_strtab_hash; /* (name, base-VA) hash of all string tables */
45 uword *the_evtdef_hash; /* (event-id, event-definition) hash */
46 uword *the_trackdef_hash; /* (track-id, track-definition) hash */
48 int widest_name_format=5;
49 int widest_track_format=5;
51 typedef struct bound_event_ {
58 bound_event_t *bound_events;
60 typedef struct bound_track_ {
65 bound_track_t *bound_tracks;
69 fprintf(stderr, "%s", s);
79 int (*pass1)(cpel_section_header_t *, int, FILE *);
80 int (*pass2)(cpel_section_header_t *, int, FILE *);
81 } section_processor_t;
83 int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
85 fprintf(ofp, "Bad (type 0) section, skipped...\n");
89 int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
94 int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
97 u8 *strtab_data_area = (u8 *)(sh+1);
99 /* Multiple string tables with the same name are Bad... */
100 p = hash_get_mem(the_strtab_hash, strtab_data_area);
102 fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
105 * Looks funny, but we really do want key = first string in the
106 * table, value = address(first string in the table)
108 hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
110 fprintf(stderr, "String Table %s\n", strtab_data_area);
115 int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
118 event_definition_section_header_t *edh;
119 event_definition_t *ep;
126 edh = (event_definition_section_header_t *)(sh+1);
127 nevents = ntohl(edh->number_of_event_definitions);
130 fprintf(stderr, "Event Definition Section: %d definitions\n",
134 p = hash_get_mem(the_strtab_hash, edh->string_table_name);
136 fprintf(ofp, "Fatal: couldn't find string table\n");
139 this_strtab = (u8 *)p[0];
141 ep = (event_definition_t *)(edh+1);
143 for (i = 0; i < nevents; i++) {
144 event_code = ntohl(ep->event);
145 p = hash_get(the_evtdef_hash, event_code);
147 fprintf(ofp, "Event %d redefined, retain first definition\n",
151 vec_add2(bound_events, bp, 1);
152 bp->event_code = event_code;
153 bp->event_str = this_strtab + ntohl(ep->event_format);
154 bp->datum_str = this_strtab + ntohl(ep->datum_format);
155 bp->is_strtab_ref = 0;
156 /* Decide if the datum format is a %s format => strtab reference */
161 for (j = 0; j < strlen((char *)bp->datum_str); j++) {
162 if (bp->datum_str[j] == '%'){
166 if (seen_percent && bp->datum_str[j] == 's') {
167 bp->is_strtab_ref = 1;
172 hash_set(the_evtdef_hash, event_code, bp - bound_events);
174 thislen = strlen((char *)bp->event_str);
175 if (thislen > widest_name_format)
176 widest_name_format = thislen;
183 int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
186 track_definition_section_header_t *tdh;
187 track_definition_t *tp;
194 tdh = (track_definition_section_header_t *)(sh+1);
195 nevents = ntohl(tdh->number_of_track_definitions);
198 fprintf(stderr, "Track Definition Section: %d definitions\n",
202 p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
204 fprintf(ofp, "Fatal: couldn't find string table\n");
207 this_strtab = (u8 *)p[0];
209 tp = (track_definition_t *)(tdh+1);
211 for (i = 0; i < nevents; i++) {
212 track_code = ntohl(tp->track);
213 p = hash_get(the_trackdef_hash, track_code);
215 fprintf(ofp, "track %d redefined, retain first definition\n",
219 vec_add2(bound_tracks, btp, 1);
220 btp->track = track_code;
221 btp->track_str = this_strtab + ntohl(tp->track_format);
222 hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
224 thislen = strlen((char *)btp->track_str);
225 if (thislen > widest_track_format)
226 widest_track_format = thislen;
232 int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
235 fprintf(stderr, "Unsupported type %d section\n",
236 ntohl(sh->section_type));
241 int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
243 event_section_header_t *eh;
245 u32 event_code, track_code;
246 u64 starttime = 0xFFFFFFFFFFFFFFFFULL;
253 u32 hours, minutes, seconds, msec, usec;
257 bound_event_t generic_event;
259 bound_track_t generic_track;
261 u8 *s, *evtpad, *trackpad;
264 generic_event.event_str = (u8 *)"%d";
265 generic_event.datum_str = (u8 *)"0x%08x";
266 generic_event.is_strtab_ref = 0;
268 generic_track.track_str = (u8 *)"%d";
269 last_track_code = 0xdeadbeef;
271 eh = (event_section_header_t *)(sh+1);
272 nevents = ntohl(eh->number_of_events);
273 ticks_per_us = ((double)ntohl(eh->clock_ticks_per_second)) / 1e6;
276 fprintf(stderr, "Event section: %d events, %.3f ticks_per_us\n",
277 nevents, ticks_per_us);
280 ep = (event_entry_t *)(eh+1);
282 p = hash_get_mem(the_strtab_hash, eh->string_table_name);
284 fprintf(ofp, "Fatal: couldn't find string table\n");
287 this_strtab = (u8 *)p[0];
289 evtpad = format(0, "%%-%ds ", widest_name_format);
291 trackpad = format(0, "%%-%ds ", widest_track_format);
292 vec_add1(trackpad, 0);
294 for (i = 0; i < nevents; i++) {
295 time0 = ntohl (ep->time[0]);
296 time1 = ntohl (ep->time[1]);
298 now = (((u64) time0)<<32) | time1;
300 /* Convert from bus ticks to usec */
306 if (starttime == 0xFFFFFFFFFFFFFFFFULL)
309 delta = now - starttime;
311 /* Delta = time since first event, in usec */
313 hours = delta / USEC_PER_HOUR;
315 delta -= ((u64) hours * USEC_PER_HOUR);
316 minutes = delta / USEC_PER_MINUTE;
318 delta -= ((u64) minutes * USEC_PER_MINUTE);
319 seconds = delta / USEC_PER_SECOND;
321 delta -= ((u64) seconds * USEC_PER_SECOND);
322 msec = delta / USEC_PER_MS;
324 delta -= ((u64) msec * USEC_PER_MS);
328 /* Output the timestamp */
329 fprintf(ofp, time_format, hours, minutes, seconds, msec, usec);
331 /* output the track */
332 track_code = ntohl(ep->track);
334 if (track_code != last_track_code) {
335 p = hash_get(the_trackdef_hash, track_code);
337 tp = &bound_tracks[p[0]];
342 s = format(0, (char *)tp->track_str, track_code);
344 fprintf(ofp, (char *)trackpad, s);
347 /* output the event and datum */
349 fprintf(stderr, "raw event code %d, raw event datum 0x%x\n",
350 ntohl(ep->event_code), ntohl(ep->event_datum));
353 event_code = ntohl(ep->event_code);
354 p = hash_get(the_evtdef_hash, event_code);
356 bp = &bound_events[p[0]];
360 s = format(0, (char *)bp->event_str, ntohl(ep->event_code));
362 fprintf(ofp, (char *)evtpad, s);
364 if (bp->is_strtab_ref) {
365 fprintf(ofp, (char *) bp->datum_str,
366 &this_strtab[ntohl(ep->event_datum)]);
368 fprintf(ofp, (char *) bp->datum_str, ntohl(ep->event_datum));
379 * Note: If necessary, add passes / columns to this table to
380 * handle section order dependencies.
383 section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
385 {bad_section, noop_pass}, /* type 0 -- f**ked */
386 {strtab_pass1, noop_pass}, /* type 1 -- STRTAB */
387 {unsupported_pass, noop_pass}, /* type 2 -- SYMTAB */
388 {evtdef_pass1, noop_pass}, /* type 3 -- EVTDEF */
389 {trackdef_pass1, noop_pass}, /* type 4 -- TRACKDEF */
390 {noop_pass, event_pass2}, /* type 5 -- EVENTS */
394 int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
398 type = ntohl(sh->section_type);
400 int (*fp)(cpel_section_header_t *, int, FILE *);
402 if (type > CPEL_NUM_SECTION_TYPES) {
403 fprintf(stderr, "Unknown section type %d\n", type);
408 fp = processors[type].pass1;
412 fp = processors[type].pass2;
416 fprintf(stderr, "Unknown pass %d\n", pass);
420 rv = (*fp)(sh, verbose, ofp);
425 int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
430 fprintf(stderr, "CPEL file: %s-endian, version %d\n",
431 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ?
433 fh->endian_version & CPEL_FILE_VERSION_MASK);
435 file_time = ntohl(fh->file_date);
437 fprintf(stderr, "File created %s", ctime(&file_time));
438 fprintf(stderr, "File has %d sections\n",
439 ntohs(fh->nsections));
446 int cpel_dump(u8 *cpel, int verbose, FILE *ofp)
448 cpel_file_header_t *fh;
449 cpel_section_header_t *sh;
454 /* First, the file header */
455 fh = (cpel_file_header_t *)cpel;
456 if (fh->endian_version != CPEL_FILE_VERSION) {
457 if (fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) {
458 fprintf(stderr, "Little endian data format not supported\n");
461 fprintf(stderr, "Unsupported file version 0x%x\n",
465 cpel_dump_file_header(fh, verbose, ofp);
466 nsections = ntohs(fh->nsections);
469 * Take two passes through the file. PASS1 builds
470 * data structures, PASS2 actually dumps the file.
471 * Just in case the sections are in an unobvious order.
473 sh = (cpel_section_header_t *)(fh+1);
474 for (i = 0; i < nsections; i++) {
475 section_size = ntohl(sh->data_length);
479 "Section type %d, size %d\n", ntohl(sh->section_type),
483 if(process_section(sh, verbose, ofp, PASS1))
487 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
490 sh = (cpel_section_header_t *)(fh+1);
491 for (i = 0; i < nsections; i++) {
492 if(process_section(sh, verbose, ofp, PASS2))
494 section_size = ntohl(sh->data_length);
496 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
502 char *mapfile (char *file)
509 maphfile = open (file, O_RDONLY);
513 fprintf (stderr, "Couldn't read %s, skipping it...\n", file);
517 if (fstat (maphfile, &statb) < 0)
519 fprintf (stderr, "Couldn't get size of %s, skipping it...\n", file);
523 /* Don't try to mmap directories, FIFOs, semaphores, etc. */
524 if (! (statb.st_mode & S_IFREG)) {
525 fprintf (stderr, "%s is not a regular file, skipping it...\n", file);
529 mapfsize = statb.st_size;
533 fprintf (stderr, "%s zero-length, skipping it...\n", file);
538 rv = mmap (0, mapfsize, PROT_READ, MAP_SHARED, maphfile, 0);
542 fprintf (stderr, "%s problem mapping, I quit...\n", file);
552 int main (int argc, char **argv)
555 char *outputfile = 0;
561 while (curarg < argc) {
562 if (!strncmp(argv[curarg], "--input-file", 3)) {
565 cpel_file = argv[curarg];
569 fatal("Missing filename after --input-file\n");
571 if (!strncmp(argv[curarg], "--output-file", 3)) {
574 outputfile = argv[curarg];
578 fatal("Missing filename after --output-file\n");
580 if (!strncmp(argv[curarg], "--verbose", 3)) {
588 "cpeldump --input-file <filename> [--output-file <filename>]\n");
589 fprintf(stderr, "%s\n", version);
596 clib_mem_init (0, ((uword)3<<30));
598 cpel = mapfile(cpel_file);
600 fprintf(stderr, "Couldn't map %s...\n", cpel_file);
605 ofp = fdopen(1, "w");
607 fprintf(stderr, "Couldn't fdopen(1)?\n");
611 ofp = fopen(outputfile, "w");
613 fprintf(stderr, "Couldn't create %s...\n", outputfile);
618 the_strtab_hash = hash_create_string (0, sizeof (uword));
619 the_evtdef_hash = hash_create (0, sizeof (uword));
620 the_trackdef_hash = hash_create (0, sizeof (uword));
622 #ifdef TEST_TRACK_INFO
625 vec_add2(bound_tracks, btp, 1);
627 btp->track_str = "cpu %d";
628 hash_set(the_trackdef_hash, 0, btp - bound_tracks);
629 hash_set(the_trackdef_hash, 1, btp - bound_tracks);
633 if (cpel_dump((u8 *)cpel, verbose, ofp)) {