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