fix code for alignment and write
[lttv.git] / genevent-new / genevent.c
index 4042dafafdeda23986643873853d0e60e71b8c3b..006c902fe721157a91bcc755497afbdd42efbe16 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 */
@@ -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,
@@ -548,11 +543,13 @@ 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) {
@@ -573,8 +570,7 @@ int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                case OFF_T:
                case ENUM:
                        fprintf(fd, "sizeof(");
-                       if(print_type(td->type,
-                                               fd, 0, basename, "")) return 1;
+                       if(print_type(td, fd, 0, basename, "")) return 1;
                        fprintf(fd, ")");
                        break;
                case STRING:
@@ -596,6 +592,10 @@ int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                        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 +653,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 +684,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 +734,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 +743,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 +751,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 +764,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 +790,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 +816,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");
+                       return 0;
                        break;
        }
 
@@ -832,6 +836,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 +871,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:
+                       printf("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 +914,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 +968,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);
@@ -981,7 +992,7 @@ int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
                fprintf(fd, "\n");
                print_tabs(1, fd);
                fprintf(fd, "*len += sizeof(");
-               if(print_type(td, fd, 0, basename, field->name)) return 1;
+               if(print_type(td, fd, 0, basename, field_name)) return 1;
                fprintf(fd, ");\n");
        } else {
                /* The type contains nested variable size subtypes :
@@ -997,7 +1008,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 +1026,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 +1045,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");
                                }
@@ -1099,7 +1110,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");
                                }
@@ -1165,11 +1176,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 +1200,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 +1234,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 +1252,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 +1264,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 +1276,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;
@@ -1328,6 +1361,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 +1393,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",
@@ -1536,7 +1581,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 +1621,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.032073 seconds and 4 git commands to generate.