4 * Common Trace Format Metadata Visitor (XML dump).
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
27 #include <babeltrace/list.h>
28 #include "ctf-scanner.h"
29 #include "ctf-parser.h"
32 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
35 void print_tabs(FILE *fd
, int depth
)
39 for (i
= 0; i
< depth
; i
++)
44 int ctf_visitor_print_unary_expression(FILE *fd
, int depth
, struct ctf_node
*node
)
48 switch (node
->u
.unary_expression
.link
) {
49 case UNARY_LINK_UNKNOWN
:
52 print_tabs(fd
, depth
);
53 fprintf(fd
, "<dotlink/>\n");
56 print_tabs(fd
, depth
);
57 fprintf(fd
, "<arrowlink/>\n");
60 print_tabs(fd
, depth
);
61 fprintf(fd
, "<dotdotdot/>\n");
64 fprintf(stderr
, "[error] %s: unknown expression link type %d\n", __func__
,
65 (int) node
->u
.unary_expression
.link
);
69 switch (node
->u
.unary_expression
.type
) {
71 print_tabs(fd
, depth
);
72 fprintf(fd
, "<unary_expression value=");
73 fprintf(fd
, "\"%s\"", node
->u
.unary_expression
.u
.string
);
76 case UNARY_SIGNED_CONSTANT
:
77 print_tabs(fd
, depth
);
78 fprintf(fd
, "<unary_expression value=");
79 fprintf(fd
, "%" PRId64
, node
->u
.unary_expression
.u
.signed_constant
);
82 case UNARY_UNSIGNED_CONSTANT
:
83 print_tabs(fd
, depth
);
84 fprintf(fd
, "<unary_expression value=");
85 fprintf(fd
, "%" PRIu64
, node
->u
.unary_expression
.u
.signed_constant
);
89 print_tabs(fd
, depth
);
90 fprintf(fd
, "<unary_expression_sbrac>\n");
91 ret
= ctf_visitor_print_unary_expression(fd
, depth
+ 1,
92 node
->u
.unary_expression
.u
.sbrac_exp
);
95 print_tabs(fd
, depth
);
96 fprintf(fd
, "</unary_expression_sbrac>\n");
99 print_tabs(fd
, depth
);
100 fprintf(fd
, "<unary_expression_nested>\n");
101 ret
= ctf_visitor_print_unary_expression(fd
, depth
+ 1,
102 node
->u
.unary_expression
.u
.nested_exp
);
105 print_tabs(fd
, depth
);
106 fprintf(fd
, "</unary_expression_nested>\n");
111 fprintf(stderr
, "[error] %s: unknown expression type %d\n", __func__
,
112 (int) node
->u
.unary_expression
.type
);
119 int ctf_visitor_print_type_specifier(FILE *fd
, int depth
, struct ctf_node
*node
)
121 print_tabs(fd
, depth
);
122 fprintf(fd
, "<type_specifier \"");
124 switch (node
->u
.type_specifier
.type
) {
132 fprintf(fd
, "short");
141 fprintf(fd
, "float");
143 case TYPESPEC_DOUBLE
:
144 fprintf(fd
, "double");
146 case TYPESPEC_SIGNED
:
147 fprintf(fd
, "signed");
149 case TYPESPEC_UNSIGNED
:
150 fprintf(fd
, "unsigned");
155 case TYPESPEC_COMPLEX
:
156 fprintf(fd
, "_Complex");
158 case TYPESPEC_IMAGINARY
:
159 fprintf(fd
, "_Imaginary");
162 fprintf(fd
, "const");
164 case TYPESPEC_ID_TYPE
:
165 fprintf(fd
, "%s", node
->u
.type_specifier
.id_type
);
168 case TYPESPEC_UNKNOWN
:
170 fprintf(stderr
, "[error] %s: unknown type specifier %d\n", __func__
,
171 (int) node
->u
.type_specifier
.type
);
174 fprintf(fd
, "\"/>\n");
179 int ctf_visitor_print_type_declarator(FILE *fd
, int depth
, struct ctf_node
*node
)
182 struct ctf_node
*iter
;
184 print_tabs(fd
, depth
);
185 fprintf(fd
, "<type_declarator>\n");
188 if (!cds_list_empty(&node
->u
.type_declarator
.pointers
)) {
189 print_tabs(fd
, depth
);
190 fprintf(fd
, "<pointers>\n");
191 cds_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
,
193 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
197 print_tabs(fd
, depth
);
198 fprintf(fd
, "</pointers>\n");
201 switch (node
->u
.type_declarator
.type
) {
203 if (node
->u
.type_declarator
.u
.id
) {
204 print_tabs(fd
, depth
);
205 fprintf(fd
, "<id \"");
206 fprintf(fd
, "%s", node
->u
.type_declarator
.u
.id
);
207 fprintf(fd
, "\" />\n");
211 if (node
->u
.type_declarator
.u
.nested
.type_declarator
) {
212 print_tabs(fd
, depth
);
213 fprintf(fd
, "<type_declarator>\n");
214 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
215 node
->u
.type_declarator
.u
.nested
.type_declarator
);
218 print_tabs(fd
, depth
);
219 fprintf(fd
, "</type_declarator>\n");
221 if (node
->u
.type_declarator
.u
.nested
.length
) {
222 print_tabs(fd
, depth
);
223 fprintf(fd
, "<length>\n");
224 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
225 node
->u
.type_declarator
.u
.nested
.length
);
228 print_tabs(fd
, depth
);
229 fprintf(fd
, "</length>\n");
231 if (node
->u
.type_declarator
.u
.nested
.abstract_array
) {
232 print_tabs(fd
, depth
);
233 fprintf(fd
, "<length>\n");
234 print_tabs(fd
, depth
);
235 fprintf(fd
, "</length>\n");
237 if (node
->u
.type_declarator
.bitfield_len
) {
238 print_tabs(fd
, depth
);
239 fprintf(fd
, "<bitfield_len>\n");
240 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
241 node
->u
.type_declarator
.bitfield_len
);
244 print_tabs(fd
, depth
);
245 fprintf(fd
, "</bitfield_len>\n");
248 case TYPEDEC_UNKNOWN
:
250 fprintf(stderr
, "[error] %s: unknown type declarator %d\n", __func__
,
251 (int) node
->u
.type_declarator
.type
);
256 print_tabs(fd
, depth
);
257 fprintf(fd
, "</type_declarator>\n");
261 int ctf_visitor_print_xml(FILE *fd
, int depth
, struct ctf_node
*node
)
264 struct ctf_node
*iter
;
266 switch (node
->type
) {
268 print_tabs(fd
, depth
);
269 fprintf(fd
, "<root>\n");
270 cds_list_for_each_entry(iter
, &node
->u
.root
._typedef
,
272 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
276 cds_list_for_each_entry(iter
, &node
->u
.root
.typealias
,
278 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
282 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_specifier
, siblings
) {
283 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
287 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
288 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
292 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
293 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
297 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
298 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
302 print_tabs(fd
, depth
);
303 fprintf(fd
, "</root>\n");
307 print_tabs(fd
, depth
);
308 fprintf(fd
, "<event>\n");
309 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
310 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
314 print_tabs(fd
, depth
);
315 fprintf(fd
, "</event>\n");
318 print_tabs(fd
, depth
);
319 fprintf(fd
, "<stream>\n");
320 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
321 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
325 print_tabs(fd
, depth
);
326 fprintf(fd
, "</stream>\n");
329 print_tabs(fd
, depth
);
330 fprintf(fd
, "<trace>\n");
331 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
332 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
336 print_tabs(fd
, depth
);
337 fprintf(fd
, "</trace>\n");
340 case NODE_CTF_EXPRESSION
:
341 print_tabs(fd
, depth
);
342 fprintf(fd
, "<ctf_expression>\n");
344 print_tabs(fd
, depth
);
345 fprintf(fd
, "<left>\n");
346 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.left
, siblings
) {
347 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
352 print_tabs(fd
, depth
);
353 fprintf(fd
, "</left>\n");
355 print_tabs(fd
, depth
);
356 fprintf(fd
, "<right>\n");
357 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.right
, siblings
) {
358 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
362 print_tabs(fd
, depth
);
363 fprintf(fd
, "</right>\n");
365 print_tabs(fd
, depth
);
366 fprintf(fd
, "</ctf_expression>\n");
368 case NODE_UNARY_EXPRESSION
:
369 return ctf_visitor_print_unary_expression(fd
, depth
, node
);
372 print_tabs(fd
, depth
);
373 fprintf(fd
, "<typedef>\n");
375 print_tabs(fd
, depth
);
376 fprintf(fd
, "<declaration_specifier>\n");
377 cds_list_for_each_entry(iter
, &node
->u
._typedef
.declaration_specifier
, siblings
) {
378 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
382 print_tabs(fd
, depth
);
383 fprintf(fd
, "</declaration_specifier>\n");
385 print_tabs(fd
, depth
);
386 fprintf(fd
, "<type_declarators>\n");
387 cds_list_for_each_entry(iter
, &node
->u
._typedef
.type_declarators
, siblings
) {
388 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
392 print_tabs(fd
, depth
);
393 fprintf(fd
, "</type_declarators>\n");
395 print_tabs(fd
, depth
);
396 fprintf(fd
, "</typedef>\n");
398 case NODE_TYPEALIAS_TARGET
:
399 print_tabs(fd
, depth
);
400 fprintf(fd
, "<target>\n");
403 print_tabs(fd
, depth
);
404 fprintf(fd
, "<declaration_specifier>\n");
405 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.declaration_specifier
, siblings
) {
406 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
410 print_tabs(fd
, depth
);
411 fprintf(fd
, "</declaration_specifier>\n");
413 print_tabs(fd
, depth
);
414 fprintf(fd
, "<type_declarators>\n");
415 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.type_declarators
, siblings
) {
416 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
420 print_tabs(fd
, depth
);
421 fprintf(fd
, "</type_declarators>\n");
424 print_tabs(fd
, depth
);
425 fprintf(fd
, "</target>\n");
427 case NODE_TYPEALIAS_ALIAS
:
428 print_tabs(fd
, depth
);
429 fprintf(fd
, "<alias>\n");
432 print_tabs(fd
, depth
);
433 fprintf(fd
, "<declaration_specifier>\n");
434 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.declaration_specifier
, siblings
) {
435 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
439 print_tabs(fd
, depth
);
440 fprintf(fd
, "</declaration_specifier>\n");
442 print_tabs(fd
, depth
);
443 fprintf(fd
, "<type_declarators>\n");
444 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.type_declarators
, siblings
) {
445 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
449 print_tabs(fd
, depth
);
450 fprintf(fd
, "</type_declarators>\n");
453 print_tabs(fd
, depth
);
454 fprintf(fd
, "</alias>\n");
457 print_tabs(fd
, depth
);
458 fprintf(fd
, "<typealias>\n");
459 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
.typealias
.target
);
462 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
.typealias
.alias
);
465 print_tabs(fd
, depth
);
466 fprintf(fd
, "</typealias>\n");
469 case NODE_TYPE_SPECIFIER
:
470 ret
= ctf_visitor_print_type_specifier(fd
, depth
, node
);
475 print_tabs(fd
, depth
);
476 if (node
->u
.pointer
.const_qualifier
)
477 fprintf(fd
, "<const_pointer />\n");
479 fprintf(fd
, "<pointer />\n");
481 case NODE_TYPE_DECLARATOR
:
482 ret
= ctf_visitor_print_type_declarator(fd
, depth
, node
);
487 case NODE_FLOATING_POINT
:
488 print_tabs(fd
, depth
);
489 fprintf(fd
, "<floating_point>\n");
490 cds_list_for_each_entry(iter
, &node
->u
.floating_point
.expressions
, siblings
) {
491 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
495 print_tabs(fd
, depth
);
496 fprintf(fd
, "</floating_point>\n");
499 print_tabs(fd
, depth
);
500 fprintf(fd
, "<integer>\n");
501 cds_list_for_each_entry(iter
, &node
->u
.integer
.expressions
, siblings
) {
502 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
506 print_tabs(fd
, depth
);
507 fprintf(fd
, "</integer>\n");
510 print_tabs(fd
, depth
);
511 fprintf(fd
, "<string>\n");
512 cds_list_for_each_entry(iter
, &node
->u
.string
.expressions
, siblings
) {
513 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
517 print_tabs(fd
, depth
);
518 fprintf(fd
, "</string>\n");
520 case NODE_ENUMERATOR
:
521 print_tabs(fd
, depth
);
522 fprintf(fd
, "<enumerator");
523 if (node
->u
.enumerator
.id
)
524 fprintf(fd
, " id=\"%s\"", node
->u
.enumerator
.id
);
526 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
, siblings
) {
527 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
531 print_tabs(fd
, depth
);
532 fprintf(fd
, "</enumerator>\n");
535 print_tabs(fd
, depth
);
536 if (node
->u
._struct
.name
)
537 fprintf(fd
, "<enum name=\"%s\">\n",
538 node
->u
._enum
.enum_id
);
540 fprintf(fd
, "<enum >\n");
543 if (node
->u
._enum
.container_type
) {
544 print_tabs(fd
, depth
);
545 fprintf(fd
, "<container_type>\n");
546 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
._enum
.container_type
);
549 print_tabs(fd
, depth
);
550 fprintf(fd
, "</container_type>\n");
553 print_tabs(fd
, depth
);
554 fprintf(fd
, "<enumerator_list>\n");
555 cds_list_for_each_entry(iter
, &node
->u
._enum
.enumerator_list
, siblings
) {
556 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
560 print_tabs(fd
, depth
);
561 fprintf(fd
, "</enumerator_list>\n");
564 print_tabs(fd
, depth
);
565 fprintf(fd
, "</enum>\n");
567 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
568 print_tabs(fd
, depth
);
569 fprintf(fd
, "<declaration_specifier>\n");
570 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.declaration_specifier
, siblings
) {
571 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
575 print_tabs(fd
, depth
);
576 fprintf(fd
, "</declaration_specifier>\n");
578 print_tabs(fd
, depth
);
579 fprintf(fd
, "<type_declarators>\n");
580 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.type_declarators
, siblings
) {
581 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
585 print_tabs(fd
, depth
);
586 fprintf(fd
, "</type_declarators>\n");
589 print_tabs(fd
, depth
);
590 fprintf(fd
, "<variant");
591 if (node
->u
.variant
.name
)
592 fprintf(fd
, " name=\"%s\"", node
->u
.variant
.name
);
593 if (node
->u
.variant
.choice
)
594 fprintf(fd
, " choice=\"%s\"", node
->u
.variant
.choice
);
596 cds_list_for_each_entry(iter
, &node
->u
.variant
.declaration_list
, siblings
) {
597 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
601 print_tabs(fd
, depth
);
602 fprintf(fd
, "</variant>\n");
605 print_tabs(fd
, depth
);
606 if (node
->u
._struct
.name
)
607 fprintf(fd
, "<struct name=\"%s\">\n",
608 node
->u
._struct
.name
);
610 fprintf(fd
, "<struct>\n");
611 cds_list_for_each_entry(iter
, &node
->u
._struct
.declaration_list
, siblings
) {
612 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
616 print_tabs(fd
, depth
);
617 fprintf(fd
, "</struct>\n");
622 fprintf(stderr
, "[error] %s: unknown node type %d\n", __func__
,