# signal _____________________________________________________
# unsigned long sys_signal(int sig, __sighandler_t handler)
#

@define _SYSCALL_SIGNAL_NAME
%(
	name = "signal"
%)

@define _SYSCALL_SIGNAL_ARGSTR
%(
	argstr = sprintf("%s, %s", sig_str, handler_str)
%)

@define _SYSCALL_SIGNAL_REGARGS
%(
	sig = int_arg(1)
	sig_str = _signal_name(sig)
	handler =  pointer_arg(2)
	handler_str = _sighandler_str(handler)
%)

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

# dw_signal _____________________________________________________

probe dw_syscall.signal = kernel.function("sys_signal").call ?
{
	@_SYSCALL_SIGNAL_NAME
	sig = __int32($sig)
	sig_str = _signal_name(sig)
	handler = $handler
	handler_str = _sighandler_str(handler)
	@_SYSCALL_SIGNAL_ARGSTR
}
probe dw_syscall.signal.return = kernel.function("sys_signal").return ?
{
	@_SYSCALL_SIGNAL_NAME
	@SYSC_RETVALSTR($return)
}

# nd_signal _____________________________________________________

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

probe nd1_syscall.signal = kprobe.function("sys_signal") ?
{
	@_SYSCALL_SIGNAL_NAME
	asmlinkage()
	@_SYSCALL_SIGNAL_REGARGS
	@_SYSCALL_SIGNAL_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.signal = kprobe.function(@arch_syscall_prefix "sys_signal") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SIGNAL_NAME
	@_SYSCALL_SIGNAL_REGARGS
	@_SYSCALL_SIGNAL_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.signal = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_signal"), @const("__NR_compat_signal"))
	@_SYSCALL_SIGNAL_NAME
	@_SYSCALL_SIGNAL_REGARGS
	@_SYSCALL_SIGNAL_ARGSTR
}

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

probe nd1_syscall.signal.return = kprobe.function("sys_signal").return ?
{
	@_SYSCALL_SIGNAL_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.signal.return = kprobe.function(@arch_syscall_prefix "sys_signal").return ?
{
	@_SYSCALL_SIGNAL_NAME
	@SYSC_RETVALSTR(returnval())
}

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