# nanosleep __________________________________________________
#
# long sys_nanosleep(struct timespec __user *rqtp,
#		struct timespec __user *rmtp)
# long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
#		struct compat_timespec __user *rmtp)
#

@define _SYSCALL_NANOSLEEP_NAME
%(
	name = "nanosleep"
%)

@define _SYSCALL_NANOSLEEP_ARGSTR
%(
	argstr = sprintf("%s, %p", req_str, rem_uaddr)
%)

@define _SYSCALL_NANOSLEEP_REGARGS
%(
	req_uaddr = pointer_arg(1)
	rem_uaddr = pointer_arg(2)
%)

@define _SYSCALL_NANOSLEEP_REGARGS_STORE
%(
        if (@probewrite(req_uaddr))
          set_pointer_arg(1, req_uaddr)

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

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

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

# dw_nanosleep _____________________________________________________

probe dw_syscall.nanosleep = kernel.function("sys_nanosleep").call
{
	@_SYSCALL_NANOSLEEP_NAME
	req_uaddr = $rqtp
	req_str = _struct_timespec_u(req_uaddr, 1)
	rem_uaddr = $rmtp
	@_SYSCALL_NANOSLEEP_ARGSTR
}
probe dw_syscall.nanosleep.return = kernel.function("sys_nanosleep").return
{
	@_SYSCALL_NANOSLEEP_NAME
	@SYSC_RETVALSTR($return)
}
probe dw_syscall.compat_nanosleep = kernel.function("compat_sys_nanosleep").call ?
{
	@_SYSCALL_NANOSLEEP_NAME
	req_uaddr = @__pointer($rqtp)
	req_str = _struct_compat_timespec_u(req_uaddr, 1)
	rem_uaddr = @__pointer($rmtp)
	@_SYSCALL_NANOSLEEP_ARGSTR
}
probe dw_syscall.compat_nanosleep.return = kernel.function("compat_sys_nanosleep").return ?
{
	@_SYSCALL_NANOSLEEP_NAME
	@SYSC_RETVALSTR($return)
}

# nd_nanosleep _____________________________________________________

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

probe nd1_syscall.nanosleep = kprobe.function("sys_nanosleep").call ?
{
	@_SYSCALL_NANOSLEEP_NAME
	asmlinkage()
	@_SYSCALL_NANOSLEEP_REGARGS
	req_str = _struct_timespec_u(req_uaddr, 1)
	@_SYSCALL_NANOSLEEP_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.nanosleep = kprobe.function(@arch_syscall_prefix "sys_nanosleep") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_NANOSLEEP_NAME
	@_SYSCALL_NANOSLEEP_REGARGS
	req_str = _struct_timespec_u(req_uaddr, 1)
	@_SYSCALL_NANOSLEEP_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_NANOSLEEP_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.nanosleep = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_nanosleep"))
	@_SYSCALL_NANOSLEEP_NAME
	@_SYSCALL_NANOSLEEP_REGARGS
	req_str = _struct_timespec_u(req_uaddr, 1)
	@_SYSCALL_NANOSLEEP_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_NANOSLEEP_REGARGS_STORE %)
}

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

probe nd1_syscall.nanosleep.return = kprobe.function("sys_nanosleep").return ?
{
	@_SYSCALL_NANOSLEEP_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.nanosleep.return = kprobe.function(@arch_syscall_prefix "sys_nanosleep").return ?
{
	@_SYSCALL_NANOSLEEP_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.nanosleep.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_nanosleep"))
	@_SYSCALL_NANOSLEEP_NAME
	@SYSC_RETVALSTR($ret) /* NB: not in the $regs */
}

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

probe nd1_syscall.compat_nanosleep = kprobe.function("compat_sys_nanosleep") ?
{
	@_SYSCALL_NANOSLEEP_NAME
	asmlinkage()
	@_SYSCALL_NANOSLEEP_REGARGS
	req_str = _struct_compat_timespec_u(req_uaddr, 1)
	@_SYSCALL_NANOSLEEP_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.compat_nanosleep = kprobe.function(@arch_syscall_prefix "compat_sys_nanosleep") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_NANOSLEEP_NAME
	@_SYSCALL_NANOSLEEP_REGARGS
	req_str = _struct_compat_timespec_u(req_uaddr, 1)
	@_SYSCALL_NANOSLEEP_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_NANOSLEEP_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_nanosleep = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_nanosleep"))
	@_SYSCALL_NANOSLEEP_NAME
	@_SYSCALL_NANOSLEEP_REGARGS
	req_str = _struct_compat_timespec_u(req_uaddr, 1)
	@_SYSCALL_NANOSLEEP_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_NANOSLEEP_REGARGS_STORE %)
}

probe nd_syscall.compat_nanosleep.return = nd1_syscall.compat_nanosleep.return!, nd2_syscall.compat_nanosleep.return!, tp_syscall.compat_nanosleep.return
  { }
  
probe nd1_syscall.compat_nanosleep.return = kprobe.function("compat_sys_nanosleep").return ?
{
	@_SYSCALL_NANOSLEEP_NAME
	@SYSC_RETVALSTR(returnval())
}

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