| 1 | /* |
| 2 | * ctf-visitor-generate-io-struct.c |
| 3 | * |
| 4 | * Common Trace Format Metadata Visitor (generate I/O structures). |
| 5 | * |
| 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <assert.h> |
| 24 | #include <glib.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <endian.h> |
| 27 | #include <errno.h> |
| 28 | #include <babeltrace/list.h> |
| 29 | #include <babeltrace/types.h> |
| 30 | #include <babeltrace/ctf/metadata.h> |
| 31 | #include <uuid/uuid.h> |
| 32 | #include "ctf-scanner.h" |
| 33 | #include "ctf-parser.h" |
| 34 | #include "ctf-ast.h" |
| 35 | |
| 36 | #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args) |
| 37 | |
| 38 | #define _cds_list_first_entry(ptr, type, member) \ |
| 39 | cds_list_entry((ptr)->next, type, member) |
| 40 | |
| 41 | static |
| 42 | struct declaration *ctf_type_specifier_list_visit(FILE *fd, |
| 43 | int depth, struct ctf_node *type_specifier_list, |
| 44 | struct declaration_scope *declaration_scope, |
| 45 | struct ctf_trace *trace); |
| 46 | |
| 47 | static |
| 48 | int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node, |
| 49 | struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace); |
| 50 | |
| 51 | /* |
| 52 | * String returned must be freed by the caller using g_free. |
| 53 | */ |
| 54 | static |
| 55 | char *concatenate_unary_strings(struct cds_list_head *head) |
| 56 | { |
| 57 | struct ctf_node *node; |
| 58 | GString *str; |
| 59 | int i = 0; |
| 60 | |
| 61 | str = g_string_new(""); |
| 62 | cds_list_for_each_entry(node, head, siblings) { |
| 63 | char *src_string; |
| 64 | |
| 65 | assert(node->type == NODE_UNARY_EXPRESSION); |
| 66 | assert(node->u.unary_expression.type == UNARY_STRING); |
| 67 | assert((node->u.unary_expression.link == UNARY_LINK_UNKNOWN) |
| 68 | ^ (i != 0)); |
| 69 | switch (node->u.unary_expression.link) { |
| 70 | case UNARY_DOTLINK: |
| 71 | g_string_append(str, "."); |
| 72 | break; |
| 73 | case UNARY_ARROWLINK: |
| 74 | g_string_append(str, "->"); |
| 75 | break; |
| 76 | case UNARY_DOTDOTDOT: |
| 77 | g_string_append(str, "..."); |
| 78 | break; |
| 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | src_string = node->u.unary_expression.u.string; |
| 83 | g_string_append(str, src_string); |
| 84 | i++; |
| 85 | } |
| 86 | return g_string_free(str, FALSE); |
| 87 | } |
| 88 | |
| 89 | static |
| 90 | int get_unary_unsigned(struct cds_list_head *head, uint64_t *value) |
| 91 | { |
| 92 | struct ctf_node *node; |
| 93 | int i = 0; |
| 94 | |
| 95 | cds_list_for_each_entry(node, head, siblings) { |
| 96 | assert(node->type == NODE_UNARY_EXPRESSION); |
| 97 | assert(node->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT); |
| 98 | assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN); |
| 99 | assert(i == 0); |
| 100 | *value = node->u.unary_expression.u.unsigned_constant; |
| 101 | i++; |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static |
| 107 | int get_unary_uuid(struct cds_list_head *head, uuid_t *uuid) |
| 108 | { |
| 109 | struct ctf_node *node; |
| 110 | int i = 0; |
| 111 | int ret = -1; |
| 112 | |
| 113 | cds_list_for_each_entry(node, head, siblings) { |
| 114 | const char *src_string; |
| 115 | |
| 116 | assert(node->type == NODE_UNARY_EXPRESSION); |
| 117 | assert(node->u.unary_expression.type == UNARY_STRING); |
| 118 | assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN); |
| 119 | assert(i == 0); |
| 120 | src_string = node->u.unary_expression.u.string; |
| 121 | ret = uuid_parse(node->u.unary_expression.u.string, *uuid); |
| 122 | } |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | static |
| 127 | struct ctf_stream *trace_stream_lookup(struct ctf_trace *trace, uint64_t stream_id) |
| 128 | { |
| 129 | if (trace->streams->len <= stream_id) |
| 130 | return NULL; |
| 131 | return g_ptr_array_index(trace->streams, stream_id); |
| 132 | } |
| 133 | |
| 134 | static |
| 135 | int visit_type_specifier(FILE *fd, struct ctf_node *type_specifier, GString *str) |
| 136 | { |
| 137 | assert(type_specifier->type == NODE_TYPE_SPECIFIER); |
| 138 | |
| 139 | switch (type_specifier->u.type_specifier.type) { |
| 140 | case TYPESPEC_VOID: |
| 141 | g_string_append(str, "void"); |
| 142 | break; |
| 143 | case TYPESPEC_CHAR: |
| 144 | g_string_append(str, "char"); |
| 145 | break; |
| 146 | case TYPESPEC_SHORT: |
| 147 | g_string_append(str, "short"); |
| 148 | break; |
| 149 | case TYPESPEC_INT: |
| 150 | g_string_append(str, "int"); |
| 151 | break; |
| 152 | case TYPESPEC_LONG: |
| 153 | g_string_append(str, "long"); |
| 154 | break; |
| 155 | case TYPESPEC_FLOAT: |
| 156 | g_string_append(str, "float"); |
| 157 | break; |
| 158 | case TYPESPEC_DOUBLE: |
| 159 | g_string_append(str, "double"); |
| 160 | break; |
| 161 | case TYPESPEC_SIGNED: |
| 162 | g_string_append(str, "signed"); |
| 163 | break; |
| 164 | case TYPESPEC_UNSIGNED: |
| 165 | g_string_append(str, "unsigned"); |
| 166 | break; |
| 167 | case TYPESPEC_BOOL: |
| 168 | g_string_append(str, "bool"); |
| 169 | break; |
| 170 | case TYPESPEC_COMPLEX: |
| 171 | g_string_append(str, "_Complex"); |
| 172 | break; |
| 173 | case TYPESPEC_IMAGINARY: |
| 174 | g_string_append(str, "_Imaginary"); |
| 175 | break; |
| 176 | case TYPESPEC_CONST: |
| 177 | g_string_append(str, "const"); |
| 178 | break; |
| 179 | case TYPESPEC_ID_TYPE: |
| 180 | if (type_specifier->u.type_specifier.id_type) |
| 181 | g_string_append(str, type_specifier->u.type_specifier.id_type); |
| 182 | break; |
| 183 | case TYPESPEC_STRUCT: |
| 184 | { |
| 185 | struct ctf_node *node = type_specifier->u.type_specifier.node; |
| 186 | |
| 187 | if (!node->u._struct.name) { |
| 188 | fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__); |
| 189 | return -EINVAL; |
| 190 | } |
| 191 | g_string_append(str, "struct "); |
| 192 | g_string_append(str, node->u._struct.name); |
| 193 | break; |
| 194 | } |
| 195 | case TYPESPEC_VARIANT: |
| 196 | { |
| 197 | struct ctf_node *node = type_specifier->u.type_specifier.node; |
| 198 | |
| 199 | if (!node->u.variant.name) { |
| 200 | fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__); |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | g_string_append(str, "variant "); |
| 204 | g_string_append(str, node->u.variant.name); |
| 205 | break; |
| 206 | } |
| 207 | case TYPESPEC_ENUM: |
| 208 | { |
| 209 | struct ctf_node *node = type_specifier->u.type_specifier.node; |
| 210 | |
| 211 | if (!node->u._enum.enum_id) { |
| 212 | fprintf(fd, "[error] %s: unexpected empty enum ID\n", __func__); |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | g_string_append(str, "enum "); |
| 216 | g_string_append(str, node->u._enum.enum_id); |
| 217 | break; |
| 218 | } |
| 219 | case TYPESPEC_FLOATING_POINT: |
| 220 | case TYPESPEC_INTEGER: |
| 221 | case TYPESPEC_STRING: |
| 222 | default: |
| 223 | fprintf(fd, "[error] %s: unknown specifier\n", __func__); |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static |
| 230 | int visit_type_specifier_list(FILE *fd, struct ctf_node *type_specifier_list, GString *str) |
| 231 | { |
| 232 | struct ctf_node *iter; |
| 233 | int alias_item_nr = 0; |
| 234 | int ret; |
| 235 | |
| 236 | cds_list_for_each_entry(iter, &type_specifier_list->u.type_specifier_list.head, siblings) { |
| 237 | if (alias_item_nr != 0) |
| 238 | g_string_append(str, " "); |
| 239 | alias_item_nr++; |
| 240 | ret = visit_type_specifier(fd, iter, str); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static |
| 248 | GQuark create_typealias_identifier(FILE *fd, int depth, |
| 249 | struct ctf_node *type_specifier_list, |
| 250 | struct ctf_node *node_type_declarator) |
| 251 | { |
| 252 | struct ctf_node *iter; |
| 253 | GString *str; |
| 254 | char *str_c; |
| 255 | GQuark alias_q; |
| 256 | int ret; |
| 257 | |
| 258 | str = g_string_new(""); |
| 259 | ret = visit_type_specifier_list(fd, type_specifier_list, str); |
| 260 | if (ret) { |
| 261 | g_string_free(str, TRUE); |
| 262 | return 0; |
| 263 | } |
| 264 | cds_list_for_each_entry(iter, &node_type_declarator->u.type_declarator.pointers, siblings) { |
| 265 | g_string_append(str, " *"); |
| 266 | if (iter->u.pointer.const_qualifier) |
| 267 | g_string_append(str, " const"); |
| 268 | } |
| 269 | str_c = g_string_free(str, FALSE); |
| 270 | alias_q = g_quark_from_string(str_c); |
| 271 | g_free(str_c); |
| 272 | return alias_q; |
| 273 | } |
| 274 | |
| 275 | static |
| 276 | struct declaration *ctf_type_declarator_visit(FILE *fd, int depth, |
| 277 | struct ctf_node *type_specifier_list, |
| 278 | GQuark *field_name, |
| 279 | struct ctf_node *node_type_declarator, |
| 280 | struct declaration_scope *declaration_scope, |
| 281 | struct declaration *nested_declaration, |
| 282 | struct ctf_trace *trace) |
| 283 | { |
| 284 | /* |
| 285 | * Visit type declarator by first taking care of sequence/array |
| 286 | * (recursively). Then, when we get to the identifier, take care |
| 287 | * of pointers. |
| 288 | */ |
| 289 | |
| 290 | if (node_type_declarator) { |
| 291 | assert(node_type_declarator->u.type_declarator.type != TYPEDEC_UNKNOWN); |
| 292 | |
| 293 | /* TODO: gcc bitfields not supported yet. */ |
| 294 | if (node_type_declarator->u.type_declarator.bitfield_len != NULL) { |
| 295 | fprintf(fd, "[error] %s: gcc bitfields are not supported yet.\n", __func__); |
| 296 | return NULL; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (!nested_declaration) { |
| 301 | if (node_type_declarator && !cds_list_empty(&node_type_declarator->u.type_declarator.pointers)) { |
| 302 | GQuark alias_q; |
| 303 | |
| 304 | /* |
| 305 | * If we have a pointer declarator, it _has_ to be present in |
| 306 | * the typealiases (else fail). |
| 307 | */ |
| 308 | alias_q = create_typealias_identifier(fd, depth, |
| 309 | type_specifier_list, node_type_declarator); |
| 310 | nested_declaration = lookup_declaration(alias_q, declaration_scope); |
| 311 | if (!nested_declaration) { |
| 312 | fprintf(fd, "[error] %s: cannot find typealias \"%s\".\n", __func__, g_quark_to_string(alias_q)); |
| 313 | return NULL; |
| 314 | } |
| 315 | } else { |
| 316 | nested_declaration = ctf_type_specifier_list_visit(fd, depth, |
| 317 | type_specifier_list, declaration_scope, trace); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if (!node_type_declarator) |
| 322 | return nested_declaration; |
| 323 | |
| 324 | if (node_type_declarator->u.type_declarator.type == TYPEDEC_ID) { |
| 325 | if (node_type_declarator->u.type_declarator.u.id) |
| 326 | *field_name = g_quark_from_string(node_type_declarator->u.type_declarator.u.id); |
| 327 | else |
| 328 | *field_name = 0; |
| 329 | return nested_declaration; |
| 330 | } else { |
| 331 | struct declaration *declaration; |
| 332 | struct ctf_node *length; |
| 333 | |
| 334 | /* TYPEDEC_NESTED */ |
| 335 | |
| 336 | /* create array/sequence, pass nested_declaration as child. */ |
| 337 | length = node_type_declarator->u.type_declarator.u.nested.length; |
| 338 | if (!length) { |
| 339 | fprintf(fd, "[error] %s: expecting length type or value.\n", __func__); |
| 340 | return NULL; |
| 341 | } |
| 342 | switch (length->type) { |
| 343 | case NODE_UNARY_EXPRESSION: |
| 344 | { |
| 345 | struct declaration_array *array_declaration; |
| 346 | size_t len; |
| 347 | |
| 348 | if (length->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
| 349 | fprintf(fd, "[error] %s: array: unexpected unary expression.\n", __func__); |
| 350 | return NULL; |
| 351 | } |
| 352 | len = length->u.unary_expression.u.unsigned_constant; |
| 353 | array_declaration = array_declaration_new(len, nested_declaration, |
| 354 | declaration_scope); |
| 355 | declaration = &array_declaration->p; |
| 356 | break; |
| 357 | } |
| 358 | case NODE_TYPE_SPECIFIER_LIST: |
| 359 | { |
| 360 | struct declaration_sequence *sequence_declaration; |
| 361 | struct declaration_integer *integer_declaration; |
| 362 | |
| 363 | declaration = ctf_type_specifier_list_visit(fd, depth, |
| 364 | length, declaration_scope, trace); |
| 365 | if (!declaration) { |
| 366 | fprintf(fd, "[error] %s: unable to find declaration type for sequence length\n", __func__); |
| 367 | return NULL; |
| 368 | } |
| 369 | if (declaration->id != CTF_TYPE_INTEGER) { |
| 370 | fprintf(fd, "[error] %s: length type for sequence is expected to be an integer (unsigned).\n", __func__); |
| 371 | declaration_unref(declaration); |
| 372 | return NULL; |
| 373 | } |
| 374 | integer_declaration = container_of(declaration, struct declaration_integer, p); |
| 375 | if (integer_declaration->signedness != false) { |
| 376 | fprintf(fd, "[error] %s: length type for sequence should always be an unsigned integer.\n", __func__); |
| 377 | declaration_unref(declaration); |
| 378 | return NULL; |
| 379 | } |
| 380 | |
| 381 | sequence_declaration = sequence_declaration_new(integer_declaration, |
| 382 | nested_declaration, declaration_scope); |
| 383 | declaration = &sequence_declaration->p; |
| 384 | break; |
| 385 | } |
| 386 | default: |
| 387 | assert(0); |
| 388 | } |
| 389 | |
| 390 | /* Pass it as content of outer container */ |
| 391 | declaration = ctf_type_declarator_visit(fd, depth, |
| 392 | type_specifier_list, field_name, |
| 393 | node_type_declarator->u.type_declarator.u.nested.type_declarator, |
| 394 | declaration_scope, declaration, trace); |
| 395 | return declaration; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static |
| 400 | int ctf_struct_type_declarators_visit(FILE *fd, int depth, |
| 401 | struct declaration_struct *struct_declaration, |
| 402 | struct ctf_node *type_specifier_list, |
| 403 | struct cds_list_head *type_declarators, |
| 404 | struct declaration_scope *declaration_scope, |
| 405 | struct ctf_trace *trace) |
| 406 | { |
| 407 | struct ctf_node *iter; |
| 408 | GQuark field_name; |
| 409 | |
| 410 | cds_list_for_each_entry(iter, type_declarators, siblings) { |
| 411 | struct declaration *field_declaration; |
| 412 | |
| 413 | field_declaration = ctf_type_declarator_visit(fd, depth, |
| 414 | type_specifier_list, |
| 415 | &field_name, iter, |
| 416 | struct_declaration->scope, |
| 417 | NULL, trace); |
| 418 | if (!field_declaration) { |
| 419 | fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__); |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | struct_declaration_add_field(struct_declaration, |
| 423 | g_quark_to_string(field_name), |
| 424 | field_declaration); |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static |
| 430 | int ctf_variant_type_declarators_visit(FILE *fd, int depth, |
| 431 | struct declaration_untagged_variant *untagged_variant_declaration, |
| 432 | struct ctf_node *type_specifier_list, |
| 433 | struct cds_list_head *type_declarators, |
| 434 | struct declaration_scope *declaration_scope, |
| 435 | struct ctf_trace *trace) |
| 436 | { |
| 437 | struct ctf_node *iter; |
| 438 | GQuark field_name; |
| 439 | |
| 440 | cds_list_for_each_entry(iter, type_declarators, siblings) { |
| 441 | struct declaration *field_declaration; |
| 442 | |
| 443 | field_declaration = ctf_type_declarator_visit(fd, depth, |
| 444 | type_specifier_list, |
| 445 | &field_name, iter, |
| 446 | untagged_variant_declaration->scope, |
| 447 | NULL, trace); |
| 448 | if (!field_declaration) { |
| 449 | fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | untagged_variant_declaration_add_field(untagged_variant_declaration, |
| 453 | g_quark_to_string(field_name), |
| 454 | field_declaration); |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static |
| 460 | int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope, |
| 461 | struct ctf_node *type_specifier_list, |
| 462 | struct cds_list_head *type_declarators, |
| 463 | struct ctf_trace *trace) |
| 464 | { |
| 465 | struct ctf_node *iter; |
| 466 | GQuark identifier; |
| 467 | |
| 468 | cds_list_for_each_entry(iter, type_declarators, siblings) { |
| 469 | struct declaration *type_declaration; |
| 470 | int ret; |
| 471 | |
| 472 | type_declaration = ctf_type_declarator_visit(fd, depth, |
| 473 | type_specifier_list, |
| 474 | &identifier, iter, |
| 475 | scope, NULL, trace); |
| 476 | if (!type_declaration) { |
| 477 | fprintf(fd, "[error] %s: problem creating type declaration\n", __func__); |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | /* |
| 481 | * Don't allow typedef and typealias of untagged |
| 482 | * variants. |
| 483 | */ |
| 484 | if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) { |
| 485 | fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__); |
| 486 | declaration_unref(type_declaration); |
| 487 | return -EPERM; |
| 488 | } |
| 489 | ret = register_declaration(identifier, type_declaration, scope); |
| 490 | if (ret) { |
| 491 | type_declaration->declaration_free(type_declaration); |
| 492 | return ret; |
| 493 | } |
| 494 | } |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static |
| 499 | int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope, |
| 500 | struct ctf_node *target, struct ctf_node *alias, |
| 501 | struct ctf_trace *trace) |
| 502 | { |
| 503 | struct declaration *type_declaration; |
| 504 | struct ctf_node *node; |
| 505 | GQuark dummy_id; |
| 506 | GQuark alias_q; |
| 507 | int err; |
| 508 | |
| 509 | /* See ctf_visitor_type_declarator() in the semantic validator. */ |
| 510 | |
| 511 | /* |
| 512 | * Create target type declaration. |
| 513 | */ |
| 514 | |
| 515 | if (cds_list_empty(&target->u.typealias_target.type_declarators)) |
| 516 | node = NULL; |
| 517 | else |
| 518 | node = _cds_list_first_entry(&target->u.typealias_target.type_declarators, |
| 519 | struct ctf_node, siblings); |
| 520 | type_declaration = ctf_type_declarator_visit(fd, depth, |
| 521 | target->u.typealias_target.type_specifier_list, |
| 522 | &dummy_id, node, |
| 523 | scope, NULL, trace); |
| 524 | if (!type_declaration) { |
| 525 | fprintf(fd, "[error] %s: problem creating type declaration\n", __func__); |
| 526 | err = -EINVAL; |
| 527 | goto error; |
| 528 | } |
| 529 | /* |
| 530 | * Don't allow typedef and typealias of untagged |
| 531 | * variants. |
| 532 | */ |
| 533 | if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) { |
| 534 | fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__); |
| 535 | declaration_unref(type_declaration); |
| 536 | return -EPERM; |
| 537 | } |
| 538 | /* |
| 539 | * The semantic validator does not check whether the target is |
| 540 | * abstract or not (if it has an identifier). Check it here. |
| 541 | */ |
| 542 | if (dummy_id != 0) { |
| 543 | fprintf(fd, "[error] %s: expecting empty identifier\n", __func__); |
| 544 | err = -EINVAL; |
| 545 | goto error; |
| 546 | } |
| 547 | /* |
| 548 | * Create alias identifier. |
| 549 | */ |
| 550 | |
| 551 | node = _cds_list_first_entry(&alias->u.typealias_alias.type_declarators, |
| 552 | struct ctf_node, siblings); |
| 553 | alias_q = create_typealias_identifier(fd, depth, |
| 554 | alias->u.typealias_alias.type_specifier_list, node); |
| 555 | err = register_declaration(alias_q, type_declaration, scope); |
| 556 | if (err) |
| 557 | goto error; |
| 558 | return 0; |
| 559 | |
| 560 | error: |
| 561 | type_declaration->declaration_free(type_declaration); |
| 562 | return err; |
| 563 | } |
| 564 | |
| 565 | static |
| 566 | int ctf_struct_declaration_list_visit(FILE *fd, int depth, |
| 567 | struct ctf_node *iter, struct declaration_struct *struct_declaration, |
| 568 | struct ctf_trace *trace) |
| 569 | { |
| 570 | int ret; |
| 571 | |
| 572 | switch (iter->type) { |
| 573 | case NODE_TYPEDEF: |
| 574 | /* For each declarator, declare type and add type to struct declaration scope */ |
| 575 | ret = ctf_typedef_visit(fd, depth, |
| 576 | struct_declaration->scope, |
| 577 | iter->u._typedef.type_specifier_list, |
| 578 | &iter->u._typedef.type_declarators, trace); |
| 579 | if (ret) |
| 580 | return ret; |
| 581 | break; |
| 582 | case NODE_TYPEALIAS: |
| 583 | /* Declare type with declarator and add type to struct declaration scope */ |
| 584 | ret = ctf_typealias_visit(fd, depth, |
| 585 | struct_declaration->scope, |
| 586 | iter->u.typealias.target, |
| 587 | iter->u.typealias.alias, trace); |
| 588 | if (ret) |
| 589 | return ret; |
| 590 | break; |
| 591 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 592 | /* Add field to structure declaration */ |
| 593 | ret = ctf_struct_type_declarators_visit(fd, depth, |
| 594 | struct_declaration, |
| 595 | iter->u.struct_or_variant_declaration.type_specifier_list, |
| 596 | &iter->u.struct_or_variant_declaration.type_declarators, |
| 597 | struct_declaration->scope, trace); |
| 598 | if (ret) |
| 599 | return ret; |
| 600 | break; |
| 601 | default: |
| 602 | fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type); |
| 603 | assert(0); |
| 604 | } |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static |
| 609 | int ctf_variant_declaration_list_visit(FILE *fd, int depth, |
| 610 | struct ctf_node *iter, |
| 611 | struct declaration_untagged_variant *untagged_variant_declaration, |
| 612 | struct ctf_trace *trace) |
| 613 | { |
| 614 | int ret; |
| 615 | |
| 616 | switch (iter->type) { |
| 617 | case NODE_TYPEDEF: |
| 618 | /* For each declarator, declare type and add type to variant declaration scope */ |
| 619 | ret = ctf_typedef_visit(fd, depth, |
| 620 | untagged_variant_declaration->scope, |
| 621 | iter->u._typedef.type_specifier_list, |
| 622 | &iter->u._typedef.type_declarators, trace); |
| 623 | if (ret) |
| 624 | return ret; |
| 625 | break; |
| 626 | case NODE_TYPEALIAS: |
| 627 | /* Declare type with declarator and add type to variant declaration scope */ |
| 628 | ret = ctf_typealias_visit(fd, depth, |
| 629 | untagged_variant_declaration->scope, |
| 630 | iter->u.typealias.target, |
| 631 | iter->u.typealias.alias, trace); |
| 632 | if (ret) |
| 633 | return ret; |
| 634 | break; |
| 635 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 636 | /* Add field to structure declaration */ |
| 637 | ret = ctf_variant_type_declarators_visit(fd, depth, |
| 638 | untagged_variant_declaration, |
| 639 | iter->u.struct_or_variant_declaration.type_specifier_list, |
| 640 | &iter->u.struct_or_variant_declaration.type_declarators, |
| 641 | untagged_variant_declaration->scope, trace); |
| 642 | if (ret) |
| 643 | return ret; |
| 644 | break; |
| 645 | default: |
| 646 | fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type); |
| 647 | assert(0); |
| 648 | } |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static |
| 653 | struct declaration *ctf_declaration_struct_visit(FILE *fd, |
| 654 | int depth, const char *name, struct cds_list_head *declaration_list, |
| 655 | int has_body, struct declaration_scope *declaration_scope, |
| 656 | struct ctf_trace *trace) |
| 657 | { |
| 658 | struct declaration_struct *struct_declaration; |
| 659 | struct ctf_node *iter; |
| 660 | int ret; |
| 661 | |
| 662 | /* |
| 663 | * For named struct (without body), lookup in |
| 664 | * declaration scope. Don't take reference on struct |
| 665 | * declaration: ref is only taken upon definition. |
| 666 | */ |
| 667 | if (!has_body) { |
| 668 | assert(name); |
| 669 | struct_declaration = |
| 670 | lookup_struct_declaration(g_quark_from_string(name), |
| 671 | declaration_scope); |
| 672 | return &struct_declaration->p; |
| 673 | } else { |
| 674 | /* For unnamed struct, create type */ |
| 675 | /* For named struct (with body), create type and add to declaration scope */ |
| 676 | if (name) { |
| 677 | if (lookup_struct_declaration(g_quark_from_string(name), |
| 678 | declaration_scope)) { |
| 679 | |
| 680 | fprintf(fd, "[error] %s: struct %s already declared in scope\n", __func__, name); |
| 681 | return NULL; |
| 682 | } |
| 683 | } |
| 684 | struct_declaration = struct_declaration_new(declaration_scope); |
| 685 | cds_list_for_each_entry(iter, declaration_list, siblings) { |
| 686 | ret = ctf_struct_declaration_list_visit(fd, depth + 1, iter, |
| 687 | struct_declaration, trace); |
| 688 | if (ret) |
| 689 | goto error; |
| 690 | } |
| 691 | if (name) { |
| 692 | ret = register_struct_declaration(g_quark_from_string(name), |
| 693 | struct_declaration, |
| 694 | declaration_scope); |
| 695 | assert(!ret); |
| 696 | } |
| 697 | return &struct_declaration->p; |
| 698 | } |
| 699 | error: |
| 700 | struct_declaration->p.declaration_free(&struct_declaration->p); |
| 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | static |
| 705 | struct declaration *ctf_declaration_variant_visit(FILE *fd, |
| 706 | int depth, const char *name, const char *choice, |
| 707 | struct cds_list_head *declaration_list, |
| 708 | int has_body, struct declaration_scope *declaration_scope, |
| 709 | struct ctf_trace *trace) |
| 710 | { |
| 711 | struct declaration_untagged_variant *untagged_variant_declaration; |
| 712 | struct declaration_variant *variant_declaration; |
| 713 | struct ctf_node *iter; |
| 714 | int ret; |
| 715 | |
| 716 | /* |
| 717 | * For named variant (without body), lookup in |
| 718 | * declaration scope. Don't take reference on variant |
| 719 | * declaration: ref is only taken upon definition. |
| 720 | */ |
| 721 | if (!has_body) { |
| 722 | assert(name); |
| 723 | untagged_variant_declaration = |
| 724 | lookup_variant_declaration(g_quark_from_string(name), |
| 725 | declaration_scope); |
| 726 | } else { |
| 727 | /* For unnamed variant, create type */ |
| 728 | /* For named variant (with body), create type and add to declaration scope */ |
| 729 | if (name) { |
| 730 | if (lookup_variant_declaration(g_quark_from_string(name), |
| 731 | declaration_scope)) { |
| 732 | |
| 733 | fprintf(fd, "[error] %s: variant %s already declared in scope\n", __func__, name); |
| 734 | return NULL; |
| 735 | } |
| 736 | } |
| 737 | untagged_variant_declaration = untagged_variant_declaration_new(declaration_scope); |
| 738 | cds_list_for_each_entry(iter, declaration_list, siblings) { |
| 739 | ret = ctf_variant_declaration_list_visit(fd, depth + 1, iter, |
| 740 | untagged_variant_declaration, trace); |
| 741 | if (ret) |
| 742 | goto error; |
| 743 | } |
| 744 | if (name) { |
| 745 | ret = register_variant_declaration(g_quark_from_string(name), |
| 746 | untagged_variant_declaration, |
| 747 | declaration_scope); |
| 748 | assert(!ret); |
| 749 | } |
| 750 | } |
| 751 | /* |
| 752 | * if tagged, create tagged variant and return. else return |
| 753 | * untagged variant. |
| 754 | */ |
| 755 | if (!choice) { |
| 756 | return &untagged_variant_declaration->p; |
| 757 | } else { |
| 758 | variant_declaration = variant_declaration_new(untagged_variant_declaration, choice); |
| 759 | if (!variant_declaration) |
| 760 | goto error; |
| 761 | declaration_unref(&untagged_variant_declaration->p); |
| 762 | return &variant_declaration->p; |
| 763 | } |
| 764 | error: |
| 765 | untagged_variant_declaration->p.declaration_free(&untagged_variant_declaration->p); |
| 766 | return NULL; |
| 767 | } |
| 768 | |
| 769 | static |
| 770 | int ctf_enumerator_list_visit(FILE *fd, int depth, |
| 771 | struct ctf_node *enumerator, |
| 772 | struct declaration_enum *enum_declaration) |
| 773 | { |
| 774 | GQuark q; |
| 775 | struct ctf_node *iter; |
| 776 | |
| 777 | q = g_quark_from_string(enumerator->u.enumerator.id); |
| 778 | if (enum_declaration->integer_declaration->signedness) { |
| 779 | int64_t start, end; |
| 780 | int nr_vals = 0; |
| 781 | |
| 782 | cds_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) { |
| 783 | int64_t *target; |
| 784 | |
| 785 | assert(iter->type == NODE_UNARY_EXPRESSION); |
| 786 | if (nr_vals == 0) |
| 787 | target = &start; |
| 788 | else |
| 789 | target = &end; |
| 790 | |
| 791 | switch (iter->u.unary_expression.type) { |
| 792 | case UNARY_SIGNED_CONSTANT: |
| 793 | *target = iter->u.unary_expression.u.signed_constant; |
| 794 | break; |
| 795 | case UNARY_UNSIGNED_CONSTANT: |
| 796 | *target = iter->u.unary_expression.u.unsigned_constant; |
| 797 | break; |
| 798 | default: |
| 799 | fprintf(fd, "[error] %s: invalid enumerator\n", __func__); |
| 800 | return -EINVAL; |
| 801 | } |
| 802 | if (nr_vals > 1) { |
| 803 | fprintf(fd, "[error] %s: invalid enumerator\n", __func__); |
| 804 | return -EINVAL; |
| 805 | } |
| 806 | nr_vals++; |
| 807 | } |
| 808 | if (nr_vals == 1) |
| 809 | end = start; |
| 810 | enum_signed_insert(enum_declaration, start, end, q); |
| 811 | } else { |
| 812 | uint64_t start, end; |
| 813 | int nr_vals = 0; |
| 814 | |
| 815 | cds_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) { |
| 816 | uint64_t *target; |
| 817 | |
| 818 | assert(iter->type == NODE_UNARY_EXPRESSION); |
| 819 | if (nr_vals == 0) |
| 820 | target = &start; |
| 821 | else |
| 822 | target = &end; |
| 823 | |
| 824 | switch (iter->u.unary_expression.type) { |
| 825 | case UNARY_UNSIGNED_CONSTANT: |
| 826 | *target = iter->u.unary_expression.u.unsigned_constant; |
| 827 | break; |
| 828 | case UNARY_SIGNED_CONSTANT: |
| 829 | /* |
| 830 | * We don't accept signed constants for enums with unsigned |
| 831 | * container type. |
| 832 | */ |
| 833 | fprintf(fd, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__); |
| 834 | return -EINVAL; |
| 835 | default: |
| 836 | fprintf(fd, "[error] %s: invalid enumerator\n", __func__); |
| 837 | return -EINVAL; |
| 838 | } |
| 839 | if (nr_vals > 1) { |
| 840 | fprintf(fd, "[error] %s: invalid enumerator\n", __func__); |
| 841 | return -EINVAL; |
| 842 | } |
| 843 | nr_vals++; |
| 844 | } |
| 845 | if (nr_vals == 1) |
| 846 | end = start; |
| 847 | enum_unsigned_insert(enum_declaration, start, end, q); |
| 848 | } |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | static |
| 853 | struct declaration *ctf_declaration_enum_visit(FILE *fd, int depth, |
| 854 | const char *name, |
| 855 | struct ctf_node *container_type, |
| 856 | struct cds_list_head *enumerator_list, |
| 857 | int has_body, |
| 858 | struct declaration_scope *declaration_scope, |
| 859 | struct ctf_trace *trace) |
| 860 | { |
| 861 | struct declaration *declaration; |
| 862 | struct declaration_enum *enum_declaration; |
| 863 | struct declaration_integer *integer_declaration; |
| 864 | struct ctf_node *iter; |
| 865 | GQuark dummy_id; |
| 866 | int ret; |
| 867 | |
| 868 | /* |
| 869 | * For named enum (without body), lookup in |
| 870 | * declaration scope. Don't take reference on enum |
| 871 | * declaration: ref is only taken upon definition. |
| 872 | */ |
| 873 | if (!has_body) { |
| 874 | assert(name); |
| 875 | enum_declaration = |
| 876 | lookup_enum_declaration(g_quark_from_string(name), |
| 877 | declaration_scope); |
| 878 | return &enum_declaration->p; |
| 879 | } else { |
| 880 | /* For unnamed enum, create type */ |
| 881 | /* For named enum (with body), create type and add to declaration scope */ |
| 882 | if (name) { |
| 883 | if (lookup_enum_declaration(g_quark_from_string(name), |
| 884 | declaration_scope)) { |
| 885 | |
| 886 | fprintf(fd, "[error] %s: enum %s already declared in scope\n", __func__, name); |
| 887 | return NULL; |
| 888 | } |
| 889 | } |
| 890 | if (!container_type) { |
| 891 | declaration = lookup_declaration(g_quark_from_static_string("int"), |
| 892 | declaration_scope); |
| 893 | if (!declaration) { |
| 894 | fprintf(fd, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__); |
| 895 | return NULL; |
| 896 | } |
| 897 | } else { |
| 898 | declaration = ctf_type_declarator_visit(fd, depth, |
| 899 | container_type, |
| 900 | &dummy_id, NULL, |
| 901 | declaration_scope, |
| 902 | NULL, trace); |
| 903 | } |
| 904 | if (!declaration) { |
| 905 | fprintf(fd, "[error] %s: unable to create container type for enumeration\n", __func__); |
| 906 | return NULL; |
| 907 | } |
| 908 | if (declaration->id != CTF_TYPE_INTEGER) { |
| 909 | fprintf(fd, "[error] %s: container type for enumeration is not integer\n", __func__); |
| 910 | return NULL; |
| 911 | } |
| 912 | integer_declaration = container_of(declaration, struct declaration_integer, p); |
| 913 | enum_declaration = enum_declaration_new(integer_declaration); |
| 914 | declaration_unref(&integer_declaration->p); /* leave ref to enum */ |
| 915 | cds_list_for_each_entry(iter, enumerator_list, siblings) { |
| 916 | ret = ctf_enumerator_list_visit(fd, depth + 1, iter, enum_declaration); |
| 917 | if (ret) |
| 918 | goto error; |
| 919 | } |
| 920 | if (name) { |
| 921 | ret = register_enum_declaration(g_quark_from_string(name), |
| 922 | enum_declaration, |
| 923 | declaration_scope); |
| 924 | assert(!ret); |
| 925 | } |
| 926 | return &enum_declaration->p; |
| 927 | } |
| 928 | error: |
| 929 | enum_declaration->p.declaration_free(&enum_declaration->p); |
| 930 | return NULL; |
| 931 | } |
| 932 | |
| 933 | static |
| 934 | struct declaration *ctf_declaration_type_specifier_visit(FILE *fd, int depth, |
| 935 | struct ctf_node *type_specifier_list, |
| 936 | struct declaration_scope *declaration_scope) |
| 937 | { |
| 938 | GString *str; |
| 939 | struct declaration *declaration; |
| 940 | char *str_c; |
| 941 | int ret; |
| 942 | GQuark id_q; |
| 943 | |
| 944 | str = g_string_new(""); |
| 945 | ret = visit_type_specifier_list(fd, type_specifier_list, str); |
| 946 | if (ret) |
| 947 | return NULL; |
| 948 | str_c = g_string_free(str, FALSE); |
| 949 | id_q = g_quark_from_string(str_c); |
| 950 | g_free(str_c); |
| 951 | declaration = lookup_declaration(id_q, declaration_scope); |
| 952 | return declaration; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Returns 0/1 boolean, or < 0 on error. |
| 957 | */ |
| 958 | static |
| 959 | int get_boolean(FILE *fd, int depth, struct ctf_node *unary_expression) |
| 960 | { |
| 961 | if (unary_expression->type != NODE_UNARY_EXPRESSION) { |
| 962 | fprintf(fd, "[error] %s: expecting unary expression\n", |
| 963 | __func__); |
| 964 | return -EINVAL; |
| 965 | } |
| 966 | switch (unary_expression->u.unary_expression.type) { |
| 967 | case UNARY_UNSIGNED_CONSTANT: |
| 968 | if (unary_expression->u.unary_expression.u.unsigned_constant == 0) |
| 969 | return 0; |
| 970 | else |
| 971 | return 1; |
| 972 | case UNARY_SIGNED_CONSTANT: |
| 973 | if (unary_expression->u.unary_expression.u.signed_constant == 0) |
| 974 | return 0; |
| 975 | else |
| 976 | return 1; |
| 977 | case UNARY_STRING: |
| 978 | if (!strcmp(unary_expression->u.unary_expression.u.string, "true")) |
| 979 | return 1; |
| 980 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "TRUE")) |
| 981 | return 1; |
| 982 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "false")) |
| 983 | return 0; |
| 984 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "FALSE")) |
| 985 | return 0; |
| 986 | else { |
| 987 | fprintf(fd, "[error] %s: unexpected string \"%s\"\n", |
| 988 | __func__, unary_expression->u.unary_expression.u.string); |
| 989 | return -EINVAL; |
| 990 | } |
| 991 | break; |
| 992 | default: |
| 993 | fprintf(fd, "[error] %s: unexpected unary expression type\n", |
| 994 | __func__); |
| 995 | return -EINVAL; |
| 996 | } |
| 997 | |
| 998 | } |
| 999 | |
| 1000 | static |
| 1001 | int get_trace_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression) |
| 1002 | { |
| 1003 | int byte_order; |
| 1004 | |
| 1005 | if (unary_expression->u.unary_expression.type != UNARY_STRING) { |
| 1006 | fprintf(fd, "[error] %s: byte_order: expecting string\n", |
| 1007 | __func__); |
| 1008 | return -EINVAL; |
| 1009 | } |
| 1010 | if (!strcmp(unary_expression->u.unary_expression.u.string, "be")) |
| 1011 | byte_order = BIG_ENDIAN; |
| 1012 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "le")) |
| 1013 | byte_order = LITTLE_ENDIAN; |
| 1014 | else { |
| 1015 | fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n", |
| 1016 | __func__, unary_expression->u.unary_expression.u.string); |
| 1017 | return -EINVAL; |
| 1018 | } |
| 1019 | return byte_order; |
| 1020 | } |
| 1021 | |
| 1022 | static |
| 1023 | int get_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression, |
| 1024 | struct ctf_trace *trace) |
| 1025 | { |
| 1026 | int byte_order; |
| 1027 | |
| 1028 | if (unary_expression->u.unary_expression.type != UNARY_STRING) { |
| 1029 | fprintf(fd, "[error] %s: byte_order: expecting string\n", |
| 1030 | __func__); |
| 1031 | return -EINVAL; |
| 1032 | } |
| 1033 | if (!strcmp(unary_expression->u.unary_expression.u.string, "native")) |
| 1034 | byte_order = trace->byte_order; |
| 1035 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "network")) |
| 1036 | byte_order = BIG_ENDIAN; |
| 1037 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "be")) |
| 1038 | byte_order = BIG_ENDIAN; |
| 1039 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "le")) |
| 1040 | byte_order = LITTLE_ENDIAN; |
| 1041 | else { |
| 1042 | fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n", |
| 1043 | __func__, unary_expression->u.unary_expression.u.string); |
| 1044 | return -EINVAL; |
| 1045 | } |
| 1046 | return byte_order; |
| 1047 | } |
| 1048 | |
| 1049 | static |
| 1050 | struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth, |
| 1051 | struct cds_list_head *expressions, |
| 1052 | struct ctf_trace *trace) |
| 1053 | { |
| 1054 | struct ctf_node *expression; |
| 1055 | uint64_t alignment, size; |
| 1056 | int byte_order = trace->byte_order; |
| 1057 | int signedness = 0; |
| 1058 | int has_alignment = 0, has_size = 0; |
| 1059 | struct declaration_integer *integer_declaration; |
| 1060 | |
| 1061 | cds_list_for_each_entry(expression, expressions, siblings) { |
| 1062 | struct ctf_node *left, *right; |
| 1063 | |
| 1064 | left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings); |
| 1065 | right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings); |
| 1066 | assert(left->u.unary_expression.type == UNARY_STRING); |
| 1067 | if (!strcmp(left->u.unary_expression.u.string, "signed")) { |
| 1068 | signedness = get_boolean(fd, depth, right); |
| 1069 | if (signedness < 0) |
| 1070 | return NULL; |
| 1071 | } else if (!strcmp(left->u.unary_expression.u.string, "byte_order")) { |
| 1072 | byte_order = get_byte_order(fd, depth, right, trace); |
| 1073 | if (byte_order < 0) |
| 1074 | return NULL; |
| 1075 | } else if (!strcmp(left->u.unary_expression.u.string, "size")) { |
| 1076 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
| 1077 | fprintf(fd, "[error] %s: size: expecting unsigned constant\n", |
| 1078 | __func__); |
| 1079 | return NULL; |
| 1080 | } |
| 1081 | size = right->u.unary_expression.u.unsigned_constant; |
| 1082 | has_size = 1; |
| 1083 | } else if (!strcmp(left->u.unary_expression.u.string, "align")) { |
| 1084 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
| 1085 | fprintf(fd, "[error] %s: align: expecting unsigned constant\n", |
| 1086 | __func__); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | alignment = right->u.unary_expression.u.unsigned_constant; |
| 1090 | has_alignment = 1; |
| 1091 | } else { |
| 1092 | fprintf(fd, "[error] %s: unknown attribute name %s\n", |
| 1093 | __func__, left->u.unary_expression.u.string); |
| 1094 | return NULL; |
| 1095 | } |
| 1096 | } |
| 1097 | if (!has_size) { |
| 1098 | fprintf(fd, "[error] %s: missing size attribute\n", __func__); |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | if (!has_alignment) { |
| 1102 | if (size % CHAR_BIT) { |
| 1103 | /* bit-packed alignment */ |
| 1104 | alignment = 1; |
| 1105 | } else { |
| 1106 | /* byte-packed alignment */ |
| 1107 | alignment = CHAR_BIT; |
| 1108 | } |
| 1109 | } |
| 1110 | integer_declaration = integer_declaration_new(size, |
| 1111 | byte_order, signedness, alignment); |
| 1112 | return &integer_declaration->p; |
| 1113 | } |
| 1114 | |
| 1115 | static |
| 1116 | struct declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth, |
| 1117 | struct cds_list_head *expressions, |
| 1118 | struct ctf_trace *trace) |
| 1119 | { |
| 1120 | struct ctf_node *expression; |
| 1121 | uint64_t alignment, exp_dig, mant_dig, byte_order = trace->byte_order; |
| 1122 | int has_alignment = 0, has_exp_dig = 0, has_mant_dig = 0; |
| 1123 | struct declaration_float *float_declaration; |
| 1124 | |
| 1125 | cds_list_for_each_entry(expression, expressions, siblings) { |
| 1126 | struct ctf_node *left, *right; |
| 1127 | |
| 1128 | left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings); |
| 1129 | right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings); |
| 1130 | assert(left->u.unary_expression.type == UNARY_STRING); |
| 1131 | if (!strcmp(left->u.unary_expression.u.string, "byte_order")) { |
| 1132 | byte_order = get_byte_order(fd, depth, right, trace); |
| 1133 | if (byte_order < 0) |
| 1134 | return NULL; |
| 1135 | } else if (!strcmp(left->u.unary_expression.u.string, "exp_dig")) { |
| 1136 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
| 1137 | fprintf(fd, "[error] %s: exp_dig: expecting unsigned constant\n", |
| 1138 | __func__); |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | exp_dig = right->u.unary_expression.u.unsigned_constant; |
| 1142 | has_exp_dig = 1; |
| 1143 | } else if (!strcmp(left->u.unary_expression.u.string, "mant_dig")) { |
| 1144 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
| 1145 | fprintf(fd, "[error] %s: mant_dig: expecting unsigned constant\n", |
| 1146 | __func__); |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | mant_dig = right->u.unary_expression.u.unsigned_constant; |
| 1150 | has_mant_dig = 1; |
| 1151 | } else if (!strcmp(left->u.unary_expression.u.string, "align")) { |
| 1152 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
| 1153 | fprintf(fd, "[error] %s: align: expecting unsigned constant\n", |
| 1154 | __func__); |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | alignment = right->u.unary_expression.u.unsigned_constant; |
| 1158 | has_alignment = 1; |
| 1159 | } else { |
| 1160 | fprintf(fd, "[error] %s: unknown attribute name %s\n", |
| 1161 | __func__, left->u.unary_expression.u.string); |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | } |
| 1165 | if (!has_mant_dig) { |
| 1166 | fprintf(fd, "[error] %s: missing mant_dig attribute\n", __func__); |
| 1167 | return NULL; |
| 1168 | } |
| 1169 | if (!has_exp_dig) { |
| 1170 | fprintf(fd, "[error] %s: missing exp_dig attribute\n", __func__); |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | if (!has_alignment) { |
| 1174 | if ((mant_dig + exp_dig) % CHAR_BIT) { |
| 1175 | /* bit-packed alignment */ |
| 1176 | alignment = 1; |
| 1177 | } else { |
| 1178 | /* byte-packed alignment */ |
| 1179 | alignment = CHAR_BIT; |
| 1180 | } |
| 1181 | } |
| 1182 | float_declaration = float_declaration_new(mant_dig, exp_dig, |
| 1183 | byte_order, alignment); |
| 1184 | return &float_declaration->p; |
| 1185 | } |
| 1186 | |
| 1187 | static |
| 1188 | struct declaration *ctf_declaration_string_visit(FILE *fd, int depth, |
| 1189 | struct cds_list_head *expressions, |
| 1190 | struct ctf_trace *trace) |
| 1191 | { |
| 1192 | struct ctf_node *expression; |
| 1193 | const char *encoding_c = NULL; |
| 1194 | enum ctf_string_encoding encoding = CTF_STRING_UTF8; |
| 1195 | struct declaration_string *string_declaration; |
| 1196 | |
| 1197 | cds_list_for_each_entry(expression, expressions, siblings) { |
| 1198 | struct ctf_node *left, *right; |
| 1199 | |
| 1200 | left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings); |
| 1201 | right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings); |
| 1202 | assert(left->u.unary_expression.type == UNARY_STRING); |
| 1203 | if (!strcmp(left->u.unary_expression.u.string, "encoding")) { |
| 1204 | if (right->u.unary_expression.type != UNARY_STRING) { |
| 1205 | fprintf(fd, "[error] %s: encoding: expecting string\n", |
| 1206 | __func__); |
| 1207 | return NULL; |
| 1208 | } |
| 1209 | encoding_c = right->u.unary_expression.u.string; |
| 1210 | } else { |
| 1211 | fprintf(fd, "[error] %s: unknown attribute name %s\n", |
| 1212 | __func__, left->u.unary_expression.u.string); |
| 1213 | return NULL; |
| 1214 | } |
| 1215 | } |
| 1216 | if (encoding_c && !strcmp(encoding_c, "ASCII")) |
| 1217 | encoding = CTF_STRING_ASCII; |
| 1218 | string_declaration = string_declaration_new(encoding); |
| 1219 | return &string_declaration->p; |
| 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | static |
| 1224 | struct declaration *ctf_type_specifier_list_visit(FILE *fd, |
| 1225 | int depth, struct ctf_node *type_specifier_list, |
| 1226 | struct declaration_scope *declaration_scope, |
| 1227 | struct ctf_trace *trace) |
| 1228 | { |
| 1229 | struct ctf_node *first; |
| 1230 | struct ctf_node *node; |
| 1231 | |
| 1232 | assert(type_specifier_list->type == NODE_TYPE_SPECIFIER_LIST); |
| 1233 | |
| 1234 | first = _cds_list_first_entry(&type_specifier_list->u.type_specifier_list.head, struct ctf_node, siblings); |
| 1235 | |
| 1236 | assert(first->type == NODE_TYPE_SPECIFIER); |
| 1237 | |
| 1238 | node = first->u.type_specifier.node; |
| 1239 | |
| 1240 | switch (first->u.type_specifier.type) { |
| 1241 | case TYPESPEC_FLOATING_POINT: |
| 1242 | return ctf_declaration_floating_point_visit(fd, depth, |
| 1243 | &node->u.floating_point.expressions, trace); |
| 1244 | case TYPESPEC_INTEGER: |
| 1245 | return ctf_declaration_integer_visit(fd, depth, |
| 1246 | &node->u.integer.expressions, trace); |
| 1247 | case TYPESPEC_STRING: |
| 1248 | return ctf_declaration_string_visit(fd, depth, |
| 1249 | &node->u.string.expressions, trace); |
| 1250 | case TYPESPEC_STRUCT: |
| 1251 | return ctf_declaration_struct_visit(fd, depth, |
| 1252 | node->u._struct.name, |
| 1253 | &node->u._struct.declaration_list, |
| 1254 | node->u._struct.has_body, |
| 1255 | declaration_scope, |
| 1256 | trace); |
| 1257 | case TYPESPEC_VARIANT: |
| 1258 | return ctf_declaration_variant_visit(fd, depth, |
| 1259 | node->u.variant.name, |
| 1260 | node->u.variant.choice, |
| 1261 | &node->u.variant.declaration_list, |
| 1262 | node->u.variant.has_body, |
| 1263 | declaration_scope, |
| 1264 | trace); |
| 1265 | case TYPESPEC_ENUM: |
| 1266 | return ctf_declaration_enum_visit(fd, depth, |
| 1267 | node->u._enum.enum_id, |
| 1268 | node->u._enum.container_type, |
| 1269 | &node->u._enum.enumerator_list, |
| 1270 | node->u._enum.has_body, |
| 1271 | declaration_scope, |
| 1272 | trace); |
| 1273 | |
| 1274 | case TYPESPEC_VOID: |
| 1275 | case TYPESPEC_CHAR: |
| 1276 | case TYPESPEC_SHORT: |
| 1277 | case TYPESPEC_INT: |
| 1278 | case TYPESPEC_LONG: |
| 1279 | case TYPESPEC_FLOAT: |
| 1280 | case TYPESPEC_DOUBLE: |
| 1281 | case TYPESPEC_SIGNED: |
| 1282 | case TYPESPEC_UNSIGNED: |
| 1283 | case TYPESPEC_BOOL: |
| 1284 | case TYPESPEC_COMPLEX: |
| 1285 | case TYPESPEC_IMAGINARY: |
| 1286 | case TYPESPEC_CONST: |
| 1287 | case TYPESPEC_ID_TYPE: |
| 1288 | return ctf_declaration_type_specifier_visit(fd, depth, |
| 1289 | type_specifier_list, declaration_scope); |
| 1290 | default: |
| 1291 | fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) first->u.type_specifier.type); |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | static |
| 1297 | int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_event *event, struct ctf_trace *trace) |
| 1298 | { |
| 1299 | int ret = 0; |
| 1300 | |
| 1301 | switch (node->type) { |
| 1302 | case NODE_TYPEDEF: |
| 1303 | ret = ctf_typedef_visit(fd, depth + 1, |
| 1304 | event->declaration_scope, |
| 1305 | node->u._typedef.type_specifier_list, |
| 1306 | &node->u._typedef.type_declarators, |
| 1307 | trace); |
| 1308 | if (ret) |
| 1309 | return ret; |
| 1310 | break; |
| 1311 | case NODE_TYPEALIAS: |
| 1312 | ret = ctf_typealias_visit(fd, depth + 1, |
| 1313 | event->declaration_scope, |
| 1314 | node->u.typealias.target, node->u.typealias.alias, |
| 1315 | trace); |
| 1316 | if (ret) |
| 1317 | return ret; |
| 1318 | break; |
| 1319 | case NODE_CTF_EXPRESSION: |
| 1320 | { |
| 1321 | char *left; |
| 1322 | |
| 1323 | left = concatenate_unary_strings(&node->u.ctf_expression.left); |
| 1324 | if (!strcmp(left, "name")) { |
| 1325 | char *right; |
| 1326 | |
| 1327 | if (CTF_EVENT_FIELD_IS_SET(event, name)) { |
| 1328 | fprintf(fd, "[error] %s: name already declared in event declaration\n", __func__); |
| 1329 | ret = -EPERM; |
| 1330 | goto error; |
| 1331 | } |
| 1332 | right = concatenate_unary_strings(&node->u.ctf_expression.right); |
| 1333 | if (!right) { |
| 1334 | fprintf(fd, "[error] %s: unexpected unary expression for event name\n", __func__); |
| 1335 | ret = -EINVAL; |
| 1336 | goto error; |
| 1337 | } |
| 1338 | event->name = g_quark_from_string(right); |
| 1339 | g_free(right); |
| 1340 | CTF_EVENT_SET_FIELD(event, name); |
| 1341 | } else if (!strcmp(left, "id")) { |
| 1342 | if (CTF_EVENT_FIELD_IS_SET(event, id)) { |
| 1343 | fprintf(fd, "[error] %s: id already declared in event declaration\n", __func__); |
| 1344 | ret = -EPERM; |
| 1345 | goto error; |
| 1346 | } |
| 1347 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->id); |
| 1348 | if (ret) { |
| 1349 | fprintf(fd, "[error] %s: unexpected unary expression for event id\n", __func__); |
| 1350 | ret = -EINVAL; |
| 1351 | goto error; |
| 1352 | } |
| 1353 | CTF_EVENT_SET_FIELD(event, id); |
| 1354 | } else if (!strcmp(left, "stream_id")) { |
| 1355 | if (CTF_EVENT_FIELD_IS_SET(event, stream_id)) { |
| 1356 | fprintf(fd, "[error] %s: stream_id already declared in event declaration\n", __func__); |
| 1357 | ret = -EPERM; |
| 1358 | goto error; |
| 1359 | } |
| 1360 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->stream_id); |
| 1361 | if (ret) { |
| 1362 | fprintf(fd, "[error] %s: unexpected unary expression for event stream_id\n", __func__); |
| 1363 | ret = -EINVAL; |
| 1364 | goto error; |
| 1365 | } |
| 1366 | event->stream = trace_stream_lookup(trace, event->stream_id); |
| 1367 | if (!event->stream) { |
| 1368 | fprintf(fd, "[error] %s: stream id %" PRIu64 " cannot be found\n", __func__, event->stream_id); |
| 1369 | ret = -EINVAL; |
| 1370 | goto error; |
| 1371 | } |
| 1372 | CTF_EVENT_SET_FIELD(event, stream_id); |
| 1373 | } else if (!strcmp(left, "context")) { |
| 1374 | struct declaration *declaration; |
| 1375 | |
| 1376 | if (event->context_decl) { |
| 1377 | fprintf(fd, "[error] %s: context already declared in event declaration\n", __func__); |
| 1378 | ret = -EINVAL; |
| 1379 | goto error; |
| 1380 | } |
| 1381 | declaration = ctf_type_specifier_list_visit(fd, depth, |
| 1382 | _cds_list_first_entry(&node->u.ctf_expression.right, |
| 1383 | struct ctf_node, siblings), |
| 1384 | event->declaration_scope, trace); |
| 1385 | if (!declaration) { |
| 1386 | ret = -EPERM; |
| 1387 | goto error; |
| 1388 | } |
| 1389 | if (declaration->id != CTF_TYPE_STRUCT) { |
| 1390 | ret = -EPERM; |
| 1391 | goto error; |
| 1392 | } |
| 1393 | event->context_decl = container_of(declaration, struct declaration_struct, p); |
| 1394 | } else if (!strcmp(left, "fields")) { |
| 1395 | struct declaration *declaration; |
| 1396 | |
| 1397 | if (event->fields_decl) { |
| 1398 | fprintf(fd, "[error] %s: fields already declared in event declaration\n", __func__); |
| 1399 | ret = -EINVAL; |
| 1400 | goto error; |
| 1401 | } |
| 1402 | declaration = ctf_type_specifier_list_visit(fd, depth, |
| 1403 | _cds_list_first_entry(&node->u.ctf_expression.right, |
| 1404 | struct ctf_node, siblings), |
| 1405 | event->declaration_scope, trace); |
| 1406 | if (!declaration) { |
| 1407 | ret = -EPERM; |
| 1408 | goto error; |
| 1409 | } |
| 1410 | if (declaration->id != CTF_TYPE_STRUCT) { |
| 1411 | ret = -EPERM; |
| 1412 | goto error; |
| 1413 | } |
| 1414 | event->fields_decl = container_of(declaration, struct declaration_struct, p); |
| 1415 | } |
| 1416 | error: |
| 1417 | g_free(left); |
| 1418 | break; |
| 1419 | } |
| 1420 | default: |
| 1421 | return -EPERM; |
| 1422 | /* TODO: declaration specifier should be added. */ |
| 1423 | } |
| 1424 | |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | static |
| 1429 | int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node, |
| 1430 | struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace) |
| 1431 | { |
| 1432 | int ret = 0; |
| 1433 | struct ctf_node *iter; |
| 1434 | struct ctf_event *event; |
| 1435 | struct definition_scope *parent_def_scope; |
| 1436 | |
| 1437 | event = g_new0(struct ctf_event, 1); |
| 1438 | event->declaration_scope = new_declaration_scope(parent_declaration_scope); |
| 1439 | cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) { |
| 1440 | ret = ctf_event_declaration_visit(fd, depth + 1, iter, event, trace); |
| 1441 | if (ret) |
| 1442 | goto error; |
| 1443 | } |
| 1444 | if (!CTF_EVENT_FIELD_IS_SET(event, name)) { |
| 1445 | ret = -EPERM; |
| 1446 | fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__); |
| 1447 | goto error; |
| 1448 | } |
| 1449 | if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) { |
| 1450 | /* Allow missing stream_id if there is only a single stream */ |
| 1451 | switch (trace->streams->len) { |
| 1452 | case 0: /* Create stream if there was none. */ |
| 1453 | ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace); |
| 1454 | if (ret) |
| 1455 | goto error; |
| 1456 | /* Fall-through */ |
| 1457 | case 1: |
| 1458 | event->stream_id = 0; |
| 1459 | event->stream = trace_stream_lookup(trace, event->stream_id); |
| 1460 | break; |
| 1461 | default: |
| 1462 | ret = -EPERM; |
| 1463 | fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__); |
| 1464 | goto error; |
| 1465 | } |
| 1466 | } |
| 1467 | /* Allow only one event without id per stream */ |
| 1468 | if (!CTF_EVENT_FIELD_IS_SET(event, id) |
| 1469 | && event->stream->events_by_id->len != 0) { |
| 1470 | ret = -EPERM; |
| 1471 | fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__); |
| 1472 | goto error; |
| 1473 | } |
| 1474 | if (event->stream->events_by_id->len <= event->id) |
| 1475 | g_ptr_array_set_size(event->stream->events_by_id, event->id + 1); |
| 1476 | g_ptr_array_index(event->stream->events_by_id, event->id) = event; |
| 1477 | g_hash_table_insert(event->stream->event_quark_to_id, |
| 1478 | (gpointer)(unsigned long) event->name, |
| 1479 | &event->id); |
| 1480 | parent_def_scope = event->stream->definition_scope; |
| 1481 | if (event->context_decl) { |
| 1482 | event->context = |
| 1483 | container_of( |
| 1484 | event->context_decl->p.definition_new(&event->context_decl->p, |
| 1485 | parent_def_scope, 0, 0), |
| 1486 | struct definition_struct, p); |
| 1487 | set_dynamic_definition_scope(&event->context->p, |
| 1488 | event->context->scope, |
| 1489 | "event.context"); |
| 1490 | parent_def_scope = event->context->scope; |
| 1491 | declaration_unref(&event->context_decl->p); |
| 1492 | } |
| 1493 | if (event->fields_decl) { |
| 1494 | event->fields = |
| 1495 | container_of( |
| 1496 | event->fields_decl->p.definition_new(&event->fields_decl->p, |
| 1497 | parent_def_scope, 0, 0), |
| 1498 | struct definition_struct, p); |
| 1499 | set_dynamic_definition_scope(&event->fields->p, |
| 1500 | event->fields->scope, |
| 1501 | "event.fields"); |
| 1502 | parent_def_scope = event->fields->scope; |
| 1503 | declaration_unref(&event->fields_decl->p); |
| 1504 | } |
| 1505 | return 0; |
| 1506 | |
| 1507 | error: |
| 1508 | declaration_unref(&event->fields_decl->p); |
| 1509 | declaration_unref(&event->context_decl->p); |
| 1510 | free_declaration_scope(event->declaration_scope); |
| 1511 | g_free(event); |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
| 1515 | |
| 1516 | static |
| 1517 | int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream *stream, struct ctf_trace *trace) |
| 1518 | { |
| 1519 | int ret = 0; |
| 1520 | |
| 1521 | switch (node->type) { |
| 1522 | case NODE_TYPEDEF: |
| 1523 | ret = ctf_typedef_visit(fd, depth + 1, |
| 1524 | stream->declaration_scope, |
| 1525 | node->u._typedef.type_specifier_list, |
| 1526 | &node->u._typedef.type_declarators, |
| 1527 | trace); |
| 1528 | if (ret) |
| 1529 | return ret; |
| 1530 | break; |
| 1531 | case NODE_TYPEALIAS: |
| 1532 | ret = ctf_typealias_visit(fd, depth + 1, |
| 1533 | stream->declaration_scope, |
| 1534 | node->u.typealias.target, node->u.typealias.alias, |
| 1535 | trace); |
| 1536 | if (ret) |
| 1537 | return ret; |
| 1538 | break; |
| 1539 | case NODE_CTF_EXPRESSION: |
| 1540 | { |
| 1541 | char *left; |
| 1542 | |
| 1543 | left = concatenate_unary_strings(&node->u.ctf_expression.left); |
| 1544 | if (!strcmp(left, "id")) { |
| 1545 | if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) { |
| 1546 | fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__); |
| 1547 | ret = -EPERM; |
| 1548 | goto error; |
| 1549 | } |
| 1550 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id); |
| 1551 | if (ret) { |
| 1552 | fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__); |
| 1553 | ret = -EINVAL; |
| 1554 | goto error; |
| 1555 | } |
| 1556 | CTF_STREAM_SET_FIELD(stream, stream_id); |
| 1557 | } else if (!strcmp(left, "event.header")) { |
| 1558 | struct declaration *declaration; |
| 1559 | |
| 1560 | if (stream->event_header_decl) { |
| 1561 | fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__); |
| 1562 | ret = -EINVAL; |
| 1563 | goto error; |
| 1564 | } |
| 1565 | declaration = ctf_type_specifier_list_visit(fd, depth, |
| 1566 | _cds_list_first_entry(&node->u.ctf_expression.right, |
| 1567 | struct ctf_node, siblings), |
| 1568 | stream->declaration_scope, trace); |
| 1569 | if (!declaration) { |
| 1570 | ret = -EPERM; |
| 1571 | goto error; |
| 1572 | } |
| 1573 | if (declaration->id != CTF_TYPE_STRUCT) { |
| 1574 | ret = -EPERM; |
| 1575 | goto error; |
| 1576 | } |
| 1577 | stream->event_header_decl = container_of(declaration, struct declaration_struct, p); |
| 1578 | } else if (!strcmp(left, "event.context")) { |
| 1579 | struct declaration *declaration; |
| 1580 | |
| 1581 | if (stream->event_context_decl) { |
| 1582 | fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__); |
| 1583 | ret = -EINVAL; |
| 1584 | goto error; |
| 1585 | } |
| 1586 | declaration = ctf_type_specifier_list_visit(fd, depth, |
| 1587 | _cds_list_first_entry(&node->u.ctf_expression.right, |
| 1588 | struct ctf_node, siblings), |
| 1589 | stream->declaration_scope, trace); |
| 1590 | if (!declaration) { |
| 1591 | ret = -EPERM; |
| 1592 | goto error; |
| 1593 | } |
| 1594 | if (declaration->id != CTF_TYPE_STRUCT) { |
| 1595 | ret = -EPERM; |
| 1596 | goto error; |
| 1597 | } |
| 1598 | stream->event_context_decl = container_of(declaration, struct declaration_struct, p); |
| 1599 | } else if (!strcmp(left, "packet.context")) { |
| 1600 | struct declaration *declaration; |
| 1601 | |
| 1602 | if (stream->packet_context_decl) { |
| 1603 | fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__); |
| 1604 | ret = -EINVAL; |
| 1605 | goto error; |
| 1606 | } |
| 1607 | declaration = ctf_type_specifier_list_visit(fd, depth, |
| 1608 | _cds_list_first_entry(&node->u.ctf_expression.right, |
| 1609 | struct ctf_node, siblings), |
| 1610 | stream->declaration_scope, trace); |
| 1611 | if (!declaration) { |
| 1612 | ret = -EPERM; |
| 1613 | goto error; |
| 1614 | } |
| 1615 | if (declaration->id != CTF_TYPE_STRUCT) { |
| 1616 | ret = -EPERM; |
| 1617 | goto error; |
| 1618 | } |
| 1619 | stream->packet_context_decl = container_of(declaration, struct declaration_struct, p); |
| 1620 | } |
| 1621 | error: |
| 1622 | g_free(left); |
| 1623 | break; |
| 1624 | } |
| 1625 | default: |
| 1626 | return -EPERM; |
| 1627 | /* TODO: declaration specifier should be added. */ |
| 1628 | } |
| 1629 | |
| 1630 | return ret; |
| 1631 | } |
| 1632 | |
| 1633 | static |
| 1634 | int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node, |
| 1635 | struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace) |
| 1636 | { |
| 1637 | int ret = 0; |
| 1638 | struct ctf_node *iter; |
| 1639 | struct ctf_stream *stream; |
| 1640 | struct definition_scope *parent_def_scope; |
| 1641 | |
| 1642 | stream = g_new0(struct ctf_stream, 1); |
| 1643 | stream->declaration_scope = new_declaration_scope(parent_declaration_scope); |
| 1644 | stream->events_by_id = g_ptr_array_new(); |
| 1645 | stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 1646 | stream->files = g_ptr_array_new(); |
| 1647 | if (node) { |
| 1648 | cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { |
| 1649 | ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace); |
| 1650 | if (ret) |
| 1651 | goto error; |
| 1652 | } |
| 1653 | } |
| 1654 | if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) { |
| 1655 | /* check that packet header has stream_id field. */ |
| 1656 | if (!trace->packet_header_decl |
| 1657 | || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) { |
| 1658 | ret = -EPERM; |
| 1659 | fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__); |
| 1660 | goto error; |
| 1661 | } |
| 1662 | } else { |
| 1663 | /* Allow only one id-less stream */ |
| 1664 | if (trace->streams->len != 0) { |
| 1665 | ret = -EPERM; |
| 1666 | fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__); |
| 1667 | goto error; |
| 1668 | } |
| 1669 | stream->stream_id = 0; |
| 1670 | } |
| 1671 | if (trace->streams->len <= stream->stream_id) |
| 1672 | g_ptr_array_set_size(trace->streams, stream->stream_id + 1); |
| 1673 | g_ptr_array_index(trace->streams, stream->stream_id) = stream; |
| 1674 | |
| 1675 | parent_def_scope = trace->definition_scope; |
| 1676 | if (stream->packet_context_decl) { |
| 1677 | stream->packet_context = |
| 1678 | container_of( |
| 1679 | stream->packet_context_decl->p.definition_new(&stream->packet_context_decl->p, |
| 1680 | parent_def_scope, 0, 0), |
| 1681 | struct definition_struct, p); |
| 1682 | set_dynamic_definition_scope(&stream->packet_context->p, |
| 1683 | stream->packet_context->scope, |
| 1684 | "stream.packet.context"); |
| 1685 | parent_def_scope = stream->packet_context->scope; |
| 1686 | declaration_unref(&stream->packet_context_decl->p); |
| 1687 | } |
| 1688 | if (stream->event_header_decl) { |
| 1689 | stream->event_header = |
| 1690 | container_of( |
| 1691 | stream->event_header_decl->p.definition_new(&stream->event_header_decl->p, |
| 1692 | parent_def_scope, 0, 0), |
| 1693 | struct definition_struct, p); |
| 1694 | set_dynamic_definition_scope(&stream->event_header->p, |
| 1695 | stream->event_header->scope, |
| 1696 | "stream.event.header"); |
| 1697 | parent_def_scope = stream->event_header->scope; |
| 1698 | declaration_unref(&stream->event_header_decl->p); |
| 1699 | } |
| 1700 | if (stream->event_context_decl) { |
| 1701 | stream->event_context = |
| 1702 | container_of( |
| 1703 | stream->event_context_decl->p.definition_new(&stream->event_context_decl->p, |
| 1704 | parent_def_scope, 0, 0), |
| 1705 | struct definition_struct, p); |
| 1706 | set_dynamic_definition_scope(&stream->event_context->p, |
| 1707 | stream->event_context->scope, |
| 1708 | "stream.event.context"); |
| 1709 | parent_def_scope = stream->event_context->scope; |
| 1710 | declaration_unref(&stream->event_context_decl->p); |
| 1711 | } |
| 1712 | stream->definition_scope = parent_def_scope; |
| 1713 | |
| 1714 | return 0; |
| 1715 | |
| 1716 | error: |
| 1717 | declaration_unref(&stream->event_header_decl->p); |
| 1718 | declaration_unref(&stream->event_context_decl->p); |
| 1719 | declaration_unref(&stream->packet_context_decl->p); |
| 1720 | g_ptr_array_free(stream->files, TRUE); |
| 1721 | g_ptr_array_free(stream->events_by_id, TRUE); |
| 1722 | g_hash_table_destroy(stream->event_quark_to_id); |
| 1723 | free_declaration_scope(stream->declaration_scope); |
| 1724 | g_free(stream); |
| 1725 | return ret; |
| 1726 | } |
| 1727 | |
| 1728 | static |
| 1729 | int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace) |
| 1730 | { |
| 1731 | int ret = 0; |
| 1732 | |
| 1733 | switch (node->type) { |
| 1734 | case NODE_TYPEDEF: |
| 1735 | ret = ctf_typedef_visit(fd, depth + 1, |
| 1736 | trace->declaration_scope, |
| 1737 | node->u._typedef.type_specifier_list, |
| 1738 | &node->u._typedef.type_declarators, |
| 1739 | trace); |
| 1740 | if (ret) |
| 1741 | return ret; |
| 1742 | break; |
| 1743 | case NODE_TYPEALIAS: |
| 1744 | ret = ctf_typealias_visit(fd, depth + 1, |
| 1745 | trace->declaration_scope, |
| 1746 | node->u.typealias.target, node->u.typealias.alias, |
| 1747 | trace); |
| 1748 | if (ret) |
| 1749 | return ret; |
| 1750 | break; |
| 1751 | case NODE_CTF_EXPRESSION: |
| 1752 | { |
| 1753 | char *left; |
| 1754 | |
| 1755 | left = concatenate_unary_strings(&node->u.ctf_expression.left); |
| 1756 | if (!strcmp(left, "major")) { |
| 1757 | if (CTF_TRACE_FIELD_IS_SET(trace, major)) { |
| 1758 | fprintf(fd, "[error] %s: major already declared in trace declaration\n", __func__); |
| 1759 | ret = -EPERM; |
| 1760 | goto error; |
| 1761 | } |
| 1762 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->major); |
| 1763 | if (ret) { |
| 1764 | fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__); |
| 1765 | ret = -EINVAL; |
| 1766 | goto error; |
| 1767 | } |
| 1768 | CTF_TRACE_SET_FIELD(trace, major); |
| 1769 | } else if (!strcmp(left, "minor")) { |
| 1770 | if (CTF_TRACE_FIELD_IS_SET(trace, minor)) { |
| 1771 | fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__); |
| 1772 | ret = -EPERM; |
| 1773 | goto error; |
| 1774 | } |
| 1775 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor); |
| 1776 | if (ret) { |
| 1777 | fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__); |
| 1778 | ret = -EINVAL; |
| 1779 | goto error; |
| 1780 | } |
| 1781 | CTF_TRACE_SET_FIELD(trace, minor); |
| 1782 | } else if (!strcmp(left, "uuid")) { |
| 1783 | if (CTF_TRACE_FIELD_IS_SET(trace, uuid)) { |
| 1784 | fprintf(fd, "[error] %s: uuid already declared in trace declaration\n", __func__); |
| 1785 | ret = -EPERM; |
| 1786 | goto error; |
| 1787 | } |
| 1788 | ret = get_unary_uuid(&node->u.ctf_expression.right, &trace->uuid); |
| 1789 | if (ret) { |
| 1790 | fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__); |
| 1791 | ret = -EINVAL; |
| 1792 | goto error; |
| 1793 | } |
| 1794 | CTF_TRACE_SET_FIELD(trace, uuid); |
| 1795 | } else if (!strcmp(left, "byte_order")) { |
| 1796 | struct ctf_node *right; |
| 1797 | int byte_order; |
| 1798 | |
| 1799 | if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { |
| 1800 | fprintf(fd, "[error] %s: endianness already declared in trace declaration\n", __func__); |
| 1801 | ret = -EPERM; |
| 1802 | goto error; |
| 1803 | } |
| 1804 | right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings); |
| 1805 | byte_order = get_trace_byte_order(fd, depth, right); |
| 1806 | if (byte_order < 0) |
| 1807 | return -EINVAL; |
| 1808 | trace->byte_order = byte_order; |
| 1809 | CTF_TRACE_SET_FIELD(trace, byte_order); |
| 1810 | } else if (!strcmp(left, "packet.header")) { |
| 1811 | struct declaration *declaration; |
| 1812 | |
| 1813 | if (trace->packet_header_decl) { |
| 1814 | fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__); |
| 1815 | ret = -EINVAL; |
| 1816 | goto error; |
| 1817 | } |
| 1818 | declaration = ctf_type_specifier_list_visit(fd, depth, |
| 1819 | _cds_list_first_entry(&node->u.ctf_expression.right, |
| 1820 | struct ctf_node, siblings), |
| 1821 | trace->declaration_scope, trace); |
| 1822 | if (!declaration) { |
| 1823 | ret = -EPERM; |
| 1824 | goto error; |
| 1825 | } |
| 1826 | if (declaration->id != CTF_TYPE_STRUCT) { |
| 1827 | ret = -EPERM; |
| 1828 | goto error; |
| 1829 | } |
| 1830 | trace->packet_header_decl = container_of(declaration, struct declaration_struct, p); |
| 1831 | } |
| 1832 | error: |
| 1833 | g_free(left); |
| 1834 | break; |
| 1835 | } |
| 1836 | default: |
| 1837 | return -EPERM; |
| 1838 | /* TODO: declaration specifier should be added. */ |
| 1839 | } |
| 1840 | |
| 1841 | return ret; |
| 1842 | } |
| 1843 | |
| 1844 | static |
| 1845 | int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace) |
| 1846 | { |
| 1847 | struct definition_scope *parent_def_scope; |
| 1848 | int ret = 0; |
| 1849 | struct ctf_node *iter; |
| 1850 | |
| 1851 | if (trace->declaration_scope) |
| 1852 | return -EEXIST; |
| 1853 | trace->declaration_scope = new_declaration_scope(trace->root_declaration_scope); |
| 1854 | trace->streams = g_ptr_array_new(); |
| 1855 | cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) { |
| 1856 | ret = ctf_trace_declaration_visit(fd, depth + 1, iter, trace); |
| 1857 | if (ret) |
| 1858 | goto error; |
| 1859 | } |
| 1860 | if (!CTF_TRACE_FIELD_IS_SET(trace, major)) { |
| 1861 | ret = -EPERM; |
| 1862 | fprintf(fd, "[error] %s: missing major field in trace declaration\n", __func__); |
| 1863 | goto error; |
| 1864 | } |
| 1865 | if (!CTF_TRACE_FIELD_IS_SET(trace, minor)) { |
| 1866 | ret = -EPERM; |
| 1867 | fprintf(fd, "[error] %s: missing minor field in trace declaration\n", __func__); |
| 1868 | goto error; |
| 1869 | } |
| 1870 | if (!CTF_TRACE_FIELD_IS_SET(trace, uuid)) { |
| 1871 | ret = -EPERM; |
| 1872 | fprintf(fd, "[error] %s: missing uuid field in trace declaration\n", __func__); |
| 1873 | goto error; |
| 1874 | } |
| 1875 | if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { |
| 1876 | ret = -EPERM; |
| 1877 | fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__); |
| 1878 | goto error; |
| 1879 | } |
| 1880 | |
| 1881 | parent_def_scope = NULL; |
| 1882 | if (trace->packet_header_decl) { |
| 1883 | trace->packet_header = |
| 1884 | container_of( |
| 1885 | trace->packet_header_decl->p.definition_new(&trace->packet_header_decl->p, |
| 1886 | parent_def_scope, 0, 0), |
| 1887 | struct definition_struct, p); |
| 1888 | set_dynamic_definition_scope(&trace->packet_header->p, |
| 1889 | trace->packet_header->scope, |
| 1890 | "trace.packet.header"); |
| 1891 | parent_def_scope = trace->packet_header->scope; |
| 1892 | declaration_unref(&trace->packet_header_decl->p); |
| 1893 | } |
| 1894 | trace->definition_scope = parent_def_scope; |
| 1895 | |
| 1896 | if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { |
| 1897 | /* check that the packet header contains a "magic" field */ |
| 1898 | if (!trace->packet_header |
| 1899 | || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) { |
| 1900 | ret = -EPERM; |
| 1901 | fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__); |
| 1902 | goto error_free_def; |
| 1903 | } |
| 1904 | } |
| 1905 | return 0; |
| 1906 | |
| 1907 | error_free_def: |
| 1908 | definition_unref(&trace->packet_header->p); |
| 1909 | error: |
| 1910 | g_ptr_array_free(trace->streams, TRUE); |
| 1911 | free_declaration_scope(trace->declaration_scope); |
| 1912 | return ret; |
| 1913 | } |
| 1914 | |
| 1915 | static |
| 1916 | int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace) |
| 1917 | { |
| 1918 | int ret = 0; |
| 1919 | |
| 1920 | switch (node->type) { |
| 1921 | case NODE_TYPEDEF: |
| 1922 | ret = ctf_typedef_visit(fd, depth + 1, |
| 1923 | trace->root_declaration_scope, |
| 1924 | node->u._typedef.type_specifier_list, |
| 1925 | &node->u._typedef.type_declarators, |
| 1926 | trace); |
| 1927 | if (ret) |
| 1928 | return ret; |
| 1929 | break; |
| 1930 | case NODE_TYPEALIAS: |
| 1931 | ret = ctf_typealias_visit(fd, depth + 1, |
| 1932 | trace->root_declaration_scope, |
| 1933 | node->u.typealias.target, node->u.typealias.alias, |
| 1934 | trace); |
| 1935 | if (ret) |
| 1936 | return ret; |
| 1937 | break; |
| 1938 | case NODE_TYPE_SPECIFIER_LIST: |
| 1939 | { |
| 1940 | struct declaration *declaration; |
| 1941 | |
| 1942 | /* |
| 1943 | * Just add the type specifier to the root scope |
| 1944 | * declaration scope. Release local reference. |
| 1945 | */ |
| 1946 | declaration = ctf_type_specifier_list_visit(fd, depth + 1, |
| 1947 | node, trace->root_declaration_scope, trace); |
| 1948 | if (!declaration) |
| 1949 | return -ENOMEM; |
| 1950 | declaration_unref(declaration); |
| 1951 | break; |
| 1952 | } |
| 1953 | default: |
| 1954 | return -EPERM; |
| 1955 | } |
| 1956 | |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node, |
| 1961 | struct ctf_trace *trace, int byte_order) |
| 1962 | { |
| 1963 | int ret = 0; |
| 1964 | struct ctf_node *iter; |
| 1965 | |
| 1966 | fprintf(fd, "CTF visitor: metadata construction... "); |
| 1967 | trace->root_declaration_scope = new_declaration_scope(NULL); |
| 1968 | trace->byte_order = byte_order; |
| 1969 | |
| 1970 | switch (node->type) { |
| 1971 | case NODE_ROOT: |
| 1972 | cds_list_for_each_entry(iter, &node->u.root.declaration_list, |
| 1973 | siblings) { |
| 1974 | ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace); |
| 1975 | if (ret) { |
| 1976 | fprintf(fd, "[error] %s: root declaration error\n", __func__); |
| 1977 | return ret; |
| 1978 | } |
| 1979 | } |
| 1980 | cds_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
| 1981 | ret = ctf_trace_visit(fd, depth + 1, iter, trace); |
| 1982 | if (ret) { |
| 1983 | fprintf(fd, "[error] %s: trace declaration error\n", __func__); |
| 1984 | return ret; |
| 1985 | } |
| 1986 | } |
| 1987 | if (!trace->streams) { |
| 1988 | fprintf(fd, "[error] %s: missing trace declaration\n", __func__); |
| 1989 | return -EINVAL; |
| 1990 | } |
| 1991 | cds_list_for_each_entry(iter, &node->u.root.stream, siblings) { |
| 1992 | ret = ctf_stream_visit(fd, depth + 1, iter, |
| 1993 | trace->root_declaration_scope, trace); |
| 1994 | if (ret) { |
| 1995 | fprintf(fd, "[error] %s: stream declaration error\n", __func__); |
| 1996 | return ret; |
| 1997 | } |
| 1998 | } |
| 1999 | cds_list_for_each_entry(iter, &node->u.root.event, siblings) { |
| 2000 | ret = ctf_event_visit(fd, depth + 1, iter, |
| 2001 | trace->root_declaration_scope, trace); |
| 2002 | if (ret) { |
| 2003 | fprintf(fd, "[error] %s: event declaration error\n", __func__); |
| 2004 | return ret; |
| 2005 | } |
| 2006 | } |
| 2007 | break; |
| 2008 | case NODE_UNKNOWN: |
| 2009 | default: |
| 2010 | fprintf(fd, "[error] %s: unknown node type %d\n", __func__, |
| 2011 | (int) node->type); |
| 2012 | return -EINVAL; |
| 2013 | } |
| 2014 | fprintf(fd, "done.\n"); |
| 2015 | return ret; |
| 2016 | } |