# sched_getaffinity __________________________________________
#
# asmlinkage long
# sys_sched_getaffinity(pid_t pid,
#			     unsigned int len,
#			     unsigned long __user *user_mask_ptr)
# COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t,  pid,
#			 unsigned int, len,
#			 compat_ulong_t __user *, user_mask_ptr)
#

@define _SYSCALL_SCHED_GETAFFINITY_NAME
%(
	name = "sched_getaffinity"
%)

@define _SYSCALL_SCHED_GETAFFINITY_ARGSTR
%(
	argstr = sprintf("%d, %u, %p", pid, len, mask_uaddr)
%)

@define _SYSCALL_SCHED_GETAFFINITY_REGARGS
%(
	pid = int_arg(1)
	len = uint_arg(2)
	mask_uaddr = pointer_arg(3)
%)

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

	if (@probewrite(len))
	  set_uint_arg(2, len)

	if (@probewrite(mask_uaddr))
	  set_pointer_arg(3, mask_uaddr)
%)

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

# dw_sched_getaffinity _____________________________________________________

probe dw_syscall.sched_getaffinity =
	kernel.function("sys_sched_getaffinity").call ?,
	kernel.function("compat_sys_sched_getaffinity").call ?
{
	@_SYSCALL_SCHED_GETAFFINITY_NAME
	pid = __int32($pid)
	len = __uint32($len)
	mask_uaddr = @__pointer($user_mask_ptr)
	@_SYSCALL_SCHED_GETAFFINITY_ARGSTR
}
probe dw_syscall.sched_getaffinity.return =
	kernel.function("sys_sched_getaffinity").return ?,
	kernel.function("compat_sys_sched_getaffinity").return ?
{
	@_SYSCALL_SCHED_GETAFFINITY_NAME
	@SYSC_RETVALSTR($return)
}

# nd_sched_getaffinity _____________________________________________________

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

probe nd1_syscall.sched_getaffinity =
	kprobe.function("sys_sched_getaffinity") ?,
	kprobe.function("compat_sys_sched_getaffinity") ?
{
	@_SYSCALL_SCHED_GETAFFINITY_NAME
	asmlinkage()
	@_SYSCALL_SCHED_GETAFFINITY_REGARGS
	@_SYSCALL_SCHED_GETAFFINITY_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.sched_getaffinity =
	kprobe.function(@arch_syscall_prefix "sys_sched_getaffinity") ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_sched_getaffinity") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SCHED_GETAFFINITY_NAME
	@_SYSCALL_SCHED_GETAFFINITY_REGARGS
	@_SYSCALL_SCHED_GETAFFINITY_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SCHED_GETAFFINITY_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sched_getaffinity = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_sched_getaffinity"), @const("__NR_compat_sched_getaffinity"))
	@_SYSCALL_SCHED_GETAFFINITY_NAME
	@_SYSCALL_SCHED_GETAFFINITY_REGARGS
	@_SYSCALL_SCHED_GETAFFINITY_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SCHED_GETAFFINITY_REGARGS_STORE %)
}

probe nd_syscall.sched_getaffinity.return = nd1_syscall.sched_getaffinity.return!, nd2_syscall.sched_getaffinity.return!, tp_syscall.sched_getaffinity.return
  { }
  
probe nd1_syscall.sched_getaffinity.return =
	kprobe.function("sys_sched_getaffinity").return ?,
	kprobe.function("compat_sys_sched_getaffinity").return ?
{
	@_SYSCALL_SCHED_GETAFFINITY_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.sched_getaffinity.return =
	kprobe.function(@arch_syscall_prefix "sys_sched_getaffinity").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_sched_getaffinity").return ?
{
	@_SYSCALL_SCHED_GETAFFINITY_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sched_getaffinity.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_sched_getaffinity"), @const("__NR_compat_sched_getaffinity"))
	@_SYSCALL_SCHED_GETAFFINITY_NAME
	@SYSC_RETVALSTR($ret)
}
