# uselib _____________________________________________________
#
# asmlinkage long
# sys_uselib(const char __user * library)
#

@define _SYSCALL_USELIB_NAME
%(
	name = "uselib"
%)

@define _SYSCALL_USELIB_ARGSTR
%(
	argstr = library
%)

@define _SYSCALL_USELIB_REGARGS
%(
	library_uaddr = pointer_arg(1)
	library = user_string_quoted(library_uaddr)
	library_unquoted = user_string_nofault(pointer_arg(1))
%)

@define _SYSCALL_USELIB_REGARGS_STORE
%(
        if (@probewrite(library_uaddr))
          set_pointer_arg(1, library_uaddr)

	if (@probewrite(library_unquoted))
	  set_user_string_arg(pointer_arg(1), library_unquoted)
%)

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

# dw_uselib _____________________________________________________

probe dw_syscall.uselib = kernel.function("sys_uselib").call ?
{
	@_SYSCALL_USELIB_NAME
	library_uaddr = $library
	library = user_string_quoted($library)
	@_SYSCALL_USELIB_ARGSTR
}
probe dw_syscall.uselib.return = kernel.function("sys_uselib").return ?
{
	@_SYSCALL_USELIB_NAME
	@SYSC_RETVALSTR($return)
}

# nd_uselib _____________________________________________________

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

probe nd1_syscall.uselib = kprobe.function("sys_uselib") ?
{
	@_SYSCALL_USELIB_NAME
	asmlinkage()
	@_SYSCALL_USELIB_REGARGS
	@_SYSCALL_USELIB_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.uselib = kprobe.function(@arch_syscall_prefix "sys_uselib") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_USELIB_NAME
	@_SYSCALL_USELIB_REGARGS
	@_SYSCALL_USELIB_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_USELIB_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.uselib = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_uselib"), @const("__NR_compat_uselib"))
	@_SYSCALL_USELIB_NAME
	@_SYSCALL_USELIB_REGARGS
	@_SYSCALL_USELIB_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_USELIB_REGARGS_STORE %)
}

probe nd_syscall.uselib.return = nd1_syscall.uselib.return!, nd2_syscall.uselib.return!, tp_syscall.uselib.return
  { }

probe nd1_syscall.uselib.return = kprobe.function("sys_uselib").return ?
{
	@_SYSCALL_USELIB_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.uselib.return = kprobe.function(@arch_syscall_prefix "sys_uselib").return ?
{
	@_SYSCALL_USELIB_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.uselib.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_uselib"), @const("__NR_compat_uselib"))
	@_SYSCALL_USELIB_NAME
	@SYSC_RETVALSTR($ret)
}
