# set_tid_address ____________________________________________
#
# asmlinkage long
# sys_set_tid_address(int __user *tidptr)
#

@define _SYSCALL_SET_TID_ADDRESS_NAME
%(
	name = "set_tid_address"
%)

@define _SYSCALL_SET_TID_ADDRESS_ARGSTR
%(
	argstr = sprintf("%p", tidptr_uaddr)
%)

@define _SYSCALL_SET_TID_ADDRESS_REGARGS
%(
	tidptr_uaddr = pointer_arg(1)
%)

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

# dw_set_tid_address _____________________________________________________

probe dw_syscall.set_tid_address = kernel.function("sys_set_tid_address").call
{
	@_SYSCALL_SET_TID_ADDRESS_NAME
	tidptr_uaddr = $tidptr
	@_SYSCALL_SET_TID_ADDRESS_ARGSTR
}
probe dw_syscall.set_tid_address.return = kernel.function("sys_set_tid_address").return
{
	@_SYSCALL_SET_TID_ADDRESS_NAME
	@SYSC_RETVALSTR($return)
}

# nd_set_tid_address _____________________________________________________

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

probe nd1_syscall.set_tid_address = kprobe.function("sys_set_tid_address") ?
{
	@_SYSCALL_SET_TID_ADDRESS_NAME
	asmlinkage()
	@_SYSCALL_SET_TID_ADDRESS_REGARGS
	@_SYSCALL_SET_TID_ADDRESS_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.set_tid_address = kprobe.function(@arch_syscall_prefix "sys_set_tid_address") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SET_TID_ADDRESS_NAME
	@_SYSCALL_SET_TID_ADDRESS_REGARGS
	@_SYSCALL_SET_TID_ADDRESS_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.set_tid_address = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_set_tid_address"), @const("__NR_compat_set_tid_address"))
	@_SYSCALL_SET_TID_ADDRESS_NAME
	@_SYSCALL_SET_TID_ADDRESS_REGARGS
	@_SYSCALL_SET_TID_ADDRESS_ARGSTR
}

probe nd_syscall.set_tid_address.return = nd1_syscall.set_tid_address.return!, nd2_syscall.set_tid_address.return!, tp_syscall.set_tid_address.return
  { }
  
probe nd1_syscall.set_tid_address.return = kprobe.function("sys_set_tid_address").return ?
{
	@_SYSCALL_SET_TID_ADDRESS_NAME
	@SYSC_RETVALSTR(returnval())
}

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