# epoll_wait _________________________________________________
#
# long sys_epoll_wait(int epfd, struct epoll_event __user *events,
#               int maxevents, int timeout)
# long compat_sys_epoll_wait(int epfd,
#		struct compat_epoll_event __user *events,
#		int maxevents, int timeout)
#

@define _SYSCALL_EPOLL_WAIT_NAME
%(
	name = "epoll_wait"
%)

@define _SYSCALL_EPOLL_WAIT_ARGSTR
%(
	argstr = sprintf("%d, %p, %d, %d", epfd, events_uaddr, maxevents, timeout)
%)

@define _SYSCALL_EPOLL_WAIT_REGARGS
%(
	epfd = int_arg(1)
	events_uaddr = pointer_arg(2)
	maxevents = int_arg(3)
	timeout = int_arg(4)
%)

probe syscall.epoll_wait = dw_syscall.epoll_wait !, nd_syscall.epoll_wait ? {}
probe syscall.epoll_wait.return = dw_syscall.epoll_wait.return !, nd_syscall.epoll_wait.return ? {}

# dw_epoll_wait _____________________________________________________

probe dw_syscall.epoll_wait = kernel.function("compat_sys_epoll_wait").call ?,
			      __syscall.epoll_wait ?
{
%( arch != "s390" || kernel_v < "3.15" %?
	@__syscall_compat_gate(@const("__NR_epoll_wait"),
			       @const("__NR_compat_epoll_wait"))
%)
	@_SYSCALL_EPOLL_WAIT_NAME
	epfd = __int32($epfd)
	events_uaddr = $events
	maxevents = __int32($maxevents)
	timeout = __int32($timeout)
	@_SYSCALL_EPOLL_WAIT_ARGSTR
}
probe __syscall.epoll_wait = kernel.function("sys_epoll_wait").call ?
{
%( arch == "s390" && kernel_v >= "3.15" %?
	# s390 switched from assembly compat wrappers to C-based
	# wrappers in kernel 3.15.
	@__syscall_gate(@const("__NR_epoll_wait"))
%)
}
probe dw_syscall.epoll_wait.return =
	kernel.function("compat_sys_epoll_wait").return ?,
	__syscall.epoll_wait.return ?
{
%( arch != "s390" || kernel_v < "3.15" %?
	@__syscall_compat_gate(@const("__NR_epoll_wait"),
			       @const("__NR_compat_epoll_wait"))
%)
	@_SYSCALL_EPOLL_WAIT_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.epoll_wait.return = kernel.function("sys_epoll_wait").return ?
{
%( arch == "s390" && kernel_v >= "3.15" %?
	@__syscall_gate(@const("__NR_epoll_wait"))
%)
}

# nd_epoll_wait _____________________________________________________

probe nd_syscall.epoll_wait = nd1_syscall.epoll_wait!, nd2_syscall.epoll_wait!, tp_syscall.epoll_wait
  { }

probe nd1_syscall.epoll_wait = kprobe.function("compat_sys_epoll_wait") ?,
                              __nd1_syscall.epoll_wait ?
{
%( arch != "s390" || kernel_v < "3.15" %?
	@__syscall_compat_gate(@const("__NR_epoll_wait"),
			       @const("__NR_compat_epoll_wait"))
%)
	@_SYSCALL_EPOLL_WAIT_NAME
	asmlinkage()
	@_SYSCALL_EPOLL_WAIT_REGARGS
	@_SYSCALL_EPOLL_WAIT_ARGSTR
}
probe __nd1_syscall.epoll_wait = kprobe.function("sys_epoll_wait") ?
{
%( arch == "s390" && kernel_v >= "3.15" %?
	@__syscall_gate(@const("__NR_epoll_wait"))
%)
}

/* kernel 4.17+ */
probe nd2_syscall.epoll_wait = kprobe.function(@arch_syscall_prefix "sys_epoll_wait") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_EPOLL_WAIT_NAME
	@_SYSCALL_EPOLL_WAIT_REGARGS
	@_SYSCALL_EPOLL_WAIT_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.epoll_wait = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_epoll_wait"), @const("__NR_compat_epoll_wait"))
	@_SYSCALL_EPOLL_WAIT_NAME
	@_SYSCALL_EPOLL_WAIT_REGARGS
	@_SYSCALL_EPOLL_WAIT_ARGSTR
}

probe nd_syscall.epoll_wait.return = nd1_syscall.epoll_wait.return!, nd2_syscall.epoll_wait.return!, tp_syscall.epoll_wait.return
  { }
  
probe nd1_syscall.epoll_wait.return =
	kprobe.function("compat_sys_epoll_wait").return ?,
	__nd1_syscall.epoll_wait.return ?
{
%( arch != "s390" || kernel_v < "3.15" %?
	@__syscall_compat_gate(@const("__NR_epoll_wait"),
			       @const("__NR_compat_epoll_wait"))
%)
	@_SYSCALL_EPOLL_WAIT_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.epoll_wait.return =
	kprobe.function("sys_epoll_wait").return ?
{
%( arch == "s390" && kernel_v >= "3.15" %?
	@__syscall_gate(@const("__NR_epoll_wait"))
%)
}

/* kernel 4.17+ */
probe nd2_syscall.epoll_wait.return = kprobe.function(@arch_syscall_prefix "sys_epoll_wait").return ?
{
	@_SYSCALL_EPOLL_WAIT_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.epoll_wait.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_epoll_wait"), @const("__NR_compat_epoll_wait"))
	@_SYSCALL_EPOLL_WAIT_NAME
	@SYSC_RETVALSTR($ret)
}
