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