| 1 | /* |
| 2 | * Copyright (C) 2007 Mathieu Desnoyers |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library 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 GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | * |
| 18 | * LTT marker control module over /proc |
| 19 | */ |
| 20 | |
| 21 | //ust// #include <linux/proc_fs.h> |
| 22 | //ust// #include <linux/module.h> |
| 23 | //ust// #include <linux/stat.h> |
| 24 | //ust// #include <linux/vmalloc.h> |
| 25 | //ust// #include <linux/marker.h> |
| 26 | //ust// #include <linux/ltt-tracer.h> |
| 27 | //ust// #include <linux/uaccess.h> |
| 28 | //ust// #include <linux/string.h> |
| 29 | //ust// #include <linux/ctype.h> |
| 30 | //ust// #include <linux/list.h> |
| 31 | //ust// #include <linux/mutex.h> |
| 32 | //ust// #include <linux/seq_file.h> |
| 33 | //ust// #include <linux/slab.h> |
| 34 | #include <ctype.h> |
| 35 | |
| 36 | #include <ust/kernelcompat.h> |
| 37 | //#include "list.h" |
| 38 | #include "tracer.h" |
| 39 | #include "usterr.h" |
| 40 | |
| 41 | #define DEFAULT_CHANNEL "cpu" |
| 42 | #define DEFAULT_PROBE "default" |
| 43 | |
| 44 | LIST_HEAD(probes_list); |
| 45 | |
| 46 | /* |
| 47 | * Mutex protecting the probe slab cache. |
| 48 | * Nests inside the traces mutex. |
| 49 | */ |
| 50 | DEFINE_MUTEX(probes_mutex); |
| 51 | |
| 52 | struct ltt_available_probe default_probe = { |
| 53 | .name = "default", |
| 54 | .format = NULL, |
| 55 | .probe_func = ltt_vtrace, |
| 56 | .callbacks[0] = ltt_serialize_data, |
| 57 | }; |
| 58 | |
| 59 | //ust//static struct kmem_cache *markers_loaded_cachep; |
| 60 | static LIST_HEAD(markers_loaded_list); |
| 61 | /* |
| 62 | * List sorted by name strcmp order. |
| 63 | */ |
| 64 | static LIST_HEAD(probes_registered_list); |
| 65 | |
| 66 | //ust// static struct proc_dir_entry *pentry; |
| 67 | |
| 68 | //ust// static struct file_operations ltt_fops; |
| 69 | |
| 70 | static struct ltt_available_probe *get_probe_from_name(const char *pname) |
| 71 | { |
| 72 | struct ltt_available_probe *iter; |
| 73 | int comparison, found = 0; |
| 74 | |
| 75 | if (!pname) |
| 76 | pname = DEFAULT_PROBE; |
| 77 | list_for_each_entry(iter, &probes_registered_list, node) { |
| 78 | comparison = strcmp(pname, iter->name); |
| 79 | if (!comparison) |
| 80 | found = 1; |
| 81 | if (comparison <= 0) |
| 82 | break; |
| 83 | } |
| 84 | if (found) |
| 85 | return iter; |
| 86 | else |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | /* (unused) |
| 91 | static char *skip_spaces(char *buf) |
| 92 | { |
| 93 | while (*buf != '\0' && isspace(*buf)) |
| 94 | buf++; |
| 95 | return buf; |
| 96 | } |
| 97 | |
| 98 | static char *skip_nonspaces(char *buf) |
| 99 | { |
| 100 | while (*buf != '\0' && !isspace(*buf)) |
| 101 | buf++; |
| 102 | return buf; |
| 103 | } |
| 104 | |
| 105 | static void get_marker_string(char *buf, char **start, |
| 106 | char **end) |
| 107 | { |
| 108 | *start = skip_spaces(buf); |
| 109 | *end = skip_nonspaces(*start); |
| 110 | **end = '\0'; |
| 111 | } |
| 112 | */ |
| 113 | |
| 114 | int ltt_probe_register(struct ltt_available_probe *pdata) |
| 115 | { |
| 116 | int ret = 0; |
| 117 | int comparison; |
| 118 | struct ltt_available_probe *iter; |
| 119 | |
| 120 | mutex_lock(&probes_mutex); |
| 121 | list_for_each_entry_reverse(iter, &probes_registered_list, node) { |
| 122 | comparison = strcmp(pdata->name, iter->name); |
| 123 | if (!comparison) { |
| 124 | ret = -EBUSY; |
| 125 | goto end; |
| 126 | } else if (comparison > 0) { |
| 127 | /* We belong to the location right after iter. */ |
| 128 | list_add(&pdata->node, &iter->node); |
| 129 | goto end; |
| 130 | } |
| 131 | } |
| 132 | /* Should be added at the head of the list */ |
| 133 | list_add(&pdata->node, &probes_registered_list); |
| 134 | end: |
| 135 | mutex_unlock(&probes_mutex); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Called when a probe does not want to be called anymore. |
| 141 | */ |
| 142 | int ltt_probe_unregister(struct ltt_available_probe *pdata) |
| 143 | { |
| 144 | int ret = 0; |
| 145 | struct ltt_active_marker *amark, *tmp; |
| 146 | |
| 147 | mutex_lock(&probes_mutex); |
| 148 | list_for_each_entry_safe(amark, tmp, &markers_loaded_list, node) { |
| 149 | if (amark->probe == pdata) { |
| 150 | ret = marker_probe_unregister_private_data( |
| 151 | pdata->probe_func, amark); |
| 152 | if (ret) |
| 153 | goto end; |
| 154 | list_del(&amark->node); |
| 155 | free(amark); |
| 156 | } |
| 157 | } |
| 158 | list_del(&pdata->node); |
| 159 | end: |
| 160 | mutex_unlock(&probes_mutex); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Connect marker "mname" to probe "pname". |
| 166 | * Only allow _only_ probe instance to be connected to a marker. |
| 167 | */ |
| 168 | int ltt_marker_connect(const char *channel, const char *mname, |
| 169 | const char *pname) |
| 170 | |
| 171 | { |
| 172 | int ret; |
| 173 | struct ltt_active_marker *pdata; |
| 174 | struct ltt_available_probe *probe; |
| 175 | |
| 176 | ltt_lock_traces(); |
| 177 | mutex_lock(&probes_mutex); |
| 178 | probe = get_probe_from_name(pname); |
| 179 | if (!probe) { |
| 180 | ret = -ENOENT; |
| 181 | goto end; |
| 182 | } |
| 183 | pdata = marker_get_private_data(channel, mname, probe->probe_func, 0); |
| 184 | if (pdata && !IS_ERR(pdata)) { |
| 185 | ret = -EEXIST; |
| 186 | goto end; |
| 187 | } |
| 188 | pdata = zmalloc(sizeof(struct ltt_active_marker)); |
| 189 | if (!pdata) { |
| 190 | ret = -ENOMEM; |
| 191 | goto end; |
| 192 | } |
| 193 | pdata->probe = probe; |
| 194 | /* |
| 195 | * ID has priority over channel in case of conflict. |
| 196 | */ |
| 197 | ret = marker_probe_register(channel, mname, NULL, |
| 198 | probe->probe_func, pdata); |
| 199 | if (ret) |
| 200 | free(pdata); |
| 201 | else |
| 202 | list_add(&pdata->node, &markers_loaded_list); |
| 203 | end: |
| 204 | mutex_unlock(&probes_mutex); |
| 205 | ltt_unlock_traces(); |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Disconnect marker "mname", probe "pname". |
| 211 | */ |
| 212 | int ltt_marker_disconnect(const char *channel, const char *mname, |
| 213 | const char *pname) |
| 214 | { |
| 215 | struct ltt_active_marker *pdata; |
| 216 | struct ltt_available_probe *probe; |
| 217 | int ret = 0; |
| 218 | |
| 219 | mutex_lock(&probes_mutex); |
| 220 | probe = get_probe_from_name(pname); |
| 221 | if (!probe) { |
| 222 | ret = -ENOENT; |
| 223 | goto end; |
| 224 | } |
| 225 | pdata = marker_get_private_data(channel, mname, probe->probe_func, 0); |
| 226 | if (IS_ERR(pdata)) { |
| 227 | ret = PTR_ERR(pdata); |
| 228 | goto end; |
| 229 | } else if (!pdata) { |
| 230 | /* |
| 231 | * Not registered by us. |
| 232 | */ |
| 233 | ret = -EPERM; |
| 234 | goto end; |
| 235 | } |
| 236 | ret = marker_probe_unregister(channel, mname, probe->probe_func, pdata); |
| 237 | if (ret) |
| 238 | goto end; |
| 239 | else { |
| 240 | list_del(&pdata->node); |
| 241 | free(pdata); |
| 242 | } |
| 243 | end: |
| 244 | mutex_unlock(&probes_mutex); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * function handling proc entry write. |
| 250 | * |
| 251 | * connect <channel name> <marker name> [<probe name>]] |
| 252 | * disconnect <channel name> <marker name> [<probe name>] |
| 253 | */ |
| 254 | //ust// static ssize_t ltt_write(struct file *file, const char __user *buffer, |
| 255 | //ust// size_t count, loff_t *offset) |
| 256 | //ust// { |
| 257 | //ust// char *kbuf; |
| 258 | //ust// char *iter, *marker_action, *arg[4]; |
| 259 | //ust// ssize_t ret; |
| 260 | //ust// int i; |
| 261 | //ust// |
| 262 | //ust// if (!count) |
| 263 | //ust// return -EINVAL; |
| 264 | //ust// |
| 265 | //ust// kbuf = vmalloc(count + 1); |
| 266 | //ust// kbuf[count] = '\0'; /* Transform into a string */ |
| 267 | //ust// ret = copy_from_user(kbuf, buffer, count); |
| 268 | //ust// if (ret) { |
| 269 | //ust// ret = -EINVAL; |
| 270 | //ust// goto end; |
| 271 | //ust// } |
| 272 | //ust// get_marker_string(kbuf, &marker_action, &iter); |
| 273 | //ust// if (!marker_action || marker_action == iter) { |
| 274 | //ust// ret = -EINVAL; |
| 275 | //ust// goto end; |
| 276 | //ust// } |
| 277 | //ust// for (i = 0; i < 4; i++) { |
| 278 | //ust// arg[i] = NULL; |
| 279 | //ust// if (iter < kbuf + count) { |
| 280 | //ust// iter++; /* skip the added '\0' */ |
| 281 | //ust// get_marker_string(iter, &arg[i], &iter); |
| 282 | //ust// if (arg[i] == iter) |
| 283 | //ust// arg[i] = NULL; |
| 284 | //ust// } |
| 285 | //ust// } |
| 286 | //ust// |
| 287 | //ust// if (!arg[0] || !arg[1]) { |
| 288 | //ust// ret = -EINVAL; |
| 289 | //ust// goto end; |
| 290 | //ust// } |
| 291 | //ust// |
| 292 | //ust// if (!strcmp(marker_action, "connect")) { |
| 293 | //ust// ret = ltt_marker_connect(arg[0], arg[1], arg[2]); |
| 294 | //ust// if (ret) |
| 295 | //ust// goto end; |
| 296 | //ust// } else if (!strcmp(marker_action, "disconnect")) { |
| 297 | //ust// ret = ltt_marker_disconnect(arg[0], arg[1], arg[2]); |
| 298 | //ust// if (ret) |
| 299 | //ust// goto end; |
| 300 | //ust// } |
| 301 | //ust// ret = count; |
| 302 | //ust// end: |
| 303 | //ust// vfree(kbuf); |
| 304 | //ust// return ret; |
| 305 | //ust// } |
| 306 | //ust// |
| 307 | //ust// static void *s_next(struct seq_file *m, void *p, loff_t *pos) |
| 308 | //ust// { |
| 309 | //ust// struct marker_iter *iter = m->private; |
| 310 | //ust// |
| 311 | //ust// marker_iter_next(iter); |
| 312 | //ust// if (!iter->marker) { |
| 313 | //ust// /* |
| 314 | //ust// * Setting the iter module to -1UL will make sure |
| 315 | //ust// * that no module can possibly hold the current marker. |
| 316 | //ust// */ |
| 317 | //ust// iter->module = (void *)-1UL; |
| 318 | //ust// return NULL; |
| 319 | //ust// } |
| 320 | //ust// return iter->marker; |
| 321 | //ust// } |
| 322 | //ust// |
| 323 | //ust// static void *s_start(struct seq_file *m, loff_t *pos) |
| 324 | //ust// { |
| 325 | //ust// struct marker_iter *iter = m->private; |
| 326 | //ust// |
| 327 | //ust// if (!*pos) |
| 328 | //ust// marker_iter_reset(iter); |
| 329 | //ust// marker_iter_start(iter); |
| 330 | //ust// if (!iter->marker) { |
| 331 | //ust// /* |
| 332 | //ust// * Setting the iter module to -1UL will make sure |
| 333 | //ust// * that no module can possibly hold the current marker. |
| 334 | //ust// */ |
| 335 | //ust// iter->module = (void *)-1UL; |
| 336 | //ust// return NULL; |
| 337 | //ust// } |
| 338 | //ust// return iter->marker; |
| 339 | //ust// } |
| 340 | //ust// |
| 341 | //ust// static void s_stop(struct seq_file *m, void *p) |
| 342 | //ust// { |
| 343 | //ust// marker_iter_stop(m->private); |
| 344 | //ust// } |
| 345 | //ust// |
| 346 | //ust// static int s_show(struct seq_file *m, void *p) |
| 347 | //ust// { |
| 348 | //ust// struct marker_iter *iter = m->private; |
| 349 | //ust// |
| 350 | //ust// seq_printf(m, "channel: %s marker: %s format: \"%s\" state: %d " |
| 351 | //ust// "event_id: %hu call: 0x%p probe %s : 0x%p\n", |
| 352 | //ust// iter->marker->channel, |
| 353 | //ust// iter->marker->name, iter->marker->format, |
| 354 | //ust// _imv_read(iter->marker->state), |
| 355 | //ust// iter->marker->event_id, |
| 356 | //ust// iter->marker->call, |
| 357 | //ust// iter->marker->ptype ? "multi" : "single", |
| 358 | //ust// iter->marker->ptype ? |
| 359 | //ust// (void*)iter->marker->multi : (void*)iter->marker->single.func); |
| 360 | //ust// return 0; |
| 361 | //ust// } |
| 362 | //ust// |
| 363 | //ust// static const struct seq_operations ltt_seq_op = { |
| 364 | //ust// .start = s_start, |
| 365 | //ust// .next = s_next, |
| 366 | //ust// .stop = s_stop, |
| 367 | //ust// .show = s_show, |
| 368 | //ust// }; |
| 369 | //ust// |
| 370 | //ust// static int ltt_open(struct inode *inode, struct file *file) |
| 371 | //ust// { |
| 372 | //ust// /* |
| 373 | //ust// * Iterator kept in m->private. |
| 374 | //ust// * Restart iteration on all modules between reads because we do not lock |
| 375 | //ust// * the module mutex between those. |
| 376 | //ust// */ |
| 377 | //ust// int ret; |
| 378 | //ust// struct marker_iter *iter; |
| 379 | //ust// |
| 380 | //ust// iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 381 | //ust// if (!iter) |
| 382 | //ust// return -ENOMEM; |
| 383 | //ust// |
| 384 | //ust// ret = seq_open(file, <t_seq_op); |
| 385 | //ust// if (ret == 0) |
| 386 | //ust// ((struct seq_file *)file->private_data)->private = iter; |
| 387 | //ust// else |
| 388 | //ust// kfree(iter); |
| 389 | //ust// return ret; |
| 390 | //ust// } |
| 391 | //ust// |
| 392 | //ust// static struct file_operations ltt_fops = { |
| 393 | //ust// .write = ltt_write, |
| 394 | //ust// .open = ltt_open, |
| 395 | //ust// .read = seq_read, |
| 396 | //ust// .llseek = seq_lseek, |
| 397 | //ust// .release = seq_release_private, |
| 398 | //ust// }; |
| 399 | |
| 400 | static void disconnect_all_markers(void) |
| 401 | { |
| 402 | struct ltt_active_marker *pdata, *tmp; |
| 403 | |
| 404 | list_for_each_entry_safe(pdata, tmp, &markers_loaded_list, node) { |
| 405 | marker_probe_unregister_private_data(pdata->probe->probe_func, |
| 406 | pdata); |
| 407 | list_del(&pdata->node); |
| 408 | free(pdata); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static char initialized = 0; |
| 413 | |
| 414 | void __attribute__((constructor)) init_marker_control(void) |
| 415 | { |
| 416 | if(!initialized) { |
| 417 | int ret; |
| 418 | |
| 419 | //ust// pentry = create_proc_entry("ltt", S_IRUSR|S_IWUSR, NULL); |
| 420 | //ust// if (!pentry) |
| 421 | //ust// return -EBUSY; |
| 422 | //ust// markers_loaded_cachep = KMEM_CACHE(ltt_active_marker, 0); |
| 423 | |
| 424 | ret = ltt_probe_register(&default_probe); |
| 425 | BUG_ON(ret); |
| 426 | ret = ltt_marker_connect("metadata", "core_marker_format", |
| 427 | DEFAULT_PROBE); |
| 428 | BUG_ON(ret); |
| 429 | ret = ltt_marker_connect("metadata", "core_marker_id", DEFAULT_PROBE); |
| 430 | BUG_ON(ret); |
| 431 | //ust// pentry->proc_fops = <t_fops; |
| 432 | |
| 433 | initialized = 1; |
| 434 | } |
| 435 | } |
| 436 | //ust// module_init(marker_control_init); |
| 437 | |
| 438 | static void __attribute__((destructor)) marker_control_exit(void) |
| 439 | { |
| 440 | int ret; |
| 441 | |
| 442 | //ust// remove_proc_entry("ltt", NULL); |
| 443 | ret = ltt_marker_disconnect("metadata", "core_marker_format", |
| 444 | DEFAULT_PROBE); |
| 445 | BUG_ON(ret); |
| 446 | ret = ltt_marker_disconnect("metadata", "core_marker_id", |
| 447 | DEFAULT_PROBE); |
| 448 | BUG_ON(ret); |
| 449 | ret = ltt_probe_unregister(&default_probe); |
| 450 | BUG_ON(ret); |
| 451 | disconnect_all_markers(); |
| 452 | //ust// kmem_cache_destroy(markers_loaded_cachep); |
| 453 | //ust// marker_synchronize_unregister(); |
| 454 | } |
| 455 | //ust// module_exit(marker_control_exit); |
| 456 | |
| 457 | //ust// MODULE_LICENSE("GPL"); |
| 458 | //ust// MODULE_AUTHOR("Mathieu Desnoyers"); |
| 459 | //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Marker Control"); |