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