# symlinkat __________________________________________________
# new function with 2.6.16
# long sys_symlinkat(const char __user *oldname, int newdfd,
#	const char __user *newname)

@define _SYSCALL_SYMLINKAT_NAME
%(
	name = "symlinkat"
%)

@define _SYSCALL_SYMLINKAT_ARGSTR
%(
	argstr = sprintf("%s, %s, %s", oldname_str, newdfd_str, newname_str)
%)

@define _SYSCALL_SYMLINKAT_REGARGS
%(
	oldname = pointer_arg(1)
	oldname_str = user_string_quoted(oldname)
	newdfd = int_arg(2)
	newdfd_str = _dfd_str(newdfd)
	newname = pointer_arg(3)
	newname_str = user_string_quoted(newname)
%)

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

# dw_symlinkat _____________________________________________________

probe dw_syscall.symlinkat = kernel.function("sys_symlinkat").call ?
{
	@__syscall_compat_gate(@const("__NR_symlinkat"),
			       @const("__NR_compat_symlinkat"))
	@_SYSCALL_SYMLINKAT_NAME
	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)
	@_SYSCALL_SYMLINKAT_ARGSTR
}
probe dw_syscall.symlinkat.return = kernel.function("sys_symlinkat").return ?
{
	@__syscall_compat_gate(@const("__NR_symlinkat"),
			       @const("__NR_compat_symlinkat"))
	@_SYSCALL_SYMLINKAT_NAME
	@SYSC_RETVALSTR($return)
}

# nd_symlinkat _____________________________________________________

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

probe nd1_syscall.symlinkat = kprobe.function("sys_symlinkat") ?
{
	@__syscall_compat_gate(@const("__NR_symlinkat"),
			       @const("__NR_compat_symlinkat"))
	@_SYSCALL_SYMLINKAT_NAME
	asmlinkage()
	@_SYSCALL_SYMLINKAT_REGARGS
	@_SYSCALL_SYMLINKAT_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.symlinkat = kprobe.function(@arch_syscall_prefix "sys_symlinkat") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SYMLINKAT_NAME
	@_SYSCALL_SYMLINKAT_REGARGS
	@_SYSCALL_SYMLINKAT_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.symlinkat = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_symlinkat"),
			       @const("__NR_compat_symlinkat"))
	@_SYSCALL_SYMLINKAT_NAME
	@_SYSCALL_SYMLINKAT_REGARGS
	@_SYSCALL_SYMLINKAT_ARGSTR
}

probe nd_syscall.symlinkat.return = nd1_syscall.symlinkat.return!, nd2_syscall.symlinkat.return!, tp_syscall.symlinkat.return
  { }
  
probe nd1_syscall.symlinkat.return = kprobe.function("sys_symlinkat").return ?
{
	@__syscall_compat_gate(@const("__NR_symlinkat"),
			       @const("__NR_compat_symlinkat"))
	@_SYSCALL_SYMLINKAT_NAME
	@SYSC_RETVALSTR(returnval())
}

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