# process_vm_readv _____________________________________________
# process_vm_readv was added in linux 3.2.
#
# SYSCALL_DEFINE6(process_vm_readv, pid_t, pid,
#		const struct iovec __user *, lvec,
#		unsigned long, liovcnt, const struct iovec __user *, rvec,
#		unsigned long, riovcnt,	unsigned long, flags)
# COMPAT_SYSCALL_DEFINE6(process_vm_readv, compat_pid_t, pid,
#		       const struct compat_iovec __user *, lvec,
#		       compat_ulong_t, liovcnt,
#		       const struct compat_iovec __user *, rvec,
#		       compat_ulong_t, riovcnt,
#		       compat_ulong_t, flags)

@define _SYSCALL_PROCESS_VM_READV_NAME
%(
	name = "process_vm_readv"
%)

@define _SYSCALL_PROCESS_VM_READV_ARGSTR
%(
	argstr = sprintf("%d, %p, %u, %p, %u, %u", pid, local_iov_uaddr,
	                 liovcnt, remote_iov_uaddr, riovcnt, flags)
%)

@define _SYSCALL_PROCESS_VM_READV_REGARGS
%(
	pid = int_arg(1)
	local_iov_uaddr = pointer_arg(2)
	liovcnt = ulong_arg(3)
	remote_iov_uaddr = pointer_arg(4)
	riovcnt = ulong_arg(5)
	flags = ulong_arg(6)
%)

@define _SYSCALL_PROCESS_VM_READV_REGARGS_STORE
%(
        if (@probewrite(pid))
          set_int_arg(1, pid)

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

	if (@probewrite(liovcnt))
	  set_ulong_arg(3, liovcnt)

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

	if (@probewrite(riovcnt))
	  set_ulong_arg(5, riovcnt)

	if (@probewrite(flags))
	  set_ulong_arg(6, flags)
%)

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

# dw_process_vm_readv _____________________________________________________

probe dw_syscall.process_vm_readv = kernel.function("sys_process_vm_readv").call ?,
	kernel.function("compat_sys_process_vm_readv").call ?
{
	@_SYSCALL_PROCESS_VM_READV_NAME
	pid = __int32($pid)
	local_iov_uaddr = @__pointer($lvec)
	liovcnt = @__compat_ulong($liovcnt)
	remote_iov_uaddr = @__pointer($rvec)
	riovcnt = @__compat_ulong($riovcnt)
	flags = @__compat_ulong($flags)
	@_SYSCALL_PROCESS_VM_READV_ARGSTR
}
probe dw_syscall.process_vm_readv.return =
	kernel.function("sys_process_vm_readv").return ?,
	kernel.function("compat_sys_process_vm_readv").return ?
{
	@_SYSCALL_PROCESS_VM_READV_NAME
	@SYSC_RETVALSTR($return)
}

# nd_process_vm_readv _____________________________________________________

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

probe nd1_syscall.process_vm_readv = kprobe.function("sys_process_vm_readv") ?,
	kprobe.function("compat_sys_process_vm_readv") ?
{
	@_SYSCALL_PROCESS_VM_READV_NAME
	asmlinkage()
	@_SYSCALL_PROCESS_VM_READV_REGARGS
	@_SYSCALL_PROCESS_VM_READV_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.process_vm_readv =
	kprobe.function(@arch_syscall_prefix "sys_process_vm_readv") ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_process_vm_readv") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_PROCESS_VM_READV_NAME
	@_SYSCALL_PROCESS_VM_READV_REGARGS
	@_SYSCALL_PROCESS_VM_READV_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PROCESS_VM_READV_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.process_vm_readv = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_process_vm_readv"), @const("__NR_compat_process_vm_readv"))
	@_SYSCALL_PROCESS_VM_READV_NAME
	@_SYSCALL_PROCESS_VM_READV_REGARGS
	@_SYSCALL_PROCESS_VM_READV_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PROCESS_VM_READV_REGARGS_STORE %)
}

probe nd_syscall.process_vm_readv.return = nd1_syscall.process_vm_readv.return!, nd2_syscall.process_vm_readv.return!, tp_syscall.process_vm_readv.return
  { }
  
probe nd1_syscall.process_vm_readv.return =
	kprobe.function("sys_process_vm_readv").return ?,
	kprobe.function("compat_sys_process_vm_readv").return ?
{
	@_SYSCALL_PROCESS_VM_READV_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.process_vm_readv.return =
	kprobe.function(@arch_syscall_prefix "sys_process_vm_readv").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_process_vm_readv").return ?
{
	@_SYSCALL_PROCESS_VM_READV_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.process_vm_readv.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_process_vm_readv"), @const("__NR_compat_process_vm_readv"))
	@_SYSCALL_PROCESS_VM_READV_NAME
	@SYSC_RETVALSTR($ret)
}
