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