xref: /aosp_15_r20/external/bcc/examples/usdt_sample/scripts/bpf_text_shared.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
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  */
filter(char const * inputString)9 static inline bool filter(char const* inputString)
10 {
11     static const char* null_ptr = 0x0;
12     static const char null_terminator = '\0';
13 
14     static const char filter_string[] = "FILTER_STRING"; ///< The filter string is replaced by python code.
15     if (null_ptr == inputString) {
16         return false;
17     }
18 
19     // Compare until (not including) the null-terminator for filter_string
20     for (int i = 0; i < sizeof(filter_string) - 1; ++i) {
21         char c1 = *inputString++;
22         if (null_terminator == c1) {
23             return false;  // If the null-terminator for inputString was reached, it can not be equal to filter_string.
24         }
25 
26         char c2 = filter_string[i];
27         if (c1 != c2) {
28             return false;
29         }
30     }
31     return true;
32 }
33 
34 /**
35  * @brief Contains the operation start data to trace.
36  */
37 struct start_data_t
38 {
39     u64 operation_id; ///< The id of the operation.
40     char input[64];   ///< The input string of the request.
41     u64 start;        ///< Timestamp of the start operation (start timestamp).
42 };
43 
44 /**
45  * @brief Contains the operation start data.
46  * key: the operation id.
47  * value: The operation start latency data.
48  */
49 BPF_HASH(start_hash, u64, struct start_data_t);
50 
51 /**
52  * @brief Reads the operation request arguments and stores the start data in the hash.
53  * @param ctx The BPF context.
54  */
trace_operation_start(struct pt_regs * ctx)55 int trace_operation_start(struct pt_regs* ctx)
56 {
57     struct start_data_t start_data = {};
58     bpf_usdt_readarg_p(2, ctx, &start_data.input, sizeof(start_data.input));
59 
60     FILTER_STATEMENT ///< Replaced by python code.
61 
62     bpf_usdt_readarg(1, ctx, &start_data.operation_id);
63 
64     start_data.start = bpf_ktime_get_ns();
65     start_hash.update(&start_data.operation_id, &start_data);
66     return 0;
67 }
68