| 1 | #ifndef _CTF_AST_H |
| 2 | #define _CTF_AST_H |
| 3 | |
| 4 | /* |
| 5 | * ctf-ast.h |
| 6 | * |
| 7 | * Copyright 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | */ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <glib.h> |
| 23 | #include "common/list.h" |
| 24 | #include <babeltrace2/babeltrace.h> |
| 25 | #include "common/macros.h" |
| 26 | #include "common/assert.h" |
| 27 | |
| 28 | #include "decoder.h" |
| 29 | #include "ctf-meta.h" |
| 30 | |
| 31 | // the parameter name (of the reentrant 'yyparse' function) |
| 32 | // data is a pointer to a 'SParserParam' structure |
| 33 | //#define YYPARSE_PARAM scanner |
| 34 | |
| 35 | struct ctf_node; |
| 36 | struct ctf_parser; |
| 37 | struct ctf_visitor_generate_ir; |
| 38 | |
| 39 | #define EINCOMPLETE 1000 |
| 40 | |
| 41 | #define FOREACH_CTF_NODES(F) \ |
| 42 | F(NODE_UNKNOWN) \ |
| 43 | F(NODE_ROOT) \ |
| 44 | F(NODE_ERROR) \ |
| 45 | F(NODE_EVENT) \ |
| 46 | F(NODE_STREAM) \ |
| 47 | F(NODE_ENV) \ |
| 48 | F(NODE_TRACE) \ |
| 49 | F(NODE_CLOCK) \ |
| 50 | F(NODE_CALLSITE) \ |
| 51 | F(NODE_CTF_EXPRESSION) \ |
| 52 | F(NODE_UNARY_EXPRESSION) \ |
| 53 | F(NODE_TYPEDEF) \ |
| 54 | F(NODE_TYPEALIAS_TARGET) \ |
| 55 | F(NODE_TYPEALIAS_ALIAS) \ |
| 56 | F(NODE_TYPEALIAS) \ |
| 57 | F(NODE_TYPE_SPECIFIER) \ |
| 58 | F(NODE_TYPE_SPECIFIER_LIST) \ |
| 59 | F(NODE_POINTER) \ |
| 60 | F(NODE_TYPE_DECLARATOR) \ |
| 61 | F(NODE_FLOATING_POINT) \ |
| 62 | F(NODE_INTEGER) \ |
| 63 | F(NODE_STRING) \ |
| 64 | F(NODE_ENUMERATOR) \ |
| 65 | F(NODE_ENUM) \ |
| 66 | F(NODE_STRUCT_OR_VARIANT_DECLARATION) \ |
| 67 | F(NODE_VARIANT) \ |
| 68 | F(NODE_STRUCT) |
| 69 | |
| 70 | enum node_type { |
| 71 | #define ENTRY(S) S, |
| 72 | FOREACH_CTF_NODES(ENTRY) |
| 73 | #undef ENTRY |
| 74 | NR_NODE_TYPES, |
| 75 | }; |
| 76 | |
| 77 | struct ctf_node { |
| 78 | /* |
| 79 | * Parent node is only set on demand by specific visitor. |
| 80 | */ |
| 81 | struct ctf_node *parent; |
| 82 | struct bt_list_head siblings; |
| 83 | struct bt_list_head tmp_head; |
| 84 | unsigned int lineno; |
| 85 | /* |
| 86 | * We mark nodes visited in the generate-ir phase (last |
| 87 | * phase). We only mark the 1-depth level nodes as visited |
| 88 | * (never the root node, and not their sub-nodes). This allows |
| 89 | * skipping already visited nodes when doing incremental |
| 90 | * metadata append. |
| 91 | */ |
| 92 | int visited; |
| 93 | |
| 94 | enum node_type type; |
| 95 | union { |
| 96 | struct { |
| 97 | } unknown; |
| 98 | struct { |
| 99 | /* |
| 100 | * Children nodes are ctf_expression, field_class_def, |
| 101 | * field_class_alias and field_class_specifier_list. |
| 102 | */ |
| 103 | struct bt_list_head declaration_list; |
| 104 | struct bt_list_head trace; |
| 105 | struct bt_list_head env; |
| 106 | struct bt_list_head stream; |
| 107 | struct bt_list_head event; |
| 108 | struct bt_list_head clock; |
| 109 | struct bt_list_head callsite; |
| 110 | } root; |
| 111 | struct { |
| 112 | /* |
| 113 | * Children nodes are ctf_expression, field_class_def, |
| 114 | * field_class_alias and field_class_specifier_list. |
| 115 | */ |
| 116 | struct bt_list_head declaration_list; |
| 117 | } event; |
| 118 | struct { |
| 119 | /* |
| 120 | * Children nodes are ctf_expression, field_class_def, |
| 121 | * field_class_alias and field_class_specifier_list. |
| 122 | */ |
| 123 | struct bt_list_head declaration_list; |
| 124 | } stream; |
| 125 | struct { |
| 126 | /* |
| 127 | * Children nodes are ctf_expression, field_class_def, |
| 128 | * field_class_alias and field_class_specifier_list. |
| 129 | */ |
| 130 | struct bt_list_head declaration_list; |
| 131 | } env; |
| 132 | struct { |
| 133 | /* |
| 134 | * Children nodes are ctf_expression, field_class_def, |
| 135 | * field_class_alias and field_class_specifier_list. |
| 136 | */ |
| 137 | struct bt_list_head declaration_list; |
| 138 | } trace; |
| 139 | struct { |
| 140 | /* |
| 141 | * Children nodes are ctf_expression, field_class_def, |
| 142 | * field_class_alias and field_class_specifier_list. |
| 143 | */ |
| 144 | struct bt_list_head declaration_list; |
| 145 | } clock; |
| 146 | struct { |
| 147 | /* |
| 148 | * Children nodes are ctf_expression, field_class_def, |
| 149 | * field_class_alias and field_class_specifier_list. |
| 150 | */ |
| 151 | struct bt_list_head declaration_list; |
| 152 | } callsite; |
| 153 | struct { |
| 154 | struct bt_list_head left; /* Should be string */ |
| 155 | struct bt_list_head right; /* Unary exp. or type */ |
| 156 | } ctf_expression; |
| 157 | struct { |
| 158 | enum { |
| 159 | UNARY_UNKNOWN = 0, |
| 160 | UNARY_STRING, |
| 161 | UNARY_SIGNED_CONSTANT, |
| 162 | UNARY_UNSIGNED_CONSTANT, |
| 163 | UNARY_SBRAC, |
| 164 | } type; |
| 165 | union { |
| 166 | /* |
| 167 | * string for identifier, id_type, keywords, |
| 168 | * string literals and character constants. |
| 169 | */ |
| 170 | char *string; |
| 171 | int64_t signed_constant; |
| 172 | uint64_t unsigned_constant; |
| 173 | struct ctf_node *sbrac_exp; |
| 174 | } u; |
| 175 | enum { |
| 176 | UNARY_LINK_UNKNOWN = 0, |
| 177 | UNARY_DOTLINK, |
| 178 | UNARY_ARROWLINK, |
| 179 | UNARY_DOTDOTDOT, |
| 180 | } link; |
| 181 | } unary_expression; |
| 182 | struct { |
| 183 | struct ctf_node *field_class_specifier_list; |
| 184 | struct bt_list_head field_class_declarators; |
| 185 | } field_class_def; |
| 186 | /* new type is "alias", existing type "target" */ |
| 187 | struct { |
| 188 | struct ctf_node *field_class_specifier_list; |
| 189 | struct bt_list_head field_class_declarators; |
| 190 | } field_class_alias_target; |
| 191 | struct { |
| 192 | struct ctf_node *field_class_specifier_list; |
| 193 | struct bt_list_head field_class_declarators; |
| 194 | } field_class_alias_name; |
| 195 | struct { |
| 196 | struct ctf_node *target; |
| 197 | struct ctf_node *alias; |
| 198 | } field_class_alias; |
| 199 | struct { |
| 200 | enum { |
| 201 | TYPESPEC_UNKNOWN = 0, |
| 202 | TYPESPEC_VOID, |
| 203 | TYPESPEC_CHAR, |
| 204 | TYPESPEC_SHORT, |
| 205 | TYPESPEC_INT, |
| 206 | TYPESPEC_LONG, |
| 207 | TYPESPEC_FLOAT, |
| 208 | TYPESPEC_DOUBLE, |
| 209 | TYPESPEC_SIGNED, |
| 210 | TYPESPEC_UNSIGNED, |
| 211 | TYPESPEC_BOOL, |
| 212 | TYPESPEC_COMPLEX, |
| 213 | TYPESPEC_IMAGINARY, |
| 214 | TYPESPEC_CONST, |
| 215 | TYPESPEC_ID_TYPE, |
| 216 | TYPESPEC_FLOATING_POINT, |
| 217 | TYPESPEC_INTEGER, |
| 218 | TYPESPEC_STRING, |
| 219 | TYPESPEC_STRUCT, |
| 220 | TYPESPEC_VARIANT, |
| 221 | TYPESPEC_ENUM, |
| 222 | } type; |
| 223 | /* For struct, variant and enum */ |
| 224 | struct ctf_node *node; |
| 225 | const char *id_type; |
| 226 | } field_class_specifier; |
| 227 | struct { |
| 228 | /* list of field_class_specifier */ |
| 229 | struct bt_list_head head; |
| 230 | } field_class_specifier_list; |
| 231 | struct { |
| 232 | unsigned int const_qualifier; |
| 233 | } pointer; |
| 234 | struct { |
| 235 | struct bt_list_head pointers; |
| 236 | enum { |
| 237 | TYPEDEC_UNKNOWN = 0, |
| 238 | TYPEDEC_ID, /* identifier */ |
| 239 | TYPEDEC_NESTED, /* (), array or sequence */ |
| 240 | } type; |
| 241 | union { |
| 242 | char *id; |
| 243 | struct { |
| 244 | /* typedec has no pointer list */ |
| 245 | struct ctf_node *field_class_declarator; |
| 246 | /* |
| 247 | * unary expression (value) or |
| 248 | * field_class_specifier_list. |
| 249 | */ |
| 250 | struct bt_list_head length; |
| 251 | /* for abstract type declarator */ |
| 252 | unsigned int abstract_array; |
| 253 | } nested; |
| 254 | } u; |
| 255 | struct ctf_node *bitfield_len; |
| 256 | } field_class_declarator; |
| 257 | struct { |
| 258 | /* Children nodes are ctf_expression. */ |
| 259 | struct bt_list_head expressions; |
| 260 | } floating_point; |
| 261 | struct { |
| 262 | /* Children nodes are ctf_expression. */ |
| 263 | struct bt_list_head expressions; |
| 264 | } integer; |
| 265 | struct { |
| 266 | /* Children nodes are ctf_expression. */ |
| 267 | struct bt_list_head expressions; |
| 268 | } string; |
| 269 | struct { |
| 270 | char *id; |
| 271 | /* |
| 272 | * Range list or single value node. Contains unary |
| 273 | * expressions. |
| 274 | */ |
| 275 | struct bt_list_head values; |
| 276 | } enumerator; |
| 277 | struct { |
| 278 | char *enum_id; |
| 279 | /* |
| 280 | * Either NULL, or points to unary expression or |
| 281 | * field_class_specifier_list. |
| 282 | */ |
| 283 | struct ctf_node *container_field_class; |
| 284 | struct bt_list_head enumerator_list; |
| 285 | int has_body; |
| 286 | } _enum; |
| 287 | struct { |
| 288 | struct ctf_node *field_class_specifier_list; |
| 289 | struct bt_list_head field_class_declarators; |
| 290 | } struct_or_variant_declaration; |
| 291 | struct { |
| 292 | char *name; |
| 293 | char *choice; |
| 294 | /* |
| 295 | * list of field_class_def, field_class_alias and |
| 296 | * declarations |
| 297 | */ |
| 298 | struct bt_list_head declaration_list; |
| 299 | int has_body; |
| 300 | } variant; |
| 301 | struct { |
| 302 | char *name; |
| 303 | /* |
| 304 | * list of field_class_def, field_class_alias and |
| 305 | * declarations |
| 306 | */ |
| 307 | struct bt_list_head declaration_list; |
| 308 | int has_body; |
| 309 | struct bt_list_head min_align; /* align() attribute */ |
| 310 | } _struct; |
| 311 | } u; |
| 312 | }; |
| 313 | |
| 314 | struct ctf_ast { |
| 315 | struct ctf_node root; |
| 316 | }; |
| 317 | |
| 318 | const char *node_type(struct ctf_node *node); |
| 319 | |
| 320 | struct meta_log_config; |
| 321 | |
| 322 | BT_HIDDEN |
| 323 | struct ctf_visitor_generate_ir *ctf_visitor_generate_ir_create( |
| 324 | const struct ctf_metadata_decoder_config *config); |
| 325 | |
| 326 | void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor); |
| 327 | |
| 328 | BT_HIDDEN |
| 329 | bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class( |
| 330 | struct ctf_visitor_generate_ir *visitor); |
| 331 | |
| 332 | BT_HIDDEN |
| 333 | struct ctf_trace_class *ctf_visitor_generate_ir_borrow_ctf_trace_class( |
| 334 | struct ctf_visitor_generate_ir *visitor); |
| 335 | |
| 336 | BT_HIDDEN |
| 337 | int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, |
| 338 | struct ctf_node *node); |
| 339 | |
| 340 | BT_HIDDEN |
| 341 | int ctf_visitor_semantic_check(int depth, struct ctf_node *node, |
| 342 | struct meta_log_config *log_cfg); |
| 343 | |
| 344 | BT_HIDDEN |
| 345 | int ctf_visitor_parent_links(int depth, struct ctf_node *node, |
| 346 | struct meta_log_config *log_cfg); |
| 347 | |
| 348 | static inline |
| 349 | char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head) |
| 350 | { |
| 351 | int i = 0; |
| 352 | GString *str; |
| 353 | struct ctf_node *node; |
| 354 | |
| 355 | str = g_string_new(NULL); |
| 356 | BT_ASSERT(str); |
| 357 | |
| 358 | bt_list_for_each_entry(node, head, siblings) { |
| 359 | char *src_string; |
| 360 | |
| 361 | if ( |
| 362 | node->type != NODE_UNARY_EXPRESSION || |
| 363 | node->u.unary_expression.type != UNARY_STRING || |
| 364 | !( |
| 365 | ( |
| 366 | node->u.unary_expression.link != |
| 367 | UNARY_LINK_UNKNOWN |
| 368 | ) ^ (i == 0) |
| 369 | ) |
| 370 | ) { |
| 371 | goto error; |
| 372 | } |
| 373 | |
| 374 | switch (node->u.unary_expression.link) { |
| 375 | case UNARY_DOTLINK: |
| 376 | g_string_append(str, "."); |
| 377 | break; |
| 378 | case UNARY_ARROWLINK: |
| 379 | g_string_append(str, "->"); |
| 380 | break; |
| 381 | case UNARY_DOTDOTDOT: |
| 382 | g_string_append(str, "..."); |
| 383 | break; |
| 384 | default: |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | src_string = node->u.unary_expression.u.string; |
| 389 | g_string_append(str, src_string); |
| 390 | i++; |
| 391 | } |
| 392 | |
| 393 | /* Destroys the container, returns the underlying string */ |
| 394 | return g_string_free(str, FALSE); |
| 395 | |
| 396 | error: |
| 397 | /* This always returns NULL */ |
| 398 | return g_string_free(str, TRUE); |
| 399 | } |
| 400 | |
| 401 | static inline |
| 402 | int ctf_ast_get_unary_uuid(struct bt_list_head *head, |
| 403 | bt_uuid_t uuid, int log_level, bt_self_component *self_comp) |
| 404 | { |
| 405 | int i = 0; |
| 406 | int ret = 0; |
| 407 | struct ctf_node *node; |
| 408 | |
| 409 | bt_list_for_each_entry(node, head, siblings) { |
| 410 | int uexpr_type = node->u.unary_expression.type; |
| 411 | int uexpr_link = node->u.unary_expression.link; |
| 412 | const char *src_string; |
| 413 | |
| 414 | if (node->type != NODE_UNARY_EXPRESSION || |
| 415 | uexpr_type != UNARY_STRING || |
| 416 | uexpr_link != UNARY_LINK_UNKNOWN || |
| 417 | i != 0) { |
| 418 | ret = -EINVAL; |
| 419 | goto end; |
| 420 | } |
| 421 | |
| 422 | src_string = node->u.unary_expression.u.string; |
| 423 | ret = bt_uuid_from_str(src_string, uuid); |
| 424 | if (ret) { |
| 425 | #ifdef BT_COMP_LOG_CUR_LVL |
| 426 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, |
| 427 | self_comp, |
| 428 | "Cannot parse UUID: uuid=\"%s\"", src_string); |
| 429 | #endif |
| 430 | goto end; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | end: |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | #endif /* _CTF_AST_H */ |