19e92fcabdf836fe909c703450f0133b8847b6a2
[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_trace_name,
45 opt_trace_domain,
46 opt_trace_procname,
47 opt_trace_vpid,
48 opt_loglevel;
49
50 enum field_item {
51 ITEM_SCOPE,
52 ITEM_HEADER,
53 ITEM_CONTEXT,
54 ITEM_PAYLOAD,
55 };
56
57 struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
58 const char *path, int flags,
59 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
60 int whence), FILE *metadata_fp);
61 void ctf_text_close_trace(struct trace_descriptor *descriptor);
62
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
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
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
152 static
153 int ctf_text_write_event(struct stream_pos *ppos,
154 struct ctf_stream *stream)
155 {
156 struct ctf_text_stream_pos *pos =
157 container_of(ppos, struct ctf_text_stream_pos, parent);
158 struct ctf_stream_class *stream_class = stream->stream_class;
159 int field_nr_saved;
160 struct ctf_event *event_class;
161 struct ctf_stream_event *event;
162 uint64_t id;
163 int ret;
164 int dom_print = 0;
165
166 id = stream->event_id;
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 }
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 }
177 event_class = g_ptr_array_index(stream_class->events_by_id, id);
178 if (!event) {
179 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
180 return -EINVAL;
181 }
182
183 if (stream->has_timestamp) {
184 uint64_t ts_sec, ts_nsec;
185
186 ts_sec = stream->timestamp / NSEC_PER_SEC;
187 ts_nsec = stream->timestamp % NSEC_PER_SEC;
188 set_field_names_print(pos, ITEM_HEADER);
189 if (pos->print_names)
190 fprintf(pos->fp, "timestamp = ");
191 else
192 fprintf(pos->fp, "[");
193 fprintf(pos->fp, "%3" PRIu64 ".%09" PRIu64,
194 ts_sec, ts_nsec);
195 if (!pos->print_names)
196 fprintf(pos->fp, "]");
197
198 if (pos->print_names)
199 fprintf(pos->fp, ", ");
200 else
201 fprintf(pos->fp, " ");
202 }
203 if ((opt_trace_name || opt_all_field_names) && stream_class->trace->path[0] != '\0') {
204 set_field_names_print(pos, ITEM_HEADER);
205 if (pos->print_names) {
206 if (opt_trace_name || opt_all_field_names)
207 fprintf(pos->fp, "trace = ");
208 }
209
210 fprintf(pos->fp, "%s", stream_class->trace->path);
211 if (pos->print_names)
212 fprintf(pos->fp, ", ");
213 else
214 fprintf(pos->fp, " ");
215 }
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 }
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 }
270 if (dom_print && !pos->print_names)
271 fprintf(pos->fp, " ");
272 set_field_names_print(pos, ITEM_HEADER);
273 if (pos->print_names)
274 fprintf(pos->fp, "name = ");
275 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
276 if (pos->print_names)
277 pos->field_nr++;
278 else
279 fprintf(pos->fp, ":");
280
281 /* print cpuid field from packet context */
282 if (stream->stream_packet_context) {
283 if (pos->field_nr++ != 0)
284 fprintf(pos->fp, ",");
285 set_field_names_print(pos, ITEM_SCOPE);
286 if (pos->print_names)
287 fprintf(pos->fp, " stream.packet.context =");
288 field_nr_saved = pos->field_nr;
289 pos->field_nr = 0;
290 set_field_names_print(pos, ITEM_CONTEXT);
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
297 /* Only show the event header in verbose mode */
298 if (babeltrace_verbose && stream->stream_event_header) {
299 if (pos->field_nr++ != 0)
300 fprintf(pos->fp, ",");
301 set_field_names_print(pos, ITEM_SCOPE);
302 if (pos->print_names)
303 fprintf(pos->fp, " stream.event.header =");
304 field_nr_saved = pos->field_nr;
305 pos->field_nr = 0;
306 set_field_names_print(pos, ITEM_CONTEXT);
307 ret = generic_rw(ppos, &stream->stream_event_header->p);
308 if (ret)
309 goto error;
310 pos->field_nr = field_nr_saved;
311 }
312
313 /* print stream-declared event context */
314 if (stream->stream_event_context) {
315 if (pos->field_nr++ != 0)
316 fprintf(pos->fp, ",");
317 set_field_names_print(pos, ITEM_SCOPE);
318 if (pos->print_names)
319 fprintf(pos->fp, " stream.event.context =");
320 field_nr_saved = pos->field_nr;
321 pos->field_nr = 0;
322 set_field_names_print(pos, ITEM_CONTEXT);
323 ret = generic_rw(ppos, &stream->stream_event_context->p);
324 if (ret)
325 goto error;
326 pos->field_nr = field_nr_saved;
327 }
328
329 /* print event-declared event context */
330 if (event->event_context) {
331 if (pos->field_nr++ != 0)
332 fprintf(pos->fp, ",");
333 set_field_names_print(pos, ITEM_SCOPE);
334 if (pos->print_names)
335 fprintf(pos->fp, " event.context =");
336 field_nr_saved = pos->field_nr;
337 pos->field_nr = 0;
338 set_field_names_print(pos, ITEM_CONTEXT);
339 ret = generic_rw(ppos, &event->event_context->p);
340 if (ret)
341 goto error;
342 pos->field_nr = field_nr_saved;
343 }
344
345 /* Read and print event payload */
346 if (event->event_fields) {
347 if (pos->field_nr++ != 0)
348 fprintf(pos->fp, ",");
349 set_field_names_print(pos, ITEM_SCOPE);
350 if (pos->print_names)
351 fprintf(pos->fp, " event.fields =");
352 field_nr_saved = pos->field_nr;
353 pos->field_nr = 0;
354 set_field_names_print(pos, ITEM_PAYLOAD);
355 ret = generic_rw(ppos, &event->event_fields->p);
356 if (ret)
357 goto error;
358 pos->field_nr = field_nr_saved;
359 }
360 /* newline */
361 fprintf(pos->fp, "\n");
362 pos->field_nr = 0;
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
372 struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
373 const char *path, int flags,
374 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
375 int whence), FILE *metadata_fp)
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) {
383 case O_RDWR:
384 if (!path)
385 fp = stdout;
386 else
387 fp = fopen(path, "w");
388 if (!fp)
389 goto error;
390 pos->fp = fp;
391 pos->parent.rw_table = write_dispatch_table;
392 pos->parent.event_cb = ctf_text_write_event;
393 pos->print_names = 0;
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 */
This page took 0.036475 seconds and 3 git commands to generate.