# semop ______________________________________________________
#
# long sys_semop (int semid,
#		struct sembuf __user *tsops,
#		unsigned nsops)
#

@define _SYSCALL_SEMOP_NAME
%(
	name = "semop"
%)

@define _SYSCALL_SEMOP_ARGSTR
%(
	argstr = sprintf("%d, %p, %u", semid, sops_uaddr, nsops)
%)

@define _SYSCALL_SEMOP_REGARGS
%(
	semid = int_arg(1)
%( systemtap_v < "2.3" %?
	tsops_uaddr = pointer_arg(2)
%)
	sops_uaddr = pointer_arg(2)
	nsops = uint_arg(3)
%)

@define _SYSCALL_SEMOP_REGARGS_STORE
%(
        if (@probewrite(semid))
          set_int_arg(1, semid)

%( systemtap_v < "2.3" %?
	if (@probewrite(tsops_uaddr))
	  set_pointer_arg(2, tsops_uaddr)
%)

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

	if (@probewrite(nsops))
	  set_uint_arg(3, nsops)
%)

@define _SYSCALL_IPC_SEMOP_REGARGS
%(
	semid = int_arg(2)
%( systemtap_v < "2.3" %?
	tsops_uaddr = pointer_arg(5)
%)
        sops_uaddr = pointer_arg(5)
        nsops = uint_arg(3)
%)

@define _SYSCALL_IPC_SEMOP_REGARGS_STORE
%(
        if (@probewrite(semid))
          set_int_arg(2, semid)

%( systemtap_v < "2.3" %?
	if (@probewrite(tsops_uaddr))
	  set_pointer_arg(5, tsops_uaddr)
%)

	if (@probewrite(sops_uaddr))
	  set_pointer_arg(5, sops_uaddr)

	if (@probewrite(nsops))
	  set_uint_arg(3, nsops)
%)

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

# dw_semop _____________________________________________________

probe dw_syscall.semop = __syscall.semop ?, __syscall.compat_semop ?
{
	@_SYSCALL_SEMOP_NAME
	@_SYSCALL_SEMOP_ARGSTR
}
probe __syscall.semop = kernel.function("sys_semop").call ?
{
	semid = __int32($semid)
%( systemtap_v < "2.3" %?
	tsops_uaddr = $tsops
%)
	sops_uaddr = $tsops
	nsops = __uint32($nsops)
}
probe __syscall.compat_semop = kernel.function("compat_sys_ipc").call ?,
        kernel.function("sys32_ipc").call ?
{
        if ($call != @const("SEMOP")) next;
        semid = __int32($first)
%( systemtap_v < "2.3" %?
	tsops_uaddr = $ptr
%)
        sops_uaddr = $ptr
        nsops = __uint32($second)
}
probe dw_syscall.semop.return = kernel.function("sys_semop").return ?,
	__syscall.compat_semop.return ?
{
	@_SYSCALL_SEMOP_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.compat_semop.return =
	kernel.function("compat_sys_ipc").return ?,
	kernel.function("sys32_ipc").return ?
{
	if (@entry($call) != @const("SEMOP")) next;
}

# nd_semop _____________________________________________________

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

probe nd1_syscall.semop = __nd1_syscall.semop ?, __nd1_syscall.compat_semop ?
{
	@_SYSCALL_SEMOP_NAME
	@_SYSCALL_SEMOP_ARGSTR
}
probe __nd1_syscall.semop = kprobe.function("sys_semop") ?
{
	asmlinkage()
	@_SYSCALL_SEMOP_REGARGS
}
probe __nd1_syscall.compat_semop = kprobe.function("compat_sys_ipc") ?,
        kprobe.function("sys32_ipc") ?
{
	if (int_arg(1) != @const("SEMOP")) next;
	@_SYSCALL_IPC_SEMOP_REGARGS
}

/* kernel 4.17+ */
probe nd2_syscall.semop = __nd2_syscall.semop ?, __nd2_syscall.compat_semop ?
{
	@_SYSCALL_SEMOP_NAME
	@_SYSCALL_SEMOP_ARGSTR
}
probe __nd2_syscall.semop = kprobe.function(@arch_syscall_prefix "sys_semop") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SEMOP_REGARGS
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SEMOP_REGARGS_STORE %)
}
probe __nd2_syscall.compat_semop = kprobe.function(@arch_syscall_prefix "compat_sys_ipc") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SEMOP")) next;
	@_SYSCALL_IPC_SEMOP_REGARGS
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_IPC_SEMOP_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.semop = __tp_syscall.semop ?, __tp_syscall.compat_semop ?
{
	@_SYSCALL_SEMOP_NAME
	@_SYSCALL_SEMOP_ARGSTR
}
probe __tp_syscall.semop = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_semop"))
	@_SYSCALL_SEMOP_REGARGS
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SEMOP_REGARGS_STORE %)
}
probe __tp_syscall.compat_semop = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_ipc"))
	if (int_arg(1) != @const("SEMOP")) next;
	@_SYSCALL_IPC_SEMOP_REGARGS
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_IPC_SEMOP_REGARGS_STORE %)
}

probe nd_syscall.semop.return = nd1_syscall.semop.return!, nd2_syscall.semop.return!, tp_syscall.semop.return
  { }
  
probe nd1_syscall.semop.return = kprobe.function("sys_semop").return ?,
	__nd1_syscall.compat_semop.return ?
{
	@_SYSCALL_SEMOP_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.compat_semop.return =
	kprobe.function("compat_sys_ipc").return ?,
	kprobe.function("sys32_ipc").return ?
{
	if (@entry(int_arg(1)) != @const("SEMOP")) next;
}

/* kernel 4.17+ */
probe nd2_syscall.semop.return = kprobe.function(@arch_syscall_prefix "sys_semop").return ?,
	__nd2_syscall.compat_semop.return ?
{
	@_SYSCALL_SEMOP_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd2_syscall.compat_semop.return =
	kprobe.function(@arch_syscall_prefix "compat_sys_ipc").return ?
{
	__set_syscall_pt_regs(@entry(pointer_arg(1)))
	if (int_arg(1) != @const("SEMOP")) next;
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.semop.return = __tp_syscall.semop.return, __tp_syscall.compat_semop.return
{
	@_SYSCALL_SEMOP_NAME
	@SYSC_RETVALSTR($ret)
}
probe __tp_syscall.semop.return = kernel.trace("sys_exit")
{
	@__syscall_gate(@const("__NR_semop"))
}
probe __tp_syscall.compat_semop.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_ipc"))
	if (int_arg(1) != @const("SEMOP")) next;
}
