1b3e4bc73438716289662696e8da6c4b33410c2e
[babeltrace.git] / formats / ctf-text / ctf-text.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * CTF Text Format registration.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
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
37 int opt_all_field_names,
38 opt_scope_field_names,
39 opt_header_field_names,
40 opt_context_field_names,
41 opt_payload_field_names,
42 opt_trace_name,
43 opt_trace_domain,
44 opt_trace_procname,
45 opt_trace_vpid,
46 opt_loglevel;
47
48 enum field_item {
49 ITEM_SCOPE,
50 ITEM_HEADER,
51 ITEM_CONTEXT,
52 ITEM_PAYLOAD,
53 };
54
55 struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
56 const char *path, int flags,
57 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
58 int whence), FILE *metadata_fp);
59 void ctf_text_close_trace(struct trace_descriptor *descriptor);
60
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
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
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
150 static
151 int ctf_text_write_event(struct stream_pos *ppos,
152 struct ctf_stream *stream)
153 {
154 struct ctf_text_stream_pos *pos =
155 container_of(ppos, struct ctf_text_stream_pos, parent);
156 struct ctf_stream_class *stream_class = stream->stream_class;
157 int field_nr_saved;
158 struct ctf_event *event_class;
159 struct ctf_stream_event *event;
160 uint64_t id;
161 int ret;
162 int dom_print = 0;
163
164 id = stream->event_id;
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 }
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 }
175 event_class = g_ptr_array_index(stream_class->events_by_id, id);
176 if (!event) {
177 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
178 return -EINVAL;
179 }
180
181 if (stream->has_timestamp) {
182 set_field_names_print(pos, ITEM_HEADER);
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, "]");
190
191 if (pos->print_names)
192 fprintf(pos->fp, ", ");
193 else
194 fprintf(pos->fp, " ");
195 }
196 if ((opt_trace_name || opt_all_field_names) && stream_class->trace->path[0] != '\0') {
197 set_field_names_print(pos, ITEM_HEADER);
198 if (pos->print_names) {
199 if (opt_trace_name || opt_all_field_names)
200 fprintf(pos->fp, "trace = ");
201 }
202
203 fprintf(pos->fp, "%s", stream_class->trace->path);
204 if (pos->print_names)
205 fprintf(pos->fp, ", ");
206 else
207 fprintf(pos->fp, " ");
208 }
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 }
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 }
263 if (dom_print && !pos->print_names)
264 fprintf(pos->fp, " ");
265 set_field_names_print(pos, ITEM_HEADER);
266 if (pos->print_names)
267 fprintf(pos->fp, "name = ");
268 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
269 if (pos->print_names)
270 pos->field_nr++;
271 else
272 fprintf(pos->fp, ":");
273
274 /* print cpuid field from packet context */
275 if (stream->stream_packet_context) {
276 if (pos->field_nr++ != 0)
277 fprintf(pos->fp, ",");
278 set_field_names_print(pos, ITEM_SCOPE);
279 if (pos->print_names)
280 fprintf(pos->fp, " stream.packet.context =");
281 field_nr_saved = pos->field_nr;
282 pos->field_nr = 0;
283 set_field_names_print(pos, ITEM_CONTEXT);
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
290 /* Only show the event header in verbose mode */
291 if (babeltrace_verbose && stream->stream_event_header) {
292 if (pos->field_nr++ != 0)
293 fprintf(pos->fp, ",");
294 set_field_names_print(pos, ITEM_SCOPE);
295 if (pos->print_names)
296 fprintf(pos->fp, " stream.event.header =");
297 field_nr_saved = pos->field_nr;
298 pos->field_nr = 0;
299 set_field_names_print(pos, ITEM_CONTEXT);
300 ret = generic_rw(ppos, &stream->stream_event_header->p);
301 if (ret)
302 goto error;
303 pos->field_nr = field_nr_saved;
304 }
305
306 /* print stream-declared event context */
307 if (stream->stream_event_context) {
308 if (pos->field_nr++ != 0)
309 fprintf(pos->fp, ",");
310 set_field_names_print(pos, ITEM_SCOPE);
311 if (pos->print_names)
312 fprintf(pos->fp, " stream.event.context =");
313 field_nr_saved = pos->field_nr;
314 pos->field_nr = 0;
315 set_field_names_print(pos, ITEM_CONTEXT);
316 ret = generic_rw(ppos, &stream->stream_event_context->p);
317 if (ret)
318 goto error;
319 pos->field_nr = field_nr_saved;
320 }
321
322 /* print event-declared event context */
323 if (event->event_context) {
324 if (pos->field_nr++ != 0)
325 fprintf(pos->fp, ",");
326 set_field_names_print(pos, ITEM_SCOPE);
327 if (pos->print_names)
328 fprintf(pos->fp, " event.context =");
329 field_nr_saved = pos->field_nr;
330 pos->field_nr = 0;
331 set_field_names_print(pos, ITEM_CONTEXT);
332 ret = generic_rw(ppos, &event->event_context->p);
333 if (ret)
334 goto error;
335 pos->field_nr = field_nr_saved;
336 }
337
338 /* Read and print event payload */
339 if (event->event_fields) {
340 if (pos->field_nr++ != 0)
341 fprintf(pos->fp, ",");
342 set_field_names_print(pos, ITEM_SCOPE);
343 if (pos->print_names)
344 fprintf(pos->fp, " event.fields =");
345 field_nr_saved = pos->field_nr;
346 pos->field_nr = 0;
347 set_field_names_print(pos, ITEM_PAYLOAD);
348 ret = generic_rw(ppos, &event->event_fields->p);
349 if (ret)
350 goto error;
351 pos->field_nr = field_nr_saved;
352 }
353 /* newline */
354 fprintf(pos->fp, "\n");
355 pos->field_nr = 0;
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
365 struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
366 const char *path, int flags,
367 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
368 int whence), FILE *metadata_fp)
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) {
376 case O_RDWR:
377 if (!path)
378 fp = stdout;
379 else
380 fp = fopen(path, "w");
381 if (!fp)
382 goto error;
383 pos->fp = fp;
384 pos->parent.rw_table = write_dispatch_table;
385 pos->parent.event_cb = ctf_text_write_event;
386 pos->print_names = 0;
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 */
This page took 0.035914 seconds and 3 git commands to generate.