# splice ___________________________________________________
#
# long sys_splice(int fd_in, loff_t __user *off_in,
#                 int fd_out, loff_t __user *off_out,
#                 size_t len, unsigned int flags)
#

@define _SYSCALL_SPLICE_NAME
%(
	name = "splice"
%)

@define _SYSCALL_SPLICE_ARGSTR
%(
	argstr = sprintf("%d, %p, %d, %p, %u, %s", fd_in, off_in, fd_out,
	                 off_out, len, flags_str);
%)

@define _SYSCALL_SPLICE_REGARGS
%(
	fd_in = int_arg(1)
	off_in = pointer_arg(2)
	fd_out = int_arg(3)
	off_out = pointer_arg(4)
	len = ulong_arg(5)
	flags = uint_arg(6)
	flags_str = (_stp_splice_flags_str(flags))
%)

@define _SYSCALL_SPLICE_REGARGS_STORE
%(
        if (@probewrite(fd_in))
          set_int_arg(1, fd_in)

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

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

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

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

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

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

# dw_splice _____________________________________________________

probe dw_syscall.splice = kernel.function("sys_splice").call ?
{
	@_SYSCALL_SPLICE_NAME
	fd_in = __int32($fd_in)
	off_in = $off_in
	fd_out = __int32($fd_out)
	off_out = $off_out
	len = __ulong($len)
	flags = $flags
	flags_str = (_stp_splice_flags_str(flags))
	@_SYSCALL_SPLICE_ARGSTR
}
probe dw_syscall.splice.return = kernel.function("sys_splice").return ?
{
	@_SYSCALL_SPLICE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_splice _____________________________________________________

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

probe nd1_syscall.splice = kprobe.function("sys_splice") ?
{
	@_SYSCALL_SPLICE_NAME
	asmlinkage()
	@_SYSCALL_SPLICE_REGARGS
	@_SYSCALL_SPLICE_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.splice = kprobe.function(@arch_syscall_prefix "sys_splice") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SPLICE_NAME
	@_SYSCALL_SPLICE_REGARGS
	@_SYSCALL_SPLICE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SPLICE_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.splice = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_splice"), @const("__NR_compat_splice"))
	@_SYSCALL_SPLICE_NAME
	@_SYSCALL_SPLICE_REGARGS
	@_SYSCALL_SPLICE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SPLICE_REGARGS_STORE %)
}

probe nd_syscall.splice.return = nd1_syscall.splice.return!, nd2_syscall.splice.return!, tp_syscall.splice.return
  { }

probe nd1_syscall.splice.return = kprobe.function("sys_splice").return ?
{
	@_SYSCALL_SPLICE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.splice.return = kprobe.function(@arch_syscall_prefix "sys_splice").return ?
{
	@_SYSCALL_SPLICE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.splice.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_splice"), @const("__NR_compat_splice"))
	@_SYSCALL_SPLICE_NAME
	@SYSC_RETVALSTR($ret)
}
