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 printf_dbg(fmt, args...) fprintf(stderr, "%s: " fmt, __func__, ## args)
34 static void print_tabs(FILE *fd
, int depth
)
38 for (i
= 0; i
< depth
; i
++)
42 int ctf_visitor_print_unary_expression(FILE *fd
, int depth
, struct ctf_node
*node
)
46 switch (node
->u
.unary_expression
.link
) {
47 case UNARY_LINK_UNKNOWN
:
50 print_tabs(fd
, depth
);
51 fprintf(fd
, "<dotlink/>\n");
54 print_tabs(fd
, depth
);
55 fprintf(fd
, "<arrowlink/>\n");
58 print_tabs(fd
, depth
);
59 fprintf(fd
, "<dotdotdot/>\n");
62 fprintf(stderr
, "[error] %s: unknown expression link type %d\n", __func__
,
63 (int) node
->u
.unary_expression
.link
);
67 switch (node
->u
.unary_expression
.type
) {
69 print_tabs(fd
, depth
);
70 fprintf(fd
, "<unary_expression value=");
71 fprintf(fd
, "\"%s\"", node
->u
.unary_expression
.u
.string
);
74 case UNARY_SIGNED_CONSTANT
:
75 print_tabs(fd
, depth
);
76 fprintf(fd
, "<unary_expression value=");
77 fprintf(fd
, "%" PRId64
, node
->u
.unary_expression
.u
.signed_constant
);
80 case UNARY_UNSIGNED_CONSTANT
:
81 print_tabs(fd
, depth
);
82 fprintf(fd
, "<unary_expression value=");
83 fprintf(fd
, "%" PRIu64
, node
->u
.unary_expression
.u
.signed_constant
);
87 print_tabs(fd
, depth
);
88 fprintf(fd
, "<unary_expression_sbrac>");
89 ret
= ctf_visitor_print_unary_expression(fd
, depth
+ 1,
90 node
->u
.unary_expression
.u
.sbrac_exp
);
93 print_tabs(fd
, depth
);
94 fprintf(fd
, "</unary_expression_sbrac>");
97 print_tabs(fd
, depth
);
98 fprintf(fd
, "<unary_expression_nested>");
99 ret
= ctf_visitor_print_unary_expression(fd
, depth
+ 1,
100 node
->u
.unary_expression
.u
.nested_exp
);
103 print_tabs(fd
, depth
);
104 fprintf(fd
, "</unary_expression_nested>");
109 fprintf(stderr
, "[error] %s: unknown expression type %d\n", __func__
,
110 (int) node
->u
.unary_expression
.type
);
116 int ctf_visitor_print_type_specifier(FILE *fd
, int depth
, struct ctf_node
*node
)
118 print_tabs(fd
, depth
);
119 fprintf(fd
, "<type_specifier \"");
121 switch (node
->u
.type_specifier
.type
) {
129 fprintf(fd
, "short");
138 fprintf(fd
, "float");
140 case TYPESPEC_DOUBLE
:
141 fprintf(fd
, "double");
143 case TYPESPEC_SIGNED
:
144 fprintf(fd
, "signed");
146 case TYPESPEC_UNSIGNED
:
147 fprintf(fd
, "unsigned");
152 case TYPESPEC_COMPLEX
:
153 fprintf(fd
, "complex");
156 fprintf(fd
, "const");
158 case TYPESPEC_ID_TYPE
:
159 fprintf(fd
, "%s", node
->u
.type_specifier
.id_type
);
162 case TYPESPEC_UNKNOWN
:
164 fprintf(stderr
, "[error] %s: unknown type specifier %d\n", __func__
,
165 (int) node
->u
.type_specifier
.type
);
168 fprintf(fd
, "\"/>\n");
172 int ctf_visitor_print_type_declarator(FILE *fd
, int depth
, struct ctf_node
*node
)
175 struct ctf_node
*iter
;
177 print_tabs(fd
, depth
);
178 fprintf(fd
, "<type_declarator>\n");
181 if (!cds_list_empty(&node
->u
.type_declarator
.pointers
)) {
182 print_tabs(fd
, depth
);
183 fprintf(fd
, "<pointers>\n");
184 cds_list_for_each_entry(iter
, &node
->u
.type_declarator
.pointers
,
186 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
190 print_tabs(fd
, depth
);
191 fprintf(fd
, "</pointers>\n");
194 switch (node
->u
.type_declarator
.type
) {
196 if (node
->u
.type_declarator
.u
.id
) {
197 print_tabs(fd
, depth
);
198 fprintf(fd
, "<id \"");
199 fprintf(fd
, "%s", node
->u
.type_declarator
.u
.id
);
200 fprintf(fd
, "\" />\n");
204 if (node
->u
.type_declarator
.u
.nested
.type_declarator
) {
205 print_tabs(fd
, depth
);
206 fprintf(fd
, "<type_declarator>\n");
207 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
208 node
->u
.type_declarator
.u
.nested
.type_declarator
);
211 print_tabs(fd
, depth
);
212 fprintf(fd
, "</type_declarator>\n");
214 if (node
->u
.type_declarator
.u
.nested
.length
) {
215 print_tabs(fd
, depth
);
216 fprintf(fd
, "<length>\n");
217 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
218 node
->u
.type_declarator
.u
.nested
.length
);
221 print_tabs(fd
, depth
);
222 fprintf(fd
, "</length>\n");
224 if (node
->u
.type_declarator
.u
.nested
.abstract_array
) {
225 print_tabs(fd
, depth
);
226 fprintf(fd
, "<length>\n");
227 print_tabs(fd
, depth
);
228 fprintf(fd
, "</length>\n");
230 if (node
->u
.type_declarator
.bitfield_len
) {
231 print_tabs(fd
, depth
);
232 fprintf(fd
, "<bitfield_len>\n");
233 ret
= ctf_visitor_print_xml(fd
, depth
+ 1,
234 node
->u
.type_declarator
.bitfield_len
);
237 print_tabs(fd
, depth
);
238 fprintf(fd
, "</bitfield_len>\n");
241 case TYPEDEC_UNKNOWN
:
243 fprintf(stderr
, "[error] %s: unknown type declarator %d\n", __func__
,
244 (int) node
->u
.type_declarator
.type
);
249 print_tabs(fd
, depth
);
250 fprintf(fd
, "</type_declarator>\n");
254 int ctf_visitor_print_xml(FILE *fd
, int depth
, struct ctf_node
*node
)
257 struct ctf_node
*iter
;
259 switch (node
->type
) {
261 print_tabs(fd
, depth
);
262 fprintf(fd
, "<root>\n");
263 cds_list_for_each_entry(iter
, &node
->u
.root
._typedef
,
265 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
269 cds_list_for_each_entry(iter
, &node
->u
.root
.typealias
,
271 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
275 cds_list_for_each_entry(iter
, &node
->u
.root
.declaration_specifier
, siblings
) {
276 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
280 cds_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
281 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
285 cds_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
286 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
290 cds_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
291 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
295 print_tabs(fd
, depth
);
296 fprintf(fd
, "</root>\n");
300 print_tabs(fd
, depth
);
301 fprintf(fd
, "<event>\n");
302 cds_list_for_each_entry(iter
, &node
->u
.event
.declaration_list
, siblings
) {
303 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
307 print_tabs(fd
, depth
);
308 fprintf(fd
, "</event>\n");
311 print_tabs(fd
, depth
);
312 fprintf(fd
, "<stream>\n");
313 cds_list_for_each_entry(iter
, &node
->u
.stream
.declaration_list
, siblings
) {
314 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
318 print_tabs(fd
, depth
);
319 fprintf(fd
, "</stream>\n");
322 print_tabs(fd
, depth
);
323 fprintf(fd
, "<trace>\n");
324 cds_list_for_each_entry(iter
, &node
->u
.trace
.declaration_list
, siblings
) {
325 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
329 print_tabs(fd
, depth
);
330 fprintf(fd
, "</trace>\n");
333 case NODE_CTF_EXPRESSION
:
334 print_tabs(fd
, depth
);
335 fprintf(fd
, "<ctf_expression>\n");
337 print_tabs(fd
, depth
);
338 fprintf(fd
, "<left>\n");
339 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.left
, siblings
) {
340 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
345 print_tabs(fd
, depth
);
346 fprintf(fd
, "</left>\n");
348 print_tabs(fd
, depth
);
349 fprintf(fd
, "<right>\n");
350 cds_list_for_each_entry(iter
, &node
->u
.ctf_expression
.right
, siblings
) {
351 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
355 print_tabs(fd
, depth
);
356 fprintf(fd
, "</right>\n");
358 print_tabs(fd
, depth
);
359 fprintf(fd
, "</ctf_expression>\n");
361 case NODE_UNARY_EXPRESSION
:
362 return ctf_visitor_print_unary_expression(fd
, depth
, node
);
365 print_tabs(fd
, depth
);
366 fprintf(fd
, "<typedef>\n");
368 print_tabs(fd
, depth
);
369 fprintf(fd
, "<declaration_specifier>\n");
370 cds_list_for_each_entry(iter
, &node
->u
._typedef
.declaration_specifier
, siblings
) {
371 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
375 print_tabs(fd
, depth
);
376 fprintf(fd
, "</declaration_specifier>\n");
378 print_tabs(fd
, depth
);
379 fprintf(fd
, "<type_declarators>\n");
380 cds_list_for_each_entry(iter
, &node
->u
._typedef
.type_declarators
, siblings
) {
381 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
385 print_tabs(fd
, depth
);
386 fprintf(fd
, "</type_declarators>\n");
388 print_tabs(fd
, depth
);
389 fprintf(fd
, "</typedef>\n");
391 case NODE_TYPEALIAS_TARGET
:
392 print_tabs(fd
, depth
);
393 fprintf(fd
, "<target>\n");
396 print_tabs(fd
, depth
);
397 fprintf(fd
, "<declaration_specifier>\n");
398 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.declaration_specifier
, siblings
) {
399 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
403 print_tabs(fd
, depth
);
404 fprintf(fd
, "</declaration_specifier>\n");
406 print_tabs(fd
, depth
);
407 fprintf(fd
, "<type_declarators>\n");
408 cds_list_for_each_entry(iter
, &node
->u
.typealias_target
.type_declarators
, siblings
) {
409 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
413 print_tabs(fd
, depth
);
414 fprintf(fd
, "</type_declarators>\n");
417 print_tabs(fd
, depth
);
418 fprintf(fd
, "</target>\n");
420 case NODE_TYPEALIAS_ALIAS
:
421 print_tabs(fd
, depth
);
422 fprintf(fd
, "<alias>\n");
425 print_tabs(fd
, depth
);
426 fprintf(fd
, "<declaration_specifier>\n");
427 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.declaration_specifier
, siblings
) {
428 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
432 print_tabs(fd
, depth
);
433 fprintf(fd
, "</declaration_specifier>\n");
435 print_tabs(fd
, depth
);
436 fprintf(fd
, "<type_declarators>\n");
437 cds_list_for_each_entry(iter
, &node
->u
.typealias_alias
.type_declarators
, siblings
) {
438 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
442 print_tabs(fd
, depth
);
443 fprintf(fd
, "</type_declarators>\n");
446 print_tabs(fd
, depth
);
447 fprintf(fd
, "</alias>\n");
450 print_tabs(fd
, depth
);
451 fprintf(fd
, "<typealias>\n");
452 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
.typealias
.target
);
455 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
.typealias
.alias
);
458 print_tabs(fd
, depth
);
459 fprintf(fd
, "</typealias>\n");
462 case NODE_TYPE_SPECIFIER
:
463 ret
= ctf_visitor_print_type_specifier(fd
, depth
, node
);
468 print_tabs(fd
, depth
);
469 if (node
->u
.pointer
.const_qualifier
)
470 fprintf(fd
, "<const_pointer />\n");
472 fprintf(fd
, "<pointer />\n");
474 case NODE_TYPE_DECLARATOR
:
475 ret
= ctf_visitor_print_type_declarator(fd
, depth
, node
);
480 case NODE_FLOATING_POINT
:
481 print_tabs(fd
, depth
);
482 fprintf(fd
, "<floating_point>\n");
483 cds_list_for_each_entry(iter
, &node
->u
.floating_point
.expressions
, siblings
) {
484 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
488 print_tabs(fd
, depth
);
489 fprintf(fd
, "</floating_point>\n");
492 print_tabs(fd
, depth
);
493 fprintf(fd
, "<integer>\n");
494 cds_list_for_each_entry(iter
, &node
->u
.integer
.expressions
, siblings
) {
495 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
499 print_tabs(fd
, depth
);
500 fprintf(fd
, "</integer>\n");
503 print_tabs(fd
, depth
);
504 fprintf(fd
, "<string>\n");
505 cds_list_for_each_entry(iter
, &node
->u
.string
.expressions
, siblings
) {
506 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
510 print_tabs(fd
, depth
);
511 fprintf(fd
, "</string>\n");
513 case NODE_ENUMERATOR
:
514 print_tabs(fd
, depth
);
515 fprintf(fd
, "<enumerator");
516 if (node
->u
.enumerator
.id
)
517 fprintf(fd
, " id=\"%s\"", node
->u
.enumerator
.id
);
519 cds_list_for_each_entry(iter
, &node
->u
.enumerator
.values
, siblings
) {
520 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
524 print_tabs(fd
, depth
);
525 fprintf(fd
, "</enumerator>\n");
528 print_tabs(fd
, depth
);
529 if (node
->u
._struct
.name
)
530 fprintf(fd
, "<enum name=\"%s\">\n",
531 node
->u
._enum
.enum_id
);
533 fprintf(fd
, "<enum >\n");
536 if (node
->u
._enum
.container_type
) {
537 print_tabs(fd
, depth
);
538 fprintf(fd
, "<container_type>\n");
539 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, node
->u
._enum
.container_type
);
542 print_tabs(fd
, depth
);
543 fprintf(fd
, "</container_type>\n");
546 print_tabs(fd
, depth
);
547 fprintf(fd
, "<enumerator_list>\n");
548 cds_list_for_each_entry(iter
, &node
->u
._enum
.enumerator_list
, siblings
) {
549 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
553 print_tabs(fd
, depth
);
554 fprintf(fd
, "</enumerator_list>\n");
557 print_tabs(fd
, depth
);
558 fprintf(fd
, "</enum>\n");
560 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
561 print_tabs(fd
, depth
);
562 fprintf(fd
, "<declaration_specifier>\n");
563 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.declaration_specifier
, siblings
) {
564 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
568 print_tabs(fd
, depth
);
569 fprintf(fd
, "</declaration_specifier>\n");
571 print_tabs(fd
, depth
);
572 fprintf(fd
, "<type_declarators>\n");
573 cds_list_for_each_entry(iter
, &node
->u
.struct_or_variant_declaration
.type_declarators
, siblings
) {
574 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
578 print_tabs(fd
, depth
);
579 fprintf(fd
, "</type_declarators>\n");
582 print_tabs(fd
, depth
);
583 fprintf(fd
, "<variant");
584 if (node
->u
.variant
.name
)
585 fprintf(fd
, " name=\"%s\"", node
->u
.variant
.name
);
586 if (node
->u
.variant
.choice
)
587 fprintf(fd
, " choice=\"%s\"", node
->u
.variant
.choice
);
589 cds_list_for_each_entry(iter
, &node
->u
.variant
.declaration_list
, siblings
) {
590 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
594 print_tabs(fd
, depth
);
595 fprintf(fd
, "</variant>\n");
598 print_tabs(fd
, depth
);
599 if (node
->u
._struct
.name
)
600 fprintf(fd
, "<struct name=\"%s\">\n",
601 node
->u
._struct
.name
);
603 fprintf(fd
, "<struct>\n");
604 cds_list_for_each_entry(iter
, &node
->u
._struct
.declaration_list
, siblings
) {
605 ret
= ctf_visitor_print_xml(fd
, depth
+ 1, iter
);
609 print_tabs(fd
, depth
);
610 fprintf(fd
, "</struct>\n");
615 fprintf(stderr
, "[error] %s: unknown node type %d\n", __func__
,