Save current event id
[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.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 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags);
38 void ctf_text_close_trace(struct trace_descriptor *descriptor);
39
40 static
41 rw_dispatch write_dispatch_table[] = {
42 [ CTF_TYPE_INTEGER ] = ctf_text_integer_write,
43 [ CTF_TYPE_FLOAT ] = ctf_text_float_write,
44 [ CTF_TYPE_ENUM ] = ctf_text_enum_write,
45 [ CTF_TYPE_STRING ] = ctf_text_string_write,
46 [ CTF_TYPE_STRUCT ] = ctf_text_struct_write,
47 [ CTF_TYPE_VARIANT ] = ctf_text_variant_write,
48 [ CTF_TYPE_ARRAY ] = ctf_text_array_write,
49 [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write,
50 };
51
52 static
53 struct format ctf_text_format = {
54 .open_trace = ctf_text_open_trace,
55 .close_trace = ctf_text_close_trace,
56 };
57
58 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN,
59 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END,
60 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED,
61 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE,
62 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE;
63
64 static
65 void __attribute__((constructor)) init_quarks(void)
66 {
67 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
68 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
69 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
70 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
71 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
72 }
73
74 int print_field(struct definition *definition)
75 {
76 /* Print all fields in verbose mode */
77 if (babeltrace_verbose)
78 return 1;
79
80 /* Filter out part of the packet context */
81 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
82 return 0;
83 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
84 return 0;
85 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
86 return 0;
87 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
88 return 0;
89 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
90 return 0;
91
92 return 1;
93 }
94
95 static
96 int ctf_text_write_event(struct stream_pos *ppos,
97 struct ctf_stream *stream)
98 {
99 struct ctf_text_stream_pos *pos =
100 container_of(ppos, struct ctf_text_stream_pos, parent);
101 struct ctf_stream_class *stream_class = stream->stream_class;
102 int field_nr_saved;
103 struct ctf_event *event_class;
104 struct ctf_stream_event *event;
105 uint64_t id;
106 int ret;
107
108 id = stream->event_id;
109
110 if (id >= stream_class->events_by_id->len) {
111 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
112 return -EINVAL;
113 }
114 event = g_ptr_array_index(stream->events_by_id, id);
115 if (!event) {
116 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
117 return -EINVAL;
118 }
119 event_class = g_ptr_array_index(stream_class->events_by_id, id);
120 if (!event) {
121 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
122 return -EINVAL;
123 }
124
125 if (stream->has_timestamp) {
126 if (pos->print_names)
127 fprintf(pos->fp, "timestamp = ");
128 else
129 fprintf(pos->fp, "[");
130 fprintf(pos->fp, "%12" PRIu64, stream->timestamp);
131 if (!pos->print_names)
132 fprintf(pos->fp, "]");
133
134 if (pos->print_names)
135 fprintf(pos->fp, ", ");
136 else
137 fprintf(pos->fp, " ");
138 }
139 if (pos->print_names)
140 fprintf(pos->fp, "name = ");
141 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
142 if (pos->print_names)
143 pos->field_nr++;
144 else
145 fprintf(pos->fp, ":");
146
147 /* print cpuid field from packet context */
148 if (stream->stream_packet_context) {
149 if (pos->field_nr++ != 0)
150 fprintf(pos->fp, ",");
151 if (pos->print_names)
152 fprintf(pos->fp, " stream.packet.context =");
153 field_nr_saved = pos->field_nr;
154 pos->field_nr = 0;
155 ret = generic_rw(ppos, &stream->stream_packet_context->p);
156 if (ret)
157 goto error;
158 pos->field_nr = field_nr_saved;
159 }
160
161 /* Only show the event header in verbose mode */
162 if (babeltrace_verbose && stream->stream_event_header) {
163 if (pos->field_nr++ != 0)
164 fprintf(pos->fp, ",");
165 if (pos->print_names)
166 fprintf(pos->fp, " stream.event.header =");
167 field_nr_saved = pos->field_nr;
168 pos->field_nr = 0;
169 ret = generic_rw(ppos, &stream->stream_event_header->p);
170 if (ret)
171 goto error;
172 pos->field_nr = field_nr_saved;
173 }
174
175 /* print stream-declared event context */
176 if (stream->stream_event_context) {
177 if (pos->field_nr++ != 0)
178 fprintf(pos->fp, ",");
179 if (pos->print_names)
180 fprintf(pos->fp, " stream.event.context =");
181 field_nr_saved = pos->field_nr;
182 pos->field_nr = 0;
183 ret = generic_rw(ppos, &stream->stream_event_context->p);
184 if (ret)
185 goto error;
186 pos->field_nr = field_nr_saved;
187 }
188
189 /* print event-declared event context */
190 if (event->event_context) {
191 if (pos->field_nr++ != 0)
192 fprintf(pos->fp, ",");
193 if (pos->print_names)
194 fprintf(pos->fp, " event.context =");
195 field_nr_saved = pos->field_nr;
196 pos->field_nr = 0;
197 ret = generic_rw(ppos, &event->event_context->p);
198 if (ret)
199 goto error;
200 pos->field_nr = field_nr_saved;
201 }
202
203 /* Read and print event payload */
204 if (event->event_fields) {
205 if (pos->field_nr++ != 0)
206 fprintf(pos->fp, ",");
207 if (pos->print_names)
208 fprintf(pos->fp, " event.fields =");
209 field_nr_saved = pos->field_nr;
210 pos->field_nr = 0;
211 ret = generic_rw(ppos, &event->event_fields->p);
212 if (ret)
213 goto error;
214 pos->field_nr = field_nr_saved;
215 }
216 /* newline */
217 fprintf(pos->fp, "\n");
218 pos->field_nr = 0;
219
220 return 0;
221
222 error:
223 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
224 return ret;
225 }
226
227
228 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
229 {
230 struct ctf_text_stream_pos *pos;
231 FILE *fp;
232
233 pos = g_new0(struct ctf_text_stream_pos, 1);
234
235 switch (flags & O_ACCMODE) {
236 case O_RDWR:
237 if (!path)
238 fp = stdout;
239 else
240 fp = fopen(path, "w");
241 if (!fp)
242 goto error;
243 pos->fp = fp;
244 pos->parent.rw_table = write_dispatch_table;
245 pos->parent.event_cb = ctf_text_write_event;
246 pos->print_names = opt_field_names;
247 break;
248 case O_RDONLY:
249 default:
250 fprintf(stdout, "[error] Incorrect open flags.\n");
251 goto error;
252 }
253
254 return &pos->trace_descriptor;
255 error:
256 g_free(pos);
257 return NULL;
258 }
259
260 void ctf_text_close_trace(struct trace_descriptor *td)
261 {
262 struct ctf_text_stream_pos *pos =
263 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
264 fclose(pos->fp);
265 g_free(pos);
266 }
267
268 void __attribute__((constructor)) ctf_text_init(void)
269 {
270 int ret;
271
272 ctf_text_format.name = g_quark_from_static_string("text");
273 ret = bt_register_format(&ctf_text_format);
274 assert(!ret);
275 }
276
277 /* TODO: finalize */
This page took 0.034393 seconds and 4 git commands to generate.