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