# execve _____________________________________________________
# NB: kprocess.exec[_complete] is aliased to syscall.execve[.return]
%( kernel_v >= "3.7" %?
# In kernels >= 3.7, sys_execve() has been moved to generic code, so we
# can use it with confidence. For kernels < 3.7, execve support is in
# arch-specific tapset code.
#
# SYSCALL_DEFINE3(execve,
#		const char __user *, filename,
#		const char __user *const __user *, argv,
#		const char __user *const __user *, envp)

@define _SYSCALL_EXECVE_NAME
%(
	name = "execve"
%)

@define _SYSCALL_EXECVE_ARGSTR
%(
	argstr = sprintf("%s, %s, %s", filename, args, env_str)
%)

probe syscall.execve = dw_syscall.execve !, nd_syscall.execve {}
probe syscall.execve.return = dw_syscall.execve.return !,
                              nd_syscall.execve.return {}

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

# dw_execve _____________________________________________________

probe dw_syscall.execve = kernel.function("sys_execve").call
{
	@_SYSCALL_EXECVE_NAME
	filename = user_string_quoted(@choose_defined($filename, $name))
	# kernel 3.0 changed the pointer's name to __argv
	__argv = @choose_defined($__argv, $argv)
	args = __get_argv(__argv, 0)
	__envp = @choose_defined($__envp, $envp)
	env_str = __get_argv(__envp, 0)
	@_SYSCALL_EXECVE_ARGSTR
}
probe dw_syscall.execve.return = kernel.function("sys_execve").return
{
	@_SYSCALL_EXECVE_NAME
	retstr = return_str(1, $return)
}

# In kernels >= 3.7, compat_sys_execve() has been moved to generic
# code, so we can use it with confidence. For kernels < 3.7,
# compat_execve support is in arch-specific tapset code.
#
# asmlinkage long compat_sys_execve(const char __user * filename,
#	const compat_uptr_t __user * argv,
#	const compat_uptr_t __user * envp)
probe dw_syscall.compat_execve = kernel.function("compat_sys_execve").call ?
{
	@_SYSCALL_EXECVE_NAME
	filename = user_string_quoted(@__pointer($filename))
	# kernel 3.0 changed the pointer's name to __argv
	__argv = @__pointer(@choose_defined($__argv, $argv))
	args = __get_compat_argv(__argv, 0)
	__envp = @__pointer(@choose_defined($__envp, $envp))
	env_str = __get_compat_argv(__envp, 0)
	@_SYSCALL_EXECVE_ARGSTR
}
probe dw_syscall.compat_execve.return =
	kernel.function("compat_sys_execve").return ?
{
	@_SYSCALL_EXECVE_NAME
	retstr = return_str(1, $return)
}

# nd_execve _____________________________________________________

probe nd_syscall.execve = kprobe.function("sys_execve")
{
	@_SYSCALL_EXECVE_NAME
	asmlinkage()
	filename = user_string_quoted(pointer_arg(1))
	args = __get_argv(pointer_arg(2), 0)
	env_str = __get_argv(pointer_arg(3), 0)
	@_SYSCALL_EXECVE_ARGSTR
}
probe nd_syscall.execve.return = kprobe.function("sys_execve").return
{
	@_SYSCALL_EXECVE_NAME
	retstr = returnstr(1)
}

# In kernels >= 3.7, compat_sys_execve() has been moved to generic
# code, so we can use it with confidence. For kernels < 3.7,
# compat_execve support is in arch-specific tapset code.
#
# asmlinkage long compat_sys_execve(const char __user * filename,
#	const compat_uptr_t __user * argv,
#	const compat_uptr_t __user * envp)
probe nd_syscall.compat_execve = kprobe.function("compat_sys_execve").call ?
{
	@_SYSCALL_EXECVE_NAME
	asmlinkage()
	filename = user_string_quoted(pointer_arg(1))
	args = __get_compat_argv(pointer_arg(2), 0)
	env_str = __get_compat_argv(pointer_arg(3), 0)
	@_SYSCALL_EXECVE_ARGSTR
}
probe nd_syscall.compat_execve.return =
	kprobe.function("compat_sys_execve").return ?
{
	@_SYSCALL_EXECVE_NAME
	retstr = returnstr(1)
}
%)
