# lookup_dcookie _____________________________________________
# long sys_lookup_dcookie(u64 cookie64, char __user * buf, size_t len)
#

@define _SYSCALL_LOOKUP_DCOOKIE_NAME
%(
	name = "lookup_dcookie"
%)

@define _SYSCALL_LOOKUP_DCOOKIE_ARGSTR
%(
	argstr = sprintf("%#lx, %p, %#x", cookie, buffer_uaddr, len)
%)

@define _SYSCALL_LOOKUP_DCOOKIE_REGARGS_STORE
%(
%( CONFIG_64BIT == "y" %?
        if (@probewrite(cookie))
          set_ulonglong_arg(1, cookie)

	if (@probewrite(buffer_uaddr))
	  set_pointer_arg(2, buffer_uaddr)

	if (@probewrite(len))
	  set_ulong_arg(3, len)
%:
        if (@probewrite(cookie))
          set_ulonglong_arg(1, cookie)

	if (@probewrite(buffer_uaddr))
	  set_pointer_arg(3, buffer_uaddr)

	if (@probewrite(len))
	  set_ulong_arg(4, len)
%)
%)

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

# dw_lookup_dcookie _____________________________________________________

probe dw_syscall.lookup_dcookie = kernel.function("sys_lookup_dcookie").call ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_LOOKUP_DCOOKIE_NAME
	cookie = $cookie64
	buffer_uaddr = $buf
	len = __ulong($len)
	@_SYSCALL_LOOKUP_DCOOKIE_ARGSTR
}
probe dw_syscall.lookup_dcookie.return = kernel.function("sys_lookup_dcookie").return ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_LOOKUP_DCOOKIE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_lookup_dcookie _____________________________________________________

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

probe nd1_syscall.lookup_dcookie = kprobe.function("sys_lookup_dcookie") ?
{
	@_SYSCALL_LOOKUP_DCOOKIE_NAME
	asmlinkage()
%( CONFIG_64BIT == "y" %?
	@__syscall_gate_compat_simple
	cookie = ulonglong_arg(1)
	buffer_uaddr = pointer_arg(2)
	len = ulong_arg(3)
%:
	# On a 32-bit kernel, 'long long' arguments take the space of
	# 2 arguments, so we have to adjust the following argument
	# numbers.
	cookie = ulonglong_arg(1)
	buffer_uaddr = pointer_arg(3)
	len = ulong_arg(4)
%)
	@_SYSCALL_LOOKUP_DCOOKIE_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.lookup_dcookie = kprobe.function(@arch_syscall_prefix "sys_lookup_dcookie") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_LOOKUP_DCOOKIE_NAME
	cookie = ulonglong_arg(1)
	buffer_uaddr = pointer_arg(2)
	len = ulong_arg(3)
	@_SYSCALL_LOOKUP_DCOOKIE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_LOOKUP_DCOOKIE_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.lookup_dcookie = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_lookup_dcookie"))
	@_SYSCALL_LOOKUP_DCOOKIE_NAME
%( CONFIG_64BIT == "y" %?
	cookie = ulonglong_arg(1)
	buffer_uaddr = pointer_arg(2)
	len = ulong_arg(3)
%:
	# On a 32-bit kernel, 'long long' arguments take the space of
	# 2 arguments, so we have to adjust the following argument
	# numbers.
	cookie = ulonglong_arg(1)
	buffer_uaddr = pointer_arg(3)
	len = ulong_arg(4)
%)
	@_SYSCALL_LOOKUP_DCOOKIE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_LOOKUP_DCOOKIE_REGARGS_STORE %)
}

probe nd_syscall.lookup_dcookie.return = nd1_syscall.lookup_dcookie.return!, nd2_syscall.lookup_dcookie.return!, tp_syscall.lookup_dcookie.return
  { }
  
probe nd1_syscall.lookup_dcookie.return = kprobe.function("sys_lookup_dcookie").return ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_LOOKUP_DCOOKIE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.lookup_dcookie.return = kprobe.function(@arch_syscall_prefix "sys_lookup_dcookie").return ?
{
	@_SYSCALL_LOOKUP_DCOOKIE_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.lookup_dcookie.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_lookup_dcookie"))
	@_SYSCALL_LOOKUP_DCOOKIE_NAME
	@SYSC_RETVALSTR($ret)
}
