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>
24 #include <sys/fcntl.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
37 typedef struct bound_event_ {
43 bound_event_t *bound_events;
45 int widest_track_format=8;
47 typedef struct bound_track_ {
52 bound_track_t *bound_tracks;
54 uword *the_strtab_hash; /* (name, base-VA) hash of all string tables */
55 uword *the_evtdef_hash; /* (event-id, event-definition) hash */
56 uword *the_trackdef_hash; /* (track-id, track-definition) hash */
57 u8 *event_strtab; /* event string-table */
61 fprintf(stderr, "%s", s);
71 int (*pass1)(cpel_section_header_t *, int, FILE *);
72 int (*pass2)(cpel_section_header_t *, int, FILE *);
73 } section_processor_t;
75 int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
77 fprintf(ofp, "Bad (type 0) section, skipped...\n");
81 int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
86 int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
89 u8 *strtab_data_area = (u8 *)(sh+1);
91 /* Multiple string tables with the same name are Bad... */
92 p = hash_get_mem(the_strtab_hash, strtab_data_area);
94 fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
97 * Looks funny, but we really do want key = first string in the
98 * table, value = address(first string in the table)
100 hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
102 fprintf(ofp, "String Table %s\n", strtab_data_area);
107 int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
110 event_definition_section_header_t *edh;
111 event_definition_t *ep;
117 edh = (event_definition_section_header_t *)(sh+1);
118 nevents = ntohl(edh->number_of_event_definitions);
121 fprintf(ofp, "Event Definition Section: %d definitions\n",
125 p = hash_get_mem(the_strtab_hash, edh->string_table_name);
127 fprintf(ofp, "Fatal: couldn't find string table\n");
130 this_strtab = (u8 *)p[0];
134 ep = (event_definition_t *)(edh+1);
136 for (i = 0; i < nevents; i++) {
137 event_code = ntohl(ep->event);
138 p = hash_get(the_evtdef_hash, event_code);
140 fprintf(ofp, "Event %d redefined, retain first definition\n",
144 vec_add2(bound_events, bp, 1);
145 bp->event_code = event_code;
146 bp->event_str = this_strtab + ntohl(ep->event_format);
147 bp->datum_str = this_strtab + ntohl(ep->datum_format);
148 hash_set(the_evtdef_hash, event_code, bp - bound_events);
150 add_event_from_cpel_file(event_code, (char *) bp->event_str,
151 (char *)bp->datum_str);
160 int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
163 track_definition_section_header_t *tdh;
164 track_definition_t *tp;
171 tdh = (track_definition_section_header_t *)(sh+1);
172 nevents = ntohl(tdh->number_of_track_definitions);
175 fprintf(ofp, "Track Definition Section: %d definitions\n",
179 p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
181 fprintf(ofp, "Fatal: couldn't find string table\n");
184 this_strtab = (u8 *)p[0];
186 tp = (track_definition_t *)(tdh+1);
188 for (i = 0; i < nevents; i++) {
189 track_code = ntohl(tp->track);
190 p = hash_get(the_trackdef_hash, track_code);
192 fprintf(ofp, "track %d redefined, retain first definition\n",
196 vec_add2(bound_tracks, btp, 1);
197 btp->track = track_code;
198 btp->track_str = this_strtab + ntohl(tp->track_format);
199 hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
201 track_strlen = strlen((char *)btp->track_str);
202 if (track_strlen > widest_track_format)
203 widest_track_format = track_strlen;
209 int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
212 fprintf(ofp, "Unsupported type %d section\n",
213 ntohl(sh->section_type));
218 int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
220 event_section_header_t *eh;
221 u32 event_code, track_code, datum;
222 u64 starttime = ~0ULL;
232 eh = (event_section_header_t *)(sh+1);
233 nevents = ntohl(eh->number_of_events);
234 ticks_per_ns = ntohl(eh->clock_ticks_per_second)/1e9;
235 ep = (event_entry_t *)(eh+1);
237 p = hash_get_mem(the_strtab_hash, eh->string_table_name);
239 fprintf(ofp, "Fatal: couldn't find string table\n");
242 event_strtab = (u8 *)p[0];
244 cpel_event_init(nevents);
246 for (i = 0; i < nevents; i++) {
247 time0 = ntohl (ep->time[0]);
248 time1 = ntohl (ep->time[1]);
250 now = (((u64) time0)<<32) | time1;
252 /* Convert from bus ticks to usec */
258 if (starttime == ~0ULL)
261 delta = now - starttime;
263 /* Delta = time since first event, in usec */
264 event_code = ntohl(ep->event_code);
265 track_code = ntohl(ep->track);
266 datum = ntohl(ep->event_datum);
268 add_cpel_event(delta, track_code, event_code, datum);
272 cpel_event_finalize();
276 char *strtab_ref(unsigned long datum)
278 return ((char *)(event_strtab + datum));
282 * Note: If necessary, add passes / columns to this table to
283 * handle section order dependencies.
286 section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
288 {bad_section, noop_pass}, /* type 0 -- f**ked */
289 {strtab_pass1, noop_pass}, /* type 1 -- STRTAB */
290 {unsupported_pass, noop_pass}, /* type 2 -- SYMTAB */
291 {evtdef_pass1, noop_pass}, /* type 3 -- EVTDEF */
292 {trackdef_pass1, noop_pass}, /* type 4 -- TRACKDEF */
293 {noop_pass, event_pass2}, /* type 5 -- EVENTS */
297 int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
301 type = ntohl(sh->section_type);
303 int (*fp)(cpel_section_header_t *, int, FILE *);
305 if (type > CPEL_NUM_SECTION_TYPES) {
306 fprintf(stderr, "Unknown section type %d\n", type);
311 fp = processors[type].pass1;
315 fp = processors[type].pass2;
319 fprintf(stderr, "Unknown pass %d\n", pass);
323 rv = (*fp)(sh, verbose, ofp);
328 int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
333 fprintf(ofp, "CPEL file: %s-endian, version %d\n",
334 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ?
336 fh->endian_version & CPEL_FILE_VERSION_MASK);
338 file_time = ntohl(fh->file_date);
340 fprintf(ofp, "File created %s", ctime(&file_time));
347 int cpel_process(u8 *cpel, int verbose, FILE *ofp)
349 cpel_file_header_t *fh;
350 cpel_section_header_t *sh;
355 /* First, the file header */
356 fh = (cpel_file_header_t *)cpel;
357 if (fh->endian_version != CPEL_FILE_VERSION) {
358 if (fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) {
359 fprintf(stderr, "Little endian data format not supported\n");
362 fprintf(stderr, "Unsupported file version 0x%x\n",
366 cpel_dump_file_header(fh, verbose, ofp);
367 nsections = ntohs(fh->nsections);
370 * Take two passes through the file. PASS1 builds
371 * data structures, PASS2 actually dumps the file.
372 * Just in case the sections are in an unobvious order.
374 sh = (cpel_section_header_t *)(fh+1);
375 for (i = 0; i < nsections; i++) {
376 section_size = ntohl(sh->data_length);
379 fprintf(ofp, "Section type %d, size %d\n", ntohl(sh->section_type),
383 if(process_section(sh, verbose, ofp, PASS1))
387 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
390 sh = (cpel_section_header_t *)(fh+1);
391 for (i = 0; i < nsections; i++) {
392 if(process_section(sh, verbose, ofp, PASS2))
394 section_size = ntohl(sh->data_length);
396 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
406 int read_cpel_file(char *cpel_file)
411 static unsigned long size;
415 unmapfile((char *)cpel, size);
416 hash_free(the_strtab_hash);
418 hash_free(the_evtdef_hash);
420 hash_free(the_trackdef_hash);
421 the_trackdef_hash = 0;
424 cpel = (u8 *)mapfile((char *)cpel_file, &size);
426 fprintf(stderr, "Couldn't map %s...\n", cpel_file);
431 ofp = fdopen(2, "w");
433 fprintf(stderr, "Couldn't fdopen(2)?\n");
438 the_strtab_hash = hash_create_string (0, sizeof (uword));
439 the_evtdef_hash = hash_create (0, sizeof (uword));
440 the_trackdef_hash = hash_create (0, sizeof (uword));
442 rv = cpel_process(cpel, verbose, ofp);
444 set_pid_ax_width(8*widest_track_format);
449 static bound_track_t generic_hex_track = {0, (u8 *) "0x%08x"};
450 static bound_track_t generic_decimal_track = {0, (u8 *) "%8ld"};
455 char *get_track_label(unsigned long track)
460 p = hash_get(the_trackdef_hash, track);
462 tp = &bound_tracks[p[0]];
465 tp = &generic_hex_track;
467 tp = &generic_decimal_track;
469 return((char *)tp->track_str);