2 *------------------------------------------------------------------
3 * Copyright (c) 2005-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>
36 typedef struct bound_event_ {
42 bound_event_t *bound_events;
44 int widest_track_format=8;
46 typedef struct bound_track_ {
51 bound_track_t *bound_tracks;
53 uword *the_strtab_hash; /* (name, base-VA) hash of all string tables */
54 uword *the_evtdef_hash; /* (event-id, event-definition) hash */
55 uword *the_trackdef_hash; /* (track-id, track-definition) hash */
56 u8 *event_strtab; /* event string-table */
60 fprintf(stderr, "%s", s);
70 int (*pass1)(cpel_section_header_t *, int, FILE *);
71 int (*pass2)(cpel_section_header_t *, int, FILE *);
72 } section_processor_t;
74 int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
76 fprintf(ofp, "Bad (type 0) section, skipped...\n");
80 int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
85 int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
88 u8 *strtab_data_area = (u8 *)(sh+1);
90 /* Multiple string tables with the same name are Bad... */
91 p = hash_get_mem(the_strtab_hash, strtab_data_area);
93 fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
96 * Looks funny, but we really do want key = first string in the
97 * table, value = address(first string in the table)
99 hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
101 fprintf(ofp, "String Table %s\n", strtab_data_area);
106 int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
109 event_definition_section_header_t *edh;
110 event_definition_t *ep;
116 edh = (event_definition_section_header_t *)(sh+1);
117 nevents = ntohl(edh->number_of_event_definitions);
120 fprintf(ofp, "Event Definition Section: %d definitions\n",
124 p = hash_get_mem(the_strtab_hash, edh->string_table_name);
126 fprintf(ofp, "Fatal: couldn't find string table\n");
129 this_strtab = (u8 *)p[0];
133 ep = (event_definition_t *)(edh+1);
135 for (i = 0; i < nevents; i++) {
136 event_code = ntohl(ep->event);
137 p = hash_get(the_evtdef_hash, event_code);
139 fprintf(ofp, "Event %d redefined, retain first definition\n",
143 vec_add2(bound_events, bp, 1);
144 bp->event_code = event_code;
145 bp->event_str = this_strtab + ntohl(ep->event_format);
146 bp->datum_str = this_strtab + ntohl(ep->datum_format);
147 hash_set(the_evtdef_hash, event_code, bp - bound_events);
149 add_event_from_cpel_file(event_code, (char *) bp->event_str,
150 (char *)bp->datum_str);
159 int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
162 track_definition_section_header_t *tdh;
163 track_definition_t *tp;
170 tdh = (track_definition_section_header_t *)(sh+1);
171 nevents = ntohl(tdh->number_of_track_definitions);
174 fprintf(ofp, "Track Definition Section: %d definitions\n",
178 p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
180 fprintf(ofp, "Fatal: couldn't find string table\n");
183 this_strtab = (u8 *)p[0];
185 tp = (track_definition_t *)(tdh+1);
187 for (i = 0; i < nevents; i++) {
188 track_code = ntohl(tp->track);
189 p = hash_get(the_trackdef_hash, track_code);
191 fprintf(ofp, "track %d redefined, retain first definition\n",
195 vec_add2(bound_tracks, btp, 1);
196 btp->track = track_code;
197 btp->track_str = this_strtab + ntohl(tp->track_format);
198 hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
200 track_strlen = strlen((char *)btp->track_str);
201 if (track_strlen > widest_track_format)
202 widest_track_format = track_strlen;
208 int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
211 fprintf(ofp, "Unsupported type %d section\n",
212 ntohl(sh->section_type));
217 int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
219 event_section_header_t *eh;
220 u32 event_code, track_code, datum;
221 u64 starttime = ~0ULL;
231 eh = (event_section_header_t *)(sh+1);
232 nevents = ntohl(eh->number_of_events);
233 ticks_per_ns = ntohl(eh->clock_ticks_per_second)/1e9;
234 ep = (event_entry_t *)(eh+1);
236 p = hash_get_mem(the_strtab_hash, eh->string_table_name);
238 fprintf(ofp, "Fatal: couldn't find string table\n");
241 event_strtab = (u8 *)p[0];
243 cpel_event_init(nevents);
245 for (i = 0; i < nevents; i++) {
246 time0 = ntohl (ep->time[0]);
247 time1 = ntohl (ep->time[1]);
249 now = (((u64) time0)<<32) | time1;
251 /* Convert from bus ticks to usec */
257 if (starttime == ~0ULL)
260 delta = now - starttime;
262 /* Delta = time since first event, in usec */
263 event_code = ntohl(ep->event_code);
264 track_code = ntohl(ep->track);
265 datum = ntohl(ep->event_datum);
267 add_cpel_event(delta, track_code, event_code, datum);
271 cpel_event_finalize();
275 char *strtab_ref(unsigned long datum)
277 return ((char *)(event_strtab + datum));
281 * Note: If necessary, add passes / columns to this table to
282 * handle section order dependencies.
285 section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
287 {bad_section, noop_pass}, /* type 0 -- f**ked */
288 {strtab_pass1, noop_pass}, /* type 1 -- STRTAB */
289 {unsupported_pass, noop_pass}, /* type 2 -- SYMTAB */
290 {evtdef_pass1, noop_pass}, /* type 3 -- EVTDEF */
291 {trackdef_pass1, noop_pass}, /* type 4 -- TRACKDEF */
292 {noop_pass, event_pass2}, /* type 5 -- EVENTS */
296 int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
300 type = ntohl(sh->section_type);
302 int (*fp)(cpel_section_header_t *, int, FILE *);
304 if (type > CPEL_NUM_SECTION_TYPES) {
305 fprintf(stderr, "Unknown section type %d\n", type);
310 fp = processors[type].pass1;
314 fp = processors[type].pass2;
318 fprintf(stderr, "Unknown pass %d\n", pass);
322 rv = (*fp)(sh, verbose, ofp);
327 int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
332 fprintf(ofp, "CPEL file: %s-endian, version %d\n",
333 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ?
335 fh->endian_version & CPEL_FILE_VERSION_MASK);
337 file_time = ntohl(fh->file_date);
339 fprintf(ofp, "File created %s", ctime(&file_time));
346 int cpel_process(u8 *cpel, int verbose, FILE *ofp)
348 cpel_file_header_t *fh;
349 cpel_section_header_t *sh;
354 /* First, the file header */
355 fh = (cpel_file_header_t *)cpel;
356 if (fh->endian_version != CPEL_FILE_VERSION) {
357 if (fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) {
358 fprintf(stderr, "Little endian data format not supported\n");
361 fprintf(stderr, "Unsupported file version 0x%x\n",
365 cpel_dump_file_header(fh, verbose, ofp);
366 nsections = ntohs(fh->nsections);
369 * Take two passes through the file. PASS1 builds
370 * data structures, PASS2 actually dumps the file.
371 * Just in case the sections are in an unobvious order.
373 sh = (cpel_section_header_t *)(fh+1);
374 for (i = 0; i < nsections; i++) {
375 section_size = ntohl(sh->data_length);
378 fprintf(ofp, "Section type %d, size %d\n", ntohl(sh->section_type),
382 if(process_section(sh, verbose, ofp, PASS1))
386 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
389 sh = (cpel_section_header_t *)(fh+1);
390 for (i = 0; i < nsections; i++) {
391 if(process_section(sh, verbose, ofp, PASS2))
393 section_size = ntohl(sh->data_length);
395 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
405 int read_cpel_file(char *cpel_file)
410 static unsigned long size;
414 unmapfile((char *)cpel, size);
415 hash_free(the_strtab_hash);
417 hash_free(the_evtdef_hash);
419 hash_free(the_trackdef_hash);
420 the_trackdef_hash = 0;
423 cpel = (u8 *)mapfile((char *)cpel_file, &size);
425 fprintf(stderr, "Couldn't map %s...\n", cpel_file);
430 ofp = fdopen(2, "w");
432 fprintf(stderr, "Couldn't fdopen(2)?\n");
437 the_strtab_hash = hash_create_string (0, sizeof (uword));
438 the_evtdef_hash = hash_create (0, sizeof (uword));
439 the_trackdef_hash = hash_create (0, sizeof (uword));
441 rv = cpel_process(cpel, verbose, ofp);
443 set_pid_ax_width(8*widest_track_format);
448 static bound_track_t generic_hex_track = {0, (u8 *) "0x%08x"};
449 static bound_track_t generic_decimal_track = {0, (u8 *) "%8ld"};
454 char *get_track_label(unsigned long track)
459 p = hash_get(the_trackdef_hash, track);
461 tp = &bound_tracks[p[0]];
464 tp = &generic_hex_track;
466 tp = &generic_decimal_track;
468 return((char *)tp->track_str);