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