# sched_getscheduler _________________________________________
#
# long sys_sched_getscheduler(pid_t pid)
#

@define _SYSCALL_SCHED_GETSCHEDULER_NAME
%(
	name = "sched_getscheduler"
%)

@define _SYSCALL_SCHED_GETSCHEDULER_ARGSTR
%(
	argstr = sprint(pid)
%)

@define _SYSCALL_SCHED_GETSCHEDULER_REGARGS
%(
	pid = int_arg(1)
%)

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

# dw_sched_getscheduler _____________________________________________________

probe dw_syscall.sched_getscheduler = kernel.function("sys_sched_getscheduler").call
{
	@_SYSCALL_SCHED_GETSCHEDULER_NAME
	pid = __int32($pid)
	@_SYSCALL_SCHED_GETSCHEDULER_ARGSTR
}
probe dw_syscall.sched_getscheduler.return = kernel.function("sys_sched_getscheduler").return
{
	@_SYSCALL_SCHED_GETSCHEDULER_NAME
	@SYSC_RETVALSTR($return)
}

# nd_sched_getscheduler _____________________________________________________

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

probe nd1_syscall.sched_getscheduler = kprobe.function("sys_sched_getscheduler") ?
{
	@_SYSCALL_SCHED_GETSCHEDULER_NAME
	asmlinkage()
	@_SYSCALL_SCHED_GETSCHEDULER_REGARGS
	@_SYSCALL_SCHED_GETSCHEDULER_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.sched_getscheduler = kprobe.function(@arch_syscall_prefix "sys_sched_getscheduler") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SCHED_GETSCHEDULER_NAME
	@_SYSCALL_SCHED_GETSCHEDULER_REGARGS
	@_SYSCALL_SCHED_GETSCHEDULER_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sched_getscheduler = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_sched_getscheduler"), @const("__NR_compat_sched_getscheduler"))
	@_SYSCALL_SCHED_GETSCHEDULER_NAME
	@_SYSCALL_SCHED_GETSCHEDULER_REGARGS
	@_SYSCALL_SCHED_GETSCHEDULER_ARGSTR
}

probe nd_syscall.sched_getscheduler.return = nd1_syscall.sched_getscheduler.return!, nd2_syscall.sched_getscheduler.return!, tp_syscall.sched_getscheduler.return
  { }
  
probe nd1_syscall.sched_getscheduler.return = kprobe.function("sys_sched_getscheduler").return ?
{
	@_SYSCALL_SCHED_GETSCHEDULER_NAME
	@SYSC_RETVALSTR(returnval())
}

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