# ppoll _______________________________________________________
#
# long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
#	struct timespec __user *tsp, const sigset_t __user *sigmask,
#	size_t sigsetsize)
#

@define _SYSCALL_PPOLL_NAME
%(
	name = "ppoll"
%)

@define _SYSCALL_PPOLL_ARGSTR
%(
	argstr = sprintf("%p, %u, %s, %p, %d",
	                 fds_uaddr, nfds, tsp_str, sigmask, sigsetsize)
%)

@define _SYSCALL_PPOLL_REGARGS
%(
	fds_uaddr = pointer_arg(1) 
	nfds = uint_arg(2)
	tsp = pointer_arg(3)
	tsp_str = _struct_timespec_u(tsp, 1)
	sigmask = pointer_arg(4)
	sigsetsize = ulong_arg(5)
%)

@define _SYSCALL_PPOLL_REGARGS_STORE
%(
        if (@probewrite(fds_uaddr))
          set_pointer_arg(1, fds_uaddr)

	if (@probewrite(nfds))
	  set_uint_arg(2, nfds)

	if (@probewrite(tsp))
	  set_pointer_arg(3, tsp)

	if (@probewrite(sigmask))
	  set_pointer_arg(4, sigmask)

	if (@probewrite(sigsetsize))
	  set_ulong_arg(5, sigsetsize)
%)

@define _SYSCALL_COMPAT_PPOLL_REGARGS
%(
	fds_uaddr = pointer_arg(1) 
	nfds = uint_arg(2)
	tsp = pointer_arg(3)
	tsp_str = _struct_compat_timespec_u(tsp, 1)
	sigmask = pointer_arg(4)
	sigsetsize = u32_arg(5)
%)

@define _SYSCALL_COMPAT_PPOLL_REGARGS_STORE
%(
        if (@probewrite(fds_uaddr))
          set_pointer_arg(1, fds_uaddr)

	if (@probewrite(nfds))
	  set_uint_arg(2, nfds)

	if (@probewrite(tsp))
	  set_pointer_arg(3, tsp)

	if (@probewrite(sigmask))
	  set_pointer_arg(4, sigmask)

	if (@probewrite(sigsetsize))
	  set_u32_arg(5, sigsetsize)
%)

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

# dw_ppoll _____________________________________________________

probe dw_syscall.ppoll = kernel.function("sys_ppoll").call ?
{
	@_SYSCALL_PPOLL_NAME
	fds_uaddr = $ufds
	nfds = __uint32($nfds)
	tsp = $tsp
	tsp_str = _struct_timespec_u(tsp, 1)
	sigmask = $sigmask
	sigsetsize = $sigsetsize
	@_SYSCALL_PPOLL_ARGSTR
}
probe dw_syscall.ppoll.return = kernel.function("sys_ppoll").return ?
{
	@_SYSCALL_PPOLL_NAME
	@SYSC_RETVALSTR($return)
}

# nd_ppoll _____________________________________________________

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

probe nd1_syscall.ppoll = kprobe.function("sys_ppoll") ?
{
	@_SYSCALL_PPOLL_NAME
	asmlinkage()
	@_SYSCALL_PPOLL_REGARGS
	@_SYSCALL_PPOLL_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.ppoll = kprobe.function(@arch_syscall_prefix "sys_ppoll") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_PPOLL_NAME
	@_SYSCALL_PPOLL_REGARGS
	@_SYSCALL_PPOLL_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PPOLL_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.ppoll = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_ppoll"))
	@_SYSCALL_PPOLL_NAME
	@_SYSCALL_PPOLL_REGARGS
	@_SYSCALL_PPOLL_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PPOLL_REGARGS_STORE %)
}

probe nd_syscall.ppoll.return = nd1_syscall.ppoll.return!, nd2_syscall.ppoll.return!, tp_syscall.ppoll.return
  { }
  
probe nd1_syscall.ppoll.return = kprobe.function("sys_ppoll").return ?
{
	@_SYSCALL_PPOLL_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.ppoll.return = kprobe.function(@arch_syscall_prefix "sys_ppoll").return ?
{
	@_SYSCALL_PPOLL_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.ppoll.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_ppoll"))
	@_SYSCALL_PPOLL_NAME
	@SYSC_RETVALSTR($ret)
}

# compat_ppoll ________________________________________________
#
# long compat_sys_ppoll(struct pollfd __user *ufds,
#	unsigned int nfds, struct compat_timespec __user *tsp,
#	const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
#

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

# dw_compat_ppoll _____________________________________________________

probe dw_syscall.compat_ppoll = kernel.function("compat_sys_ppoll").call ?
{
	@_SYSCALL_PPOLL_NAME
	fds_uaddr = @__pointer($ufds)
	nfds = __uint32($nfds)
	tsp = @__pointer($tsp)
	tsp_str = _struct_compat_timespec_u(tsp, 1)
	sigmask = @__pointer($sigmask)
	sigsetsize = $sigsetsize
	@_SYSCALL_PPOLL_ARGSTR
}
probe dw_syscall.compat_ppoll.return = kernel.function("compat_sys_ppoll").return ?
{
	@_SYSCALL_PPOLL_NAME
	@SYSC_RETVALSTR($return)
}

# nd_compat_ppoll _____________________________________________________

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

probe nd1_syscall.compat_ppoll = kprobe.function("compat_sys_ppoll") ?
{
	@_SYSCALL_PPOLL_NAME
	asmlinkage()
	@_SYSCALL_COMPAT_PPOLL_REGARGS
	@_SYSCALL_PPOLL_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.compat_ppoll = kprobe.function(@arch_syscall_prefix "compat_sys_ppoll") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_PPOLL_NAME
	@_SYSCALL_COMPAT_PPOLL_REGARGS
	@_SYSCALL_PPOLL_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_PPOLL_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_ppoll = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_ppoll"))
	@_SYSCALL_PPOLL_NAME
	@_SYSCALL_COMPAT_PPOLL_REGARGS
	@_SYSCALL_PPOLL_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_PPOLL_REGARGS_STORE %)
}

probe nd_syscall.compat_ppoll.return = nd1_syscall.compat_ppoll.return!, nd2_syscall.compat_ppoll.return!, tp_syscall.compat_ppoll.return
  { }
  
probe nd1_syscall.compat_ppoll.return = kprobe.function("compat_sys_ppoll").return ?
{
	@_SYSCALL_PPOLL_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.compat_ppoll.return = kprobe.function(@arch_syscall_prefix "compat_sys_ppoll").return ?
{
	@_SYSCALL_PPOLL_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_ppoll.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_ppoll"))
	@_SYSCALL_PPOLL_NAME
	@SYSC_RETVALSTR($ret)
}
