# timer_gettime ______________________________________________
#
# long sys_timer_gettime(timer_t timer_id,
#			 struct itimerspec __user *setting)
# long compat_sys_timer_gettime(timer_t timer_id,
#			 struct itimerspec __user *setting)
#

@define _SYSCALL_TIMER_GETTIME_NAME
%(
	name = "timer_gettime"
%)

@define _SYSCALL_TIMER_GETTIME_ARGSTR
%(
	argstr = sprintf("%d, %p", timerid, value_uaddr)
%)

@define _SYSCALL_TIMER_GETTIME_REGARGS
%(
	timerid = int_arg(1)
	value_uaddr = pointer_arg(2)
%)

@define _SYSCALL_TIMER_GETTIME_REGARGS_STORE
%(
        if (@probewrite(timerid))
          set_int_arg(1, timerid)

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

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

# dw_timer_gettime _____________________________________________________

probe dw_syscall.timer_gettime = __syscall.timer_gettime,
                              kernel.function("compat_sys_timer_gettime").call ?
{
	@_SYSCALL_TIMER_GETTIME_NAME
	timerid = __int32($timer_id)
	value_uaddr = $setting
	@_SYSCALL_TIMER_GETTIME_ARGSTR
}
probe __syscall.timer_gettime = kernel.function("sys_timer_gettime").call
{
	@__syscall_gate(@const("__NR_timer_gettime"))
}
probe dw_syscall.timer_gettime.return =
        __syscall.timer_gettime.return,
        kernel.function("compat_sys_timer_gettime").return ?
{
	@_SYSCALL_TIMER_GETTIME_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.timer_gettime.return = kernel.function("sys_timer_gettime").return
{
	@__syscall_gate(@const("__NR_timer_gettime"))
}

# nd_timer_gettime _____________________________________________________

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

probe nd1_syscall.timer_gettime = __nd1_syscall.timer_gettime,
	__nd1_syscall.compat_timer_gettime ?
{
	@_SYSCALL_TIMER_GETTIME_NAME
}

probe __nd1_syscall.timer_gettime = kprobe.function("sys_timer_gettime")
{
	@__syscall_gate(@const("__NR_timer_gettime"))
	asmlinkage()
	@_SYSCALL_TIMER_GETTIME_REGARGS
	@_SYSCALL_TIMER_GETTIME_ARGSTR
}
probe __nd1_syscall.compat_timer_gettime = kprobe.function("compat_sys_timer_gettime") ?
{
	asmlinkage()
	@_SYSCALL_TIMER_GETTIME_REGARGS
	@_SYSCALL_TIMER_GETTIME_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.timer_gettime = __nd2_syscall.timer_gettime,
	__nd2_syscall.compat_timer_gettime ?
{
	@_SYSCALL_TIMER_GETTIME_NAME
}
probe __nd2_syscall.timer_gettime =
	kprobe.function(@arch_syscall_prefix "sys_timer_gettime")
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_TIMER_GETTIME_REGARGS
	@_SYSCALL_TIMER_GETTIME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_TIMER_GETTIME_REGARGS_STORE %)
}
probe __nd2_syscall.compat_timer_gettime =
	kprobe.function(@arch_syscall_prefix "compat_sys_timer_gettime") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_TIMER_GETTIME_REGARGS
	@_SYSCALL_TIMER_GETTIME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_TIMER_GETTIME_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.timer_gettime = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_timer_gettime"), @const("__NR_compat_timer_gettime"))
	@_SYSCALL_TIMER_GETTIME_NAME
	@_SYSCALL_TIMER_GETTIME_REGARGS
	@_SYSCALL_TIMER_GETTIME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_TIMER_GETTIME_REGARGS_STORE %)
}

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

probe nd1_syscall.timer_gettime.return =
	__nd1_syscall.timer_gettime.return,
	kprobe.function("compat_sys_timer_gettime").return ?
{
	@_SYSCALL_TIMER_GETTIME_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.timer_gettime.return = kprobe.function("sys_timer_gettime").return
{
	@__syscall_gate(@const("__NR_timer_gettime"))
}

/* kernel 4.17+ */
probe nd2_syscall.timer_gettime.return =
	kprobe.function(@arch_syscall_prefix "sys_timer_gettime").return,
	kprobe.function(@arch_syscall_prefix "compat_sys_timer_gettime").return ?
{
	@_SYSCALL_TIMER_GETTIME_NAME
	@SYSC_RETVALSTR(returnval())
}

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