83e10ff2c0a85910c79e3c01f1a8d2456ef31e24
3 genevent.c: Generate helper declarations and functions to trace events
4 from an event description file.
6 Copyright (C) 2002, Xianxiu Yang
7 Copyright (C) 2002, Michel Dagenais
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* This program reads the ".event" event definitions input files
24 specified as command line arguments and generates corresponding
25 ".c" and ".h" files required to trace such events in the kernel.
27 The program uses a very simple tokenizer, called from a hand written
28 recursive descent parser to fill a data structure describing the events.
29 The result is a sequence of events definitions which refer to type
32 A table of named types is maintained to allow refering to types by name
33 when the same type is used at several places. Finally a sequence of
34 all types is maintained to facilitate the freeing of all type
35 information when the processing of an ".event" file is finished. */
42 #include <linux/errno.h>
48 /* Named types may be referenced from anywhere */
52 int main(int argc
, char** argv
)
56 char buffer
[BUFFER_SIZE
];
60 printf("At least one event definition file is needed\n");
65 in
.error
= error_callback
;
67 for(i
= 1 ; i
< argc
; i
++) {
69 in
.name
= allocAndCopy(argv
[i
]);
71 in
.fp
= fopen(in
.name
, "r");
73 in
.error(&in
,"cannot open facility input file");
77 token
= getToken(&in
);
78 if(in
.type
== ENDFILE
) break;
80 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
83 if(strcmp("facility",token
) == 0) {
84 fac
= memAlloc(sizeof(facility
));
86 fac
->description
= NULL
;
87 sequence_init(&(fac
->events
));
88 table_init(&(fac
->named_types
));
89 sequence_init(&(fac
->unnamed_types
));
91 parseFacility(&in
, fac
);
93 //check if any namedType is not defined
94 checkNamedTypesImplemented(&fac
->named_types
);
96 else in
.error(&in
,"facility token was expected");
98 generateFile(argv
[i
]);
101 free(fac
->description
);
102 freeEvents(&fac
->events
);
103 sequence_dispose(&fac
->events
);
104 freeNamedType(&fac
->named_types
);
105 table_dispose(&fac
->named_types
);
106 freeTypes(&fac
->unnamed_types
);
107 sequence_dispose(&fac
->unnamed_types
);
119 /*****************************************************************************
121 * generateFile : generate .c and .h file
123 * name : name of event definition file
124 ****************************************************************************/
125 void generateFile(char *name
){
126 char *loadName
, *hName
, *tmp
, *tmp2
;
129 unsigned long checksum
=0;
131 //remove .xml if it exists
132 tmp
= &name
[strlen(name
)-4];
133 if(strcmp(tmp
, ".xml") == 0){
137 tmp
= strrchr(name
,'/');
144 loadName
= appendString("ltt-facility-loader-", tmp
);
145 tmp2
= appendString(loadName
,".h");
148 hName
= appendString("ltt-facility-", tmp
);
149 tmp2
= appendString(hName
,".h");
152 lFp
= fopen(loadName
,"w");
154 printf("Cannot open the file : %s\n",loadName
);
158 hFp
= fopen(hName
,"w");
160 printf("Cannot open the file : %s\n",hName
);
167 generateChecksum(fac
->name
, &checksum
, &(fac
->events
));
169 /* generate .h file, event enumeration then structures and functions */
170 fprintf(hFp
, "#ifndef _LTT_FACILITY_%s_H_\n",fac
->capname
);
171 fprintf(hFp
, "#define _LTT_FACILITY_%s_H_\n\n",fac
->capname
);
172 generateEnumEvent(hFp
, fac
->name
, &nbEvent
, checksum
);
173 generateTypeDefs(hFp
);
174 generateStructFunc(hFp
, fac
->name
,checksum
);
175 fprintf(hFp
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
177 /* generate .h file, calls to register the facility at init time */
178 generateLoaderfile(lFp
,fac
->name
,nbEvent
,checksum
,fac
->capname
);
185 /*****************************************************************************
187 * generateEnumEvent : output event enum to .h file
189 * fp : file to be written to
190 * facName : name of facility
192 * nbEvent : number of events in the facility
193 ****************************************************************************/
194 void generateEnumEvent(FILE *fp
, char *facName
, int * nbEvent
, unsigned long checksum
) {
197 fprintf(fp
,"#include <linux/ltt-log.h>\n\n");
199 fprintf(fp
,"/**** facility handle ****/\n\n");
200 fprintf(fp
,"extern trace_facility_t ltt_facility_%s_%X;\n",facName
, checksum
);
201 fprintf(fp
,"extern trace_facility_t ltt_facility_%s;\n\n\n",facName
, checksum
);
203 fprintf(fp
,"/**** event type ****/\n\n");
204 fprintf(fp
,"enum %s_event {\n",facName
);
206 for(pos
= 0; pos
< fac
->events
.position
;pos
++) {
207 fprintf(fp
,"\t%s", ((event
*)(fac
->events
.array
[pos
]))->name
);
208 if(pos
!= fac
->events
.position
-1) fprintf(fp
,",\n");
210 fprintf(fp
,"\n};\n\n\n");
212 // fprintf(fp,"/**** number of events in the facility ****/\n\n");
213 // fprintf(fp,"int nbEvents_%s = %d;\n\n\n",facName, fac->events.position);
214 *nbEvent
= fac
->events
.position
;
218 /*****************************************************************************
220 * printStruct : Generic struct printing function
222 * fp : file to be written to
223 * len : number of fields
224 * array : array of field info
225 * name : basic struct name
226 * facName : name of facility
227 * whichTypeFirst : struct or array/sequence first
228 * hasStrSeq : string or sequence present?
229 * structCount : struct postfix
230 ****************************************************************************/
233 printStruct(FILE * fp
, int len
, void ** array
, char * name
, char * facName
,
234 int * whichTypeFirst
, int * hasStrSeq
, int * structCount
)
239 type_descriptor
* td
;
241 for (pos
= 0; pos
< len
; pos
++) {
242 fld
= (field
*)array
[pos
];
244 if( td
->type
!= STRING
&& td
->type
!= SEQUENCE
&&
246 if (*whichTypeFirst
== 0) {
247 *whichTypeFirst
= 1; //struct first
252 fprintf(fp
,"struct %s_%s",name
, facName
);
254 fprintf(fp
, "_%d {\n",++*structCount
);
259 fprintf(fp
, "\t%s %s; /* %s */\n",
260 getTypeStr(td
),fld
->name
,fld
->description
);
262 if (*whichTypeFirst
== 0) {
263 //string or sequence or array first
268 fprintf(fp
,"} __attribute__ ((packed));\n\n");
275 fprintf(fp
,"} __attribute__ ((packed));\n\n");
280 /*****************************************************************************
282 * generateHfile : Create the typedefs
284 * fp : file to be written to
285 ****************************************************************************/
287 generateTypeDefs(FILE * fp
)
291 fprintf(fp
, "/**** Basic Type Definitions ****/\n\n");
293 for (pos
= 0; pos
< fac
->named_types
.values
.position
; pos
++) {
294 type_descriptor
* type
=
295 (type_descriptor
*)fac
->named_types
.values
.array
[pos
];
296 printStruct(fp
, type
->fields
.position
, type
->fields
.array
,
297 "", type
->type_name
, &tmp
, &tmp
, NULL
);
298 fprintf(fp
, "typedef struct _%s %s;\n\n",
299 type
->type_name
, type
->type_name
);
304 /*****************************************************************************
306 * generateEnumDefinition: generate enum definition if it exists
308 * fp : file to be written to
310 ****************************************************************************/
311 void generateEnumDefinition(FILE * fp
, type_descriptor
* type
){
314 if(type
->already_printed
) return;
316 fprintf(fp
,"enum {\n");
317 for(pos
= 0; pos
< type
->labels
.position
; pos
++){
318 fprintf(fp
,"\t%s", type
->labels
.array
[pos
]);
319 if (pos
!= type
->labels
.position
- 1) fprintf(fp
,",");
320 if(type
->labels_description
.array
[pos
] != NULL
)
321 fprintf(fp
,"\t/* %s */\n",type
->labels_description
.array
[pos
]);
325 fprintf(fp
,"};\n\n\n");
327 type
->already_printed
= 1;
330 /*****************************************************************************
332 * generateStrucTFunc: output structure and function to .h file
334 * fp : file to be written to
335 * facName : name of facility
336 ****************************************************************************/
337 void generateStructFunc(FILE * fp
, char * facName
, unsigned long checksum
){
340 type_descriptor
* td
;
342 int hasStrSeq
, flag
, structCount
, seqCount
,strCount
, whichTypeFirst
=0;
344 for(pos
= 0; pos
< fac
->events
.position
; pos
++){
345 ev
= (event
*) fac
->events
.array
[pos
];
346 //yxx if(ev->nested)continue;
347 fprintf(fp
,"/**** structure and trace function for event: %s ****/\n\n",ev
->name
);
348 if(ev
->type
== 0){ // event without type
349 fprintf(fp
,"static inline void trace_%s_%s(void){\n",facName
,ev
->name
);
350 fprintf(fp
,"\tltt_log_event(ltt_facility_%s_%X, %s, 0, NULL);\n",facName
,checksum
,ev
->name
);
351 fprintf(fp
,"};\n\n\n");
355 //if fields contain enum, print out enum definition
356 for(pos1
= 0; pos1
< ev
->type
->fields
.position
; pos1
++){
357 fld
= (field
*)ev
->type
->fields
.array
[pos1
];
358 if(fld
->type
->type
== ENUM
) generateEnumDefinition(fp
, fld
->type
);
361 //default: no string, array or sequence in the event
366 //structure for kernel
367 printStruct(fp
, ev
->type
->fields
.position
, ev
->type
->fields
.array
,
368 ev
->name
, facName
, &whichTypeFirst
, &hasStrSeq
, &structCount
);
370 //trace function : function name and parameters
373 fprintf(fp
,"static inline void trace_%s_%s(",facName
,ev
->name
);
374 for(pos1
= 0; pos1
< ev
->type
->fields
.position
; pos1
++){
375 fld
= (field
*)ev
->type
->fields
.array
[pos1
];
377 if(td
->type
== ARRAY
){
378 fprintf(fp
,"%s * %s",getTypeStr(td
), fld
->name
);
379 }else if(td
->type
== STRING
){
380 fprintf(fp
,"short int strLength_%d, %s * %s",++strCount
, getTypeStr(td
), fld
->name
);
381 }else if(td
->type
== SEQUENCE
){
382 fprintf(fp
,"%s seqLength_%d, %s * %s",uintOutputTypes
[td
->size
], ++seqCount
,getTypeStr(td
), fld
->name
);
383 }else fprintf(fp
,"%s %s",getTypeStr(td
), fld
->name
);
384 if(pos1
!= ev
->type
->fields
.position
-1)fprintf(fp
,", ");
388 //length of buffer : length of all structures
389 fprintf(fp
,"\tint bufLength = ");
390 for(pos1
=0;pos1
<structCount
;pos1
++){
391 fprintf(fp
,"sizeof(struct %s_%s_%d)",ev
->name
, facName
,pos1
+1);
392 if(pos1
!= structCount
-1) fprintf(fp
," + ");
395 //length of buffer : length of all arrays, sequences and strings
399 for(pos1
= 0; pos1
< ev
->type
->fields
.position
; pos1
++){
400 fld
= (field
*)ev
->type
->fields
.array
[pos1
];
402 if(td
->type
== SEQUENCE
|| td
->type
==STRING
||td
->type
==ARRAY
){
403 if(structCount
|| flag
> 0) fprintf(fp
," + ");
404 if(td
->type
== SEQUENCE
) fprintf(fp
,"sizeof(%s) + sizeof(%s) * seqLength_%d",uintOutputTypes
[td
->size
], getTypeStr(td
), ++seqCount
);
405 else if(td
->type
==STRING
) fprintf(fp
,"strLength_%d + 1", ++strCount
);
406 else if(td
->type
==ARRAY
) fprintf(fp
,"sizeof(%s) * %d", getTypeStr(td
),td
->size
);
407 if(structCount
== 0) flag
= 1;
413 fprintf(fp
,"\tchar buff[bufLength];\n");
415 //declare a char pointer if needed
416 if(structCount
+ hasStrSeq
> 1) fprintf(fp
,"\tchar * ptr = buff;\n");
418 //allocate memory for new struct and initialize it
419 if(whichTypeFirst
== 1){ //struct first
420 for(pos1
=0;pos1
<structCount
;pos1
++){
421 if(pos1
==0) fprintf(fp
,"\tstruct %s_%s_1 * __1 = (struct %s_%s_1 *)buff;\n",ev
->name
, facName
,ev
->name
, facName
);
422 else fprintf(fp
,"\tstruct %s_%s_%d __%d;\n",ev
->name
, facName
,pos1
+1,pos1
+1);
424 }else if(whichTypeFirst
== 2){
425 for(pos1
=0;pos1
<structCount
;pos1
++)
426 fprintf(fp
,"\tstruct %s_%s_%d __%d;\n",ev
->name
, facName
,pos1
+1,pos1
+1);
430 if(structCount
) fprintf(fp
,"\t//initialize structs\n");
433 for(pos1
= 0; pos1
< ev
->type
->fields
.position
; pos1
++){
434 fld
= (field
*)ev
->type
->fields
.array
[pos1
];
436 if(td
->type
!= ARRAY
&& td
->type
!= SEQUENCE
&& td
->type
!= STRING
){
440 if(structCount
> 1) fprintf(fp
,"\n");
442 if(structCount
== 1 && whichTypeFirst
== 1) fprintf(fp
, "\t__1->%s = %s;\n",fld
->name
,fld
->name
);
443 else fprintf(fp
, "\t__%d.%s = %s;\n",structCount
,fld
->name
,fld
->name
);
446 if(structCount
) fprintf(fp
,"\n");
448 //set ptr to the end of first struct if needed;
449 if(whichTypeFirst
== 1 && structCount
+ hasStrSeq
> 1){
450 fprintf(fp
,"\n\t//set ptr to the end of the first struct\n");
451 fprintf(fp
,"\tptr += sizeof(struct %s_%s_1);\n\n",ev
->name
, facName
);
454 //copy struct, sequence and string to buffer
459 for(pos1
= 0; pos1
< ev
->type
->fields
.position
; pos1
++){
460 fld
= (field
*)ev
->type
->fields
.array
[pos1
];
462 if(td
->type
!= STRING
&& td
->type
!= SEQUENCE
&& td
->type
!= ARRAY
){
463 if(flag
== 0) structCount
++;
465 if((structCount
> 1 || whichTypeFirst
== 2) && flag
== 1){
466 fprintf(fp
,"\t//copy struct to buffer\n");
467 fprintf(fp
,"\tmemcpy(ptr, &__%d, sizeof(struct %s_%s_%d));\n",structCount
, ev
->name
, facName
,structCount
);
468 fprintf(fp
,"\tptr += sizeof(struct %s_%s_%d);\n\n",ev
->name
, facName
,structCount
);
470 }else if(td
->type
== SEQUENCE
){
472 fprintf(fp
,"\t//copy sequence length and sequence to buffer\n");
473 fprintf(fp
,"\t*ptr = seqLength_%d;\n",++seqCount
);
474 fprintf(fp
,"\tptr += sizeof(%s);\n",uintOutputTypes
[td
->size
]);
475 fprintf(fp
,"\tmemcpy(ptr, %s, sizeof(%s) * seqLength_%d);\n",fld
->name
, getTypeStr(td
), seqCount
);
476 fprintf(fp
,"\tptr += sizeof(%s) * seqLength_%d;\n\n",getTypeStr(td
), seqCount
);
477 }else if(td
->type
==STRING
){
479 fprintf(fp
,"\t//copy string to buffer\n");
480 fprintf(fp
,"\tif(strLength_%d > 0){\n",++strCount
);
481 fprintf(fp
,"\t\tmemcpy(ptr, %s, strLength_%d + 1);\n", fld
->name
, strCount
);
482 fprintf(fp
,"\t\tptr += strLength_%d + 1;\n",strCount
);
483 fprintf(fp
,"\t}else{\n");
484 fprintf(fp
,"\t\t*ptr = '\\0';\n");
485 fprintf(fp
,"\t\tptr += 1;\n");
486 fprintf(fp
,"\t}\n\n");
487 }else if(td
->type
==ARRAY
){
489 fprintf(fp
,"\t//copy array to buffer\n");
490 fprintf(fp
,"\tmemcpy(ptr, %s, sizeof(%s) * %d);\n", fld
->name
, getTypeStr(td
), td
->size
);
491 fprintf(fp
,"\tptr += sizeof(%s) * %d;\n\n",getTypeStr(td
), td
->size
);
494 if(structCount
+ seqCount
> 1) fprintf(fp
,"\n");
496 //call trace function
497 fprintf(fp
,"\n\t//call trace function\n");
498 fprintf(fp
,"\tltt_log_event(ltt_facility_%s_%X, %s, bufLength, buff);\n",facName
,checksum
,ev
->name
);
499 fprintf(fp
,"};\n\n\n");
504 /*****************************************************************************
506 * getTypeStr : generate type string
508 * td : a type descriptor
510 * char * : type string
511 ****************************************************************************/
512 char * getTypeStr(type_descriptor
* td
){
513 type_descriptor
* t
;
517 return intOutputTypes
[td
->size
];
519 return uintOutputTypes
[td
->size
];
525 return "unsigned long";
533 return floatOutputTypes
[td
->size
];
537 return uintOutputTypes
[td
->size
];
543 return intOutputTypes
[t
->size
];
545 return uintOutputTypes
[t
->size
];
551 return "unsigned long";
559 return floatOutputTypes
[t
->size
];
563 return uintOutputTypes
[t
->size
];
565 error_callback(NULL
,"Nested struct is not supportted");
569 case STRUCT
: //for now we do not support nested struct
570 error_callback(NULL
,"Nested struct is not supportted");
573 error_callback(NULL
,"No type information");
579 /*****************************************************************************
581 * generateLoaderfile: generate a facility loaded .h file
583 * fp : file to be written to
584 * facName : name of facility
585 * nbEvent : number of events in the facility
586 * checksum : checksum for the facility
587 ****************************************************************************/
588 void generateLoaderfile(FILE * fp
, char * facName
, int nbEvent
, unsigned long checksum
, char *capname
){
589 fprintf(fp
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n",capname
);
590 fprintf(fp
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n",capname
);
591 fprintf(fp
,"#include <linux/ltt-facilities.h>\n", facName
, checksum
);
592 fprintf(fp
,"#include <linux/module.h>\n\n", facName
, checksum
);
593 fprintf(fp
,"ltt_facility_t\tltt_facility_%s;\n", facName
, checksum
);
594 fprintf(fp
,"ltt_facility_t\tltt_facility_%s_%X;\n\n", facName
, checksum
);
596 fprintf(fp
,"EXPORT_SYMBOL(ltt_facility_%s);\n\n",facName
, checksum
);
597 fprintf(fp
,"EXPORT_SYMBOL(ltt_facility_%s_%X);\n\n",facName
, checksum
);
598 fprintf(fp
,"#define LTT_FACILITY_SYMBOL\t\t\t\tltt_facility_%s\n",
600 fprintf(fp
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\t\t\tltt_facility_%s_%X\n",
602 fprintf(fp
,"#define LTT_FACILITY_CHECKSUM\t\t\t0x%X\n", checksum
);
603 fprintf(fp
,"#define LTT_FACILITY_NAME\t\t\t\t\t\"%s\"\n", facName
);
604 fprintf(fp
,"#define LTT_FACILITY_NUM_EVENTS\t\t%d\n\n", nbEvent
);
605 fprintf(fp
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n",capname
);
This page took 0.078817 seconds and 4 git commands to generate.