fix strlen + \0
[lttv.git] / genevent-new / genevent.c
index 4042dafafdeda23986643873853d0e60e71b8c3b..4151c64a8de746ed9bfda84ab225f868e0e1618d 100644 (file)
 #define dprintf(...)
 #endif
 
-void preset_field_type_size(event_t *event_type,
-    off_t offset_root, off_t offset_parent,
-    enum field_status *fixed_root, enum field_status *fixed_parent,
-    field_t *field);
-
 /* Code printing */
 
 void print_tabs(unsigned int tabs, FILE *fd)
@@ -99,11 +94,6 @@ void print_tabs(unsigned int tabs, FILE *fd)
                fprintf(fd, "\t");
 }
 
-/* Type size checking */
-/* Uses #error in the generated code to signal error and stop the compiler */
-int print_check(int fd);
-
-
 /* print type.
  *
  * Copied from construct_types_and_fields in LTTV facility.c */
@@ -179,7 +169,7 @@ int print_type(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                        fprintf(fd, "off_t");
                        break;
                case STRING:
-                       fprintf(fd, "char *");
+                       fprintf(fd, "const char *");
                        break;
                case ENUM:
                        fprintf(fd, "enum lttng_%s", basename);
@@ -293,7 +283,7 @@ int print_arg(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                        fprintf(fd, " %s", field_name);
                        break;
                case STRING:
-                       fprintf(fd, "char *");
+                       fprintf(fd, "const char *");
                        fprintf(fd, " %s", field_name);
                        break;
                case ENUM:
@@ -372,9 +362,14 @@ int has_type_fixed_size(type_descriptor_t *td)
                        break;
                case ARRAY:
                        assert(td->size >= 0);
-                       return has_type_fixed(((field_t*)td->fields.array[0])->type);
+                       return has_type_fixed_size(((field_t*)td->fields.array[0])->type);
+                       break;
+               case NONE:
+                       printf("There is a type defined to NONE : bad.\n");
+                       assert(0);
                        break;
        }
+       return 0; //make gcc happy.
 }
 
 
@@ -431,7 +426,7 @@ int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                                if(print_type_declaration(((field_t*)td->fields.array[0])->type,
                                                                                                                                        fd,     0, basename, "")) return 1;
                        }
-                       fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %llu\n", basename,
+                       fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename,
                                        td->size);
                        fprintf(fd, "typedef ");
                        if(print_type(((field_t*)td->fields.array[0])->type,
@@ -456,12 +451,13 @@ int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                        print_tabs(1, fd);
                        if(print_type(((field_t*)td->fields.array[0])->type,
                                                fd, tabs, basename, "")) return 1;
-                       fprintf(fd, "len;\n");
+                       fprintf(fd, " len;\n");
                        print_tabs(1, fd);
                        if(print_type(((field_t*)td->fields.array[1])->type,
                                                fd, tabs, basename, "")) return 1;
                        fprintf(fd, " *array;\n");
-                       fprintf(fd, "};\n");
+                       fprintf(fd, "};\n");    /* We do not LTT_ALIGN, because we never copy
+                                                                                                                        it to the buffer directly. */
                        fprintf(fd, "\n");
                break;
 
@@ -486,11 +482,10 @@ int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                                fprintf(fd, "%s", field->name);
                                fprintf(fd, ";\n");
                        }
-                       fprintf(fd, "};\n");
+                       fprintf(fd, "} LTT_ALIGN;\n");
                        fprintf(fd, "\n");
                        break;
        case UNION:
-                       /* TODO : Do not allow variable length fields in a union */
                        for(unsigned int i=0;i<td->fields.position;i++){
                                field_t *field = (field_t*)(td->fields.array[i]);
                                type_descriptor_t *type = field->type;
@@ -511,7 +506,7 @@ int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                                fprintf(fd, "%s", field->name);
                                fprintf(fd, ";\n");
                        }
-                       fprintf(fd, "};\n");
+                       fprintf(fd, "} LTT_ALIGN;\n");
                        fprintf(fd, "\n");
                        break;
        default:
@@ -548,56 +543,84 @@ int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                 * the array. */
                if((basename_len != 0)
                                && (basename[basename_len-1] != '_'
+                               && field_name != NULL
                                && (field_name[0] != '\0'))) {
                        strncat(basename, "_", PATH_MAX - basename_len);
                        basename_len = strlen(basename);
                }
-               strncat(basename, field_name, PATH_MAX - basename_len);
+               if(field_name != NULL)
+                       strncat(basename, field_name, PATH_MAX - basename_len);
        }
        
-       switch(td->type) {
-               case INT_FIXED:
-               case UINT_FIXED:
-               case CHAR:
-               case UCHAR:
-               case SHORT:
-               case USHORT:
-               case INT:
-               case UINT:
-               case FLOAT:
-               case POINTER:
-               case LONG:
-               case ULONG:
-               case SIZE_T:
-               case SSIZE_T:
-               case OFF_T:
-               case ENUM:
-                       fprintf(fd, "sizeof(");
-                       if(print_type(td->type,
-                                               fd, 0, basename, "")) return 1;
-                       fprintf(fd, ")");
-                       break;
-               case STRING:
-                       fprintf(fd, "sizeof(char)");
-                       break;
-               case SEQUENCE:
-                       fprintf(fd, "lttng_get_alignment_sequence_%s(&obj->%s)", basename,
-                                       field_name);
-                       break;
-               case STRUCT:
-                       fprintf(fd, "lttng_get_alignment_struct_%s(&obj->%s)", basename,
-                                       field_name);
-                       break;
-               case UNION:
-                       fprintf(fd, "lttng_get_alignment_union_%s(&obj->%s)", basename,
-                                       field_name);
-                       break;
-               case ARRAY:
-                       fprintf(fd, "lttng_get_alignment_array_%s(obj->%s)", basename,
-                                       field_name);
-                       break;
+       if(field_name[0] == '\0') {
+               /* We are in a write function : it's the obj that we must align. */
+               switch(td->type) {
+                       case SEQUENCE:
+                               fprintf(fd, "lttng_get_alignment_sequence_%s(obj)", basename);
+                               break;
+                       case STRUCT:
+                               fprintf(fd, "lttng_get_alignment_struct_%s(obj)", basename);
+                               break;
+                       case UNION:
+                               fprintf(fd, "lttng_get_alignment_union_%s(obj)", basename);
+                               break;
+                       case ARRAY:
+                               fprintf(fd, "lttng_get_alignment_array_%s(obj)", basename);
+                               break;
+                       default:
+                               printf("error : type unexpected\n");
+                               return 1;
+                               break;
+               }
+       } else {
+               
+               switch(td->type) {
+                       case INT_FIXED:
+                       case UINT_FIXED:
+                       case CHAR:
+                       case UCHAR:
+                       case SHORT:
+                       case USHORT:
+                       case INT:
+                       case UINT:
+                       case FLOAT:
+                       case POINTER:
+                       case LONG:
+                       case ULONG:
+                       case SIZE_T:
+                       case SSIZE_T:
+                       case OFF_T:
+                       case ENUM:
+                               fprintf(fd, "sizeof(");
+                               if(print_type(td, fd, 0, basename, "")) return 1;
+                               fprintf(fd, ")");
+                               break;
+                       case STRING:
+                               fprintf(fd, "sizeof(char)");
+                               break;
+                       case SEQUENCE:
+                               fprintf(fd, "lttng_get_alignment_sequence_%s(&obj->%s)", basename,
+                                               field_name);
+                               break;
+                       case STRUCT:
+                               fprintf(fd, "lttng_get_alignment_struct_%s(&obj->%s)", basename,
+                                               field_name);
+                               break;
+                       case UNION:
+                               fprintf(fd, "lttng_get_alignment_union_%s(&obj->%s)", basename,
+                                               field_name);
+                               break;
+                       case ARRAY:
+                               fprintf(fd, "lttng_get_alignment_array_%s(obj->%s)", basename,
+                                               field_name);
+                               break;
+                       case NONE:
+                               printf("error : type NONE unexpected\n");
+                               return 1;
+                               break;
+               }
        }
-
+       
        return 0;
 }
 
@@ -653,31 +676,30 @@ int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                        print_tabs(tabs, fd);
                        fprintf(fd, "size = ");
                        fprintf(fd, "sizeof(");
-                       if(print_type(td->type,
-                                               fd, 0, basename, "")) return 1;
+                       if(print_type(td, fd, 0, basename, "")) return 1;
                        fprintf(fd, ");\n");
                        print_tabs(tabs, fd);
-                       fprintf(fd, "size += ltt_align(*to+*len, size) + size;");
+                       fprintf(fd, "size += ltt_align(*to+*len, size) + size;\n");
                        print_tabs(tabs, fd);
                        fprintf(fd, "*len += size;");
                        break;
                case STRING:
                        print_tabs(tabs, fd);
-                       fprintf(fd, "lttng_write_string_%s(buffer, to_base, to, from, len, obj->%s);\n");
+                       fprintf(fd, "lttng_write_string_%s(buffer, to_base, to, from, len, obj->%s);\n", basename, field_name);
                        break;
                case SEQUENCE:
                        print_tabs(tabs, fd);
-                       fprintf(fd, "lttng_write_%s(buffer, to_base, to, from, len, obj->%s)", basename,
+                       fprintf(fd, "lttng_write_%s(buffer, to_base, to, from, len, &obj->%s)", basename,
                                        field_name);
                        break;
                case STRUCT:
                        print_tabs(tabs, fd);
-                       fprintf(fd, "lttng_write_struct_%s(buffer, to_base, to, from, len, obj->%s)", basename,
+                       fprintf(fd, "lttng_write_struct_%s(buffer, to_base, to, from, len, &obj->%s)", basename,
                                        field_name);
                        break;
                case UNION:
                        print_tabs(tabs, fd);
-                       fprintf(fd, "lttng_write_union_%s(buffer, to_base, to, from, len, obj->%s)", basename,
+                       fprintf(fd, "lttng_write_union_%s(buffer, to_base, to, from, len, &obj->%s)", basename,
                                        field_name);
                        break;
                case ARRAY:
@@ -685,6 +707,10 @@ int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                        fprintf(fd, "lttng_write_array_%s(buffer, to_base, to, from, len, obj->%s)", basename,
                                        field_name);
                        break;
+               case NONE:
+                       printf("Error : type NONE unexpected\n");
+                       return 1;
+                       break;
        }
 
        return 0;
@@ -731,7 +757,7 @@ int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
                        fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n",
                                        basename);
                        print_tabs(2, fd);
-                       if(print_type(td->type, fd, 0, basename, "")) return 1;
+                       if(print_type(td,       fd, 0, basename, "")) return 1;
                        fprintf(fd, " *obj)\n");
                        fprintf(fd, "{\n");
                        print_tabs(1, fd);
@@ -740,7 +766,7 @@ int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
                        print_tabs(1, fd);
                        fprintf(fd, "localign = ");
                        if(print_type_alignment(((field_t*)td->fields.array[0])->type,
-                                               fd, 0, basename, field->name)) return 1;
+                                               fd, 0, basename, ((field_t*)td->fields.array[0])->name)) return 1;
                        fprintf(fd, ";\n");
                        print_tabs(1, fd);
                        fprintf(fd, "align = max(align, localign);\n");
@@ -748,7 +774,7 @@ int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
                        print_tabs(1, fd);
                        fprintf(fd, "localign = ");
                        if(print_type_alignment(((field_t*)td->fields.array[1])->type,
-                                               fd, 0, basename, field->name)) return 1;
+                                               fd, 0, basename, ((field_t*)td->fields.array[1])->name)) return 1;
                        fprintf(fd, ";\n");
                        print_tabs(1, fd);
                        fprintf(fd, "align = max(align, localign);\n");
@@ -761,7 +787,7 @@ int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
                        fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n",
                                        basename);
                        print_tabs(2, fd);
-                       if(print_type(td->type, fd, 0, basename, "")) return 1;
+                       if(print_type(td,       fd, 0, basename, "")) return 1;
                        fprintf(fd, " *obj)\n");
                        fprintf(fd, "{\n");
                        print_tabs(1, fd);
@@ -787,7 +813,7 @@ int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
                        fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n",
                                        basename);
                        print_tabs(2, fd);
-                       if(print_type(td->type, fd, 0, basename, "")) return 1;
+                       if(print_type(td,       fd, 0, basename, "")) return 1;
                        fprintf(fd, " *obj)\n");
                        fprintf(fd, "{\n");
                        print_tabs(1, fd);
@@ -813,17 +839,18 @@ int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
                        fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n",
                                        basename);
                        print_tabs(2, fd);
-                       if(print_type(td->type, fd, 0, basename, "")) return 1;
+                       if(print_type(td,       fd, 0, basename, "")) return 1;
                        fprintf(fd, " obj)\n");
                        fprintf(fd, "{\n");
                        print_tabs(1, fd);
                        fprintf(fd, "return \n");
                        if(print_type_alignment(((field_t*)td->fields.array[0])->type,
-                                               fd, 0, basename, field->name)) return 1;
+                                               fd, 0, basename, ((field_t*)td->fields.array[0])->name)) return 1;
                        fprintf(fd, ";\n");
                        break;
                default:
-                       printf("print_type_alignment_fct : type has no alignment function.\n");
+                       dprintf("print_type_alignment_fct : type has no alignment function.\n");
+                       return 0;
                        break;
        }
 
@@ -832,6 +859,7 @@ int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
        fprintf(fd, "}\n");
        fprintf(fd, "\n");
 
+       return 0;
 }
 
 /* print type write function.
@@ -866,26 +894,32 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                strncat(basename, field_name, PATH_MAX - basename_len);
        }
        
+       switch(td->type) {
+               case SEQUENCE:
+               case STRUCT:
+               case UNION:
+               case ARRAY:
+                       break;
+               default:
+                       dprintf("print_type_write_fct : type has no write function.\n");
+                       return 0;
+                       break;
+       }
+       
        /* Print header */
        switch(td->type) {
                case SEQUENCE:
-                       fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n",
+                       fprintf(fd, "static inline void lttng_write_sequence_%s(\n",
                                        basename);
-
-                       fprintf(fd, "lttng_get_alignment_sequence_%s(&obj->%s)", basename,
-                                       field_name);
                        break;
                case STRUCT:
-                       fprintf(fd, "lttng_get_alignment_struct_%s(&obj->%s)", basename,
-                                       field_name);
+                       fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename);
                        break;
                case UNION:
-                       fprintf(fd, "lttng_get_alignment_union_%s(&obj->%s)", basename,
-                                       field_name);
+                       fprintf(fd, "static inline void lttng_write_union_%s(\n", basename);
                        break;
                case ARRAY:
-                       fprintf(fd, "lttng_get_alignment_array_%s(obj->%s)", basename,
-                                       field_name);
+                       fprintf(fd, "static inline void lttng_write_array_%s(\n", basename);
                        break;
                default:
                        printf("print_type_write_fct : type has no write function.\n");
@@ -903,7 +937,7 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
        print_tabs(2, fd);
        fprintf(fd, "size_t *len,\n");
        print_tabs(2, fd);
-       if(print_type(td->type, fd, 0, basename, "")) return 1;
+       if(print_type(td,       fd, 0, basename, "")) return 1;
 
        switch(td->type) {
                case SEQUENCE:
@@ -957,8 +991,8 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
        }
        
        print_tabs(1, fd);
-       fprintf(fd, "align = \n");
-       if(print_type_alignment(td, fd, 0, basename, field->name)) return 1;
+       fprintf(fd, "align = ");
+       if(print_type_alignment(td, fd, 0, basename, field_name)) return 1;
        fprintf(fd, ";\n");
        fprintf(fd, "\n");
        print_tabs(1, fd);
@@ -980,9 +1014,11 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                fprintf(fd, "/* Contains only fixed size fields : use compiler sizeof() */\n");
                fprintf(fd, "\n");
                print_tabs(1, fd);
-               fprintf(fd, "*len += sizeof(");
-               if(print_type(td, fd, 0, basename, field->name)) return 1;
+               fprintf(fd, "size = sizeof(");
+               if(print_type(td, fd, 0, basename, field_name)) return 1;
                fprintf(fd, ");\n");
+               print_tabs(1, fd);
+               fprintf(fd, "*len += size;\n");
        } else {
                /* The type contains nested variable size subtypes :
                 * we must write field by field. */
@@ -997,7 +1033,7 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                                print_tabs(1, fd);
                                fprintf(fd, "size = sizeof(\n");
                                if(print_type_write(((field_t*)td->fields.array[0])->type,
-                                               fd, 1, basename, field->name)) return 1;
+                                               fd, 1, basename, ((field_t*)td->fields.array[0])->name)) return 1;
                                fprintf(fd, ");\n");
                                print_tabs(1, fd);
                                fprintf(fd, "*to += ltt_align(*to, size);\n");
@@ -1015,7 +1051,7 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                                        print_tabs(1, fd);
                                        fprintf(fd, "size = sizeof(\n");
                                        if(print_type_write(((field_t*)td->fields.array[1])->type,
-                                                       fd, 1, basename, field->name)) return 1;
+                                                       fd, 1, basename, ((field_t*)td->fields.array[1])->name)) return 1;
                                        fprintf(fd, ");\n");
                                        print_tabs(1, fd);
                                        fprintf(fd, "*to += ltt_align(*to, size);\n");
@@ -1034,7 +1070,7 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                                        print_tabs(1, fd);
                                        fprintf(fd, "for(unsigned int i=0; i<obj->len; i++) {\n");
                                        if(print_type_write(((field_t*)td->fields.array[1])->type,
-                                                       fd, 2, basename, field->name)) return 1;
+                                                       fd, 2, basename, ((field_t*)td->fields.array[1])->name)) return 1;
                                        print_tabs(1, fd);
                                        fprintf(fd, "}\n");
                                }
@@ -1053,7 +1089,7 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                                fprintf(fd, "*from = obj+1;\n");
                                break;
                        case STRING:
-                               fprintf(fd, "size = strlen(obj);\n");
+                               fprintf(fd, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
                                print_tabs(1, fd);
                                fprintf(fd, "if(buffer != NULL)\n");
                                print_tabs(2, fd);
@@ -1099,7 +1135,7 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                                        print_tabs(1, fd);
                                        fprintf(fd, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename);
                                        if(print_type_write(((field_t*)td->fields.array[1])->type,
-                                                       fd, 2, basename, field->name)) return 1;
+                                                       fd, 2, basename, ((field_t*)td->fields.array[1])->name)) return 1;
                                        print_tabs(1, fd);
                                        fprintf(fd, "}\n");
                                }
@@ -1126,22 +1162,44 @@ int print_event_logging_function(char *basename, facility_t *fac,
                event_t *event, FILE *fd)
 {
        fprintf(fd, "static inline void trace_%s(\n", basename);
+       int     has_argument = 0;
+
+  /* Does it support per trace tracing ? */
+  if(event->per_trace) {
+               print_tabs(2, fd);
+    fprintf(fd, "struct ltt_trace_struct *dest_trace");
+               has_argument = 1;
+  }
+  
+  /* Does it support per tracefile tracing ? */
+  if(event->per_tracefile) {
+               if(has_argument) {
+                       fprintf(fd, ",");
+                       fprintf(fd, "\n");
+               }
+    fprintf(fd, "unsigned int tracefile_index");
+               has_argument = 1;
+  }
+
        for(unsigned int j = 0; j < event->fields.position; j++) {
                /* For each field, print the function argument */
                field_t *f = (field_t*)event->fields.array[j];
                type_descriptor_t *t = f->type;
-               if(print_arg(t, fd, 2, basename, f->name)) return 1;
-               if(j < event->fields.position-1) {
+               if(has_argument) {
                        fprintf(fd, ",");
                        fprintf(fd, "\n");
                }
+               if(print_arg(t, fd, 2, basename, f->name)) return 1;
+               has_argument = 1;
        }
-       if(event->fields.position == 0) {
+       if(!has_argument) {
                print_tabs(2, fd);
                fprintf(fd, "void");
        }
        fprintf(fd,")\n");
-       fprintf(fd, "#ifndef CONFIG_LTT\n");
+       fprintf(fd, 
+                       "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n",
+                       fac->capname);
        fprintf(fd, "{\n");
        fprintf(fd, "}\n");
        fprintf(fd,"#else\n");
@@ -1165,11 +1223,13 @@ int print_event_logging_function(char *basename, facility_t *fac,
        fprintf(fd, "void *from;");
        print_tabs(1, fd);
        fprintf(fd, "size_t len = 0;\n");
-       fprintf(fd, "\n");
+       print_tabs(1, fd);
+       fprintf(fd, "size_t reserve_size;\n");
+       print_tabs(1, fd);
        fprintf(fd, "size_t slot_size;\n");
-       fprintf(fd, "\n");
+       print_tabs(1, fd);
        fprintf(fd, "cycles_t tsc;\n");
-       fprintf(fd, "\n");
+       print_tabs(1, fd);
        fprintf(fd, "size_t before_hdr_pad, size_t after_hdr_pad;\n");
        fprintf(fd, "\n");
        
@@ -1187,18 +1247,23 @@ int print_event_logging_function(char *basename, facility_t *fac,
        print_tabs(1, fd);
        fprintf(fd, "/* size = to_base + to + len */\n");
        print_tabs(1, fd);
-       fprintf(fd, "/* Assume that the padding for alignment starts at a void*\n");
+       fprintf(fd, "/* Assume that the padding for alignment starts at a\n");
        print_tabs(1, fd);
-       fprintf(fd, "/* address. */\n");
+       fprintf(fd, " * sizeof(void *) address. */\n");
        fprintf(fd, "\n");
 
        for(unsigned int i=0;i<event->fields.position;i++){
-               field_t *field = (field_t*)(td->fields.array[i]);
+               field_t *field = (field_t*)(event->fields.array[i]);
                type_descriptor_t *type = field->type;
                if(print_type_write(type,
                                fd, 1, basename, field->name)) return 1;
                fprintf(fd, "\n");
        }
+       print_tabs(1, fd);
+       fprintf(fd, "reserve_size = to_base + to + len;\n");
+       print_tabs(1, fd);
+       fprintf(fd, "to_base = to = len = 0;\n");
+       fprintf(fd, "\n");
 
        /* Take locks : make sure the trace does not vanish while we write on
         * it. A simple preemption disabling is enough (using rcu traces). */
@@ -1216,7 +1281,7 @@ int print_event_logging_function(char *basename, facility_t *fac,
                print_tabs(1, fd);
                fprintf(fd, 
                        "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
-                                       "\t\t\t\tevent_%s);\n",
+                                       "\t\t\t\t\t\tevent_%s);\n",
                                fac->name, fac->checksum, event->name);
        }
        fprintf(fd,"\n");
@@ -1234,7 +1299,7 @@ int print_event_logging_function(char *basename, facility_t *fac,
        }
  
        print_tabs(2, fd);
-       fprintf(fp, "channel = ltt_get_channel_from_index(trace, index);\n");
+       fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n");
        print_tabs(2, fd);
        fprintf(fd, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n");
        fprintf(fd, "\n");
@@ -1246,8 +1311,11 @@ int print_event_logging_function(char *basename, facility_t *fac,
        print_tabs(2, fd);
        fprintf(fd, "slot_size = 0;\n");
        print_tabs(2, fd);
-       fprintf(fd, "buffer = ltt_reserve_slot(trace, relayfs_buf, to_base + to + len, &slot_size, &tsc,\n"
-               "\t\t&before_hdr_pad, &after_hdr_pad);\n");
+       fprintf(fd, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
+       print_tabs(3, fd);
+       fprintf(fd, "reserve_size, &slot_size, &tsc,\n");
+       print_tabs(3, fd);
+       fprintf(fd, "&before_hdr_pad, &after_hdr_pad);\n");
        /* If error, return */
        print_tabs(2, fd);
        fprintf(fd, "if(!buffer) return;\n\n");
@@ -1255,12 +1323,24 @@ int print_event_logging_function(char *basename, facility_t *fac,
        /* write data : assume stack alignment is the same as struct alignment. */
 
        for(unsigned int i=0;i<event->fields.position;i++){
-               field_t *field = (field_t*)(td->fields.array[i]);
+               field_t *field = (field_t*)(event->fields.array[i]);
                type_descriptor_t *type = field->type;
 
                /* Set from */
-               print_tabs(3, fd);
-               fprintf(fd, "from = %s;\n", f->name);
+               print_tabs(2, fd);
+               switch(type->type) {
+                       case SEQUENCE:
+                       case UNION:
+                       case ARRAY:
+                       case STRUCT:
+                       case STRING:
+                               fprintf(fd, "from = %s;\n", field->name);
+                               break;
+                       default:
+                               fprintf(fd, "from = &%s;\n", field->name);
+                               break;
+               }
+
 
                if(print_type_write(type,
                                fd, 2, basename, field->name)) return 1;
@@ -1299,7 +1379,8 @@ int print_event_logging_function(char *basename, facility_t *fac,
        fprintf(fd, "preempt_enable_no_resched();\n");
 
        fprintf(fd, "}\n");
-       fprintf(fd, "#endif //CONFIG_LTT\n\n");
+       fprintf(fd, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n",
+                       fac->capname);
        return 0;
 }
 
@@ -1311,9 +1392,10 @@ void print_log_header_head(facility_t *fac, FILE *fd)
 {
        fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname);
        fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname);
+  fprintf(fd, "#include <linux/types.h>\n");
+  fprintf(fd, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac->name);
+  fprintf(fd, "#include <linux/ltt-core.h>\n");
        fprintf(fd, "\n");
-       fprintf(fd, "/* Facility activation at compile time. */\n");
-       fprintf(fd, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac->capname);
 }
 
 
@@ -1328,6 +1410,12 @@ int print_log_header_types(facility_t *fac, FILE *fd)
                /* For each named type, print the definition */
                if((print_type_declaration(types->array[i], fd,
                                                0, "", ""))) return 1;
+               /* Print also the align function */
+               if((print_type_alignment_fct(types->array[i], fd,
+                                               0, "", ""))) return 1;
+               /* Print also the write function */
+               if((print_type_write_fct(types->array[i], fd,
+                                               0, "", ""))) return 1;
        }
        return 0;
 }
@@ -1354,9 +1442,15 @@ int print_log_header_events(facility_t *fac, FILE *fd)
                        /* For each unnamed type, print the definition */
                        field_t *f = (field_t*)event->fields.array[j];
                        type_descriptor_t *t = f->type;
-                       if(t->type_name == NULL)
+                       if(t->type_name == NULL) {
                                if((print_type_declaration(t, fd, 0, basename, f->name))) return 1;
+                               /* Print also the align function */
+                               if((print_type_alignment_fct(t, fd, 0, basename, f->name))) return 1;
+                               /* Print also the write function */
+                               if((print_type_write_fct(t, fd, 0, basename, f->name))) return 1;
+                       }
                }
+
                fprintf(fd, "\n");
 
                fprintf(fd, "/* Event %s logging function */\n",
@@ -1373,7 +1467,6 @@ int print_log_header_events(facility_t *fac, FILE *fd)
 
 void print_log_header_tail(facility_t *fac, FILE *fd)
 {
-       fprintf(fd, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac->capname);
        fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
 }
        
@@ -1427,6 +1520,9 @@ int print_id_header(facility_t *fac)
        char filename[PATH_MAX];
        unsigned int filename_size = 0;
        FILE *fd;
+       char basename[PATH_MAX];
+       char basename_len = 0;
+
        dprintf("%s\n", fac->name);
 
        strcpy(filename, "ltt-facility-id-");
@@ -1446,6 +1542,41 @@ int print_id_header(facility_t *fac)
                return errno;
        }
 
+  fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname);
+  fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname);
+  fprintf(fd, "#ifdef CONFIG_LTT\n");
+
+  fprintf(fd,"#include <linux/ltt-facilities.h>\n\n");
+
+  fprintf(fd,"/****  facility handle  ****/\n\n");
+  fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n",
+                       fac->name, fac->checksum);
+  fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name);
+
+       strncpy(basename, fac->name, PATH_MAX);
+       basename_len = strlen(basename);
+       strncat(basename, "_", PATH_MAX - basename_len);
+       basename_len++;
+       
+       fprintf(fd,"/****  event index  ****/\n\n");
+       fprintf(fd,"enum %s_event {\n",fac->name);
+       
+       for(unsigned int i = 0; i < fac->events.position; i++) {
+               event_t *event = (event_t*)fac->events.array[i];
+               strncpy(basename+basename_len, event->name, PATH_MAX-basename_len);
+               print_tabs(1, fd);
+               fprintf(fd, "event_%s,\n", basename);
+       }
+       print_tabs(1, fd);
+       fprintf(fd, "facility_%s_num_events\n", fac->name);
+       fprintf(fd, "};\n");
+       fprintf(fd, "\n");
+
+
+  fprintf(fd, "#endif //CONFIG_LTT\n");
+  fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname);
+
+
        fclose(fd);
 
        return 0;
@@ -1478,6 +1609,27 @@ int print_loader_header(facility_t *fac)
                return errno;
        }
 
+  fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
+  fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname);
+  fprintf(fd, "#ifdef CONFIG_LTT\n\n");
+  fprintf(fd,"#include <linux/ltt-facilities.h>\n");
+  fprintf(fd,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n",
+                       fac->name);
+  fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name);
+  fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
+                       fac->name, fac->checksum);
+
+  fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
+      fac->name);
+  fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
+      fac->name, fac->checksum);
+  fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac->checksum);
+  fprintf(fd,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac->name);
+  fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
+                       fac->name);
+  fprintf(fd, "#endif //CONFIG_LTT\n\n");
+  fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
+
        fclose(fd);
 
        return 0;
@@ -1509,6 +1661,92 @@ int print_loader_c(facility_t *fac)
                return errno;
        }
 
+  fprintf(fd, "/*\n");
+  fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name);
+  fprintf(fd, " *\n");
+  fprintf(fd, " * (C) Copyright  2005 - \n");
+  fprintf(fd, " *          Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
+  fprintf(fd, " *\n");
+  fprintf(fd, " * Contains the LTT facility loader.\n");
+  fprintf(fd, " *\n");
+  fprintf(fd, " */\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "#include <linux/ltt-facilities.h>\n");
+  fprintf(fd, "#include <linux/module.h>\n");
+  fprintf(fd, "#include <linux/init.h>\n");
+  fprintf(fd, "#include <linux/config.h>\n");
+  fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name);
+  fprintf(fd, "\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "#ifdef CONFIG_LTT\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
+  fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "#define SYMBOL_STRING(sym) #sym\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "static struct ltt_facility facility = {\n");
+  fprintf(fd, "\t.name = ltt_facility_name,\n");
+  fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
+  fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
+  fprintf(fd, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
+  fprintf(fd, "};\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "#ifndef MODULE\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "/* Built-in facility. */\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "static int __init facility_init(void)\n");
+  fprintf(fd, "{\n");
+  fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac->name);
+  fprintf(fd, "\n");
+  fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n");
+  fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
+  fprintf(fd, "\t\n");
+  fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n");
+  fprintf(fd, "}\n");
+  fprintf(fd, "__initcall(facility_init);\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "#else \n");
+  fprintf(fd, "\n");
+  fprintf(fd, "/* Dynamic facility. */\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "static int __init facility_init(void)\n");
+  fprintf(fd, "{\n");
+  fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", fac->name);
+  fprintf(fd, "\n");
+  fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n");
+  fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n");
+  fprintf(fd, "}\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "static void __exit facility_exit(void)\n");
+  fprintf(fd, "{\n");
+  fprintf(fd, "\tint err;\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n");
+  fprintf(fd, "\tif(err != 0)\n");
+  fprintf(fd, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "}\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "module_init(facility_init)\n");
+  fprintf(fd, "module_exit(facility_exit)\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "MODULE_LICENSE(\"GPL\");\n");
+  fprintf(fd, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
+  fprintf(fd, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "#endif //MODULE\n");
+  fprintf(fd, "\n");
+  fprintf(fd, "#endif //CONFIG_LTT\n");
 
        fclose(fd);
 
@@ -1536,7 +1774,6 @@ facility_t *ltt_facility_open(char * pathname)
        char *token;
        parse_file_t in;
        facility_t * fac = NULL;
-       unsigned long checksum;
        char buffer[BUFFER_SIZE];
        int generated = FALSE;
 
@@ -1577,7 +1814,7 @@ facility_t *ltt_facility_open(char * pathname)
                        //check if any namedType is not defined
                        checkNamedTypesImplemented(&fac->named_types);
                
-                       generateChecksum(fac->name, &checksum, &fac->events);
+                       generateChecksum(fac->name, &fac->checksum, &fac->events);
                        
                        generated = TRUE;
                }
This page took 0.035399 seconds and 4 git commands to generate.