-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlttng-events.c
2716 lines (2475 loc) · 65.8 KB
/
lttng-events.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* lttng-events.c
*
* Holds LTTng per-session event registry.
*
* Copyright (C) 2010-2012 Mathieu Desnoyers <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; only
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* This page_alloc.h wrapper needs to be included before gfpflags.h because it
* overrides a function with a define.
*/
#include "wrapper/page_alloc.h"
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/utsname.h>
#include <linux/err.h>
#include <linux/seq_file.h>
#include <linux/file.h>
#include <linux/anon_inodes.h>
#include <wrapper/file.h>
#include <linux/jhash.h>
#include <linux/uaccess.h>
#include <linux/vmalloc.h>
#include <wrapper/uuid.h>
#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
#include <wrapper/random.h>
#include <wrapper/tracepoint.h>
#include <wrapper/list.h>
#include <lttng-kernel-version.h>
#include <lttng-events.h>
#include <lttng-tracer.h>
#include <lttng-abi-old.h>
#include <lttng-endian.h>
#include <wrapper/vzalloc.h>
#include <wrapper/ringbuffer/backend.h>
#include <wrapper/ringbuffer/frontend.h>
#define METADATA_CACHE_DEFAULT_SIZE 4096
static LIST_HEAD(sessions);
static LIST_HEAD(lttng_transport_list);
/*
* Protect the sessions and metadata caches.
*/
static DEFINE_MUTEX(sessions_mutex);
static struct kmem_cache *event_cache;
static void lttng_session_lazy_sync_enablers(struct lttng_session *session);
static void lttng_session_sync_enablers(struct lttng_session *session);
static void lttng_enabler_destroy(struct lttng_enabler *enabler);
static void _lttng_event_destroy(struct lttng_event *event);
static void _lttng_channel_destroy(struct lttng_channel *chan);
static int _lttng_event_unregister(struct lttng_event *event);
static
int _lttng_event_metadata_statedump(struct lttng_session *session,
struct lttng_channel *chan,
struct lttng_event *event);
static
int _lttng_session_metadata_statedump(struct lttng_session *session);
static
void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
static
int _lttng_field_statedump(struct lttng_session *session,
const struct lttng_event_field *field,
size_t nesting);
void synchronize_trace(void)
{
synchronize_sched();
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
#ifdef CONFIG_PREEMPT_RT_FULL
synchronize_rcu();
#endif
#else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
#ifdef CONFIG_PREEMPT_RT
synchronize_rcu();
#endif
#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
}
void lttng_lock_sessions(void)
{
mutex_lock(&sessions_mutex);
}
void lttng_unlock_sessions(void)
{
mutex_unlock(&sessions_mutex);
}
/*
* Called with sessions lock held.
*/
int lttng_session_active(void)
{
struct lttng_session *iter;
list_for_each_entry(iter, &sessions, list) {
if (iter->active)
return 1;
}
return 0;
}
struct lttng_session *lttng_session_create(void)
{
struct lttng_session *session;
struct lttng_metadata_cache *metadata_cache;
int i;
mutex_lock(&sessions_mutex);
session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
if (!session)
goto err;
INIT_LIST_HEAD(&session->chan);
INIT_LIST_HEAD(&session->events);
uuid_le_gen(&session->uuid);
metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
GFP_KERNEL);
if (!metadata_cache)
goto err_free_session;
metadata_cache->data = lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE);
if (!metadata_cache->data)
goto err_free_cache;
metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
kref_init(&metadata_cache->refcount);
mutex_init(&metadata_cache->lock);
session->metadata_cache = metadata_cache;
INIT_LIST_HEAD(&metadata_cache->metadata_stream);
memcpy(&metadata_cache->uuid, &session->uuid,
sizeof(metadata_cache->uuid));
INIT_LIST_HEAD(&session->enablers_head);
for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++)
INIT_HLIST_HEAD(&session->events_ht.table[i]);
list_add(&session->list, &sessions);
mutex_unlock(&sessions_mutex);
return session;
err_free_cache:
kfree(metadata_cache);
err_free_session:
kfree(session);
err:
mutex_unlock(&sessions_mutex);
return NULL;
}
void metadata_cache_destroy(struct kref *kref)
{
struct lttng_metadata_cache *cache =
container_of(kref, struct lttng_metadata_cache, refcount);
vfree(cache->data);
kfree(cache);
}
void lttng_session_destroy(struct lttng_session *session)
{
struct lttng_channel *chan, *tmpchan;
struct lttng_event *event, *tmpevent;
struct lttng_metadata_stream *metadata_stream;
struct lttng_enabler *enabler, *tmpenabler;
int ret;
mutex_lock(&sessions_mutex);
ACCESS_ONCE(session->active) = 0;
list_for_each_entry(chan, &session->chan, list) {
ret = lttng_syscalls_unregister(chan);
WARN_ON(ret);
}
list_for_each_entry(event, &session->events, list) {
ret = _lttng_event_unregister(event);
WARN_ON(ret);
}
synchronize_trace(); /* Wait for in-flight events to complete */
list_for_each_entry_safe(enabler, tmpenabler,
&session->enablers_head, node)
lttng_enabler_destroy(enabler);
list_for_each_entry_safe(event, tmpevent, &session->events, list)
_lttng_event_destroy(event);
list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
BUG_ON(chan->channel_type == METADATA_CHANNEL);
_lttng_channel_destroy(chan);
}
list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
_lttng_metadata_channel_hangup(metadata_stream);
if (session->pid_tracker)
lttng_pid_tracker_destroy(session->pid_tracker);
kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
list_del(&session->list);
mutex_unlock(&sessions_mutex);
kfree(session);
}
int lttng_session_statedump(struct lttng_session *session)
{
int ret;
mutex_lock(&sessions_mutex);
ret = lttng_statedump_start(session);
mutex_unlock(&sessions_mutex);
return ret;
}
int lttng_session_enable(struct lttng_session *session)
{
int ret = 0;
struct lttng_channel *chan;
mutex_lock(&sessions_mutex);
if (session->active) {
ret = -EBUSY;
goto end;
}
/* Set transient enabler state to "enabled" */
session->tstate = 1;
/*
* Snapshot the number of events per channel to know the type of header
* we need to use.
*/
list_for_each_entry(chan, &session->chan, list) {
if (chan->header_type)
continue; /* don't change it if session stop/restart */
if (chan->free_event_id < 31)
chan->header_type = 1; /* compact */
else
chan->header_type = 2; /* large */
}
/* We need to sync enablers with session before activation. */
lttng_session_sync_enablers(session);
/* Clear each stream's quiescent state. */
list_for_each_entry(chan, &session->chan, list) {
if (chan->channel_type != METADATA_CHANNEL)
lib_ring_buffer_clear_quiescent_channel(chan->chan);
}
ACCESS_ONCE(session->active) = 1;
ACCESS_ONCE(session->been_active) = 1;
ret = _lttng_session_metadata_statedump(session);
if (ret) {
ACCESS_ONCE(session->active) = 0;
goto end;
}
ret = lttng_statedump_start(session);
if (ret)
ACCESS_ONCE(session->active) = 0;
end:
mutex_unlock(&sessions_mutex);
return ret;
}
int lttng_session_disable(struct lttng_session *session)
{
int ret = 0;
struct lttng_channel *chan;
mutex_lock(&sessions_mutex);
if (!session->active) {
ret = -EBUSY;
goto end;
}
ACCESS_ONCE(session->active) = 0;
/* Set transient enabler state to "disabled" */
session->tstate = 0;
lttng_session_sync_enablers(session);
/* Set each stream's quiescent state. */
list_for_each_entry(chan, &session->chan, list) {
if (chan->channel_type != METADATA_CHANNEL)
lib_ring_buffer_set_quiescent_channel(chan->chan);
}
end:
mutex_unlock(&sessions_mutex);
return ret;
}
int lttng_session_metadata_regenerate(struct lttng_session *session)
{
int ret = 0;
struct lttng_channel *chan;
struct lttng_event *event;
struct lttng_metadata_cache *cache = session->metadata_cache;
struct lttng_metadata_stream *stream;
mutex_lock(&sessions_mutex);
if (!session->active) {
ret = -EBUSY;
goto end;
}
mutex_lock(&cache->lock);
memset(cache->data, 0, cache->cache_alloc);
cache->metadata_written = 0;
cache->version++;
list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) {
stream->metadata_out = 0;
stream->metadata_in = 0;
}
mutex_unlock(&cache->lock);
session->metadata_dumped = 0;
list_for_each_entry(chan, &session->chan, list) {
chan->metadata_dumped = 0;
}
list_for_each_entry(event, &session->events, list) {
event->metadata_dumped = 0;
}
ret = _lttng_session_metadata_statedump(session);
end:
mutex_unlock(&sessions_mutex);
return ret;
}
int lttng_channel_enable(struct lttng_channel *channel)
{
int ret = 0;
mutex_lock(&sessions_mutex);
if (channel->channel_type == METADATA_CHANNEL) {
ret = -EPERM;
goto end;
}
if (channel->enabled) {
ret = -EEXIST;
goto end;
}
/* Set transient enabler state to "enabled" */
channel->tstate = 1;
lttng_session_sync_enablers(channel->session);
/* Set atomically the state to "enabled" */
ACCESS_ONCE(channel->enabled) = 1;
end:
mutex_unlock(&sessions_mutex);
return ret;
}
int lttng_channel_disable(struct lttng_channel *channel)
{
int ret = 0;
mutex_lock(&sessions_mutex);
if (channel->channel_type == METADATA_CHANNEL) {
ret = -EPERM;
goto end;
}
if (!channel->enabled) {
ret = -EEXIST;
goto end;
}
/* Set atomically the state to "disabled" */
ACCESS_ONCE(channel->enabled) = 0;
/* Set transient enabler state to "enabled" */
channel->tstate = 0;
lttng_session_sync_enablers(channel->session);
end:
mutex_unlock(&sessions_mutex);
return ret;
}
int lttng_event_enable(struct lttng_event *event)
{
int ret = 0;
mutex_lock(&sessions_mutex);
if (event->chan->channel_type == METADATA_CHANNEL) {
ret = -EPERM;
goto end;
}
if (event->enabled) {
ret = -EEXIST;
goto end;
}
switch (event->instrumentation) {
case LTTNG_KERNEL_TRACEPOINT:
case LTTNG_KERNEL_SYSCALL:
ret = -EINVAL;
break;
case LTTNG_KERNEL_KPROBE:
case LTTNG_KERNEL_FUNCTION:
case LTTNG_KERNEL_NOOP:
ACCESS_ONCE(event->enabled) = 1;
break;
case LTTNG_KERNEL_KRETPROBE:
ret = lttng_kretprobes_event_enable_state(event, 1);
break;
default:
WARN_ON_ONCE(1);
ret = -EINVAL;
}
end:
mutex_unlock(&sessions_mutex);
return ret;
}
int lttng_event_disable(struct lttng_event *event)
{
int ret = 0;
mutex_lock(&sessions_mutex);
if (event->chan->channel_type == METADATA_CHANNEL) {
ret = -EPERM;
goto end;
}
if (!event->enabled) {
ret = -EEXIST;
goto end;
}
switch (event->instrumentation) {
case LTTNG_KERNEL_TRACEPOINT:
case LTTNG_KERNEL_SYSCALL:
ret = -EINVAL;
break;
case LTTNG_KERNEL_KPROBE:
case LTTNG_KERNEL_FUNCTION:
case LTTNG_KERNEL_NOOP:
ACCESS_ONCE(event->enabled) = 0;
break;
case LTTNG_KERNEL_KRETPROBE:
ret = lttng_kretprobes_event_enable_state(event, 0);
break;
default:
WARN_ON_ONCE(1);
ret = -EINVAL;
}
end:
mutex_unlock(&sessions_mutex);
return ret;
}
static struct lttng_transport *lttng_transport_find(const char *name)
{
struct lttng_transport *transport;
list_for_each_entry(transport, <tng_transport_list, node) {
if (!strcmp(transport->name, name))
return transport;
}
return NULL;
}
struct lttng_channel *lttng_channel_create(struct lttng_session *session,
const char *transport_name,
void *buf_addr,
size_t subbuf_size, size_t num_subbuf,
unsigned int switch_timer_interval,
unsigned int read_timer_interval,
enum channel_type channel_type)
{
struct lttng_channel *chan;
struct lttng_transport *transport = NULL;
mutex_lock(&sessions_mutex);
if (session->been_active && channel_type != METADATA_CHANNEL)
goto active; /* Refuse to add channel to active session */
transport = lttng_transport_find(transport_name);
if (!transport) {
printk(KERN_WARNING "LTTng transport %s not found\n",
transport_name);
goto notransport;
}
if (!try_module_get(transport->owner)) {
printk(KERN_WARNING "LTT : Can't lock transport module.\n");
goto notransport;
}
chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
if (!chan)
goto nomem;
chan->session = session;
chan->id = session->free_chan_id++;
chan->ops = &transport->ops;
/*
* Note: the channel creation op already writes into the packet
* headers. Therefore the "chan" information used as input
* should be already accessible.
*/
chan->chan = transport->ops.channel_create(transport_name,
chan, buf_addr, subbuf_size, num_subbuf,
switch_timer_interval, read_timer_interval);
if (!chan->chan)
goto create_error;
chan->tstate = 1;
chan->enabled = 1;
chan->transport = transport;
chan->channel_type = channel_type;
list_add(&chan->list, &session->chan);
mutex_unlock(&sessions_mutex);
return chan;
create_error:
kfree(chan);
nomem:
if (transport)
module_put(transport->owner);
notransport:
active:
mutex_unlock(&sessions_mutex);
return NULL;
}
/*
* Only used internally at session destruction for per-cpu channels, and
* when metadata channel is released.
* Needs to be called with sessions mutex held.
*/
static
void _lttng_channel_destroy(struct lttng_channel *chan)
{
chan->ops->channel_destroy(chan->chan);
module_put(chan->transport->owner);
list_del(&chan->list);
lttng_destroy_context(chan->ctx);
kfree(chan);
}
void lttng_metadata_channel_destroy(struct lttng_channel *chan)
{
BUG_ON(chan->channel_type != METADATA_CHANNEL);
/* Protect the metadata cache with the sessions_mutex. */
mutex_lock(&sessions_mutex);
_lttng_channel_destroy(chan);
mutex_unlock(&sessions_mutex);
}
EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
static
void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
{
stream->finalized = 1;
wake_up_interruptible(&stream->read_wait);
}
/*
* Supports event creation while tracing session is active.
* Needs to be called with sessions mutex held.
*/
struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
struct lttng_kernel_event *event_param,
void *filter,
const struct lttng_event_desc *event_desc,
enum lttng_kernel_instrumentation itype)
{
struct lttng_session *session = chan->session;
struct lttng_event *event;
const char *event_name;
struct hlist_head *head;
size_t name_len;
uint32_t hash;
int ret;
if (chan->free_event_id == -1U) {
ret = -EMFILE;
goto full;
}
switch (itype) {
case LTTNG_KERNEL_TRACEPOINT:
event_name = event_desc->name;
break;
case LTTNG_KERNEL_KPROBE:
case LTTNG_KERNEL_KRETPROBE:
case LTTNG_KERNEL_FUNCTION:
case LTTNG_KERNEL_NOOP:
case LTTNG_KERNEL_SYSCALL:
event_name = event_param->name;
break;
default:
WARN_ON_ONCE(1);
ret = -EINVAL;
goto type_error;
}
name_len = strlen(event_name);
hash = jhash(event_name, name_len, 0);
head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
lttng_hlist_for_each_entry(event, head, hlist) {
WARN_ON_ONCE(!event->desc);
if (!strncmp(event->desc->name, event_name,
LTTNG_KERNEL_SYM_NAME_LEN - 1)
&& chan == event->chan) {
ret = -EEXIST;
goto exist;
}
}
event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
if (!event) {
ret = -ENOMEM;
goto cache_error;
}
event->chan = chan;
event->filter = filter;
event->id = chan->free_event_id++;
event->instrumentation = itype;
event->evtype = LTTNG_TYPE_EVENT;
INIT_LIST_HEAD(&event->bytecode_runtime_head);
INIT_LIST_HEAD(&event->enablers_ref_head);
switch (itype) {
case LTTNG_KERNEL_TRACEPOINT:
/* Event will be enabled by enabler sync. */
event->enabled = 0;
event->registered = 0;
event->desc = lttng_event_get(event_name);
if (!event->desc) {
ret = -ENOENT;
goto register_error;
}
/* Populate lttng_event structure before event registration. */
smp_wmb();
break;
case LTTNG_KERNEL_KPROBE:
/*
* Needs to be explicitly enabled after creation, since
* we may want to apply filters.
*/
event->enabled = 0;
event->registered = 1;
/*
* Populate lttng_event structure before event
* registration.
*/
smp_wmb();
ret = lttng_kprobes_register(event_name,
event_param->u.kprobe.symbol_name,
event_param->u.kprobe.offset,
event_param->u.kprobe.addr,
event);
if (ret) {
ret = -EINVAL;
goto register_error;
}
ret = try_module_get(event->desc->owner);
WARN_ON_ONCE(!ret);
break;
case LTTNG_KERNEL_KRETPROBE:
{
struct lttng_event *event_return;
/* kretprobe defines 2 events */
/*
* Needs to be explicitly enabled after creation, since
* we may want to apply filters.
*/
event->enabled = 0;
event->registered = 1;
event_return =
kmem_cache_zalloc(event_cache, GFP_KERNEL);
if (!event_return) {
ret = -ENOMEM;
goto register_error;
}
event_return->chan = chan;
event_return->filter = filter;
event_return->id = chan->free_event_id++;
event_return->enabled = 0;
event_return->registered = 1;
event_return->instrumentation = itype;
/*
* Populate lttng_event structure before kretprobe registration.
*/
smp_wmb();
ret = lttng_kretprobes_register(event_name,
event_param->u.kretprobe.symbol_name,
event_param->u.kretprobe.offset,
event_param->u.kretprobe.addr,
event, event_return);
if (ret) {
kmem_cache_free(event_cache, event_return);
ret = -EINVAL;
goto register_error;
}
/* Take 2 refs on the module: one per event. */
ret = try_module_get(event->desc->owner);
WARN_ON_ONCE(!ret);
ret = try_module_get(event->desc->owner);
WARN_ON_ONCE(!ret);
ret = _lttng_event_metadata_statedump(chan->session, chan,
event_return);
WARN_ON_ONCE(ret > 0);
if (ret) {
kmem_cache_free(event_cache, event_return);
module_put(event->desc->owner);
module_put(event->desc->owner);
goto statedump_error;
}
list_add(&event_return->list, &chan->session->events);
break;
}
case LTTNG_KERNEL_FUNCTION:
/*
* Needs to be explicitly enabled after creation, since
* we may want to apply filters.
*/
event->enabled = 0;
event->registered = 1;
/*
* Populate lttng_event structure before event
* registration.
*/
smp_wmb();
ret = lttng_ftrace_register(event_name,
event_param->u.ftrace.symbol_name,
event);
if (ret) {
goto register_error;
}
ret = try_module_get(event->desc->owner);
WARN_ON_ONCE(!ret);
break;
case LTTNG_KERNEL_NOOP:
case LTTNG_KERNEL_SYSCALL:
/*
* Needs to be explicitly enabled after creation, since
* we may want to apply filters.
*/
event->enabled = 0;
event->registered = 0;
event->desc = event_desc;
if (!event->desc) {
ret = -EINVAL;
goto register_error;
}
break;
default:
WARN_ON_ONCE(1);
ret = -EINVAL;
goto register_error;
}
ret = _lttng_event_metadata_statedump(chan->session, chan, event);
WARN_ON_ONCE(ret > 0);
if (ret) {
goto statedump_error;
}
hlist_add_head(&event->hlist, head);
list_add(&event->list, &chan->session->events);
return event;
statedump_error:
/* If a statedump error occurs, events will not be readable. */
register_error:
kmem_cache_free(event_cache, event);
cache_error:
exist:
type_error:
full:
return ERR_PTR(ret);
}
struct lttng_event *lttng_event_create(struct lttng_channel *chan,
struct lttng_kernel_event *event_param,
void *filter,
const struct lttng_event_desc *event_desc,
enum lttng_kernel_instrumentation itype)
{
struct lttng_event *event;
mutex_lock(&sessions_mutex);
event = _lttng_event_create(chan, event_param, filter, event_desc,
itype);
mutex_unlock(&sessions_mutex);
return event;
}
/* Only used for tracepoints for now. */
static
void register_event(struct lttng_event *event)
{
const struct lttng_event_desc *desc;
int ret = -EINVAL;
if (event->registered)
return;
desc = event->desc;
switch (event->instrumentation) {
case LTTNG_KERNEL_TRACEPOINT:
ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
desc->probe_callback,
event);
break;
case LTTNG_KERNEL_SYSCALL:
ret = lttng_syscall_filter_enable(event->chan,
desc->name);
break;
case LTTNG_KERNEL_KPROBE:
case LTTNG_KERNEL_KRETPROBE:
case LTTNG_KERNEL_FUNCTION:
case LTTNG_KERNEL_NOOP:
ret = 0;
break;
default:
WARN_ON_ONCE(1);
}
if (!ret)
event->registered = 1;
}
/*
* Only used internally at session destruction.
*/
int _lttng_event_unregister(struct lttng_event *event)
{
const struct lttng_event_desc *desc;
int ret = -EINVAL;
if (!event->registered)
return 0;
desc = event->desc;
switch (event->instrumentation) {
case LTTNG_KERNEL_TRACEPOINT:
ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
event->desc->probe_callback,
event);
break;
case LTTNG_KERNEL_KPROBE:
lttng_kprobes_unregister(event);
ret = 0;
break;
case LTTNG_KERNEL_KRETPROBE:
lttng_kretprobes_unregister(event);
ret = 0;
break;
case LTTNG_KERNEL_FUNCTION:
lttng_ftrace_unregister(event);
ret = 0;
break;
case LTTNG_KERNEL_SYSCALL:
ret = lttng_syscall_filter_disable(event->chan,
desc->name);
break;
case LTTNG_KERNEL_NOOP:
ret = 0;
break;
default:
WARN_ON_ONCE(1);
}
if (!ret)
event->registered = 0;
return ret;
}
/*
* Only used internally at session destruction.
*/
static
void _lttng_event_destroy(struct lttng_event *event)
{
switch (event->instrumentation) {
case LTTNG_KERNEL_TRACEPOINT:
lttng_event_put(event->desc);
break;
case LTTNG_KERNEL_KPROBE:
module_put(event->desc->owner);
lttng_kprobes_destroy_private(event);
break;
case LTTNG_KERNEL_KRETPROBE:
module_put(event->desc->owner);
lttng_kretprobes_destroy_private(event);
break;
case LTTNG_KERNEL_FUNCTION:
module_put(event->desc->owner);
lttng_ftrace_destroy_private(event);
break;
case LTTNG_KERNEL_NOOP:
case LTTNG_KERNEL_SYSCALL:
break;
default:
WARN_ON_ONCE(1);
}
list_del(&event->list);
lttng_destroy_context(event->ctx);
kmem_cache_free(event_cache, event);
}
int lttng_session_track_pid(struct lttng_session *session, int pid)
{
int ret;
if (pid < -1)
return -EINVAL;
mutex_lock(&sessions_mutex);
if (pid == -1) {
/* track all pids: destroy tracker. */
if (session->pid_tracker) {
struct lttng_pid_tracker *lpf;
lpf = session->pid_tracker;
rcu_assign_pointer(session->pid_tracker, NULL);
synchronize_trace();
lttng_pid_tracker_destroy(lpf);
}
ret = 0;
} else {
if (!session->pid_tracker) {
struct lttng_pid_tracker *lpf;
lpf = lttng_pid_tracker_create();
if (!lpf) {
ret = -ENOMEM;
goto unlock;
}
ret = lttng_pid_tracker_add(lpf, pid);
rcu_assign_pointer(session->pid_tracker, lpf);
} else {
ret = lttng_pid_tracker_add(session->pid_tracker, pid);
}
}
unlock:
mutex_unlock(&sessions_mutex);
return ret;
}
int lttng_session_untrack_pid(struct lttng_session *session, int pid)
{
int ret;
if (pid < -1)
return -EINVAL;
mutex_lock(&sessions_mutex);
if (pid == -1) {
/* untrack all pids: replace by empty tracker. */
struct lttng_pid_tracker *old_lpf = session->pid_tracker;
struct lttng_pid_tracker *lpf;
lpf = lttng_pid_tracker_create();
if (!lpf) {
ret = -ENOMEM;
goto unlock;
}
rcu_assign_pointer(session->pid_tracker, lpf);
synchronize_trace();
if (old_lpf)
lttng_pid_tracker_destroy(old_lpf);
ret = 0;
} else {
if (!session->pid_tracker) {
ret = -ENOENT;
goto unlock;
}
ret = lttng_pid_tracker_del(session->pid_tracker, pid);
}
unlock:
mutex_unlock(&sessions_mutex);
return ret;
}
static
void *pid_list_start(struct seq_file *m, loff_t *pos)
{
struct lttng_session *session = m->private;
struct lttng_pid_tracker *lpf;
struct lttng_pid_hash_node *e;
int iter = 0, i;
mutex_lock(&sessions_mutex);
lpf = session->pid_tracker;
if (lpf) {
for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
struct hlist_head *head = &lpf->pid_hash[i];
lttng_hlist_for_each_entry(e, head, hlist) {
if (iter++ >= *pos)
return e;
}
}
} else {
/* PID tracker disabled. */
if (iter >= *pos && iter == 0) {