Remove all code that depends on trace collection directory structure
[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 #define NSEC_PER_SEC 1000000000ULL
38
39 int opt_all_field_names,
40 opt_scope_field_names,
41 opt_header_field_names,
42 opt_context_field_names,
43 opt_payload_field_names,
44 opt_all_fields,
45 opt_trace_field,
46 opt_trace_domain_field,
47 opt_trace_procname_field,
48 opt_trace_vpid_field,
49 opt_loglevel_field,
50 opt_delta_field = 1;
51
52 enum field_item {
53 ITEM_SCOPE,
54 ITEM_HEADER,
55 ITEM_CONTEXT,
56 ITEM_PAYLOAD,
57 };
58
59 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
60 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
61 int whence), FILE *metadata_fp);
62 void ctf_text_close_trace(struct trace_descriptor *descriptor);
63
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
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
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
153 static
154 int ctf_text_write_event(struct stream_pos *ppos,
155 struct ctf_stream *stream)
156 {
157 struct ctf_text_stream_pos *pos =
158 container_of(ppos, struct ctf_text_stream_pos, parent);
159 struct ctf_stream_class *stream_class = stream->stream_class;
160 int field_nr_saved;
161 struct ctf_event *event_class;
162 struct ctf_stream_event *event;
163 uint64_t id;
164 int ret;
165 int dom_print = 0;
166
167 id = stream->event_id;
168
169 if (id >= stream_class->events_by_id->len) {
170 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
171 return -EINVAL;
172 }
173 event = g_ptr_array_index(stream->events_by_id, id);
174 if (!event) {
175 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
176 return -EINVAL;
177 }
178 event_class = g_ptr_array_index(stream_class->events_by_id, id);
179 if (!event) {
180 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
181 return -EINVAL;
182 }
183
184 /* Print events discarded */
185 if (stream->events_discarded) {
186 fflush(pos->fp);
187 fprintf(stderr, "[warning] Tracer discarded %d events between [",
188 stream->events_discarded);
189 ctf_print_timestamp(stderr, stream, stream->prev_timestamp);
190 fprintf(stderr, "] and [");
191 ctf_print_timestamp(stderr, stream, stream->prev_timestamp_end);
192 fprintf(stderr, "]. You should consider increasing the buffer size.\n");
193 fflush(stderr);
194 stream->events_discarded = 0;
195 }
196
197 if (stream->has_timestamp) {
198 set_field_names_print(pos, ITEM_HEADER);
199 if (pos->print_names)
200 fprintf(pos->fp, "timestamp = ");
201 else
202 fprintf(pos->fp, "[");
203 ctf_print_timestamp(pos->fp, stream, stream->timestamp);
204 if (!pos->print_names)
205 fprintf(pos->fp, "]");
206
207 if (pos->print_names)
208 fprintf(pos->fp, ", ");
209 else
210 fprintf(pos->fp, " ");
211 }
212 if ((opt_delta_field || opt_all_fields) && stream->has_timestamp) {
213 uint64_t delta, delta_sec, delta_nsec;
214
215 set_field_names_print(pos, ITEM_HEADER);
216 if (pos->print_names)
217 fprintf(pos->fp, "delta = ");
218 else
219 fprintf(pos->fp, "(");
220 if (pos->last_timestamp != -1ULL) {
221 delta = stream->timestamp - pos->last_timestamp;
222 delta_sec = delta / NSEC_PER_SEC;
223 delta_nsec = delta % NSEC_PER_SEC;
224 fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64,
225 delta_sec, delta_nsec);
226 } else {
227 fprintf(pos->fp, "+?.?????????");
228 }
229 if (!pos->print_names)
230 fprintf(pos->fp, ")");
231
232 if (pos->print_names)
233 fprintf(pos->fp, ", ");
234 else
235 fprintf(pos->fp, " ");
236 pos->last_timestamp = stream->timestamp;
237 }
238
239 if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') {
240 set_field_names_print(pos, ITEM_HEADER);
241 if (pos->print_names) {
242 fprintf(pos->fp, "trace = ");
243 }
244 fprintf(pos->fp, "%s", stream_class->trace->path);
245 if (pos->print_names)
246 fprintf(pos->fp, ", ");
247 else
248 fprintf(pos->fp, " ");
249 }
250 if ((opt_trace_domain_field && !opt_all_fields) && stream_class->trace->env.domain[0] != '\0') {
251 set_field_names_print(pos, ITEM_HEADER);
252 if (pos->print_names) {
253 fprintf(pos->fp, "trace:domain = ");
254 }
255 fprintf(pos->fp, "%s", stream_class->trace->env.domain);
256 if (pos->print_names)
257 fprintf(pos->fp, ", ");
258 dom_print = 1;
259 }
260 if ((opt_trace_procname_field && !opt_all_fields) && stream_class->trace->env.procname[0] != '\0') {
261 set_field_names_print(pos, ITEM_HEADER);
262 if (pos->print_names) {
263 fprintf(pos->fp, "trace:procname = ");
264 } else if (dom_print) {
265 fprintf(pos->fp, ":");
266 }
267 fprintf(pos->fp, "%s", stream_class->trace->env.procname);
268 if (pos->print_names)
269 fprintf(pos->fp, ", ");
270 dom_print = 1;
271 }
272 if ((opt_trace_vpid_field && !opt_all_fields) && stream_class->trace->env.vpid != -1) {
273 set_field_names_print(pos, ITEM_HEADER);
274 if (pos->print_names) {
275 fprintf(pos->fp, "trace:vpid = ");
276 } else if (dom_print) {
277 fprintf(pos->fp, ":");
278 }
279 fprintf(pos->fp, "%d", stream_class->trace->env.vpid);
280 if (pos->print_names)
281 fprintf(pos->fp, ", ");
282 dom_print = 1;
283 }
284 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) {
285 set_field_names_print(pos, ITEM_HEADER);
286 if (pos->print_names) {
287 fprintf(pos->fp, "loglevel = ");
288 } else if (dom_print) {
289 fprintf(pos->fp, ":");
290 }
291 fprintf(pos->fp, "(%d)",
292 event_class->loglevel);
293 if (pos->print_names)
294 fprintf(pos->fp, ", ");
295 dom_print = 1;
296 }
297 if (dom_print && !pos->print_names)
298 fprintf(pos->fp, " ");
299 set_field_names_print(pos, ITEM_HEADER);
300 if (pos->print_names)
301 fprintf(pos->fp, "name = ");
302 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
303 if (pos->print_names)
304 pos->field_nr++;
305 else
306 fprintf(pos->fp, ":");
307
308 /* print cpuid field from packet context */
309 if (stream->stream_packet_context) {
310 if (pos->field_nr++ != 0)
311 fprintf(pos->fp, ",");
312 set_field_names_print(pos, ITEM_SCOPE);
313 if (pos->print_names)
314 fprintf(pos->fp, " stream.packet.context =");
315 field_nr_saved = pos->field_nr;
316 pos->field_nr = 0;
317 set_field_names_print(pos, ITEM_CONTEXT);
318 ret = generic_rw(ppos, &stream->stream_packet_context->p);
319 if (ret)
320 goto error;
321 pos->field_nr = field_nr_saved;
322 }
323
324 /* Only show the event header in verbose mode */
325 if (babeltrace_verbose && stream->stream_event_header) {
326 if (pos->field_nr++ != 0)
327 fprintf(pos->fp, ",");
328 set_field_names_print(pos, ITEM_SCOPE);
329 if (pos->print_names)
330 fprintf(pos->fp, " stream.event.header =");
331 field_nr_saved = pos->field_nr;
332 pos->field_nr = 0;
333 set_field_names_print(pos, ITEM_CONTEXT);
334 ret = generic_rw(ppos, &stream->stream_event_header->p);
335 if (ret)
336 goto error;
337 pos->field_nr = field_nr_saved;
338 }
339
340 /* print stream-declared event context */
341 if (stream->stream_event_context) {
342 if (pos->field_nr++ != 0)
343 fprintf(pos->fp, ",");
344 set_field_names_print(pos, ITEM_SCOPE);
345 if (pos->print_names)
346 fprintf(pos->fp, " stream.event.context =");
347 field_nr_saved = pos->field_nr;
348 pos->field_nr = 0;
349 set_field_names_print(pos, ITEM_CONTEXT);
350 ret = generic_rw(ppos, &stream->stream_event_context->p);
351 if (ret)
352 goto error;
353 pos->field_nr = field_nr_saved;
354 }
355
356 /* print event-declared event context */
357 if (event->event_context) {
358 if (pos->field_nr++ != 0)
359 fprintf(pos->fp, ",");
360 set_field_names_print(pos, ITEM_SCOPE);
361 if (pos->print_names)
362 fprintf(pos->fp, " event.context =");
363 field_nr_saved = pos->field_nr;
364 pos->field_nr = 0;
365 set_field_names_print(pos, ITEM_CONTEXT);
366 ret = generic_rw(ppos, &event->event_context->p);
367 if (ret)
368 goto error;
369 pos->field_nr = field_nr_saved;
370 }
371
372 /* Read and print event payload */
373 if (event->event_fields) {
374 if (pos->field_nr++ != 0)
375 fprintf(pos->fp, ",");
376 set_field_names_print(pos, ITEM_SCOPE);
377 if (pos->print_names)
378 fprintf(pos->fp, " event.fields =");
379 field_nr_saved = pos->field_nr;
380 pos->field_nr = 0;
381 set_field_names_print(pos, ITEM_PAYLOAD);
382 ret = generic_rw(ppos, &event->event_fields->p);
383 if (ret)
384 goto error;
385 pos->field_nr = field_nr_saved;
386 }
387 /* newline */
388 fprintf(pos->fp, "\n");
389 pos->field_nr = 0;
390
391 return 0;
392
393 error:
394 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
395 return ret;
396 }
397
398
399 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
400 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
401 int whence), FILE *metadata_fp)
402 {
403 struct ctf_text_stream_pos *pos;
404 FILE *fp;
405
406 pos = g_new0(struct ctf_text_stream_pos, 1);
407
408 pos->last_timestamp = -1ULL;
409 switch (flags & O_ACCMODE) {
410 case O_RDWR:
411 if (!path)
412 fp = stdout;
413 else
414 fp = fopen(path, "w");
415 if (!fp)
416 goto error;
417 pos->fp = fp;
418 pos->parent.rw_table = write_dispatch_table;
419 pos->parent.event_cb = ctf_text_write_event;
420 pos->print_names = 0;
421 break;
422 case O_RDONLY:
423 default:
424 fprintf(stderr, "[error] Incorrect open flags.\n");
425 goto error;
426 }
427
428 return &pos->trace_descriptor;
429 error:
430 g_free(pos);
431 return NULL;
432 }
433
434 void ctf_text_close_trace(struct trace_descriptor *td)
435 {
436 struct ctf_text_stream_pos *pos =
437 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
438 fclose(pos->fp);
439 g_free(pos);
440 }
441
442 void __attribute__((constructor)) ctf_text_init(void)
443 {
444 int ret;
445
446 ctf_text_format.name = g_quark_from_static_string("text");
447 ret = bt_register_format(&ctf_text_format);
448 assert(!ret);
449 }
450
451 /* TODO: finalize */
This page took 0.039247 seconds and 4 git commands to generate.