# preadv ____________________________________________________
#
# SYSCALL_DEFINE5(preadv, unsigned long, fd,
#		  const struct iovec __user *, vec,
#		  unsigned long, vlen, unsigned long, pos_l,
#		  unsigned long, pos_h)
# COMPAT_SYSCALL_DEFINE5(preadv, unsigned long, fd,
#		const struct compat_iovec __user *,vec,
#		unsigned long, vlen, u32, pos_low, u32, pos_high)
#

@define _SYSCALL_PREADV_NAME
%(
	name = "preadv"
%)

@define _SYSCALL_PREADV_ARGSTR
%(
	argstr = sprintf("%d, %p, %d, 0x%x", fd, vector_uaddr, count, offset)
%)

@define _SYSCALL_PREADV_REGARGS
%(
	fd = int_arg(1)
	vector_uaddr = pointer_arg(2)
	count = int_arg(3)
	offset = @__pos_from_hilo(ulong_arg(5), ulong_arg(4))
%)

@define _SYSCALL_PREADV_REGARGS_STORE
%(
        if (@probewrite(fd))
          set_int_arg(1, fd)

        if (@probewrite(vector_uaddr))
          set_pointer_arg(2, vector_uaddr)

        if (@probewrite(count))
          set_int_arg(3, count)

        if (@probewrite(offset)) {
          set_ulong_arg(4, __ulong(offset & 0xffffffff))
          set_ulong_arg(5, __ulong(offset >> 32))
        }
%)

@define _SYSCALL_COMPAT_PREADV_REGARGS
%(
	fd = int_arg(1)
	vector_uaddr = pointer_arg(2)
	count = int_arg(3)
	offset = (u32_arg(5) << 32) + u32_arg(4)
%)

@define _SYSCALL_COMPAT_PREADV_REGARGS_STORE
%(
        if (@probewrite(fd))
          set_int_arg(1, fd)

        if (@probewrite(vector_uaddr))
          set_pointer_arg(2, vector_uaddr)

        if (@probewrite(count))
          set_int_arg(3, count)

        if (@probewrite(offset)) {
          set_u32_arg(4, offset & 0xffffffff)
          set_u32_arg(5, offset >> 32)
        }
%)

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

# dw_preadv _____________________________________________________

probe dw_syscall.preadv = __syscall.preadv ?, __syscall.compat_preadv ?
{
	@_SYSCALL_PREADV_NAME
}
probe __syscall.preadv = kernel.function("sys_preadv")
{
	fd = __int32($fd)
	vector_uaddr = $vec
	count = __int32($vlen)
	offset = @__pos_from_hilo($pos_h, $pos_l)
	@_SYSCALL_PREADV_ARGSTR
}
probe __syscall.compat_preadv = kernel.function("compat_sys_preadv")
{
	fd = __int32($fd)
	vector_uaddr = $vec
	count = __int32($vlen)
	offset = ($pos_high << 32) + __uint32($pos_low)
	@_SYSCALL_PREADV_ARGSTR
}
probe dw_syscall.preadv.return = kernel.function("sys_preadv").return ?,
			      kernel.function("compat_sys_preadv").return ?
{
	@_SYSCALL_PREADV_NAME
	@SYSC_RETVALSTR($return)
}

# nd_preadv _____________________________________________________

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

probe nd1_syscall.preadv = __nd1_syscall.preadv ?, __nd1_syscall.compat_preadv ?
{
	@_SYSCALL_PREADV_NAME
}
probe __nd1_syscall.preadv = kprobe.function("sys_preadv")
{
	asmlinkage()
	@_SYSCALL_PREADV_REGARGS
	@_SYSCALL_PREADV_ARGSTR
}
probe __nd1_syscall.compat_preadv = kprobe.function("compat_sys_preadv")
{
	@_SYSCALL_COMPAT_PREADV_REGARGS
	@_SYSCALL_PREADV_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.preadv = __nd2_syscall.preadv ?, __nd2_syscall.compat_preadv ?
{
	@_SYSCALL_PREADV_NAME
}
probe __nd2_syscall.preadv = kprobe.function(@arch_syscall_prefix "sys_preadv") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_PREADV_REGARGS
	@_SYSCALL_PREADV_ARGSTR
},
{
	%( @_IS_SREG_KERNEL %? @_SYSCALL_PREADV_REGARGS_STORE %)
}

probe __nd2_syscall.compat_preadv = kprobe.function(@arch_syscall_prefix "compat_sys_preadv") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_COMPAT_PREADV_REGARGS
	@_SYSCALL_PREADV_ARGSTR
},
{
	%( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_PREADV_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.preadv = __tp_syscall.preadv ?, __tp_syscall.compat_preadv ?
{
	@_SYSCALL_PREADV_NAME
}
probe __tp_syscall.preadv = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_preadv"))
	@_SYSCALL_PREADV_REGARGS
	@_SYSCALL_PREADV_ARGSTR
},
{
	%( @_IS_SREG_KERNEL %? @_SYSCALL_PREADV_REGARGS_STORE %)
}
probe __tp_syscall.compat_preadv = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_preadv"))
	@_SYSCALL_COMPAT_PREADV_REGARGS
	@_SYSCALL_PREADV_ARGSTR
},
{
	%( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_PREADV_REGARGS_STORE %)
}

probe nd_syscall.preadv.return = nd1_syscall.preadv.return!, nd2_syscall.preadv.return!, tp_syscall.preadv.return
  { }
  
probe nd1_syscall.preadv.return = kprobe.function("sys_preadv").return ?,
				  kprobe.function("compat_sys_preadv").return ?
{
	@_SYSCALL_PREADV_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.preadv.return = kprobe.function(@arch_syscall_prefix "sys_preadv").return ?,
      				  kprobe.function(@arch_syscall_prefix "compat_sys_preadv").return ?
{
	@_SYSCALL_PREADV_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.preadv.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_preadv"), @const("__NR_compat_preadv"))
	@_SYSCALL_PREADV_NAME
	@SYSC_RETVALSTR($ret)
}
