# getsockopt _________________________________________________
# long sys_getsockopt(int fd,
#                int level,
#                int optname,
#                char __user *optval,
#                int __user *optlen)
#

@define _SYSCALL_GETSOCKOPT_NAME
%(
	name = "getsockopt"
%)

@define _SYSCALL_GETSOCKOPT_ARGSTR
%(
	argstr = sprintf("%d, %s, %s, %p, %p", fd, _sockopt_level_str(level),
	                 _sockopt_optname_str(optname), optval_uaddr, optlen_uaddr)
%)

@define _SYSCALL_GETSOCKOPT_REGARGS
%(
	fd = int_arg(1)
	level = int_arg(2)
	optname = int_arg(3)
	optval_uaddr = pointer_arg(4)
	optlen_uaddr = pointer_arg(5)
%)

@define _SYSCALL_SOCKETCALL_GETSOCKOPT_REGARGS
%(
	__args = &@ulong_cast(pointer_arg(2))
	fd = __int32(user_ulong(&@ulong_cast(__args)[0]))
	level = __int32(user_ulong(&@ulong_cast(__args)[1]))
	optname = __int32(user_ulong(&@ulong_cast(__args)[2]))
	optval_uaddr = user_ulong(&@ulong_cast(__args)[3])
	optlen_uaddr = user_ulong(&@ulong_cast(__args)[4])
%)

@define _SYSCALL_COMPAT_SOCKETCALL_GETSOCKOPT_REGARGS
%(
	__args = &@uint_cast(pointer_arg(2))
	fd = user_int(&@uint_cast(__args)[0])
	level = user_int(&@uint_cast(__args)[1])
	optname = user_int(&@uint_cast(__args)[2])
	optval_uaddr = user_uint32(&@uint_cast(__args)[3])
	optlen_uaddr = user_uint32(&@uint_cast(__args)[4])
%)

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

# dw_getsockopt _____________________________________________________

probe dw_syscall.getsockopt = __syscall.getsockopt ?,
	__syscall.socketcall.getsockopt ?,
	__syscall.compat_socketcall.getsockopt ?
{
	@_SYSCALL_GETSOCKOPT_NAME
	level_str = _sockopt_level_str(level)
	optname_str = _sockopt_optname_str(optname)
	@_SYSCALL_GETSOCKOPT_ARGSTR
}
probe __syscall.getsockopt = kernel.function("sys_getsockopt").call ?
{
	@__syscall_gate(@const("__NR_getsockopt"))
	fd = __int32($fd)
	level = __int32($level)
	optname = __int32($optname)
	optval_uaddr = $optval
	optlen_uaddr = $optlen
}
probe __syscall.socketcall.getsockopt = kernel.function("sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_GETSOCKOPT")) next;
	fd = __int32(user_ulong(&@ulong_cast($args)[0]))
	level = __int32(user_ulong(&@ulong_cast($args)[1]))
	optname = __int32(user_ulong(&@ulong_cast($args)[2]))
	optval_uaddr = user_ulong(&@ulong_cast($args)[3])
	optlen_uaddr = user_ulong(&@ulong_cast($args)[4])
}
probe __syscall.compat_socketcall.getsockopt =
	kernel.function("compat_sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_GETSOCKOPT")) next;
	fd = user_int(&@uint_cast($args)[0])
	level = user_int(&@uint_cast($args)[1])
	optname = user_int(&@uint_cast($args)[2])
	optval_uaddr = user_uint32(&@uint_cast($args)[3])
	optlen_uaddr = user_uint32(&@uint_cast($args)[4])
}
probe dw_syscall.getsockopt.return = __syscall.getsockopt.return ?,
      __syscall.socketcall.getsockopt.return ?
{
	@_SYSCALL_GETSOCKOPT_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.getsockopt.return = kernel.function("sys_getsockopt").return ?
{
	@__syscall_gate(@const("__NR_getsockopt"))
}
probe __syscall.socketcall.getsockopt.return =
	kernel.function("sys_socketcall").return ?,
	kernel.function("compat_sys_socketcall").return ?
{
	if (__int32(@entry($call)) != @const("SYS_GETSOCKOPT")) next;
}

# nd_getsockopt _____________________________________________________

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

probe nd1_syscall.getsockopt = __nd1_syscall.getsockopt ?,
	__nd1_syscall.socketcall.getsockopt ?,
	__nd1_syscall.compat_socketcall.getsockopt ?
{
	@_SYSCALL_GETSOCKOPT_NAME
	level_str = _sockopt_level_str(level)
	optname_str = _sockopt_optname_str(optname)
	@_SYSCALL_GETSOCKOPT_ARGSTR
}
probe __nd1_syscall.getsockopt = kprobe.function("sys_getsockopt") ?
{
	asmlinkage()
	@__syscall_gate(@const("__NR_getsockopt"))
	@_SYSCALL_GETSOCKOPT_REGARGS
}
probe __nd1_syscall.socketcall.getsockopt =
	kprobe.function("sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_GETSOCKOPT")) next;
	@_SYSCALL_SOCKETCALL_GETSOCKOPT_REGARGS
}
probe __nd1_syscall.compat_socketcall.getsockopt =
	kprobe.function("compat_sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_GETSOCKOPT")) next;
	@_SYSCALL_COMPAT_SOCKETCALL_GETSOCKOPT_REGARGS
}

/* kernel 4.17+ */
probe nd2_syscall.getsockopt = __nd2_syscall.getsockopt ?,
	__nd2_syscall.socketcall.getsockopt ?,
	__nd2_syscall.compat_socketcall.getsockopt ?
{
	@_SYSCALL_GETSOCKOPT_NAME
	level_str = _sockopt_level_str(level)
	optname_str = _sockopt_optname_str(optname)
	@_SYSCALL_GETSOCKOPT_ARGSTR
}
probe __nd2_syscall.getsockopt = kprobe.function(@arch_syscall_prefix "sys_getsockopt") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_GETSOCKOPT_REGARGS
}
probe __nd2_syscall.socketcall.getsockopt = kprobe.function(@arch_syscall_prefix "sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_GETSOCKOPT")) next;
	@_SYSCALL_SOCKETCALL_GETSOCKOPT_REGARGS
}
probe __nd2_syscall.compat_socketcall.getsockopt =
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_GETSOCKOPT")) next;
	@_SYSCALL_COMPAT_SOCKETCALL_GETSOCKOPT_REGARGS
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.getsockopt = __tp_syscall.getsockopt ?,
	__tp_syscall.socketcall.getsockopt ?
{
	@_SYSCALL_GETSOCKOPT_NAME
	level_str = _sockopt_level_str(level)
	optname_str = _sockopt_optname_str(optname)
	@_SYSCALL_GETSOCKOPT_ARGSTR
}
probe __tp_syscall.getsockopt = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_getsockopt"))
	@_SYSCALL_GETSOCKOPT_REGARGS
}
probe __tp_syscall.socketcall.getsockopt = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_socketcall"), @const("__NR_compat_socketcall"))
	if (int_arg(1) != @const("SYS_GETSOCKOPT")) next;
	if (@__compat_task) {
		@_SYSCALL_COMPAT_SOCKETCALL_GETSOCKOPT_REGARGS
	}
	else {
		@_SYSCALL_SOCKETCALL_GETSOCKOPT_REGARGS
	}
}

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

probe nd1_syscall.getsockopt.return = __nd1_syscall.getsockopt.return ?,
      __nd1_syscall.socketcall.getsockopt.return ?
{
	@_SYSCALL_GETSOCKOPT_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.getsockopt.return =
	kprobe.function("sys_getsockopt").return ?
{
	@__syscall_gate(@const("__NR_getsockopt"))
}
probe __nd1_syscall.socketcall.getsockopt.return =
	kprobe.function("sys_socketcall").return ?,
	kprobe.function("compat_sys_socketcall").return ?
{
	if (@entry(__asmlinkage_int_arg(1)) != @const("SYS_GETSOCKOPT")) next;
}

/* kernel 4.17+ */
probe nd2_syscall.getsockopt.return =
	kprobe.function(@arch_syscall_prefix "sys_getsockopt").return ?,
	__nd2_syscall.socketcall.getsockopt.return ?
{
	@_SYSCALL_GETSOCKOPT_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd2_syscall.socketcall.getsockopt.return =
	kprobe.function(@arch_syscall_prefix "sys_socketcall").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").return ?
{
	__set_syscall_pt_regs(@entry(pointer_arg(1)))
	if (int_arg(1) != @const("SYS_GETSOCKOPT")) next;
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.getsockopt.return = __tp_syscall.getsockopt.return ?,
	__tp_syscall.socketcall.getsockopt.return ?
{
	@_SYSCALL_GETSOCKOPT_NAME
	@SYSC_RETVALSTR($ret)
}
probe __tp_syscall.getsockopt.return = kernel.trace("sys_exit")
{
	@__syscall_gate(@const("__NR_getsockopt"))
}
probe __tp_syscall.socketcall.getsockopt.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_socketcall"), @const("__NR_compat_socketcall"))
	if (int_arg(1) != @const("SYS_GETSOCKOPT")) next;
}
