2 * BabelTrace - Common Trace Format (CTF)
4 * CTF Text Format registration.
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/format.h>
30 #include <babeltrace/ctf-text/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/ctf/events-internal.h>
37 #include <sys/types.h>
45 #define NSEC_PER_SEC 1000000000LL
47 int opt_all_field_names
,
48 opt_scope_field_names
,
49 opt_header_field_names
,
50 opt_context_field_names
,
51 opt_payload_field_names
,
54 opt_trace_domain_field
,
55 opt_trace_procname_field
,
57 opt_trace_hostname_field
,
58 opt_trace_default_fields
= 1,
72 BT_LOGLEVEL_EMERG
= 0,
73 BT_LOGLEVEL_ALERT
= 1,
76 BT_LOGLEVEL_WARNING
= 4,
77 BT_LOGLEVEL_NOTICE
= 5,
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,
90 struct bt_trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
91 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
92 int whence
), FILE *metadata_fp
);
94 int ctf_text_close_trace(struct bt_trace_descriptor
*descriptor
);
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
,
109 struct bt_format ctf_text_format
= {
110 .open_trace
= ctf_text_open_trace
,
111 .close_trace
= ctf_text_close_trace
,
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
,
118 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
,
119 Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM
;
122 void __attribute__((constructor
)) init_quarks(void)
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");
129 Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM
= g_quark_from_static_string("stream.packet.context.packet_seq_num");
133 struct ctf_callsite_dups
*ctf_trace_callsite_lookup(struct ctf_trace
*trace
,
134 GQuark callsite_name
)
136 return g_hash_table_lookup(trace
->callsites
,
137 (gpointer
) (unsigned long) callsite_name
);
140 int print_field(struct bt_definition
*definition
)
142 /* Print all fields in verbose mode */
143 if (babeltrace_verbose
)
146 /* Filter out part of the packet context */
147 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
149 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
151 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
153 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
155 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
157 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM
)
164 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
168 if (opt_all_field_names
|| opt_scope_field_names
)
169 pos
->print_names
= 1;
171 pos
->print_names
= 0;
174 if (opt_all_field_names
|| opt_header_field_names
)
175 pos
->print_names
= 1;
177 pos
->print_names
= 0;
180 if (opt_all_field_names
|| opt_context_field_names
)
181 pos
->print_names
= 1;
183 pos
->print_names
= 0;
186 if (opt_all_field_names
|| opt_payload_field_names
)
187 pos
->print_names
= 1;
189 pos
->print_names
= 0;
198 const char *print_loglevel(int value
)
203 case BT_LOGLEVEL_EMERG
:
204 return "TRACE_EMERG";
205 case BT_LOGLEVEL_ALERT
:
206 return "TRACE_ALERT";
207 case BT_LOGLEVEL_CRIT
:
209 case BT_LOGLEVEL_ERR
:
211 case BT_LOGLEVEL_WARNING
:
212 return "TRACE_WARNING";
213 case BT_LOGLEVEL_NOTICE
:
214 return "TRACE_NOTICE";
215 case BT_LOGLEVEL_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";
234 return "<<UNKNOWN>>";
239 int ctf_text_write_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
242 struct ctf_text_stream_pos
*pos
=
243 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
244 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
246 struct ctf_event_declaration
*event_class
;
247 struct ctf_event_definition
*event
;
252 id
= stream
->event_id
;
254 if (id
>= stream_class
->events_by_id
->len
) {
255 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
258 event
= g_ptr_array_index(stream
->events_by_id
, id
);
260 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
263 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
265 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
269 if (stream
->has_timestamp
) {
270 set_field_names_print(pos
, ITEM_HEADER
);
271 if (pos
->print_names
)
272 fprintf(pos
->fp
, "timestamp = ");
274 fprintf(pos
->fp
, "[");
275 if (opt_clock_cycles
) {
276 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
278 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
280 if (!pos
->print_names
)
281 fprintf(pos
->fp
, "]");
283 if (pos
->print_names
)
284 fprintf(pos
->fp
, ", ");
286 fprintf(pos
->fp
, " ");
288 if (opt_delta_field
&& stream
->has_timestamp
) {
289 uint64_t delta
, delta_sec
, delta_nsec
;
291 set_field_names_print(pos
, ITEM_HEADER
);
292 if (pos
->print_names
)
293 fprintf(pos
->fp
, "delta = ");
295 fprintf(pos
->fp
, "(");
296 if (pos
->last_real_timestamp
!= -1ULL) {
297 delta
= stream
->real_timestamp
- pos
->last_real_timestamp
;
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
);
303 fprintf(pos
->fp
, "+?.?????????");
305 if (!pos
->print_names
)
306 fprintf(pos
->fp
, ")");
308 if (pos
->print_names
)
309 fprintf(pos
->fp
, ", ");
311 fprintf(pos
->fp
, " ");
312 pos
->last_real_timestamp
= stream
->real_timestamp
;
313 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
316 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->parent
.path
[0] != '\0') {
317 set_field_names_print(pos
, ITEM_HEADER
);
318 if (pos
->print_names
) {
319 fprintf(pos
->fp
, "trace = ");
321 fprintf(pos
->fp
, "%s", stream_class
->trace
->parent
.path
);
322 if (pos
->print_names
)
323 fprintf(pos
->fp
, ", ");
325 fprintf(pos
->fp
, " ");
327 if ((opt_trace_hostname_field
|| opt_all_fields
|| opt_trace_default_fields
)
328 && stream_class
->trace
->env
.hostname
[0] != '\0') {
329 set_field_names_print(pos
, ITEM_HEADER
);
330 if (pos
->print_names
) {
331 fprintf(pos
->fp
, "trace:hostname = ");
333 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
334 if (pos
->print_names
)
335 fprintf(pos
->fp
, ", ");
338 if ((opt_trace_domain_field
|| opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
339 set_field_names_print(pos
, ITEM_HEADER
);
340 if (pos
->print_names
) {
341 fprintf(pos
->fp
, "trace:domain = ");
343 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
344 if (pos
->print_names
)
345 fprintf(pos
->fp
, ", ");
348 if ((opt_trace_procname_field
|| opt_all_fields
|| opt_trace_default_fields
)
349 && stream_class
->trace
->env
.procname
[0] != '\0') {
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
, ":");
356 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
357 if (pos
->print_names
)
358 fprintf(pos
->fp
, ", ");
361 if ((opt_trace_vpid_field
|| opt_all_fields
|| opt_trace_default_fields
)
362 && stream_class
->trace
->env
.vpid
!= -1) {
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
, ":");
369 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
370 if (pos
->print_names
)
371 fprintf(pos
->fp
, ", ");
374 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
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
, ":");
381 fprintf(pos
->fp
, "%s (%d)",
382 print_loglevel(event_class
->loglevel
),
383 event_class
->loglevel
);
384 if (pos
->print_names
)
385 fprintf(pos
->fp
, ", ");
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
, ":");
395 fprintf(pos
->fp
, "\"%s\"",
396 g_quark_to_string(event_class
->model_emf_uri
));
397 if (pos
->print_names
)
398 fprintf(pos
->fp
, ", ");
401 if ((opt_callsite_field
|| opt_all_fields
)) {
402 struct ctf_callsite_dups
*cs_dups
;
403 struct ctf_callsite
*callsite
;
405 cs_dups
= ctf_trace_callsite_lookup(stream_class
->trace
,
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
, ":");
416 fprintf(pos
->fp
, "[");
417 bt_list_for_each_entry(callsite
, &cs_dups
->head
, node
) {
419 fprintf(pos
->fp
, ",");
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
,
425 fprintf(pos
->fp
, "%s:%s:%" PRIu64
"",
426 callsite
->func
, callsite
->file
,
431 fprintf(pos
->fp
, "]");
432 if (pos
->print_names
)
433 fprintf(pos
->fp
, ", ");
437 if (dom_print
&& !pos
->print_names
)
438 fprintf(pos
->fp
, " ");
439 set_field_names_print(pos
, ITEM_HEADER
);
440 if (pos
->print_names
)
441 fprintf(pos
->fp
, "name = ");
442 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
443 if (pos
->print_names
)
446 fprintf(pos
->fp
, ":");
448 /* print cpuid field from packet context */
449 if (stream
->stream_packet_context
) {
450 if (pos
->field_nr
++ != 0)
451 fprintf(pos
->fp
, ",");
452 set_field_names_print(pos
, ITEM_SCOPE
);
453 if (pos
->print_names
)
454 fprintf(pos
->fp
, " stream.packet.context =");
455 field_nr_saved
= pos
->field_nr
;
457 set_field_names_print(pos
, ITEM_CONTEXT
);
458 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
461 pos
->field_nr
= field_nr_saved
;
464 /* Only show the event header in verbose mode */
465 if (babeltrace_verbose
&& stream
->stream_event_header
) {
466 if (pos
->field_nr
++ != 0)
467 fprintf(pos
->fp
, ",");
468 set_field_names_print(pos
, ITEM_SCOPE
);
469 if (pos
->print_names
)
470 fprintf(pos
->fp
, " stream.event.header =");
471 field_nr_saved
= pos
->field_nr
;
473 set_field_names_print(pos
, ITEM_CONTEXT
);
474 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
477 pos
->field_nr
= field_nr_saved
;
480 /* print stream-declared event context */
481 if (stream
->stream_event_context
) {
482 if (pos
->field_nr
++ != 0)
483 fprintf(pos
->fp
, ",");
484 set_field_names_print(pos
, ITEM_SCOPE
);
485 if (pos
->print_names
)
486 fprintf(pos
->fp
, " stream.event.context =");
487 field_nr_saved
= pos
->field_nr
;
489 set_field_names_print(pos
, ITEM_CONTEXT
);
490 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
493 pos
->field_nr
= field_nr_saved
;
496 /* print event-declared event context */
497 if (event
->event_context
) {
498 if (pos
->field_nr
++ != 0)
499 fprintf(pos
->fp
, ",");
500 set_field_names_print(pos
, ITEM_SCOPE
);
501 if (pos
->print_names
)
502 fprintf(pos
->fp
, " event.context =");
503 field_nr_saved
= pos
->field_nr
;
505 set_field_names_print(pos
, ITEM_CONTEXT
);
506 ret
= generic_rw(ppos
, &event
->event_context
->p
);
509 pos
->field_nr
= field_nr_saved
;
512 /* Read and print event payload */
513 if (event
->event_fields
) {
514 if (pos
->field_nr
++ != 0)
515 fprintf(pos
->fp
, ",");
516 set_field_names_print(pos
, ITEM_SCOPE
);
517 if (pos
->print_names
)
518 fprintf(pos
->fp
, " event.fields =");
519 field_nr_saved
= pos
->field_nr
;
521 set_field_names_print(pos
, ITEM_PAYLOAD
);
522 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
525 pos
->field_nr
= field_nr_saved
;
528 fprintf(pos
->fp
, "\n");
534 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
539 struct bt_trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
540 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
541 int whence
), FILE *metadata_fp
)
543 struct ctf_text_stream_pos
*pos
;
546 pos
= g_new0(struct ctf_text_stream_pos
, 1);
548 pos
->last_real_timestamp
= -1ULL;
549 pos
->last_cycles_timestamp
= -1ULL;
550 switch (flags
& O_ACCMODE
) {
555 fp
= fopen(path
, "w");
559 pos
->parent
.rw_table
= write_dispatch_table
;
560 pos
->parent
.event_cb
= ctf_text_write_event
;
561 pos
->parent
.trace
= &pos
->trace_descriptor
;
562 pos
->print_names
= 0;
563 babeltrace_ctf_console_output
++;
567 fprintf(stderr
, "[error] Incorrect open flags.\n");
571 return &pos
->trace_descriptor
;
578 int ctf_text_close_trace(struct bt_trace_descriptor
*td
)
581 struct ctf_text_stream_pos
*pos
=
582 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
584 babeltrace_ctf_console_output
--;
585 if (pos
->fp
!= stdout
) {
586 ret
= fclose(pos
->fp
);
588 perror("Error on fclose");
597 void __attribute__((constructor
)) ctf_text_init(void)
601 ctf_text_format
.name
= g_quark_from_static_string("text");
602 ret
= bt_register_format(&ctf_text_format
);
607 void __attribute__((destructor
)) ctf_text_exit(void)
609 bt_unregister_format(&ctf_text_format
);