add libltt
[lttv.git] / genevent / genevent.c
CommitLineData
3888436c 1/*
2
3genevent.c: Generate helper declarations and functions to trace events
4 from an event description file.
5
6Copyright (C) 2002, Xianxiu Yang
7Copyright (C) 2002, Michel Dagenais
8
9This 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
11the Free Software Foundation; version 2 of the License.
12
13This 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.
17
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
21*/
22
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.
26
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
30 definitions.
31
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. */
36
37#include <stdlib.h>
38#include <string.h>
31cbc5d3 39#include <ctype.h>
3888436c 40#include <stdio.h>
41#include <stdarg.h>
42#include <linux/errno.h>
d7ed29cd 43#include <assert.h>
3888436c 44
45#include "parser.h"
46#include "genevent.h"
47
48/* Named types may be referenced from anywhere */
49
50facility * fac;
51
52int main(int argc, char** argv)
53{
54 char *token;
55 parse_file in;
56 char buffer[BUFFER_SIZE];
57 int i;
58
59 if(argc < 2){
60 printf("At least one event definition file is needed\n");
61 exit(1);
62 }
63
64 in.buffer = buffer;
65 in.error = error_callback;
66
67 for(i = 1 ; i < argc ; i++) {
68 in.lineno = 0;
69 in.name = allocAndCopy(argv[i]);
70
71 in.fp = fopen(in.name, "r");
72 if(!in.fp ){
73 in.error(&in,"cannot open facility input file");
74 }
75
76 while(1){
77 token = getToken(&in);
78 if(in.type == ENDFILE) break;
79
80 if(strcmp(token, "<")) in.error(&in,"not a facility file");
81 token = getName(&in);
82
83 if(strcmp("facility",token) == 0) {
a8f6f123 84 fac = memAlloc(sizeof(facility));
85 fac->name = NULL;
86 fac->description = NULL;
87 sequence_init(&(fac->events));
88 table_init(&(fac->named_types));
89 sequence_init(&(fac->unnamed_types));
90
91 parseFacility(&in, fac);
92
93 //check if any namedType is not defined
94 checkNamedTypesImplemented(&fac->named_types);
31cbc5d3 95 }
a8f6f123 96 else in.error(&in,"facility token was expected");
3888436c 97
98 generateFile(argv[i]);
99
100 free(fac->name);
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);
108 free(fac);
109 }
110
111 free(in.name);
112 fclose(in.fp);
113
114 }
115 return 0;
116}
117
118
119/*****************************************************************************
120 *Function name
121 * generateFile : generate .c and .h file
122 *Input Params
123 * name : name of event definition file
124 ****************************************************************************/
125void generateFile(char *name){
a8f6f123 126 char *loadName, *hName, *hIdName, *cName, *tmp, *tmp2;
127 FILE * lFp, *hFp, *iFp, *cFp;
3888436c 128 int nbEvent;
129 unsigned long checksum=0;
130
131 //remove .xml if it exists
132 tmp = &name[strlen(name)-4];
133 if(strcmp(tmp, ".xml") == 0){
134 *tmp = '\0';
135 }
136
137 tmp = strrchr(name,'/');
138 if(tmp){
139 tmp++;
140 }else{
141 tmp = name;
142 }
6d387597 143
144 loadName = appendString("ltt-facility-loader-", tmp);
145 tmp2 = appendString(loadName,".h");
146 free(loadName);
147 loadName = tmp2;
148 hName = appendString("ltt-facility-", tmp);
149 tmp2 = appendString(hName,".h");
150 free(hName);
151 hName = tmp2;
a8f6f123 152 hIdName = appendString("ltt-facility-id-", tmp);
153 tmp2 = appendString(hIdName,".h");
154 free(hIdName);
155 hIdName = tmp2;
156 cName = appendString("ltt-facility-loader-", tmp);
157 tmp2 = appendString(cName,".c");
158 free(cName);
159 cName = tmp2;
6d387597 160 lFp = fopen(loadName,"w");
161 if(!lFp){
162 printf("Cannot open the file : %s\n",loadName);
3888436c 163 exit(1);
164 }
165
166 hFp = fopen(hName,"w");
167 if(!hFp){
168 printf("Cannot open the file : %s\n",hName);
169 exit(1);
170 }
a8f6f123 171
172 iFp = fopen(hIdName,"w");
173 if(!iFp){
174 printf("Cannot open the file : %s\n",hIdName);
175 exit(1);
176 }
177
178 cFp = fopen(cName,"w");
179 if(!cFp){
180 printf("Cannot open the file : %s\n",cName);
181 exit(1);
182 }
3888436c 183
6d387597 184 free(loadName);
3888436c 185 free(hName);
a8f6f123 186 free(hIdName);
187 free(cName);
3888436c 188
189 generateChecksum(fac->name, &checksum, &(fac->events));
190
191 /* generate .h file, event enumeration then structures and functions */
a8f6f123 192 fprintf(iFp, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname);
193 fprintf(iFp, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname);
194 generateEnumEvent(iFp, fac->name, &nbEvent, checksum);
195 fprintf(iFp, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname);
196
197
198 fprintf(hFp, "#ifndef _LTT_FACILITY_%s_H_\n",fac->capname);
199 fprintf(hFp, "#define _LTT_FACILITY_%s_H_\n\n",fac->capname);
200 generateTypeDefs(hFp, fac->name);
31cbc5d3 201 generateStructFunc(hFp, fac->name,checksum);
a8f6f123 202 fprintf(hFp, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
3888436c 203
6d387597 204 /* generate .h file, calls to register the facility at init time */
8c6ca411 205 generateLoaderfile(lFp,fac->name,nbEvent,checksum,fac->capname);
3888436c 206
a8f6f123 207 // create ltt-facility-loader-facname.c
208 generateCfile(cFp, tmp);
209
3888436c 210 fclose(hFp);
a8f6f123 211 fclose(iFp);
6d387597 212 fclose(lFp);
a8f6f123 213 fclose(cFp);
214
3888436c 215}
216
217
218/*****************************************************************************
219 *Function name
220 * generateEnumEvent : output event enum to .h file
221 *Input Params
222 * fp : file to be written to
223 * facName : name of facility
224 *Output Params
225 * nbEvent : number of events in the facility
226 ****************************************************************************/
31cbc5d3 227void generateEnumEvent(FILE *fp, char *facName, int * nbEvent, unsigned long checksum) {
3888436c 228 int pos = 0;
229
a8f6f123 230 fprintf(fp,"#include <linux/ltt-facilities.h>\n\n");
3888436c 231
232 fprintf(fp,"/**** facility handle ****/\n\n");
a8f6f123 233 fprintf(fp,"extern ltt_facility_t ltt_facility_%s_%X;\n",facName, checksum);
234 fprintf(fp,"extern ltt_facility_t ltt_facility_%s;\n\n\n",facName, checksum);
3888436c 235
236 fprintf(fp,"/**** event type ****/\n\n");
237 fprintf(fp,"enum %s_event {\n",facName);
238
239 for(pos = 0; pos < fac->events.position;pos++) {
a8f6f123 240 fprintf(fp,"\tevent_%s", ((event *)(fac->events.array[pos]))->name);
3888436c 241 if(pos != fac->events.position-1) fprintf(fp,",\n");
242 }
243 fprintf(fp,"\n};\n\n\n");
244
245 // fprintf(fp,"/**** number of events in the facility ****/\n\n");
246 // fprintf(fp,"int nbEvents_%s = %d;\n\n\n",facName, fac->events.position);
247 *nbEvent = fac->events.position;
248}
249
250
31cbc5d3 251/*****************************************************************************
252 *Function name
253 * printStruct : Generic struct printing function
254 *Input Params
255 * fp : file to be written to
256 * len : number of fields
257 * array : array of field info
258 * name : basic struct name
259 * facName : name of facility
260 * whichTypeFirst : struct or array/sequence first
261 * hasStrSeq : string or sequence present?
262 * structCount : struct postfix
263 ****************************************************************************/
cb1eb7ce 264
31cbc5d3 265static void
266printStruct(FILE * fp, int len, void ** array, char * name, char * facName,
a8f6f123 267 int * whichTypeFirst, int * hasStrSeq, int * structCount)
31cbc5d3 268{
269 int flag = 0;
270 int pos;
271 field * fld;
272 type_descriptor * td;
273
274 for (pos = 0; pos < len; pos++) {
275 fld = (field *)array[pos];
276 td = fld->type;
a8f6f123 277 if( td->type == STRING || td->type == SEQUENCE ||
278 td->type == ARRAY) {
279 (*hasStrSeq)++;
280 }
281// if (*whichTypeFirst == 0) {
282// *whichTypeFirst = 1; //struct first
283// }
31cbc5d3 284 if (flag == 0) {
285 flag = 1;
286
cb1eb7ce 287 fprintf(fp,"struct %s_%s",name, facName);
288 if (structCount) {
a8f6f123 289 fprintf(fp, "_%d {\n",++*structCount);
cb1eb7ce 290 } else {
291 fprintf(fp, " {\n");
292 }
31cbc5d3 293 }
cb1eb7ce 294 fprintf(fp, "\t%s %s; /* %s */\n",
295 getTypeStr(td),fld->name,fld->description );
a8f6f123 296#if 0
31cbc5d3 297 } else {
cb1eb7ce 298 if (*whichTypeFirst == 0) {
31cbc5d3 299 //string or sequence or array first
cb1eb7ce 300 *whichTypeFirst = 2;
301 }
302 (*hasStrSeq)++;
303 if(flag) {
304 fprintf(fp,"} __attribute__ ((packed));\n\n");
305 }
306 flag = 0;
31cbc5d3 307 }
a8f6f123 308#endif //0
31cbc5d3 309 }
310
311 if(flag) {
312 fprintf(fp,"} __attribute__ ((packed));\n\n");
313 }
314}
315
316
317/*****************************************************************************
318 *Function name
319 * generateHfile : Create the typedefs
320 *Input Params
321 * fp : file to be written to
322 ****************************************************************************/
323void
a8f6f123 324generateTypeDefs(FILE * fp, char *facName)
31cbc5d3 325{
326 int pos, tmp = 1;
327
a8f6f123 328 fprintf(fp,"#include <linux/types.h>\n");
329 fprintf(fp,"#include <linux/spinlock.h>\n");
330 fprintf(fp,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n", facName);
331 fprintf(fp,"#include <linux/ltt-core.h>\n");
332
31cbc5d3 333 fprintf(fp, "/**** Basic Type Definitions ****/\n\n");
334
335 for (pos = 0; pos < fac->named_types.values.position; pos++) {
336 type_descriptor * type =
337 (type_descriptor*)fac->named_types.values.array[pos];
338 printStruct(fp, type->fields.position, type->fields.array,
339 "", type->type_name, &tmp, &tmp, NULL);
340 fprintf(fp, "typedef struct _%s %s;\n\n",
341 type->type_name, type->type_name);
342 }
343}
344
345
3888436c 346/*****************************************************************************
347 *Function name
348 * generateEnumDefinition: generate enum definition if it exists
349 *Input Params
350 * fp : file to be written to
351 * fHead : enum type
352 ****************************************************************************/
353void generateEnumDefinition(FILE * fp, type_descriptor * type){
354 int pos;
355
a8f6f123 356 if(type->already_printed) return;
357
3888436c 358 fprintf(fp,"enum {\n");
359 for(pos = 0; pos < type->labels.position; pos++){
a8f6f123 360 fprintf(fp,"\tLTT_ENUM_%s", type->labels.array[pos]);
8c6ca411 361 if (pos != type->labels.position - 1) fprintf(fp,",");
a8f6f123 362 if(type->labels_description.array[pos] != NULL)
363 fprintf(fp,"\t/* %s */\n",type->labels_description.array[pos]);
364 else
365 fprintf(fp,"\n");
3888436c 366 }
8c6ca411 367 fprintf(fp,"};\n\n\n");
44cac8a9 368
a8f6f123 369 type->already_printed = 1;
3888436c 370}
371
372/*****************************************************************************
373 *Function name
374 * generateStrucTFunc: output structure and function to .h file
375 *Input Params
376 * fp : file to be written to
377 * facName : name of facility
378 ****************************************************************************/
31cbc5d3 379void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
3888436c 380 event * ev;
381 field * fld;
382 type_descriptor * td;
383 int pos, pos1;
384 int hasStrSeq, flag, structCount, seqCount,strCount, whichTypeFirst=0;
385
386 for(pos = 0; pos < fac->events.position; pos++){
387 ev = (event *) fac->events.array[pos];
388 //yxx if(ev->nested)continue;
d7ed29cd 389 fprintf(fp,"/**** structure and trace function for event: %s ****/\n\n",
390 ev->name);
a8f6f123 391 //if(ev->type == 0){ // event without type
392 // fprintf(fp,"static inline void trace_%s_%s(void){\n",facName,ev->name);
393 // fprintf(fp,"\tltt_log_event(ltt_facility_%s_%X, event_%s, 0, NULL);\n",
394 // facName,checksum,ev->name);
395 // fprintf(fp,"};\n\n\n");
396 // continue;
397 //}
3888436c 398
399 //if fields contain enum, print out enum definition
d7ed29cd 400 //MD : fixed in generateEnumDefinition to do not print the same enum
401 //twice.
a8f6f123 402 if(ev->type != 0)
403 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
404 fld = (field *)ev->type->fields.array[pos1];
405 if(fld->type->type == ENUM) generateEnumDefinition(fp, fld->type);
406 }
3888436c 407
408 //default: no string, array or sequence in the event
409 hasStrSeq = 0;
410 whichTypeFirst = 0;
3888436c 411 structCount = 0;
31cbc5d3 412
413 //structure for kernel
a8f6f123 414 if(ev->type != 0)
415 printStruct(fp, ev->type->fields.position, ev->type->fields.array,
416 ev->name, facName, &whichTypeFirst, &hasStrSeq, &structCount);
3888436c 417
418 //trace function : function name and parameters
419 seqCount = 0;
420 strCount = 0;
421 fprintf(fp,"static inline void trace_%s_%s(",facName,ev->name);
a8f6f123 422 if(ev->type == 0)
423 fprintf(fp, "void");
424 else
425 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
426 fld = (field *)ev->type->fields.array[pos1];
427 td = fld->type;
428 if(td->type == ARRAY ){
429 fprintf(fp,"%s * %s",getTypeStr(td), fld->name);
430 }else if(td->type == STRING){
431 fprintf(fp,"short int strlength_%d, %s * %s",
432 ++strCount, getTypeStr(td), fld->name);
433 }else if(td->type == SEQUENCE){
434 fprintf(fp,"%s seqlength_%d, %s * %s",
435 uintOutputTypes[td->size], ++seqCount,getTypeStr(td), fld->name);
436 }else fprintf(fp,"%s %s",getTypeStr(td), fld->name);
437 if(pos1 != ev->type->fields.position - 1) fprintf(fp,", ");
438 }
d7ed29cd 439 fprintf(fp,")\n{\n");
3888436c 440
441 //length of buffer : length of all structures
a8f6f123 442 fprintf(fp,"\tint length = ");
443 if(ev->type == 0) fprintf(fp, "0");
444
3888436c 445 for(pos1=0;pos1<structCount;pos1++){
446 fprintf(fp,"sizeof(struct %s_%s_%d)",ev->name, facName,pos1+1);
447 if(pos1 != structCount-1) fprintf(fp," + ");
448 }
449
450 //length of buffer : length of all arrays, sequences and strings
451 seqCount = 0;
452 strCount = 0;
453 flag = 0;
a8f6f123 454 if(ev->type != 0)
455 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
456 fld = (field *)ev->type->fields.array[pos1];
457 td = fld->type;
458 if(td->type == SEQUENCE || td->type==STRING || td->type==ARRAY){
459 if(structCount || flag > 0) fprintf(fp," + ");
460 if(td->type == SEQUENCE)
461 fprintf(fp,"sizeof(%s) + sizeof(%s) * seqlength_%d",
462 uintOutputTypes[td->size], getTypeStr(td), ++seqCount);
463 else if(td->type==STRING) fprintf(fp,"strlength_%d + 1", ++strCount);
464 else if(td->type==ARRAY)
465 fprintf(fp,"sizeof(%s) * %d", getTypeStr(td),td->size);
466 if(structCount == 0) flag = 1;
467 }
468 }
3888436c 469 fprintf(fp,";\n");
470
471 //allocate buffer
d7ed29cd 472 // MD no more need. fprintf(fp,"\tchar buff[buflength];\n");
473 // write directly to the channel
474 fprintf(fp, "\tunsigned int index;\n");
475 fprintf(fp, "\tstruct ltt_channel_struct *channel;\n");
476 fprintf(fp, "\tstruct ltt_trace_struct *trace;\n");
a8f6f123 477 fprintf(fp, "\tunsigned long _flags;\n");
478 if(ev->type != 0)
479 fprintf(fp, "\tstruct %s_%s_1* __1;\n\n", ev->name, facName);
d7ed29cd 480
a8f6f123 481 fprintf(fp, "\tread_lock(&ltt_traces.traces_rwlock);\n\n");
482 fprintf(fp,
483 "\tif(ltt_traces.num_active_traces == 0) goto unlock_traces;\n\n");
d7ed29cd 484
485 fprintf(fp,
486 "\tindex = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
a8f6f123 487 "\t\t\t\tevent_%s);\n",
d7ed29cd 488 facName, checksum, ev->name);
489 fprintf(fp,"\n");
490
491 fprintf(fp, "\t/* Disable interrupts. */\n");
a8f6f123 492 fprintf(fp, "\tlocal_irq_save(_flags);\n\n");
3888436c 493
a8f6f123 494 /* For each trace */
d7ed29cd 495 fprintf(fp, "\tlist_for_each_entry(trace, &ltt_traces.head, list) {\n");
a8f6f123 496 fprintf(fp, "\t\tif(!trace->active) continue;\n\n");
497
498 fprintf(fp, "\t\tunsigned int header_length = "
499 "ltt_get_event_header_size(trace);\n");
500 fprintf(fp, "\t\tunsigned int event_length = header_length + length;\n");
d7ed29cd 501
502 /* Reserve the channel */
503 fprintf(fp, "\t\tchannel = ltt_get_channel_from_index(trace, index);\n");
504 fprintf(fp,
a8f6f123 505 "\t\tvoid *buff = relay_reserve(channel->rchan, event_length);\n");
506 fprintf(fp, "\t\tif(buff == NULL) {\n");
507 fprintf(fp, "\t\t\t/* Buffer is full*/\n");
508 fprintf(fp, "\t\t\t/* for debug BUG(); */\n"); // DEBUG!
509 fprintf(fp, "\t\t\tchannel->events_lost[smp_processor_id()]++;\n");
510 fprintf(fp, "\t\t\tgoto commit_work;\n");
511 fprintf(fp, "\t\t}\n");
d7ed29cd 512
a8f6f123 513 /* DEBUG */
514 fprintf(fp, "/* for debug printk(\"f%%lu e\%%u \", ltt_facility_%s_%X, event_%s); */",
515 facName, checksum, ev->name);
516
517 /* Write the header */
518 fprintf(fp, "\n");
519 fprintf(fp, "\t\tltt_write_event_header(trace, channel, buff, \n"
520 "\t\t\t\tltt_facility_%s_%X, event_%s, length);\n",
521 facName, checksum, ev->name);
522 fprintf(fp, "\n");
523
d7ed29cd 524 //declare a char pointer if needed : starts at the end of the structs.
525 if(structCount + hasStrSeq > 1) {
526 fprintf(fp,"\t\tchar * ptr = (char*)buff + header_length");
527 for(pos1=0;pos1<structCount;pos1++){
528 fprintf(fp," + sizeof(struct %s_%s_%d)",ev->name, facName,pos1+1);
529 }
530 if(structCount + hasStrSeq > 1) fprintf(fp,";\n");
3888436c 531 }
d7ed29cd 532
533 // Declare an alias pointer of the struct type to the beginning
534 // of the reserved area, just after the event header.
a8f6f123 535 if(ev->type != 0)
536 fprintf(fp, "\t\t__1 = (struct %s_%s_1 *)(buff + header_length);\n",
537 ev->name, facName);
d7ed29cd 538 //allocate memory for new struct and initialize it
539 //if(whichTypeFirst == 1){ //struct first
a8f6f123 540 //for(pos1=0;pos1<structCount;pos1++){
541 // if(pos1==0) fprintf(fp,
d7ed29cd 542 // "\tstruct %s_%s_1 * __1 = (struct %s_%s_1 *)buff;\n",
543 // ev->name, facName,ev->name, facName);
544 //MD disabled else fprintf(fp,
545 // "\tstruct %s_%s_%d __%d;\n",
546 // ev->name, facName,pos1+1,pos1+1);
547 //}
548 //}else if(whichTypeFirst == 2){
549 // for(pos1=0;pos1<structCount;pos1++)
a8f6f123 550 // fprintf(fp,"\tstruct %s_%s_%d __%d;\n",
d7ed29cd 551 // ev->name, facName,pos1+1,pos1+1);
552 //}
3888436c 553 fprintf(fp,"\n");
554
d7ed29cd 555 if(structCount) fprintf(fp,"\t\t//initialize structs\n");
556 //flag = 0;
557 //structCount = 0;
a8f6f123 558 if(ev->type != 0)
559 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
560 fld = (field *)ev->type->fields.array[pos1];
561 td = fld->type;
562 if(td->type != ARRAY && td->type != SEQUENCE && td->type != STRING){
563 //if(flag == 0){
564 // flag = 1;
565 // structCount++;
566 // if(structCount > 1) fprintf(fp,"\n");
567 //}
568 fprintf(fp, "\t\t__1->%s = %s;\n", fld->name, fld->name );
569
570 //if(structCount == 1 && whichTypeFirst == 1)
571 // fprintf(fp, "\t__1->%s = %s;\n",fld->name,fld->name );
572 //else
573 // fprintf(fp, "\t__%d.%s = %s;\n",structCount ,fld->name,fld->name);
574 }
575 //else flag = 0;
576 }
577 if(structCount) fprintf(fp,"\n");
3888436c 578 //set ptr to the end of first struct if needed;
a8f6f123 579 if(structCount + hasStrSeq > 1){
580 fprintf(fp,"\n\t\t//set ptr to the end of the first struct\n");
581 fprintf(fp,"\t\tptr += sizeof(struct %s_%s_1);\n\n",ev->name, facName);
582 }
3888436c 583
584 //copy struct, sequence and string to buffer
585 seqCount = 0;
586 strCount = 0;
587 flag = 0;
588 structCount = 0;
a8f6f123 589 if(ev->type != 0)
590 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
591 fld = (field *)ev->type->fields.array[pos1];
592 td = fld->type;
593 // if(td->type != STRING && td->type != SEQUENCE && td->type != ARRAY){
594 // if(flag == 0) structCount++;
595 // flag++;
596 // if((structCount > 1 || whichTypeFirst == 2) && flag == 1){
597 // assert(0); // MD : disabled !
598 // fprintf(fp,"\t//copy struct to buffer\n");
599 // fprintf(fp,"\tmemcpy(ptr, &__%d, sizeof(struct %s_%s_%d));\n",
600 // structCount, ev->name, facName,structCount);
601 // fprintf(fp,"\tptr += sizeof(struct %s_%s_%d);\n\n",
602 // ev->name, facName,structCount);
603 // }
604 // }
605 //else if(td->type == SEQUENCE){
606 if(td->type == SEQUENCE){
607 flag = 0;
608 fprintf(fp,"\t\t//copy sequence length and sequence to buffer\n");
609 fprintf(fp,"\t\t*ptr = seqlength_%d;\n",++seqCount);
610 fprintf(fp,"\t\tptr += sizeof(%s);\n",uintOutputTypes[td->size]);
611 fprintf(fp,"\t\tmemcpy(ptr, %s, sizeof(%s) * seqlength_%d);\n",
612 fld->name, getTypeStr(td), seqCount);
613 fprintf(fp,"\t\tptr += sizeof(%s) * seqlength_%d;\n\n",
614 getTypeStr(td), seqCount);
615 }
616 else if(td->type==STRING){
617 flag = 0;
618 fprintf(fp,"\t\t//copy string to buffer\n");
619 fprintf(fp,"\t\tif(strlength_%d > 0){\n",++strCount);
620 fprintf(fp,"\t\t\tmemcpy(ptr, %s, strlength_%d + 1);\n",
621 fld->name, strCount);
622 fprintf(fp,"\t\t\tptr += strlength_%d + 1;\n",strCount);
623 fprintf(fp,"\t\t}else{\n");
624 fprintf(fp,"\t\t\t*ptr = '\\0';\n");
625 fprintf(fp,"\t\t\tptr += 1;\n");
626 fprintf(fp,"\t\t}\n\n");
627 }else if(td->type==ARRAY){
628 flag = 0;
629 fprintf(fp,"\t//copy array to buffer\n");
630 fprintf(fp,"\tmemcpy(ptr, %s, sizeof(%s) * %d);\n",
631 fld->name, getTypeStr(td), td->size);
632 fprintf(fp,"\tptr += sizeof(%s) * %d;\n\n", getTypeStr(td), td->size);
633 }
634 }
3888436c 635 if(structCount + seqCount > 1) fprintf(fp,"\n");
636
d7ed29cd 637 fprintf(fp,"\n");
638 fprintf(fp,"commit_work:\n");
639 fprintf(fp,"\n");
640 fprintf(fp, "\t\t/* Commit the work */\n");
a8f6f123 641 fprintf(fp, "\t\trelay_commit(channel->rchan->buf[smp_processor_id()],\n"
642 "\t\t\t\tbuff, event_length);\n");
d7ed29cd 643
644 /* End of traces iteration */
645 fprintf(fp, "\t}\n\n");
646
647 fprintf(fp, "\t/* Re-enable interrupts */\n");
a8f6f123 648 fprintf(fp, "\tlocal_irq_restore(_flags);\n");
d7ed29cd 649 fprintf(fp, "\tpreempt_check_resched();\n");
a8f6f123 650
651 fprintf(fp, "\n");
5cbe87a9 652 fprintf(fp, "unlock_traces:\n");
d7ed29cd 653 fprintf(fp, "\tread_unlock(&ltt_traces.traces_rwlock);\n");
3888436c 654 //call trace function
d7ed29cd 655 //fprintf(fp,"\n\t//call trace function\n");
656 //fprintf(fp,"\tltt_log_event(ltt_facility_%s_%X, %s, bufLength, buff);\n",facName,checksum,ev->name);
a8f6f123 657 fprintf(fp,"}\n\n\n");
3888436c 658 }
659
660}
661
662/*****************************************************************************
663 *Function name
664 * getTypeStr : generate type string
665 *Input Params
666 * td : a type descriptor
667 *Return Values
668 * char * : type string
669 ****************************************************************************/
670char * getTypeStr(type_descriptor * td){
671 type_descriptor * t ;
672
673 switch(td->type){
674 case INT:
675 return intOutputTypes[td->size];
676 case UINT:
677 return uintOutputTypes[td->size];
31cbc5d3 678 case POINTER:
679 return "void *";
680 case LONG:
681 return "long";
682 case ULONG:
683 return "unsigned long";
684 case SIZE_T:
685 return "size_t";
686 case SSIZE_T:
687 return "ssize_t";
688 case OFF_T:
689 return "off_t";
3888436c 690 case FLOAT:
691 return floatOutputTypes[td->size];
692 case STRING:
a8f6f123 693 return "const char";
3888436c 694 case ENUM:
695 return uintOutputTypes[td->size];
696 case ARRAY:
697 case SEQUENCE:
698 t = td->nested_type;
699 switch(t->type){
700 case INT:
a8f6f123 701 return intOutputTypes[t->size];
3888436c 702 case UINT:
a8f6f123 703 return uintOutputTypes[t->size];
31cbc5d3 704 case POINTER:
705 return "void *";
706 case LONG:
707 return "long";
708 case ULONG:
709 return "unsigned long";
710 case SIZE_T:
711 return "size_t";
712 case SSIZE_T:
713 return "ssize_t";
714 case OFF_T:
715 return "off_t";
3888436c 716 case FLOAT:
a8f6f123 717 return floatOutputTypes[t->size];
3888436c 718 case STRING:
a8f6f123 719 return "const char";
3888436c 720 case ENUM:
a8f6f123 721 return uintOutputTypes[t->size];
3888436c 722 default :
a8f6f123 723 error_callback(NULL,"Nested struct is not supportted");
724 break;
3888436c 725 }
726 break;
727 case STRUCT: //for now we do not support nested struct
728 error_callback(NULL,"Nested struct is not supportted");
729 break;
730 default:
731 error_callback(NULL,"No type information");
732 break;
733 }
734 return NULL;
735}
736
737/*****************************************************************************
738 *Function name
6d387597 739 * generateLoaderfile: generate a facility loaded .h file
3888436c 740 *Input Params
741 * fp : file to be written to
742 * facName : name of facility
743 * nbEvent : number of events in the facility
744 * checksum : checksum for the facility
745 ****************************************************************************/
8c6ca411 746void generateLoaderfile(FILE * fp, char * facName, int nbEvent, unsigned long checksum, char *capname){
a8f6f123 747 fprintf(fp, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n",capname);
748 fprintf(fp, "#define _LTT_FACILITY_LOADER_%s_H_\n\n",capname);
8c6ca411 749 fprintf(fp,"#include <linux/ltt-facilities.h>\n", facName, checksum);
44cac8a9 750 fprintf(fp,"ltt_facility_t\tltt_facility_%s;\n", facName, checksum);
6d387597 751 fprintf(fp,"ltt_facility_t\tltt_facility_%s_%X;\n\n", facName, checksum);
752
a8f6f123 753 fprintf(fp,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
44cac8a9 754 facName);
a8f6f123 755 fprintf(fp,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
6d387597 756 facName, checksum);
a8f6f123 757 fprintf(fp,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", checksum);
758 fprintf(fp,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", facName);
759 fprintf(fp,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\t%d\n\n", nbEvent);
760 fprintf(fp, "#endif //_LTT_FACILITY_LOADER_%s_H_\n",capname);
761}
762
763void generateCfile(FILE * fp, char * filefacname){
764
765 fprintf(fp, "/*\n");
766 fprintf(fp, " * ltt-facility-loader-%s.c\n", filefacname);
767 fprintf(fp, " *\n");
768 fprintf(fp, " * (C) Copyright 2005 - \n");
769 fprintf(fp, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
770 fprintf(fp, " *\n");
771 fprintf(fp, " * Contains the LTT facility loader.\n");
772 fprintf(fp, " *\n");
773 fprintf(fp, " */\n");
774 fprintf(fp, "\n");
775 fprintf(fp, "\n");
776 fprintf(fp, "#include <linux/ltt-facilities.h>\n");
777 fprintf(fp, "#include <linux/module.h>\n");
778 fprintf(fp, "#include <linux/init.h>\n");
779 fprintf(fp, "#include <linux/config.h>\n");
780 fprintf(fp, "#include \"ltt-facility-loader-%s.h\"\n", filefacname);
781 fprintf(fp, "\n");
782 fprintf(fp, "\n");
783 fprintf(fp, "#ifdef CONFIG_LTT\n");
784 fprintf(fp, "\n");
785 fprintf(fp, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
786 fprintf(fp, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
787 fprintf(fp, "\n");
788 fprintf(fp, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
789 fprintf(fp, "\n");
790 fprintf(fp, "#define SYMBOL_STRING(sym) #sym\n");
791 fprintf(fp, "\n");
792 fprintf(fp, "static struct ltt_facility facility = {\n");
793 fprintf(fp, "\t.name = ltt_facility_name,\n");
794 fprintf(fp, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
795 fprintf(fp, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
796 fprintf(fp, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL)\n");
797 fprintf(fp, "};\n");
798 fprintf(fp, "\n");
799 fprintf(fp, "#ifndef MODULE\n");
800 fprintf(fp, "\n");
801 fprintf(fp, "/* Built-in facility. */\n");
802 fprintf(fp, "\n");
803 fprintf(fp, "static int __init facility_init(void)\n");
804 fprintf(fp, "{\n");
805 fprintf(fp, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", filefacname);
806 fprintf(fp, "\n");
807 fprintf(fp, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n");
808 fprintf(fp, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
809 fprintf(fp, "\t\n");
810 fprintf(fp, "\treturn LTT_FACILITY_SYMBOL;\n");
811 fprintf(fp, "}\n");
812 fprintf(fp, "__initcall(facility_init);\n");
813 fprintf(fp, "\n");
814 fprintf(fp, "\n");
815 fprintf(fp, "\n");
816 fprintf(fp, "#else \n");
817 fprintf(fp, "\n");
818 fprintf(fp, "/* Dynamic facility. */\n");
819 fprintf(fp, "\n");
820 fprintf(fp, "static int __init facility_init(void)\n");
821 fprintf(fp, "{\n");
822 fprintf(fp, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", filefacname);
823 fprintf(fp, "\n");
824 fprintf(fp, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n");
825 fprintf(fp, "\tLTT_FACILITY_SYMBOL_CHECKSUM = LTT_FACILITY_SYMBOL;\n");
826 fprintf(fp, "\n");
827 fprintf(fp, "\treturn LTT_FACILITY_SYMBOL;\n");
828 fprintf(fp, "}\n");
829 fprintf(fp, "\n");
830 fprintf(fp, "static void __exit facility_exit(void)\n");
831 fprintf(fp, "{\n");
832 fprintf(fp, "\tint err;\n");
833 fprintf(fp, "\n");
834 fprintf(fp, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n");
835 fprintf(fp, "\tif(err != 0)\n");
836 fprintf(fp, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
837 fprintf(fp, "\n");
838 fprintf(fp, "}\n");
839 fprintf(fp, "\n");
840 fprintf(fp, "module_init(facility_init)\n");
841 fprintf(fp, "module_exit(facility_exit)\n");
842 fprintf(fp, "\n");
843 fprintf(fp, "\n");
844 fprintf(fp, "MODULE_LICENSE(\"GPL\");\n");
845 fprintf(fp, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
846 fprintf(fp, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
847 fprintf(fp, "\n");
848 fprintf(fp, "#endif //MODULE\n");
849 fprintf(fp, "\n");
850 fprintf(fp, "#endif //CONFIG_LTT\n");
3888436c 851}
852
853
This page took 0.06115 seconds and 4 git commands to generate.