# renameat2 ___________________________________________________
# SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
#		int, newdfd, const char __user *, newname, unsigned int, flags)
#
# renameat2() was added to Linux in kernel 3.15.
#

@define _SYSCALL_RENAMEAT2_NAME
%(
	name = "renameat2"
%)

@define _SYSCALL_RENAMEAT2_ARGSTR
%(
	argstr = sprintf("%s, %s, %s, %s, %s", olddfd_str, oldname_str, newdfd_str, newname_str, flags_str)
%)

@define _SYSCALL_RENAMEAT2_REGARGS
%(
	olddfd = int_arg(1)
	olddfd_str = _dfd_str(olddfd)
	oldname = pointer_arg(2)
	oldname_str = user_string_quoted(oldname)
	oldname_str_unquoted = user_string_nofault(pointer_arg(2))
	newdfd = int_arg(3)
	newdfd_str = _dfd_str(newdfd)
	newname = pointer_arg(4)
	newname_str = user_string_quoted(newname)
	newname_str_unquoted = user_string_nofault(pointer_arg(4))
	flags = uint_arg(5)
	flags_str = _renameat2_flags_str(flags)
%)

@define _SYSCALL_RENAMEAT2_REGARGS_STORE
%(
        if (@probewrite(olddfd))
          set_int_arg(1, olddfd)

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

	if (@probewrite(oldname_str_unquoted))
	  set_user_string_arg(pointer_arg(2), oldname_str_unquoted)

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

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

	if (@probewrite(newname_str_unquoted))
	  set_user_string_arg(pointer_arg(4), newname_str_unquoted)

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

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

# dw_renameat2 _____________________________________________________

probe dw_syscall.renameat2 = kernel.function("sys_renameat2").call ?
{
	@__syscall_compat_gate(@const("__NR_renameat2"),
			       @const("__NR_compat_renameat2"))
	@_SYSCALL_RENAMEAT2_NAME
	olddfd = __int32($olddfd)
	olddfd_str = _dfd_str(__int32($olddfd))
	oldname = $oldname
	oldname_str = user_string_quoted($oldname)
	newdfd = __int32($newdfd)
	newdfd_str = _dfd_str(__int32($newdfd))
	newname = $newname
	newname_str = user_string_quoted($newname)
	flags = __uint32($flags)
	flags_str = _renameat2_flags_str(flags)
	@_SYSCALL_RENAMEAT2_ARGSTR
}
probe dw_syscall.renameat2.return = kernel.function("sys_renameat2").return ?
{
	@__syscall_compat_gate(@const("__NR_renameat2"),
			       @const("__NR_compat_renameat2"))
	@_SYSCALL_RENAMEAT2_NAME
	@SYSC_RETVALSTR($return)
}

# nd_renameat2 _____________________________________________________

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

probe nd1_syscall.renameat2 = kprobe.function("sys_renameat2") ?
{
	@__syscall_compat_gate(@const("__NR_renameat2"),
			       @const("__NR_compat_renameat2"))
	@_SYSCALL_RENAMEAT2_NAME
	asmlinkage()
	@_SYSCALL_RENAMEAT2_REGARGS
	@_SYSCALL_RENAMEAT2_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.renameat2 = kprobe.function(@arch_syscall_prefix "sys_renameat2") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_RENAMEAT2_NAME
	@_SYSCALL_RENAMEAT2_REGARGS
	@_SYSCALL_RENAMEAT2_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_RENAMEAT2_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.renameat2 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_renameat2"), @const("__NR_compat_renameat2"))
	@_SYSCALL_RENAMEAT2_NAME
	@_SYSCALL_RENAMEAT2_REGARGS
	@_SYSCALL_RENAMEAT2_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_RENAMEAT2_REGARGS_STORE %)
}

probe nd_syscall.renameat2.return = nd1_syscall.renameat2.return!, nd2_syscall.renameat2.return!, tp_syscall.renameat2.return
  { }
  
probe nd1_syscall.renameat2.return = kprobe.function("sys_renameat2").return ?
{
	@__syscall_compat_gate(@const("__NR_renameat2"),
			       @const("__NR_compat_renameat2"))
	@_SYSCALL_RENAMEAT2_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.renameat2.return = kprobe.function(@arch_syscall_prefix "sys_renameat2").return ?
{
	@_SYSCALL_RENAMEAT2_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.renameat2.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_renameat2"), @const("__NR_compat_renameat2"))
	@_SYSCALL_RENAMEAT2_NAME
	@SYSC_RETVALSTR($ret)
}
