# readv ______________________________________________________
#
# ssize_t sys_readv(unsigned long fd,
#		const struct iovec __user *vec,
#		unsigned long vlen)
# ssize_t compat_sys_readv(unsigned long fd,
#		const struct compat_iovec __user *vec,
#		unsigned long vlen)
#

@define _SYSCALL_READV_NAME
%(
	name = "readv"
%)

@define _SYSCALL_READV_ARGSTR
%(
	argstr = sprintf("%d, %p, %d", fd, vector_uaddr, count)
%)

@define _SYSCALL_READV_REGARGS
%(
	vector_uaddr = pointer_arg(2)
	count = int_arg(3)
	fd = int_arg(1)
%)

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

# dw_readv _____________________________________________________

probe dw_syscall.readv = kernel.function("compat_sys_readv").call ?,
                      kernel.function("sys_readv").call
{
	@_SYSCALL_READV_NAME
	vector_uaddr = $vec
	count = __int32($vlen)
	# Although the kernel gets an unsigned long fd, on the
	# user-side it is a signed int.  Fix this.
	fd = __int32($fd)
	@_SYSCALL_READV_ARGSTR
}
probe dw_syscall.readv.return = kernel.function("compat_sys_readv").return ?,
                             kernel.function("sys_readv").return
{
	@_SYSCALL_READV_NAME
	@SYSC_RETVALSTR($return)
}

# nd_readv _____________________________________________________

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

probe nd1_syscall.readv = kprobe.function("compat_sys_readv") ?,
                          kprobe.function("sys_readv") ?
{
	@_SYSCALL_READV_NAME
	asmlinkage()
	@_SYSCALL_READV_REGARGS
	@_SYSCALL_READV_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.readv = kprobe.function(@arch_syscall_prefix "sys_readv") ?,
                          kprobe.function(@arch_syscall_prefix "compat_sys_readv") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_READV_NAME
	@_SYSCALL_READV_REGARGS
	@_SYSCALL_READV_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.readv = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_readv"), @const("__NR_compat_readv"))
	@_SYSCALL_READV_NAME
	@_SYSCALL_READV_REGARGS
	@_SYSCALL_READV_ARGSTR
}

probe nd_syscall.readv.return = nd1_syscall.readv.return!, nd2_syscall.readv.return!, tp_syscall.readv.return
  { }
  
probe nd1_syscall.readv.return = kprobe.function("compat_sys_readv").return ?,
                                 kprobe.function("sys_readv").return ?
{
	@_SYSCALL_READV_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.readv.return = kprobe.function(@arch_syscall_prefix "sys_readv").return ?,
                                 kprobe.function(@arch_syscall_prefix "compat_sys_readv").return ?
{
	@_SYSCALL_READV_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.readv.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_readv"), @const("__NR_compat_readv"))
	@_SYSCALL_READV_NAME
	@SYSC_RETVALSTR($ret)
}
