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