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
|
// SPDX-License-Identifier: LGPL-2.1
/*
* Copyright (C) 2021 VMware Inc, Steven Rostedt <rostedt@goodmis.org>
*
* Updates:
* Copyright (C) 2021, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include "tracefs.h"
#include "tracefs-local.h"
#define DYNEVENTS_EVENTS "dynamic_events"
#define KPROBE_EVENTS "kprobe_events"
#define UPROBE_EVENTS "uprobe_events"
#define SYNTH_EVENTS "synthetic_events"
#define DYNEVENTS_DEFAULT_GROUP "dynamic"
#define EVENT_INDEX(B) (ffs(B) - 1)
struct dyn_events_desc;
static int dyn_generic_parse(struct dyn_events_desc *,
const char *, char *, struct tracefs_dynevent **);
static int dyn_synth_parse(struct dyn_events_desc *,
const char *, char *, struct tracefs_dynevent **);
static int dyn_generic_del(struct dyn_events_desc *, struct tracefs_dynevent *);
static int dyn_synth_del(struct dyn_events_desc *, struct tracefs_dynevent *);
struct dyn_events_desc {
enum tracefs_dynevent_type type;
const char *file;
const char *prefix;
int (*del)(struct dyn_events_desc *desc, struct tracefs_dynevent *dyn);
int (*parse)(struct dyn_events_desc *desc, const char *group,
char *line, struct tracefs_dynevent **ret_dyn);
} dynevents[] = {
{TRACEFS_DYNEVENT_KPROBE, KPROBE_EVENTS, "p", dyn_generic_del, dyn_generic_parse},
{TRACEFS_DYNEVENT_KRETPROBE, KPROBE_EVENTS, "r", dyn_generic_del, dyn_generic_parse},
{TRACEFS_DYNEVENT_UPROBE, UPROBE_EVENTS, "p", dyn_generic_del, dyn_generic_parse},
{TRACEFS_DYNEVENT_URETPROBE, UPROBE_EVENTS, "r", dyn_generic_del, dyn_generic_parse},
{TRACEFS_DYNEVENT_EPROBE, "", "e", dyn_generic_del, dyn_generic_parse},
{TRACEFS_DYNEVENT_SYNTH, SYNTH_EVENTS, "", dyn_synth_del, dyn_synth_parse},
};
static int dyn_generic_del(struct dyn_events_desc *desc, struct tracefs_dynevent *dyn)
{
char *str;
int ret;
if (dyn->system)
ret = asprintf(&str, "-:%s/%s", dyn->system, dyn->event);
else
ret = asprintf(&str, "-:%s", dyn->event);
if (ret < 0)
return -1;
ret = tracefs_instance_file_append(NULL, desc->file, str);
free(str);
return ret < 0 ? ret : 0;
}
/**
* tracefs_dynevent_free - Free a dynamic event context
* @devent: Pointer to a dynamic event context
*
* The dynamic event, described by this context, is not
* removed from the system by this API. It only frees the memory.
*/
void tracefs_dynevent_free(struct tracefs_dynevent *devent)
{
if (!devent)
return;
free(devent->system);
free(devent->event);
free(devent->address);
free(devent->format);
free(devent->prefix);
free(devent->trace_file);
free(devent);
}
static void parse_prefix(char *word, char **prefix, char **system, char **name)
{
char *sav;
*prefix = NULL;
*system = NULL;
*name = NULL;
*prefix = strtok_r(word, ":", &sav);
*system = strtok_r(NULL, "/", &sav);
if (!(*system))
return;
*name = strtok_r(NULL, " \t", &sav);
if (!(*name)) {
*name = *system;
*system = NULL;
}
}
/*
* Parse lines from dynamic_events, kprobe_events and uprobe_events files
* PREFIX[:[SYSTEM/]EVENT] [ADDRSS] [FORMAT]
*/
static int dyn_generic_parse(struct dyn_events_desc *desc, const char *group,
char *line, struct tracefs_dynevent **ret_dyn)
{
struct tracefs_dynevent *dyn;
char *word;
char *format = NULL;
char *address = NULL;
char *system;
char *prefix;
char *event;
char *sav;
if (strncmp(line, desc->prefix, strlen(desc->prefix)))
return -1;
word = strtok_r(line, " \t", &sav);
if (!word || *word == '\0')
return -1;
parse_prefix(word, &prefix, &system, &event);
if (!prefix)
return -1;
if (desc->type != TRACEFS_DYNEVENT_SYNTH) {
address = strtok_r(NULL, " \t", &sav);
if (!address || *address == '\0')
return -1;
}
format = strtok_r(NULL, "", &sav);
/* KPROBEs and UPROBEs share the same prefix, check the format */
if (desc->type & (TRACEFS_DYNEVENT_UPROBE | TRACEFS_DYNEVENT_URETPROBE)) {
if (!strchr(address, '/'))
return -1;
}
if (group && (!system || strcmp(group, system) != 0))
return -1;
if (!ret_dyn)
return 0;
dyn = calloc(1, sizeof(*dyn));
if (!dyn)
return -1;
dyn->type = desc->type;
dyn->trace_file = strdup(desc->file);
if (!dyn->trace_file)
goto error;
dyn->prefix = strdup(prefix);
if (!dyn->prefix)
goto error;
if (system) {
dyn->system = strdup(system);
if (!dyn->system)
goto error;
}
if (event) {
dyn->event = strdup(event);
if (!dyn->event)
goto error;
}
if (address) {
dyn->address = strdup(address);
if (!dyn->address)
goto error;
}
if (format) {
dyn->format = strdup(format);
if (!dyn->format)
goto error;
}
*ret_dyn = dyn;
return 0;
error:
tracefs_dynevent_free(dyn);
return -1;
}
static int dyn_synth_del(struct dyn_events_desc *desc, struct tracefs_dynevent *dyn)
{
char *str;
int ret;
if (!strcmp(desc->file, DYNEVENTS_EVENTS))
return dyn_generic_del(desc, dyn);
ret = asprintf(&str, "!%s", dyn->event);
if (ret < 0)
return -1;
ret = tracefs_instance_file_append(NULL, desc->file, str);
free(str);
return ret < 0 ? ret : 0;
}
/*
* Parse lines from synthetic_events file
* EVENT ARG [ARG]
*/
static int dyn_synth_parse(struct dyn_events_desc *desc, const char *group,
char *line, struct tracefs_dynevent **ret_dyn)
{
struct tracefs_dynevent *dyn;
char *format;
char *event;
char *sav;
if (!strcmp(desc->file, DYNEVENTS_EVENTS))
return dyn_generic_parse(desc, group, line, ret_dyn);
/* synthetic_events file has slightly different syntax */
event = strtok_r(line, " \t", &sav);
if (!event || *event == '\0')
return -1;
format = strtok_r(NULL, "", &sav);
if (!format || *format == '\0')
return -1;
if (!ret_dyn)
return 0;
dyn = calloc(1, sizeof(*dyn));
if (!dyn)
return -1;
dyn->type = desc->type;
dyn->trace_file = strdup(desc->file);
if (!dyn->trace_file)
goto error;
dyn->event = strdup(event);
if (!dyn->event)
goto error;
dyn->format = strdup(format+1);
if (!dyn->format)
goto error;
*ret_dyn = dyn;
return 0;
error:
tracefs_dynevent_free(dyn);
return -1;
}
static void init_devent_desc(void)
{
int i;
BUILD_BUG_ON(ARRAY_SIZE(dynevents) != EVENT_INDEX(TRACEFS_DYNEVENT_MAX));
if (!tracefs_file_exists(NULL, DYNEVENTS_EVENTS))
return;
/* Use ftrace dynamic_events, if available */
for (i = 0; i < EVENT_INDEX(TRACEFS_DYNEVENT_MAX); i++)
dynevents[i].file = DYNEVENTS_EVENTS;
dynevents[EVENT_INDEX(TRACEFS_DYNEVENT_SYNTH)].prefix = "s";
}
static struct dyn_events_desc *get_devent_desc(enum tracefs_dynevent_type type)
{
static bool init;
if (type >= TRACEFS_DYNEVENT_MAX)
return NULL;
if (!init) {
init_devent_desc();
init = true;
}
return &dynevents[EVENT_INDEX(type)];
}
/**
* dynevent_alloc - Allocate new dynamic event
* @type: Type of the dynamic event
* @system: The system name (NULL for the default dynamic)
* @event: Name of the event
* @addr: The function and offset (or address) to insert the probe
* @format: The format string to define the probe.
*
* Allocate a dynamic event context that will be in the @system group
* (or dynamic if @system is NULL). Have the name of @event and
* will be associated to @addr, if applicable for that event type
* (function name, with or without offset, or a address). And the @format will
* define the format of the kprobe.
* The dynamic event is not created in the system.
*
* Return a pointer to a dynamic event context on success, or NULL on error.
* The returned pointer must be freed with tracefs_dynevent_free()
*
* errno will be set to EINVAL if event is NULL.
*/
__hidden struct tracefs_dynevent *
dynevent_alloc(enum tracefs_dynevent_type type, const char *system,
const char *event, const char *address, const char *format)
{
struct tracefs_dynevent *devent;
struct dyn_events_desc *desc;
if (!event) {
errno = EINVAL;
return NULL;
}
desc = get_devent_desc(type);
if (!desc || !desc->file) {
errno = ENOTSUP;
return NULL;
}
devent = calloc(1, sizeof(*devent));
if (!devent)
return NULL;
devent->type = type;
devent->trace_file = strdup(desc->file);
if (!devent->trace_file)
goto err;
if (!system)
system = DYNEVENTS_DEFAULT_GROUP;
devent->system = strdup(system);
if (!devent->system)
goto err;
devent->event = strdup(event);
if (!devent->event)
goto err;
devent->prefix = strdup(desc->prefix);
if (!devent->prefix)
goto err;
if (address) {
devent->address = strdup(address);
if (!devent->address)
goto err;
}
if (format) {
devent->format = strdup(format);
if (!devent->format)
goto err;
}
return devent;
err:
tracefs_dynevent_free(devent);
return NULL;
}
/**
* tracefs_dynevent_create - Create a dynamic event in the system
* @devent: Pointer to a dynamic event context, describing the event
*
* Return 0 on success, or -1 on error.
*/
int tracefs_dynevent_create(struct tracefs_dynevent *devent)
{
char *str;
int ret;
if (!devent)
return -1;
if (devent->system && devent->system[0])
ret = asprintf(&str, "%s%s%s/%s %s %s\n",
devent->prefix, strlen(devent->prefix) ? ":" : "",
devent->system, devent->event,
devent->address ? devent->address : "",
devent->format ? devent->format : "");
else
ret = asprintf(&str, "%s%s%s %s %s\n",
devent->prefix, strlen(devent->prefix) ? ":" : "",
devent->event,
devent->address ? devent->address : "",
devent->format ? devent->format : "");
if (ret < 0)
return -1;
ret = tracefs_instance_file_append(NULL, devent->trace_file, str);
free(str);
return ret < 0 ? ret : 0;
}
static void disable_events(const char *system, const char *event,
char **list)
{
struct tracefs_instance *instance;
int i;
/*
* Note, this will not fail even on error.
* That is because even if something fails, it may still
* work enough to clear the kprobes. If that's the case
* the clearing after the loop will succeed and the function
* is a success, even though other parts had failed. If
* one of the kprobe events is enabled in one of the
* instances that fail, then the clearing will fail too
* and the function will return an error.
*/
tracefs_event_disable(NULL, system, event);
/* No need to test results */
if (!list)
return;
for (i = 0; list[i]; i++) {
instance = tracefs_instance_alloc(NULL, list[i]);
/* If this fails, try the next one */
if (!instance)
continue;
tracefs_event_disable(instance, system, event);
tracefs_instance_free(instance);
}
}
/**
* tracefs_dynevent_destroy - Remove a dynamic event from the system
* @devent: A dynamic event context, describing the dynamic event that will be deleted.
* @force: Will attempt to disable all events before removing them.
*
* The dynamic event context is not freed by this API. It only removes the event from the system.
* If there are any enabled events, and @force is not set, then it will error with -1 and errno
* to be EBUSY.
*
* Return 0 on success, or -1 on error.
*/
int tracefs_dynevent_destroy(struct tracefs_dynevent *devent, bool force)
{
struct dyn_events_desc *desc;
char **instance_list;
if (!devent)
return -1;
if (force) {
instance_list = tracefs_instances(NULL);
disable_events(devent->system, devent->event, instance_list);
tracefs_list_free(instance_list);
}
desc = get_devent_desc(devent->type);
if (!desc)
return -1;
return desc->del(desc, devent);
}
static int get_all_dynevents(enum tracefs_dynevent_type type, const char *system,
struct tracefs_dynevent ***ret_all)
{
struct dyn_events_desc *desc;
struct tracefs_dynevent *devent, **tmp, **all = NULL;
char *content;
int count = 0;
char *line;
char *next;
int ret;
desc = get_devent_desc(type);
if (!desc)
return -1;
content = tracefs_instance_file_read(NULL, desc->file, NULL);
if (!content)
return -1;
line = content;
do {
next = strchr(line, '\n');
if (next)
*next = '\0';
ret = desc->parse(desc, system, line, ret_all ? &devent : NULL);
if (!ret) {
if (ret_all) {
tmp = realloc(all, (count + 1) * sizeof(*tmp));
if (!tmp)
goto error;
all = tmp;
all[count] = devent;
}
count++;
}
line = next + 1;
} while (next);
free(content);
if (ret_all)
*ret_all = all;
return count;
error:
free(content);
free(all);
return -1;
}
/**
* tracefs_dynevent_list_free - Deletes an array of pointers to dynamic event contexts
* @events: An array of pointers to dynamic event contexts. The last element of the array
* must be a NULL pointer.
*/
void tracefs_dynevent_list_free(struct tracefs_dynevent **events)
{
int i;
if (!events)
return;
for (i = 0; events[i]; i++)
tracefs_dynevent_free(events[i]);
free(events);
}
/**
* tracefs_dynevent_get_all - return an array of pointers to dynamic events of given types
* @types: Dynamic event type, or bitmask of dynamic event types. If 0 is passed, all types
* are considered.
* @system: Get events from that system only. If @system is NULL, events from all systems
* are returned.
*
* Returns an array of pointers to dynamic events of given types that exist in the system.
* The array must be freed with tracefs_dynevent_list_free(). If there are no events a NULL
* pointer is returned.
*/
struct tracefs_dynevent **
tracefs_dynevent_get_all(unsigned int types, const char *system)
{
struct tracefs_dynevent **events, **tmp, **all_events = NULL;
int count, all = 0;
int i;
for (i = 1; i < TRACEFS_DYNEVENT_MAX; i <<= 1) {
if (types) {
if (i > types)
break;
if (!(types & i))
continue;
}
count = get_all_dynevents(i, system, &events);
if (count > 0) {
tmp = realloc(all_events, (all + count + 1) * sizeof(*tmp));
if (!tmp)
goto error;
all_events = tmp;
memcpy(all_events + all, events, count * sizeof(*events));
all += count;
/* Add a NULL pointer at the end */
all_events[all] = NULL;
free(events);
}
}
return all_events;
error:
if (all_events) {
for (i = 0; i < all; i++)
free(all_events[i]);
free(all_events);
}
return NULL;
}
/**
* tracefs_dynevent_get - return a single dynamic event if it exists
* @type; Dynamic event type
* @system: Get events from that system only. May be NULL.
* @event: Get event of the system type (may not be NULL)
*
* Returns the dynamic event of the given @type and @system for with the @event
* name. If @system is NULL, it will return the first dynamic event that it finds
* that matches the @event name.
*
* The returned event must be freed with tracefs_dynevent_free().
* NULL is returned if no event match is found, or other error.
*/
struct tracefs_dynevent *
tracefs_dynevent_get(enum tracefs_dynevent_type type, const char *system,
const char *event)
{
struct tracefs_dynevent **events;
struct tracefs_dynevent *devent = NULL;
int count;
int i;
if (!event) {
errno = -EINVAL;
return NULL;
}
count = get_all_dynevents(type, system, &events);
if (count <= 0)
return NULL;
for (i = 0; i < count; i++) {
if (strcmp(events[i]->event, event) == 0)
break;
}
if (i < count) {
devent = events[i];
events[i] = NULL;
}
tracefs_dynevent_list_free(events);
return devent;
}
/**
* tracefs_dynevent_destroy_all - removes all dynamic events of given types from the system
* @types: Dynamic event type, or bitmask of dynamic event types. If 0 is passed, all types
* are considered.
* @force: Will attempt to disable all events before removing them.
*
* Will remove all dynamic events of the given types from the system. If there are any enabled
* events, and @force is not set, then the removal of these will fail. If @force is set, then
* it will attempt to disable all the events in all instances before removing them.
*
* Returns zero if all requested events are removed successfully, or -1 if some of them are not
* removed.
*/
int tracefs_dynevent_destroy_all(unsigned int types, bool force)
{
struct tracefs_dynevent **all;
int ret = 0;
int i;
all = tracefs_dynevent_get_all(types, NULL);
if (!all)
return 0;
for (i = 0; all[i]; i++) {
if (tracefs_dynevent_destroy(all[i], force))
ret = -1;
}
tracefs_dynevent_list_free(all);
return ret;
}
/**
* dynevent_get_count - Count dynamic events of given types and system
* @types: Dynamic event type, or bitmask of dynamic event types. If 0 is passed, all types
* are considered.
* @system: Count events from that system only. If @system is NULL, events from all systems
* are counted.
*
* Return the count of requested dynamic events
*/
__hidden int dynevent_get_count(unsigned int types, const char *system)
{
int count, all = 0;
int i;
for (i = 1; i < TRACEFS_DYNEVENT_MAX; i <<= 1) {
if (types) {
if (i > types)
break;
if (!(types & i))
continue;
}
count = get_all_dynevents(i, system, NULL);
if (count > 0)
all += count;
}
return all;
}
static enum tracefs_dynevent_type
dynevent_info(struct tracefs_dynevent *dynevent, char **system,
char **event, char **prefix, char **addr, char **format)
{
char **lv[] = { system, event, prefix, addr, format };
char **rv[] = { &dynevent->system, &dynevent->event, &dynevent->prefix,
&dynevent->address, &dynevent->format };
int i;
for (i = 0; i < ARRAY_SIZE(lv); i++) {
if (lv[i]) {
if (*rv[i]) {
*lv[i] = strdup(*rv[i]);
if (!*lv[i])
goto error;
} else {
*lv[i] = NULL;
}
}
}
return dynevent->type;
error:
for (i--; i >= 0; i--) {
if (lv[i])
free(*lv[i]);
}
return TRACEFS_DYNEVENT_UNKNOWN;
}
/**
* tracefs_dynevent_info - return details of a dynamic event
* @dynevent: A dynamic event context, describing given dynamic event.
* @group: return, group in which the dynamic event is configured
* @event: return, name of the dynamic event
* @prefix: return, prefix string of the dynamic event
* @addr: return, the function and offset (or address) of the dynamic event
* @format: return, the format string of the dynamic event
*
* Returns the type of the dynamic event, or TRACEFS_DYNEVENT_UNKNOWN in case of an error.
* Any of the @group, @event, @prefix, @addr and @format parameters are optional.
* If a valid pointer is passed, in case of success - a string is allocated and returned.
* These strings must be freed with free().
*/
enum tracefs_dynevent_type
tracefs_dynevent_info(struct tracefs_dynevent *dynevent, char **system,
char **event, char **prefix, char **addr, char **format)
{
if (!dynevent)
return TRACEFS_DYNEVENT_UNKNOWN;
return dynevent_info(dynevent, system, event, prefix, addr, format);
}
/**
* tracefs_dynevent_get_event - return tep event representing the given dynamic event
* @tep: a handle to the trace event parser context that holds the events
* @dynevent: a dynamic event context, describing given dynamic event.
*
* Returns a pointer to a tep event describing the given dynamic event. The pointer
* is managed by the @tep handle and must not be freed. In case of an error, or in case
* the requested dynamic event is missing in the @tep handler - NULL is returned.
*/
struct tep_event *
tracefs_dynevent_get_event(struct tep_handle *tep, struct tracefs_dynevent *dynevent)
{
if (!tep || !dynevent || !dynevent->event)
return NULL;
return get_tep_event(tep, dynevent->system, dynevent->event);
}
|