| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <errno.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "lttngerr.h" |
| 27 | #include "ltt-sessiond.h" |
| 28 | #include "libkernelctl.h" |
| 29 | #include "kernel-ctl.h" |
| 30 | |
| 31 | /* |
| 32 | * kernel_create_session |
| 33 | * |
| 34 | * Create a new kernel session, register it to the kernel tracer and add it to |
| 35 | * the session daemon session. |
| 36 | */ |
| 37 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
| 38 | { |
| 39 | int ret; |
| 40 | struct ltt_kernel_session *lks; |
| 41 | |
| 42 | /* Allocate data structure */ |
| 43 | lks = trace_create_kernel_session(); |
| 44 | if (lks == NULL) { |
| 45 | ret = -1; |
| 46 | goto error; |
| 47 | } |
| 48 | |
| 49 | /* Kernel tracer session creation */ |
| 50 | ret = kernctl_create_session(tracer_fd); |
| 51 | if (ret < 0) { |
| 52 | perror("ioctl kernel create session"); |
| 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | lks->fd = ret; |
| 57 | session->kernel_session = lks; |
| 58 | session->kern_session_count++; |
| 59 | |
| 60 | DBG("Kernel session created (fd: %d)", lks->fd); |
| 61 | |
| 62 | return 0; |
| 63 | |
| 64 | error: |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * kernel_create_channel |
| 70 | * |
| 71 | * Create a kernel channel, register it to the kernel tracer and add it to the |
| 72 | * kernel session. |
| 73 | */ |
| 74 | int kernel_create_channel(struct ltt_kernel_session *session) |
| 75 | { |
| 76 | int ret; |
| 77 | struct ltt_kernel_channel *lkc; |
| 78 | |
| 79 | /* Allocate kernel channel */ |
| 80 | lkc = trace_create_kernel_channel(); |
| 81 | if (lkc == NULL) { |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | /* Kernel tracer channel creation */ |
| 86 | ret = kernctl_create_channel(session->fd, lkc->channel); |
| 87 | if (ret < 0) { |
| 88 | perror("ioctl kernel create channel"); |
| 89 | goto error; |
| 90 | } |
| 91 | |
| 92 | /* Setup the channel fd */ |
| 93 | lkc->fd = ret; |
| 94 | /* Add channel to session */ |
| 95 | cds_list_add(&lkc->list, &session->channel_list.head); |
| 96 | session->channel_count++; |
| 97 | |
| 98 | DBG("Kernel channel created (fd: %d and path: %s)", lkc->fd, lkc->pathname); |
| 99 | |
| 100 | return 0; |
| 101 | |
| 102 | error: |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * kernel_enable_event |
| 108 | * |
| 109 | * Create a kernel event, enable it to the kernel tracer and add it to the |
| 110 | * channel event list of the kernel session. |
| 111 | */ |
| 112 | int kernel_enable_event(struct ltt_kernel_session *session, char *name) |
| 113 | { |
| 114 | int ret; |
| 115 | struct ltt_kernel_channel *chan; |
| 116 | struct ltt_kernel_event *event; |
| 117 | |
| 118 | event = trace_create_kernel_event(name, LTTNG_KERNEL_TRACEPOINTS); |
| 119 | if (event == NULL) { |
| 120 | goto error; |
| 121 | } |
| 122 | |
| 123 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 124 | ret = kernctl_create_event(chan->fd, event->event); |
| 125 | if (ret < 0) { |
| 126 | goto error; |
| 127 | } |
| 128 | |
| 129 | event->fd = ret; |
| 130 | /* Add event to event list */ |
| 131 | cds_list_add(&event->list, &chan->events_list.head); |
| 132 | DBG("Event %s enabled (fd: %d)", name, event->fd); |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | |
| 137 | error: |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * kernel_open_metadata |
| 143 | * |
| 144 | * Create kernel metadata, open from the kernel tracer and add it to the |
| 145 | * kernel session. |
| 146 | */ |
| 147 | int kernel_open_metadata(struct ltt_kernel_session *session) |
| 148 | { |
| 149 | int ret; |
| 150 | struct ltt_kernel_metadata *lkm; |
| 151 | |
| 152 | /* Allocate kernel metadata */ |
| 153 | lkm = trace_create_kernel_metadata(); |
| 154 | if (lkm == NULL) { |
| 155 | goto error; |
| 156 | } |
| 157 | |
| 158 | /* Kernel tracer metadata creation */ |
| 159 | ret = kernctl_open_metadata(session->fd, lkm->conf); |
| 160 | if (ret < 0) { |
| 161 | goto error; |
| 162 | } |
| 163 | |
| 164 | lkm->fd = ret; |
| 165 | session->metadata = lkm; |
| 166 | |
| 167 | DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname); |
| 168 | |
| 169 | return 0; |
| 170 | |
| 171 | error: |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * kernel_start_session |
| 177 | * |
| 178 | * Start tracing session. |
| 179 | */ |
| 180 | int kernel_start_session(struct ltt_kernel_session *session) |
| 181 | { |
| 182 | int ret; |
| 183 | |
| 184 | ret = kernctl_start_session(session->fd); |
| 185 | if (ret < 0) { |
| 186 | goto error; |
| 187 | } |
| 188 | |
| 189 | DBG("Kernel session started"); |
| 190 | |
| 191 | return 0; |
| 192 | |
| 193 | error: |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * kernel_stop_session |
| 199 | * |
| 200 | * Stop tracing session. |
| 201 | */ |
| 202 | int kernel_stop_session(struct ltt_kernel_session *session) |
| 203 | { |
| 204 | int ret; |
| 205 | |
| 206 | ret = kernctl_stop_session(session->fd); |
| 207 | if (ret < 0) { |
| 208 | goto error; |
| 209 | } |
| 210 | |
| 211 | DBG("Kernel session stopped"); |
| 212 | |
| 213 | return 0; |
| 214 | |
| 215 | error: |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * kernel_create_channel_stream |
| 221 | * |
| 222 | * Create a stream for a channel, register it to the kernel tracer and add it |
| 223 | * to the stream list of the channel. |
| 224 | * |
| 225 | * Return the number of created stream. Else, a negative value. |
| 226 | */ |
| 227 | int kernel_create_channel_stream(struct ltt_kernel_channel *channel) |
| 228 | { |
| 229 | int ret; |
| 230 | struct ltt_kernel_stream *lks; |
| 231 | |
| 232 | while ((ret = kernctl_create_stream(channel->fd)) > 0) { |
| 233 | lks = trace_create_kernel_stream(); |
| 234 | if (lks == NULL) { |
| 235 | close(ret); |
| 236 | goto error; |
| 237 | } |
| 238 | |
| 239 | lks->fd = ret; |
| 240 | ret = asprintf(&lks->pathname, "%s/trace_%d", |
| 241 | channel->pathname, channel->stream_count); |
| 242 | if (ret < 0) { |
| 243 | perror("asprintf kernel create stream"); |
| 244 | goto error; |
| 245 | } |
| 246 | |
| 247 | /* Add stream to channe stream list */ |
| 248 | cds_list_add(&lks->list, &channel->stream_list.head); |
| 249 | channel->stream_count++; |
| 250 | |
| 251 | DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)", |
| 252 | channel->stream_count, lks->fd, lks->state, lks->pathname); |
| 253 | } |
| 254 | |
| 255 | return channel->stream_count; |
| 256 | |
| 257 | error: |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * kernel_create_metadata_stream |
| 263 | * |
| 264 | * Create the metadata stream and set it to the kernel session. |
| 265 | */ |
| 266 | int kernel_create_metadata_stream(struct ltt_kernel_session *session) |
| 267 | { |
| 268 | int ret; |
| 269 | |
| 270 | ret = kernctl_create_stream(session->metadata->fd); |
| 271 | if (ret < 0) { |
| 272 | perror("kernel create metadata stream"); |
| 273 | goto error; |
| 274 | } |
| 275 | |
| 276 | DBG("Kernel metadata stream created (fd: %d)", ret); |
| 277 | session->metadata_stream_fd = ret; |
| 278 | |
| 279 | return 0; |
| 280 | |
| 281 | error: |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * kernel_list_events |
| 287 | * |
| 288 | * Get the event list from the kernel tracer and return that list in the CTF |
| 289 | * format. |
| 290 | */ |
| 291 | ssize_t kernel_list_events(int tracer_fd, char **list) |
| 292 | { |
| 293 | int fd; |
| 294 | char *buf, *line = NULL; |
| 295 | size_t nb, nbmem, total = 0; |
| 296 | ssize_t size; |
| 297 | FILE *fp; |
| 298 | |
| 299 | fd = kernctl_tracepoint_list(tracer_fd); |
| 300 | if (fd < 0) { |
| 301 | perror("kernel tracepoint list"); |
| 302 | goto error; |
| 303 | } |
| 304 | |
| 305 | fp = fdopen(fd, "r"); |
| 306 | if (fp == NULL) { |
| 307 | perror("kernel tracepoint list fdopen"); |
| 308 | goto error; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Init memory size counter |
| 313 | * See kernel-ctl.h for explanation of this value |
| 314 | */ |
| 315 | nbmem = KERNEL_EVENT_LIST_SIZE; |
| 316 | buf = malloc(nbmem); |
| 317 | |
| 318 | while ((size = getline(&line, &nb, fp)) != -1) { |
| 319 | if (total + size > nbmem) { |
| 320 | DBG("Reallocating event list from %ld to %ld bytes", nbmem, |
| 321 | total + size + KERNEL_EVENT_LIST_SIZE); |
| 322 | /* Adding the default size again */ |
| 323 | nbmem = total + size + KERNEL_EVENT_LIST_SIZE; |
| 324 | buf = realloc(buf, nbmem); |
| 325 | if (buf == NULL) { |
| 326 | perror("realloc list events"); |
| 327 | goto error; |
| 328 | } |
| 329 | } |
| 330 | memcpy(buf + total, line, size); |
| 331 | total += size; |
| 332 | } |
| 333 | |
| 334 | *list = buf; |
| 335 | |
| 336 | DBG("Kernel list events done"); |
| 337 | |
| 338 | return total; |
| 339 | |
| 340 | error: |
| 341 | return -1; |
| 342 | } |
| 343 | |