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