Add USDT sample (#1229)
[platform/upstream/bcc.git] / examples / usdt_sample / scripts / bpf_text_shared.c
1 #include <linux/blkdev.h>
2 #include <uapi/linux/ptrace.h>
3
4 /**
5  * @brief Helper method to filter based on the specified inputString.
6  * @param inputString The operation input string to check against the filter.
7  * @return True if the specified inputString starts with the hard-coded FILTER_STRING; otherwise, false.
8  */
9 static inline bool filter(char const* inputString)
10 {
11     char needle[] = "FILTER_STRING"; ///< The FILTER STRING is replaced by python code.
12     char haystack[sizeof(needle)] = {};
13     bpf_probe_read(&haystack, sizeof(haystack), (void*)inputString);
14     for (int i = 0; i < sizeof(needle) - 1; ++i) {
15         if (needle[i] != haystack[i]) {
16             return false;
17         }
18     }
19     return true;
20 }
21
22 /**
23  * @brief Contains the operation start data to trace.
24  */
25 struct start_data_t
26 {
27     u64 operation_id; ///< The id of the operation.
28     char input[64];   ///< The input string of the request.
29     u64 start;        ///< Timestamp of the start operation (start timestamp).
30 };
31
32 /**
33  * @brief Contains the operation start data.
34  * key: the operation id.
35  * value: The operation start latency data.
36  */
37 BPF_HASH(start_hash, u64, struct start_data_t);
38
39 /**
40  * @brief Reads the operation request arguments and stores the start data in the hash.
41  * @param ctx The BPF context.
42  */
43 int trace_operation_start(struct pt_regs* ctx)
44 {
45     struct start_data_t start_data = {};
46     bpf_usdt_readarg_p(2, ctx, &start_data.input, sizeof(start_data.input));
47
48     FILTER ///< Replaced by python code.
49
50     bpf_usdt_readarg(1, ctx, &start_data.operation_id);
51
52     start_data.start = bpf_ktime_get_ns();
53     start_hash.update(&start_data.operation_id, &start_data);
54     return 0;
55 }