# madvise ____________________________________________________
# long sys_madvise(unsigned long start, size_t len_in, int behavior)
#

@define _SYSCALL_MADVISE_NAME
%(
	name = "madvise"
%)

@define _SYSCALL_MADVISE_ARGSTR
%(
	argstr = sprintf("%p, %u, %s", start, length, advice_str)
%)

@define _SYSCALL_MADVISE_REGARGS
%(
	start = ulong_arg(1)
	length = ulong_arg(2)
	advice = int_arg(3)
	advice_str = _madvice_advice_str(advice)
%)

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

# dw_madvise _____________________________________________________

probe dw_syscall.madvise = kernel.function("sys_madvise").call ?
{
	@_SYSCALL_MADVISE_NAME
	start = __ulong($start)
	length = __ulong($len_in)
	advice = __int32($behavior)
	advice_str = _madvice_advice_str(advice)
	@_SYSCALL_MADVISE_ARGSTR
}
probe dw_syscall.madvise.return = kernel.function("sys_madvise").return ?
{
	@_SYSCALL_MADVISE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_madvise _____________________________________________________

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

probe nd1_syscall.madvise = kprobe.function("sys_madvise") ?
{
	@_SYSCALL_MADVISE_NAME
	asmlinkage()
	@_SYSCALL_MADVISE_REGARGS
	@_SYSCALL_MADVISE_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.madvise = kprobe.function(@arch_syscall_prefix "sys_madvise") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_MADVISE_NAME
	@_SYSCALL_MADVISE_REGARGS
	@_SYSCALL_MADVISE_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.madvise = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_madvise"), @const("__NR_compat_madvise"))
	@_SYSCALL_MADVISE_NAME
	@_SYSCALL_MADVISE_REGARGS
	@_SYSCALL_MADVISE_ARGSTR
}

probe nd_syscall.madvise.return = nd1_syscall.madvise.return!, nd2_syscall.madvise.return!, tp_syscall.madvise.return
  { }
  
probe nd1_syscall.madvise.return = kprobe.function("sys_madvise").return ?
{
	@_SYSCALL_MADVISE_NAME
	@SYSC_RETVALSTR(returnval())
}

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