2 * BabelTrace - Common Trace Format (CTF)
4 * CTF Text Format registration.
6 * Copyright 2010, 2011 - 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.
19 #include <babeltrace/format.h>
20 #include <babeltrace/ctf-text/types.h>
21 #include <babeltrace/ctf/metadata.h>
22 #include <babeltrace/babeltrace.h>
24 #include <uuid/uuid.h>
27 #include <sys/types.h>
35 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
);
36 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
39 rw_dispatch write_dispatch_table
[] = {
40 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
41 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
42 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
43 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
44 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
45 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
46 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
47 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
51 struct format ctf_text_format
= {
52 .open_trace
= ctf_text_open_trace
,
53 .close_trace
= ctf_text_close_trace
,
57 int ctf_text_write_event(struct stream_pos
*ppos
,
58 struct ctf_stream_class
*stream_class
)
60 struct ctf_text_stream_pos
*pos
=
61 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
62 struct ctf_event
*event_class
;
68 /* print event header */
69 if (stream_class
->event_header
) {
71 len_index
= struct_declaration_lookup_field_index(stream_class
->event_header_decl
,
72 g_quark_from_static_string("id"));
74 struct definition_integer
*defint
;
75 struct definition
*field
;
77 field
= struct_definition_get_field_from_index(stream_class
->event_header
, len_index
);
78 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
79 defint
= container_of(field
, struct definition_integer
, p
);
80 assert(defint
->declaration
->signedness
== FALSE
);
81 id
= defint
->value
._unsigned
; /* set id */
85 if (id
>= stream_class
->events_by_id
->len
) {
86 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
89 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
91 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
96 fprintf(pos
->fp
, ", ");
98 fprintf(pos
->fp
, "timestamp = ");
100 fprintf(pos
->fp
, "[");
101 fprintf(pos
->fp
, "%" PRIu64
, (uint64_t) 0); /* TODO */
102 if (!pos
->print_names
)
103 fprintf(pos
->fp
, "]");
106 fprintf(pos
->fp
, ", ");
107 if (pos
->print_names
)
108 fprintf(pos
->fp
, "name = ");
109 fprintf(pos
->fp
, "%s: ", g_quark_to_string(event_class
->name
));
111 if (stream_class
->event_header
) {
113 fprintf(pos
->fp
, ", ");
114 if (pos
->print_names
)
115 fprintf(pos
->fp
, "stream.event.header = ");
116 ret
= generic_rw(ppos
, &stream_class
->event_header
->p
);
121 /* print stream-declared event context */
122 if (stream_class
->event_context
) {
124 fprintf(pos
->fp
, ", ");
125 if (pos
->print_names
)
126 fprintf(pos
->fp
, "stream.event.context = ");
127 fprintf(pos
->fp
, " ");
128 ret
= generic_rw(ppos
, &stream_class
->event_context
->p
);
133 /* print event-declared event context */
134 if (event_class
->context
) {
136 fprintf(pos
->fp
, ", ");
137 if (pos
->print_names
)
138 fprintf(pos
->fp
, "event.context = ");
139 fprintf(pos
->fp
, " ");
140 ret
= generic_rw(ppos
, &event_class
->context
->p
);
145 /* Read and print event payload */
146 if (event_class
->fields
) {
148 fprintf(pos
->fp
, ", ");
149 if (pos
->print_names
)
150 fprintf(pos
->fp
, "event.fields = ");
151 fprintf(pos
->fp
, " ");
152 ret
= generic_rw(ppos
, &event_class
->fields
->p
);
156 fprintf(pos
->fp
, "\n");
161 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
166 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
)
168 struct ctf_text_stream_pos
*pos
;
171 pos
= g_new0(struct ctf_text_stream_pos
, 1);
173 switch (flags
& O_ACCMODE
) {
178 fp
= fopen(path
, "w");
182 pos
->parent
.rw_table
= write_dispatch_table
;
183 pos
->parent
.event_cb
= ctf_text_write_event
;
184 pos
->print_names
= opt_field_names
;
188 fprintf(stdout
, "[error] Incorrect open flags.\n");
192 return &pos
->trace_descriptor
;
198 void ctf_text_close_trace(struct trace_descriptor
*td
)
200 struct ctf_text_stream_pos
*pos
=
201 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
206 void __attribute__((constructor
)) ctf_text_init(void)
210 ctf_text_format
.name
= g_quark_from_static_string("text");
211 ret
= bt_register_format(&ctf_text_format
);