Rename struct ctf_file_event to struct ctf_stream_event
[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 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19#include <babeltrace/format.h>
20#include <babeltrace/ctf-text/types.h>
31262354 21#include <babeltrace/ctf/metadata.h>
642ec66d
MD
22#include <babeltrace/babeltrace.h>
23#include <inttypes.h>
24#include <uuid/uuid.h>
25#include <sys/mman.h>
26#include <errno.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <dirent.h>
31#include <glib.h>
32#include <unistd.h>
33#include <stdlib.h>
34
35struct trace_descriptor *ctf_text_open_trace(const char *path, int flags);
36void ctf_text_close_trace(struct trace_descriptor *descriptor);
37
642ec66d
MD
38static
39rw_dispatch write_dispatch_table[] = {
40 [ CTF_TYPE_INTEGER ] = ctf_text_integer_write,
41 [ CTF_TYPE_FLOAT ] = ctf_text_float_write,
42 [ CTF_TYPE_ENUM ] = ctf_text_enum_write,
43 [ CTF_TYPE_STRING ] = ctf_text_string_write,
44 [ CTF_TYPE_STRUCT ] = ctf_text_struct_write,
45 [ CTF_TYPE_VARIANT ] = ctf_text_variant_write,
46 [ CTF_TYPE_ARRAY ] = ctf_text_array_write,
47 [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write,
48};
49
50static
51struct format ctf_text_format = {
52 .open_trace = ctf_text_open_trace,
53 .close_trace = ctf_text_close_trace,
54};
55
d335f0f7
MD
56static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN,
57 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END,
58 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED,
59 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE,
60 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE;
61
62static
63void __attribute__((constructor)) init_quarks(void)
64{
65 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
66 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
67 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
68 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
69 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
70}
71
72int print_field(struct definition *definition)
73{
74 /* Print all fields in verbose mode */
75 if (babeltrace_verbose)
76 return 1;
77
78 /* Filter out part of the packet context */
79 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
80 return 0;
81 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
82 return 0;
83 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
84 return 0;
85 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
86 return 0;
87 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
88 return 0;
89
90 return 1;
91}
92
31262354
MD
93static
94int ctf_text_write_event(struct stream_pos *ppos,
764af3f4 95 struct ctf_stream *stream)
31262354
MD
96{
97 struct ctf_text_stream_pos *pos =
98 container_of(ppos, struct ctf_text_stream_pos, parent);
764af3f4 99 struct ctf_stream_class *stream_class = stream->stream_class;
fd3382e8 100 int field_nr_saved;
31262354 101 struct ctf_event *event_class;
42dc00b7 102 struct ctf_stream_event *event;
31262354 103 uint64_t id = 0;
31262354
MD
104 int ret;
105
106 /* print event header */
e28d4618 107 if (stream->stream_event_header) {
ccdb988e
MD
108 struct definition_integer *integer_definition;
109 struct definition *variant;
110
31262354 111 /* lookup event id */
e28d4618 112 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
ccdb988e
MD
113 if (integer_definition) {
114 id = integer_definition->value._unsigned;
115 } else {
116 struct definition_enum *enum_definition;
117
e28d4618 118 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
ccdb988e
MD
119 if (enum_definition) {
120 id = enum_definition->integer->value._unsigned;
121 }
122 }
31262354 123
e28d4618 124 variant = lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e
MD
125 if (variant) {
126 integer_definition = lookup_integer(variant, "id", FALSE);
127 if (integer_definition) {
128 id = integer_definition->value._unsigned;
129 }
31262354
MD
130 }
131 }
132
133 if (id >= stream_class->events_by_id->len) {
134 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
135 return -EINVAL;
136 }
e28d4618
MD
137 event = g_ptr_array_index(stream->events_by_id, id);
138 if (!event) {
139 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
140 return -EINVAL;
141 }
31262354 142 event_class = g_ptr_array_index(stream_class->events_by_id, id);
e28d4618 143 if (!event) {
31262354
MD
144 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
145 return -EINVAL;
146 }
147
be60c7fb
MD
148 if (stream->timestamp) {
149 if (pos->print_names)
150 fprintf(pos->fp, "timestamp = ");
151 else
152 fprintf(pos->fp, "[");
153 fprintf(pos->fp, "%12" PRIu64, stream->timestamp);
154 if (!pos->print_names)
155 fprintf(pos->fp, "]");
aa6bffae 156
be60c7fb
MD
157 if (pos->print_names)
158 fprintf(pos->fp, ", ");
159 else
160 fprintf(pos->fp, " ");
161 }
31262354
MD
162 if (pos->print_names)
163 fprintf(pos->fp, "name = ");
8178fe48
MD
164 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
165 if (pos->print_names)
fd3382e8 166 pos->field_nr++;
8178fe48 167 else
fd3382e8 168 fprintf(pos->fp, ":");
31262354 169
e28d4618
MD
170 /* print cpuid field from packet context */
171 if (stream->stream_packet_context) {
172 if (pos->field_nr++ != 0)
173 fprintf(pos->fp, ",");
174 if (pos->print_names)
175 fprintf(pos->fp, " stream.packet.context =");
176 field_nr_saved = pos->field_nr;
177 pos->field_nr = 0;
178 ret = generic_rw(ppos, &stream->stream_packet_context->p);
179 if (ret)
180 goto error;
181 pos->field_nr = field_nr_saved;
182 }
183
764af3f4 184 /* Only show the event header in verbose mode */
e28d4618 185 if (babeltrace_verbose && stream->stream_event_header) {
fd3382e8
MD
186 if (pos->field_nr++ != 0)
187 fprintf(pos->fp, ",");
31262354 188 if (pos->print_names)
fd3382e8
MD
189 fprintf(pos->fp, " stream.event.header =");
190 field_nr_saved = pos->field_nr;
191 pos->field_nr = 0;
e28d4618 192 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
193 if (ret)
194 goto error;
fd3382e8 195 pos->field_nr = field_nr_saved;
31262354
MD
196 }
197
198 /* print stream-declared event context */
e28d4618 199 if (stream->stream_event_context) {
fd3382e8
MD
200 if (pos->field_nr++ != 0)
201 fprintf(pos->fp, ",");
31262354 202 if (pos->print_names)
fd3382e8
MD
203 fprintf(pos->fp, " stream.event.context =");
204 field_nr_saved = pos->field_nr;
205 pos->field_nr = 0;
e28d4618 206 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
207 if (ret)
208 goto error;
fd3382e8 209 pos->field_nr = field_nr_saved;
31262354
MD
210 }
211
212 /* print event-declared event context */
e28d4618 213 if (event->event_context) {
fd3382e8
MD
214 if (pos->field_nr++ != 0)
215 fprintf(pos->fp, ",");
31262354 216 if (pos->print_names)
fd3382e8
MD
217 fprintf(pos->fp, " event.context =");
218 field_nr_saved = pos->field_nr;
219 pos->field_nr = 0;
e28d4618 220 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
221 if (ret)
222 goto error;
fd3382e8 223 pos->field_nr = field_nr_saved;
31262354
MD
224 }
225
226 /* Read and print event payload */
e28d4618 227 if (event->event_fields) {
fd3382e8
MD
228 if (pos->field_nr++ != 0)
229 fprintf(pos->fp, ",");
31262354 230 if (pos->print_names)
fd3382e8
MD
231 fprintf(pos->fp, " event.fields =");
232 field_nr_saved = pos->field_nr;
233 pos->field_nr = 0;
e28d4618 234 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
235 if (ret)
236 goto error;
fd3382e8 237 pos->field_nr = field_nr_saved;
31262354 238 }
fd3382e8 239 /* newline */
31262354 240 fprintf(pos->fp, "\n");
fd3382e8 241 pos->field_nr = 0;
31262354
MD
242
243 return 0;
244
245error:
246 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
247 return ret;
248}
249
250
642ec66d
MD
251struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
252{
253 struct ctf_text_stream_pos *pos;
254 FILE *fp;
255
256 pos = g_new0(struct ctf_text_stream_pos, 1);
257
258 switch (flags & O_ACCMODE) {
989c73bc 259 case O_RDWR:
b61922b5 260 if (!path)
6cf7957b
MD
261 fp = stdout;
262 else
263 fp = fopen(path, "w");
642ec66d
MD
264 if (!fp)
265 goto error;
266 pos->fp = fp;
267 pos->parent.rw_table = write_dispatch_table;
31262354 268 pos->parent.event_cb = ctf_text_write_event;
d63ca2cd 269 pos->print_names = opt_field_names;
642ec66d
MD
270 break;
271 case O_RDONLY:
272 default:
273 fprintf(stdout, "[error] Incorrect open flags.\n");
274 goto error;
275 }
276
277 return &pos->trace_descriptor;
278error:
279 g_free(pos);
280 return NULL;
281}
282
283void ctf_text_close_trace(struct trace_descriptor *td)
284{
285 struct ctf_text_stream_pos *pos =
286 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
287 fclose(pos->fp);
288 g_free(pos);
289}
290
291void __attribute__((constructor)) ctf_text_init(void)
292{
293 int ret;
294
295 ctf_text_format.name = g_quark_from_static_string("text");
296 ret = bt_register_format(&ctf_text_format);
297 assert(!ret);
298}
299
300/* TODO: finalize */
This page took 0.054569 seconds and 4 git commands to generate.