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, |
8c250d87 MD |
44 | opt_trace_name, |
45 | opt_trace_domain, | |
46 | opt_trace_procname, | |
d86d62f8 | 47 | opt_trace_vpid, |
8d8ed9af MD |
48 | opt_loglevel, |
49 | opt_delta = 1; | |
cba1661c MD |
50 | |
51 | enum field_item { | |
52 | ITEM_SCOPE, | |
53 | ITEM_HEADER, | |
54 | ITEM_CONTEXT, | |
55 | ITEM_PAYLOAD, | |
56 | }; | |
b88d6e85 | 57 | |
8c250d87 MD |
58 | struct trace_descriptor *ctf_text_open_trace(const char *collection_path, |
59 | 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) { | |
170 | fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id); | |
171 | return -EINVAL; | |
172 | } | |
e28d4618 MD |
173 | event = g_ptr_array_index(stream->events_by_id, id); |
174 | if (!event) { | |
175 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); | |
176 | return -EINVAL; | |
177 | } | |
31262354 | 178 | event_class = g_ptr_array_index(stream_class->events_by_id, id); |
e28d4618 | 179 | if (!event) { |
31262354 MD |
180 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); |
181 | return -EINVAL; | |
182 | } | |
183 | ||
5e2eb0ae | 184 | if (stream->has_timestamp) { |
bfe74b8c MD |
185 | uint64_t ts_sec, ts_nsec; |
186 | ||
187 | ts_sec = stream->timestamp / NSEC_PER_SEC; | |
188 | ts_nsec = stream->timestamp % NSEC_PER_SEC; | |
cba1661c | 189 | set_field_names_print(pos, ITEM_HEADER); |
be60c7fb MD |
190 | if (pos->print_names) |
191 | fprintf(pos->fp, "timestamp = "); | |
192 | else | |
193 | fprintf(pos->fp, "["); | |
bfe74b8c MD |
194 | fprintf(pos->fp, "%3" PRIu64 ".%09" PRIu64, |
195 | ts_sec, ts_nsec); | |
be60c7fb MD |
196 | if (!pos->print_names) |
197 | fprintf(pos->fp, "]"); | |
aa6bffae | 198 | |
be60c7fb MD |
199 | if (pos->print_names) |
200 | fprintf(pos->fp, ", "); | |
201 | else | |
202 | fprintf(pos->fp, " "); | |
203 | } | |
8d8ed9af MD |
204 | if (opt_delta && stream->has_timestamp) { |
205 | uint64_t delta, delta_sec, delta_nsec; | |
206 | ||
207 | set_field_names_print(pos, ITEM_HEADER); | |
208 | if (pos->print_names) | |
209 | fprintf(pos->fp, "delta = "); | |
210 | else | |
211 | fprintf(pos->fp, "("); | |
212 | if (pos->last_timestamp != -1ULL) { | |
213 | delta = stream->timestamp - pos->last_timestamp; | |
214 | delta_sec = delta / NSEC_PER_SEC; | |
215 | delta_nsec = delta % NSEC_PER_SEC; | |
216 | fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64, | |
217 | delta_sec, delta_nsec); | |
218 | } else { | |
219 | fprintf(pos->fp, "+?.?????????"); | |
220 | } | |
221 | if (!pos->print_names) | |
222 | fprintf(pos->fp, ")"); | |
223 | ||
224 | if (pos->print_names) | |
225 | fprintf(pos->fp, ", "); | |
226 | else | |
227 | fprintf(pos->fp, " "); | |
228 | pos->last_timestamp = stream->timestamp; | |
229 | } | |
230 | ||
82662ad4 MD |
231 | if ((opt_trace_name || opt_all_field_names) && stream_class->trace->path[0] != '\0') { |
232 | set_field_names_print(pos, ITEM_HEADER); | |
8c250d87 MD |
233 | if (pos->print_names) { |
234 | if (opt_trace_name || opt_all_field_names) | |
235 | fprintf(pos->fp, "trace = "); | |
236 | } | |
82662ad4 MD |
237 | |
238 | fprintf(pos->fp, "%s", stream_class->trace->path); | |
82662ad4 MD |
239 | if (pos->print_names) |
240 | fprintf(pos->fp, ", "); | |
241 | else | |
242 | fprintf(pos->fp, " "); | |
243 | } | |
8c250d87 MD |
244 | if ((opt_trace_domain) && stream_class->trace->domain[0] != '\0') { |
245 | set_field_names_print(pos, ITEM_HEADER); | |
246 | if (pos->print_names) { | |
247 | fprintf(pos->fp, "trace:domain = "); | |
248 | } | |
249 | if (opt_trace_domain) | |
250 | fprintf(pos->fp, "%s", stream_class->trace->domain); | |
251 | if (pos->print_names) | |
252 | fprintf(pos->fp, ", "); | |
253 | dom_print = 1; | |
254 | } | |
255 | if ((opt_trace_procname) && stream_class->trace->procname[0] != '\0') { | |
256 | set_field_names_print(pos, ITEM_HEADER); | |
257 | if (pos->print_names) { | |
258 | fprintf(pos->fp, "trace:procname = "); | |
259 | } else if (dom_print) { | |
260 | fprintf(pos->fp, ":"); | |
261 | } | |
262 | ||
263 | if (opt_trace_procname) | |
264 | fprintf(pos->fp, "%s", stream_class->trace->procname); | |
265 | if (pos->print_names) | |
266 | fprintf(pos->fp, ", "); | |
267 | dom_print = 1; | |
268 | } | |
269 | if ((opt_trace_vpid) && stream_class->trace->vpid[0] != '\0') { | |
270 | set_field_names_print(pos, ITEM_HEADER); | |
271 | if (pos->print_names) { | |
272 | fprintf(pos->fp, "trace:vpid = "); | |
273 | } else if (dom_print) { | |
274 | fprintf(pos->fp, ":"); | |
275 | } | |
276 | ||
277 | if (opt_trace_vpid) | |
278 | fprintf(pos->fp, "%s", stream_class->trace->vpid); | |
279 | if (pos->print_names) | |
280 | fprintf(pos->fp, ", "); | |
281 | dom_print = 1; | |
282 | } | |
d86d62f8 MD |
283 | if ((opt_loglevel || opt_all_field_names) && event_class->loglevel_identifier != 0) { |
284 | set_field_names_print(pos, ITEM_HEADER); | |
285 | if (pos->print_names) { | |
286 | fprintf(pos->fp, "loglevel = "); | |
287 | } else if (dom_print) { | |
288 | fprintf(pos->fp, ":"); | |
289 | } | |
290 | ||
291 | fprintf(pos->fp, "%s (%lld)", | |
292 | g_quark_to_string(event_class->loglevel_identifier), | |
293 | (long long) event_class->loglevel_value); | |
294 | if (pos->print_names) | |
295 | fprintf(pos->fp, ", "); | |
296 | dom_print = 1; | |
297 | } | |
8c250d87 MD |
298 | if (dom_print && !pos->print_names) |
299 | fprintf(pos->fp, " "); | |
cba1661c | 300 | set_field_names_print(pos, ITEM_HEADER); |
31262354 MD |
301 | if (pos->print_names) |
302 | fprintf(pos->fp, "name = "); | |
8178fe48 MD |
303 | fprintf(pos->fp, "%s", g_quark_to_string(event_class->name)); |
304 | if (pos->print_names) | |
fd3382e8 | 305 | pos->field_nr++; |
8178fe48 | 306 | else |
fd3382e8 | 307 | fprintf(pos->fp, ":"); |
31262354 | 308 | |
e28d4618 MD |
309 | /* print cpuid field from packet context */ |
310 | if (stream->stream_packet_context) { | |
311 | if (pos->field_nr++ != 0) | |
312 | fprintf(pos->fp, ","); | |
cba1661c | 313 | set_field_names_print(pos, ITEM_SCOPE); |
e28d4618 MD |
314 | if (pos->print_names) |
315 | fprintf(pos->fp, " stream.packet.context ="); | |
316 | field_nr_saved = pos->field_nr; | |
317 | pos->field_nr = 0; | |
cba1661c | 318 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 MD |
319 | ret = generic_rw(ppos, &stream->stream_packet_context->p); |
320 | if (ret) | |
321 | goto error; | |
322 | pos->field_nr = field_nr_saved; | |
323 | } | |
324 | ||
764af3f4 | 325 | /* Only show the event header in verbose mode */ |
e28d4618 | 326 | if (babeltrace_verbose && stream->stream_event_header) { |
fd3382e8 MD |
327 | if (pos->field_nr++ != 0) |
328 | fprintf(pos->fp, ","); | |
cba1661c | 329 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 330 | if (pos->print_names) |
fd3382e8 MD |
331 | fprintf(pos->fp, " stream.event.header ="); |
332 | field_nr_saved = pos->field_nr; | |
333 | pos->field_nr = 0; | |
cba1661c | 334 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 335 | ret = generic_rw(ppos, &stream->stream_event_header->p); |
31262354 MD |
336 | if (ret) |
337 | goto error; | |
fd3382e8 | 338 | pos->field_nr = field_nr_saved; |
31262354 MD |
339 | } |
340 | ||
341 | /* print stream-declared event context */ | |
e28d4618 | 342 | if (stream->stream_event_context) { |
fd3382e8 MD |
343 | if (pos->field_nr++ != 0) |
344 | fprintf(pos->fp, ","); | |
cba1661c | 345 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 346 | if (pos->print_names) |
fd3382e8 MD |
347 | fprintf(pos->fp, " stream.event.context ="); |
348 | field_nr_saved = pos->field_nr; | |
349 | pos->field_nr = 0; | |
cba1661c | 350 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 351 | ret = generic_rw(ppos, &stream->stream_event_context->p); |
31262354 MD |
352 | if (ret) |
353 | goto error; | |
fd3382e8 | 354 | pos->field_nr = field_nr_saved; |
31262354 MD |
355 | } |
356 | ||
357 | /* print event-declared event context */ | |
e28d4618 | 358 | if (event->event_context) { |
fd3382e8 MD |
359 | if (pos->field_nr++ != 0) |
360 | fprintf(pos->fp, ","); | |
cba1661c | 361 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 362 | if (pos->print_names) |
fd3382e8 MD |
363 | fprintf(pos->fp, " event.context ="); |
364 | field_nr_saved = pos->field_nr; | |
365 | pos->field_nr = 0; | |
cba1661c | 366 | set_field_names_print(pos, ITEM_CONTEXT); |
e28d4618 | 367 | ret = generic_rw(ppos, &event->event_context->p); |
31262354 MD |
368 | if (ret) |
369 | goto error; | |
fd3382e8 | 370 | pos->field_nr = field_nr_saved; |
31262354 MD |
371 | } |
372 | ||
373 | /* Read and print event payload */ | |
e28d4618 | 374 | if (event->event_fields) { |
fd3382e8 MD |
375 | if (pos->field_nr++ != 0) |
376 | fprintf(pos->fp, ","); | |
cba1661c | 377 | set_field_names_print(pos, ITEM_SCOPE); |
31262354 | 378 | if (pos->print_names) |
fd3382e8 MD |
379 | fprintf(pos->fp, " event.fields ="); |
380 | field_nr_saved = pos->field_nr; | |
381 | pos->field_nr = 0; | |
cba1661c | 382 | set_field_names_print(pos, ITEM_PAYLOAD); |
e28d4618 | 383 | ret = generic_rw(ppos, &event->event_fields->p); |
31262354 MD |
384 | if (ret) |
385 | goto error; | |
fd3382e8 | 386 | pos->field_nr = field_nr_saved; |
31262354 | 387 | } |
fd3382e8 | 388 | /* newline */ |
31262354 | 389 | fprintf(pos->fp, "\n"); |
fd3382e8 | 390 | pos->field_nr = 0; |
31262354 MD |
391 | |
392 | return 0; | |
393 | ||
394 | error: | |
395 | fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n"); | |
396 | return ret; | |
397 | } | |
398 | ||
399 | ||
8c250d87 MD |
400 | struct trace_descriptor *ctf_text_open_trace(const char *collection_path, |
401 | const char *path, int flags, | |
b086c01a | 402 | void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, |
ae23d232 | 403 | int whence), FILE *metadata_fp) |
642ec66d MD |
404 | { |
405 | struct ctf_text_stream_pos *pos; | |
406 | FILE *fp; | |
407 | ||
408 | pos = g_new0(struct ctf_text_stream_pos, 1); | |
409 | ||
8d8ed9af | 410 | pos->last_timestamp = -1ULL; |
642ec66d | 411 | switch (flags & O_ACCMODE) { |
989c73bc | 412 | case O_RDWR: |
b61922b5 | 413 | if (!path) |
6cf7957b MD |
414 | fp = stdout; |
415 | else | |
416 | fp = fopen(path, "w"); | |
642ec66d MD |
417 | if (!fp) |
418 | goto error; | |
419 | pos->fp = fp; | |
420 | pos->parent.rw_table = write_dispatch_table; | |
31262354 | 421 | pos->parent.event_cb = ctf_text_write_event; |
cba1661c | 422 | pos->print_names = 0; |
642ec66d MD |
423 | break; |
424 | case O_RDONLY: | |
425 | default: | |
426 | fprintf(stdout, "[error] Incorrect open flags.\n"); | |
427 | goto error; | |
428 | } | |
429 | ||
430 | return &pos->trace_descriptor; | |
431 | error: | |
432 | g_free(pos); | |
433 | return NULL; | |
434 | } | |
435 | ||
436 | void ctf_text_close_trace(struct trace_descriptor *td) | |
437 | { | |
438 | struct ctf_text_stream_pos *pos = | |
439 | container_of(td, struct ctf_text_stream_pos, trace_descriptor); | |
440 | fclose(pos->fp); | |
441 | g_free(pos); | |
442 | } | |
443 | ||
444 | void __attribute__((constructor)) ctf_text_init(void) | |
445 | { | |
446 | int ret; | |
447 | ||
448 | ctf_text_format.name = g_quark_from_static_string("text"); | |
449 | ret = bt_register_format(&ctf_text_format); | |
450 | assert(!ret); | |
451 | } | |
452 | ||
453 | /* TODO: finalize */ |