Move ctf-metadata plugin into its own shared object
[babeltrace.git] / formats / ctf-metadata / ctf-metadata.c
CommitLineData
5d93a76e
MD
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
44static
45struct 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);
48static
49int ctf_metadata_close_trace(struct bt_trace_descriptor *descriptor);
50
51static
52struct bt_format ctf_metadata_format = {
53 .open_trace = ctf_metadata_open_trace,
54 .close_trace = ctf_metadata_close_trace,
55};
56
57static
58int 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
76static
77struct 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->print_names = 0;
99 break;
100 case O_RDONLY:
101 default:
102 fprintf(stderr, "[error] Incorrect open flags.\n");
103 goto error;
104 }
105
106 return &pos->trace_descriptor;
107error:
108 g_free(pos);
109 return NULL;
110}
111
112static
113int ctf_metadata_close_trace(struct bt_trace_descriptor *td)
114{
115 int ret;
116 struct ctf_text_stream_pos *pos =
117 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
118 if (pos->fp != stdout) {
119 ret = fclose(pos->fp);
120 if (ret) {
121 perror("Error on fclose");
122 return -1;
123 }
124 }
125 g_free(pos);
126 return 0;
127}
128
129static
130void __attribute__((constructor)) ctf_metadata_init(void)
131{
132 int ret;
133
134 ctf_metadata_format.name = g_quark_from_static_string("ctf-metadata");
135 ret = bt_register_format(&ctf_metadata_format);
136 assert(!ret);
137}
138
139static
140void __attribute__((destructor)) ctf_metadata_exit(void)
141{
142 bt_unregister_format(&ctf_metadata_format);
143}
This page took 0.026778 seconds and 4 git commands to generate.