| 1 |
/* |
| 2 |
* writer.c |
| 3 |
* |
| 4 |
* Babeltrace CTF Writer |
| 5 |
* |
| 6 |
* Copyright 2013, 2014 Jérémie Galarneau <[email protected]> |
| 7 |
* |
| 8 |
* Author: Jérémie Galarneau <[email protected]> |
| 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/ctf-ir/clock-internal.h> |
| 30 |
#include <babeltrace/ctf-writer/writer-internal.h> |
| 31 |
#include <babeltrace/ctf-ir/field-types-internal.h> |
| 32 |
#include <babeltrace/ctf-ir/fields-internal.h> |
| 33 |
#include <babeltrace/ctf-writer/functor-internal.h> |
| 34 |
#include <babeltrace/ctf-ir/stream-class-internal.h> |
| 35 |
#include <babeltrace/ctf-ir/stream-internal.h> |
| 36 |
#include <babeltrace/ctf-ir/trace-internal.h> |
| 37 |
#include <babeltrace/ref.h> |
| 38 |
#include <babeltrace/compiler.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <sys/stat.h> |
| 42 |
#include <errno.h> |
| 43 |
#include <unistd.h> |
| 44 |
#include <fcntl.h> |
| 45 |
#include <inttypes.h> |
| 46 |
|
| 47 |
static |
| 48 |
void bt_ctf_writer_destroy(struct bt_object *obj); |
| 49 |
|
| 50 |
struct bt_ctf_writer *bt_ctf_writer_create(const char *path) |
| 51 |
{ |
| 52 |
struct bt_ctf_writer *writer = NULL; |
| 53 |
|
| 54 |
if (!path) { |
| 55 |
goto error; |
| 56 |
} |
| 57 |
|
| 58 |
writer = g_new0(struct bt_ctf_writer, 1); |
| 59 |
if (!writer) { |
| 60 |
goto error; |
| 61 |
} |
| 62 |
|
| 63 |
bt_object_init(writer, bt_ctf_writer_destroy); |
| 64 |
writer->path = g_string_new(path); |
| 65 |
if (!writer->path) { |
| 66 |
goto error_destroy; |
| 67 |
} |
| 68 |
|
| 69 |
writer->trace = bt_ctf_trace_create(); |
| 70 |
if (!writer->trace) { |
| 71 |
goto error_destroy; |
| 72 |
} |
| 73 |
|
| 74 |
writer->trace->is_created_by_writer = 1; |
| 75 |
bt_object_set_parent(writer->trace, writer); |
| 76 |
bt_put(writer->trace); |
| 77 |
/* Create trace directory if necessary and open a metadata file */ |
| 78 |
if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { |
| 79 |
perror("g_mkdir_with_parents"); |
| 80 |
goto error_destroy; |
| 81 |
} |
| 82 |
|
| 83 |
writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG); |
| 84 |
if (writer->trace_dir_fd < 0) { |
| 85 |
perror("open"); |
| 86 |
goto error_destroy; |
| 87 |
} |
| 88 |
|
| 89 |
writer->metadata_fd = openat(writer->trace_dir_fd, "metadata", |
| 90 |
O_WRONLY | O_CREAT | O_TRUNC, |
| 91 |
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 92 |
|
| 93 |
return writer; |
| 94 |
|
| 95 |
error_destroy: |
| 96 |
unlinkat(writer->trace_dir_fd, "metadata", 0); |
| 97 |
BT_PUT(writer); |
| 98 |
error: |
| 99 |
return writer; |
| 100 |
} |
| 101 |
|
| 102 |
void bt_ctf_writer_destroy(struct bt_object *obj) |
| 103 |
{ |
| 104 |
struct bt_ctf_writer *writer; |
| 105 |
|
| 106 |
writer = container_of(obj, struct bt_ctf_writer, base); |
| 107 |
bt_ctf_writer_flush_metadata(writer); |
| 108 |
if (writer->path) { |
| 109 |
g_string_free(writer->path, TRUE); |
| 110 |
} |
| 111 |
|
| 112 |
if (writer->trace_dir_fd > 0) { |
| 113 |
if (close(writer->trace_dir_fd)) { |
| 114 |
perror("close"); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if (writer->metadata_fd > 0) { |
| 119 |
if (close(writer->metadata_fd)) { |
| 120 |
perror("close"); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
bt_object_release(writer->trace); |
| 125 |
g_free(writer); |
| 126 |
} |
| 127 |
|
| 128 |
BT_HIDDEN |
| 129 |
struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer) |
| 130 |
{ |
| 131 |
struct bt_ctf_trace *trace = NULL; |
| 132 |
|
| 133 |
if (!writer) { |
| 134 |
goto end; |
| 135 |
} |
| 136 |
|
| 137 |
trace = writer->trace; |
| 138 |
bt_get(trace); |
| 139 |
end: |
| 140 |
return trace; |
| 141 |
} |
| 142 |
|
| 143 |
struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, |
| 144 |
struct bt_ctf_stream_class *stream_class) |
| 145 |
{ |
| 146 |
struct bt_ctf_stream *stream = NULL; |
| 147 |
int stream_class_count; |
| 148 |
bool stream_class_found = false; |
| 149 |
int i; |
| 150 |
|
| 151 |
if (!writer || !stream_class) { |
| 152 |
goto error; |
| 153 |
} |
| 154 |
|
| 155 |
/* Make sure the stream class is part of the writer's trace */ |
| 156 |
stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace); |
| 157 |
if (stream_class_count < 0) { |
| 158 |
goto error; |
| 159 |
} |
| 160 |
|
| 161 |
for (i = 0; i < stream_class_count; i++) { |
| 162 |
struct bt_ctf_stream_class *existing_stream_class = |
| 163 |
bt_ctf_trace_get_stream_class(writer->trace, i); |
| 164 |
|
| 165 |
if (existing_stream_class == stream_class) { |
| 166 |
stream_class_found = true; |
| 167 |
} |
| 168 |
|
| 169 |
BT_PUT(existing_stream_class); |
| 170 |
|
| 171 |
if (stream_class_found) { |
| 172 |
break; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (!stream_class_found) { |
| 177 |
int ret = bt_ctf_trace_add_stream_class(writer->trace, |
| 178 |
stream_class); |
| 179 |
|
| 180 |
if (ret) { |
| 181 |
goto error; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
stream = bt_ctf_stream_create(stream_class, NULL); |
| 186 |
if (!stream) { |
| 187 |
goto error; |
| 188 |
} |
| 189 |
|
| 190 |
return stream; |
| 191 |
|
| 192 |
error: |
| 193 |
BT_PUT(stream); |
| 194 |
return stream; |
| 195 |
} |
| 196 |
|
| 197 |
int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, |
| 198 |
const char *name, |
| 199 |
const char *value) |
| 200 |
{ |
| 201 |
int ret = -1; |
| 202 |
|
| 203 |
if (!writer || !name || !value) { |
| 204 |
goto end; |
| 205 |
} |
| 206 |
|
| 207 |
ret = bt_ctf_trace_set_environment_field_string(writer->trace, |
| 208 |
name, value); |
| 209 |
end: |
| 210 |
return ret; |
| 211 |
} |
| 212 |
|
| 213 |
int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, |
| 214 |
struct bt_ctf_clock *clock) |
| 215 |
{ |
| 216 |
int ret = -1; |
| 217 |
|
| 218 |
if (!writer || !clock) { |
| 219 |
goto end; |
| 220 |
} |
| 221 |
|
| 222 |
ret = bt_ctf_trace_add_clock(writer->trace, clock); |
| 223 |
end: |
| 224 |
return ret; |
| 225 |
} |
| 226 |
|
| 227 |
char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer) |
| 228 |
{ |
| 229 |
char *metadata_string = NULL; |
| 230 |
|
| 231 |
if (!writer) { |
| 232 |
goto end; |
| 233 |
} |
| 234 |
|
| 235 |
metadata_string = bt_ctf_trace_get_metadata_string( |
| 236 |
writer->trace); |
| 237 |
end: |
| 238 |
return metadata_string; |
| 239 |
} |
| 240 |
|
| 241 |
void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer) |
| 242 |
{ |
| 243 |
int ret; |
| 244 |
char *metadata_string = NULL; |
| 245 |
|
| 246 |
if (!writer) { |
| 247 |
goto end; |
| 248 |
} |
| 249 |
|
| 250 |
metadata_string = bt_ctf_trace_get_metadata_string( |
| 251 |
writer->trace); |
| 252 |
if (!metadata_string) { |
| 253 |
goto end; |
| 254 |
} |
| 255 |
|
| 256 |
if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) { |
| 257 |
perror("lseek"); |
| 258 |
goto end; |
| 259 |
} |
| 260 |
|
| 261 |
if (ftruncate(writer->metadata_fd, 0)) { |
| 262 |
perror("ftruncate"); |
| 263 |
goto end; |
| 264 |
} |
| 265 |
|
| 266 |
ret = write(writer->metadata_fd, metadata_string, |
| 267 |
strlen(metadata_string)); |
| 268 |
if (ret < 0) { |
| 269 |
perror("write"); |
| 270 |
goto end; |
| 271 |
} |
| 272 |
end: |
| 273 |
g_free(metadata_string); |
| 274 |
} |
| 275 |
|
| 276 |
int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, |
| 277 |
enum bt_ctf_byte_order byte_order) |
| 278 |
{ |
| 279 |
int ret = 0; |
| 280 |
|
| 281 |
if (!writer || writer->frozen) { |
| 282 |
ret = -1; |
| 283 |
goto end; |
| 284 |
} |
| 285 |
|
| 286 |
ret = bt_ctf_trace_set_byte_order(writer->trace, |
| 287 |
byte_order); |
| 288 |
end: |
| 289 |
return ret; |
| 290 |
} |
| 291 |
|
| 292 |
void bt_ctf_writer_get(struct bt_ctf_writer *writer) |
| 293 |
{ |
| 294 |
bt_get(writer); |
| 295 |
} |
| 296 |
|
| 297 |
void bt_ctf_writer_put(struct bt_ctf_writer *writer) |
| 298 |
{ |
| 299 |
bt_put(writer); |
| 300 |
} |
| 301 |
|
| 302 |
BT_HIDDEN |
| 303 |
void bt_ctf_writer_freeze(struct bt_ctf_writer *writer) |
| 304 |
{ |
| 305 |
writer->frozen = 1; |
| 306 |
} |