# 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)
%)

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
	retstr = return_str(1, $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 = __nd_syscall.sendmsg ?,
	__nd_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 __nd_syscall.sendmsg = kprobe.function("sys_sendmsg") ?
{
	@__syscall_gate(@const("__NR_sendmsg"))
	asmlinkage()
	s = int_arg(1)
	msg_uaddr = pointer_arg(2)
	flags = uint_arg(3)
}
probe __nd_syscall.socketcall.sendmsg = kprobe.function("sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	__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]))
}
probe nd_syscall.sendmsg.return = __nd_syscall.sendmsg.return ?,
	__nd_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
	retstr = returnstr(1)
}
probe __nd_syscall.sendmsg.return = kprobe.function("sys_sendmsg").return ?
{
	@__syscall_gate(@const("__NR_sendmsg"))
}
probe __nd_syscall.socketcall.sendmsg.return =
	kprobe.function("sys_socketcall").return ?
{
	if (@entry(__asmlinkage_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
	retstr = return_str(1, $return)
}

# nd_compat_sys_sendmsg _____________________________________________________

probe nd_syscall.compat_sys_sendmsg =
	kprobe.function("compat_sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	__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)
	@_SYSCALL_SENDMSG_ARGSTR
}
probe nd_syscall.compat_sys_sendmsg.return =
	kprobe.function("compat_sys_socketcall").return ?
{
	if (@entry(__asmlinkage_int_arg(1)) != @const("SYS_SENDMSG")) next;
	@_SYSCALL_SENDMSG_NAME
	retstr = returnstr(1)
}
