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