Handle negative time and offset from Epoch
[babeltrace.git] / formats / ctf-metadata / ctf-metadata.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * CTF Metadata Dump.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/format.h>
30 #include <babeltrace/ctf-text/types.h>
31 #include <babeltrace/babeltrace-internal.h>
32 #include <babeltrace/ctf/events-internal.h>
33 #include <inttypes.h>
34 #include <sys/mman.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <dirent.h>
40 #include <glib.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43
44 static
45 struct bt_trace_descriptor *ctf_metadata_open_trace(const char *path, int flags,
46 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
47 int whence), FILE *metadata_fp);
48 static
49 int ctf_metadata_close_trace(struct bt_trace_descriptor *descriptor);
50
51 static
52 struct bt_format ctf_metadata_format = {
53 .open_trace = ctf_metadata_open_trace,
54 .close_trace = ctf_metadata_close_trace,
55 };
56
57 static
58 int ctf_metadata_trace_pre_handler(struct bt_stream_pos *ppos,
59 struct bt_trace_descriptor *td)
60 {
61 struct ctf_text_stream_pos *pos =
62 container_of(ppos, struct ctf_text_stream_pos, parent);
63 struct ctf_trace *trace;
64
65 trace = container_of(td, struct ctf_trace, parent);
66 if (!trace->metadata_string)
67 return -EINVAL;
68 if (trace->metadata_packetized) {
69 fprintf(pos->fp, "/* CTF %u.%u */\n",
70 BT_CTF_MAJOR, BT_CTF_MINOR);
71 }
72 fprintf(pos->fp, "%s", trace->metadata_string);
73 return 0;
74 }
75
76 static
77 struct bt_trace_descriptor *ctf_metadata_open_trace(const char *path, int flags,
78 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
79 int whence), FILE *metadata_fp)
80 {
81 struct ctf_text_stream_pos *pos;
82 FILE *fp;
83
84 pos = g_new0(struct ctf_text_stream_pos, 1);
85
86 pos->last_real_timestamp = -1ULL;
87 pos->last_cycles_timestamp = -1ULL;
88 switch (flags & O_ACCMODE) {
89 case O_RDWR:
90 if (!path)
91 fp = stdout;
92 else
93 fp = fopen(path, "w");
94 if (!fp)
95 goto error;
96 pos->fp = fp;
97 pos->parent.pre_trace_cb = ctf_metadata_trace_pre_handler;
98 pos->parent.trace = &pos->trace_descriptor;
99 pos->print_names = 0;
100 break;
101 case O_RDONLY:
102 default:
103 fprintf(stderr, "[error] Incorrect open flags.\n");
104 goto error;
105 }
106
107 return &pos->trace_descriptor;
108 error:
109 g_free(pos);
110 return NULL;
111 }
112
113 static
114 int ctf_metadata_close_trace(struct bt_trace_descriptor *td)
115 {
116 int ret;
117 struct ctf_text_stream_pos *pos =
118 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
119 if (pos->fp != stdout) {
120 ret = fclose(pos->fp);
121 if (ret) {
122 perror("Error on fclose");
123 return -1;
124 }
125 }
126 g_free(pos);
127 return 0;
128 }
129
130 static
131 void __attribute__((constructor)) ctf_metadata_init(void)
132 {
133 int ret;
134
135 ctf_metadata_format.name = g_quark_from_static_string("ctf-metadata");
136 ret = bt_register_format(&ctf_metadata_format);
137 assert(!ret);
138 }
139
140 static
141 void __attribute__((destructor)) ctf_metadata_exit(void)
142 {
143 bt_unregister_format(&ctf_metadata_format);
144 }
This page took 0.032065 seconds and 4 git commands to generate.