# utimensat ____________________________________________________
# long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
# long compat_sys_utimensat(unsigned int dfd, char __user *filename, struct compat_timespec __user *t, int flags)
#

@define _SYSCALL_UTIMENSAT_NAME
%(
	name = "utimensat"
%)

@define _SYSCALL_UTIMENSAT_ARGSTR
%(
	argstr = sprintf("%s, %s, %s, %s", dfd_str, filename, tsp_str, flags_str) 
%)

@define _SYSCALL_UTIMENSAT_REGARGS
%(
	dfd = int_arg(1)
	dfd_str = _dfd_str(dfd)
	filename_uaddr = pointer_arg(2)
	filename = user_string_quoted(filename_uaddr)
	tsp_uaddr = pointer_arg(3)
	flags = int_arg(4)
	flags_str = _at_flag_str(flags)
%)

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

# dw_utimensat _____________________________________________________

probe dw_syscall.utimensat = kernel.function("sys_utimensat").call ?
{
	@_SYSCALL_UTIMENSAT_NAME
	dfd = __int32($dfd)
	dfd_str = _dfd_str(__int32($dfd))
	filename_uaddr = $filename
	filename = user_string_quoted($filename)
	tsp_uaddr = $utimes
	tsp_str = _struct_timespec_u(tsp_uaddr, 2)
	flags = __int32($flags)
	flags_str = _at_flag_str(__int32($flags))
	@_SYSCALL_UTIMENSAT_ARGSTR
}
probe dw_syscall.utimensat.return = kernel.function("sys_utimensat").return ?
{
	@_SYSCALL_UTIMENSAT_NAME
	@SYSC_RETVALSTR($return)
}

# nd_utimensat _____________________________________________________

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

probe nd1_syscall.utimensat = kprobe.function("sys_utimensat") ?
{
	@_SYSCALL_UTIMENSAT_NAME
	asmlinkage()
	@_SYSCALL_UTIMENSAT_REGARGS
	tsp_str = _struct_timespec_u(tsp_uaddr, 2)
	@_SYSCALL_UTIMENSAT_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.utimensat = kprobe.function(@arch_syscall_prefix "sys_utimensat") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_UTIMENSAT_NAME
	@_SYSCALL_UTIMENSAT_REGARGS
	tsp_str = _struct_timespec_u(tsp_uaddr, 2)
	@_SYSCALL_UTIMENSAT_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.utimensat = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_utimensat"))
	@_SYSCALL_UTIMENSAT_NAME
	@_SYSCALL_UTIMENSAT_REGARGS
	tsp_str = _struct_timespec_u(tsp_uaddr, 2)
	@_SYSCALL_UTIMENSAT_ARGSTR
}

probe nd_syscall.utimensat.return = nd1_syscall.utimensat.return!, nd2_syscall.utimensat.return!, tp_syscall.utimensat.return
  { }
  
probe nd1_syscall.utimensat.return = kprobe.function("sys_utimensat").return ?
{
	@_SYSCALL_UTIMENSAT_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.utimensat.return = kprobe.function(@arch_syscall_prefix "sys_utimensat").return ?
{
	@_SYSCALL_UTIMENSAT_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.utimensat.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_utimensat"))
	@_SYSCALL_UTIMENSAT_NAME
	@SYSC_RETVALSTR($ret)
}

# compat_utimensat _____________________________________________
# long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
# long compat_sys_utimensat(unsigned int dfd, char __user *filename, struct compat_timespec __user *t, int flags)
#

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

# dw_compat_utimensat _____________________________________________________

probe dw_syscall.compat_utimensat = kernel.function("compat_sys_utimensat").call ?
{
	@_SYSCALL_UTIMENSAT_NAME
	dfd = __int32($dfd)
	dfd_str = _dfd_str(__int32($dfd))
	filename_uaddr = @__pointer($filename)
	filename = user_string_quoted(filename_uaddr)
	tsp_uaddr = @__pointer($t)
	tsp_str = _struct_compat_timespec_u(tsp_uaddr, 2)
	flags = __int32($flags)
	flags_str = _at_flag_str(__int32($flags))
	@_SYSCALL_UTIMENSAT_ARGSTR
}
probe dw_syscall.compat_utimensat.return =  kernel.function("compat_sys_utimensat").return ?
{
	@_SYSCALL_UTIMENSAT_NAME
	@SYSC_RETVALSTR($return)
}

# nd_compat_utimensat _____________________________________________________

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

probe nd1_syscall.compat_utimensat = kprobe.function("compat_sys_utimensat") ?
{
	@_SYSCALL_UTIMENSAT_NAME
	asmlinkage()
	@_SYSCALL_UTIMENSAT_REGARGS
	tsp_str = _struct_compat_timespec_u(tsp_uaddr, 2)
	@_SYSCALL_UTIMENSAT_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.compat_utimensat = kprobe.function(@arch_syscall_prefix "compat_sys_utimensat") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_UTIMENSAT_NAME
	@_SYSCALL_UTIMENSAT_REGARGS
	tsp_str = _struct_compat_timespec_u(tsp_uaddr, 2)
	@_SYSCALL_UTIMENSAT_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_utimensat = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_utimensat"))
	@_SYSCALL_UTIMENSAT_NAME
	@_SYSCALL_UTIMENSAT_REGARGS
	tsp_str = _struct_compat_timespec_u(tsp_uaddr, 2)
	@_SYSCALL_UTIMENSAT_ARGSTR
}

probe nd_syscall.compat_utimensat.return = nd1_syscall.compat_utimensat.return!, nd2_syscall.compat_utimensat.return!, tp_syscall.compat_utimensat.return
  { }
  
probe nd1_syscall.compat_utimensat.return = kprobe.function("compat_sys_utimensat").return ?
{
	@_SYSCALL_UTIMENSAT_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.compat_utimensat.return = kprobe.function(@arch_syscall_prefix "compat_sys_utimensat").return ?
{
	@_SYSCALL_UTIMENSAT_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_utimensat.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_utimensat"))
	@_SYSCALL_UTIMENSAT_NAME
	@SYSC_RETVALSTR($ret)
}
