# msync ______________________________________________________
# long sys_msync(unsigned long start, size_t len, int flags)

@define _SYSCALL_MSYNC_NAME
%(
	name = "msync"
%)

@define _SYSCALL_MSYNC_ARGSTR
%(
	argstr = sprintf("%p, %u, %s", start, length, flags_str)
%)

@define _SYSCALL_MSYNC_REGARGS
%(
	start = ulong_arg(1)
	length = ulong_arg(2)
	flags = int_arg(3)
	flags_str = _msync_flag_str(flags)
%)

@define _SYSCALL_MSYNC_REGARGS_STORE
%(
        if (@probewrite(start))
          set_ulong_arg(1, start)

	if (@probewrite(length))
	  set_ulong_arg(2, length)

	if (@probewrite(flags))
	  set_int_arg(3, flags)
%)

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

# dw_msync _____________________________________________________

probe dw_syscall.msync = kernel.function("sys_msync").call ?
{
	@_SYSCALL_MSYNC_NAME
	start = $start
	length = __ulong($len)
	flags = __int32($flags)
	flags_str = _msync_flag_str(flags)
	@_SYSCALL_MSYNC_ARGSTR
}
probe dw_syscall.msync.return = kernel.function("sys_msync").return ?
{
	@_SYSCALL_MSYNC_NAME
	@SYSC_RETVALSTR($return)
}

# nd_msync _____________________________________________________

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

probe nd1_syscall.msync = kprobe.function("sys_msync") ?
{
	@_SYSCALL_MSYNC_NAME
	asmlinkage()
	@_SYSCALL_MSYNC_REGARGS
	@_SYSCALL_MSYNC_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.msync = kprobe.function(@arch_syscall_prefix "sys_msync") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_MSYNC_NAME
	@_SYSCALL_MSYNC_REGARGS
	@_SYSCALL_MSYNC_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_MSYNC_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.msync = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_msync"), @const("__NR_compat_msync"))
	@_SYSCALL_MSYNC_NAME
	@_SYSCALL_MSYNC_REGARGS
	@_SYSCALL_MSYNC_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_MSYNC_REGARGS_STORE %)
}

probe nd_syscall.msync.return = nd1_syscall.msync.return!, nd2_syscall.msync.return!, tp_syscall.msync.return
  { }
  
probe nd1_syscall.msync.return = kprobe.function("sys_msync").return ?
{
	@_SYSCALL_MSYNC_NAME
	@SYSC_RETVALSTR(returnval())
}

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