# sched_getattr ______________________________________________
#
# SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
#                 unsigned int, size, unsigned int, flags)
#

@define _SYSCALL_SCHED_GETATTR_NAME
%(
	name = "sched_getattr"
%)

@define _SYSCALL_SCHED_GETATTR_ARGSTR
%(
	argstr = sprintf("%d, %s, %u, %u", pid, sched_attr_str, size, flags)
%)

@define _SYSCALL_SCHED_GETATTR_REGARGS
%(
	pid = int_arg(1)
	sched_attr_uaddr = pointer_arg(2)
	sched_attr_str = _struct_sched_attr_u(sched_attr_uaddr)
	size = uint_arg(3)
	flags = uint_arg(4)
%)

@define _SYSCALL_SCHED_GETATTR_REGARGS_STORE
%(
        if (@probewrite(pid))
          set_int_arg(1, pid)

	if (@probewrite(sched_attr_uaddr))
	  set_pointer_arg(2, sched_attr_uaddr)

	if (@probewrite(size))
	  set_uint_arg(3, size)

	if (@probewrite(flags))
	  set_uint_arg(4, flags)
%)

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

# dw_sched_getattr _____________________________________________________

probe dw_syscall.sched_getattr = kernel.function("sys_sched_getattr") ?
{
	@_SYSCALL_SCHED_GETATTR_NAME
	pid = __int32($pid)
	sched_attr_uaddr = $uattr
	sched_attr_str = _struct_sched_attr_u(sched_attr_uaddr)
        /* Kernel commit 1251201c0d34f (v5.3-rc8) changes $size to $usize */
	size = __uint32(@choose_defined($usize, $size))
	flags = __uint32($flags)
	@_SYSCALL_SCHED_GETATTR_ARGSTR
}
probe dw_syscall.sched_getattr.return =
	kernel.function("sys_sched_getattr").return ?
{
	@_SYSCALL_SCHED_GETATTR_NAME
	@SYSC_RETVALSTR($return)
}

# nd_sched_getattr _____________________________________________________

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

probe nd1_syscall.sched_getattr = kprobe.function("sys_sched_getattr") ?
{
	@_SYSCALL_SCHED_GETATTR_NAME
	asmlinkage()
	@_SYSCALL_SCHED_GETATTR_REGARGS
	@_SYSCALL_SCHED_GETATTR_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.sched_getattr = kprobe.function(@arch_syscall_prefix "sys_sched_getattr") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SCHED_GETATTR_NAME
	@_SYSCALL_SCHED_GETATTR_REGARGS
	@_SYSCALL_SCHED_GETATTR_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SCHED_GETATTR_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sched_getattr = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_sched_getattr"), @const("__NR_compat_sched_getattr"))
	@_SYSCALL_SCHED_GETATTR_NAME
	@_SYSCALL_SCHED_GETATTR_REGARGS
	@_SYSCALL_SCHED_GETATTR_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SCHED_GETATTR_REGARGS_STORE %)
}

probe nd_syscall.sched_getattr.return = nd1_syscall.sched_getattr.return!, nd2_syscall.sched_getattr.return!, tp_syscall.sched_getattr.return
  { }
  
probe nd1_syscall.sched_getattr.return =
	kprobe.function("sys_sched_getattr").return ?
{
	@_SYSCALL_SCHED_GETATTR_NAME
	@SYSC_RETVALSTR(returnval())
}

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