1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 * Copyright (C) 2022, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
4 *
5 */
6 #include <stdlib.h>
7 #include <errno.h>
8
9 #include "tracefs.h"
10 #include "tracefs-local.h"
11
12 #define UPROBE_DEFAULT_GROUP "uprobes"
13
14 static struct tracefs_dynevent *
uprobe_alloc(enum tracefs_dynevent_type type,const char * system,const char * event,const char * file,unsigned long long offset,const char * fetchargs)15 uprobe_alloc(enum tracefs_dynevent_type type, const char *system, const char *event,
16 const char *file, unsigned long long offset, const char *fetchargs)
17 {
18 struct tracefs_dynevent *kp;
19 char *target;
20
21 if (!event || !file) {
22 errno = EINVAL;
23 return NULL;
24 }
25
26 if (!system)
27 system = UPROBE_DEFAULT_GROUP;
28
29 if (asprintf(&target, "%s:0x%0*llx", file, (int)(sizeof(void *) * 2), offset) < 0)
30 return NULL;
31
32 kp = dynevent_alloc(type, system, event, target, fetchargs);
33 free(target);
34
35 return kp;
36 }
37
38 /**
39 * tracefs_uprobe_alloc - Allocate new user probe (uprobe)
40 * @system: The system name (NULL for the default uprobes)
41 * @event: The name of the event to create
42 * @file: The full path to the binary file, where the uprobe will be set
43 * @offset: Offset within the @file
44 * @fetchargs: String with arguments, that will be fetched with the uprobe
45 *
46 * Allocate new uprobe context that will be in the @system group
47 * (or uprobes if @system is NULL) and with @event name. The new uprobe will be
48 * attached to @offset within the @file. The arguments described in @fetchargs
49 * will fetched with the uprobe. See linux/Documentation/trace/uprobetracer.rst
50 * for more details.
51 *
52 * The uprobe is not created in the system.
53 *
54 * Return a pointer to a uprobe context on success, or NULL on error.
55 * The returned pointer must be freed with tracefs_dynevent_free()
56 *
57 */
58 struct tracefs_dynevent *
tracefs_uprobe_alloc(const char * system,const char * event,const char * file,unsigned long long offset,const char * fetchargs)59 tracefs_uprobe_alloc(const char *system, const char *event,
60 const char *file, unsigned long long offset, const char *fetchargs)
61 {
62 return uprobe_alloc(TRACEFS_DYNEVENT_UPROBE, system, event, file, offset, fetchargs);
63 }
64
65 /**
66 * tracefs_uretprobe_alloc - Allocate new user return probe (uretprobe)
67 * @system: The system name (NULL for the default uprobes)
68 * @event: The name of the event to create
69 * @file: The full path to the binary file, where the uretprobe will be set
70 * @offset: Offset within the @file
71 * @fetchargs: String with arguments, that will be fetched with the uretprobe
72 *
73 * Allocate mew uretprobe context that will be in the @system group
74 * (or uprobes if @system is NULL) and with @event name. The new uretprobe will
75 * be attached to @offset within the @file. The arguments described in @fetchargs
76 * will fetched with the uprobe. See linux/Documentation/trace/uprobetracer.rst
77 * for more details.
78 *
79 * The uretprobe is not created in the system.
80 *
81 * Return a pointer to a uretprobe context on success, or NULL on error.
82 * The returned pointer must be freed with tracefs_dynevent_free()
83 *
84 */
85 struct tracefs_dynevent *
tracefs_uretprobe_alloc(const char * system,const char * event,const char * file,unsigned long long offset,const char * fetchargs)86 tracefs_uretprobe_alloc(const char *system, const char *event,
87 const char *file, unsigned long long offset, const char *fetchargs)
88 {
89 return uprobe_alloc(TRACEFS_DYNEVENT_URETPROBE, system, event, file, offset, fetchargs);
90 }
91