Add -t option
[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_class)
59 {
60 struct ctf_text_stream_pos *pos =
61 container_of(ppos, struct ctf_text_stream_pos, parent);
62 struct ctf_event *event_class;
63 uint64_t id = 0;
64 int len_index;
65 int ret;
66
67 /* print event header */
68 if (stream_class->event_header) {
69 /* lookup event id */
70 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
71 g_quark_from_static_string("id"));
72 if (len_index >= 0) {
73 struct definition_integer *defint;
74 struct definition *field;
75
76 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
77 assert(field->declaration->id == CTF_TYPE_INTEGER);
78 defint = container_of(field, struct definition_integer, p);
79 assert(defint->declaration->signedness == FALSE);
80 id = defint->value._unsigned; /* set id */
81 }
82 }
83
84 if (id >= stream_class->events_by_id->len) {
85 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
86 return -EINVAL;
87 }
88 event_class = g_ptr_array_index(stream_class->events_by_id, id);
89 if (!event_class) {
90 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
91 return -EINVAL;
92 }
93
94 if (pos->print_names)
95 fprintf(pos->fp, "name = ");
96 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
97
98 if (stream_class->event_header) {
99 if (pos->print_names)
100 fprintf(pos->fp, ", stream.event.header =");
101 else
102 fprintf(pos->fp, ",");
103 fprintf(pos->fp, " ");
104 ret = generic_rw(ppos, &stream_class->event_header->p);
105 if (ret)
106 goto error;
107 }
108
109 /* print stream-declared event context */
110 if (stream_class->event_context) {
111 if (pos->print_names)
112 fprintf(pos->fp, ", stream.event.context =");
113 else
114 fprintf(pos->fp, ",");
115 fprintf(pos->fp, " ");
116 ret = generic_rw(ppos, &stream_class->event_context->p);
117 if (ret)
118 goto error;
119 }
120
121 /* print event-declared event context */
122 if (event_class->context) {
123 if (pos->print_names)
124 fprintf(pos->fp, ", event.context =");
125 else
126 fprintf(pos->fp, ",");
127 fprintf(pos->fp, " ");
128 ret = generic_rw(ppos, &event_class->context->p);
129 if (ret)
130 goto error;
131 }
132
133 /* Read and print event payload */
134 if (event_class->fields) {
135 if (pos->print_names)
136 fprintf(pos->fp, ", event.fields =");
137 else
138 fprintf(pos->fp, ",");
139 fprintf(pos->fp, " ");
140 ret = generic_rw(ppos, &event_class->fields->p);
141 if (ret)
142 goto error;
143 }
144 fprintf(pos->fp, "\n");
145
146 return 0;
147
148 error:
149 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
150 return ret;
151 }
152
153
154 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
155 {
156 struct ctf_text_stream_pos *pos;
157 FILE *fp;
158
159 pos = g_new0(struct ctf_text_stream_pos, 1);
160
161 switch (flags & O_ACCMODE) {
162 case O_RDWR:
163 if (!path)
164 fp = stdout;
165 else
166 fp = fopen(path, "w");
167 if (!fp)
168 goto error;
169 pos->fp = fp;
170 pos->parent.rw_table = write_dispatch_table;
171 pos->parent.event_cb = ctf_text_write_event;
172 pos->print_names = opt_field_names;
173 break;
174 case O_RDONLY:
175 default:
176 fprintf(stdout, "[error] Incorrect open flags.\n");
177 goto error;
178 }
179
180 return &pos->trace_descriptor;
181 error:
182 g_free(pos);
183 return NULL;
184 }
185
186 void ctf_text_close_trace(struct trace_descriptor *td)
187 {
188 struct ctf_text_stream_pos *pos =
189 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
190 fclose(pos->fp);
191 g_free(pos);
192 }
193
194 void __attribute__((constructor)) ctf_text_init(void)
195 {
196 int ret;
197
198 ctf_text_format.name = g_quark_from_static_string("text");
199 ret = bt_register_format(&ctf_text_format);
200 assert(!ret);
201 }
202
203 /* TODO: finalize */
This page took 0.032174 seconds and 4 git commands to generate.