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