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> |
642ec66d MD |
25 | #include <inttypes.h> |
26 | #include <uuid/uuid.h> | |
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, | |
49 | opt_loglevel_field, | |
7d97fad9 | 50 | opt_delta_field = 1; |
cba1661c MD |
51 | |
52 | enum field_item { | |
53 | ITEM_SCOPE, | |
54 | ITEM_HEADER, | |
55 | ITEM_CONTEXT, | |
56 | ITEM_PAYLOAD, | |
57 | }; | |
b88d6e85 | 58 | |
5b80ddfb | 59 | struct trace_descriptor *ctf_text_open_trace(const char *path, int flags, |
b086c01a | 60 | void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, |
ae23d232 | 61 | int whence), FILE *metadata_fp); |
642ec66d MD |
62 | void ctf_text_close_trace(struct trace_descriptor *descriptor); |
63 | ||
642ec66d MD |
64 | static |
65 | rw_dispatch write_dispatch_table[] = { | |
66 | [ CTF_TYPE_INTEGER ] = ctf_text_integer_write, | |
67 | [ CTF_TYPE_FLOAT ] = ctf_text_float_write, | |
68 | [ CTF_TYPE_ENUM ] = ctf_text_enum_write, | |
69 | [ CTF_TYPE_STRING ] = ctf_text_string_write, | |
70 | [ CTF_TYPE_STRUCT ] = ctf_text_struct_write, | |
71 | [ CTF_TYPE_VARIANT ] = ctf_text_variant_write, | |
72 | [ CTF_TYPE_ARRAY ] = ctf_text_array_write, | |
73 | [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write, | |
74 | }; | |
75 | ||
76 | static | |
77 | struct format ctf_text_format = { | |
78 | .open_trace = ctf_text_open_trace, | |
79 | .close_trace = ctf_text_close_trace, | |
80 | }; | |
81 | ||
d335f0f7 MD |
82 | static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN, |
83 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END, | |
84 | Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED, | |
85 | Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE, | |
86 | Q_STREAM_PACKET_CONTEXT_PACKET_SIZE; | |
87 | ||
88 | static | |
89 | void __attribute__((constructor)) init_quarks(void) | |
90 | { | |
91 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin"); | |
92 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end"); | |
93 | Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded"); | |
94 | Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size"); | |
95 | Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size"); | |
96 | } | |
97 | ||
98 | int print_field(struct definition *definition) | |
99 | { | |
100 | /* Print all fields in verbose mode */ | |
101 | if (babeltrace_verbose) | |
102 | return 1; | |
103 | ||
104 | /* Filter out part of the packet context */ | |
105 | if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN) | |
106 | return 0; | |
107 | if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END) | |
108 | return 0; | |
109 | if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED) | |
110 | return 0; | |
111 | if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE) | |
112 | return 0; | |
113 | if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE) | |
114 | return 0; | |
115 | ||
116 | return 1; | |
117 | } | |
118 | ||
cba1661c MD |
119 | static |
120 | void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item) | |
121 | { | |
122 | switch (item) { | |
123 | case ITEM_SCOPE: | |
124 | if (opt_all_field_names || opt_scope_field_names) | |
125 | pos->print_names = 1; | |
126 | else | |
127 | pos->print_names = 0; | |
128 | break; | |
129 | case ITEM_HEADER: | |
130 | if (opt_all_field_names || opt_header_field_names) | |
131 | pos->print_names = 1; | |
132 | else | |
133 | pos->print_names = 0; | |
134 | break; | |
135 | case ITEM_CONTEXT: | |
136 | if (opt_all_field_names || opt_context_field_names) | |
137 | pos->print_names = 1; | |
138 | else | |
139 | pos->print_names = 0; | |
140 | break; | |
141 | case ITEM_PAYLOAD: | |
142 | if (opt_all_field_names || opt_payload_field_names) | |
143 | pos->print_names = 1; | |
144 | else | |
145 | pos->print_names = 0; | |
146 | ||
147 | break; | |
148 | default: | |
149 | assert(0); | |
150 | } | |
151 | } | |
152 | ||
31262354 MD |
153 | static |
154 | int ctf_text_write_event(struct stream_pos *ppos, | |
764af3f4 | 155 | struct ctf_stream *stream) |
31262354 MD |
156 | { |
157 | struct ctf_text_stream_pos *pos = | |
158 | container_of(ppos, struct ctf_text_stream_pos, parent); | |
764af3f4 | 159 | struct ctf_stream_class *stream_class = stream->stream_class; |
fd3382e8 | 160 | int field_nr_saved; |
31262354 | 161 | struct ctf_event *event_class; |
42dc00b7 | 162 | struct ctf_stream_event *event; |
c87a8eb2 | 163 | uint64_t id; |
31262354 | 164 | int ret; |
8c250d87 | 165 | int dom_print = 0; |
31262354 | 166 | |
c87a8eb2 | 167 | id = stream->event_id; |
31262354 MD |
168 | |
169 | if (id >= stream_class->events_by_id->len) { | |
3394d22e | 170 | fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id); |
31262354 MD |
171 | return -EINVAL; |
172 | } | |
e28d4618 MD |
173 | event = g_ptr_array_index(stream->events_by_id, id); |
174 | if (!event) { | |
3394d22e | 175 | fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id); |
e28d4618 MD |
176 | return -EINVAL; |
177 | } | |
31262354 | 178 | event_class = g_ptr_array_index(stream_class->events_by_id, id); |
e28d4618 | 179 | if (!event) { |
3394d22e | 180 | fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id); |
31262354 MD |
181 | return -EINVAL; |
182 | } | |
183 | ||
fca04958 MD |
184 | /* Print events discarded */ |
185 | if (stream->events_discarded) { | |
7d97fad9 | 186 | fflush(pos->fp); |
fca04958 MD |
187 | fprintf(stderr, "[warning] Tracer discarded %d events between [", |
188 | stream->events_discarded); | |
7d97fad9 | 189 | ctf_print_timestamp(stderr, stream, stream->prev_timestamp); |
fca04958 | 190 | fprintf(stderr, "] and ["); |
7d97fad9 | 191 | ctf_print_timestamp(stderr, stream, stream->prev_timestamp_end); |
fca04958 MD |
192 | fprintf(stderr, "]. You should consider increasing the buffer size.\n"); |
193 | fflush(stderr); | |
194 | stream->events_discarded = 0; | |
195 | } | |
196 | ||
5e2eb0ae | 197 | if (stream->has_timestamp) { |
cba1661c | 198 | set_field_names_print(pos, ITEM_HEADER); |
be60c7fb MD |
199 | if (pos->print_names) |
200 | fprintf(pos->fp, "timestamp = "); | |
201 | else | |
202 | fprintf(pos->fp, "["); | |
7d97fad9 | 203 | ctf_print_timestamp(pos->fp, stream, stream->timestamp); |
be60c7fb MD |
204 | if (!pos->print_names) |
205 | fprintf(pos->fp, "]"); | |
aa6bffae | 206 | |
be60c7fb MD |
207 | if (pos->print_names) |
208 | fprintf(pos->fp, ", "); | |
209 | else | |
210 | fprintf(pos->fp, " "); | |
211 | } | |
359d7456 | 212 | if ((opt_delta_field || opt_all_fields) && stream->has_timestamp) { |
8d8ed9af MD |
213 | uint64_t delta, delta_sec, delta_nsec; |
214 | ||
215 | set_field_names_print(pos, ITEM_HEADER); | |
216 | if (pos->print_names) | |
217 | fprintf(pos->fp, "delta = "); | |
218 | else | |
219 | fprintf(pos->fp, "("); | |
220 | if (pos->last_timestamp != -1ULL) { | |
221 | delta = stream->timestamp - pos->last_timestamp; | |
222 | delta_sec = delta / NSEC_PER_SEC; | |
223 | delta_nsec = delta % NSEC_PER_SEC; | |
224 | fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64, | |
225 | delta_sec, delta_nsec); | |
226 | } else { | |
227 | fprintf(pos->fp, "+?.?????????"); | |
228 | } | |
229 | if (!pos->print_names) | |
230 | fprintf(pos->fp, ")"); | |
231 | ||
232 | if (pos->print_names) | |
233 | fprintf(pos->fp, ", "); | |
234 | else | |
235 | fprintf(pos->fp, " "); | |
236 | pos->last_timestamp = stream->timestamp; | |
237 | } | |
238 | ||
359d7456 | 239 | if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') { |
82662ad4 | 240 | set_field_names_print(pos, ITEM_HEADER); |
8c250d87 | 241 | if (pos->print_names) { |
359d7456 | 242 | fprintf(pos->fp, "trace = "); |
8c250d87 | 243 | } |
82662ad4 | 244 | fprintf(pos->fp, "%s", stream_class->trace->path); |
82662ad4 MD |
245 | if (pos->print_names) |
246 | fprintf(pos->fp, ", "); | |
247 | else | |
248 | fprintf(pos->fp, " "); | |
249 | } | |
1842a4c8 | 250 | if ((opt_trace_domain_field && !opt_all_fields) && stream_class->trace->env.domain[0] != '\0') { |
8c250d87 MD |
251 | set_field_names_print(pos, ITEM_HEADER); |
252 | if (pos->print_names) { | |
253 | fprintf(pos->fp, "trace:domain = "); | |
254 | } | |
1842a4c8 | 255 | fprintf(pos->fp, "%s", stream_class->trace->env.domain); |
8c250d87 MD |
256 | if (pos->print_names) |
257 | fprintf(pos->fp, ", "); | |
258 | dom_print = 1; | |
259 | } | |
1842a4c8 | 260 | if ((opt_trace_procname_field && !opt_all_fields) && stream_class->trace->env.procname[0] != '\0') { |
8c250d87 MD |
261 | set_field_names_print(pos, ITEM_HEADER); |
262 | if (pos->print_names) { | |
263 | fprintf(pos->fp, "trace:procname = "); | |
264 | } else if (dom_print) { | |
265 | fprintf(pos->fp, ":"); | |
266 | } | |
1842a4c8 | 267 | fprintf(pos->fp, "%s", stream_class->trace->env.procname); |
8c250d87 MD |
268 | if (pos->print_names) |
269 | fprintf(pos->fp, ", "); | |
270 | dom_print = 1; | |
271 | } | |
1842a4c8 | 272 | if ((opt_trace_vpid_field && !opt_all_fields) && stream_class->trace->env.vpid != -1) { |
8c250d87 MD |
273 | set_field_names_print(pos, ITEM_HEADER); |
274 | if (pos->print_names) { | |
275 | fprintf(pos->fp, "trace:vpid = "); | |
276 | } else if (dom_print) { | |
277 | fprintf(pos->fp, ":"); | |
278 | } | |
1842a4c8 | 279 | fprintf(pos->fp, "%d", stream_class->trace->env.vpid); |
8c250d87 MD |
280 | if (pos->print_names) |
281 | fprintf(pos->fp, ", "); | |
282 | dom_print = 1; | |
283 | } | |
306eeaa6 | 284 | if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) { |
d86d62f8 MD |
285 | set_field_names_print(pos, ITEM_HEADER); |
286 | if (pos->print_names) { | |
287 | fprintf(pos->fp, "loglevel = "); | |
288 | } else if (dom_print) { | |
289 | fprintf(pos->fp, ":"); | |
290 | } | |
306eeaa6 MD |
291 | fprintf(pos->fp, "(%d)", |
292 | event_class->loglevel); | |
d86d62f8 MD |
293 | if (pos->print_names) |
294 | fprintf(pos->fp, ", "); | |
295 | dom_print = 1; | |
296 | } | |
8c250d87 MD |
297 | if (dom_print && !pos->print_names) |
298 | fprintf(pos->fp, " "); | |
cba1661c | 299 | set_field_names_print(pos, ITEM_HEADER); |
31262354 MD |
300 | if (pos->print_names) |
301 | fprintf(pos->fp, "name = "); | |
8178fe48 MD |
302 | fprintf(pos->fp, "%s", g_quark_to_string(event_class->name)); |
303 | if (pos->print_names) | |
fd3382e8 | 304 | pos->field_nr++; |
8178fe48 | 305 | else |
fd3382e8 | 306 | fprintf(pos->fp, ":"); |
31262354 | 307 | |
e28d4618 MD |
308 | /* print cpuid field from packet context */ |
309 | if (stream->stream_packet_context) { | |
310 | if (pos->field_nr++ != 0) | |
311 | fprintf(pos->fp, ","); | |
cba1661c | 312 | set_field_names_print(pos, ITEM_SCOPE); |
e28d4618 MD |
313 | if (pos->print_names) |
314 | fprintf(pos->fp, " stream.packet.context ="); | |
315 | field_nr_saved = pos->field_nr; | |
316 | pos->field_nr = 0; | |
cba1661c | 317 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 MD |
318 | ret = generic_rw(ppos, &stream->stream_packet_context->p); |
319 | if (ret) | |
320 | goto error; | |
321 | pos->field_nr = field_nr_saved; | |
322 | } | |
323 | ||
764af3f4 | 324 | /* Only show the event header in verbose mode */ |
e28d4618 | 325 | if (babeltrace_verbose && stream->stream_event_header) { |
fd3382e8 MD |
326 | if (pos->field_nr++ != 0) |
327 | fprintf(pos->fp, ","); | |
cba1661c | 328 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 329 | if (pos->print_names) |
fd3382e8 MD |
330 | fprintf(pos->fp, " stream.event.header ="); |
331 | field_nr_saved = pos->field_nr; | |
332 | pos->field_nr = 0; | |
cba1661c | 333 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 334 | ret = generic_rw(ppos, &stream->stream_event_header->p); |
31262354 MD |
335 | if (ret) |
336 | goto error; | |
fd3382e8 | 337 | pos->field_nr = field_nr_saved; |
31262354 MD |
338 | } |
339 | ||
340 | /* print stream-declared event context */ | |
e28d4618 | 341 | if (stream->stream_event_context) { |
fd3382e8 MD |
342 | if (pos->field_nr++ != 0) |
343 | fprintf(pos->fp, ","); | |
cba1661c | 344 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 345 | if (pos->print_names) |
fd3382e8 MD |
346 | fprintf(pos->fp, " stream.event.context ="); |
347 | field_nr_saved = pos->field_nr; | |
348 | pos->field_nr = 0; | |
cba1661c | 349 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 350 | ret = generic_rw(ppos, &stream->stream_event_context->p); |
31262354 MD |
351 | if (ret) |
352 | goto error; | |
fd3382e8 | 353 | pos->field_nr = field_nr_saved; |
31262354 MD |
354 | } |
355 | ||
356 | /* print event-declared event context */ | |
e28d4618 | 357 | if (event->event_context) { |
fd3382e8 MD |
358 | if (pos->field_nr++ != 0) |
359 | fprintf(pos->fp, ","); | |
cba1661c | 360 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 361 | if (pos->print_names) |
fd3382e8 MD |
362 | fprintf(pos->fp, " event.context ="); |
363 | field_nr_saved = pos->field_nr; | |
364 | pos->field_nr = 0; | |
cba1661c | 365 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 366 | ret = generic_rw(ppos, &event->event_context->p); |
31262354 MD |
367 | if (ret) |
368 | goto error; | |
fd3382e8 | 369 | pos->field_nr = field_nr_saved; |
31262354 MD |
370 | } |
371 | ||
372 | /* Read and print event payload */ | |
e28d4618 | 373 | if (event->event_fields) { |
fd3382e8 MD |
374 | if (pos->field_nr++ != 0) |
375 | fprintf(pos->fp, ","); | |
cba1661c | 376 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 377 | if (pos->print_names) |
fd3382e8 MD |
378 | fprintf(pos->fp, " event.fields ="); |
379 | field_nr_saved = pos->field_nr; | |
380 | pos->field_nr = 0; | |
cba1661c | 381 | set_field_names_print(pos, ITEM_PAYLOAD); |
e28d4618 | 382 | ret = generic_rw(ppos, &event->event_fields->p); |
31262354 MD |
383 | if (ret) |
384 | goto error; | |
fd3382e8 | 385 | pos->field_nr = field_nr_saved; |
31262354 | 386 | } |
fd3382e8 | 387 | /* newline */ |
31262354 | 388 | fprintf(pos->fp, "\n"); |
fd3382e8 | 389 | pos->field_nr = 0; |
90fcbacc | 390 | stream->consumed = 1; |
31262354 MD |
391 | |
392 | return 0; | |
393 | ||
394 | error: | |
3394d22e | 395 | 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 |
396 | return ret; |
397 | } | |
398 | ||
399 | ||
5b80ddfb | 400 | struct trace_descriptor *ctf_text_open_trace(const char *path, int flags, |
b086c01a | 401 | void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, |
ae23d232 | 402 | int whence), FILE *metadata_fp) |
642ec66d MD |
403 | { |
404 | struct ctf_text_stream_pos *pos; | |
405 | FILE *fp; | |
406 | ||
407 | pos = g_new0(struct ctf_text_stream_pos, 1); | |
408 | ||
8d8ed9af | 409 | pos->last_timestamp = -1ULL; |
642ec66d | 410 | switch (flags & O_ACCMODE) { |
989c73bc | 411 | case O_RDWR: |
b61922b5 | 412 | if (!path) |
6cf7957b MD |
413 | fp = stdout; |
414 | else | |
415 | fp = fopen(path, "w"); | |
642ec66d MD |
416 | if (!fp) |
417 | goto error; | |
418 | pos->fp = fp; | |
419 | pos->parent.rw_table = write_dispatch_table; | |
31262354 | 420 | pos->parent.event_cb = ctf_text_write_event; |
cba1661c | 421 | pos->print_names = 0; |
642ec66d MD |
422 | break; |
423 | case O_RDONLY: | |
424 | default: | |
3394d22e | 425 | fprintf(stderr, "[error] Incorrect open flags.\n"); |
642ec66d MD |
426 | goto error; |
427 | } | |
428 | ||
429 | return &pos->trace_descriptor; | |
430 | error: | |
431 | g_free(pos); | |
432 | return NULL; | |
433 | } | |
434 | ||
435 | void ctf_text_close_trace(struct trace_descriptor *td) | |
436 | { | |
437 | struct ctf_text_stream_pos *pos = | |
438 | container_of(td, struct ctf_text_stream_pos, trace_descriptor); | |
439 | fclose(pos->fp); | |
440 | g_free(pos); | |
441 | } | |
442 | ||
443 | void __attribute__((constructor)) ctf_text_init(void) | |
444 | { | |
445 | int ret; | |
446 | ||
447 | ctf_text_format.name = g_quark_from_static_string("text"); | |
448 | ret = bt_register_format(&ctf_text_format); | |
449 | assert(!ret); | |
450 | } | |
451 | ||
452 | /* TODO: finalize */ |