| 1 | #ifndef BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H |
| 2 | #define BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | * |
| 7 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include <babeltrace/assert-internal.h> |
| 29 | #include <babeltrace/babeltrace-internal.h> |
| 30 | #include <babeltrace/ctf-writer/event.h> |
| 31 | #include <babeltrace/ctf-writer/field-types.h> |
| 32 | #include <babeltrace/ctf-writer/fields.h> |
| 33 | #include <babeltrace/ctf-writer/stream-class.h> |
| 34 | #include <babeltrace/ctf-writer/stream.h> |
| 35 | #include <babeltrace/ctf-writer/trace-internal.h> |
| 36 | #include <babeltrace/ctf-writer/object-internal.h> |
| 37 | #include <babeltrace/values.h> |
| 38 | #include <glib.h> |
| 39 | |
| 40 | struct bt_ctf_event_class_common { |
| 41 | struct bt_ctf_object base; |
| 42 | struct bt_ctf_field_type_common *context_field_type; |
| 43 | struct bt_ctf_field_type_common *payload_field_type; |
| 44 | int frozen; |
| 45 | |
| 46 | /* |
| 47 | * This flag indicates if the event class is valid. A valid |
| 48 | * event class is _always_ frozen. However, an event class |
| 49 | * may be frozen, but not valid yet. This is okay, as long as |
| 50 | * no events are created out of this event class. |
| 51 | */ |
| 52 | int valid; |
| 53 | |
| 54 | /* Attributes */ |
| 55 | GString *name; |
| 56 | int64_t id; |
| 57 | int log_level; |
| 58 | GString *emf_uri; |
| 59 | }; |
| 60 | |
| 61 | BT_HIDDEN |
| 62 | void bt_ctf_event_class_common_freeze(struct bt_ctf_event_class_common *event_class); |
| 63 | |
| 64 | BT_HIDDEN |
| 65 | void bt_ctf_event_class_common_set_native_byte_order( |
| 66 | struct bt_ctf_event_class_common *event_class, int byte_order); |
| 67 | |
| 68 | static inline |
| 69 | struct bt_ctf_stream_class_common *bt_ctf_event_class_common_borrow_stream_class( |
| 70 | struct bt_ctf_event_class_common *event_class) |
| 71 | { |
| 72 | BT_ASSERT(event_class); |
| 73 | return (void *) bt_ctf_object_borrow_parent(&event_class->base); |
| 74 | } |
| 75 | |
| 76 | typedef struct bt_ctf_field_type_common *(*bt_ctf_field_type_structure_create_func)(); |
| 77 | |
| 78 | BT_HIDDEN |
| 79 | int bt_ctf_event_class_common_initialize(struct bt_ctf_event_class_common *event_class, |
| 80 | const char *name, bt_ctf_object_release_func release_func, |
| 81 | bt_ctf_field_type_structure_create_func ft_struct_create_func); |
| 82 | |
| 83 | BT_HIDDEN |
| 84 | void bt_ctf_event_class_common_finalize(struct bt_ctf_object *obj); |
| 85 | |
| 86 | BT_HIDDEN |
| 87 | int bt_ctf_event_class_common_validate_single_clock_class( |
| 88 | struct bt_ctf_event_class_common *event_class, |
| 89 | struct bt_ctf_clock_class **expected_clock_class); |
| 90 | |
| 91 | static inline |
| 92 | const char *bt_ctf_event_class_common_get_name( |
| 93 | struct bt_ctf_event_class_common *event_class) |
| 94 | { |
| 95 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 96 | BT_ASSERT(event_class->name); |
| 97 | return event_class->name->str; |
| 98 | } |
| 99 | |
| 100 | static inline |
| 101 | int64_t bt_ctf_event_class_common_get_id( |
| 102 | struct bt_ctf_event_class_common *event_class) |
| 103 | { |
| 104 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 105 | return event_class->id; |
| 106 | } |
| 107 | |
| 108 | static inline |
| 109 | int bt_ctf_event_class_common_set_id( |
| 110 | struct bt_ctf_event_class_common *event_class, uint64_t id_param) |
| 111 | { |
| 112 | int ret = 0; |
| 113 | int64_t id = (int64_t) id_param; |
| 114 | |
| 115 | if (!event_class) { |
| 116 | BT_LOGW_STR("Invalid parameter: event class is NULL."); |
| 117 | ret = -1; |
| 118 | goto end; |
| 119 | } |
| 120 | |
| 121 | if (event_class->frozen) { |
| 122 | BT_LOGW("Invalid parameter: event class is frozen: " |
| 123 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 124 | event_class, |
| 125 | bt_ctf_event_class_common_get_name(event_class), |
| 126 | bt_ctf_event_class_common_get_id(event_class)); |
| 127 | ret = -1; |
| 128 | goto end; |
| 129 | } |
| 130 | |
| 131 | if (id < 0) { |
| 132 | BT_LOGW("Invalid parameter: invalid event class's ID: " |
| 133 | "addr=%p, name=\"%s\", id=%" PRIu64, |
| 134 | event_class, |
| 135 | bt_ctf_event_class_common_get_name(event_class), |
| 136 | id_param); |
| 137 | ret = -1; |
| 138 | goto end; |
| 139 | } |
| 140 | |
| 141 | event_class->id = id; |
| 142 | BT_LOGV("Set event class's ID: " |
| 143 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 144 | event_class, bt_ctf_event_class_common_get_name(event_class), id); |
| 145 | |
| 146 | end: |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static inline |
| 151 | int bt_ctf_event_class_common_get_log_level( |
| 152 | struct bt_ctf_event_class_common *event_class) |
| 153 | { |
| 154 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 155 | return event_class->log_level; |
| 156 | } |
| 157 | |
| 158 | static inline |
| 159 | int bt_ctf_event_class_common_set_log_level( |
| 160 | struct bt_ctf_event_class_common *event_class, int log_level) |
| 161 | { |
| 162 | int ret = 0; |
| 163 | |
| 164 | if (!event_class) { |
| 165 | BT_LOGW_STR("Invalid parameter: event class is NULL."); |
| 166 | ret = -1; |
| 167 | goto end; |
| 168 | } |
| 169 | |
| 170 | if (event_class->frozen) { |
| 171 | BT_LOGW("Invalid parameter: event class is frozen: " |
| 172 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 173 | event_class, |
| 174 | bt_ctf_event_class_common_get_name(event_class), |
| 175 | bt_ctf_event_class_common_get_id(event_class)); |
| 176 | ret = -1; |
| 177 | goto end; |
| 178 | } |
| 179 | |
| 180 | switch (log_level) { |
| 181 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED: |
| 182 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_EMERGENCY: |
| 183 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_ALERT: |
| 184 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_CRITICAL: |
| 185 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_ERROR: |
| 186 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_WARNING: |
| 187 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_NOTICE: |
| 188 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO: |
| 189 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM: |
| 190 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM: |
| 191 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS: |
| 192 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE: |
| 193 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT: |
| 194 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION: |
| 195 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE: |
| 196 | case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG: |
| 197 | break; |
| 198 | default: |
| 199 | BT_LOGW("Invalid parameter: unknown event class log level: " |
| 200 | "addr=%p, name=\"%s\", id=%" PRId64 ", log-level=%d", |
| 201 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 202 | bt_ctf_event_class_common_get_id(event_class), log_level); |
| 203 | ret = -1; |
| 204 | goto end; |
| 205 | } |
| 206 | |
| 207 | event_class->log_level = log_level; |
| 208 | BT_LOGV("Set event class's log level: " |
| 209 | "addr=%p, name=\"%s\", id=%" PRId64 ", log-level=%s", |
| 210 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 211 | bt_ctf_event_class_common_get_id(event_class), |
| 212 | bt_ctf_event_class_log_level_string(log_level)); |
| 213 | |
| 214 | end: |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static inline |
| 219 | const char *bt_ctf_event_class_common_get_emf_uri( |
| 220 | struct bt_ctf_event_class_common *event_class) |
| 221 | { |
| 222 | const char *emf_uri = NULL; |
| 223 | |
| 224 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 225 | |
| 226 | if (event_class->emf_uri->len > 0) { |
| 227 | emf_uri = event_class->emf_uri->str; |
| 228 | } |
| 229 | |
| 230 | return emf_uri; |
| 231 | } |
| 232 | |
| 233 | static inline |
| 234 | int bt_ctf_event_class_common_set_emf_uri( |
| 235 | struct bt_ctf_event_class_common *event_class, |
| 236 | const char *emf_uri) |
| 237 | { |
| 238 | int ret = 0; |
| 239 | |
| 240 | if (!event_class) { |
| 241 | BT_LOGW_STR("Invalid parameter: event class is NULL."); |
| 242 | ret = -1; |
| 243 | goto end; |
| 244 | } |
| 245 | |
| 246 | if (emf_uri && strlen(emf_uri) == 0) { |
| 247 | BT_LOGW_STR("Invalid parameter: EMF URI is empty."); |
| 248 | ret = -1; |
| 249 | goto end; |
| 250 | } |
| 251 | |
| 252 | if (event_class->frozen) { |
| 253 | BT_LOGW("Invalid parameter: event class is frozen: " |
| 254 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 255 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 256 | bt_ctf_event_class_common_get_id(event_class)); |
| 257 | ret = -1; |
| 258 | goto end; |
| 259 | } |
| 260 | |
| 261 | if (emf_uri) { |
| 262 | g_string_assign(event_class->emf_uri, emf_uri); |
| 263 | BT_LOGV("Set event class's EMF URI: " |
| 264 | "addr=%p, name=\"%s\", id=%" PRId64 ", emf-uri=\"%s\"", |
| 265 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 266 | bt_ctf_event_class_common_get_id(event_class), emf_uri); |
| 267 | } else { |
| 268 | g_string_assign(event_class->emf_uri, ""); |
| 269 | BT_LOGV("Reset event class's EMF URI: " |
| 270 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 271 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 272 | bt_ctf_event_class_common_get_id(event_class)); |
| 273 | } |
| 274 | |
| 275 | end: |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static inline |
| 280 | struct bt_ctf_field_type_common *bt_ctf_event_class_common_borrow_context_field_type( |
| 281 | struct bt_ctf_event_class_common *event_class) |
| 282 | { |
| 283 | struct bt_ctf_field_type_common *context_ft = NULL; |
| 284 | |
| 285 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 286 | |
| 287 | if (!event_class->context_field_type) { |
| 288 | BT_LOGV("Event class has no context field type: " |
| 289 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 290 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 291 | bt_ctf_event_class_common_get_id(event_class)); |
| 292 | goto end; |
| 293 | } |
| 294 | |
| 295 | context_ft = event_class->context_field_type; |
| 296 | |
| 297 | end: |
| 298 | return context_ft; |
| 299 | } |
| 300 | |
| 301 | static inline |
| 302 | int bt_ctf_event_class_common_set_context_field_type( |
| 303 | struct bt_ctf_event_class_common *event_class, |
| 304 | struct bt_ctf_field_type_common *context_ft) |
| 305 | { |
| 306 | int ret = 0; |
| 307 | |
| 308 | if (!event_class) { |
| 309 | BT_LOGW_STR("Invalid parameter: event class is NULL."); |
| 310 | ret = -1; |
| 311 | goto end; |
| 312 | } |
| 313 | |
| 314 | if (event_class->frozen) { |
| 315 | BT_LOGW("Invalid parameter: event class is frozen: " |
| 316 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 317 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 318 | bt_ctf_event_class_common_get_id(event_class)); |
| 319 | ret = -1; |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | if (context_ft && bt_ctf_field_type_common_get_type_id(context_ft) != |
| 324 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 325 | BT_LOGW("Invalid parameter: event class's context field type must be a structure: " |
| 326 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 327 | "context-ft-id=%s", |
| 328 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 329 | bt_ctf_event_class_common_get_id(event_class), |
| 330 | bt_ctf_field_type_id_string( |
| 331 | bt_ctf_field_type_common_get_type_id(context_ft))); |
| 332 | ret = -1; |
| 333 | goto end; |
| 334 | } |
| 335 | |
| 336 | bt_ctf_object_put_ref(event_class->context_field_type); |
| 337 | event_class->context_field_type = context_ft; |
| 338 | bt_ctf_object_get_ref(event_class->context_field_type); |
| 339 | BT_LOGV("Set event class's context field type: " |
| 340 | "event-class-addr=%p, event-class-name=\"%s\", " |
| 341 | "event-class-id=%" PRId64 ", context-ft-addr=%p", |
| 342 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 343 | bt_ctf_event_class_common_get_id(event_class), context_ft); |
| 344 | |
| 345 | end: |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | static inline |
| 350 | struct bt_ctf_field_type_common *bt_ctf_event_class_common_borrow_payload_field_type( |
| 351 | struct bt_ctf_event_class_common *event_class) |
| 352 | { |
| 353 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 354 | return event_class->payload_field_type; |
| 355 | } |
| 356 | |
| 357 | static inline |
| 358 | int bt_ctf_event_class_common_set_payload_field_type( |
| 359 | struct bt_ctf_event_class_common *event_class, |
| 360 | struct bt_ctf_field_type_common *payload_ft) |
| 361 | { |
| 362 | int ret = 0; |
| 363 | |
| 364 | if (!event_class) { |
| 365 | BT_LOGW_STR("Invalid parameter: event class is NULL."); |
| 366 | ret = -1; |
| 367 | goto end; |
| 368 | } |
| 369 | |
| 370 | if (payload_ft && bt_ctf_field_type_common_get_type_id(payload_ft) != |
| 371 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 372 | BT_LOGW("Invalid parameter: event class's payload field type must be a structure: " |
| 373 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 374 | "payload-ft-addr=%p, payload-ft-id=%s", |
| 375 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 376 | bt_ctf_event_class_common_get_id(event_class), payload_ft, |
| 377 | bt_ctf_field_type_id_string( |
| 378 | bt_ctf_field_type_common_get_type_id(payload_ft))); |
| 379 | ret = -1; |
| 380 | goto end; |
| 381 | } |
| 382 | |
| 383 | bt_ctf_object_put_ref(event_class->payload_field_type); |
| 384 | event_class->payload_field_type = payload_ft; |
| 385 | bt_ctf_object_get_ref(event_class->payload_field_type); |
| 386 | BT_LOGV("Set event class's payload field type: " |
| 387 | "event-class-addr=%p, event-class-name=\"%s\", " |
| 388 | "event-class-id=%" PRId64 ", payload-ft-addr=%p", |
| 389 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 390 | bt_ctf_event_class_common_get_id(event_class), payload_ft); |
| 391 | end: |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | #endif /* BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H */ |