2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
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>
36 #include <sys/types.h>
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
);
49 int ctf_metadata_close_trace(struct bt_trace_descriptor
*descriptor
);
52 struct bt_format ctf_metadata_format
= {
53 .open_trace
= ctf_metadata_open_trace
,
54 .close_trace
= ctf_metadata_close_trace
,
58 int ctf_metadata_trace_pre_handler(struct bt_stream_pos
*ppos
,
59 struct bt_trace_descriptor
*td
)
61 struct ctf_text_stream_pos
*pos
=
62 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
63 struct ctf_trace
*trace
;
65 trace
= container_of(td
, struct ctf_trace
, parent
);
66 if (!trace
->metadata_string
)
68 if (trace
->metadata_packetized
) {
69 fprintf(pos
->fp
, "/* CTF %u.%u */\n",
70 BT_CTF_MAJOR
, BT_CTF_MINOR
);
72 fprintf(pos
->fp
, "%s", trace
->metadata_string
);
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
)
81 struct ctf_text_stream_pos
*pos
;
84 pos
= g_new0(struct ctf_text_stream_pos
, 1);
86 pos
->last_real_timestamp
= -1ULL;
87 pos
->last_cycles_timestamp
= -1ULL;
88 switch (flags
& O_ACCMODE
) {
93 fp
= fopen(path
, "w");
97 pos
->parent
.pre_trace_cb
= ctf_metadata_trace_pre_handler
;
98 pos
->parent
.trace
= &pos
->trace_descriptor
;
103 fprintf(stderr
, "[error] Incorrect open flags.\n");
107 return &pos
->trace_descriptor
;
114 int ctf_metadata_close_trace(struct bt_trace_descriptor
*td
)
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
);
122 perror("Error on fclose");
131 void __attribute__((constructor
)) ctf_metadata_init(void)
135 ctf_metadata_format
.name
= g_quark_from_static_string("ctf-metadata");
136 ret
= bt_register_format(&ctf_metadata_format
);
141 void __attribute__((destructor
)) ctf_metadata_exit(void)
143 bt_unregister_format(&ctf_metadata_format
);