# pread64 ____________________________________________________
#
# ssize_t sys_pread64(unsigned int fd,
#		 char __user *buf,
#		 size_t count,
#		 loff_t pos)
# COMPAT_SYSCALL_DEFINE5(s390_pread64, unsigned int, fd, char __user *, ubuf,
#		         compat_size_t, count, u32, high, u32, low)
#

@define _SYSCALL_PREAD_NAME
%(
	name = "pread"
%)

@define _SYSCALL_PREAD_ARGSTR
%(
	argstr = sprintf("%d, %p, %u, %d", fd, buf_uaddr, count, offset)
%)

@define _SYSCALL_PREAD_REGARGS
%(
	fd = int_arg(1)
	buf_uaddr = pointer_arg(2)
	count = ulong_arg(3)
%)

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

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

	if (@probewrite(count))
	  set_ulong_arg(3, count)
%)

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

# dw_pread _____________________________________________________

probe dw_syscall.pread =
%( arch == "s390" %?
	__syscall.compat_pread ?,
%)
	__syscall.pread
{
	@_SYSCALL_PREAD_NAME
	fd = __int32($fd)
	buf_uaddr = @__pointer(@choose_defined($buf, $ubuf))
	count = @__compat_ulong($count)
	@_SYSCALL_PREAD_ARGSTR
}
probe __syscall.pread = kernel.function("sys_pread64").call
{
%( arch == "s390" %?
	@__syscall_gate(@const("__NR_pread64"))
%)
	offset = $pos
}
%( arch == "s390" %?
probe __syscall.compat_pread = kernel.function("sys32_pread64").call ?,
	kernel.function("compat_sys_s390_pread64").call ?
{
	offset = ((@choose_defined($poshi, $high) << 32)
		  + __uint32(@choose_defined($poslo, $low)))
}
%)
probe dw_syscall.pread.return =
%( arch == "s390" %?
	kernel.function("sys32_pread64").return ?,
	kernel.function("compat_sys_s390_pread64").return ?,
%)
	__syscall.pread.return
{
	@_SYSCALL_PREAD_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.pread.return = kernel.function("sys_pread64").return
{
%( arch == "s390" %?
	@__syscall_gate(@const("__NR_pread64"))
%)
}

# nd_pread _____________________________________________________

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

probe nd1_syscall.pread =
%( arch == "s390" %?
	__nd1_syscall.compat_pread ?,
%)
	__nd1_syscall.pread
{
	@_SYSCALL_PREAD_NAME
	asmlinkage()
	@_SYSCALL_PREAD_REGARGS
	@_SYSCALL_PREAD_ARGSTR
}
probe __nd1_syscall.pread = kprobe.function("sys_pread64")
{
	asmlinkage()
%( arch == "s390" %?
	@__syscall_gate(@const("__NR_pread64"))
%)
	offset = longlong_arg(4)
}
%( arch == "s390" %?
probe __nd1_syscall.compat_pread = kprobe.function("sys32_pread64") ?,
	kprobe.function("compat_sys_s390_pread64") ?
{
	asmlinkage()
	offset = (u32_arg(4) << 32) + u32_arg(5)
}
%)

/* kernel 4.17+ */
probe nd2_syscall.pread = __nd2_syscall.pread, __nd2_syscall.compat_pread
{
	@_SYSCALL_PREAD_NAME
	@_SYSCALL_PREAD_REGARGS
	@_SYSCALL_PREAD_ARGSTR
}
probe __nd2_syscall.pread = kprobe.function(@arch_syscall_prefix "sys_pread64") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	offset = longlong_arg(4)
},
{
%( @_IS_SREG_KERNEL %?
        @_SYSCALL_PREAD_REGARGS_STORE
	if (@probewrite(offset))
	  set_longlong_arg(4, offset)
%)
}
probe __nd2_syscall.compat_pread = kprobe.function(@arch_syscall_prefix "compat_sys_x86_pread") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	offset = (u32_arg(5) << 32) + u32_arg(4)
},
{
%( @_IS_SREG_KERNEL %?
        @_SYSCALL_PREAD_REGARGS_STORE
	if (@probewrite(offset)) {
	  set_u32_arg(4, offset & 0xffffffff)
	  set_u32_arg(5, offset >> 32)
	}
%)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.pread = __tp_syscall.pread, __tp_syscall.compat_pread 
{
	@_SYSCALL_PREAD_NAME
	@_SYSCALL_PREAD_REGARGS
	@_SYSCALL_PREAD_ARGSTR
}
probe __tp_syscall.pread = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_pread64"))
	offset = longlong_arg(4)
},
{
%( @_IS_SREG_KERNEL %?
        @_SYSCALL_PREAD_REGARGS_STORE
	if (@probewrite(offset))
	  set_longlong_arg(4, offset)
%)
}
probe __tp_syscall.compat_pread = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_pread64"))
	offset = (u32_arg(5) << 32) + u32_arg(4)
},
{
%( @_IS_SREG_KERNEL %?
        @_SYSCALL_PREAD_REGARGS_STORE
	if (@probewrite(offset)) {
	  set_u32_arg(4, offset & 0xffffffff)
	  set_u32_arg(5, offset >> 32)
	}
%)
}

probe nd_syscall.pread.return = nd1_syscall.pread.return!, nd2_syscall.pread.return!, tp_syscall.pread.return
  { }
  
probe nd1_syscall.pread.return =
%( arch == "s390" %?
	kprobe.function("sys32_pread64").return ?,
	kprobe.function("compat_sys_s390_pread64").return ?,
%)
	__nd1_syscall.pread.return
{
	@_SYSCALL_PREAD_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.pread.return = kprobe.function("sys_pread64").return
{
%( arch == "s390" %?
	@__syscall_gate(@const("__NR_pread64"))
%)
}

/* kernel 4.17+ */
probe nd2_syscall.pread.return = kprobe.function(@arch_syscall_prefix "sys_pread64").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_x86_pread").return ?
{
	@_SYSCALL_PREAD_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.pread.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_pread64"), @const("__NR_compat_pread64"))
	@_SYSCALL_PREAD_NAME
	@SYSC_RETVALSTR($ret)
}
