# delete_module ______________________________________________
# long sys_delete_module(const char __user *name_user, unsigned int flags)

@define _SYSCALL_DELETE_MODULE_NAME
%(
	name = "delete_module"
%)

@define _SYSCALL_DELETE_MODULE_ARGSTR
%(
	argstr = sprintf("%s, %s", name_user, _module_flags_str(flags))
%)

@define _SYSCALL_DELETE_MODULE_REGARGS
%(
	name_user = user_string_quoted(pointer_arg(1))
	name_user_unquoted = user_string_nofault(pointer_arg(1))
	flags = uint_arg(2)
	flags_str = _module_flags_str(flags)
%)

@define _SYSCALL_DELETE_MODULE_REGARGS_STORE
%(
        if (@probewrite(name_user_unquoted))
          set_user_string_arg(pointer_arg(1), name_user_unquoted)

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

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

# dw_delete_module _____________________________________________________

probe dw_syscall.delete_module = kernel.function("sys_delete_module").call ?
{
	@_SYSCALL_DELETE_MODULE_NAME
	name_user = user_string_quoted($name_user)
	flags = __uint32($flags)
	flags_str = _module_flags_str(__uint32($flags))
	@_SYSCALL_DELETE_MODULE_ARGSTR
}
probe dw_syscall.delete_module.return =
	kernel.function("sys_delete_module").return ?
{
	@_SYSCALL_DELETE_MODULE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_delete_module _____________________________________________________

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

probe nd1_syscall.delete_module = kprobe.function("sys_delete_module") ?
{
	@_SYSCALL_DELETE_MODULE_NAME
	asmlinkage()
	@_SYSCALL_DELETE_MODULE_REGARGS
	@_SYSCALL_DELETE_MODULE_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.delete_module = kprobe.function(@arch_syscall_prefix "sys_delete_module") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_DELETE_MODULE_NAME
	@_SYSCALL_DELETE_MODULE_REGARGS
	@_SYSCALL_DELETE_MODULE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_DELETE_MODULE_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.delete_module = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_delete_module"), @const("__NR_compat_delete_module"))
	@_SYSCALL_DELETE_MODULE_NAME
	@_SYSCALL_DELETE_MODULE_REGARGS
	@_SYSCALL_DELETE_MODULE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_DELETE_MODULE_REGARGS_STORE %)
}

probe nd_syscall.delete_module.return = nd1_syscall.delete_module.return!, nd2_syscall.delete_module.return!, tp_syscall.delete_module.return
  { }
  
probe nd1_syscall.delete_module.return =
	kprobe.function("sys_delete_module").return ?
{
	@_SYSCALL_DELETE_MODULE_NAME
	@SYSC_RETVALSTR(returnval())
}

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