Free the callbacks on iter_destroy
[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 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
642ec66d
MD
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>
31262354 23#include <babeltrace/ctf/metadata.h>
70bd0a12 24#include <babeltrace/babeltrace-internal.h>
642ec66d
MD
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
37struct trace_descriptor *ctf_text_open_trace(const char *path, int flags);
38void ctf_text_close_trace(struct trace_descriptor *descriptor);
39
642ec66d
MD
40static
41rw_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
52static
53struct format ctf_text_format = {
54 .open_trace = ctf_text_open_trace,
55 .close_trace = ctf_text_close_trace,
56};
57
d335f0f7
MD
58static 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
64static
65void __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
74int 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
31262354
MD
95static
96int ctf_text_write_event(struct stream_pos *ppos,
764af3f4 97 struct ctf_stream *stream)
31262354
MD
98{
99 struct ctf_text_stream_pos *pos =
100 container_of(ppos, struct ctf_text_stream_pos, parent);
764af3f4 101 struct ctf_stream_class *stream_class = stream->stream_class;
fd3382e8 102 int field_nr_saved;
31262354 103 struct ctf_event *event_class;
42dc00b7 104 struct ctf_stream_event *event;
c87a8eb2 105 uint64_t id;
31262354
MD
106 int ret;
107
c87a8eb2 108 id = stream->event_id;
31262354
MD
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 }
e28d4618
MD
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 }
31262354 119 event_class = g_ptr_array_index(stream_class->events_by_id, id);
e28d4618 120 if (!event) {
31262354
MD
121 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
122 return -EINVAL;
123 }
124
5e2eb0ae 125 if (stream->has_timestamp) {
be60c7fb
MD
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, "]");
aa6bffae 133
be60c7fb
MD
134 if (pos->print_names)
135 fprintf(pos->fp, ", ");
136 else
137 fprintf(pos->fp, " ");
138 }
31262354
MD
139 if (pos->print_names)
140 fprintf(pos->fp, "name = ");
8178fe48
MD
141 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
142 if (pos->print_names)
fd3382e8 143 pos->field_nr++;
8178fe48 144 else
fd3382e8 145 fprintf(pos->fp, ":");
31262354 146
e28d4618
MD
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
764af3f4 161 /* Only show the event header in verbose mode */
e28d4618 162 if (babeltrace_verbose && stream->stream_event_header) {
fd3382e8
MD
163 if (pos->field_nr++ != 0)
164 fprintf(pos->fp, ",");
31262354 165 if (pos->print_names)
fd3382e8
MD
166 fprintf(pos->fp, " stream.event.header =");
167 field_nr_saved = pos->field_nr;
168 pos->field_nr = 0;
e28d4618 169 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
170 if (ret)
171 goto error;
fd3382e8 172 pos->field_nr = field_nr_saved;
31262354
MD
173 }
174
175 /* print stream-declared event context */
e28d4618 176 if (stream->stream_event_context) {
fd3382e8
MD
177 if (pos->field_nr++ != 0)
178 fprintf(pos->fp, ",");
31262354 179 if (pos->print_names)
fd3382e8
MD
180 fprintf(pos->fp, " stream.event.context =");
181 field_nr_saved = pos->field_nr;
182 pos->field_nr = 0;
e28d4618 183 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
184 if (ret)
185 goto error;
fd3382e8 186 pos->field_nr = field_nr_saved;
31262354
MD
187 }
188
189 /* print event-declared event context */
e28d4618 190 if (event->event_context) {
fd3382e8
MD
191 if (pos->field_nr++ != 0)
192 fprintf(pos->fp, ",");
31262354 193 if (pos->print_names)
fd3382e8
MD
194 fprintf(pos->fp, " event.context =");
195 field_nr_saved = pos->field_nr;
196 pos->field_nr = 0;
e28d4618 197 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
198 if (ret)
199 goto error;
fd3382e8 200 pos->field_nr = field_nr_saved;
31262354
MD
201 }
202
203 /* Read and print event payload */
e28d4618 204 if (event->event_fields) {
fd3382e8
MD
205 if (pos->field_nr++ != 0)
206 fprintf(pos->fp, ",");
31262354 207 if (pos->print_names)
fd3382e8
MD
208 fprintf(pos->fp, " event.fields =");
209 field_nr_saved = pos->field_nr;
210 pos->field_nr = 0;
e28d4618 211 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
212 if (ret)
213 goto error;
fd3382e8 214 pos->field_nr = field_nr_saved;
31262354 215 }
fd3382e8 216 /* newline */
31262354 217 fprintf(pos->fp, "\n");
fd3382e8 218 pos->field_nr = 0;
31262354
MD
219
220 return 0;
221
222error:
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
642ec66d
MD
228struct 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) {
989c73bc 236 case O_RDWR:
b61922b5 237 if (!path)
6cf7957b
MD
238 fp = stdout;
239 else
240 fp = fopen(path, "w");
642ec66d
MD
241 if (!fp)
242 goto error;
243 pos->fp = fp;
244 pos->parent.rw_table = write_dispatch_table;
31262354 245 pos->parent.event_cb = ctf_text_write_event;
d63ca2cd 246 pos->print_names = opt_field_names;
642ec66d
MD
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;
255error:
256 g_free(pos);
257 return NULL;
258}
259
260void 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
268void __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.052539 seconds and 4 git commands to generate.