%( kernel_v < "3.7" %?
# execve _____________________________________________________
#
# In kernels < 3.7, sys_execve() was in arch-specific code (and had
# varying arguments). It was just a wrapper around generic
# do_execve(), but the wrapper could error out before calling
# do_execve(). So, we'll have to handle it in arch-specific tapset
# code to catch all calls.
#
#   asmlinkage int sys_execve(struct pt_regs regs)

@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 {}

# dw_execve _____________________________________________________

probe dw_syscall.execve = kernel.function("sys_execve").call
{
	@_SYSCALL_EXECVE_NAME
	filename = user_string_quoted(@choose_defined($regs->bx, $regs->ebx))
	args = __get_argv(@choose_defined($regs->cx, $regs->ecx), 0)
	env_str = __get_argv(@choose_defined($regs->dx, $regs->edx), 0)
	@_SYSCALL_EXECVE_ARGSTR
}
probe dw_syscall.execve.return = kernel.function("sys_execve").return
{
	@_SYSCALL_EXECVE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_execve _____________________________________________________

probe nd_syscall.execve = kprobe.function("sys_execve")
{
	@_SYSCALL_EXECVE_NAME
%( kernel_v < "2.6.30" %?
	asmlinkage()
	# Notice here that a pointer to a pt_regs struct isn't passed,
	# a pt_regs struct itself is passed.
	filename = user_string_quoted(pointer_arg(1))
	args = __get_argv(pointer_arg(2), 0)
	env_str = __get_argv(pointer_arg(3), 0)
%:
	__regs = &@cast(ulong_arg(1), "pt_regs", "kernel<asm/ptrace.h>")
	filename = user_string_quoted(__regs->bx)
	args = __get_argv(__regs->cx, 0)
	env_str = __get_argv(__regs->dx, 0)
%)
	@_SYSCALL_EXECVE_ARGSTR
}
probe nd_syscall.execve.return = kprobe.function("sys_execve").return
{
	@_SYSCALL_EXECVE_NAME
	@SYSC_RETVALSTR(returnval())
}
%)
