# clock_settime ______________________________________________
# long sys_clock_settime(clockid_t which_clock,
#                   const struct timespec __user *tp)
#
# long compat_sys_clock_settime(clockid_t which_clock,
#		struct compat_timespec __user *tp)

@define _SYSCALL_CLOCK_SETTIME_NAME
%(
	name = "clock_settime"
%)

@define _SYSCALL_CLOCK_SETTIME_ARGSTR
%(
	argstr = sprintf("%s, %s", clk_id_str, tp_uaddr_str)
%)

@define _SYSCALL_CLOCK_SETTIME_REGARGS
%(
	clk_id = int_arg(1)
	clk_id_str = _get_wc_str(clk_id)
	tp_uaddr = pointer_arg(2)
%)

@define _SYSCALL_CLOCK_SETTIME_REGARGS_STORE
%(
        if (@probewrite(clk_id))
          set_int_arg(1, clk_id)

	if (@probewrite(tp_uaddr))
	  set_pointer_arg(2, tp_uaddr)
%)

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

# dw_clock_settime _____________________________________________________

probe dw_syscall.clock_settime = __syscall.clock_settime,
	__syscall.compat_clock_settime ?
{
	@_SYSCALL_CLOCK_SETTIME_NAME
}
probe __syscall.clock_settime = kernel.function("sys_clock_settime").call
{
	@__syscall_gate(@const("__NR_clock_settime"))
	clk_id = __int32($which_clock)
	clk_id_str = _get_wc_str($which_clock)
	tp_uaddr = $tp
	tp_uaddr_str = _struct_timespec_u(tp_uaddr, 1)
	@_SYSCALL_CLOCK_SETTIME_ARGSTR
}
probe __syscall.compat_clock_settime =
	kernel.function("compat_sys_clock_settime").call ?
{
	clk_id = __int32($which_clock)
	clk_id_str = _get_wc_str(__int32($which_clock))
	tp_uaddr = $tp
	tp_uaddr_str = _struct_compat_timespec_u(tp_uaddr, 1)
	@_SYSCALL_CLOCK_SETTIME_ARGSTR
}
probe dw_syscall.clock_settime.return = __syscall.clock_settime.return,
	__syscall.compat_clock_settime.return ?
{
	@_SYSCALL_CLOCK_SETTIME_NAME
}
probe __syscall.clock_settime.return =
	kernel.function("sys_clock_settime").return
{
	@__syscall_gate(@const("__NR_clock_settime"))
	@SYSC_RETVALSTR($return)
}
probe __syscall.compat_clock_settime.return =
	kernel.function("compat_sys_clock_settime").return ?
{
	@SYSC_RETVALSTR($return)
}

# nd_clock_settime _____________________________________________________

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

probe nd1_syscall.clock_settime = __nd1_syscall.clock_settime,
	__nd1_syscall.compat_clock_settime ?
{
	@_SYSCALL_CLOCK_SETTIME_NAME
}
probe __nd1_syscall.clock_settime = kprobe.function("sys_clock_settime").call
{
	@__syscall_gate(@const("__NR_clock_settime"))
	asmlinkage()
	@_SYSCALL_CLOCK_SETTIME_REGARGS
	tp_uaddr_str = _struct_timespec_u(tp_uaddr, 1)
	@_SYSCALL_CLOCK_SETTIME_ARGSTR
}
probe __nd1_syscall.compat_clock_settime =
	kprobe.function("compat_sys_clock_settime").call ?
{
	asmlinkage()
	@_SYSCALL_CLOCK_SETTIME_REGARGS
	tp_uaddr_str = _struct_compat_timespec_u(tp_uaddr, 1)
	@_SYSCALL_CLOCK_SETTIME_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.clock_settime = __nd2_syscall.clock_settime,
	__nd2_syscall.compat_clock_settime ?
{
	@_SYSCALL_CLOCK_SETTIME_NAME
}
probe __nd2_syscall.clock_settime = kprobe.function(@arch_syscall_prefix "sys_clock_settime").call
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_CLOCK_SETTIME_REGARGS
	tp_uaddr_str = _struct_timespec_u(tp_uaddr, 1)
	@_SYSCALL_CLOCK_SETTIME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_CLOCK_SETTIME_REGARGS_STORE %)
}
probe __nd2_syscall.compat_clock_settime =
	kprobe.function(@arch_syscall_prefix "compat_sys_clock_settime").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_CLOCK_SETTIME_REGARGS
	tp_uaddr_str = _struct_compat_timespec_u(tp_uaddr, 1)
	@_SYSCALL_CLOCK_SETTIME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_CLOCK_SETTIME_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.clock_settime = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_clock_settime"), @const("__NR_compat_clock_settime"))
	@_SYSCALL_CLOCK_SETTIME_NAME
	@_SYSCALL_CLOCK_SETTIME_REGARGS
	if (@__compat_task)
		tp_uaddr_str = _struct_compat_timespec_u(tp_uaddr, 1)
	else
		tp_uaddr_str = _struct_timespec_u(tp_uaddr, 1)
	@_SYSCALL_CLOCK_SETTIME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_CLOCK_SETTIME_REGARGS_STORE %)
}

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

probe nd1_syscall.clock_settime.return = __nd1_syscall.clock_settime.return,
	__nd1_syscall.compat_clock_settime.return ?
{
 	@_SYSCALL_CLOCK_SETTIME_NAME
}
probe __nd1_syscall.clock_settime.return =
	kprobe.function("sys_clock_settime").return
{
	@__syscall_gate(@const("__NR_clock_settime"))
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.compat_clock_settime.return =
	kprobe.function("compat_sys_clock_settime").return ?
{
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.clock_settime.return = kprobe.function(@arch_syscall_prefix "sys_clock_settime").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_clock_settime").return ?
{
	@_SYSCALL_CLOCK_SETTIME_NAME
	@SYSC_RETVALSTR(returnval())
}

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