3888436c |
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> |
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 | |
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) { |
31cbc5d3 |
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"); |
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 | ****************************************************************************/ |
125 | void generateFile(char *name){ |
6d387597 |
126 | char *loadName, *hName, *tmp, *tmp2; |
127 | FILE * lFp, *hFp; |
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; |
152 | lFp = fopen(loadName,"w"); |
153 | if(!lFp){ |
154 | printf("Cannot open the file : %s\n",loadName); |
3888436c |
155 | exit(1); |
156 | } |
157 | |
158 | hFp = fopen(hName,"w"); |
159 | if(!hFp){ |
160 | printf("Cannot open the file : %s\n",hName); |
161 | exit(1); |
162 | } |
163 | |
6d387597 |
164 | free(loadName); |
3888436c |
165 | free(hName); |
166 | |
167 | generateChecksum(fac->name, &checksum, &(fac->events)); |
168 | |
169 | /* generate .h file, event enumeration then structures and functions */ |
31cbc5d3 |
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); |
3888436c |
176 | |
6d387597 |
177 | /* generate .h file, calls to register the facility at init time */ |
8c6ca411 |
178 | generateLoaderfile(lFp,fac->name,nbEvent,checksum,fac->capname); |
3888436c |
179 | |
180 | fclose(hFp); |
6d387597 |
181 | fclose(lFp); |
3888436c |
182 | } |
183 | |
184 | |
185 | /***************************************************************************** |
186 | *Function name |
187 | * generateEnumEvent : output event enum to .h file |
188 | *Input Params |
189 | * fp : file to be written to |
190 | * facName : name of facility |
191 | *Output Params |
192 | * nbEvent : number of events in the facility |
193 | ****************************************************************************/ |
31cbc5d3 |
194 | void generateEnumEvent(FILE *fp, char *facName, int * nbEvent, unsigned long checksum) { |
3888436c |
195 | int pos = 0; |
196 | |
31cbc5d3 |
197 | fprintf(fp,"#include <linux/ltt-log.h>\n\n"); |
3888436c |
198 | |
199 | fprintf(fp,"/**** facility handle ****/\n\n"); |
44cac8a9 |
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); |
3888436c |
202 | |
203 | fprintf(fp,"/**** event type ****/\n\n"); |
204 | fprintf(fp,"enum %s_event {\n",facName); |
205 | |
206 | for(pos = 0; pos < fac->events.position;pos++) { |
31cbc5d3 |
207 | fprintf(fp,"\t%s", ((event *)(fac->events.array[pos]))->name); |
3888436c |
208 | if(pos != fac->events.position-1) fprintf(fp,",\n"); |
209 | } |
210 | fprintf(fp,"\n};\n\n\n"); |
211 | |
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; |
215 | } |
216 | |
217 | |
31cbc5d3 |
218 | /***************************************************************************** |
219 | *Function name |
220 | * printStruct : Generic struct printing function |
221 | *Input Params |
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 | ****************************************************************************/ |
cb1eb7ce |
231 | |
31cbc5d3 |
232 | static void |
233 | printStruct(FILE * fp, int len, void ** array, char * name, char * facName, |
234 | int * whichTypeFirst, int * hasStrSeq, int * structCount) |
235 | { |
236 | int flag = 0; |
237 | int pos; |
238 | field * fld; |
239 | type_descriptor * td; |
240 | |
241 | for (pos = 0; pos < len; pos++) { |
242 | fld = (field *)array[pos]; |
243 | td = fld->type; |
244 | if( td->type != STRING && td->type != SEQUENCE && |
cb1eb7ce |
245 | td->type != ARRAY) { |
31cbc5d3 |
246 | if (*whichTypeFirst == 0) { |
247 | *whichTypeFirst = 1; //struct first |
248 | } |
249 | if (flag == 0) { |
250 | flag = 1; |
251 | |
cb1eb7ce |
252 | fprintf(fp,"struct %s_%s",name, facName); |
253 | if (structCount) { |
254 | fprintf(fp, "_%d {\n",++*structCount); |
255 | } else { |
256 | fprintf(fp, " {\n"); |
257 | } |
31cbc5d3 |
258 | } |
cb1eb7ce |
259 | fprintf(fp, "\t%s %s; /* %s */\n", |
260 | getTypeStr(td),fld->name,fld->description ); |
31cbc5d3 |
261 | } else { |
cb1eb7ce |
262 | if (*whichTypeFirst == 0) { |
31cbc5d3 |
263 | //string or sequence or array first |
cb1eb7ce |
264 | *whichTypeFirst = 2; |
265 | } |
266 | (*hasStrSeq)++; |
267 | if(flag) { |
268 | fprintf(fp,"} __attribute__ ((packed));\n\n"); |
269 | } |
270 | flag = 0; |
31cbc5d3 |
271 | } |
272 | } |
273 | |
274 | if(flag) { |
275 | fprintf(fp,"} __attribute__ ((packed));\n\n"); |
276 | } |
277 | } |
278 | |
279 | |
280 | /***************************************************************************** |
281 | *Function name |
282 | * generateHfile : Create the typedefs |
283 | *Input Params |
284 | * fp : file to be written to |
285 | ****************************************************************************/ |
286 | void |
287 | generateTypeDefs(FILE * fp) |
288 | { |
289 | int pos, tmp = 1; |
290 | |
291 | fprintf(fp, "/**** Basic Type Definitions ****/\n\n"); |
292 | |
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); |
300 | } |
301 | } |
302 | |
303 | |
3888436c |
304 | /***************************************************************************** |
305 | *Function name |
306 | * generateEnumDefinition: generate enum definition if it exists |
307 | *Input Params |
308 | * fp : file to be written to |
309 | * fHead : enum type |
310 | ****************************************************************************/ |
311 | void generateEnumDefinition(FILE * fp, type_descriptor * type){ |
312 | int pos; |
313 | |
44cac8a9 |
314 | if(type->already_printed) return; |
315 | |
3888436c |
316 | fprintf(fp,"enum {\n"); |
317 | for(pos = 0; pos < type->labels.position; pos++){ |
31cbc5d3 |
318 | fprintf(fp,"\t%s", type->labels.array[pos]); |
8c6ca411 |
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]); |
322 | else |
323 | fprintf(fp,"\n"); |
3888436c |
324 | } |
8c6ca411 |
325 | fprintf(fp,"};\n\n\n"); |
44cac8a9 |
326 | |
327 | type->already_printed = 1; |
3888436c |
328 | } |
329 | |
330 | /***************************************************************************** |
331 | *Function name |
332 | * generateStrucTFunc: output structure and function to .h file |
333 | *Input Params |
334 | * fp : file to be written to |
335 | * facName : name of facility |
336 | ****************************************************************************/ |
31cbc5d3 |
337 | void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){ |
3888436c |
338 | event * ev; |
339 | field * fld; |
340 | type_descriptor * td; |
341 | int pos, pos1; |
342 | int hasStrSeq, flag, structCount, seqCount,strCount, whichTypeFirst=0; |
343 | |
344 | for(pos = 0; pos < fac->events.position; pos++){ |
345 | ev = (event *) fac->events.array[pos]; |
346 | //yxx if(ev->nested)continue; |
d7ed29cd |
347 | fprintf(fp,"/**** structure and trace function for event: %s ****/\n\n", |
348 | ev->name); |
3888436c |
349 | if(ev->type == 0){ // event without type |
350 | fprintf(fp,"static inline void trace_%s_%s(void){\n",facName,ev->name); |
d7ed29cd |
351 | fprintf(fp,"\tltt_log_event(ltt_facility_%s_%X, %s, 0, NULL);\n", |
352 | facName,checksum,ev->name); |
3888436c |
353 | fprintf(fp,"};\n\n\n"); |
354 | continue; |
355 | } |
356 | |
357 | //if fields contain enum, print out enum definition |
d7ed29cd |
358 | //MD : fixed in generateEnumDefinition to do not print the same enum |
359 | //twice. |
3888436c |
360 | for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){ |
361 | fld = (field *)ev->type->fields.array[pos1]; |
362 | if(fld->type->type == ENUM) generateEnumDefinition(fp, fld->type); |
363 | } |
364 | |
365 | //default: no string, array or sequence in the event |
366 | hasStrSeq = 0; |
367 | whichTypeFirst = 0; |
3888436c |
368 | structCount = 0; |
31cbc5d3 |
369 | |
370 | //structure for kernel |
371 | printStruct(fp, ev->type->fields.position, ev->type->fields.array, |
372 | ev->name, facName, &whichTypeFirst, &hasStrSeq, &structCount); |
3888436c |
373 | |
374 | //trace function : function name and parameters |
375 | seqCount = 0; |
376 | strCount = 0; |
377 | fprintf(fp,"static inline void trace_%s_%s(",facName,ev->name); |
378 | for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){ |
379 | fld = (field *)ev->type->fields.array[pos1]; |
380 | td = fld->type; |
381 | if(td->type == ARRAY ){ |
d7ed29cd |
382 | fprintf(fp,"%s * %s",getTypeStr(td), fld->name); |
3888436c |
383 | }else if(td->type == STRING){ |
d7ed29cd |
384 | fprintf(fp,"short int strlength_%d, %s * %s", |
385 | ++strCount, getTypeStr(td), fld->name); |
3888436c |
386 | }else if(td->type == SEQUENCE){ |
d7ed29cd |
387 | fprintf(fp,"%s seqlength_%d, %s * %s", |
388 | uintOutputTypes[td->size], ++seqCount,getTypeStr(td), fld->name); |
389 | }else fprintf(fp,"%s %s",getTypeStr(td), fld->name); |
390 | if(pos1 != ev->type->fields.position - 1) fprintf(fp,", "); |
3888436c |
391 | } |
d7ed29cd |
392 | fprintf(fp,")\n{\n"); |
3888436c |
393 | |
394 | //length of buffer : length of all structures |
d7ed29cd |
395 | fprintf(fp,"\tint length = "); |
3888436c |
396 | for(pos1=0;pos1<structCount;pos1++){ |
397 | fprintf(fp,"sizeof(struct %s_%s_%d)",ev->name, facName,pos1+1); |
398 | if(pos1 != structCount-1) fprintf(fp," + "); |
399 | } |
400 | |
401 | //length of buffer : length of all arrays, sequences and strings |
402 | seqCount = 0; |
403 | strCount = 0; |
404 | flag = 0; |
405 | for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){ |
406 | fld = (field *)ev->type->fields.array[pos1]; |
407 | td = fld->type; |
d7ed29cd |
408 | if(td->type == SEQUENCE || td->type==STRING || td->type==ARRAY){ |
409 | if(structCount || flag > 0) fprintf(fp," + "); |
410 | if(td->type == SEQUENCE) |
411 | fprintf(fp,"sizeof(%s) + sizeof(%s) * seqlength_%d", |
412 | uintOutputTypes[td->size], getTypeStr(td), ++seqCount); |
413 | else if(td->type==STRING) fprintf(fp,"strlength_%d + 1", ++strCount); |
414 | else if(td->type==ARRAY) |
415 | fprintf(fp,"sizeof(%s) * %d", getTypeStr(td),td->size); |
416 | if(structCount == 0) flag = 1; |
3888436c |
417 | } |
418 | } |
419 | fprintf(fp,";\n"); |
420 | |
421 | //allocate buffer |
d7ed29cd |
422 | // MD no more need. fprintf(fp,"\tchar buff[buflength];\n"); |
423 | // write directly to the channel |
424 | fprintf(fp, "\tunsigned int index;\n"); |
425 | fprintf(fp, "\tstruct ltt_channel_struct *channel;\n"); |
426 | fprintf(fp, "\tstruct ltt_trace_struct *trace;\n"); |
427 | fprintf(fp, "\tunsigned long flags;\n"); |
428 | fprintf(fp, "\tstruct %s_%s_1* __1;\n\n", ev->name, facName); |
429 | |
5cbe87a9 |
430 | fprintf(fp, "\tread_lock(<t_traces.traces_rwlock);\n\n"); |
431 | fprintf(fp, |
432 | "\tif(ltt_traces.num_active_traces == 0) goto unlock_traces;\n\n"); |
d7ed29cd |
433 | |
434 | fprintf(fp, |
435 | "\tindex = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\ |
436 | "\t\t\t\t%s);\n", |
437 | facName, checksum, ev->name); |
438 | fprintf(fp,"\n"); |
439 | |
440 | fprintf(fp, "\t/* Disable interrupts. */\n"); |
5cbe87a9 |
441 | fprintf(fp, "\tlocal_irq_save(flags);\n\n"); |
3888436c |
442 | |
5cbe87a9 |
443 | /* For each trace */ |
d7ed29cd |
444 | fprintf(fp, "\tlist_for_each_entry(trace, <t_traces.head, list) {\n"); |
5cbe87a9 |
445 | fprintf(fp, "\t\tif(!trace->active) goto skip_trace;\n\n"); |
446 | |
d7ed29cd |
447 | fprintf(fp, "\t\tunsigned int header_length = " |
448 | "ltt_get_event_header_size(trace);\n"); |
449 | fprintf(fp, "\t\tunsigned int event_length = header_length + length;\n"); |
450 | |
451 | /* Reserve the channel */ |
452 | fprintf(fp, "\t\tchannel = ltt_get_channel_from_index(trace, index);\n"); |
453 | fprintf(fp, |
454 | "\t\tvoid *buff = relay_reserve(channel->rchan, event_length);\n"); |
455 | fprintf(fp, "\t\tif(buff == NULL) {\n"); |
456 | fprintf(fp, "\t\t\t/* Buffer is full*/\n"); |
457 | fprintf(fp, "\t\t\tchannel->events_lost++;\n"); |
458 | fprintf(fp, "\t\t\tgoto commit_work;\n"); |
459 | fprintf(fp, "\t\t}\n"); |
460 | |
461 | /* Write the header */ |
462 | fprintf(fp, "\n"); |
463 | fprintf(fp, "\t\tltt_write_event_header(channel, buff, \n" |
464 | "\t\t\t\tltt_facility_%s_%X, %s, length);\n", |
465 | facName, checksum, ev->name); |
466 | fprintf(fp, "\n"); |
467 | |
468 | //declare a char pointer if needed : starts at the end of the structs. |
469 | if(structCount + hasStrSeq > 1) { |
470 | fprintf(fp,"\t\tchar * ptr = (char*)buff + header_length"); |
471 | for(pos1=0;pos1<structCount;pos1++){ |
472 | fprintf(fp," + sizeof(struct %s_%s_%d)",ev->name, facName,pos1+1); |
473 | } |
474 | if(structCount + hasStrSeq > 1) fprintf(fp,";\n"); |
3888436c |
475 | } |
d7ed29cd |
476 | |
477 | // Declare an alias pointer of the struct type to the beginning |
478 | // of the reserved area, just after the event header. |
479 | fprintf(fp, "\t\t__1 = (struct %s_%s_1 *)(buff + header_length);\n", |
480 | ev->name, facName); |
481 | //allocate memory for new struct and initialize it |
482 | //if(whichTypeFirst == 1){ //struct first |
483 | //for(pos1=0;pos1<structCount;pos1++){ |
484 | // if(pos1==0) fprintf(fp, |
485 | // "\tstruct %s_%s_1 * __1 = (struct %s_%s_1 *)buff;\n", |
486 | // ev->name, facName,ev->name, facName); |
487 | //MD disabled else fprintf(fp, |
488 | // "\tstruct %s_%s_%d __%d;\n", |
489 | // ev->name, facName,pos1+1,pos1+1); |
490 | //} |
491 | //}else if(whichTypeFirst == 2){ |
492 | // for(pos1=0;pos1<structCount;pos1++) |
493 | // fprintf(fp,"\tstruct %s_%s_%d __%d;\n", |
494 | // ev->name, facName,pos1+1,pos1+1); |
495 | //} |
3888436c |
496 | fprintf(fp,"\n"); |
497 | |
d7ed29cd |
498 | if(structCount) fprintf(fp,"\t\t//initialize structs\n"); |
499 | //flag = 0; |
500 | //structCount = 0; |
3888436c |
501 | for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){ |
502 | fld = (field *)ev->type->fields.array[pos1]; |
503 | td = fld->type; |
504 | if(td->type != ARRAY && td->type != SEQUENCE && td->type != STRING){ |
d7ed29cd |
505 | //if(flag == 0){ |
506 | // flag = 1; |
507 | // structCount++; |
508 | // if(structCount > 1) fprintf(fp,"\n"); |
509 | //} |
510 | fprintf(fp, "\t\t__1->%s = %s;\n", fld->name, fld->name ); |
511 | |
512 | //if(structCount == 1 && whichTypeFirst == 1) |
513 | // fprintf(fp, "\t__1->%s = %s;\n",fld->name,fld->name ); |
514 | //else |
515 | // fprintf(fp, "\t__%d.%s = %s;\n",structCount ,fld->name,fld->name); |
516 | } |
517 | //else flag = 0; |
3888436c |
518 | } |
d7ed29cd |
519 | // if(structCount) fprintf(fp,"\n"); |
3888436c |
520 | //set ptr to the end of first struct if needed; |
d7ed29cd |
521 | //if(whichTypeFirst == 1 && structCount + hasStrSeq > 1){ |
522 | // fprintf(fp,"\n\t//set ptr to the end of the first struct\n"); |
523 | // fprintf(fp,"\tptr += sizeof(struct %s_%s_1);\n\n",ev->name, facName); |
524 | // } |
3888436c |
525 | |
526 | //copy struct, sequence and string to buffer |
527 | seqCount = 0; |
528 | strCount = 0; |
529 | flag = 0; |
530 | structCount = 0; |
531 | for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){ |
532 | fld = (field *)ev->type->fields.array[pos1]; |
533 | td = fld->type; |
d7ed29cd |
534 | // if(td->type != STRING && td->type != SEQUENCE && td->type != ARRAY){ |
535 | // if(flag == 0) structCount++; |
536 | // flag++; |
537 | // if((structCount > 1 || whichTypeFirst == 2) && flag == 1){ |
538 | // assert(0); // MD : disabled ! |
539 | // fprintf(fp,"\t//copy struct to buffer\n"); |
540 | // fprintf(fp,"\tmemcpy(ptr, &__%d, sizeof(struct %s_%s_%d));\n", |
541 | // structCount, ev->name, facName,structCount); |
542 | // fprintf(fp,"\tptr += sizeof(struct %s_%s_%d);\n\n", |
543 | // ev->name, facName,structCount); |
544 | // } |
545 | // } |
546 | //else if(td->type == SEQUENCE){ |
547 | if(td->type == SEQUENCE){ |
548 | flag = 0; |
549 | fprintf(fp,"\t\t//copy sequence length and sequence to buffer\n"); |
550 | fprintf(fp,"\t\t*ptr = seqlength_%d;\n",++seqCount); |
551 | fprintf(fp,"\t\tptr += sizeof(%s);\n",uintOutputTypes[td->size]); |
552 | fprintf(fp,"\t\tmemcpy(ptr, %s, sizeof(%s) * seqlength_%d);\n", |
553 | fld->name, getTypeStr(td), seqCount); |
554 | fprintf(fp,"\t\tptr += sizeof(%s) * seqlength_%d;\n\n", |
555 | getTypeStr(td), seqCount); |
556 | } |
557 | else if(td->type==STRING){ |
558 | flag = 0; |
559 | fprintf(fp,"\t\t//copy string to buffer\n"); |
560 | fprintf(fp,"\t\tif(strlength_%d > 0){\n",++strCount); |
561 | fprintf(fp,"\t\t\tmemcpy(ptr, %s, strlength_%d + 1);\n", |
562 | fld->name, strCount); |
563 | fprintf(fp,"\t\t\tptr += strlength_%d + 1;\n",strCount); |
564 | fprintf(fp,"\t\t}else{\n"); |
565 | fprintf(fp,"\t\t\t*ptr = '\\0';\n"); |
566 | fprintf(fp,"\t\t\tptr += 1;\n"); |
567 | fprintf(fp,"\t\t}\n\n"); |
3888436c |
568 | }else if(td->type==ARRAY){ |
d7ed29cd |
569 | flag = 0; |
570 | fprintf(fp,"\t//copy array to buffer\n"); |
571 | fprintf(fp,"\tmemcpy(ptr, %s, sizeof(%s) * %d);\n", |
572 | fld->name, getTypeStr(td), td->size); |
573 | fprintf(fp,"\tptr += sizeof(%s) * %d;\n\n", getTypeStr(td), td->size); |
3888436c |
574 | } |
575 | } |
576 | if(structCount + seqCount > 1) fprintf(fp,"\n"); |
577 | |
d7ed29cd |
578 | fprintf(fp,"\n"); |
579 | fprintf(fp,"commit_work:\n"); |
580 | fprintf(fp,"\n"); |
581 | fprintf(fp, "\t\t/* Commit the work */\n"); |
582 | fprintf(fp, "\t\trelay_commit(channel->rchan, buff, event_length);\n"); |
583 | |
584 | /* End of traces iteration */ |
5cbe87a9 |
585 | fprintf(fp, "skip_trace:\n\n"); |
d7ed29cd |
586 | fprintf(fp, "\t}\n\n"); |
587 | |
588 | fprintf(fp, "\t/* Re-enable interrupts */\n"); |
589 | fprintf(fp, "\tlocal_irq_restore(flags);\n"); |
590 | fprintf(fp, "\tpreempt_check_resched();\n"); |
5cbe87a9 |
591 | |
592 | fprintf(fp, "\n"); |
593 | fprintf(fp, "unlock_traces:\n"); |
d7ed29cd |
594 | fprintf(fp, "\tread_unlock(<t_traces.traces_rwlock);\n"); |
3888436c |
595 | //call trace function |
d7ed29cd |
596 | //fprintf(fp,"\n\t//call trace function\n"); |
597 | //fprintf(fp,"\tltt_log_event(ltt_facility_%s_%X, %s, bufLength, buff);\n",facName,checksum,ev->name); |
3888436c |
598 | fprintf(fp,"};\n\n\n"); |
599 | } |
600 | |
601 | } |
602 | |
603 | /***************************************************************************** |
604 | *Function name |
605 | * getTypeStr : generate type string |
606 | *Input Params |
607 | * td : a type descriptor |
608 | *Return Values |
609 | * char * : type string |
610 | ****************************************************************************/ |
611 | char * getTypeStr(type_descriptor * td){ |
612 | type_descriptor * t ; |
613 | |
614 | switch(td->type){ |
615 | case INT: |
616 | return intOutputTypes[td->size]; |
617 | case UINT: |
618 | return uintOutputTypes[td->size]; |
31cbc5d3 |
619 | case POINTER: |
620 | return "void *"; |
621 | case LONG: |
622 | return "long"; |
623 | case ULONG: |
624 | return "unsigned long"; |
625 | case SIZE_T: |
626 | return "size_t"; |
627 | case SSIZE_T: |
628 | return "ssize_t"; |
629 | case OFF_T: |
630 | return "off_t"; |
3888436c |
631 | case FLOAT: |
632 | return floatOutputTypes[td->size]; |
633 | case STRING: |
634 | return "char"; |
635 | case ENUM: |
636 | return uintOutputTypes[td->size]; |
637 | case ARRAY: |
638 | case SEQUENCE: |
639 | t = td->nested_type; |
640 | switch(t->type){ |
641 | case INT: |
31cbc5d3 |
642 | return intOutputTypes[t->size]; |
3888436c |
643 | case UINT: |
31cbc5d3 |
644 | return uintOutputTypes[t->size]; |
645 | case POINTER: |
646 | return "void *"; |
647 | case LONG: |
648 | return "long"; |
649 | case ULONG: |
650 | return "unsigned long"; |
651 | case SIZE_T: |
652 | return "size_t"; |
653 | case SSIZE_T: |
654 | return "ssize_t"; |
655 | case OFF_T: |
656 | return "off_t"; |
3888436c |
657 | case FLOAT: |
31cbc5d3 |
658 | return floatOutputTypes[t->size]; |
3888436c |
659 | case STRING: |
31cbc5d3 |
660 | return "char"; |
3888436c |
661 | case ENUM: |
31cbc5d3 |
662 | return uintOutputTypes[t->size]; |
3888436c |
663 | default : |
31cbc5d3 |
664 | error_callback(NULL,"Nested struct is not supportted"); |
665 | break; |
3888436c |
666 | } |
667 | break; |
668 | case STRUCT: //for now we do not support nested struct |
669 | error_callback(NULL,"Nested struct is not supportted"); |
670 | break; |
671 | default: |
672 | error_callback(NULL,"No type information"); |
673 | break; |
674 | } |
675 | return NULL; |
676 | } |
677 | |
678 | /***************************************************************************** |
679 | *Function name |
6d387597 |
680 | * generateLoaderfile: generate a facility loaded .h file |
3888436c |
681 | *Input Params |
682 | * fp : file to be written to |
683 | * facName : name of facility |
684 | * nbEvent : number of events in the facility |
685 | * checksum : checksum for the facility |
686 | ****************************************************************************/ |
8c6ca411 |
687 | void generateLoaderfile(FILE * fp, char * facName, int nbEvent, unsigned long checksum, char *capname){ |
688 | fprintf(fp, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n",capname); |
689 | fprintf(fp, "#define _LTT_FACILITY_LOADER_%s_H_\n\n",capname); |
690 | fprintf(fp,"#include <linux/ltt-facilities.h>\n", facName, checksum); |
691 | fprintf(fp,"#include <linux/module.h>\n\n", facName, checksum); |
44cac8a9 |
692 | fprintf(fp,"ltt_facility_t\tltt_facility_%s;\n", facName, checksum); |
6d387597 |
693 | fprintf(fp,"ltt_facility_t\tltt_facility_%s_%X;\n\n", facName, checksum); |
694 | |
44cac8a9 |
695 | fprintf(fp,"EXPORT_SYMBOL(ltt_facility_%s);\n\n",facName, checksum); |
6d387597 |
696 | fprintf(fp,"EXPORT_SYMBOL(ltt_facility_%s_%X);\n\n",facName, checksum); |
44cac8a9 |
697 | fprintf(fp,"#define LTT_FACILITY_SYMBOL\t\t\t\tltt_facility_%s\n", |
698 | facName); |
699 | fprintf(fp,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\t\t\tltt_facility_%s_%X\n", |
6d387597 |
700 | facName, checksum); |
701 | fprintf(fp,"#define LTT_FACILITY_CHECKSUM\t\t\t0x%X\n", checksum); |
702 | fprintf(fp,"#define LTT_FACILITY_NAME\t\t\t\t\t\"%s\"\n", facName); |
703 | fprintf(fp,"#define LTT_FACILITY_NUM_EVENTS\t\t%d\n\n", nbEvent); |
8c6ca411 |
704 | fprintf(fp, "#endif //_LTT_FACILITY_LOADER_%s_H_\n",capname); |
3888436c |
705 | } |
706 | |
707 | |