Fix compile warning in semantic validator
[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 *
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>
31262354 21#include <babeltrace/ctf/metadata.h>
642ec66d
MD
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
642ec66d
MD
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
31262354
MD
56static
57int ctf_text_write_event(struct stream_pos *ppos,
764af3f4 58 struct ctf_stream *stream)
31262354
MD
59{
60 struct ctf_text_stream_pos *pos =
61 container_of(ppos, struct ctf_text_stream_pos, parent);
764af3f4 62 struct ctf_stream_class *stream_class = stream->stream_class;
fd3382e8 63 int field_nr_saved;
31262354 64 struct ctf_event *event_class;
e28d4618 65 struct ctf_file_event *event;
31262354 66 uint64_t id = 0;
31262354
MD
67 int ret;
68
69 /* print event header */
e28d4618 70 if (stream->stream_event_header) {
ccdb988e
MD
71 struct definition_integer *integer_definition;
72 struct definition *variant;
73
31262354 74 /* lookup event id */
e28d4618 75 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
ccdb988e
MD
76 if (integer_definition) {
77 id = integer_definition->value._unsigned;
78 } else {
79 struct definition_enum *enum_definition;
80
e28d4618 81 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
ccdb988e
MD
82 if (enum_definition) {
83 id = enum_definition->integer->value._unsigned;
84 }
85 }
31262354 86
e28d4618 87 variant = lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e
MD
88 if (variant) {
89 integer_definition = lookup_integer(variant, "id", FALSE);
90 if (integer_definition) {
91 id = integer_definition->value._unsigned;
92 }
31262354
MD
93 }
94 }
95
96 if (id >= stream_class->events_by_id->len) {
97 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
98 return -EINVAL;
99 }
e28d4618
MD
100 event = g_ptr_array_index(stream->events_by_id, id);
101 if (!event) {
102 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
103 return -EINVAL;
104 }
31262354 105 event_class = g_ptr_array_index(stream_class->events_by_id, id);
e28d4618 106 if (!event) {
31262354
MD
107 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
108 return -EINVAL;
109 }
110
be60c7fb
MD
111 if (stream->timestamp) {
112 if (pos->print_names)
113 fprintf(pos->fp, "timestamp = ");
114 else
115 fprintf(pos->fp, "[");
116 fprintf(pos->fp, "%12" PRIu64, stream->timestamp);
117 if (!pos->print_names)
118 fprintf(pos->fp, "]");
aa6bffae 119
be60c7fb
MD
120 if (pos->print_names)
121 fprintf(pos->fp, ", ");
122 else
123 fprintf(pos->fp, " ");
124 }
31262354
MD
125 if (pos->print_names)
126 fprintf(pos->fp, "name = ");
8178fe48
MD
127 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
128 if (pos->print_names)
fd3382e8 129 pos->field_nr++;
8178fe48 130 else
fd3382e8 131 fprintf(pos->fp, ":");
31262354 132
e28d4618
MD
133 /* print cpuid field from packet context */
134 if (stream->stream_packet_context) {
135 if (pos->field_nr++ != 0)
136 fprintf(pos->fp, ",");
137 if (pos->print_names)
138 fprintf(pos->fp, " stream.packet.context =");
139 field_nr_saved = pos->field_nr;
140 pos->field_nr = 0;
141 ret = generic_rw(ppos, &stream->stream_packet_context->p);
142 if (ret)
143 goto error;
144 pos->field_nr = field_nr_saved;
145 }
146
764af3f4 147 /* Only show the event header in verbose mode */
e28d4618 148 if (babeltrace_verbose && stream->stream_event_header) {
fd3382e8
MD
149 if (pos->field_nr++ != 0)
150 fprintf(pos->fp, ",");
31262354 151 if (pos->print_names)
fd3382e8
MD
152 fprintf(pos->fp, " stream.event.header =");
153 field_nr_saved = pos->field_nr;
154 pos->field_nr = 0;
e28d4618 155 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
156 if (ret)
157 goto error;
fd3382e8 158 pos->field_nr = field_nr_saved;
31262354
MD
159 }
160
161 /* print stream-declared event context */
e28d4618 162 if (stream->stream_event_context) {
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.context =");
167 field_nr_saved = pos->field_nr;
168 pos->field_nr = 0;
e28d4618 169 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
170 if (ret)
171 goto error;
fd3382e8 172 pos->field_nr = field_nr_saved;
31262354
MD
173 }
174
175 /* print event-declared event context */
e28d4618 176 if (event->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, " event.context =");
181 field_nr_saved = pos->field_nr;
182 pos->field_nr = 0;
e28d4618 183 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
184 if (ret)
185 goto error;
fd3382e8 186 pos->field_nr = field_nr_saved;
31262354
MD
187 }
188
189 /* Read and print event payload */
e28d4618 190 if (event->event_fields) {
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.fields =");
195 field_nr_saved = pos->field_nr;
196 pos->field_nr = 0;
e28d4618 197 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
198 if (ret)
199 goto error;
fd3382e8 200 pos->field_nr = field_nr_saved;
31262354 201 }
fd3382e8 202 /* newline */
31262354 203 fprintf(pos->fp, "\n");
fd3382e8 204 pos->field_nr = 0;
31262354
MD
205
206 return 0;
207
208error:
209 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
210 return ret;
211}
212
213
642ec66d
MD
214struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
215{
216 struct ctf_text_stream_pos *pos;
217 FILE *fp;
218
219 pos = g_new0(struct ctf_text_stream_pos, 1);
220
221 switch (flags & O_ACCMODE) {
989c73bc 222 case O_RDWR:
b61922b5 223 if (!path)
6cf7957b
MD
224 fp = stdout;
225 else
226 fp = fopen(path, "w");
642ec66d
MD
227 if (!fp)
228 goto error;
229 pos->fp = fp;
230 pos->parent.rw_table = write_dispatch_table;
31262354 231 pos->parent.event_cb = ctf_text_write_event;
d63ca2cd 232 pos->print_names = opt_field_names;
642ec66d
MD
233 break;
234 case O_RDONLY:
235 default:
236 fprintf(stdout, "[error] Incorrect open flags.\n");
237 goto error;
238 }
239
240 return &pos->trace_descriptor;
241error:
242 g_free(pos);
243 return NULL;
244}
245
246void ctf_text_close_trace(struct trace_descriptor *td)
247{
248 struct ctf_text_stream_pos *pos =
249 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
250 fclose(pos->fp);
251 g_free(pos);
252}
253
254void __attribute__((constructor)) ctf_text_init(void)
255{
256 int ret;
257
258 ctf_text_format.name = g_quark_from_static_string("text");
259 ret = bt_register_format(&ctf_text_format);
260 assert(!ret);
261}
262
263/* TODO: finalize */
This page took 0.035051 seconds and 4 git commands to generate.