+
+
+/* print type declaration.
+ *
+ * Copied from construct_types_and_fields in LTTV facility.c */
+
+int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs,
+ char *nest_name, char *field_name)
+{
+ char basename[PATH_MAX];
+ unsigned int basename_len = 0;
+
+ if(td->custom_write) return 0; /* Does print custom type */
+
+ strncpy(basename, nest_name, PATH_MAX);
+ basename_len = strlen(basename);
+
+ /* For a named type, we use the type_name directly */
+ if(td->type_name != NULL) {
+ strncpy(basename, td->type_name, PATH_MAX);
+ basename_len = strlen(basename);
+ } else {
+ /* For a unnamed type, there must be a field name, except for
+ * the array. */
+ if((basename_len != 0)
+ && (basename[basename_len-1] != '_'
+ && (field_name[0] != '\0'))) {
+ strncat(basename, "_", PATH_MAX - basename_len);
+ basename_len = strlen(basename);
+ }
+ strncat(basename, field_name, PATH_MAX - basename_len);
+ }
+
+ switch(td->type) {
+ case ENUM:
+ fprintf(fd, "enum lttng_%s", basename);
+ fprintf(fd, " {\n");
+ for(unsigned int i=0;i<td->labels.position;i++){
+ print_tabs(1, fd);
+ fprintf(fd, "LTTNG_%s = %d", ((char*)td->labels.array[i]),
+ (*(int*)td->labels_values.array[i]));
+ fprintf(fd, ",\n");
+ }
+ fprintf(fd, "};\n");
+ fprintf(fd, "\n");
+ break;
+
+ case ARRAY:
+ dprintf("%s\n", basename);
+ assert(td->size >= 0);
+ if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
+ /* Not a named nested type : we must print its declaration first */
+ if(print_type_declaration(((field_t*)td->fields.array[0])->type,
+ fd, 0, basename, "")) return 1;
+ }
+ 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,
+ fd, tabs, basename, "")) return 1;
+ fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename,
+ basename);
+ fprintf(fd, "\n");
+ break;
+ case SEQUENCE:
+ /* We assume that the sequence length type does not need to be declared.
+ */
+ if(((field_t*)td->fields.array[1])->type->type_name == NULL) {
+ /* Not a named nested type : we must print its declaration first */
+ if(print_type_declaration(((field_t*)td->fields.array[1])->type,
+ fd, 0, basename, "")) return 1;
+ }
+ fprintf(fd, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
+ basename,
+ basename);
+ fprintf(fd, "struct lttng_sequence_%s", basename);
+ fprintf(fd, " {\n");
+ print_tabs(1, fd);
+ if(print_type(((field_t*)td->fields.array[0])->type,
+ fd, tabs, basename, "")) return 1;
+ fprintf(fd, " len;\n");
+ print_tabs(1, fd);
+ fprintf(fd, "const ");
+ if(print_type(((field_t*)td->fields.array[1])->type,
+ fd, tabs, basename, "")) return 1;
+ fprintf(fd, " *array;\n");
+ fprintf(fd, "};\n"); /* We do not LTT_ALIGN, because we never copy
+ it to the buffer directly. */
+ fprintf(fd, "\n");
+ break;
+
+ case STRUCT:
+ 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;
+ if(type->type_name == NULL) {
+ /* Not a named nested type : we must print its declaration first */
+ if(print_type_declaration(type,
+ fd, 0, basename, field->name)) return 1;
+ }
+ }
+ fprintf(fd, "struct lttng_%s", basename);
+ fprintf(fd, " {\n");
+ 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;
+ print_tabs(1, fd);
+ if(print_type(type, fd, tabs, basename, field->name)) return 1;
+ fprintf(fd, " ");
+ fprintf(fd, "%s", field->name);
+ fprintf(fd, ";\n");
+ }
+ fprintf(fd, "} LTT_ALIGN;\n");
+ fprintf(fd, "\n");
+ break;
+ case 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;
+ if(type->type_name == NULL) {
+ /* Not a named nested type : we must print its declaration first */
+ if(print_type_declaration(type,
+ fd, 0, basename, field->name)) return 1;
+ }
+ }
+ fprintf(fd, "union lttng_%s", basename);
+ fprintf(fd, " {\n");
+ for(unsigned i=0;i<td->fields.position;i++){
+ field_t *field = (field_t*)(td->fields.array[i]);
+ type_descriptor_t *type = field->type;
+ print_tabs(1, fd);
+ if(print_type(type, fd, tabs, basename, field->name)) return 1;
+ fprintf(fd, " ");
+ fprintf(fd, "%s", field->name);
+ fprintf(fd, ";\n");
+ }
+ fprintf(fd, "} LTT_ALIGN;\n");
+ fprintf(fd, "\n");
+ break;
+ default:
+ dprintf("print_type_declaration : unknown type or nothing to declare.\n");
+ break;
+ }
+
+ return 0;
+}
+
+
+/* print type alignment.
+ *
+ * Copied from construct_types_and_fields in LTTV facility.c
+ *
+ * basename is the name which identifies the type (along with a prefix
+ * (possibly)). */
+
+int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs,
+ char *nest_name, char *field_name, char *obj_prefix)
+{
+ char basename[PATH_MAX];
+ unsigned int basename_len = 0;
+
+ strncpy(basename, nest_name, PATH_MAX);
+ basename_len = strlen(basename);
+
+ /* For a named type, we use the type_name directly */
+ if(td->type_name != NULL) {
+ strncpy(basename, td->type_name, PATH_MAX);
+ basename_len = strlen(basename);
+ } else {
+ /* For a unnamed type, there must be a field name, except for
+ * 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);
+ }
+ if(field_name != NULL)
+ strncat(basename, field_name, PATH_MAX - basename_len);
+ }
+
+ 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(%s)", basename,
+ obj_prefix);
+ break;
+ case STRUCT:
+ fprintf(fd, "lttng_get_alignment_struct_%s(%s)", basename,
+ obj_prefix);
+ break;
+ case UNION:
+ fprintf(fd, "lttng_get_alignment_union_%s(%s)", basename,
+ obj_prefix);
+ break;
+ case ARRAY:
+ fprintf(fd, "lttng_get_alignment_array_%s(%s)", basename,
+ obj_prefix);
+ case STRING:
+ fprintf(fd, "sizeof(char)");
+ break;
+ 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;
+
+ 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(&%s%s)", basename,
+ obj_prefix, field_name);
+ break;
+ case STRUCT:
+ fprintf(fd, "lttng_get_alignment_struct_%s(&%s%s)", basename,
+ obj_prefix, field_name);
+ break;
+ case UNION:
+ fprintf(fd, "lttng_get_alignment_union_%s(&%s%s)", basename,
+ obj_prefix, field_name);
+ break;
+ case ARRAY:
+ fprintf(fd, "lttng_get_alignment_array_%s(%s%s)", basename,
+ obj_prefix, field_name);
+ break;
+ case NONE:
+ printf("error : type NONE unexpected\n");
+ return 1;
+ break;
+ }
+ }
+
+ return 0;
+}
+
+/* print type write.
+ *
+ * Copied from construct_types_and_fields in LTTV facility.c
+ *
+ * basename is the name which identifies the type (along with a prefix
+ * (possibly)). */
+
+int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs,
+ char *nest_name, char *field_name, char *obj_prefix, int get_ptr)
+{
+ char basename[PATH_MAX];
+ unsigned int basename_len = 0;
+ char get_ptr_char[2] = "";
+ char custom[PATH_MAX] = "";
+
+ strncpy(basename, nest_name, PATH_MAX);
+ basename_len = strlen(basename);
+
+ /* For a named type, we use the type_name directly */
+ if(td->type_name != NULL) {
+ strncpy(basename, td->type_name, PATH_MAX);
+ basename_len = strlen(basename);
+ } else {
+ /* For a unnamed type, there must be a field name, except for
+ * the array. */
+ if((basename_len != 0)
+ && (basename[basename_len-1] != '_'
+ && (field_name[0] != '\0'))) {
+ strncat(basename, "_", PATH_MAX - basename_len);
+ basename_len = strlen(basename);
+ }
+ strncat(basename, field_name, PATH_MAX - basename_len);
+ }
+
+ if(get_ptr) {
+ strcpy(get_ptr_char, "&");
+ }
+
+ if(td->custom_write) {
+ strcpy(custom, "_custom");
+ }
+
+ 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:
+ print_tabs(tabs, fd);
+ fprintf(fd, "align = ");
+ if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1;
+ fprintf(fd, ";\n");
+ fprintf(fd, "\n");
+ print_tabs(tabs, fd);
+ fprintf(fd, "if(*len == 0) {\n");
+ print_tabs(tabs+1, fd);
+ fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n");
+ print_tabs(tabs, fd);
+ fprintf(fd, "} else {\n");
+ print_tabs(tabs+1, fd);
+ fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
+ print_tabs(tabs, fd);
+ fprintf(fd, "}\n");
+ fprintf(fd, "\n");
+
+ print_tabs(tabs, fd);
+ fprintf(fd, "*len += ");
+ fprintf(fd, "sizeof(");
+ if(print_type(td, fd, 0, basename, "")) return 1;
+ fprintf(fd, ");\n");
+
+ break;
+ case STRING:
+ print_tabs(tabs, fd);
+ fprintf(fd,
+ "lttng_write%s_string_%s(buffer, to_base, to, from, len, %s%s);\n",
+ custom, basename, obj_prefix, field_name);
+ break;
+ case SEQUENCE:
+ print_tabs(tabs, fd);
+ fprintf(fd,
+ "lttng_write%s_sequence_%s(buffer, to_base, to, from, len, %s%s%s);",
+ custom, basename, get_ptr_char, obj_prefix, field_name);
+ break;
+ case STRUCT:
+ print_tabs(tabs, fd);
+ fprintf(fd,
+ "lttng_write%s_struct_%s(buffer, to_base, to, from, len, %s%s%s);",
+ custom, basename, get_ptr_char, obj_prefix, field_name);
+ break;
+ case UNION:
+ print_tabs(tabs, fd);
+ fprintf(fd,
+ "lttng_write%s_union_%s(buffer, to_base, to, from, len, %s%s%s);",
+ custom, basename, get_ptr_char, obj_prefix, field_name);
+ break;
+ case ARRAY:
+ print_tabs(tabs, fd);
+ fprintf(fd,
+ "lttng_write%s_array_%s(buffer, to_base, to, from, len, %s%s);",
+ custom, basename, obj_prefix, field_name);
+ break;
+ case NONE:
+ printf("Error : type NONE unexpected\n");
+ return 1;
+ break;
+ }
+
+ return 0;
+}
+
+/* print need local vars ?.
+ *
+ * Copied from print_type_write
+ *
+ * Does the type_write call needs local size and from variables ?
+ * return value : 1 yes, 0 no.
+ */
+
+int has_type_local(type_descriptor_t * td)
+{
+ 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:
+ return 1;
+ break;
+ case STRING:
+ case SEQUENCE:
+ case STRUCT:
+ case UNION:
+ case ARRAY:
+ return 0;
+ break;
+ case NONE:
+ printf("Error : type NONE unexpected\n");
+ return 1;
+ break;
+ }
+
+ return 0;
+}
+
+
+
+/* print type alignment function.
+ *
+ * Copied from construct_types_and_fields in LTTV facility.c
+ *
+ * basename is the name which identifies the type (along with a prefix
+ * (possibly)). */
+
+int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
+ unsigned int tabs,
+ char *nest_name, char *field_name)
+{
+ char basename[PATH_MAX];
+ unsigned int basename_len = 0;
+
+ if(td->custom_write) return 0; /* Does print custom type */
+
+ strncpy(basename, nest_name, PATH_MAX);
+ basename_len = strlen(basename);
+
+ /* For a named type, we use the type_name directly */
+ if(td->type_name != NULL) {
+ strncpy(basename, td->type_name, PATH_MAX);
+ basename_len = strlen(basename);
+ } else {
+ /* For a unnamed type, there must be a field name, except for
+ * the array. */
+ if((basename_len != 0)
+ && (basename[basename_len-1] != '_'
+ && (field_name[0] != '\0'))) {
+ strncat(basename, "_", PATH_MAX - basename_len);
+ basename_len = strlen(basename);
+ }
+ strncat(basename, field_name, PATH_MAX - basename_len);
+ }
+
+ switch(td->type) {
+ case SEQUENCE:
+ if(((field_t*)td->fields.array[1])->type->type_name == NULL) {
+ /* Not a named nested type : we must print its align fct */
+ if(print_type_alignment_fct(((field_t*)td->fields.array[1])->type, fd,
+ 0, basename, "")) return 1;
+ }
+ /* Function header */
+ fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n",
+ basename);
+ print_tabs(2, fd);
+ if(print_type(td, fd, 0, basename, "")) return 1;
+ fprintf(fd, " *obj)\n");
+ fprintf(fd, "{\n");
+ print_tabs(1, fd);
+ fprintf(fd, "size_t align=0, localign;");
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "localign = ");
+ if(print_type_alignment(((field_t*)td->fields.array[0])->type,
+ fd, 0, basename, "len", "obj->")) return 1;
+ fprintf(fd, ";\n");
+ print_tabs(1, fd);
+ fprintf(fd, "align = max(align, localign);\n");
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "localign = ");
+ if(print_type_alignment(((field_t*)td->fields.array[1])->type,
+ fd, 0, basename, "array[0]", "obj->")) return 1;
+ fprintf(fd, ";\n");
+ print_tabs(1, fd);
+ fprintf(fd, "align = max(align, localign);\n");
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "return align;\n");
+ break;
+ case STRUCT:
+ 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;
+ if(type->type_name == NULL) {
+ /* Not a named nested type : we must print its align fct */
+ if(print_type_alignment_fct(type, fd,
+ 0, basename, field->name)) return 1;
+ }
+ }
+ /* Function header */
+ fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n",
+ basename);
+ print_tabs(2, fd);
+ if(print_type(td, fd, 0, basename, "")) return 1;
+ fprintf(fd, " *obj)\n");
+ fprintf(fd, "{\n");
+ print_tabs(1, fd);
+ fprintf(fd, "size_t align=0, localign;");
+ fprintf(fd, "\n");
+ 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;
+ print_tabs(1, fd);
+ fprintf(fd, "localign = ");
+ if(print_type_alignment(type, fd, 0, basename, field->name, "obj->"))
+ return 1;
+ fprintf(fd, ";\n");
+ print_tabs(1, fd);
+ fprintf(fd, "align = max(align, localign);\n");
+ fprintf(fd, "\n");
+ }
+ print_tabs(1, fd);
+ fprintf(fd, "return align;\n");
+
+ break;
+ case 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;
+ if(type->type_name == NULL) {
+ /* Not a named nested type : we must print its align fct */
+ if(print_type_alignment_fct(type, fd,
+ 0, basename, field->name)) return 1;
+ }
+ }
+ /* Function header */
+ fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n",
+ basename);
+ print_tabs(2, fd);
+ if(print_type(td, fd, 0, basename, "")) return 1;
+ fprintf(fd, " *obj)\n");
+ fprintf(fd, "{\n");
+ print_tabs(1, fd);
+ fprintf(fd, "size_t align=0, localign;");
+ fprintf(fd, "\n");
+ 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;
+ print_tabs(1, fd);
+ fprintf(fd, "localign = ");
+ if(print_type_alignment(type, fd, 0, basename, field->name, "obj->"))
+ return 1;
+ fprintf(fd, ";\n");
+ print_tabs(1, fd);
+ fprintf(fd, "align = max(align, localign);\n");
+ fprintf(fd, "\n");
+ }
+ print_tabs(1, fd);
+ fprintf(fd, "return align;\n");
+
+ break;
+ case ARRAY:
+ if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
+ /* Not a named nested type : we must print its align fct */
+ if(print_type_alignment_fct(((field_t*)td->fields.array[0])->type, fd,
+ 0, basename, "")) return 1;
+ }
+ /* Function header */
+ fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n",
+ basename);
+ print_tabs(2, fd);
+ 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, "", "obj[0]"))
+ return 1;
+ fprintf(fd, ";\n");
+ break;
+ default:
+ dprintf("print_type_alignment_fct : type has no alignment function.\n");
+ return 0;
+ break;
+ }
+
+
+ /* Function footer */
+ fprintf(fd, "}\n");
+ fprintf(fd, "\n");
+
+ return 0;
+}
+
+/* print type write function.
+ *
+ * Copied from construct_types_and_fields in LTTV facility.c
+ *
+ * basename is the name which identifies the type (along with a prefix
+ * (possibly)). */
+
+int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
+ char *nest_name, char *field_name)
+{
+ char basename[PATH_MAX];
+ unsigned int basename_len = 0;
+
+ if(td->custom_write) return 0; /* Does print custom type */
+
+ strncpy(basename, nest_name, PATH_MAX);
+ basename_len = strlen(basename);
+
+ /* For a named type, we use the type_name directly */
+ if(td->type_name != NULL) {
+ strncpy(basename, td->type_name, PATH_MAX);
+ basename_len = strlen(basename);
+ } else {
+ /* For a unnamed type, there must be a field name, except for
+ * the array. */
+ if((basename_len != 0)
+ && (basename[basename_len-1] != '_'
+ && (field_name[0] != '\0'))) {
+ strncat(basename, "_", PATH_MAX - basename_len);
+ basename_len = strlen(basename);
+ }
+ strncat(basename, field_name, PATH_MAX - basename_len);
+ }
+
+ switch(td->type) {
+ case SEQUENCE:
+ if(((field_t*)td->fields.array[1])->type->type_name == NULL) {
+ /* Not a named nested type : we must print its write fct */
+ if(print_type_write_fct(((field_t*)td->fields.array[1])->type, fd,
+ 0, basename, "")) return 1;
+ }
+ break;
+ case STRUCT:
+ 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;
+ if(type->type_name == NULL) {
+ /* Not a named nested type : we must print its write fct */
+ if(print_type_write_fct(type, fd,
+ 0, basename, field->name)) return 1;
+ }
+ }
+ break;
+ case 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;
+ if(type->type_name == NULL) {
+ /* Not a named nested type : we must print its write fct */
+ if(print_type_write_fct(type, fd,
+ 0, basename, field->name)) return 1;
+ }
+ }
+ break;
+ case ARRAY:
+ if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
+ /* Not a named nested type : we must print its write fct */
+ if(print_type_write_fct(((field_t*)td->fields.array[0])->type, fd,
+ 0, basename, "")) return 1;
+ }
+ break;
+ case STRING:
+ 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 void lttng_write_sequence_%s(\n",
+ basename);
+ break;
+ case STRUCT:
+ fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename);
+ break;
+ case UNION:
+ fprintf(fd, "static inline void lttng_write_union_%s(\n", basename);
+ break;
+ case ARRAY:
+ fprintf(fd, "static inline void lttng_write_array_%s(\n", basename);
+ break;
+ case STRING:
+ fprintf(fd, "static inline void lttng_write_string_%s(\n", basename);
+ break;
+ default:
+ printf("print_type_write_fct : type has no write function.\n");
+ break;
+ }
+
+ print_tabs(2, fd);
+ fprintf(fd, "void *buffer,\n");
+ print_tabs(2, fd);
+ fprintf(fd, "size_t *to_base,\n");
+ print_tabs(2, fd);
+ fprintf(fd, "size_t *to,\n");
+ print_tabs(2, fd);
+ fprintf(fd, "const void **from,\n");
+ print_tabs(2, fd);
+ fprintf(fd, "size_t *len,\n");
+ print_tabs(2, fd);
+ if(print_type(td, fd, 0, basename, "")) return 1;
+
+ switch(td->type) {
+ case SEQUENCE:
+ fprintf(fd, " *obj)\n");
+ break;
+ case STRUCT:
+ fprintf(fd, " *obj)\n");
+ break;
+ case UNION:
+ fprintf(fd, " *obj)\n");
+ break;
+ case ARRAY:
+ fprintf(fd, " obj)\n");
+ break;
+ case STRING:
+ fprintf(fd, " obj)\n");
+ break;
+ default:
+ printf("print_type_write_fct : type has no write function.\n");
+ break;
+ }
+
+ fprintf(fd, "{\n");
+
+ switch(td->type) {
+ case STRING:
+ print_tabs(1, fd);
+ fprintf(fd, "size_t size;\n");
+ break;
+ default:
+ break;
+ }
+
+ print_tabs(1, fd);
+ fprintf(fd, "size_t align;\n");
+ fprintf(fd, "\n");
+
+ switch(td->type) {
+ case SEQUENCE:
+ case STRING:
+ print_tabs(1, fd);
+ fprintf(fd, "/* Flush pending memcpy */\n");
+ print_tabs(1, fd);
+ fprintf(fd, "if(*len != 0) {\n");
+ print_tabs(2, fd);
+ fprintf(fd, "if(buffer != NULL)\n");
+ print_tabs(3, fd);
+ fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
+ print_tabs(1, fd);
+ fprintf(fd, "}\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to += *len;\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*len = 0;\n");
+ fprintf(fd, "\n");
+ break;
+ case STRUCT:
+ case UNION:
+ case ARRAY:
+ break;
+ default:
+ printf("print_type_write_fct : type has no write function.\n");
+ break;
+ }
+
+ print_tabs(1, fd);
+ fprintf(fd, "align = ");
+ if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1;
+ fprintf(fd, ";\n");
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "if(*len == 0) {\n");
+ print_tabs(2, fd);
+ fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n");
+ print_tabs(1, fd);
+ fprintf(fd, "} else {\n");
+ print_tabs(2, fd);
+ fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
+ print_tabs(1, fd);
+ fprintf(fd, "}\n");
+ fprintf(fd, "\n");
+
+ /* First, check if the type has a fixed size. If it is the case, then the size
+ * to write is know by the compiler : simply use a sizeof() */
+ if(has_type_fixed_size(td)) {
+ print_tabs(1, fd);
+ 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, ");\n");
+ } else {
+ /* The type contains nested variable size subtypes :
+ * we must write field by field. */
+ print_tabs(1, fd);
+ fprintf(fd, "/* Contains variable sized fields : must explode the structure */\n");
+ fprintf(fd, "\n");
+
+ switch(td->type) {
+ case SEQUENCE:
+ print_tabs(1, fd);
+ fprintf(fd, "/* Copy members */\n");
+// print_tabs(1, fd);
+// fprintf(fd, "size = sizeof(\n");
+ if(print_type_write(((field_t*)td->fields.array[0])->type,
+ fd, 1, basename, "len", "obj->", 1)) return 1;
+ fprintf(fd, "\n");
+// fprintf(fd, ");\n");
+// print_tabs(1, fd);
+// fprintf(fd, "*to += ltt_align(*to, size);\n");
+ print_tabs(1, fd);
+ fprintf(fd, "if(buffer != NULL)\n");
+ print_tabs(2, fd);
+ fprintf(fd, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to += *len;\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*len = 0;\n");
+ fprintf(fd, "\n");
+
+ /* Write the child : varlen child or not ? */
+ if(has_type_fixed_size(((field_t*)td->fields.array[1])->type)) {
+ /* Fixed size len child : use a multiplication of its size */
+// print_tabs(1, fd);
+// fprintf(fd, "size = sizeof(\n");
+
+ //print_tabs(1, fd);
+ /* We know that *len does not contain alignment because of the
+ * previous align output. len is always 0 here. */
+ if(print_type_write(((field_t*)td->fields.array[1])->type,
+ fd, 1, basename, "array[0]", "obj->", 1))
+ return 1;
+// fprintf(fd, ");\n");
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*len = obj->len * (*len);\n");
+ print_tabs(1, fd);
+ fprintf(fd, "if(buffer != NULL)\n");
+ print_tabs(2, fd);
+ fprintf(fd, "memcpy(buffer+*to_base+*to, obj->array, *len);\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to += *len;\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*len = 0;\n");
+ fprintf(fd, "\n");
+ } else {
+ print_tabs(1, fd);
+ fprintf(fd, "/* Variable length child : iter. */\n");
+ 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, "array[i]", "obj->", 1)) return 1;
+ print_tabs(1, fd);
+ fprintf(fd, "}\n");
+ }
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to_base = *to_base+*to;\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to = 0;\n");
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "/* Put source *from just after the C sequence */\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*from = obj+1;\n");
+ break;
+ case STRING:
+ print_tabs(1, fd);
+ 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);
+ fprintf(fd, "memcpy(buffer+*to_base+*to, obj, size);\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to += size;\n");
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to_base = *to_base+*to;\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*to = 0;\n");
+ fprintf(fd, "\n");
+ print_tabs(1, fd);
+ fprintf(fd, "/* Put source *from just after the C string */\n");
+ print_tabs(1, fd);
+ fprintf(fd, "*from += size;\n");
+ break;
+ case STRUCT:
+ 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;
+ if(print_type_write(type,
+ fd, 1, basename, field->name, "obj->", 1)) return 1;
+ fprintf(fd, "\n");
+ }
+ break;
+ case UNION:
+ printf("ERROR : A union CANNOT contain a variable size child.\n");
+ return 1;
+ break;
+ case ARRAY:
+ /* Write the child : varlen child or not ? */
+ if(has_type_fixed_size(((field_t*)td->fields.array[0])->type)) {
+ /* Error : if an array has a variable size, then its child must also
+ * have a variable size. */
+ assert(0);
+ } else {
+ print_tabs(1, fd);
+ fprintf(fd, "/* Variable length child : iter. */\n");
+ 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[0])->type,
+ fd, 2, basename, "", "obj->array[i]", 1)) return 1;
+ print_tabs(1, fd);
+ fprintf(fd, "}\n");
+ }
+ break;
+ default:
+ printf("print_type_write_fct : type has no write function.\n");
+ break;
+ }
+
+
+ }
+
+
+ /* Function footer */
+ fprintf(fd, "}\n");
+ fprintf(fd, "\n");
+ return 0;
+}
+
+
+
+/* Print the logging function of an event. This is the core of genevent */
+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;
+ int has_type_fixed = 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;