Rename struct ctf_file_event to struct ctf_stream_event
[babeltrace.git] / formats / ctf-text / ctf-text.c
... / ...
CommitLineData
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>
21#include <babeltrace/ctf/metadata.h>
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
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
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
93static
94int ctf_text_write_event(struct stream_pos *ppos,
95 struct ctf_stream *stream)
96{
97 struct ctf_text_stream_pos *pos =
98 container_of(ppos, struct ctf_text_stream_pos, parent);
99 struct ctf_stream_class *stream_class = stream->stream_class;
100 int field_nr_saved;
101 struct ctf_event *event_class;
102 struct ctf_stream_event *event;
103 uint64_t id = 0;
104 int ret;
105
106 /* print event header */
107 if (stream->stream_event_header) {
108 struct definition_integer *integer_definition;
109 struct definition *variant;
110
111 /* lookup event id */
112 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
113 if (integer_definition) {
114 id = integer_definition->value._unsigned;
115 } else {
116 struct definition_enum *enum_definition;
117
118 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
119 if (enum_definition) {
120 id = enum_definition->integer->value._unsigned;
121 }
122 }
123
124 variant = lookup_variant(&stream->stream_event_header->p, "v");
125 if (variant) {
126 integer_definition = lookup_integer(variant, "id", FALSE);
127 if (integer_definition) {
128 id = integer_definition->value._unsigned;
129 }
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 }
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 }
142 event_class = g_ptr_array_index(stream_class->events_by_id, id);
143 if (!event) {
144 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
145 return -EINVAL;
146 }
147
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, "]");
156
157 if (pos->print_names)
158 fprintf(pos->fp, ", ");
159 else
160 fprintf(pos->fp, " ");
161 }
162 if (pos->print_names)
163 fprintf(pos->fp, "name = ");
164 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
165 if (pos->print_names)
166 pos->field_nr++;
167 else
168 fprintf(pos->fp, ":");
169
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
184 /* Only show the event header in verbose mode */
185 if (babeltrace_verbose && stream->stream_event_header) {
186 if (pos->field_nr++ != 0)
187 fprintf(pos->fp, ",");
188 if (pos->print_names)
189 fprintf(pos->fp, " stream.event.header =");
190 field_nr_saved = pos->field_nr;
191 pos->field_nr = 0;
192 ret = generic_rw(ppos, &stream->stream_event_header->p);
193 if (ret)
194 goto error;
195 pos->field_nr = field_nr_saved;
196 }
197
198 /* print stream-declared event context */
199 if (stream->stream_event_context) {
200 if (pos->field_nr++ != 0)
201 fprintf(pos->fp, ",");
202 if (pos->print_names)
203 fprintf(pos->fp, " stream.event.context =");
204 field_nr_saved = pos->field_nr;
205 pos->field_nr = 0;
206 ret = generic_rw(ppos, &stream->stream_event_context->p);
207 if (ret)
208 goto error;
209 pos->field_nr = field_nr_saved;
210 }
211
212 /* print event-declared event context */
213 if (event->event_context) {
214 if (pos->field_nr++ != 0)
215 fprintf(pos->fp, ",");
216 if (pos->print_names)
217 fprintf(pos->fp, " event.context =");
218 field_nr_saved = pos->field_nr;
219 pos->field_nr = 0;
220 ret = generic_rw(ppos, &event->event_context->p);
221 if (ret)
222 goto error;
223 pos->field_nr = field_nr_saved;
224 }
225
226 /* Read and print event payload */
227 if (event->event_fields) {
228 if (pos->field_nr++ != 0)
229 fprintf(pos->fp, ",");
230 if (pos->print_names)
231 fprintf(pos->fp, " event.fields =");
232 field_nr_saved = pos->field_nr;
233 pos->field_nr = 0;
234 ret = generic_rw(ppos, &event->event_fields->p);
235 if (ret)
236 goto error;
237 pos->field_nr = field_nr_saved;
238 }
239 /* newline */
240 fprintf(pos->fp, "\n");
241 pos->field_nr = 0;
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
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) {
259 case O_RDWR:
260 if (!path)
261 fp = stdout;
262 else
263 fp = fopen(path, "w");
264 if (!fp)
265 goto error;
266 pos->fp = fp;
267 pos->parent.rw_table = write_dispatch_table;
268 pos->parent.event_cb = ctf_text_write_event;
269 pos->print_names = opt_field_names;
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.030473 seconds and 4 git commands to generate.