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