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; | |
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 | ||
be60c7fb MD |
96 | if (stream->timestamp) { |
97 | if (pos->print_names) | |
98 | fprintf(pos->fp, "timestamp = "); | |
99 | else | |
100 | fprintf(pos->fp, "["); | |
101 | fprintf(pos->fp, "%12" PRIu64, stream->timestamp); | |
102 | if (!pos->print_names) | |
103 | fprintf(pos->fp, "]"); | |
aa6bffae | 104 | |
be60c7fb MD |
105 | if (pos->print_names) |
106 | fprintf(pos->fp, ", "); | |
107 | else | |
108 | fprintf(pos->fp, " "); | |
109 | } | |
31262354 MD |
110 | if (pos->print_names) |
111 | fprintf(pos->fp, "name = "); | |
8178fe48 MD |
112 | fprintf(pos->fp, "%s", g_quark_to_string(event_class->name)); |
113 | if (pos->print_names) | |
fd3382e8 | 114 | pos->field_nr++; |
8178fe48 | 115 | else |
fd3382e8 | 116 | fprintf(pos->fp, ":"); |
31262354 | 117 | |
764af3f4 MD |
118 | /* Only show the event header in verbose mode */ |
119 | if (babeltrace_verbose && stream_class->event_header) { | |
fd3382e8 MD |
120 | if (pos->field_nr++ != 0) |
121 | fprintf(pos->fp, ","); | |
31262354 | 122 | if (pos->print_names) |
fd3382e8 MD |
123 | fprintf(pos->fp, " stream.event.header ="); |
124 | field_nr_saved = pos->field_nr; | |
125 | pos->field_nr = 0; | |
31262354 MD |
126 | ret = generic_rw(ppos, &stream_class->event_header->p); |
127 | if (ret) | |
128 | goto error; | |
fd3382e8 | 129 | pos->field_nr = field_nr_saved; |
31262354 MD |
130 | } |
131 | ||
132 | /* print stream-declared event context */ | |
133 | if (stream_class->event_context) { | |
fd3382e8 MD |
134 | if (pos->field_nr++ != 0) |
135 | fprintf(pos->fp, ","); | |
31262354 | 136 | if (pos->print_names) |
fd3382e8 MD |
137 | fprintf(pos->fp, " stream.event.context ="); |
138 | field_nr_saved = pos->field_nr; | |
139 | pos->field_nr = 0; | |
31262354 MD |
140 | ret = generic_rw(ppos, &stream_class->event_context->p); |
141 | if (ret) | |
142 | goto error; | |
fd3382e8 | 143 | pos->field_nr = field_nr_saved; |
31262354 MD |
144 | } |
145 | ||
146 | /* print event-declared event context */ | |
147 | if (event_class->context) { | |
fd3382e8 MD |
148 | if (pos->field_nr++ != 0) |
149 | fprintf(pos->fp, ","); | |
31262354 | 150 | if (pos->print_names) |
fd3382e8 MD |
151 | fprintf(pos->fp, " event.context ="); |
152 | field_nr_saved = pos->field_nr; | |
153 | pos->field_nr = 0; | |
31262354 MD |
154 | ret = generic_rw(ppos, &event_class->context->p); |
155 | if (ret) | |
156 | goto error; | |
fd3382e8 | 157 | pos->field_nr = field_nr_saved; |
31262354 MD |
158 | } |
159 | ||
160 | /* Read and print event payload */ | |
161 | if (event_class->fields) { | |
fd3382e8 MD |
162 | if (pos->field_nr++ != 0) |
163 | fprintf(pos->fp, ","); | |
31262354 | 164 | if (pos->print_names) |
fd3382e8 MD |
165 | fprintf(pos->fp, " event.fields ="); |
166 | field_nr_saved = pos->field_nr; | |
167 | pos->field_nr = 0; | |
31262354 MD |
168 | ret = generic_rw(ppos, &event_class->fields->p); |
169 | if (ret) | |
170 | goto error; | |
fd3382e8 | 171 | pos->field_nr = field_nr_saved; |
31262354 | 172 | } |
fd3382e8 | 173 | /* newline */ |
31262354 | 174 | fprintf(pos->fp, "\n"); |
fd3382e8 | 175 | pos->field_nr = 0; |
31262354 MD |
176 | |
177 | return 0; | |
178 | ||
179 | error: | |
180 | fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n"); | |
181 | return ret; | |
182 | } | |
183 | ||
184 | ||
642ec66d MD |
185 | struct trace_descriptor *ctf_text_open_trace(const char *path, int flags) |
186 | { | |
187 | struct ctf_text_stream_pos *pos; | |
188 | FILE *fp; | |
189 | ||
190 | pos = g_new0(struct ctf_text_stream_pos, 1); | |
191 | ||
192 | switch (flags & O_ACCMODE) { | |
989c73bc | 193 | case O_RDWR: |
b61922b5 | 194 | if (!path) |
6cf7957b MD |
195 | fp = stdout; |
196 | else | |
197 | fp = fopen(path, "w"); | |
642ec66d MD |
198 | if (!fp) |
199 | goto error; | |
200 | pos->fp = fp; | |
201 | pos->parent.rw_table = write_dispatch_table; | |
31262354 | 202 | pos->parent.event_cb = ctf_text_write_event; |
d63ca2cd | 203 | pos->print_names = opt_field_names; |
642ec66d MD |
204 | break; |
205 | case O_RDONLY: | |
206 | default: | |
207 | fprintf(stdout, "[error] Incorrect open flags.\n"); | |
208 | goto error; | |
209 | } | |
210 | ||
211 | return &pos->trace_descriptor; | |
212 | error: | |
213 | g_free(pos); | |
214 | return NULL; | |
215 | } | |
216 | ||
217 | void ctf_text_close_trace(struct trace_descriptor *td) | |
218 | { | |
219 | struct ctf_text_stream_pos *pos = | |
220 | container_of(td, struct ctf_text_stream_pos, trace_descriptor); | |
221 | fclose(pos->fp); | |
222 | g_free(pos); | |
223 | } | |
224 | ||
225 | void __attribute__((constructor)) ctf_text_init(void) | |
226 | { | |
227 | int ret; | |
228 | ||
229 | ctf_text_format.name = g_quark_from_static_string("text"); | |
230 | ret = bt_register_format(&ctf_text_format); | |
231 | assert(!ret); | |
232 | } | |
233 | ||
234 | /* TODO: finalize */ |