| 1 |
/* |
| 2 |
* ctf-visitor-parent-links.c |
| 3 |
* |
| 4 |
* Common Trace Format Metadata Parent Link Creator. |
| 5 |
* |
| 6 |
* Copyright 2010 - Mathieu Desnoyers <[email protected]> |
| 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 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
* SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <assert.h> |
| 32 |
#include <glib.h> |
| 33 |
#include <inttypes.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <babeltrace/babeltrace-internal.h> |
| 36 |
#include <babeltrace/list.h> |
| 37 |
#include "ctf-scanner.h" |
| 38 |
#include "ctf-parser.h" |
| 39 |
#include "ctf-ast.h" |
| 40 |
|
| 41 |
#define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args) |
| 42 |
|
| 43 |
static |
| 44 |
int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node) |
| 45 |
{ |
| 46 |
int ret = 0; |
| 47 |
|
| 48 |
switch (node->u.unary_expression.link) { |
| 49 |
case UNARY_LINK_UNKNOWN: |
| 50 |
case UNARY_DOTLINK: |
| 51 |
case UNARY_ARROWLINK: |
| 52 |
case UNARY_DOTDOTDOT: |
| 53 |
break; |
| 54 |
default: |
| 55 |
fprintf(fd, "[error] %s: unknown expression link type %d\n", __func__, |
| 56 |
(int) node->u.unary_expression.link); |
| 57 |
return -EINVAL; |
| 58 |
} |
| 59 |
|
| 60 |
switch (node->u.unary_expression.type) { |
| 61 |
case UNARY_STRING: |
| 62 |
case UNARY_SIGNED_CONSTANT: |
| 63 |
case UNARY_UNSIGNED_CONSTANT: |
| 64 |
break; |
| 65 |
case UNARY_SBRAC: |
| 66 |
node->u.unary_expression.u.sbrac_exp->parent = node; |
| 67 |
ret = ctf_visitor_unary_expression(fd, depth + 1, |
| 68 |
node->u.unary_expression.u.sbrac_exp); |
| 69 |
if (ret) |
| 70 |
return ret; |
| 71 |
break; |
| 72 |
|
| 73 |
case UNARY_UNKNOWN: |
| 74 |
default: |
| 75 |
fprintf(fd, "[error] %s: unknown expression type %d\n", __func__, |
| 76 |
(int) node->u.unary_expression.type); |
| 77 |
return -EINVAL; |
| 78 |
} |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
static |
| 83 |
int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node) |
| 84 |
{ |
| 85 |
int ret; |
| 86 |
|
| 87 |
switch (node->u.type_specifier.type) { |
| 88 |
case TYPESPEC_VOID: |
| 89 |
case TYPESPEC_CHAR: |
| 90 |
case TYPESPEC_SHORT: |
| 91 |
case TYPESPEC_INT: |
| 92 |
case TYPESPEC_LONG: |
| 93 |
case TYPESPEC_FLOAT: |
| 94 |
case TYPESPEC_DOUBLE: |
| 95 |
case TYPESPEC_SIGNED: |
| 96 |
case TYPESPEC_UNSIGNED: |
| 97 |
case TYPESPEC_BOOL: |
| 98 |
case TYPESPEC_COMPLEX: |
| 99 |
case TYPESPEC_IMAGINARY: |
| 100 |
case TYPESPEC_CONST: |
| 101 |
case TYPESPEC_ID_TYPE: |
| 102 |
break; |
| 103 |
case TYPESPEC_FLOATING_POINT: |
| 104 |
case TYPESPEC_INTEGER: |
| 105 |
case TYPESPEC_STRING: |
| 106 |
case TYPESPEC_STRUCT: |
| 107 |
case TYPESPEC_VARIANT: |
| 108 |
case TYPESPEC_ENUM: |
| 109 |
node->u.type_specifier.node->parent = node; |
| 110 |
ret = ctf_visitor_parent_links(fd, depth + 1, node->u.type_specifier.node); |
| 111 |
if (ret) |
| 112 |
return ret; |
| 113 |
break; |
| 114 |
|
| 115 |
case TYPESPEC_UNKNOWN: |
| 116 |
default: |
| 117 |
fprintf(fd, "[error] %s: unknown type specifier %d\n", __func__, |
| 118 |
(int) node->u.type_specifier.type); |
| 119 |
return -EINVAL; |
| 120 |
} |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
static |
| 125 |
int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node) |
| 126 |
{ |
| 127 |
int ret = 0; |
| 128 |
struct ctf_node *iter; |
| 129 |
|
| 130 |
depth++; |
| 131 |
|
| 132 |
bt_list_for_each_entry(iter, &node->u.type_declarator.pointers, |
| 133 |
siblings) { |
| 134 |
iter->parent = node; |
| 135 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 136 |
if (ret) |
| 137 |
return ret; |
| 138 |
} |
| 139 |
|
| 140 |
switch (node->u.type_declarator.type) { |
| 141 |
case TYPEDEC_ID: |
| 142 |
break; |
| 143 |
case TYPEDEC_NESTED: |
| 144 |
if (node->u.type_declarator.u.nested.type_declarator) { |
| 145 |
node->u.type_declarator.u.nested.type_declarator->parent = node; |
| 146 |
ret = ctf_visitor_parent_links(fd, depth + 1, |
| 147 |
node->u.type_declarator.u.nested.type_declarator); |
| 148 |
if (ret) |
| 149 |
return ret; |
| 150 |
} |
| 151 |
if (!node->u.type_declarator.u.nested.abstract_array) { |
| 152 |
bt_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length, |
| 153 |
siblings) { |
| 154 |
iter->parent = node; |
| 155 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 156 |
if (ret) |
| 157 |
return ret; |
| 158 |
} |
| 159 |
} |
| 160 |
if (node->u.type_declarator.bitfield_len) { |
| 161 |
node->u.type_declarator.bitfield_len = node; |
| 162 |
ret = ctf_visitor_parent_links(fd, depth + 1, |
| 163 |
node->u.type_declarator.bitfield_len); |
| 164 |
if (ret) |
| 165 |
return ret; |
| 166 |
} |
| 167 |
break; |
| 168 |
case TYPEDEC_UNKNOWN: |
| 169 |
default: |
| 170 |
fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__, |
| 171 |
(int) node->u.type_declarator.type); |
| 172 |
return -EINVAL; |
| 173 |
} |
| 174 |
depth--; |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
int ctf_visitor_parent_links(FILE *fd, int depth, struct ctf_node *node) |
| 179 |
{ |
| 180 |
int ret = 0; |
| 181 |
struct ctf_node *iter; |
| 182 |
|
| 183 |
if (node->visited) |
| 184 |
return 0; |
| 185 |
|
| 186 |
switch (node->type) { |
| 187 |
case NODE_ROOT: |
| 188 |
bt_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) { |
| 189 |
iter->parent = node; |
| 190 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 191 |
if (ret) |
| 192 |
return ret; |
| 193 |
} |
| 194 |
bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
| 195 |
iter->parent = node; |
| 196 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 197 |
if (ret) |
| 198 |
return ret; |
| 199 |
} |
| 200 |
bt_list_for_each_entry(iter, &node->u.root.stream, siblings) { |
| 201 |
iter->parent = node; |
| 202 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 203 |
if (ret) |
| 204 |
return ret; |
| 205 |
} |
| 206 |
bt_list_for_each_entry(iter, &node->u.root.event, siblings) { |
| 207 |
iter->parent = node; |
| 208 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 209 |
if (ret) |
| 210 |
return ret; |
| 211 |
} |
| 212 |
bt_list_for_each_entry(iter, &node->u.root.clock, siblings) { |
| 213 |
iter->parent = node; |
| 214 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 215 |
if (ret) |
| 216 |
return ret; |
| 217 |
} |
| 218 |
bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) { |
| 219 |
iter->parent = node; |
| 220 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 221 |
if (ret) |
| 222 |
return ret; |
| 223 |
} |
| 224 |
break; |
| 225 |
|
| 226 |
case NODE_EVENT: |
| 227 |
bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) { |
| 228 |
iter->parent = node; |
| 229 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 230 |
if (ret) |
| 231 |
return ret; |
| 232 |
} |
| 233 |
break; |
| 234 |
case NODE_STREAM: |
| 235 |
bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { |
| 236 |
iter->parent = node; |
| 237 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 238 |
if (ret) |
| 239 |
return ret; |
| 240 |
} |
| 241 |
break; |
| 242 |
case NODE_ENV: |
| 243 |
bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) { |
| 244 |
iter->parent = node; |
| 245 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 246 |
if (ret) |
| 247 |
return ret; |
| 248 |
} |
| 249 |
break; |
| 250 |
case NODE_TRACE: |
| 251 |
bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) { |
| 252 |
iter->parent = node; |
| 253 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 254 |
if (ret) |
| 255 |
return ret; |
| 256 |
} |
| 257 |
break; |
| 258 |
case NODE_CLOCK: |
| 259 |
bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) { |
| 260 |
iter->parent = node; |
| 261 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 262 |
if (ret) |
| 263 |
return ret; |
| 264 |
} |
| 265 |
break; |
| 266 |
case NODE_CALLSITE: |
| 267 |
bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) { |
| 268 |
iter->parent = node; |
| 269 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 270 |
if (ret) |
| 271 |
return ret; |
| 272 |
} |
| 273 |
break; |
| 274 |
|
| 275 |
case NODE_CTF_EXPRESSION: |
| 276 |
depth++; |
| 277 |
bt_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) { |
| 278 |
iter->parent = node; |
| 279 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 280 |
if (ret) |
| 281 |
return ret; |
| 282 |
} |
| 283 |
bt_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) { |
| 284 |
iter->parent = node; |
| 285 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 286 |
if (ret) |
| 287 |
return ret; |
| 288 |
} |
| 289 |
depth--; |
| 290 |
break; |
| 291 |
case NODE_UNARY_EXPRESSION: |
| 292 |
return ctf_visitor_unary_expression(fd, depth, node); |
| 293 |
|
| 294 |
case NODE_TYPEDEF: |
| 295 |
depth++; |
| 296 |
node->u._typedef.type_specifier_list->parent = node; |
| 297 |
ret = ctf_visitor_parent_links(fd, depth + 1, node->u._typedef.type_specifier_list); |
| 298 |
if (ret) |
| 299 |
return ret; |
| 300 |
bt_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) { |
| 301 |
iter->parent = node; |
| 302 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 303 |
if (ret) |
| 304 |
return ret; |
| 305 |
} |
| 306 |
depth--; |
| 307 |
break; |
| 308 |
case NODE_TYPEALIAS_TARGET: |
| 309 |
depth++; |
| 310 |
node->u.typealias_target.type_specifier_list->parent = node; |
| 311 |
ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_target.type_specifier_list); |
| 312 |
if (ret) |
| 313 |
return ret; |
| 314 |
bt_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) { |
| 315 |
iter->parent = node; |
| 316 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 317 |
if (ret) |
| 318 |
return ret; |
| 319 |
} |
| 320 |
depth--; |
| 321 |
break; |
| 322 |
case NODE_TYPEALIAS_ALIAS: |
| 323 |
depth++; |
| 324 |
node->u.typealias_alias.type_specifier_list->parent = node; |
| 325 |
ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_alias.type_specifier_list); |
| 326 |
if (ret) |
| 327 |
return ret; |
| 328 |
bt_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) { |
| 329 |
iter->parent = node; |
| 330 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 331 |
if (ret) |
| 332 |
return ret; |
| 333 |
} |
| 334 |
depth--; |
| 335 |
break; |
| 336 |
case NODE_TYPEALIAS: |
| 337 |
node->u.typealias.target->parent = node; |
| 338 |
ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.target); |
| 339 |
if (ret) |
| 340 |
return ret; |
| 341 |
node->u.typealias.alias->parent = node; |
| 342 |
ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.alias); |
| 343 |
if (ret) |
| 344 |
return ret; |
| 345 |
break; |
| 346 |
|
| 347 |
case NODE_TYPE_SPECIFIER_LIST: |
| 348 |
bt_list_for_each_entry(iter, &node->u.type_specifier_list.head, siblings) { |
| 349 |
iter->parent = node; |
| 350 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 351 |
if (ret) |
| 352 |
return ret; |
| 353 |
} |
| 354 |
break; |
| 355 |
|
| 356 |
case NODE_TYPE_SPECIFIER: |
| 357 |
ret = ctf_visitor_type_specifier(fd, depth, node); |
| 358 |
if (ret) |
| 359 |
return ret; |
| 360 |
break; |
| 361 |
case NODE_POINTER: |
| 362 |
break; |
| 363 |
case NODE_TYPE_DECLARATOR: |
| 364 |
ret = ctf_visitor_type_declarator(fd, depth, node); |
| 365 |
if (ret) |
| 366 |
return ret; |
| 367 |
break; |
| 368 |
|
| 369 |
case NODE_FLOATING_POINT: |
| 370 |
bt_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) { |
| 371 |
iter->parent = node; |
| 372 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 373 |
if (ret) |
| 374 |
return ret; |
| 375 |
} |
| 376 |
break; |
| 377 |
case NODE_INTEGER: |
| 378 |
bt_list_for_each_entry(iter, &node->u.integer.expressions, siblings) { |
| 379 |
iter->parent = node; |
| 380 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 381 |
if (ret) |
| 382 |
return ret; |
| 383 |
} |
| 384 |
break; |
| 385 |
case NODE_STRING: |
| 386 |
bt_list_for_each_entry(iter, &node->u.string.expressions, siblings) { |
| 387 |
iter->parent = node; |
| 388 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 389 |
if (ret) |
| 390 |
return ret; |
| 391 |
} |
| 392 |
break; |
| 393 |
case NODE_ENUMERATOR: |
| 394 |
bt_list_for_each_entry(iter, &node->u.enumerator.values, siblings) { |
| 395 |
iter->parent = node; |
| 396 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 397 |
if (ret) |
| 398 |
return ret; |
| 399 |
} |
| 400 |
break; |
| 401 |
case NODE_ENUM: |
| 402 |
depth++; |
| 403 |
if (node->u._enum.container_type) { |
| 404 |
ret = ctf_visitor_parent_links(fd, depth + 1, node->u._enum.container_type); |
| 405 |
if (ret) |
| 406 |
return ret; |
| 407 |
} |
| 408 |
|
| 409 |
bt_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) { |
| 410 |
iter->parent = node; |
| 411 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 412 |
if (ret) |
| 413 |
return ret; |
| 414 |
} |
| 415 |
depth--; |
| 416 |
break; |
| 417 |
case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 418 |
node->u.struct_or_variant_declaration.type_specifier_list->parent = node; |
| 419 |
ret = ctf_visitor_parent_links(fd, depth + 1, |
| 420 |
node->u.struct_or_variant_declaration.type_specifier_list); |
| 421 |
if (ret) |
| 422 |
return ret; |
| 423 |
bt_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) { |
| 424 |
iter->parent = node; |
| 425 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 426 |
if (ret) |
| 427 |
return ret; |
| 428 |
} |
| 429 |
break; |
| 430 |
case NODE_VARIANT: |
| 431 |
bt_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) { |
| 432 |
iter->parent = node; |
| 433 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 434 |
if (ret) |
| 435 |
return ret; |
| 436 |
} |
| 437 |
break; |
| 438 |
case NODE_STRUCT: |
| 439 |
bt_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) { |
| 440 |
iter->parent = node; |
| 441 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 442 |
if (ret) |
| 443 |
return ret; |
| 444 |
} |
| 445 |
bt_list_for_each_entry(iter, &node->u._struct.min_align, |
| 446 |
siblings) { |
| 447 |
iter->parent = node; |
| 448 |
ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 449 |
if (ret) |
| 450 |
return ret; |
| 451 |
} |
| 452 |
break; |
| 453 |
|
| 454 |
case NODE_UNKNOWN: |
| 455 |
default: |
| 456 |
fprintf(fd, "[error] %s: unknown node type %d\n", __func__, |
| 457 |
(int) node->type); |
| 458 |
return -EINVAL; |
| 459 |
} |
| 460 |
return ret; |
| 461 |
} |