Ongoing transition to the generic ring buffer
[lttng-modules.git] / ltt-tracer.c
1 /*
2 * ltt/ltt-tracer.c
3 *
4 * Copyright (c) 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Tracing management internal kernel API. Trace buffer allocation/free, tracing
7 * start/stop.
8 *
9 * Author:
10 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 *
12 * Inspired from LTT :
13 * Karim Yaghmour (karim@opersys.com)
14 * Tom Zanussi (zanussi@us.ibm.com)
15 * Bob Wisniewski (bob@watson.ibm.com)
16 * And from K42 :
17 * Bob Wisniewski (bob@watson.ibm.com)
18 *
19 * Changelog:
20 * 22/09/06, Move to the marker/probes mechanism.
21 * 19/10/05, Complete lockless mechanism.
22 * 27/05/05, Modular redesign and rewrite.
23 *
24 * Dual LGPL v2.1/GPL v2 license.
25 */
26
27 #include <linux/time.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/rcupdate.h>
33 #include <linux/sched.h>
34 #include <linux/bitops.h>
35 #include <linux/fs.h>
36 #include <linux/cpu.h>
37 #include <linux/kref.h>
38 #include <linux/delay.h>
39 #include <linux/vmalloc.h>
40 #include <asm/atomic.h>
41
42 #include "ltt-tracer.h"
43
44 static void synchronize_trace(void)
45 {
46 synchronize_sched();
47 #ifdef CONFIG_PREEMPT_RT
48 synchronize_rcu();
49 #endif
50 }
51
52 static void async_wakeup(unsigned long data);
53
54 static DEFINE_TIMER(ltt_async_wakeup_timer, async_wakeup, 0, 0);
55
56 /* Default callbacks for modules */
57 notrace
58 int ltt_filter_control_default(enum ltt_filter_control_msg msg,
59 struct ltt_trace *trace)
60 {
61 return 0;
62 }
63
64 int ltt_statedump_default(struct ltt_trace *trace)
65 {
66 return 0;
67 }
68
69 /* Callbacks for registered modules */
70
71 int (*ltt_filter_control_functor)
72 (enum ltt_filter_control_msg msg, struct ltt_trace *trace) =
73 ltt_filter_control_default;
74 struct module *ltt_filter_control_owner;
75
76 /* These function pointers are protected by a trace activation check */
77 struct module *ltt_run_filter_owner;
78 int (*ltt_statedump_functor)(struct ltt_trace *trace) = ltt_statedump_default;
79 struct module *ltt_statedump_owner;
80
81 struct chan_info_struct {
82 const char *name;
83 unsigned int def_sb_size;
84 unsigned int def_n_sb;
85 } chan_infos[] = {
86 [LTT_CHANNEL_METADATA] = {
87 LTT_METADATA_CHANNEL,
88 LTT_DEFAULT_SUBBUF_SIZE_LOW,
89 LTT_DEFAULT_N_SUBBUFS_LOW,
90 },
91 [LTT_CHANNEL_FD_STATE] = {
92 LTT_FD_STATE_CHANNEL,
93 LTT_DEFAULT_SUBBUF_SIZE_LOW,
94 LTT_DEFAULT_N_SUBBUFS_LOW,
95 },
96 [LTT_CHANNEL_GLOBAL_STATE] = {
97 LTT_GLOBAL_STATE_CHANNEL,
98 LTT_DEFAULT_SUBBUF_SIZE_LOW,
99 LTT_DEFAULT_N_SUBBUFS_LOW,
100 },
101 [LTT_CHANNEL_IRQ_STATE] = {
102 LTT_IRQ_STATE_CHANNEL,
103 LTT_DEFAULT_SUBBUF_SIZE_LOW,
104 LTT_DEFAULT_N_SUBBUFS_LOW,
105 },
106 [LTT_CHANNEL_MODULE_STATE] = {
107 LTT_MODULE_STATE_CHANNEL,
108 LTT_DEFAULT_SUBBUF_SIZE_LOW,
109 LTT_DEFAULT_N_SUBBUFS_LOW,
110 },
111 [LTT_CHANNEL_NETIF_STATE] = {
112 LTT_NETIF_STATE_CHANNEL,
113 LTT_DEFAULT_SUBBUF_SIZE_LOW,
114 LTT_DEFAULT_N_SUBBUFS_LOW,
115 },
116 [LTT_CHANNEL_SOFTIRQ_STATE] = {
117 LTT_SOFTIRQ_STATE_CHANNEL,
118 LTT_DEFAULT_SUBBUF_SIZE_LOW,
119 LTT_DEFAULT_N_SUBBUFS_LOW,
120 },
121 [LTT_CHANNEL_SWAP_STATE] = {
122 LTT_SWAP_STATE_CHANNEL,
123 LTT_DEFAULT_SUBBUF_SIZE_LOW,
124 LTT_DEFAULT_N_SUBBUFS_LOW,
125 },
126 [LTT_CHANNEL_SYSCALL_STATE] = {
127 LTT_SYSCALL_STATE_CHANNEL,
128 LTT_DEFAULT_SUBBUF_SIZE_LOW,
129 LTT_DEFAULT_N_SUBBUFS_LOW,
130 },
131 [LTT_CHANNEL_TASK_STATE] = {
132 LTT_TASK_STATE_CHANNEL,
133 LTT_DEFAULT_SUBBUF_SIZE_LOW,
134 LTT_DEFAULT_N_SUBBUFS_LOW,
135 },
136 [LTT_CHANNEL_VM_STATE] = {
137 LTT_VM_STATE_CHANNEL,
138 LTT_DEFAULT_SUBBUF_SIZE_MED,
139 LTT_DEFAULT_N_SUBBUFS_MED,
140 },
141 [LTT_CHANNEL_FS] = {
142 LTT_FS_CHANNEL,
143 LTT_DEFAULT_SUBBUF_SIZE_MED,
144 LTT_DEFAULT_N_SUBBUFS_MED,
145 },
146 [LTT_CHANNEL_INPUT] = {
147 LTT_INPUT_CHANNEL,
148 LTT_DEFAULT_SUBBUF_SIZE_LOW,
149 LTT_DEFAULT_N_SUBBUFS_LOW,
150 },
151 [LTT_CHANNEL_IPC] = {
152 LTT_IPC_CHANNEL,
153 LTT_DEFAULT_SUBBUF_SIZE_LOW,
154 LTT_DEFAULT_N_SUBBUFS_LOW,
155 },
156 [LTT_CHANNEL_KERNEL] = {
157 LTT_KERNEL_CHANNEL,
158 LTT_DEFAULT_SUBBUF_SIZE_HIGH,
159 LTT_DEFAULT_N_SUBBUFS_HIGH,
160 },
161 [LTT_CHANNEL_MM] = {
162 LTT_MM_CHANNEL,
163 LTT_DEFAULT_SUBBUF_SIZE_MED,
164 LTT_DEFAULT_N_SUBBUFS_MED,
165 },
166 [LTT_CHANNEL_RCU] = {
167 LTT_RCU_CHANNEL,
168 LTT_DEFAULT_SUBBUF_SIZE_MED,
169 LTT_DEFAULT_N_SUBBUFS_MED,
170 },
171 [LTT_CHANNEL_DEFAULT] = {
172 NULL,
173 LTT_DEFAULT_SUBBUF_SIZE_MED,
174 LTT_DEFAULT_N_SUBBUFS_MED,
175 },
176 };
177
178 static enum ltt_channels get_channel_type_from_name(const char *name)
179 {
180 int i;
181
182 if (!name)
183 return LTT_CHANNEL_DEFAULT;
184
185 for (i = 0; i < ARRAY_SIZE(chan_infos); i++)
186 if (chan_infos[i].name && !strcmp(name, chan_infos[i].name))
187 return (enum ltt_channels)i;
188
189 return LTT_CHANNEL_DEFAULT;
190 }
191
192 /**
193 * ltt_module_register - LTT module registration
194 * @name: module type
195 * @function: callback to register
196 * @owner: module which owns the callback
197 *
198 * The module calling this registration function must ensure that no
199 * trap-inducing code will be executed by "function". E.g. vmalloc_sync_all()
200 * must be called between a vmalloc and the moment the memory is made visible to
201 * "function". This registration acts as a vmalloc_sync_all. Therefore, only if
202 * the module allocates virtual memory after its registration must it
203 * synchronize the TLBs.
204 */
205 int ltt_module_register(enum ltt_module_function name, void *function,
206 struct module *owner)
207 {
208 int ret = 0;
209
210 /*
211 * Make sure no page fault can be triggered by the module about to be
212 * registered. We deal with this here so we don't have to call
213 * vmalloc_sync_all() in each module's init.
214 */
215 vmalloc_sync_all();
216
217 switch (name) {
218 case LTT_FUNCTION_RUN_FILTER:
219 if (ltt_run_filter_owner != NULL) {
220 ret = -EEXIST;
221 goto end;
222 }
223 ltt_filter_register((ltt_run_filter_functor)function);
224 ltt_run_filter_owner = owner;
225 break;
226 case LTT_FUNCTION_FILTER_CONTROL:
227 if (ltt_filter_control_owner != NULL) {
228 ret = -EEXIST;
229 goto end;
230 }
231 ltt_filter_control_functor =
232 (int (*)(enum ltt_filter_control_msg,
233 struct ltt_trace *))function;
234 ltt_filter_control_owner = owner;
235 break;
236 case LTT_FUNCTION_STATEDUMP:
237 if (ltt_statedump_owner != NULL) {
238 ret = -EEXIST;
239 goto end;
240 }
241 ltt_statedump_functor =
242 (int (*)(struct ltt_trace *))function;
243 ltt_statedump_owner = owner;
244 break;
245 }
246 end:
247 return ret;
248 }
249 EXPORT_SYMBOL_GPL(ltt_module_register);
250
251 /**
252 * ltt_module_unregister - LTT module unregistration
253 * @name: module type
254 */
255 void ltt_module_unregister(enum ltt_module_function name)
256 {
257 switch (name) {
258 case LTT_FUNCTION_RUN_FILTER:
259 ltt_filter_unregister();
260 ltt_run_filter_owner = NULL;
261 /* Wait for preempt sections to finish */
262 synchronize_trace();
263 break;
264 case LTT_FUNCTION_FILTER_CONTROL:
265 ltt_filter_control_functor = ltt_filter_control_default;
266 ltt_filter_control_owner = NULL;
267 break;
268 case LTT_FUNCTION_STATEDUMP:
269 ltt_statedump_functor = ltt_statedump_default;
270 ltt_statedump_owner = NULL;
271 break;
272 }
273
274 }
275 EXPORT_SYMBOL_GPL(ltt_module_unregister);
276
277 static LIST_HEAD(ltt_transport_list);
278
279 /**
280 * ltt_transport_register - LTT transport registration
281 * @transport: transport structure
282 *
283 * Registers a transport which can be used as output to extract the data out of
284 * LTTng. The module calling this registration function must ensure that no
285 * trap-inducing code will be executed by the transport functions. E.g.
286 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
287 * is made visible to the transport function. This registration acts as a
288 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
289 * after its registration must it synchronize the TLBs.
290 */
291 void ltt_transport_register(struct ltt_transport *transport)
292 {
293 /*
294 * Make sure no page fault can be triggered by the module about to be
295 * registered. We deal with this here so we don't have to call
296 * vmalloc_sync_all() in each module's init.
297 */
298 vmalloc_sync_all();
299
300 ltt_lock_traces();
301 list_add_tail(&transport->node, &ltt_transport_list);
302 ltt_unlock_traces();
303 }
304 EXPORT_SYMBOL_GPL(ltt_transport_register);
305
306 /**
307 * ltt_transport_unregister - LTT transport unregistration
308 * @transport: transport structure
309 */
310 void ltt_transport_unregister(struct ltt_transport *transport)
311 {
312 ltt_lock_traces();
313 list_del(&transport->node);
314 ltt_unlock_traces();
315 }
316 EXPORT_SYMBOL_GPL(ltt_transport_unregister);
317
318 static inline
319 int is_channel_overwrite(enum ltt_channels chan, enum trace_mode mode)
320 {
321 switch (mode) {
322 case LTT_TRACE_NORMAL:
323 return 0;
324 case LTT_TRACE_FLIGHT:
325 switch (chan) {
326 case LTT_CHANNEL_METADATA:
327 return 0;
328 default:
329 return 1;
330 }
331 case LTT_TRACE_HYBRID:
332 switch (chan) {
333 case LTT_CHANNEL_KERNEL:
334 case LTT_CHANNEL_FS:
335 case LTT_CHANNEL_MM:
336 case LTT_CHANNEL_RCU:
337 case LTT_CHANNEL_IPC:
338 case LTT_CHANNEL_INPUT:
339 return 1;
340 default:
341 return 0;
342 }
343 default:
344 return 0;
345 }
346 }
347
348 /**
349 * _ltt_trace_find - find a trace by given name.
350 * trace_name: trace name
351 *
352 * Returns a pointer to the trace structure, NULL if not found.
353 */
354 static struct ltt_trace *_ltt_trace_find(const char *trace_name)
355 {
356 struct ltt_trace *trace;
357
358 list_for_each_entry(trace, &ltt_traces.head, list)
359 if (!strncmp(trace->trace_name, trace_name, NAME_MAX))
360 return trace;
361
362 return NULL;
363 }
364
365 /* _ltt_trace_find_setup :
366 * find a trace in setup list by given name.
367 *
368 * Returns a pointer to the trace structure, NULL if not found.
369 */
370 struct ltt_trace *_ltt_trace_find_setup(const char *trace_name)
371 {
372 struct ltt_trace *trace;
373
374 list_for_each_entry(trace, &ltt_traces.setup_head, list)
375 if (!strncmp(trace->trace_name, trace_name, NAME_MAX))
376 return trace;
377
378 return NULL;
379 }
380 EXPORT_SYMBOL_GPL(_ltt_trace_find_setup);
381
382 /**
383 * ltt_release_trace - Release a LTT trace
384 * @kref : reference count on the trace
385 */
386 void ltt_release_trace(struct kref *kref)
387 {
388 struct ltt_trace *trace = container_of(kref, struct ltt_trace, kref);
389
390 trace->ops->remove_dirs(trace);
391 module_put(trace->transport->owner);
392 ltt_channels_trace_free(trace);
393 kfree(trace);
394 }
395 EXPORT_SYMBOL_GPL(ltt_release_trace);
396
397 static inline void prepare_chan_size_num(unsigned int *subbuf_size,
398 unsigned int *n_subbufs)
399 {
400 /* Make sure the subbuffer size is larger than a page */
401 *subbuf_size = max_t(unsigned int, *subbuf_size, PAGE_SIZE);
402
403 /* round to next power of 2 */
404 *subbuf_size = 1 << get_count_order(*subbuf_size);
405 *n_subbufs = 1 << get_count_order(*n_subbufs);
406
407 /* Subbuf size and number must both be power of two */
408 WARN_ON(hweight32(*subbuf_size) != 1);
409 WARN_ON(hweight32(*n_subbufs) != 1);
410 }
411
412 int _ltt_trace_setup(const char *trace_name)
413 {
414 int err = 0;
415 struct ltt_trace *new_trace = NULL;
416 int metadata_index;
417 unsigned int chan;
418 enum ltt_channels chantype;
419
420 if (_ltt_trace_find_setup(trace_name)) {
421 printk(KERN_ERR "LTT : Trace name %s already used.\n",
422 trace_name);
423 err = -EEXIST;
424 goto traces_error;
425 }
426
427 if (_ltt_trace_find(trace_name)) {
428 printk(KERN_ERR "LTT : Trace name %s already used.\n",
429 trace_name);
430 err = -EEXIST;
431 goto traces_error;
432 }
433
434 new_trace = kzalloc(sizeof(struct ltt_trace), GFP_KERNEL);
435 if (!new_trace) {
436 printk(KERN_ERR
437 "LTT : Unable to allocate memory for trace %s\n",
438 trace_name);
439 err = -ENOMEM;
440 goto traces_error;
441 }
442 strncpy(new_trace->trace_name, trace_name, NAME_MAX);
443 if (ltt_channels_trace_alloc(&new_trace->nr_channels, 0)) {
444 printk(KERN_ERR
445 "LTT : Unable to allocate memory for chaninfo %s\n",
446 trace_name);
447 err = -ENOMEM;
448 goto trace_free;
449 }
450
451 /*
452 * Force metadata channel to no overwrite.
453 */
454 metadata_index = ltt_channels_get_index_from_name("metadata");
455 WARN_ON(metadata_index < 0);
456 new_trace->settings[metadata_index].overwrite = 0;
457
458 /*
459 * Set hardcoded tracer defaults for some channels
460 */
461 for (chan = 0; chan < new_trace->nr_channels; chan++) {
462 chantype = get_channel_type_from_name(
463 ltt_channels_get_name_from_index(chan));
464 new_trace->settings[chan].sb_size =
465 chan_infos[chantype].def_sb_size;
466 new_trace->settings[chan].n_sb =
467 chan_infos[chantype].def_n_sb;
468 }
469
470 list_add(&new_trace->list, &ltt_traces.setup_head);
471 return 0;
472
473 trace_free:
474 kfree(new_trace);
475 traces_error:
476 return err;
477 }
478 EXPORT_SYMBOL_GPL(_ltt_trace_setup);
479
480
481 int ltt_trace_setup(const char *trace_name)
482 {
483 int ret;
484 ltt_lock_traces();
485 ret = _ltt_trace_setup(trace_name);
486 ltt_unlock_traces();
487 return ret;
488 }
489 EXPORT_SYMBOL_GPL(ltt_trace_setup);
490
491 /* must be called from within a traces lock. */
492 static void _ltt_trace_free(struct ltt_trace *trace)
493 {
494 list_del(&trace->list);
495 kfree(trace);
496 }
497
498 int ltt_trace_set_type(const char *trace_name, const char *trace_type)
499 {
500 int err = 0;
501 struct ltt_trace *trace;
502 struct ltt_transport *tran_iter, *transport = NULL;
503
504 ltt_lock_traces();
505
506 trace = _ltt_trace_find_setup(trace_name);
507 if (!trace) {
508 printk(KERN_ERR "LTT : Trace not found %s\n", trace_name);
509 err = -ENOENT;
510 goto traces_error;
511 }
512
513 list_for_each_entry(tran_iter, &ltt_transport_list, node) {
514 if (!strcmp(tran_iter->name, trace_type)) {
515 transport = tran_iter;
516 break;
517 }
518 }
519 if (!transport) {
520 printk(KERN_ERR "LTT : Transport %s is not present.\n",
521 trace_type);
522 err = -EINVAL;
523 goto traces_error;
524 }
525
526 trace->transport = transport;
527
528 traces_error:
529 ltt_unlock_traces();
530 return err;
531 }
532 EXPORT_SYMBOL_GPL(ltt_trace_set_type);
533
534 int ltt_trace_set_channel_subbufsize(const char *trace_name,
535 const char *channel_name,
536 unsigned int size)
537 {
538 int err = 0;
539 struct ltt_trace *trace;
540 int index;
541
542 ltt_lock_traces();
543
544 trace = _ltt_trace_find_setup(trace_name);
545 if (!trace) {
546 printk(KERN_ERR "LTT : Trace not found %s\n", trace_name);
547 err = -ENOENT;
548 goto traces_error;
549 }
550
551 index = ltt_channels_get_index_from_name(channel_name);
552 if (index < 0) {
553 printk(KERN_ERR "LTT : Channel %s not found\n", channel_name);
554 err = -ENOENT;
555 goto traces_error;
556 }
557 trace->settings[index].sb_size = size;
558
559 traces_error:
560 ltt_unlock_traces();
561 return err;
562 }
563 EXPORT_SYMBOL_GPL(ltt_trace_set_channel_subbufsize);
564
565 int ltt_trace_set_channel_subbufcount(const char *trace_name,
566 const char *channel_name,
567 unsigned int cnt)
568 {
569 int err = 0;
570 struct ltt_trace *trace;
571 int index;
572
573 ltt_lock_traces();
574
575 trace = _ltt_trace_find_setup(trace_name);
576 if (!trace) {
577 printk(KERN_ERR "LTT : Trace not found %s\n", trace_name);
578 err = -ENOENT;
579 goto traces_error;
580 }
581
582 index = ltt_channels_get_index_from_name(channel_name);
583 if (index < 0) {
584 printk(KERN_ERR "LTT : Channel %s not found\n", channel_name);
585 err = -ENOENT;
586 goto traces_error;
587 }
588 trace->settings[index].n_sb = cnt;
589
590 traces_error:
591 ltt_unlock_traces();
592 return err;
593 }
594 EXPORT_SYMBOL_GPL(ltt_trace_set_channel_subbufcount);
595
596 int ltt_trace_set_channel_switch_timer(const char *trace_name,
597 const char *channel_name,
598 unsigned long interval)
599 {
600 int err = 0;
601 struct ltt_trace *trace;
602 int index;
603
604 ltt_lock_traces();
605
606 trace = _ltt_trace_find_setup(trace_name);
607 if (!trace) {
608 printk(KERN_ERR "LTT : Trace not found %s\n", trace_name);
609 err = -ENOENT;
610 goto traces_error;
611 }
612
613 index = ltt_channels_get_index_from_name(channel_name);
614 if (index < 0) {
615 printk(KERN_ERR "LTT : Channel %s not found\n", channel_name);
616 err = -ENOENT;
617 goto traces_error;
618 }
619 ltt_channels_trace_set_timer(&trace->settings[index], interval);
620
621 traces_error:
622 ltt_unlock_traces();
623 return err;
624 }
625 EXPORT_SYMBOL_GPL(ltt_trace_set_channel_switch_timer);
626
627 int ltt_trace_set_channel_overwrite(const char *trace_name,
628 const char *channel_name,
629 unsigned int overwrite)
630 {
631 int err = 0;
632 struct ltt_trace *trace;
633 int index;
634
635 ltt_lock_traces();
636
637 trace = _ltt_trace_find_setup(trace_name);
638 if (!trace) {
639 printk(KERN_ERR "LTT : Trace not found %s\n", trace_name);
640 err = -ENOENT;
641 goto traces_error;
642 }
643
644 /*
645 * Always put the metadata channel in non-overwrite mode :
646 * This is a very low traffic channel and it can't afford to have its
647 * data overwritten : this data (marker info) is necessary to be
648 * able to read the trace.
649 */
650 if (overwrite && !strcmp(channel_name, "metadata")) {
651 printk(KERN_ERR "LTT : Trying to set metadata channel to "
652 "overwrite mode\n");
653 err = -EINVAL;
654 goto traces_error;
655 }
656
657 index = ltt_channels_get_index_from_name(channel_name);
658 if (index < 0) {
659 printk(KERN_ERR "LTT : Channel %s not found\n", channel_name);
660 err = -ENOENT;
661 goto traces_error;
662 }
663
664 trace->settings[index].overwrite = overwrite;
665
666 traces_error:
667 ltt_unlock_traces();
668 return err;
669 }
670 EXPORT_SYMBOL_GPL(ltt_trace_set_channel_overwrite);
671
672 int ltt_trace_alloc(const char *trace_name)
673 {
674 int err = 0;
675 struct ltt_trace *trace;
676 int sb_size, n_sb;
677 unsigned long flags;
678 int chan;
679 const char *channel_name;
680
681 ltt_lock_traces();
682
683 trace = _ltt_trace_find_setup(trace_name);
684 if (!trace) {
685 printk(KERN_ERR "LTT : Trace not found %s\n", trace_name);
686 err = -ENOENT;
687 goto traces_error;
688 }
689
690 kref_init(&trace->kref);
691 init_waitqueue_head(&trace->kref_wq);
692 trace->active = 0;
693 get_trace_clock();
694 trace->freq_scale = trace_clock_freq_scale();
695
696 if (!trace->transport) {
697 printk(KERN_ERR "LTT : Transport is not set.\n");
698 err = -EINVAL;
699 goto transport_error;
700 }
701 if (!try_module_get(trace->transport->owner)) {
702 printk(KERN_ERR "LTT : Can't lock transport module.\n");
703 err = -ENODEV;
704 goto transport_error;
705 }
706 trace->ops = &trace->transport->ops;
707
708 err = trace->ops->create_dirs(trace);
709 if (err) {
710 printk(KERN_ERR "LTT : Can't create dir for trace %s.\n",
711 trace_name);
712 goto dirs_error;
713 }
714
715 local_irq_save(flags);
716 trace->start_freq = trace_clock_frequency();
717 trace->start_tsc = trace_clock_read64();
718 do_gettimeofday(&trace->start_time);
719 local_irq_restore(flags);
720
721 for (chan = 0; chan < trace->nr_channels; chan++) {
722 channel_name = ltt_channels_get_name_from_index(chan);
723 WARN_ON(!channel_name);
724 /*
725 * note: sb_size and n_sb will be overwritten with updated
726 * values by channel creation.
727 */
728 sb_size = trace->settings[chan].sb_size;
729 n_sb = trace->settings[chan].n_sb;
730 prepare_chan_size_num(&sb_size, &n_sb);
731 trace->channels[chan] = ltt_create_channel(channel_name,
732 trace, NULL, sb_size, n_sb,
733 trace->settings[chan].overwrite,
734 trace->settings[chan].switch_timer_interval,
735 trace->settings[chan].read_timer_interval);
736 if (err != 0) {
737 printk(KERN_ERR "LTT : Can't create channel %s.\n",
738 channel_name);
739 goto create_channel_error;
740 }
741 }
742
743 list_del(&trace->list);
744 if (list_empty(&ltt_traces.head))
745 set_kernel_trace_flag_all_tasks();
746 list_add_rcu(&trace->list, &ltt_traces.head);
747 synchronize_trace();
748
749 ltt_unlock_traces();
750
751 return 0;
752
753 create_channel_error:
754 for (chan--; chan >= 0; chan--)
755 ltt_channel_destroy(trace->channels[chan]);
756 trace->ops->remove_dirs(trace);
757
758 dirs_error:
759 module_put(trace->transport->owner);
760 transport_error:
761 put_trace_clock();
762 traces_error:
763 ltt_unlock_traces();
764 return err;
765 }
766 EXPORT_SYMBOL_GPL(ltt_trace_alloc);
767
768 /*
769 * It is worked as a wrapper for current version of ltt_control.ko.
770 * We will make a new ltt_control based on debugfs, and control each channel's
771 * buffer.
772 */
773 static
774 int ltt_trace_create(const char *trace_name, const char *trace_type,
775 enum trace_mode mode,
776 unsigned int subbuf_size_low, unsigned int n_subbufs_low,
777 unsigned int subbuf_size_med, unsigned int n_subbufs_med,
778 unsigned int subbuf_size_high, unsigned int n_subbufs_high)
779 {
780 int err = 0;
781
782 err = ltt_trace_setup(trace_name);
783 if (IS_ERR_VALUE(err))
784 return err;
785
786 err = ltt_trace_set_type(trace_name, trace_type);
787 if (IS_ERR_VALUE(err))
788 return err;
789
790 err = ltt_trace_alloc(trace_name);
791 if (IS_ERR_VALUE(err))
792 return err;
793
794 return err;
795 }
796
797 /* Must be called while sure that trace is in the list. */
798 static int _ltt_trace_destroy(struct ltt_trace *trace)
799 {
800 int err = -EPERM;
801
802 if (trace == NULL) {
803 err = -ENOENT;
804 goto traces_error;
805 }
806 if (trace->active) {
807 printk(KERN_ERR
808 "LTT : Can't destroy trace %s : tracer is active\n",
809 trace->trace_name);
810 err = -EBUSY;
811 goto active_error;
812 }
813 /* Everything went fine */
814 list_del_rcu(&trace->list);
815 synchronize_trace();
816 if (list_empty(&ltt_traces.head)) {
817 clear_kernel_trace_flag_all_tasks();
818 }
819 return 0;
820
821 /* error handling */
822 active_error:
823 traces_error:
824 return err;
825 }
826
827 /* Sleepable part of the destroy */
828 static void __ltt_trace_destroy(struct ltt_trace *trace)
829 {
830 int i;
831
832 for (i = 0; i < trace->nr_channels; i++)
833 ltt_channel_destroy(trace->channels[i]);
834 kref_put(&trace->kref, ltt_release_trace);
835 }
836
837 int ltt_trace_destroy(const char *trace_name)
838 {
839 int err = 0;
840 struct ltt_trace *trace;
841
842 ltt_lock_traces();
843
844 trace = _ltt_trace_find(trace_name);
845 if (trace) {
846 err = _ltt_trace_destroy(trace);
847 if (err)
848 goto error;
849
850 __ltt_trace_destroy(trace);
851 ltt_unlock_traces();
852 put_trace_clock();
853
854 return 0;
855 }
856
857 trace = _ltt_trace_find_setup(trace_name);
858 if (trace) {
859 _ltt_trace_free(trace);
860 ltt_unlock_traces();
861 return 0;
862 }
863
864 err = -ENOENT;
865
866 /* Error handling */
867 error:
868 ltt_unlock_traces();
869 return err;
870 }
871 EXPORT_SYMBOL_GPL(ltt_trace_destroy);
872
873 /* must be called from within a traces lock. */
874 static int _ltt_trace_start(struct ltt_trace *trace)
875 {
876 int err = 0;
877
878 if (trace == NULL) {
879 err = -ENOENT;
880 goto traces_error;
881 }
882 if (trace->active)
883 printk(KERN_INFO "LTT : Tracing already active for trace %s\n",
884 trace->trace_name);
885 if (!try_module_get(ltt_run_filter_owner)) {
886 err = -ENODEV;
887 printk(KERN_ERR "LTT : Can't lock filter module.\n");
888 goto get_ltt_run_filter_error;
889 }
890 trace->active = 1;
891 /* Read by trace points without protection : be careful */
892 ltt_traces.num_active_traces++;
893 return err;
894
895 /* error handling */
896 get_ltt_run_filter_error:
897 traces_error:
898 return err;
899 }
900
901 int ltt_trace_start(const char *trace_name)
902 {
903 int err = 0;
904 struct ltt_trace *trace;
905
906 ltt_lock_traces();
907
908 trace = _ltt_trace_find(trace_name);
909 err = _ltt_trace_start(trace);
910 if (err)
911 goto no_trace;
912
913 ltt_unlock_traces();
914
915 /*
916 * Call the kernel state dump.
917 * Events will be mixed with real kernel events, it's ok.
918 * Notice that there is no protection on the trace : that's exactly
919 * why we iterate on the list and check for trace equality instead of
920 * directly using this trace handle inside the logging function.
921 */
922
923 ltt_dump_marker_state(trace);
924
925 if (!try_module_get(ltt_statedump_owner)) {
926 err = -ENODEV;
927 printk(KERN_ERR
928 "LTT : Can't lock state dump module.\n");
929 } else {
930 ltt_statedump_functor(trace);
931 module_put(ltt_statedump_owner);
932 }
933
934 return err;
935
936 /* Error handling */
937 no_trace:
938 ltt_unlock_traces();
939 return err;
940 }
941 EXPORT_SYMBOL_GPL(ltt_trace_start);
942
943 /* must be called from within traces lock */
944 static int _ltt_trace_stop(struct ltt_trace *trace)
945 {
946 int err = -EPERM;
947
948 if (trace == NULL) {
949 err = -ENOENT;
950 goto traces_error;
951 }
952 if (!trace->active)
953 printk(KERN_INFO "LTT : Tracing not active for trace %s\n",
954 trace->trace_name);
955 if (trace->active) {
956 trace->active = 0;
957 ltt_traces.num_active_traces--;
958 synchronize_trace(); /* Wait for each tracing to be finished */
959 }
960 module_put(ltt_run_filter_owner);
961 /* Everything went fine */
962 return 0;
963
964 /* Error handling */
965 traces_error:
966 return err;
967 }
968
969 int ltt_trace_stop(const char *trace_name)
970 {
971 int err = 0;
972 struct ltt_trace *trace;
973
974 ltt_lock_traces();
975 trace = _ltt_trace_find(trace_name);
976 err = _ltt_trace_stop(trace);
977 ltt_unlock_traces();
978 return err;
979 }
980 EXPORT_SYMBOL_GPL(ltt_trace_stop);
981
982 /**
983 * ltt_control - Trace control in-kernel API
984 * @msg: Action to perform
985 * @trace_name: Trace on which the action must be done
986 * @trace_type: Type of trace (normal, flight, hybrid)
987 * @args: Arguments specific to the action
988 */
989 int ltt_control(enum ltt_control_msg msg, const char *trace_name,
990 const char *trace_type, union ltt_control_args args)
991 {
992 int err = -EPERM;
993
994 printk(KERN_ALERT "ltt_control : trace %s\n", trace_name);
995 switch (msg) {
996 case LTT_CONTROL_START:
997 printk(KERN_DEBUG "Start tracing %s\n", trace_name);
998 err = ltt_trace_start(trace_name);
999 break;
1000 case LTT_CONTROL_STOP:
1001 printk(KERN_DEBUG "Stop tracing %s\n", trace_name);
1002 err = ltt_trace_stop(trace_name);
1003 break;
1004 case LTT_CONTROL_CREATE_TRACE:
1005 printk(KERN_DEBUG "Creating trace %s\n", trace_name);
1006 err = ltt_trace_create(trace_name, trace_type,
1007 args.new_trace.mode,
1008 args.new_trace.subbuf_size_low,
1009 args.new_trace.n_subbufs_low,
1010 args.new_trace.subbuf_size_med,
1011 args.new_trace.n_subbufs_med,
1012 args.new_trace.subbuf_size_high,
1013 args.new_trace.n_subbufs_high);
1014 break;
1015 case LTT_CONTROL_DESTROY_TRACE:
1016 printk(KERN_DEBUG "Destroying trace %s\n", trace_name);
1017 err = ltt_trace_destroy(trace_name);
1018 break;
1019 }
1020 return err;
1021 }
1022 EXPORT_SYMBOL_GPL(ltt_control);
1023
1024 /**
1025 * ltt_filter_control - Trace filter control in-kernel API
1026 * @msg: Action to perform on the filter
1027 * @trace_name: Trace on which the action must be done
1028 */
1029 int ltt_filter_control(enum ltt_filter_control_msg msg, const char *trace_name)
1030 {
1031 int err;
1032 struct ltt_trace *trace;
1033
1034 printk(KERN_DEBUG "ltt_filter_control : trace %s\n", trace_name);
1035 ltt_lock_traces();
1036 trace = _ltt_trace_find(trace_name);
1037 if (trace == NULL) {
1038 printk(KERN_ALERT
1039 "Trace does not exist. Cannot proxy control request\n");
1040 err = -ENOENT;
1041 goto trace_error;
1042 }
1043 if (!try_module_get(ltt_filter_control_owner)) {
1044 err = -ENODEV;
1045 goto get_module_error;
1046 }
1047 switch (msg) {
1048 case LTT_FILTER_DEFAULT_ACCEPT:
1049 printk(KERN_DEBUG
1050 "Proxy filter default accept %s\n", trace_name);
1051 err = (*ltt_filter_control_functor)(msg, trace);
1052 break;
1053 case LTT_FILTER_DEFAULT_REJECT:
1054 printk(KERN_DEBUG
1055 "Proxy filter default reject %s\n", trace_name);
1056 err = (*ltt_filter_control_functor)(msg, trace);
1057 break;
1058 default:
1059 err = -EPERM;
1060 }
1061 module_put(ltt_filter_control_owner);
1062
1063 get_module_error:
1064 trace_error:
1065 ltt_unlock_traces();
1066 return err;
1067 }
1068 EXPORT_SYMBOL_GPL(ltt_filter_control);
1069
1070 int __init ltt_init(void)
1071 {
1072 /* Make sure no page fault can be triggered by this module */
1073 vmalloc_sync_all();
1074 init_timer_deferrable(&ltt_async_wakeup_timer);
1075 return 0;
1076 }
1077
1078 module_init(ltt_init)
1079
1080 static void __exit ltt_exit(void)
1081 {
1082 struct ltt_trace *trace;
1083 struct list_head *pos, *n;
1084
1085 ltt_lock_traces();
1086 /* Stop each trace, currently being read by RCU read-side */
1087 list_for_each_entry_rcu(trace, &ltt_traces.head, list)
1088 _ltt_trace_stop(trace);
1089 /* Wait for quiescent state. Readers have preemption disabled. */
1090 synchronize_trace();
1091 /* Safe iteration is now permitted. It does not have to be RCU-safe
1092 * because no readers are left. */
1093 list_for_each_safe(pos, n, &ltt_traces.head) {
1094 trace = container_of(pos, struct ltt_trace, list);
1095 /* _ltt_trace_destroy does a synchronize_trace() */
1096 _ltt_trace_destroy(trace);
1097 __ltt_trace_destroy(trace);
1098 }
1099 /* free traces in pre-alloc status */
1100 list_for_each_safe(pos, n, &ltt_traces.setup_head) {
1101 trace = container_of(pos, struct ltt_trace, list);
1102 _ltt_trace_free(trace);
1103 }
1104
1105 ltt_unlock_traces();
1106 }
1107
1108 module_exit(ltt_exit)
1109
1110 MODULE_LICENSE("GPL and additional rights");
1111 MODULE_AUTHOR("Mathieu Desnoyers");
1112 MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Tracer Kernel API");
This page took 0.058303 seconds and 4 git commands to generate.