1*4882a593Smuzhiyun // SPDX-License-Identifier: LGPL-2.1
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2017 National Instruments Corp.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Author: Julia Cartwright <julia@ni.com>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun #include <stdio.h>
9*4882a593Smuzhiyun #include <stdlib.h>
10*4882a593Smuzhiyun #include <string.h>
11*4882a593Smuzhiyun #include <linux/futex.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include "event-parse.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define ARRAY_SIZE(_a) (sizeof(_a) / sizeof((_a)[0]))
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun struct futex_args {
18*4882a593Smuzhiyun unsigned long long uaddr;
19*4882a593Smuzhiyun unsigned long long op;
20*4882a593Smuzhiyun unsigned long long val;
21*4882a593Smuzhiyun unsigned long long utime; /* or val2 */
22*4882a593Smuzhiyun unsigned long long uaddr2;
23*4882a593Smuzhiyun unsigned long long val3;
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun struct futex_op {
27*4882a593Smuzhiyun const char *name;
28*4882a593Smuzhiyun const char *fmt_val;
29*4882a593Smuzhiyun const char *fmt_utime;
30*4882a593Smuzhiyun const char *fmt_uaddr2;
31*4882a593Smuzhiyun const char *fmt_val3;
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static const struct futex_op futex_op_tbl[] = {
35*4882a593Smuzhiyun { "FUTEX_WAIT", " val=0x%08llx", " utime=0x%08llx", NULL, NULL },
36*4882a593Smuzhiyun { "FUTEX_WAKE", " val=%llu", NULL, NULL, NULL },
37*4882a593Smuzhiyun { "FUTEX_FD", " val=%llu", NULL, NULL, NULL },
38*4882a593Smuzhiyun { "FUTEX_REQUEUE", " val=%llu", " val2=%llu", " uaddr2=0x%08llx", NULL },
39*4882a593Smuzhiyun { "FUTEX_CMP_REQUEUE", " val=%llu", " val2=%llu", " uaddr2=0x%08llx", " val3=0x%08llx" },
40*4882a593Smuzhiyun { "FUTEX_WAKE_OP", " val=%llu", " val2=%llu", " uaddr2=0x%08llx", " val3=0x%08llx" },
41*4882a593Smuzhiyun { "FUTEX_LOCK_PI", NULL, " utime=0x%08llx", NULL, NULL },
42*4882a593Smuzhiyun { "FUTEX_UNLOCK_PI", NULL, NULL, NULL, NULL },
43*4882a593Smuzhiyun { "FUTEX_TRYLOCK_PI", NULL, NULL, NULL, NULL },
44*4882a593Smuzhiyun { "FUTEX_WAIT_BITSET", " val=0x%08llx", " utime=0x%08llx", NULL, " val3=0x%08llx" },
45*4882a593Smuzhiyun { "FUTEX_WAKE_BITSET", " val=%llu", NULL, NULL, " val3=0x%08llx" },
46*4882a593Smuzhiyun { "FUTEX_WAIT_REQUEUE_PI", " val=0x%08llx", " utime=0x%08llx", " uaddr2=0x%08llx", " val3=0x%08llx" },
47*4882a593Smuzhiyun { "FUTEX_CMP_REQUEUE_PI", " val=%llu", " val2=%llu", " uaddr2=0x%08llx", " val3=0x%08llx" },
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun
futex_print(struct trace_seq * s,const struct futex_args * args,const struct futex_op * fop)51*4882a593Smuzhiyun static void futex_print(struct trace_seq *s, const struct futex_args *args,
52*4882a593Smuzhiyun const struct futex_op *fop)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun trace_seq_printf(s, " uaddr=0x%08llx", args->uaddr);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun if (fop->fmt_val)
57*4882a593Smuzhiyun trace_seq_printf(s, fop->fmt_val, args->val);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (fop->fmt_utime)
60*4882a593Smuzhiyun trace_seq_printf(s,fop->fmt_utime, args->utime);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun if (fop->fmt_uaddr2)
63*4882a593Smuzhiyun trace_seq_printf(s, fop->fmt_uaddr2, args->uaddr2);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun if (fop->fmt_val3)
66*4882a593Smuzhiyun trace_seq_printf(s, fop->fmt_val3, args->val3);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
futex_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * context)69*4882a593Smuzhiyun static int futex_handler(struct trace_seq *s, struct tep_record *record,
70*4882a593Smuzhiyun struct tep_event *event, void *context)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun const struct futex_op *fop;
73*4882a593Smuzhiyun struct futex_args args;
74*4882a593Smuzhiyun unsigned long long cmd;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun if (tep_get_field_val(s, event, "uaddr", record, &args.uaddr, 1))
77*4882a593Smuzhiyun return 1;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (tep_get_field_val(s, event, "op", record, &args.op, 1))
80*4882a593Smuzhiyun return 1;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun if (tep_get_field_val(s, event, "val", record, &args.val, 1))
83*4882a593Smuzhiyun return 1;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (tep_get_field_val(s, event, "utime", record, &args.utime, 1))
86*4882a593Smuzhiyun return 1;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (tep_get_field_val(s, event, "uaddr2", record, &args.uaddr2, 1))
89*4882a593Smuzhiyun return 1;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if (tep_get_field_val(s, event, "val3", record, &args.val3, 1))
92*4882a593Smuzhiyun return 1;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun cmd = args.op & FUTEX_CMD_MASK;
95*4882a593Smuzhiyun if (cmd >= ARRAY_SIZE(futex_op_tbl))
96*4882a593Smuzhiyun return 1;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun fop = &futex_op_tbl[cmd];
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun trace_seq_printf(s, "op=%s", fop->name);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (args.op & FUTEX_PRIVATE_FLAG)
103*4882a593Smuzhiyun trace_seq_puts(s, "|FUTEX_PRIVATE_FLAG");
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun if (args.op & FUTEX_CLOCK_REALTIME)
106*4882a593Smuzhiyun trace_seq_puts(s, "|FUTEX_CLOCK_REALTIME");
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun futex_print(s, &args, fop);
109*4882a593Smuzhiyun return 0;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
TEP_PLUGIN_LOADER(struct tep_handle * tep)112*4882a593Smuzhiyun int TEP_PLUGIN_LOADER(struct tep_handle *tep)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun tep_register_event_handler(tep, -1, "syscalls", "sys_enter_futex",
115*4882a593Smuzhiyun futex_handler, NULL);
116*4882a593Smuzhiyun return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
TEP_PLUGIN_UNLOADER(struct tep_handle * tep)119*4882a593Smuzhiyun void TEP_PLUGIN_UNLOADER(struct tep_handle *tep)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun tep_unregister_event_handler(tep, -1, "syscalls", "sys_enter_futex",
122*4882a593Smuzhiyun futex_handler, NULL);
123*4882a593Smuzhiyun }
124