Privatize struct bt_trace descriptor, move trace path
[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>
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
47int 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
64enum field_item {
65 ITEM_SCOPE,
66 ITEM_HEADER,
67 ITEM_CONTEXT,
68 ITEM_PAYLOAD,
69};
b88d6e85 70
39535929
MD
71enum 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 89static
1b8455b7 90struct 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 93static
1b8455b7 94int ctf_text_close_trace(struct bt_trace_descriptor *descriptor);
642ec66d 95
642ec66d
MD
96static
97rw_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
108static
37b99bdb 109struct 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
114static 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,
118 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE;
119
120static
121void __attribute__((constructor)) init_quarks(void)
122{
123 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
124 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
125 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
126 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
127 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
128}
129
f133896d 130static
c5ff71a3 131struct ctf_callsite_dups *ctf_trace_callsite_lookup(struct ctf_trace *trace,
f133896d
MD
132 GQuark callsite_name)
133{
134 return g_hash_table_lookup(trace->callsites,
135 (gpointer) (unsigned long) callsite_name);
136}
137
0d69b916 138int print_field(struct bt_definition *definition)
d335f0f7
MD
139{
140 /* Print all fields in verbose mode */
141 if (babeltrace_verbose)
142 return 1;
143
144 /* Filter out part of the packet context */
145 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
146 return 0;
147 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
148 return 0;
149 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
150 return 0;
151 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
152 return 0;
153 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
154 return 0;
155
156 return 1;
157}
158
cba1661c
MD
159static
160void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item)
161{
162 switch (item) {
163 case ITEM_SCOPE:
164 if (opt_all_field_names || opt_scope_field_names)
165 pos->print_names = 1;
166 else
167 pos->print_names = 0;
168 break;
169 case ITEM_HEADER:
170 if (opt_all_field_names || opt_header_field_names)
171 pos->print_names = 1;
172 else
173 pos->print_names = 0;
174 break;
175 case ITEM_CONTEXT:
176 if (opt_all_field_names || opt_context_field_names)
177 pos->print_names = 1;
178 else
179 pos->print_names = 0;
180 break;
181 case ITEM_PAYLOAD:
182 if (opt_all_field_names || opt_payload_field_names)
183 pos->print_names = 1;
184 else
185 pos->print_names = 0;
186
187 break;
188 default:
189 assert(0);
190 }
191}
192
39535929
MD
193static
194const char *print_loglevel(int value)
195{
196 switch (value) {
197 case -1:
198 return "";
199 case BT_LOGLEVEL_EMERG:
200 return "TRACE_EMERG";
201 case BT_LOGLEVEL_ALERT:
202 return "TRACE_ALERT";
203 case BT_LOGLEVEL_CRIT:
204 return "TRACE_CRIT";
205 case BT_LOGLEVEL_ERR:
206 return "TRACE_ERR";
207 case BT_LOGLEVEL_WARNING:
208 return "TRACE_WARNING";
209 case BT_LOGLEVEL_NOTICE:
210 return "TRACE_NOTICE";
211 case BT_LOGLEVEL_INFO:
212 return "TRACE_INFO";
213 case BT_LOGLEVEL_DEBUG_SYSTEM:
214 return "TRACE_DEBUG_SYSTEM";
215 case BT_LOGLEVEL_DEBUG_PROGRAM:
216 return "TRACE_DEBUG_PROGRAM";
217 case BT_LOGLEVEL_DEBUG_PROCESS:
218 return "TRACE_DEBUG_PROCESS";
219 case BT_LOGLEVEL_DEBUG_MODULE:
220 return "TRACE_DEBUG_MODULE";
221 case BT_LOGLEVEL_DEBUG_UNIT:
222 return "TRACE_DEBUG_UNIT";
223 case BT_LOGLEVEL_DEBUG_FUNCTION:
224 return "TRACE_DEBUG_FUNCTION";
225 case BT_LOGLEVEL_DEBUG_LINE:
226 return "TRACE_DEBUG_LINE";
227 case BT_LOGLEVEL_DEBUG:
228 return "TRACE_DEBUG";
229 default:
230 return "<<UNKNOWN>>";
231 }
232}
233
31262354 234static
1cf393f6 235int ctf_text_write_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
e4195791 236
31262354
MD
237{
238 struct ctf_text_stream_pos *pos =
239 container_of(ppos, struct ctf_text_stream_pos, parent);
f380e105 240 struct ctf_stream_declaration *stream_class = stream->stream_class;
fd3382e8 241 int field_nr_saved;
4716614a 242 struct ctf_event_declaration *event_class;
c716f83b 243 struct ctf_event_definition *event;
c87a8eb2 244 uint64_t id;
31262354 245 int ret;
8c250d87 246 int dom_print = 0;
31262354 247
c87a8eb2 248 id = stream->event_id;
31262354
MD
249
250 if (id >= stream_class->events_by_id->len) {
3394d22e 251 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
252 return -EINVAL;
253 }
e28d4618
MD
254 event = g_ptr_array_index(stream->events_by_id, id);
255 if (!event) {
3394d22e 256 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
e28d4618
MD
257 return -EINVAL;
258 }
31262354 259 event_class = g_ptr_array_index(stream_class->events_by_id, id);
9ff2b8ad
FO
260 if (!event_class) {
261 fprintf(stderr, "[error] Event class id %" PRIu64 " is unknown.\n", id);
31262354
MD
262 return -EINVAL;
263 }
264
fca04958
MD
265 /* Print events discarded */
266 if (stream->events_discarded) {
7d97fad9 267 fflush(pos->fp);
c829a65c 268 ctf_print_discarded(stderr, stream, 0);
fca04958
MD
269 stream->events_discarded = 0;
270 }
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 = ");
345 }
1842a4c8 346 fprintf(pos->fp, "%s", stream_class->trace->env.domain);
8c250d87
MD
347 if (pos->print_names)
348 fprintf(pos->fp, ", ");
349 dom_print = 1;
350 }
c3815874
MD
351 if ((opt_trace_procname_field || opt_all_fields || opt_trace_default_fields)
352 && stream_class->trace->env.procname[0] != '\0') {
8c250d87
MD
353 set_field_names_print(pos, ITEM_HEADER);
354 if (pos->print_names) {
355 fprintf(pos->fp, "trace:procname = ");
356 } else if (dom_print) {
357 fprintf(pos->fp, ":");
358 }
1842a4c8 359 fprintf(pos->fp, "%s", stream_class->trace->env.procname);
8c250d87
MD
360 if (pos->print_names)
361 fprintf(pos->fp, ", ");
362 dom_print = 1;
363 }
c3815874
MD
364 if ((opt_trace_vpid_field || opt_all_fields || opt_trace_default_fields)
365 && stream_class->trace->env.vpid != -1) {
8c250d87
MD
366 set_field_names_print(pos, ITEM_HEADER);
367 if (pos->print_names) {
368 fprintf(pos->fp, "trace:vpid = ");
369 } else if (dom_print) {
370 fprintf(pos->fp, ":");
371 }
1842a4c8 372 fprintf(pos->fp, "%d", stream_class->trace->env.vpid);
8c250d87
MD
373 if (pos->print_names)
374 fprintf(pos->fp, ", ");
375 dom_print = 1;
376 }
306eeaa6 377 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) {
d86d62f8
MD
378 set_field_names_print(pos, ITEM_HEADER);
379 if (pos->print_names) {
380 fprintf(pos->fp, "loglevel = ");
381 } else if (dom_print) {
382 fprintf(pos->fp, ":");
383 }
39535929
MD
384 fprintf(pos->fp, "%s (%d)",
385 print_loglevel(event_class->loglevel),
306eeaa6 386 event_class->loglevel);
d86d62f8
MD
387 if (pos->print_names)
388 fprintf(pos->fp, ", ");
f6714e20
MD
389 dom_print = 1;
390 }
391 if ((opt_emf_field || opt_all_fields) && event_class->model_emf_uri) {
392 set_field_names_print(pos, ITEM_HEADER);
393 if (pos->print_names) {
394 fprintf(pos->fp, "model.emf.uri = ");
395 } else if (dom_print) {
396 fprintf(pos->fp, ":");
397 }
9b3c1d6f 398 fprintf(pos->fp, "\"%s\"",
f6714e20
MD
399 g_quark_to_string(event_class->model_emf_uri));
400 if (pos->print_names)
401 fprintf(pos->fp, ", ");
d86d62f8
MD
402 dom_print = 1;
403 }
f133896d 404 if ((opt_callsite_field || opt_all_fields)) {
c5ff71a3 405 struct ctf_callsite_dups *cs_dups;
f133896d
MD
406 struct ctf_callsite *callsite;
407
c5ff71a3 408 cs_dups = ctf_trace_callsite_lookup(stream_class->trace,
f133896d 409 event_class->name);
c5ff71a3
MD
410 if (cs_dups) {
411 int i = 0;
412
f133896d
MD
413 set_field_names_print(pos, ITEM_HEADER);
414 if (pos->print_names) {
415 fprintf(pos->fp, "callsite = ");
416 } else if (dom_print) {
417 fprintf(pos->fp, ":");
418 }
c5ff71a3
MD
419 fprintf(pos->fp, "[");
420 bt_list_for_each_entry(callsite, &cs_dups->head, node) {
421 if (i != 0)
422 fprintf(pos->fp, ",");
b448902b
MD
423 if (CTF_CALLSITE_FIELD_IS_SET(callsite, ip)) {
424 fprintf(pos->fp, "%s@0x%" PRIx64 ":%s:%" PRIu64 "",
425 callsite->func, callsite->ip, callsite->file,
426 callsite->line);
427 } else {
428 fprintf(pos->fp, "%s:%s:%" PRIu64 "",
429 callsite->func, callsite->file,
430 callsite->line);
431 }
c5ff71a3
MD
432 i++;
433 }
434 fprintf(pos->fp, "]");
f133896d
MD
435 if (pos->print_names)
436 fprintf(pos->fp, ", ");
437 dom_print = 1;
438 }
439 }
8c250d87
MD
440 if (dom_print && !pos->print_names)
441 fprintf(pos->fp, " ");
cba1661c 442 set_field_names_print(pos, ITEM_HEADER);
31262354
MD
443 if (pos->print_names)
444 fprintf(pos->fp, "name = ");
8178fe48
MD
445 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
446 if (pos->print_names)
fd3382e8 447 pos->field_nr++;
8178fe48 448 else
fd3382e8 449 fprintf(pos->fp, ":");
31262354 450
e28d4618
MD
451 /* print cpuid field from packet context */
452 if (stream->stream_packet_context) {
453 if (pos->field_nr++ != 0)
454 fprintf(pos->fp, ",");
cba1661c 455 set_field_names_print(pos, ITEM_SCOPE);
e28d4618
MD
456 if (pos->print_names)
457 fprintf(pos->fp, " stream.packet.context =");
458 field_nr_saved = pos->field_nr;
459 pos->field_nr = 0;
cba1661c 460 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618
MD
461 ret = generic_rw(ppos, &stream->stream_packet_context->p);
462 if (ret)
463 goto error;
464 pos->field_nr = field_nr_saved;
465 }
466
764af3f4 467 /* Only show the event header in verbose mode */
e28d4618 468 if (babeltrace_verbose && stream->stream_event_header) {
fd3382e8
MD
469 if (pos->field_nr++ != 0)
470 fprintf(pos->fp, ",");
cba1661c 471 set_field_names_print(pos, ITEM_SCOPE);
31262354 472 if (pos->print_names)
fd3382e8
MD
473 fprintf(pos->fp, " stream.event.header =");
474 field_nr_saved = pos->field_nr;
475 pos->field_nr = 0;
cba1661c 476 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 477 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
478 if (ret)
479 goto error;
fd3382e8 480 pos->field_nr = field_nr_saved;
31262354
MD
481 }
482
483 /* print stream-declared event context */
e28d4618 484 if (stream->stream_event_context) {
fd3382e8
MD
485 if (pos->field_nr++ != 0)
486 fprintf(pos->fp, ",");
cba1661c 487 set_field_names_print(pos, ITEM_SCOPE);
31262354 488 if (pos->print_names)
fd3382e8
MD
489 fprintf(pos->fp, " stream.event.context =");
490 field_nr_saved = pos->field_nr;
491 pos->field_nr = 0;
cba1661c 492 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 493 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
494 if (ret)
495 goto error;
fd3382e8 496 pos->field_nr = field_nr_saved;
31262354
MD
497 }
498
499 /* print event-declared event context */
e28d4618 500 if (event->event_context) {
fd3382e8
MD
501 if (pos->field_nr++ != 0)
502 fprintf(pos->fp, ",");
cba1661c 503 set_field_names_print(pos, ITEM_SCOPE);
31262354 504 if (pos->print_names)
fd3382e8
MD
505 fprintf(pos->fp, " event.context =");
506 field_nr_saved = pos->field_nr;
507 pos->field_nr = 0;
cba1661c 508 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 509 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
510 if (ret)
511 goto error;
fd3382e8 512 pos->field_nr = field_nr_saved;
31262354
MD
513 }
514
515 /* Read and print event payload */
e28d4618 516 if (event->event_fields) {
fd3382e8
MD
517 if (pos->field_nr++ != 0)
518 fprintf(pos->fp, ",");
cba1661c 519 set_field_names_print(pos, ITEM_SCOPE);
31262354 520 if (pos->print_names)
fd3382e8
MD
521 fprintf(pos->fp, " event.fields =");
522 field_nr_saved = pos->field_nr;
523 pos->field_nr = 0;
cba1661c 524 set_field_names_print(pos, ITEM_PAYLOAD);
e28d4618 525 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
526 if (ret)
527 goto error;
fd3382e8 528 pos->field_nr = field_nr_saved;
31262354 529 }
fd3382e8 530 /* newline */
31262354 531 fprintf(pos->fp, "\n");
fd3382e8 532 pos->field_nr = 0;
31262354
MD
533
534 return 0;
535
536error:
3394d22e 537 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
538 return ret;
539}
540
95febab3 541static
1b8455b7 542struct bt_trace_descriptor *ctf_text_open_trace(const char *path, int flags,
1cf393f6 543 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 544 int whence), FILE *metadata_fp)
642ec66d
MD
545{
546 struct ctf_text_stream_pos *pos;
547 FILE *fp;
548
549 pos = g_new0(struct ctf_text_stream_pos, 1);
550
03798a93
JD
551 pos->last_real_timestamp = -1ULL;
552 pos->last_cycles_timestamp = -1ULL;
642ec66d 553 switch (flags & O_ACCMODE) {
989c73bc 554 case O_RDWR:
b61922b5 555 if (!path)
6cf7957b
MD
556 fp = stdout;
557 else
558 fp = fopen(path, "w");
642ec66d
MD
559 if (!fp)
560 goto error;
561 pos->fp = fp;
562 pos->parent.rw_table = write_dispatch_table;
31262354 563 pos->parent.event_cb = ctf_text_write_event;
ca334c72 564 pos->parent.trace = &pos->trace_descriptor;
cba1661c 565 pos->print_names = 0;
642ec66d
MD
566 break;
567 case O_RDONLY:
568 default:
3394d22e 569 fprintf(stderr, "[error] Incorrect open flags.\n");
642ec66d
MD
570 goto error;
571 }
572
573 return &pos->trace_descriptor;
574error:
575 g_free(pos);
576 return NULL;
577}
578
95febab3 579static
1b8455b7 580int ctf_text_close_trace(struct bt_trace_descriptor *td)
642ec66d 581{
f824ae04 582 int ret;
642ec66d
MD
583 struct ctf_text_stream_pos *pos =
584 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
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 596static
642ec66d
MD
597void __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
606static
607void __attribute__((destructor)) ctf_text_exit(void)
608{
609 bt_unregister_format(&ctf_text_format);
610}
This page took 0.056938 seconds and 4 git commands to generate.