| 1 | /* |
| 2 | * event.c |
| 3 | * |
| 4 | * Linux Trace Toolkit Control Library |
| 5 | * |
| 6 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * SPDX-License-Identifier: LGPL-2.1-only |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #define _LGPL_SOURCE |
| 13 | #include <assert.h> |
| 14 | #include <stddef.h> |
| 15 | |
| 16 | #include <common/error.h> |
| 17 | #include <common/sessiond-comm/sessiond-comm.h> |
| 18 | #include <lttng/event-internal.h> |
| 19 | #include <lttng/event.h> |
| 20 | #include <lttng/lttng-error.h> |
| 21 | #include <lttng/userspace-probe-internal.h> |
| 22 | |
| 23 | struct lttng_event *lttng_event_create(void) |
| 24 | { |
| 25 | struct lttng_event *event; |
| 26 | struct lttng_event_extended *event_extended; |
| 27 | |
| 28 | event = zmalloc(sizeof(*event)); |
| 29 | if (!event) { |
| 30 | PERROR("Error allocating event structure"); |
| 31 | goto end; |
| 32 | } |
| 33 | |
| 34 | event_extended = zmalloc(sizeof(*event_extended)); |
| 35 | if (!event_extended) { |
| 36 | PERROR("Error allocating event extended structure"); |
| 37 | goto error; |
| 38 | } |
| 39 | event->extended.ptr = event_extended; |
| 40 | end: |
| 41 | return event; |
| 42 | error: |
| 43 | free(event); |
| 44 | event = NULL; |
| 45 | goto end; |
| 46 | } |
| 47 | |
| 48 | void lttng_event_destroy(struct lttng_event *event) |
| 49 | { |
| 50 | struct lttng_event_extended *event_extended; |
| 51 | |
| 52 | if (!event) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | event_extended = (struct lttng_event_extended *) event->extended.ptr; |
| 57 | |
| 58 | if (event_extended) { |
| 59 | if (event_extended->probe_location) { |
| 60 | lttng_userspace_probe_location_destroy( |
| 61 | event_extended->probe_location); |
| 62 | } |
| 63 | free(event_extended); |
| 64 | } |
| 65 | free(event); |
| 66 | } |
| 67 | |
| 68 | int lttng_event_get_filter_expression(struct lttng_event *event, |
| 69 | const char **filter_expression) |
| 70 | { |
| 71 | int ret = 0; |
| 72 | struct lttng_event_extended *event_extended; |
| 73 | |
| 74 | if (!event || !filter_expression) { |
| 75 | ret = -LTTNG_ERR_INVALID; |
| 76 | goto end; |
| 77 | } |
| 78 | |
| 79 | event_extended = (struct lttng_event_extended *) event->extended.ptr; |
| 80 | if (!event_extended) { |
| 81 | /* |
| 82 | * This can happen since the lttng_event structure is |
| 83 | * used for other tasks where this pointer is never set. |
| 84 | */ |
| 85 | *filter_expression = NULL; |
| 86 | goto end; |
| 87 | } |
| 88 | |
| 89 | *filter_expression = event_extended->filter_expression; |
| 90 | end: |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | int lttng_event_get_exclusion_name_count(struct lttng_event *event) |
| 95 | { |
| 96 | int ret = 0; |
| 97 | struct lttng_event_extended *event_extended; |
| 98 | |
| 99 | if (!event) { |
| 100 | ret = -LTTNG_ERR_INVALID; |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | event_extended = (struct lttng_event_extended *) event->extended.ptr; |
| 105 | if (!event_extended) { |
| 106 | /* |
| 107 | * This can happen since the lttng_event structure is |
| 108 | * used for other tasks where this pointer is never set. |
| 109 | */ |
| 110 | goto end; |
| 111 | } |
| 112 | |
| 113 | if (event_extended->exclusions.count > INT_MAX) { |
| 114 | ret = -LTTNG_ERR_OVERFLOW; |
| 115 | goto end; |
| 116 | } |
| 117 | ret = (int) event_extended->exclusions.count; |
| 118 | end: |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | int lttng_event_get_exclusion_name(struct lttng_event *event, |
| 123 | size_t index, const char **exclusion_name) |
| 124 | { |
| 125 | int ret = 0; |
| 126 | struct lttng_event_extended *event_extended; |
| 127 | |
| 128 | if (!event || !exclusion_name) { |
| 129 | ret = -LTTNG_ERR_INVALID; |
| 130 | goto end; |
| 131 | } |
| 132 | |
| 133 | if (index > UINT_MAX) { |
| 134 | ret = -LTTNG_ERR_OVERFLOW; |
| 135 | goto end; |
| 136 | } |
| 137 | |
| 138 | event_extended = (struct lttng_event_extended *) event->extended.ptr; |
| 139 | if (!event_extended) { |
| 140 | /* |
| 141 | * This can happen since the lttng_event structure is |
| 142 | * used for other tasks where this pointer is never set. |
| 143 | */ |
| 144 | ret = -LTTNG_ERR_INVALID; |
| 145 | goto end; |
| 146 | } |
| 147 | |
| 148 | if (index >= event_extended->exclusions.count) { |
| 149 | ret = -LTTNG_ERR_INVALID; |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | *exclusion_name = event_extended->exclusions.strings + |
| 154 | (index * LTTNG_SYMBOL_NAME_LEN); |
| 155 | end: |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | const struct lttng_userspace_probe_location * |
| 160 | lttng_event_get_userspace_probe_location(const struct lttng_event *event) |
| 161 | { |
| 162 | struct lttng_userspace_probe_location *probe_location = NULL; |
| 163 | struct lttng_event_extended *event_extended; |
| 164 | |
| 165 | if (!event) { |
| 166 | goto end; |
| 167 | } |
| 168 | |
| 169 | event_extended = (struct lttng_event_extended *) event->extended.ptr; |
| 170 | if (!event_extended) { |
| 171 | goto end; |
| 172 | } |
| 173 | probe_location = event_extended->probe_location; |
| 174 | end: |
| 175 | return probe_location; |
| 176 | } |
| 177 | |
| 178 | int lttng_event_set_userspace_probe_location(struct lttng_event *event, |
| 179 | struct lttng_userspace_probe_location *probe_location) |
| 180 | { |
| 181 | int ret = 0; |
| 182 | struct lttng_event_extended *event_extended; |
| 183 | |
| 184 | if (!event || !probe_location) { |
| 185 | ret = -LTTNG_ERR_INVALID; |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | event_extended = (struct lttng_event_extended *) event->extended.ptr; |
| 190 | assert(event_extended); |
| 191 | if (event_extended->probe_location) { |
| 192 | lttng_userspace_probe_location_destroy( |
| 193 | event_extended->probe_location); |
| 194 | } |
| 195 | event_extended->probe_location = probe_location; |
| 196 | end: |
| 197 | return ret; |
| 198 | } |