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