filter fix
[lttv.git] / genevent / genevent.c
1 /*
2
3 genevent.c: Generate helper declarations and functions to trace events
4 from an event description file.
5
6 Copyright (C) 2002, Xianxiu Yang
7 Copyright (C) 2002, Michel Dagenais
8
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.
12
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.
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>
39 #include <ctype.h>
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <linux/errno.h>
43 #include <assert.h>
44
45 #include "parser.h"
46 #include "genevent.h"
47
48 /* Named types may be referenced from anywhere */
49
50 facility * fac;
51
52 int 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) {
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);
95 }
96 else in.error(&in,"facility token was expected");
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 ****************************************************************************/
125 void generateFile(char *name){
126 char *loadName, *hName, *hIdName, *cName, *tmp, *tmp2;
127 FILE * lFp, *hFp, *iFp, *cFp;
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 }
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;
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;
160 lFp = fopen(loadName,"w");
161 if(!lFp){
162 printf("Cannot open the file : %s\n",loadName);
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 }
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 }
183
184 free(loadName);
185 free(hName);
186 free(hIdName);
187 free(cName);
188
189 generateChecksum(fac->name, &checksum, &(fac->events));
190
191 /* generate .h file, event enumeration then structures and functions */
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);
201 generateStructFunc(hFp, fac->name,checksum);
202 fprintf(hFp, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
203
204 /* generate .h file, calls to register the facility at init time */
205 generateLoaderfile(lFp,fac->name,nbEvent,checksum,fac->capname);
206
207 // create ltt-facility-loader-facname.c
208 generateCfile(cFp, tmp);
209
210 fclose(hFp);
211 fclose(iFp);
212 fclose(lFp);
213 fclose(cFp);
214
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 ****************************************************************************/
227 void generateEnumEvent(FILE *fp, char *facName, int * nbEvent, unsigned long checksum) {
228 int pos = 0;
229
230 fprintf(fp,"#include <linux/ltt-facilities.h>\n\n");
231
232 fprintf(fp,"/**** facility handle ****/\n\n");
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);
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++) {
240 fprintf(fp,"\tevent_%s", ((event *)(fac->events.array[pos]))->name);
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
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 ****************************************************************************/
264
265 static void
266 printStruct(FILE * fp, int len, void ** array, char * name, char * facName,
267 int * whichTypeFirst, int * hasStrSeq, int * structCount)
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;
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 // }
284 if (flag == 0) {
285 flag = 1;
286
287 fprintf(fp,"struct %s_%s",name, facName);
288 if (structCount) {
289 fprintf(fp, "_%d {\n",++*structCount);
290 } else {
291 fprintf(fp, " {\n");
292 }
293 }
294 fprintf(fp, "\t%s %s; /* %s */\n",
295 getTypeStr(td),fld->name,fld->description );
296 #if 0
297 } else {
298 if (*whichTypeFirst == 0) {
299 //string or sequence or array first
300 *whichTypeFirst = 2;
301 }
302 (*hasStrSeq)++;
303 if(flag) {
304 fprintf(fp,"} __attribute__ ((packed));\n\n");
305 }
306 flag = 0;
307 }
308 #endif //0
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 ****************************************************************************/
323 void
324 generateTypeDefs(FILE * fp, char *facName)
325 {
326 int pos, tmp = 1;
327
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 fprintf(fp,"#ifdef CONFIG_UML\n");
333 fprintf(fp,"#include \"irq_user.h\"\n");
334 fprintf(fp,"#endif //CONFIG_UML\n");
335
336 fprintf(fp, "/**** Basic Type Definitions ****/\n\n");
337
338 for (pos = 0; pos < fac->named_types.values.position; pos++) {
339 type_descriptor * type =
340 (type_descriptor*)fac->named_types.values.array[pos];
341 printStruct(fp, type->fields.position, type->fields.array,
342 "", type->type_name, &tmp, &tmp, NULL);
343 fprintf(fp, "typedef struct _%s %s;\n\n",
344 type->type_name, type->type_name);
345 }
346 }
347
348
349 /*****************************************************************************
350 *Function name
351 * generateEnumDefinition: generate enum definition if it exists
352 *Input Params
353 * fp : file to be written to
354 * fHead : enum type
355 ****************************************************************************/
356 void generateEnumDefinition(FILE * fp, type_descriptor * type){
357 int pos;
358
359 if(type->already_printed) return;
360
361 fprintf(fp,"enum {\n");
362 for(pos = 0; pos < type->labels.position; pos++){
363 fprintf(fp,"\tLTT_ENUM_%s", type->labels.array[pos]);
364 if (pos != type->labels.position - 1) fprintf(fp,",");
365 if(type->labels_description.array[pos] != NULL)
366 fprintf(fp,"\t/* %s */\n",type->labels_description.array[pos]);
367 else
368 fprintf(fp,"\n");
369 }
370 fprintf(fp,"};\n\n\n");
371
372 type->already_printed = 1;
373 }
374
375 /*****************************************************************************
376 *Function name
377 * generateStrucTFunc: output structure and function to .h file
378 *Input Params
379 * fp : file to be written to
380 * facName : name of facility
381 ****************************************************************************/
382 void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
383 event * ev;
384 field * fld;
385 type_descriptor * td;
386 int pos, pos1;
387 int hasStrSeq, flag, structCount, seqCount,strCount, whichTypeFirst=0;
388
389 for(pos = 0; pos < fac->events.position; pos++){
390 ev = (event *) fac->events.array[pos];
391 //yxx if(ev->nested)continue;
392 fprintf(fp,"/**** structure and trace function for event: %s ****/\n\n",
393 ev->name);
394 //if(ev->type == 0){ // event without type
395 // fprintf(fp,"static inline void trace_%s_%s(void){\n",facName,ev->name);
396 // fprintf(fp,"\tltt_log_event(ltt_facility_%s_%X, event_%s, 0, NULL);\n",
397 // facName,checksum,ev->name);
398 // fprintf(fp,"};\n\n\n");
399 // continue;
400 //}
401
402 //if fields contain enum, print out enum definition
403 //MD : fixed in generateEnumDefinition to do not print the same enum
404 //twice.
405 if(ev->type != 0)
406 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
407 fld = (field *)ev->type->fields.array[pos1];
408 if(fld->type->type == ENUM) generateEnumDefinition(fp, fld->type);
409 }
410
411 //default: no string, array or sequence in the event
412 hasStrSeq = 0;
413 whichTypeFirst = 0;
414 structCount = 0;
415
416 //structure for kernel
417 if(ev->type != 0)
418 printStruct(fp, ev->type->fields.position, ev->type->fields.array,
419 ev->name, facName, &whichTypeFirst, &hasStrSeq, &structCount);
420
421 //trace function : function name and parameters
422 seqCount = 0;
423 strCount = 0;
424 fprintf(fp,"static inline void trace_%s_%s(",facName,ev->name);
425 if(ev->type == 0)
426 fprintf(fp, "void");
427 else
428 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
429 fld = (field *)ev->type->fields.array[pos1];
430 td = fld->type;
431 if(td->type == ARRAY ){
432 fprintf(fp,"%s * %s",getTypeStr(td), fld->name);
433 }else if(td->type == STRING){
434 fprintf(fp,"short int strlength_%d, %s * %s",
435 ++strCount, getTypeStr(td), fld->name);
436 }else if(td->type == SEQUENCE){
437 fprintf(fp,"%s seqlength_%d, %s * %s",
438 uintOutputTypes[td->size], ++seqCount,getTypeStr(td), fld->name);
439 }else fprintf(fp,"%s %s",getTypeStr(td), fld->name);
440 if(pos1 != ev->type->fields.position - 1) fprintf(fp,", ");
441 }
442 fprintf(fp,")\n{\n");
443
444 //length of buffer : length of all structures
445 fprintf(fp,"\tint length = ");
446 if(ev->type == 0) fprintf(fp, "0");
447
448 for(pos1=0;pos1<structCount;pos1++){
449 fprintf(fp,"sizeof(struct %s_%s_%d)",ev->name, facName,pos1+1);
450 if(pos1 != structCount-1) fprintf(fp," + ");
451 }
452
453 //length of buffer : length of all arrays, sequences and strings
454 seqCount = 0;
455 strCount = 0;
456 flag = 0;
457 if(ev->type != 0)
458 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
459 fld = (field *)ev->type->fields.array[pos1];
460 td = fld->type;
461 if(td->type == SEQUENCE || td->type==STRING || td->type==ARRAY){
462 if(structCount || flag > 0) fprintf(fp," + ");
463 if(td->type == SEQUENCE)
464 fprintf(fp,"sizeof(%s) + sizeof(%s) * seqlength_%d",
465 uintOutputTypes[td->size], getTypeStr(td), ++seqCount);
466 else if(td->type==STRING) fprintf(fp,"strlength_%d + 1", ++strCount);
467 else if(td->type==ARRAY)
468 fprintf(fp,"sizeof(%s) * %d", getTypeStr(td),td->size);
469 if(structCount == 0) flag = 1;
470 }
471 }
472 fprintf(fp,";\n");
473
474 //allocate buffer
475 // MD no more need. fprintf(fp,"\tchar buff[buflength];\n");
476 // write directly to the channel
477 fprintf(fp, "\tunsigned int index;\n");
478 fprintf(fp, "\tstruct ltt_channel_struct *channel;\n");
479 fprintf(fp, "\tstruct ltt_trace_struct *trace;\n");
480 fprintf(fp, "\tunsigned long _flags;\n");
481 fprintf(fp, "\tvoid *buff;\n");
482 fprintf(fp, "\tunsigned int header_length;\n");
483 fprintf(fp, "\tunsigned int event_length;\n");
484 fprintf(fp, "\tint resret;\n");
485
486 if(ev->type != 0)
487 fprintf(fp, "\tstruct %s_%s_1* __1;\n\n", ev->name, facName);
488
489 fprintf(fp, "\t/* Disable interrupts. */\n");
490 fprintf(fp, "\tlocal_irq_save(_flags);\n");
491 fprintf(fp, "\tpreempt_disable();\n\n");
492
493 fprintf(fp, "\tltt_nesting[smp_processor_id()]++;\n");
494 fprintf(fp, "\tif(ltt_nesting[smp_processor_id] > 1) goto unlock;\n");
495 fprintf(fp, "\tspin_lock(&ltt_traces.locks[smp_processor_id()]);\n\n");
496
497 fprintf(fp,
498 "\tif(ltt_traces.num_active_traces == 0) goto unlock_traces;\n\n");
499
500 fprintf(fp,
501 "\tindex = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
502 "\t\t\t\tevent_%s);\n",
503 facName, checksum, ev->name);
504 fprintf(fp,"\n");
505
506 /* For each trace */
507 fprintf(fp, "\tlist_for_each_entry(trace, &ltt_traces.head, list) {\n");
508 fprintf(fp, "\t\tif(!trace->active) continue;\n\n");
509
510 fprintf(fp, "\t\theader_length = "
511 "ltt_get_event_header_size(trace);\n");
512 fprintf(fp, "\t\tevent_length = header_length + length;\n");
513
514 /* Reserve the channel */
515 fprintf(fp, "\t\tchannel = ltt_get_channel_from_index(trace, index);\n");
516 fprintf(fp, "\t\tbuff = relay_reserve(channel->rchan, event_length, &resret);\n");
517 fprintf(fp, "\t\tif(buff == NULL) {\n");
518 fprintf(fp, "\t\t\t/* Buffer is full*/\n");
519 fprintf(fp, "\t\t\t/* for debug BUG(); */\n"); // DEBUG!
520 fprintf(fp, "\t\t\tchannel->events_lost[smp_processor_id()]++;\n");
521 fprintf(fp, "\t\t\tbreak;\n"); /* don't commit a NULL reservation! */
522 fprintf(fp, "\t\t}\n");
523
524 /* DEBUG */
525 fprintf(fp, "\t\tif(resret == 1) {\n");
526 fprintf(fp, "printk(\"f%%lu e\%%u \", ltt_facility_%s_%X, event_%s);",
527 facName, checksum, ev->name);
528 fprintf(fp, "\t\t}\n");
529
530 /* Write the header */
531 fprintf(fp, "\n");
532 fprintf(fp, "\t\tltt_write_event_header(trace, channel, buff, \n"
533 "\t\t\t\tltt_facility_%s_%X, event_%s, length);\n",
534 facName, checksum, ev->name);
535 fprintf(fp, "\n");
536
537 //declare a char pointer if needed : starts at the end of the structs.
538 if(structCount + hasStrSeq > 1) {
539 fprintf(fp,"\t\tchar * ptr = (char*)buff + header_length");
540 for(pos1=0;pos1<structCount;pos1++){
541 fprintf(fp," + sizeof(struct %s_%s_%d)",ev->name, facName,pos1+1);
542 }
543 if(structCount + hasStrSeq > 1) fprintf(fp,";\n");
544 }
545
546 // Declare an alias pointer of the struct type to the beginning
547 // of the reserved area, just after the event header.
548 if(ev->type != 0)
549 fprintf(fp, "\t\t__1 = (struct %s_%s_1 *)(buff + header_length);\n",
550 ev->name, facName);
551 //allocate memory for new struct and initialize it
552 //if(whichTypeFirst == 1){ //struct first
553 //for(pos1=0;pos1<structCount;pos1++){
554 // if(pos1==0) fprintf(fp,
555 // "\tstruct %s_%s_1 * __1 = (struct %s_%s_1 *)buff;\n",
556 // ev->name, facName,ev->name, facName);
557 //MD disabled else fprintf(fp,
558 // "\tstruct %s_%s_%d __%d;\n",
559 // ev->name, facName,pos1+1,pos1+1);
560 //}
561 //}else if(whichTypeFirst == 2){
562 // for(pos1=0;pos1<structCount;pos1++)
563 // fprintf(fp,"\tstruct %s_%s_%d __%d;\n",
564 // ev->name, facName,pos1+1,pos1+1);
565 //}
566 fprintf(fp,"\n");
567
568 if(structCount) fprintf(fp,"\t\t//initialize structs\n");
569 //flag = 0;
570 //structCount = 0;
571 if(ev->type != 0)
572 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
573 fld = (field *)ev->type->fields.array[pos1];
574 td = fld->type;
575 if(td->type != ARRAY && td->type != SEQUENCE && td->type != STRING){
576 //if(flag == 0){
577 // flag = 1;
578 // structCount++;
579 // if(structCount > 1) fprintf(fp,"\n");
580 //}
581 fprintf(fp, "\t\t__1->%s = %s;\n", fld->name, fld->name );
582
583 //if(structCount == 1 && whichTypeFirst == 1)
584 // fprintf(fp, "\t__1->%s = %s;\n",fld->name,fld->name );
585 //else
586 // fprintf(fp, "\t__%d.%s = %s;\n",structCount ,fld->name,fld->name);
587 }
588 //else flag = 0;
589 }
590 if(structCount) fprintf(fp,"\n");
591 //set ptr to the end of first struct if needed;
592 if(structCount + hasStrSeq > 1){
593 fprintf(fp,"\n\t\t//set ptr to the end of the first struct\n");
594 fprintf(fp,"\t\tptr += sizeof(struct %s_%s_1);\n\n",ev->name, facName);
595 }
596
597 //copy struct, sequence and string to buffer
598 seqCount = 0;
599 strCount = 0;
600 flag = 0;
601 structCount = 0;
602 if(ev->type != 0)
603 for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
604 fld = (field *)ev->type->fields.array[pos1];
605 td = fld->type;
606 // if(td->type != STRING && td->type != SEQUENCE && td->type != ARRAY){
607 // if(flag == 0) structCount++;
608 // flag++;
609 // if((structCount > 1 || whichTypeFirst == 2) && flag == 1){
610 // assert(0); // MD : disabled !
611 // fprintf(fp,"\t//copy struct to buffer\n");
612 // fprintf(fp,"\tmemcpy(ptr, &__%d, sizeof(struct %s_%s_%d));\n",
613 // structCount, ev->name, facName,structCount);
614 // fprintf(fp,"\tptr += sizeof(struct %s_%s_%d);\n\n",
615 // ev->name, facName,structCount);
616 // }
617 // }
618 //else if(td->type == SEQUENCE){
619 if(td->type == SEQUENCE){
620 flag = 0;
621 fprintf(fp,"\t\t//copy sequence length and sequence to buffer\n");
622 fprintf(fp,"\t\t*ptr = seqlength_%d;\n",++seqCount);
623 fprintf(fp,"\t\tptr += sizeof(%s);\n",uintOutputTypes[td->size]);
624 fprintf(fp,"\t\tmemcpy(ptr, %s, sizeof(%s) * seqlength_%d);\n",
625 fld->name, getTypeStr(td), seqCount);
626 fprintf(fp,"\t\tptr += sizeof(%s) * seqlength_%d;\n\n",
627 getTypeStr(td), seqCount);
628 }
629 else if(td->type==STRING){
630 flag = 0;
631 fprintf(fp,"\t\t//copy string to buffer\n");
632 fprintf(fp,"\t\tif(strlength_%d > 0){\n",++strCount);
633 fprintf(fp,"\t\t\tmemcpy(ptr, %s, strlength_%d + 1);\n",
634 fld->name, strCount);
635 fprintf(fp,"\t\t\tptr += strlength_%d + 1;\n",strCount);
636 fprintf(fp,"\t\t}else{\n");
637 fprintf(fp,"\t\t\t*ptr = '\\0';\n");
638 fprintf(fp,"\t\t\tptr += 1;\n");
639 fprintf(fp,"\t\t}\n\n");
640 }else if(td->type==ARRAY){
641 flag = 0;
642 fprintf(fp,"\t//copy array to buffer\n");
643 fprintf(fp,"\tmemcpy(ptr, %s, sizeof(%s) * %d);\n",
644 fld->name, getTypeStr(td), td->size);
645 fprintf(fp,"\tptr += sizeof(%s) * %d;\n\n", getTypeStr(td), td->size);
646 }
647 }
648 if(structCount + seqCount > 1) fprintf(fp,"\n");
649
650 fprintf(fp,"\n");
651 fprintf(fp, "\t\t/* Commit the work */\n");
652 fprintf(fp, "\t\trelay_commit(channel->rchan->buf[smp_processor_id()],\n"
653 "\t\t\t\tbuff, event_length);\n");
654
655 /* End of traces iteration */
656 fprintf(fp, "\t}\n\n");
657
658 fprintf(fp, "\n");
659 fprintf(fp, "unlock_traces:\n");
660 fprintf(fp, "\tread_unlock(&ltt_traces.traces_rwlock);\n");
661
662 fprintf(fp, "unlock:\n");
663 fprintf(fp, "\tltt_nesting[smp_processor_id()]--;\n");
664 fprintf(fp, "\t/* Re-enable interrupts */\n");
665 fprintf(fp, "\tlocal_irq_restore(_flags);\n");
666 fprintf(fp, "\tpreempt_enable();\n");
667 //fprintf(fp, "\tpreempt_check_resched();\n");
668
669 //call trace function
670 //fprintf(fp,"\n\t//call trace function\n");
671 //fprintf(fp,"\tltt_log_event(ltt_facility_%s_%X, %s, bufLength, buff);\n",facName,checksum,ev->name);
672 fprintf(fp,"}\n\n\n");
673 }
674
675 }
676
677 /*****************************************************************************
678 *Function name
679 * getTypeStr : generate type string
680 *Input Params
681 * td : a type descriptor
682 *Return Values
683 * char * : type string
684 ****************************************************************************/
685 char * getTypeStr(type_descriptor * td){
686 type_descriptor * t ;
687
688 switch(td->type){
689 case INT:
690 return intOutputTypes[td->size];
691 case UINT:
692 return uintOutputTypes[td->size];
693 case POINTER:
694 return "void *";
695 case LONG:
696 return "long";
697 case ULONG:
698 return "unsigned long";
699 case SIZE_T:
700 return "size_t";
701 case SSIZE_T:
702 return "ssize_t";
703 case OFF_T:
704 return "off_t";
705 case FLOAT:
706 return floatOutputTypes[td->size];
707 case STRING:
708 return "const char";
709 case ENUM:
710 return uintOutputTypes[td->size];
711 case ARRAY:
712 case SEQUENCE:
713 t = td->nested_type;
714 switch(t->type){
715 case INT:
716 return intOutputTypes[t->size];
717 case UINT:
718 return uintOutputTypes[t->size];
719 case POINTER:
720 return "void *";
721 case LONG:
722 return "long";
723 case ULONG:
724 return "unsigned long";
725 case SIZE_T:
726 return "size_t";
727 case SSIZE_T:
728 return "ssize_t";
729 case OFF_T:
730 return "off_t";
731 case FLOAT:
732 return floatOutputTypes[t->size];
733 case STRING:
734 return "const char";
735 case ENUM:
736 return uintOutputTypes[t->size];
737 default :
738 error_callback(NULL,"Nested struct is not supportted");
739 break;
740 }
741 break;
742 case STRUCT: //for now we do not support nested struct
743 error_callback(NULL,"Nested struct is not supportted");
744 break;
745 default:
746 error_callback(NULL,"No type information");
747 break;
748 }
749 return NULL;
750 }
751
752 /*****************************************************************************
753 *Function name
754 * generateLoaderfile: generate a facility loaded .h file
755 *Input Params
756 * fp : file to be written to
757 * facName : name of facility
758 * nbEvent : number of events in the facility
759 * checksum : checksum for the facility
760 ****************************************************************************/
761 void generateLoaderfile(FILE * fp, char * facName, int nbEvent, unsigned long checksum, char *capname){
762 fprintf(fp, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n",capname);
763 fprintf(fp, "#define _LTT_FACILITY_LOADER_%s_H_\n\n",capname);
764 fprintf(fp,"#include <linux/ltt-facilities.h>\n", facName, checksum);
765 fprintf(fp,"ltt_facility_t\tltt_facility_%s;\n", facName, checksum);
766 fprintf(fp,"ltt_facility_t\tltt_facility_%s_%X;\n\n", facName, checksum);
767
768 fprintf(fp,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
769 facName);
770 fprintf(fp,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
771 facName, checksum);
772 fprintf(fp,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", checksum);
773 fprintf(fp,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", facName);
774 fprintf(fp,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\t%d\n\n", nbEvent);
775 fprintf(fp, "#endif //_LTT_FACILITY_LOADER_%s_H_\n",capname);
776 }
777
778 void generateCfile(FILE * fp, char * filefacname){
779
780 fprintf(fp, "/*\n");
781 fprintf(fp, " * ltt-facility-loader-%s.c\n", filefacname);
782 fprintf(fp, " *\n");
783 fprintf(fp, " * (C) Copyright 2005 - \n");
784 fprintf(fp, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
785 fprintf(fp, " *\n");
786 fprintf(fp, " * Contains the LTT facility loader.\n");
787 fprintf(fp, " *\n");
788 fprintf(fp, " */\n");
789 fprintf(fp, "\n");
790 fprintf(fp, "\n");
791 fprintf(fp, "#include <linux/ltt-facilities.h>\n");
792 fprintf(fp, "#include <linux/module.h>\n");
793 fprintf(fp, "#include <linux/init.h>\n");
794 fprintf(fp, "#include <linux/config.h>\n");
795 fprintf(fp, "#include \"ltt-facility-loader-%s.h\"\n", filefacname);
796 fprintf(fp, "\n");
797 fprintf(fp, "\n");
798 fprintf(fp, "#ifdef CONFIG_LTT\n");
799 fprintf(fp, "\n");
800 fprintf(fp, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
801 fprintf(fp, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
802 fprintf(fp, "\n");
803 fprintf(fp, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
804 fprintf(fp, "\n");
805 fprintf(fp, "#define SYMBOL_STRING(sym) #sym\n");
806 fprintf(fp, "\n");
807 fprintf(fp, "static struct ltt_facility facility = {\n");
808 fprintf(fp, "\t.name = ltt_facility_name,\n");
809 fprintf(fp, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
810 fprintf(fp, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
811 fprintf(fp, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL)\n");
812 fprintf(fp, "};\n");
813 fprintf(fp, "\n");
814 fprintf(fp, "#ifndef MODULE\n");
815 fprintf(fp, "\n");
816 fprintf(fp, "/* Built-in facility. */\n");
817 fprintf(fp, "\n");
818 fprintf(fp, "static int __init facility_init(void)\n");
819 fprintf(fp, "{\n");
820 fprintf(fp, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", filefacname);
821 fprintf(fp, "\n");
822 fprintf(fp, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n");
823 fprintf(fp, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
824 fprintf(fp, "\t\n");
825 fprintf(fp, "\treturn LTT_FACILITY_SYMBOL;\n");
826 fprintf(fp, "}\n");
827 fprintf(fp, "__initcall(facility_init);\n");
828 fprintf(fp, "\n");
829 fprintf(fp, "\n");
830 fprintf(fp, "\n");
831 fprintf(fp, "#else \n");
832 fprintf(fp, "\n");
833 fprintf(fp, "/* Dynamic facility. */\n");
834 fprintf(fp, "\n");
835 fprintf(fp, "static int __init facility_init(void)\n");
836 fprintf(fp, "{\n");
837 fprintf(fp, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", filefacname);
838 fprintf(fp, "\n");
839 fprintf(fp, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n");
840 fprintf(fp, "\tLTT_FACILITY_SYMBOL_CHECKSUM = LTT_FACILITY_SYMBOL;\n");
841 fprintf(fp, "\n");
842 fprintf(fp, "\treturn LTT_FACILITY_SYMBOL;\n");
843 fprintf(fp, "}\n");
844 fprintf(fp, "\n");
845 fprintf(fp, "static void __exit facility_exit(void)\n");
846 fprintf(fp, "{\n");
847 fprintf(fp, "\tint err;\n");
848 fprintf(fp, "\n");
849 fprintf(fp, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n");
850 fprintf(fp, "\tif(err != 0)\n");
851 fprintf(fp, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
852 fprintf(fp, "\n");
853 fprintf(fp, "}\n");
854 fprintf(fp, "\n");
855 fprintf(fp, "module_init(facility_init)\n");
856 fprintf(fp, "module_exit(facility_exit)\n");
857 fprintf(fp, "\n");
858 fprintf(fp, "\n");
859 fprintf(fp, "MODULE_LICENSE(\"GPL\");\n");
860 fprintf(fp, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
861 fprintf(fp, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
862 fprintf(fp, "\n");
863 fprintf(fp, "#endif //MODULE\n");
864 fprintf(fp, "\n");
865 fprintf(fp, "#endif //CONFIG_LTT\n");
866 }
867
868
This page took 0.060856 seconds and 4 git commands to generate.