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