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