70e0e92ab6604a7535724b520808247b7dc43489
[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 - 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>
21 #include <babeltrace/ctf/metadata.h>
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
35 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags);
36 void ctf_text_close_trace(struct trace_descriptor *descriptor);
37
38 static
39 rw_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
50 static
51 struct format ctf_text_format = {
52 .open_trace = ctf_text_open_trace,
53 .close_trace = ctf_text_close_trace,
54 };
55
56 static
57 int ctf_text_write_event(struct stream_pos *ppos,
58 struct ctf_stream *stream)
59 {
60 struct ctf_text_stream_pos *pos =
61 container_of(ppos, struct ctf_text_stream_pos, parent);
62 struct ctf_stream_class *stream_class = stream->stream_class;
63 int field_nr_saved;
64 struct ctf_event *event_class;
65 uint64_t id = 0;
66 int len_index;
67 int ret;
68
69 /* print event header */
70 if (stream_class->event_header) {
71 /* lookup event id */
72 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
73 g_quark_from_static_string("id"));
74 if (len_index >= 0) {
75 struct definition_integer *defint;
76 struct definition *field;
77
78 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
79 assert(field->declaration->id == CTF_TYPE_INTEGER);
80 defint = container_of(field, struct definition_integer, p);
81 assert(defint->declaration->signedness == FALSE);
82 id = defint->value._unsigned; /* set id */
83 }
84 }
85
86 if (id >= stream_class->events_by_id->len) {
87 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
88 return -EINVAL;
89 }
90 event_class = g_ptr_array_index(stream_class->events_by_id, id);
91 if (!event_class) {
92 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
93 return -EINVAL;
94 }
95
96 if (pos->print_names)
97 fprintf(pos->fp, "timestamp = ");
98 else
99 fprintf(pos->fp, "[");
100 fprintf(pos->fp, "%12" PRIu64, stream->timestamp);
101 if (!pos->print_names)
102 fprintf(pos->fp, "]");
103
104 if (pos->print_names)
105 fprintf(pos->fp, ", ");
106 else
107 fprintf(pos->fp, " ");
108 if (pos->print_names)
109 fprintf(pos->fp, "name = ");
110 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
111 if (pos->print_names)
112 pos->field_nr++;
113 else
114 fprintf(pos->fp, ":");
115
116 /* Only show the event header in verbose mode */
117 if (babeltrace_verbose && stream_class->event_header) {
118 if (pos->field_nr++ != 0)
119 fprintf(pos->fp, ",");
120 if (pos->print_names)
121 fprintf(pos->fp, " stream.event.header =");
122 field_nr_saved = pos->field_nr;
123 pos->field_nr = 0;
124 ret = generic_rw(ppos, &stream_class->event_header->p);
125 if (ret)
126 goto error;
127 pos->field_nr = field_nr_saved;
128 }
129
130 /* print stream-declared event context */
131 if (stream_class->event_context) {
132 if (pos->field_nr++ != 0)
133 fprintf(pos->fp, ",");
134 if (pos->print_names)
135 fprintf(pos->fp, " stream.event.context =");
136 field_nr_saved = pos->field_nr;
137 pos->field_nr = 0;
138 ret = generic_rw(ppos, &stream_class->event_context->p);
139 if (ret)
140 goto error;
141 pos->field_nr = field_nr_saved;
142 }
143
144 /* print event-declared event context */
145 if (event_class->context) {
146 if (pos->field_nr++ != 0)
147 fprintf(pos->fp, ",");
148 if (pos->print_names)
149 fprintf(pos->fp, " event.context =");
150 field_nr_saved = pos->field_nr;
151 pos->field_nr = 0;
152 ret = generic_rw(ppos, &event_class->context->p);
153 if (ret)
154 goto error;
155 pos->field_nr = field_nr_saved;
156 }
157
158 /* Read and print event payload */
159 if (event_class->fields) {
160 if (pos->field_nr++ != 0)
161 fprintf(pos->fp, ",");
162 if (pos->print_names)
163 fprintf(pos->fp, " event.fields =");
164 field_nr_saved = pos->field_nr;
165 pos->field_nr = 0;
166 ret = generic_rw(ppos, &event_class->fields->p);
167 if (ret)
168 goto error;
169 pos->field_nr = field_nr_saved;
170 }
171 /* newline */
172 fprintf(pos->fp, "\n");
173 pos->field_nr = 0;
174
175 return 0;
176
177 error:
178 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
179 return ret;
180 }
181
182
183 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
184 {
185 struct ctf_text_stream_pos *pos;
186 FILE *fp;
187
188 pos = g_new0(struct ctf_text_stream_pos, 1);
189
190 switch (flags & O_ACCMODE) {
191 case O_RDWR:
192 if (!path)
193 fp = stdout;
194 else
195 fp = fopen(path, "w");
196 if (!fp)
197 goto error;
198 pos->fp = fp;
199 pos->parent.rw_table = write_dispatch_table;
200 pos->parent.event_cb = ctf_text_write_event;
201 pos->print_names = opt_field_names;
202 break;
203 case O_RDONLY:
204 default:
205 fprintf(stdout, "[error] Incorrect open flags.\n");
206 goto error;
207 }
208
209 return &pos->trace_descriptor;
210 error:
211 g_free(pos);
212 return NULL;
213 }
214
215 void ctf_text_close_trace(struct trace_descriptor *td)
216 {
217 struct ctf_text_stream_pos *pos =
218 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
219 fclose(pos->fp);
220 g_free(pos);
221 }
222
223 void __attribute__((constructor)) ctf_text_init(void)
224 {
225 int ret;
226
227 ctf_text_format.name = g_quark_from_static_string("text");
228 ret = bt_register_format(&ctf_text_format);
229 assert(!ret);
230 }
231
232 /* TODO: finalize */
This page took 0.033783 seconds and 3 git commands to generate.