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 | ||
b88d6e85 MD |
37 | int opt_field_names; |
38 | ||
b086c01a JD |
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, | |
ae23d232 | 41 | int whence), FILE *metadata_fp); |
642ec66d MD |
42 | void ctf_text_close_trace(struct trace_descriptor *descriptor); |
43 | ||
642ec66d MD |
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 | ||
d335f0f7 MD |
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 | ||
31262354 MD |
99 | static |
100 | int ctf_text_write_event(struct stream_pos *ppos, | |
764af3f4 | 101 | struct ctf_stream *stream) |
31262354 MD |
102 | { |
103 | struct ctf_text_stream_pos *pos = | |
104 | container_of(ppos, struct ctf_text_stream_pos, parent); | |
764af3f4 | 105 | struct ctf_stream_class *stream_class = stream->stream_class; |
fd3382e8 | 106 | int field_nr_saved; |
31262354 | 107 | struct ctf_event *event_class; |
42dc00b7 | 108 | struct ctf_stream_event *event; |
c87a8eb2 | 109 | uint64_t id; |
31262354 MD |
110 | int ret; |
111 | ||
c87a8eb2 | 112 | id = stream->event_id; |
31262354 MD |
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 | } | |
e28d4618 MD |
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 | } | |
31262354 | 123 | event_class = g_ptr_array_index(stream_class->events_by_id, id); |
e28d4618 | 124 | if (!event) { |
31262354 MD |
125 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); |
126 | return -EINVAL; | |
127 | } | |
128 | ||
5e2eb0ae | 129 | if (stream->has_timestamp) { |
be60c7fb MD |
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, "]"); | |
aa6bffae | 137 | |
be60c7fb MD |
138 | if (pos->print_names) |
139 | fprintf(pos->fp, ", "); | |
140 | else | |
141 | fprintf(pos->fp, " "); | |
142 | } | |
31262354 MD |
143 | if (pos->print_names) |
144 | fprintf(pos->fp, "name = "); | |
8178fe48 MD |
145 | fprintf(pos->fp, "%s", g_quark_to_string(event_class->name)); |
146 | if (pos->print_names) | |
fd3382e8 | 147 | pos->field_nr++; |
8178fe48 | 148 | else |
fd3382e8 | 149 | fprintf(pos->fp, ":"); |
31262354 | 150 | |
e28d4618 MD |
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 | ||
764af3f4 | 165 | /* Only show the event header in verbose mode */ |
e28d4618 | 166 | if (babeltrace_verbose && stream->stream_event_header) { |
fd3382e8 MD |
167 | if (pos->field_nr++ != 0) |
168 | fprintf(pos->fp, ","); | |
31262354 | 169 | if (pos->print_names) |
fd3382e8 MD |
170 | fprintf(pos->fp, " stream.event.header ="); |
171 | field_nr_saved = pos->field_nr; | |
172 | pos->field_nr = 0; | |
e28d4618 | 173 | ret = generic_rw(ppos, &stream->stream_event_header->p); |
31262354 MD |
174 | if (ret) |
175 | goto error; | |
fd3382e8 | 176 | pos->field_nr = field_nr_saved; |
31262354 MD |
177 | } |
178 | ||
179 | /* print stream-declared event context */ | |
e28d4618 | 180 | if (stream->stream_event_context) { |
fd3382e8 MD |
181 | if (pos->field_nr++ != 0) |
182 | fprintf(pos->fp, ","); | |
31262354 | 183 | if (pos->print_names) |
fd3382e8 MD |
184 | fprintf(pos->fp, " stream.event.context ="); |
185 | field_nr_saved = pos->field_nr; | |
186 | pos->field_nr = 0; | |
e28d4618 | 187 | ret = generic_rw(ppos, &stream->stream_event_context->p); |
31262354 MD |
188 | if (ret) |
189 | goto error; | |
fd3382e8 | 190 | pos->field_nr = field_nr_saved; |
31262354 MD |
191 | } |
192 | ||
193 | /* print event-declared event context */ | |
e28d4618 | 194 | if (event->event_context) { |
fd3382e8 MD |
195 | if (pos->field_nr++ != 0) |
196 | fprintf(pos->fp, ","); | |
31262354 | 197 | if (pos->print_names) |
fd3382e8 MD |
198 | fprintf(pos->fp, " event.context ="); |
199 | field_nr_saved = pos->field_nr; | |
200 | pos->field_nr = 0; | |
e28d4618 | 201 | ret = generic_rw(ppos, &event->event_context->p); |
31262354 MD |
202 | if (ret) |
203 | goto error; | |
fd3382e8 | 204 | pos->field_nr = field_nr_saved; |
31262354 MD |
205 | } |
206 | ||
207 | /* Read and print event payload */ | |
e28d4618 | 208 | if (event->event_fields) { |
fd3382e8 MD |
209 | if (pos->field_nr++ != 0) |
210 | fprintf(pos->fp, ","); | |
31262354 | 211 | if (pos->print_names) |
fd3382e8 MD |
212 | fprintf(pos->fp, " event.fields ="); |
213 | field_nr_saved = pos->field_nr; | |
214 | pos->field_nr = 0; | |
e28d4618 | 215 | ret = generic_rw(ppos, &event->event_fields->p); |
31262354 MD |
216 | if (ret) |
217 | goto error; | |
fd3382e8 | 218 | pos->field_nr = field_nr_saved; |
31262354 | 219 | } |
fd3382e8 | 220 | /* newline */ |
31262354 | 221 | fprintf(pos->fp, "\n"); |
fd3382e8 | 222 | pos->field_nr = 0; |
31262354 MD |
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 | ||
b086c01a JD |
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, | |
ae23d232 | 234 | int whence), FILE *metadata_fp) |
642ec66d MD |
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) { | |
989c73bc | 242 | case O_RDWR: |
b61922b5 | 243 | if (!path) |
6cf7957b MD |
244 | fp = stdout; |
245 | else | |
246 | fp = fopen(path, "w"); | |
642ec66d MD |
247 | if (!fp) |
248 | goto error; | |
249 | pos->fp = fp; | |
250 | pos->parent.rw_table = write_dispatch_table; | |
31262354 | 251 | pos->parent.event_cb = ctf_text_write_event; |
d63ca2cd | 252 | pos->print_names = opt_field_names; |
642ec66d MD |
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 */ |