# sendmsg ____________________________________________________
#
# long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
#

@define _SYSCALL_SENDMSG_NAME
%(
	name = "sendmsg"
%)

@define _SYSCALL_SENDMSG_ARGSTR
%(
	argstr = sprintf("%d, %p, %s", s, msg_uaddr, flags_str)
%)

@define _SYSCALL_SENDMSG_REGARGS
%(
	s = int_arg(1)
	msg_uaddr = pointer_arg(2)
	flags = uint_arg(3)
%)

@define _SYSCALL_SENDMSG_REGARGS_STORE
%(
        if (@probewrite(s))
          set_int_arg(1, s)

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

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

@define _SYSCALL_SOCKETCALL_SENDMSG_REGARGS
%(
	__args = &@ulong_cast(pointer_arg(2))
	s = __int32(user_ulong(&@ulong_cast(__args)[0]))
	msg_uaddr = user_ulong(&@ulong_cast(__args)[1])
	flags = __uint32(user_ulong(&@ulong_cast(__args)[2]))
%)

@define _SYSCALL_COMPAT_SOCKETCALL_SENDMSG_REGARGS
%(
	__args = &@uint_cast(pointer_arg(2))
	s = user_int(&@uint_cast(__args)[0])
	msg_uaddr = user_uint32(&@uint_cast(__args)[1])
	flags = user_uint32(&@uint_cast(__args)[2])
	flags_str = _msg_flags_str(flags)
%)

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

# dw_sendmsg _____________________________________________________

probe dw_syscall.sendmsg = __syscall.sendmsg ?, __syscall.socketcall.sendmsg ?
{
	# Avoid probe hits from compat_sys_socketcall() calling
	# compat_sys_sendmsg(), which sometimes calls
	# sys_sendmsg(). We could call __syscall_gate2() here with
	# NR_sendmsg and NR_socketcall, but all we really need to
	# check is that we're not in a compat task.
	@__syscall_gate_compat_simple
	@_SYSCALL_SENDMSG_NAME
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_SENDMSG_ARGSTR
}
probe __syscall.sendmsg = kernel.function("sys_sendmsg").call ?
{
	@__syscall_gate(@const("__NR_sendmsg"))
	s = __int32($fd)
	msg_uaddr = $msg
	flags = __uint32($flags)
}
probe __syscall.socketcall.sendmsg = kernel.function("sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_SENDMSG")) next;
	s = __int32(user_ulong(&@ulong_cast($args)[0]))
	msg_uaddr = user_ulong(&@ulong_cast($args)[1])
	flags = __uint32(user_ulong(&@ulong_cast($args)[2]))
}
probe dw_syscall.sendmsg.return = __syscall.sendmsg.return ?,
	__syscall.socketcall.sendmsg.return ?
{
	# Avoid probe hits from compat_sys_socketcall() calling
	# compat_sys_sendmsg(), which sometimes calls
	# sys_sendmsg(). We could call __syscall_gate2() here with
	# NR_sendmsg and NR_socketcall, but all we really need to
	# check is that we're not in a compat task.
	@__syscall_gate_compat_simple
	@_SYSCALL_SENDMSG_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.sendmsg.return = kernel.function("sys_sendmsg").return ?
{
	@__syscall_gate(@const("__NR_sendmsg"))
}
probe __syscall.socketcall.sendmsg.return =
	kernel.function("sys_socketcall").return ?
{
	if (__int32(@entry($call)) != @const("SYS_SENDMSG")) next;
}

# nd_sendmsg _____________________________________________________

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

probe nd1_syscall.sendmsg = __nd1_syscall.sendmsg ?,
	__nd1_syscall.socketcall.sendmsg ?
{
	# Avoid probe hits from compat_sys_socketcall() calling
	# compat_sys_sendmsg(), which sometimes calls
	# sys_sendmsg(). We could call __syscall_gate2() here with
	# NR_sendmsg and NR_socketcall, but all we really need to
	# check is that we're not in a compat task.
	@__syscall_gate_compat_simple

	@_SYSCALL_SENDMSG_NAME
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_SENDMSG_ARGSTR
}
probe __nd1_syscall.sendmsg = kprobe.function("sys_sendmsg") ?
{
	@__syscall_gate(@const("__NR_sendmsg"))
	asmlinkage()
	@_SYSCALL_SENDMSG_REGARGS
}
probe __nd1_syscall.socketcall.sendmsg = kprobe.function("sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SOCKETCALL_SENDMSG_REGARGS
}

/* kernel 4.17+ */
probe nd2_syscall.sendmsg = __nd2_syscall.sendmsg ?,
	__nd2_syscall.socketcall.sendmsg ?
{
	# Avoid probe hits from compat_sys_socketcall() calling
	# compat_sys_sendmsg(), which sometimes calls
	# sys_sendmsg(). We could call __syscall_gate2() here with
	# NR_sendmsg and NR_socketcall, but all we really need to
	# check is that we're not in a compat task.
	@__syscall_gate_compat_simple

	@_SYSCALL_SENDMSG_NAME
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_SENDMSG_ARGSTR
}
probe __nd2_syscall.sendmsg = kprobe.function(@arch_syscall_prefix "sys_sendmsg") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SENDMSG_REGARGS
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SENDMSG_REGARGS_STORE %)
}
probe __nd2_syscall.socketcall.sendmsg = kprobe.function(@arch_syscall_prefix "sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SOCKETCALL_SENDMSG_REGARGS
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sendmsg = __tp_syscall.sendmsg ?,
	__tp_syscall.socketcall.sendmsg ?
{
	# Avoid probe hits from compat_sys_socketcall() calling
	# compat_sys_sendmsg(), which sometimes calls
	# sys_sendmsg(). We could call __syscall_gate2() here with
	# NR_sendmsg and NR_socketcall, but all we really need to
	# check is that we're not in a compat task.
	@__syscall_gate_compat_simple

	@_SYSCALL_SENDMSG_NAME
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_SENDMSG_ARGSTR
}
probe __tp_syscall.sendmsg = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_sendmsg"))
	@_SYSCALL_SENDMSG_REGARGS
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SENDMSG_REGARGS_STORE %)
}
probe __tp_syscall.socketcall.sendmsg = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_socketcall"))
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SOCKETCALL_SENDMSG_REGARGS
}

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

probe nd1_syscall.sendmsg.return = __nd1_syscall.sendmsg.return ?,
	__nd1_syscall.socketcall.sendmsg.return ?
{
	# Avoid probe hits from compat_sys_socketcall() calling
	# compat_sys_sendmsg(), which sometimes calls
	# sys_sendmsg(). We could call __syscall_gate2() here with
	# NR_sendmsg and NR_socketcall, but all we really need to
	# check is that we're not in a compat task.
	@__syscall_gate_compat_simple

	@_SYSCALL_SENDMSG_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.sendmsg.return = kprobe.function("sys_sendmsg").return ?
{
	@__syscall_gate(@const("__NR_sendmsg"))
}
probe __nd1_syscall.socketcall.sendmsg.return =
	kprobe.function("sys_socketcall").return ?
{
	if (@entry(__asmlinkage_int_arg(1)) != @const("SYS_SENDMSG")) next;
}

/* kernel 4.17+ */
probe nd2_syscall.sendmsg.return =
	kprobe.function(@arch_syscall_prefix "sys_sendmsg").return ?,
	__nd2_syscall.socketcall.sendmsg.return ?
{
	# Avoid probe hits from compat_sys_socketcall() calling
	# compat_sys_sendmsg(), which sometimes calls
	# sys_sendmsg(). We could call __syscall_gate2() here with
	# NR_sendmsg and NR_socketcall, but all we really need to
	# check is that we're not in a compat task.
	@__syscall_gate_compat_simple

	@_SYSCALL_SENDMSG_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd2_syscall.socketcall.sendmsg.return =
	kprobe.function(@arch_syscall_prefix "sys_socketcall").return ?
{
	__set_syscall_pt_regs(@entry(pointer_arg(1)))
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sendmsg.return = __tp_syscall.sendmsg.return ?,
	__tp_syscall.socketcall.sendmsg.return ?
{
	# Avoid probe hits from compat_sys_socketcall() calling
	# compat_sys_sendmsg(), which sometimes calls
	# sys_sendmsg(). We could call __syscall_gate2() here with
	# NR_sendmsg and NR_socketcall, but all we really need to
	# check is that we're not in a compat task.
	@__syscall_gate_compat_simple

	@_SYSCALL_SENDMSG_NAME
	@SYSC_RETVALSTR($ret)
}
probe __tp_syscall.sendmsg.return = kernel.trace("sys_exit")
{
	@__syscall_gate(@const("__NR_sendmsg"))
}
probe __tp_syscall.socketcall.sendmsg.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_socketcall"))
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
}

# compat_sys_sendmsg ________________________________________
#
# long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg, unsigned flags)
#
# On all tested kernels/architectures, the compat sendmsg() syscall
# goes through compat_sys_socketcall(). compat_sys_socketcall() then
# calls an inlined version of compat_sys_sendmsg() on some
# architectures (like x86_64 and ppc64). So, the only reliable thing
# to do here is just probe compat_sys_socketcall().
#
# Note that this probe should have been either called
# 'syscall.compat_sendmsg' or just merged with 'syscall.sendmsg'.
#

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

# dw_compat_sys_sendmsg _____________________________________________________

probe dw_syscall.compat_sys_sendmsg =
	kernel.function("compat_sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	s = user_int(&@uint_cast($args)[0])
	msg_uaddr = user_uint32(&@uint_cast($args)[1])
	flags = user_uint32(&@uint_cast($args)[2])
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_SENDMSG_ARGSTR
}
probe dw_syscall.compat_sys_sendmsg.return =
 	kernel.function("compat_sys_socketcall").return ?
{
	if (__int32(@entry($call)) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	@SYSC_RETVALSTR($return)
}

# nd_compat_sys_sendmsg _____________________________________________________

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

probe nd1_syscall.compat_sys_sendmsg =
	kprobe.function("compat_sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	@_SYSCALL_COMPAT_SOCKETCALL_SENDMSG_REGARGS
	@_SYSCALL_SENDMSG_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.compat_sys_sendmsg =
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	@_SYSCALL_COMPAT_SOCKETCALL_SENDMSG_REGARGS
	@_SYSCALL_SENDMSG_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_sys_sendmsg = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_socketcall"))
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	@_SYSCALL_COMPAT_SOCKETCALL_SENDMSG_REGARGS
	@_SYSCALL_SENDMSG_ARGSTR
}

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

probe nd1_syscall.compat_sys_sendmsg.return =
	kprobe.function("compat_sys_socketcall").return ?
{
	if (@entry(__asmlinkage_int_arg(1)) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.compat_sys_sendmsg.return =
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").return ?
{
	__set_syscall_pt_regs(@entry(pointer_arg(1)))
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_sys_sendmsg.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_socketcall"))
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	@SYSC_RETVALSTR($ret)
}
