blob: be0a70df24e63e4fb9145fa99ed0c01bbe0c6f19 [file] [log] [blame]
Dave Barach52642c32016-02-11 19:28:19 -05001/*
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <netinet/in.h>
20#include <string.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <sys/fcntl.h>
25#include <sys/mman.h>
26#include <unistd.h>
27#include <ctype.h>
28#include <vppinfra/clib.h>
29#include <vppinfra/vec.h>
30#include <vppinfra/hash.h>
Dave Barachde9571f2017-03-25 08:20:22 -040031#include <vppinfra/mem.h>
Dave Barach52642c32016-02-11 19:28:19 -050032#include <pwd.h>
33#include <stdarg.h>
34#include <time.h>
35#include "cpel.h"
36
37char *time_format = "%.03d:%.02d:%.02d:%.03d:%.03d ";
38static char version[] = "cpeldump 2.0";
39
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)
44
45uword *the_strtab_hash; /* (name, base-VA) hash of all string tables */
46uword *the_evtdef_hash; /* (event-id, event-definition) hash */
47uword *the_trackdef_hash; /* (track-id, track-definition) hash */
48
49int widest_name_format=5;
50int widest_track_format=5;
51
52typedef struct bound_event_ {
53 u32 event_code;
54 u8 *event_str;
55 u8 *datum_str;
56 u32 is_strtab_ref;
57} bound_event_t;
58
59bound_event_t *bound_events;
60
61typedef struct bound_track_ {
62 u32 track;
63 u8 *track_str;
64} bound_track_t;
65
66bound_track_t *bound_tracks;
67
68void fatal(char *s)
69{
70 fprintf(stderr, "%s", s);
71 exit(1);
72}
73
74typedef enum {
75 PASS1=1,
76 PASS2=2,
77} pass_t;
78
79typedef struct {
80 int (*pass1)(cpel_section_header_t *, int, FILE *);
81 int (*pass2)(cpel_section_header_t *, int, FILE *);
82} section_processor_t;
83
84int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
85{
86 fprintf(ofp, "Bad (type 0) section, skipped...\n");
87 return(0);
88}
89
90int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
91{
92 return(0);
93}
94
95int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
96{
97 uword *p;
98 u8 *strtab_data_area = (u8 *)(sh+1);
99
100 /* Multiple string tables with the same name are Bad... */
101 p = hash_get_mem(the_strtab_hash, strtab_data_area);
102 if (p) {
103 fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
104 }
105 /*
106 * Looks funny, but we really do want key = first string in the
107 * table, value = address(first string in the table)
108 */
109 hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
110 if (verbose) {
111 fprintf(stderr, "String Table %s\n", strtab_data_area);
112 }
113 return(0);
114}
115
116int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
117{
118 int i, nevents;
119 event_definition_section_header_t *edh;
120 event_definition_t *ep;
121 u8 *this_strtab;
122 u32 event_code;
123 uword *p;
124 bound_event_t *bp;
125 int thislen;
126
127 edh = (event_definition_section_header_t *)(sh+1);
128 nevents = ntohl(edh->number_of_event_definitions);
129
130 if (verbose) {
131 fprintf(stderr, "Event Definition Section: %d definitions\n",
132 nevents);
133 }
134
135 p = hash_get_mem(the_strtab_hash, edh->string_table_name);
136 if (!p) {
137 fprintf(ofp, "Fatal: couldn't find string table\n");
138 return(1);
139 }
140 this_strtab = (u8 *)p[0];
141
142 ep = (event_definition_t *)(edh+1);
143
144 for (i = 0; i < nevents; i++) {
145 event_code = ntohl(ep->event);
146 p = hash_get(the_evtdef_hash, event_code);
147 if (p) {
148 fprintf(ofp, "Event %d redefined, retain first definition\n",
149 event_code);
150 continue;
151 }
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 */
158 {
159 int j;
160 int seen_percent=0;
161
162 for (j = 0; j < strlen((char *)bp->datum_str); j++) {
163 if (bp->datum_str[j] == '%'){
164 seen_percent=1;
165 continue;
166 }
167 if (seen_percent && bp->datum_str[j] == 's') {
168 bp->is_strtab_ref = 1;
169 }
170 }
171 }
172
173 hash_set(the_evtdef_hash, event_code, bp - bound_events);
174
175 thislen = strlen((char *)bp->event_str);
176 if (thislen > widest_name_format)
177 widest_name_format = thislen;
178
179 ep++;
180 }
181 return (0);
182}
183
184int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
185{
186 int i, nevents;
187 track_definition_section_header_t *tdh;
188 track_definition_t *tp;
189 u8 *this_strtab;
190 u32 track_code;
191 uword *p;
192 bound_track_t *btp;
193 int thislen;
194
195 tdh = (track_definition_section_header_t *)(sh+1);
196 nevents = ntohl(tdh->number_of_track_definitions);
197
198 if (verbose) {
199 fprintf(stderr, "Track Definition Section: %d definitions\n",
200 nevents);
201 }
202
203 p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
204 if (!p) {
205 fprintf(ofp, "Fatal: couldn't find string table\n");
206 return(1);
207 }
208 this_strtab = (u8 *)p[0];
209
210 tp = (track_definition_t *)(tdh+1);
211
212 for (i = 0; i < nevents; i++) {
213 track_code = ntohl(tp->track);
214 p = hash_get(the_trackdef_hash, track_code);
215 if (p) {
216 fprintf(ofp, "track %d redefined, retain first definition\n",
217 track_code);
218 continue;
219 }
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);
224
225 thislen = strlen((char *)btp->track_str);
226 if (thislen > widest_track_format)
227 widest_track_format = thislen;
228 tp++;
229 }
230 return (0);
231}
232
233int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
234{
235 if (verbose) {
236 fprintf(stderr, "Unsupported type %d section\n",
237 ntohl(sh->section_type));
238 }
239 return(0);
240}
241
242int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
243{
244 event_section_header_t *eh;
245 f64 ticks_per_us;
246 u32 event_code, track_code;
247 u64 starttime = 0xFFFFFFFFFFFFFFFFULL;
248 int nevents;
249 int i;
250 uword *p;
251 event_entry_t *ep;
252 u64 now;
253 u64 delta;
254 u32 hours, minutes, seconds, msec, usec;
255 u32 time0, time1;
256 double d;
257 bound_event_t *bp;
258 bound_event_t generic_event;
259 bound_track_t *tp=0;
260 bound_track_t generic_track;
261 u32 last_track_code;
262 u8 *s, *evtpad, *trackpad;
263 u8 *this_strtab;
264
265 generic_event.event_str = (u8 *)"%d";
266 generic_event.datum_str = (u8 *)"0x%08x";
267 generic_event.is_strtab_ref = 0;
268
269 generic_track.track_str = (u8 *)"%d";
270 last_track_code = 0xdeadbeef;
271
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;
275
276 if (verbose) {
277 fprintf(stderr, "Event section: %d events, %.3f ticks_per_us\n",
278 nevents, ticks_per_us);
279 }
280
281 ep = (event_entry_t *)(eh+1);
282
283 p = hash_get_mem(the_strtab_hash, eh->string_table_name);
284 if (!p) {
285 fprintf(ofp, "Fatal: couldn't find string table\n");
286 return(1);
287 }
288 this_strtab = (u8 *)p[0];
289
290 evtpad = format(0, "%%-%ds ", widest_name_format);
291 vec_add1(evtpad, 0);
292 trackpad = format(0, "%%-%ds ", widest_track_format);
293 vec_add1(trackpad, 0);
294
295 for (i = 0; i < nevents; i++) {
296 time0 = ntohl (ep->time[0]);
297 time1 = ntohl (ep->time[1]);
298
299 now = (((u64) time0)<<32) | time1;
300
301 /* Convert from bus ticks to usec */
302 d = now;
303 d /= ticks_per_us;
304
305 now = d;
306
307 if (starttime == 0xFFFFFFFFFFFFFFFFULL)
308 starttime = now;
309
310 delta = now - starttime;
311
312 /* Delta = time since first event, in usec */
313
314 hours = delta / USEC_PER_HOUR;
315 if (hours)
316 delta -= ((u64) hours * USEC_PER_HOUR);
317 minutes = delta / USEC_PER_MINUTE;
318 if (minutes)
319 delta -= ((u64) minutes * USEC_PER_MINUTE);
320 seconds = delta / USEC_PER_SECOND;
321 if (seconds)
322 delta -= ((u64) seconds * USEC_PER_SECOND);
323 msec = delta / USEC_PER_MS;
324 if (msec)
325 delta -= ((u64) msec * USEC_PER_MS);
326
327 usec = delta;
328
329 /* Output the timestamp */
330 fprintf(ofp, time_format, hours, minutes, seconds, msec, usec);
331
332 /* output the track */
333 track_code = ntohl(ep->track);
334
335 if (track_code != last_track_code) {
336 p = hash_get(the_trackdef_hash, track_code);
337 if (p) {
338 tp = &bound_tracks[p[0]];
339 } else {
340 tp = &generic_track;
341 }
342 }
343 s = format(0, (char *)tp->track_str, track_code);
344 vec_add1(s, 0);
345 fprintf(ofp, (char *)trackpad, s);
346 vec_free(s);
347
348 /* output the event and datum */
349 if (0 && verbose) {
350 fprintf(stderr, "raw event code %d, raw event datum 0x%x\n",
351 ntohl(ep->event_code), ntohl(ep->event_datum));
352 }
353
354 event_code = ntohl(ep->event_code);
355 p = hash_get(the_evtdef_hash, event_code);
356 if (p) {
357 bp = &bound_events[p[0]];
358 } else {
359 bp = &generic_event;
360 }
361 s = format(0, (char *)bp->event_str, ntohl(ep->event_code));
362 vec_add1(s, 0);
363 fprintf(ofp, (char *)evtpad, s);
364 vec_free(s);
365 if (bp->is_strtab_ref) {
366 fprintf(ofp, (char *) bp->datum_str,
367 &this_strtab[ntohl(ep->event_datum)]);
368 } else {
369 fprintf(ofp, (char *) bp->datum_str, ntohl(ep->event_datum));
370 }
371 fputs("\n", ofp);
372 ep++;
373 }
374 vec_free(evtpad);
375 vec_free(trackpad);
376 return(0);
377}
378
379/*
380 * Note: If necessary, add passes / columns to this table to
381 * handle section order dependencies.
382 */
383
384section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
385{
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 */
392};
393
394
395int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
396 pass_t pass)
397{
398 u32 type;
399 type = ntohl(sh->section_type);
400 int rv;
401 int (*fp)(cpel_section_header_t *, int, FILE *);
402
403 if (type > CPEL_NUM_SECTION_TYPES) {
404 fprintf(stderr, "Unknown section type %d\n", type);
405 return(1);
406 }
407 switch(pass) {
408 case PASS1:
409 fp = processors[type].pass1;
410 break;
411
412 case PASS2:
413 fp = processors[type].pass2;
414 break;
415
416 default:
417 fprintf(stderr, "Unknown pass %d\n", pass);
418 return(1);
419 }
420
421 rv = (*fp)(sh, verbose, ofp);
422
423 return(rv);
424}
425
426int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
427{
428 time_t file_time;
429
430 if (verbose) {
431 fprintf(stderr, "CPEL file: %s-endian, version %d\n",
432 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ?
433 "little" : "big"),
434 fh->endian_version & CPEL_FILE_VERSION_MASK);
435
436 file_time = ntohl(fh->file_date);
437
438 fprintf(stderr, "File created %s", ctime(&file_time));
439 fprintf(stderr, "File has %d sections\n",
440 ntohs(fh->nsections));
441 }
442
443 return(0);
444}
445
446
447int cpel_dump(u8 *cpel, int verbose, FILE *ofp)
448{
449 cpel_file_header_t *fh;
450 cpel_section_header_t *sh;
451 u16 nsections;
452 u32 section_size;
453 int i;
454
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");
460 return(1);
461 }
462 fprintf(stderr, "Unsupported file version 0x%x\n",
463 fh->endian_version);
464 return(1);
465 }
466 cpel_dump_file_header(fh, verbose, ofp);
467 nsections = ntohs(fh->nsections);
468
469 /*
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.
473 */
474 sh = (cpel_section_header_t *)(fh+1);
475 for (i = 0; i < nsections; i++) {
476 section_size = ntohl(sh->data_length);
477
478 if(verbose) {
479 fprintf(stderr,
480 "Section type %d, size %d\n", ntohl(sh->section_type),
481 section_size);
482 }
483
484 if(process_section(sh, verbose, ofp, PASS1))
485 return(1);
486
487 sh++;
488 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
489 }
490
491 sh = (cpel_section_header_t *)(fh+1);
492 for (i = 0; i < nsections; i++) {
493 if(process_section(sh, verbose, ofp, PASS2))
494 return(1);
495 section_size = ntohl(sh->data_length);
496 sh++;
497 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
498 }
499 return(0);
500}
501
502
503char *mapfile (char *file)
504{
505 struct stat statb;
506 char *rv;
507 int maphfile;
508 size_t mapfsize;
509
510 maphfile = open (file, O_RDONLY);
511
512 if (maphfile < 0)
513 {
514 fprintf (stderr, "Couldn't read %s, skipping it...\n", file);
515 return (NULL);
516 }
517
518 if (fstat (maphfile, &statb) < 0)
519 {
520 fprintf (stderr, "Couldn't get size of %s, skipping it...\n", file);
521 return (NULL);
522 }
523
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);
527 return (NULL);
528 }
529
530 mapfsize = statb.st_size;
531
532 if (mapfsize < 3)
533 {
534 fprintf (stderr, "%s zero-length, skipping it...\n", file);
535 close (maphfile);
536 return (NULL);
537 }
538
539 rv = mmap (0, mapfsize, PROT_READ, MAP_SHARED, maphfile, 0);
540
541 if (rv == 0)
542 {
543 fprintf (stderr, "%s problem mapping, I quit...\n", file);
544 exit (-1);
545 }
546 close (maphfile);
547 return (rv);
548}
549
550/*
551 * main
552 */
553int main (int argc, char **argv)
554{
555 char *cpel_file = 0;
556 char *outputfile = 0;
557 FILE *ofp;
558 char *cpel;
559 int verbose=0;
560 int curarg=1;
561
562 while (curarg < argc) {
563 if (!strncmp(argv[curarg], "--input-file", 3)) {
564 curarg++;
565 if (curarg < argc) {
566 cpel_file = argv[curarg];
567 curarg++;
568 continue;
569 }
570 fatal("Missing filename after --input-file\n");
571 }
572 if (!strncmp(argv[curarg], "--output-file", 3)) {
573 curarg ++;
574 if (curarg < argc) {
575 outputfile = argv[curarg];
576 curarg ++;
577 continue;
578 }
579 fatal("Missing filename after --output-file\n");
580 }
581 if (!strncmp(argv[curarg], "--verbose", 3)) {
582 curarg++;
583 verbose = 1;
584 continue;
585 }
586
587 usage:
588 fprintf(stderr,
589 "cpeldump --input-file <filename> [--output-file <filename>]\n");
590 fprintf(stderr, "%s\n", version);
591 exit(1);
592 }
593
594 if (cpel_file == 0)
595 goto usage;
596
Dave Barachde9571f2017-03-25 08:20:22 -0400597 clib_mem_init (0, ((uword)3<<30));
598
Dave Barach52642c32016-02-11 19:28:19 -0500599 cpel = mapfile(cpel_file);
600 if (cpel == 0) {
601 fprintf(stderr, "Couldn't map %s...\n", cpel_file);
602 exit(1);
603 }
604
605 if (!outputfile) {
606 ofp = fdopen(1, "w");
607 if (ofp == NULL) {
608 fprintf(stderr, "Couldn't fdopen(1)?\n");
609 exit(1);
610 }
611 } else {
612 ofp = fopen(outputfile, "w");
613 if (ofp == NULL) {
614 fprintf(stderr, "Couldn't create %s...\n", outputfile);
615 exit(1);
616 }
617 }
618
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));
622
623#ifdef TEST_TRACK_INFO
624 {
625 bound_track_t *btp;
626 vec_add2(bound_tracks, btp, 1);
627 btp->track = 0;
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);
631 }
632#endif
633
634 if (cpel_dump((u8 *)cpel, verbose, ofp)) {
635 if (outputfile)
636 unlink(outputfile);
637 }
638
639 fclose(ofp);
640 return(0);
641}