# rename _____________________________________________________
#
# asmlinkage long
# sys_rename(const char __user * oldname,
#		const char __user * newname)
#

@define _SYSCALL_RENAME_NAME
%(
	name = "rename"
%)

@define _SYSCALL_RENAME_ARGSTR
%(
	argstr = sprintf("%s, %s", oldpath, newpath)
%)

@define _SYSCALL_RENAME_REGARGS
%(
	oldpath = user_string_quoted(pointer_arg(1))
	oldpath_unquoted = user_string_nofault(pointer_arg(1))
	newpath = user_string_quoted(pointer_arg(2))
	newpath_unquoted = user_string_nofault(pointer_arg(2))
%)

@define _SYSCALL_RENAME_REGARGS_STORE
%(
        if (@probewrite(oldpath_unquoted))
          set_user_string_arg(pointer_arg(1), oldpath_unquoted)

	if (@probewrite(newpath_unquoted))
	  set_user_string_arg(pointer_arg(2), newpath_unquoted)
%)

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

# dw_rename _____________________________________________________

probe dw_syscall.rename = kernel.function("sys_rename").call
{
	@_SYSCALL_RENAME_NAME
	oldpath = user_string_quoted($oldname)
	newpath = user_string_quoted($newname)
	@_SYSCALL_RENAME_ARGSTR
}
probe dw_syscall.rename.return = kernel.function("sys_rename").return
{
	@_SYSCALL_RENAME_NAME
	@SYSC_RETVALSTR($return)
}

# nd_rename _____________________________________________________

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

probe nd1_syscall.rename = kprobe.function("sys_rename") ?
{
	@_SYSCALL_RENAME_NAME
	asmlinkage()
	@_SYSCALL_RENAME_REGARGS
	@_SYSCALL_RENAME_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.rename = kprobe.function(@arch_syscall_prefix "sys_rename") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_RENAME_NAME
	@_SYSCALL_RENAME_REGARGS
	@_SYSCALL_RENAME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_RENAME_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.rename = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_rename"), @const("__NR_compat_rename"))
	@_SYSCALL_RENAME_NAME
	@_SYSCALL_RENAME_REGARGS
	@_SYSCALL_RENAME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_RENAME_REGARGS_STORE %)
}

probe nd_syscall.rename.return = nd1_syscall.rename.return!, nd2_syscall.rename.return!, tp_syscall.rename.return
  { }
  
probe nd1_syscall.rename.return = kprobe.function("sys_rename").return ?
{
	@_SYSCALL_RENAME_NAME
	@SYSC_RETVALSTR(returnval())
}

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