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