# pwritev2 __________________________________________________
#
# SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
#		unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
#		int, flags)
# COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
#		const struct compat_iovec __user *,vec,
#		compat_ulong_t, vlen, u32, pos_low, u32, pos_high, int, flags)

@define _SYSCALL_PWRITEV2_NAME
%(
	name = "pwritev2"
%)

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

@define _SYSCALL_PWRITEV2_REGARGS
%(
	fd = int_arg(1)
	vector_uaddr = pointer_arg(2)
	count = int_arg(3)
	offset = @__pos_from_hilo(ulong_arg(5), ulong_arg(4))
	flags = int_arg(6)
	flags_str = _preadv2_flags_str(flags)
%)

@define _SYSCALL_COMPAT_PWRITEV2_REGARGS
%(
	fd = int_arg(1)
	vector_uaddr = pointer_arg(2)
	count = int_arg(3)
	offset = (u32_arg(5) << 32) + u32_arg(4)
	flags = int_arg(6)
	flags_str = _preadv2_flags_str(flags)
%)

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

# dw_pwritev2 _____________________________________________________

probe dw_syscall.pwritev2 = __syscall.pwritev2 ?, __syscall.compat_pwritev2 ?
{
	@_SYSCALL_PWRITEV2_NAME
}
probe __syscall.pwritev2 = kernel.function("sys_pwritev2").call ?
{
	fd = __int32($fd)
	vector_uaddr = $vec
	count = __int32($vlen)
	offset = @__pos_from_hilo($pos_h, $pos_l)
	flags = $flags
	flags_str = _preadv2_flags_str(flags)
	@_SYSCALL_PWRITEV2_ARGSTR
}
probe __syscall.compat_pwritev2 = kernel.function("compat_sys_pwritev2").call ?
{
	fd = __int32($fd)
	vector_uaddr = $vec
	count = __int32($vlen)
	offset = ($pos_high << 32) + $pos_low
	flags = $flags
	flags_str = _preadv2_flags_str(flags)
	@_SYSCALL_PWRITEV2_ARGSTR
}
probe dw_syscall.pwritev2.return = kernel.function("sys_pwritev2").return ?,
	kernel.function("compat_sys_pwritev2").return ?
{
	@_SYSCALL_PWRITEV2_NAME
	@SYSC_RETVALSTR($return)
}

# nd_pwritev2 _____________________________________________________

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

probe nd1_syscall.pwritev2 = __nd1_syscall.pwritev2 ?,
	__nd1_syscall.compat_pwritev2 ?
{
	@_SYSCALL_PWRITEV2_NAME
}
probe __nd1_syscall.pwritev2 = kprobe.function("sys_pwritev2").call
{
	asmlinkage()
	@_SYSCALL_PWRITEV2_REGARGS
	@_SYSCALL_PWRITEV2_ARGSTR
}
probe __nd1_syscall.compat_pwritev2 =
	kprobe.function("compat_sys_pwritev2").call
{
	asmlinkage()
	@_SYSCALL_COMPAT_PWRITEV2_REGARGS
	@_SYSCALL_PWRITEV2_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.pwritev2 = __nd2_syscall.pwritev2 ?,
	__nd2_syscall.compat_pwritev2 ?
{
	@_SYSCALL_PWRITEV2_NAME
}
probe __nd2_syscall.pwritev2 = kprobe.function(@arch_syscall_prefix "sys_pwritev2").call
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_PWRITEV2_REGARGS
	@_SYSCALL_PWRITEV2_ARGSTR
}
probe __nd2_syscall.compat_pwritev2 =
	kprobe.function(@arch_syscall_prefix "compat_sys_pwritev2").call
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_COMPAT_PWRITEV2_REGARGS
	@_SYSCALL_PWRITEV2_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.pwritev2 = __tp_syscall.pwritev2 ?,
	__tp_syscall.compat_pwritev2 ?
{
	@_SYSCALL_PWRITEV2_NAME
}
probe __tp_syscall.pwritev2 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_pwritev2"))
	@_SYSCALL_PWRITEV2_REGARGS
	@_SYSCALL_PWRITEV2_ARGSTR
}
probe __tp_syscall.compat_pwritev2 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_pwritev2"))
	@_SYSCALL_COMPAT_PWRITEV2_REGARGS
	@_SYSCALL_PWRITEV2_ARGSTR
}

probe nd_syscall.pwritev2.return = nd1_syscall.pwritev2.return!, nd2_syscall.pwritev2.return!, tp_syscall.pwritev2.return
  { }
  
probe nd1_syscall.pwritev2.return = kprobe.function("sys_pwritev2").return ?,
	kprobe.function("compat_sys_pwritev2").return ?
{
	@_SYSCALL_PWRITEV2_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.pwritev2.return = kprobe.function(@arch_syscall_prefix "sys_pwritev2").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_pwritev2").return ?
{
	@_SYSCALL_PWRITEV2_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.pwritev2.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_pwritev2"), @const("__NR_compat_pwritev2"))
	@_SYSCALL_PWRITEV2_NAME
	@SYSC_RETVALSTR($ret)
}
