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