Commit | Line | Data |
---|---|---|
642ec66d MD |
1 | /* |
2 | * BabelTrace - Common Trace Format (CTF) | |
3 | * | |
4 | * CTF Text Format registration. | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
642ec66d MD |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
c462e188 MD |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
642ec66d MD |
27 | */ |
28 | ||
29 | #include <babeltrace/format.h> | |
30 | #include <babeltrace/ctf-text/types.h> | |
31262354 | 31 | #include <babeltrace/ctf/metadata.h> |
70bd0a12 | 32 | #include <babeltrace/babeltrace-internal.h> |
e4195791 | 33 | #include <babeltrace/ctf/events-internal.h> |
642ec66d | 34 | #include <inttypes.h> |
642ec66d MD |
35 | #include <sys/mman.h> |
36 | #include <errno.h> | |
37 | #include <sys/types.h> | |
38 | #include <sys/stat.h> | |
39 | #include <fcntl.h> | |
40 | #include <dirent.h> | |
41 | #include <glib.h> | |
42 | #include <unistd.h> | |
43 | #include <stdlib.h> | |
44 | ||
bfe74b8c MD |
45 | #define NSEC_PER_SEC 1000000000ULL |
46 | ||
cba1661c MD |
47 | int opt_all_field_names, |
48 | opt_scope_field_names, | |
49 | opt_header_field_names, | |
50 | opt_context_field_names, | |
82662ad4 | 51 | opt_payload_field_names, |
359d7456 MD |
52 | opt_all_fields, |
53 | opt_trace_field, | |
54 | opt_trace_domain_field, | |
55 | opt_trace_procname_field, | |
56 | opt_trace_vpid_field, | |
32cfb8ad | 57 | opt_trace_hostname_field, |
c3815874 | 58 | opt_trace_default_fields = 1, |
359d7456 | 59 | opt_loglevel_field, |
f6714e20 | 60 | opt_emf_field, |
f133896d | 61 | opt_callsite_field, |
7d97fad9 | 62 | opt_delta_field = 1; |
cba1661c MD |
63 | |
64 | enum field_item { | |
65 | ITEM_SCOPE, | |
66 | ITEM_HEADER, | |
67 | ITEM_CONTEXT, | |
68 | ITEM_PAYLOAD, | |
69 | }; | |
b88d6e85 | 70 | |
39535929 MD |
71 | enum bt_loglevel { |
72 | BT_LOGLEVEL_EMERG = 0, | |
73 | BT_LOGLEVEL_ALERT = 1, | |
74 | BT_LOGLEVEL_CRIT = 2, | |
75 | BT_LOGLEVEL_ERR = 3, | |
76 | BT_LOGLEVEL_WARNING = 4, | |
77 | BT_LOGLEVEL_NOTICE = 5, | |
78 | BT_LOGLEVEL_INFO = 6, | |
79 | BT_LOGLEVEL_DEBUG_SYSTEM = 7, | |
80 | BT_LOGLEVEL_DEBUG_PROGRAM = 8, | |
81 | BT_LOGLEVEL_DEBUG_PROCESS = 9, | |
82 | BT_LOGLEVEL_DEBUG_MODULE = 10, | |
83 | BT_LOGLEVEL_DEBUG_UNIT = 11, | |
84 | BT_LOGLEVEL_DEBUG_FUNCTION = 12, | |
85 | BT_LOGLEVEL_DEBUG_LINE = 13, | |
86 | BT_LOGLEVEL_DEBUG = 14, | |
87 | }; | |
88 | ||
95febab3 | 89 | static |
1b8455b7 | 90 | struct bt_trace_descriptor *ctf_text_open_trace(const char *path, int flags, |
1cf393f6 | 91 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
ae23d232 | 92 | int whence), FILE *metadata_fp); |
95febab3 | 93 | static |
1b8455b7 | 94 | int ctf_text_close_trace(struct bt_trace_descriptor *descriptor); |
642ec66d | 95 | |
642ec66d MD |
96 | static |
97 | rw_dispatch write_dispatch_table[] = { | |
98 | [ CTF_TYPE_INTEGER ] = ctf_text_integer_write, | |
99 | [ CTF_TYPE_FLOAT ] = ctf_text_float_write, | |
100 | [ CTF_TYPE_ENUM ] = ctf_text_enum_write, | |
101 | [ CTF_TYPE_STRING ] = ctf_text_string_write, | |
102 | [ CTF_TYPE_STRUCT ] = ctf_text_struct_write, | |
103 | [ CTF_TYPE_VARIANT ] = ctf_text_variant_write, | |
104 | [ CTF_TYPE_ARRAY ] = ctf_text_array_write, | |
105 | [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write, | |
106 | }; | |
107 | ||
108 | static | |
37b99bdb | 109 | struct bt_format ctf_text_format = { |
642ec66d MD |
110 | .open_trace = ctf_text_open_trace, |
111 | .close_trace = ctf_text_close_trace, | |
112 | }; | |
113 | ||
d335f0f7 MD |
114 | static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN, |
115 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END, | |
116 | Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED, | |
117 | Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE, | |
28ec1636 JD |
118 | Q_STREAM_PACKET_CONTEXT_PACKET_SIZE, |
119 | Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM; | |
d335f0f7 MD |
120 | |
121 | static | |
122 | void __attribute__((constructor)) init_quarks(void) | |
123 | { | |
124 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin"); | |
125 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end"); | |
126 | Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded"); | |
127 | Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size"); | |
128 | Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size"); | |
28ec1636 | 129 | Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM = g_quark_from_static_string("stream.packet.context.packet_seq_num"); |
d335f0f7 MD |
130 | } |
131 | ||
f133896d | 132 | static |
c5ff71a3 | 133 | struct ctf_callsite_dups *ctf_trace_callsite_lookup(struct ctf_trace *trace, |
f133896d MD |
134 | GQuark callsite_name) |
135 | { | |
136 | return g_hash_table_lookup(trace->callsites, | |
137 | (gpointer) (unsigned long) callsite_name); | |
138 | } | |
139 | ||
0d69b916 | 140 | int print_field(struct bt_definition *definition) |
d335f0f7 MD |
141 | { |
142 | /* Print all fields in verbose mode */ | |
143 | if (babeltrace_verbose) | |
144 | return 1; | |
145 | ||
146 | /* Filter out part of the packet context */ | |
147 | if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN) | |
148 | return 0; | |
149 | if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END) | |
150 | return 0; | |
151 | if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED) | |
152 | return 0; | |
153 | if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE) | |
154 | return 0; | |
155 | if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE) | |
156 | return 0; | |
28ec1636 JD |
157 | if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM) |
158 | return 0; | |
d335f0f7 MD |
159 | |
160 | return 1; | |
161 | } | |
162 | ||
cba1661c MD |
163 | static |
164 | void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item) | |
165 | { | |
166 | switch (item) { | |
167 | case ITEM_SCOPE: | |
168 | if (opt_all_field_names || opt_scope_field_names) | |
169 | pos->print_names = 1; | |
170 | else | |
171 | pos->print_names = 0; | |
172 | break; | |
173 | case ITEM_HEADER: | |
174 | if (opt_all_field_names || opt_header_field_names) | |
175 | pos->print_names = 1; | |
176 | else | |
177 | pos->print_names = 0; | |
178 | break; | |
179 | case ITEM_CONTEXT: | |
180 | if (opt_all_field_names || opt_context_field_names) | |
181 | pos->print_names = 1; | |
182 | else | |
183 | pos->print_names = 0; | |
184 | break; | |
185 | case ITEM_PAYLOAD: | |
186 | if (opt_all_field_names || opt_payload_field_names) | |
187 | pos->print_names = 1; | |
188 | else | |
189 | pos->print_names = 0; | |
190 | ||
191 | break; | |
192 | default: | |
193 | assert(0); | |
194 | } | |
195 | } | |
196 | ||
39535929 MD |
197 | static |
198 | const char *print_loglevel(int value) | |
199 | { | |
200 | switch (value) { | |
201 | case -1: | |
202 | return ""; | |
203 | case BT_LOGLEVEL_EMERG: | |
204 | return "TRACE_EMERG"; | |
205 | case BT_LOGLEVEL_ALERT: | |
206 | return "TRACE_ALERT"; | |
207 | case BT_LOGLEVEL_CRIT: | |
208 | return "TRACE_CRIT"; | |
209 | case BT_LOGLEVEL_ERR: | |
210 | return "TRACE_ERR"; | |
211 | case BT_LOGLEVEL_WARNING: | |
212 | return "TRACE_WARNING"; | |
213 | case BT_LOGLEVEL_NOTICE: | |
214 | return "TRACE_NOTICE"; | |
215 | case BT_LOGLEVEL_INFO: | |
216 | return "TRACE_INFO"; | |
217 | case BT_LOGLEVEL_DEBUG_SYSTEM: | |
218 | return "TRACE_DEBUG_SYSTEM"; | |
219 | case BT_LOGLEVEL_DEBUG_PROGRAM: | |
220 | return "TRACE_DEBUG_PROGRAM"; | |
221 | case BT_LOGLEVEL_DEBUG_PROCESS: | |
222 | return "TRACE_DEBUG_PROCESS"; | |
223 | case BT_LOGLEVEL_DEBUG_MODULE: | |
224 | return "TRACE_DEBUG_MODULE"; | |
225 | case BT_LOGLEVEL_DEBUG_UNIT: | |
226 | return "TRACE_DEBUG_UNIT"; | |
227 | case BT_LOGLEVEL_DEBUG_FUNCTION: | |
228 | return "TRACE_DEBUG_FUNCTION"; | |
229 | case BT_LOGLEVEL_DEBUG_LINE: | |
230 | return "TRACE_DEBUG_LINE"; | |
231 | case BT_LOGLEVEL_DEBUG: | |
232 | return "TRACE_DEBUG"; | |
233 | default: | |
234 | return "<<UNKNOWN>>"; | |
235 | } | |
236 | } | |
237 | ||
31262354 | 238 | static |
1cf393f6 | 239 | int ctf_text_write_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream) |
e4195791 | 240 | |
31262354 MD |
241 | { |
242 | struct ctf_text_stream_pos *pos = | |
243 | container_of(ppos, struct ctf_text_stream_pos, parent); | |
f380e105 | 244 | struct ctf_stream_declaration *stream_class = stream->stream_class; |
fd3382e8 | 245 | int field_nr_saved; |
4716614a | 246 | struct ctf_event_declaration *event_class; |
c716f83b | 247 | struct ctf_event_definition *event; |
c87a8eb2 | 248 | uint64_t id; |
31262354 | 249 | int ret; |
8c250d87 | 250 | int dom_print = 0; |
31262354 | 251 | |
c87a8eb2 | 252 | id = stream->event_id; |
31262354 MD |
253 | |
254 | if (id >= stream_class->events_by_id->len) { | |
3394d22e | 255 | fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id); |
31262354 MD |
256 | return -EINVAL; |
257 | } | |
e28d4618 MD |
258 | event = g_ptr_array_index(stream->events_by_id, id); |
259 | if (!event) { | |
3394d22e | 260 | fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id); |
e28d4618 MD |
261 | return -EINVAL; |
262 | } | |
31262354 | 263 | event_class = g_ptr_array_index(stream_class->events_by_id, id); |
9ff2b8ad FO |
264 | if (!event_class) { |
265 | fprintf(stderr, "[error] Event class id %" PRIu64 " is unknown.\n", id); | |
31262354 MD |
266 | return -EINVAL; |
267 | } | |
268 | ||
5e2eb0ae | 269 | if (stream->has_timestamp) { |
cba1661c | 270 | set_field_names_print(pos, ITEM_HEADER); |
be60c7fb MD |
271 | if (pos->print_names) |
272 | fprintf(pos->fp, "timestamp = "); | |
273 | else | |
274 | fprintf(pos->fp, "["); | |
03798a93 JD |
275 | if (opt_clock_cycles) { |
276 | ctf_print_timestamp(pos->fp, stream, stream->cycles_timestamp); | |
277 | } else { | |
278 | ctf_print_timestamp(pos->fp, stream, stream->real_timestamp); | |
279 | } | |
be60c7fb MD |
280 | if (!pos->print_names) |
281 | fprintf(pos->fp, "]"); | |
aa6bffae | 282 | |
be60c7fb MD |
283 | if (pos->print_names) |
284 | fprintf(pos->fp, ", "); | |
285 | else | |
286 | fprintf(pos->fp, " "); | |
287 | } | |
e4497aa0 | 288 | if (opt_delta_field && stream->has_timestamp) { |
8d8ed9af MD |
289 | uint64_t delta, delta_sec, delta_nsec; |
290 | ||
291 | set_field_names_print(pos, ITEM_HEADER); | |
292 | if (pos->print_names) | |
293 | fprintf(pos->fp, "delta = "); | |
294 | else | |
295 | fprintf(pos->fp, "("); | |
03798a93 JD |
296 | if (pos->last_real_timestamp != -1ULL) { |
297 | delta = stream->real_timestamp - pos->last_real_timestamp; | |
8d8ed9af MD |
298 | delta_sec = delta / NSEC_PER_SEC; |
299 | delta_nsec = delta % NSEC_PER_SEC; | |
300 | fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64, | |
301 | delta_sec, delta_nsec); | |
302 | } else { | |
303 | fprintf(pos->fp, "+?.?????????"); | |
304 | } | |
305 | if (!pos->print_names) | |
306 | fprintf(pos->fp, ")"); | |
307 | ||
308 | if (pos->print_names) | |
309 | fprintf(pos->fp, ", "); | |
310 | else | |
311 | fprintf(pos->fp, " "); | |
03798a93 JD |
312 | pos->last_real_timestamp = stream->real_timestamp; |
313 | pos->last_cycles_timestamp = stream->cycles_timestamp; | |
8d8ed9af MD |
314 | } |
315 | ||
caf929fa | 316 | if ((opt_trace_field || opt_all_fields) && stream_class->trace->parent.path[0] != '\0') { |
82662ad4 | 317 | set_field_names_print(pos, ITEM_HEADER); |
8c250d87 | 318 | if (pos->print_names) { |
359d7456 | 319 | fprintf(pos->fp, "trace = "); |
8c250d87 | 320 | } |
caf929fa | 321 | fprintf(pos->fp, "%s", stream_class->trace->parent.path); |
82662ad4 MD |
322 | if (pos->print_names) |
323 | fprintf(pos->fp, ", "); | |
324 | else | |
325 | fprintf(pos->fp, " "); | |
326 | } | |
c3815874 MD |
327 | if ((opt_trace_hostname_field || opt_all_fields || opt_trace_default_fields) |
328 | && stream_class->trace->env.hostname[0] != '\0') { | |
32cfb8ad MD |
329 | set_field_names_print(pos, ITEM_HEADER); |
330 | if (pos->print_names) { | |
331 | fprintf(pos->fp, "trace:hostname = "); | |
332 | } | |
333 | fprintf(pos->fp, "%s", stream_class->trace->env.hostname); | |
334 | if (pos->print_names) | |
335 | fprintf(pos->fp, ", "); | |
336 | dom_print = 1; | |
337 | } | |
c3815874 | 338 | if ((opt_trace_domain_field || opt_all_fields) && stream_class->trace->env.domain[0] != '\0') { |
8c250d87 MD |
339 | set_field_names_print(pos, ITEM_HEADER); |
340 | if (pos->print_names) { | |
341 | fprintf(pos->fp, "trace:domain = "); | |
342 | } | |
1842a4c8 | 343 | fprintf(pos->fp, "%s", stream_class->trace->env.domain); |
8c250d87 MD |
344 | if (pos->print_names) |
345 | fprintf(pos->fp, ", "); | |
346 | dom_print = 1; | |
347 | } | |
c3815874 MD |
348 | if ((opt_trace_procname_field || opt_all_fields || opt_trace_default_fields) |
349 | && stream_class->trace->env.procname[0] != '\0') { | |
8c250d87 MD |
350 | set_field_names_print(pos, ITEM_HEADER); |
351 | if (pos->print_names) { | |
352 | fprintf(pos->fp, "trace:procname = "); | |
353 | } else if (dom_print) { | |
354 | fprintf(pos->fp, ":"); | |
355 | } | |
1842a4c8 | 356 | fprintf(pos->fp, "%s", stream_class->trace->env.procname); |
8c250d87 MD |
357 | if (pos->print_names) |
358 | fprintf(pos->fp, ", "); | |
359 | dom_print = 1; | |
360 | } | |
c3815874 MD |
361 | if ((opt_trace_vpid_field || opt_all_fields || opt_trace_default_fields) |
362 | && stream_class->trace->env.vpid != -1) { | |
8c250d87 MD |
363 | set_field_names_print(pos, ITEM_HEADER); |
364 | if (pos->print_names) { | |
365 | fprintf(pos->fp, "trace:vpid = "); | |
366 | } else if (dom_print) { | |
367 | fprintf(pos->fp, ":"); | |
368 | } | |
1842a4c8 | 369 | fprintf(pos->fp, "%d", stream_class->trace->env.vpid); |
8c250d87 MD |
370 | if (pos->print_names) |
371 | fprintf(pos->fp, ", "); | |
372 | dom_print = 1; | |
373 | } | |
306eeaa6 | 374 | if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) { |
d86d62f8 MD |
375 | set_field_names_print(pos, ITEM_HEADER); |
376 | if (pos->print_names) { | |
377 | fprintf(pos->fp, "loglevel = "); | |
378 | } else if (dom_print) { | |
379 | fprintf(pos->fp, ":"); | |
380 | } | |
39535929 MD |
381 | fprintf(pos->fp, "%s (%d)", |
382 | print_loglevel(event_class->loglevel), | |
306eeaa6 | 383 | event_class->loglevel); |
d86d62f8 MD |
384 | if (pos->print_names) |
385 | fprintf(pos->fp, ", "); | |
f6714e20 MD |
386 | dom_print = 1; |
387 | } | |
388 | if ((opt_emf_field || opt_all_fields) && event_class->model_emf_uri) { | |
389 | set_field_names_print(pos, ITEM_HEADER); | |
390 | if (pos->print_names) { | |
391 | fprintf(pos->fp, "model.emf.uri = "); | |
392 | } else if (dom_print) { | |
393 | fprintf(pos->fp, ":"); | |
394 | } | |
9b3c1d6f | 395 | fprintf(pos->fp, "\"%s\"", |
f6714e20 MD |
396 | g_quark_to_string(event_class->model_emf_uri)); |
397 | if (pos->print_names) | |
398 | fprintf(pos->fp, ", "); | |
d86d62f8 MD |
399 | dom_print = 1; |
400 | } | |
f133896d | 401 | if ((opt_callsite_field || opt_all_fields)) { |
c5ff71a3 | 402 | struct ctf_callsite_dups *cs_dups; |
f133896d MD |
403 | struct ctf_callsite *callsite; |
404 | ||
c5ff71a3 | 405 | cs_dups = ctf_trace_callsite_lookup(stream_class->trace, |
f133896d | 406 | event_class->name); |
c5ff71a3 MD |
407 | if (cs_dups) { |
408 | int i = 0; | |
409 | ||
f133896d MD |
410 | set_field_names_print(pos, ITEM_HEADER); |
411 | if (pos->print_names) { | |
412 | fprintf(pos->fp, "callsite = "); | |
413 | } else if (dom_print) { | |
414 | fprintf(pos->fp, ":"); | |
415 | } | |
c5ff71a3 MD |
416 | fprintf(pos->fp, "["); |
417 | bt_list_for_each_entry(callsite, &cs_dups->head, node) { | |
418 | if (i != 0) | |
419 | fprintf(pos->fp, ","); | |
b448902b MD |
420 | if (CTF_CALLSITE_FIELD_IS_SET(callsite, ip)) { |
421 | fprintf(pos->fp, "%s@0x%" PRIx64 ":%s:%" PRIu64 "", | |
422 | callsite->func, callsite->ip, callsite->file, | |
423 | callsite->line); | |
424 | } else { | |
425 | fprintf(pos->fp, "%s:%s:%" PRIu64 "", | |
426 | callsite->func, callsite->file, | |
427 | callsite->line); | |
428 | } | |
c5ff71a3 MD |
429 | i++; |
430 | } | |
431 | fprintf(pos->fp, "]"); | |
f133896d MD |
432 | if (pos->print_names) |
433 | fprintf(pos->fp, ", "); | |
434 | dom_print = 1; | |
435 | } | |
436 | } | |
8c250d87 MD |
437 | if (dom_print && !pos->print_names) |
438 | fprintf(pos->fp, " "); | |
cba1661c | 439 | set_field_names_print(pos, ITEM_HEADER); |
31262354 MD |
440 | if (pos->print_names) |
441 | fprintf(pos->fp, "name = "); | |
8178fe48 MD |
442 | fprintf(pos->fp, "%s", g_quark_to_string(event_class->name)); |
443 | if (pos->print_names) | |
fd3382e8 | 444 | pos->field_nr++; |
8178fe48 | 445 | else |
fd3382e8 | 446 | fprintf(pos->fp, ":"); |
31262354 | 447 | |
e28d4618 MD |
448 | /* print cpuid field from packet context */ |
449 | if (stream->stream_packet_context) { | |
450 | if (pos->field_nr++ != 0) | |
451 | fprintf(pos->fp, ","); | |
cba1661c | 452 | set_field_names_print(pos, ITEM_SCOPE); |
e28d4618 MD |
453 | if (pos->print_names) |
454 | fprintf(pos->fp, " stream.packet.context ="); | |
455 | field_nr_saved = pos->field_nr; | |
456 | pos->field_nr = 0; | |
cba1661c | 457 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 MD |
458 | ret = generic_rw(ppos, &stream->stream_packet_context->p); |
459 | if (ret) | |
460 | goto error; | |
461 | pos->field_nr = field_nr_saved; | |
462 | } | |
463 | ||
764af3f4 | 464 | /* Only show the event header in verbose mode */ |
e28d4618 | 465 | if (babeltrace_verbose && stream->stream_event_header) { |
fd3382e8 MD |
466 | if (pos->field_nr++ != 0) |
467 | fprintf(pos->fp, ","); | |
cba1661c | 468 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 469 | if (pos->print_names) |
fd3382e8 MD |
470 | fprintf(pos->fp, " stream.event.header ="); |
471 | field_nr_saved = pos->field_nr; | |
472 | pos->field_nr = 0; | |
cba1661c | 473 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 474 | ret = generic_rw(ppos, &stream->stream_event_header->p); |
31262354 MD |
475 | if (ret) |
476 | goto error; | |
fd3382e8 | 477 | pos->field_nr = field_nr_saved; |
31262354 MD |
478 | } |
479 | ||
480 | /* print stream-declared event context */ | |
e28d4618 | 481 | if (stream->stream_event_context) { |
fd3382e8 MD |
482 | if (pos->field_nr++ != 0) |
483 | fprintf(pos->fp, ","); | |
cba1661c | 484 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 485 | if (pos->print_names) |
fd3382e8 MD |
486 | fprintf(pos->fp, " stream.event.context ="); |
487 | field_nr_saved = pos->field_nr; | |
488 | pos->field_nr = 0; | |
cba1661c | 489 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 490 | ret = generic_rw(ppos, &stream->stream_event_context->p); |
31262354 MD |
491 | if (ret) |
492 | goto error; | |
fd3382e8 | 493 | pos->field_nr = field_nr_saved; |
31262354 MD |
494 | } |
495 | ||
496 | /* print event-declared event context */ | |
e28d4618 | 497 | if (event->event_context) { |
fd3382e8 MD |
498 | if (pos->field_nr++ != 0) |
499 | fprintf(pos->fp, ","); | |
cba1661c | 500 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 501 | if (pos->print_names) |
fd3382e8 MD |
502 | fprintf(pos->fp, " event.context ="); |
503 | field_nr_saved = pos->field_nr; | |
504 | pos->field_nr = 0; | |
cba1661c | 505 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 506 | ret = generic_rw(ppos, &event->event_context->p); |
31262354 MD |
507 | if (ret) |
508 | goto error; | |
fd3382e8 | 509 | pos->field_nr = field_nr_saved; |
31262354 MD |
510 | } |
511 | ||
512 | /* Read and print event payload */ | |
e28d4618 | 513 | if (event->event_fields) { |
fd3382e8 MD |
514 | if (pos->field_nr++ != 0) |
515 | fprintf(pos->fp, ","); | |
cba1661c | 516 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 517 | if (pos->print_names) |
fd3382e8 MD |
518 | fprintf(pos->fp, " event.fields ="); |
519 | field_nr_saved = pos->field_nr; | |
520 | pos->field_nr = 0; | |
cba1661c | 521 | set_field_names_print(pos, ITEM_PAYLOAD); |
e28d4618 | 522 | ret = generic_rw(ppos, &event->event_fields->p); |
31262354 MD |
523 | if (ret) |
524 | goto error; | |
fd3382e8 | 525 | pos->field_nr = field_nr_saved; |
31262354 | 526 | } |
fd3382e8 | 527 | /* newline */ |
31262354 | 528 | fprintf(pos->fp, "\n"); |
fd3382e8 | 529 | pos->field_nr = 0; |
31262354 MD |
530 | |
531 | return 0; | |
532 | ||
533 | error: | |
3394d22e | 534 | fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n"); |
31262354 MD |
535 | return ret; |
536 | } | |
537 | ||
95febab3 | 538 | static |
1b8455b7 | 539 | struct bt_trace_descriptor *ctf_text_open_trace(const char *path, int flags, |
1cf393f6 | 540 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
ae23d232 | 541 | int whence), FILE *metadata_fp) |
642ec66d MD |
542 | { |
543 | struct ctf_text_stream_pos *pos; | |
544 | FILE *fp; | |
545 | ||
546 | pos = g_new0(struct ctf_text_stream_pos, 1); | |
547 | ||
03798a93 JD |
548 | pos->last_real_timestamp = -1ULL; |
549 | pos->last_cycles_timestamp = -1ULL; | |
642ec66d | 550 | switch (flags & O_ACCMODE) { |
989c73bc | 551 | case O_RDWR: |
b61922b5 | 552 | if (!path) |
6cf7957b MD |
553 | fp = stdout; |
554 | else | |
555 | fp = fopen(path, "w"); | |
642ec66d MD |
556 | if (!fp) |
557 | goto error; | |
558 | pos->fp = fp; | |
559 | pos->parent.rw_table = write_dispatch_table; | |
31262354 | 560 | pos->parent.event_cb = ctf_text_write_event; |
ca334c72 | 561 | pos->parent.trace = &pos->trace_descriptor; |
cba1661c | 562 | pos->print_names = 0; |
2654fe9b | 563 | babeltrace_ctf_console_output++; |
642ec66d MD |
564 | break; |
565 | case O_RDONLY: | |
566 | default: | |
3394d22e | 567 | fprintf(stderr, "[error] Incorrect open flags.\n"); |
642ec66d MD |
568 | goto error; |
569 | } | |
570 | ||
571 | return &pos->trace_descriptor; | |
572 | error: | |
573 | g_free(pos); | |
574 | return NULL; | |
575 | } | |
576 | ||
95febab3 | 577 | static |
1b8455b7 | 578 | int ctf_text_close_trace(struct bt_trace_descriptor *td) |
642ec66d | 579 | { |
f824ae04 | 580 | int ret; |
642ec66d MD |
581 | struct ctf_text_stream_pos *pos = |
582 | container_of(td, struct ctf_text_stream_pos, trace_descriptor); | |
2654fe9b MD |
583 | |
584 | babeltrace_ctf_console_output--; | |
9a3bb76a MD |
585 | if (pos->fp != stdout) { |
586 | ret = fclose(pos->fp); | |
587 | if (ret) { | |
588 | perror("Error on fclose"); | |
589 | return -1; | |
590 | } | |
f824ae04 | 591 | } |
642ec66d | 592 | g_free(pos); |
f824ae04 | 593 | return 0; |
642ec66d MD |
594 | } |
595 | ||
95febab3 | 596 | static |
642ec66d MD |
597 | void __attribute__((constructor)) ctf_text_init(void) |
598 | { | |
599 | int ret; | |
600 | ||
601 | ctf_text_format.name = g_quark_from_static_string("text"); | |
602 | ret = bt_register_format(&ctf_text_format); | |
603 | assert(!ret); | |
604 | } | |
605 | ||
95febab3 MD |
606 | static |
607 | void __attribute__((destructor)) ctf_text_exit(void) | |
608 | { | |
609 | bt_unregister_format(&ctf_text_format); | |
610 | } |