# perf_event_open ____________________________________________
#
# SYSCALL_DEFINE5(perf_event_open,
#		struct perf_event_attr __user *, attr_uptr,
#		pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)

@define _SYSCALL_PERF_EVENT_OPEN_NAME
%(
	name = "perf_event_open"
%)

@define _SYSCALL_PERF_EVENT_OPEN_ARGSTR
%(
	argstr = sprintf("%p, %d, %d, %d, %s", attr_uaddr, pid, cpu, group_fd, flags_str)
%)

@define _SYSCALL_PERF_EVENT_OPEN_REGARGS
%(
	attr_uaddr = pointer_arg(1)
	pid = int_arg(2)
	cpu = int_arg(3)
	group_fd = int_arg(4)
	flags = ulong_arg(5)
	flags_str = _perf_event_open_flags_str(flags)
%)

@define _SYSCALL_PERF_EVENT_OPEN_REGARGS_STORE
%(
        if (@probewrite(attr_uaddr))
          set_pointer_arg(1, attr_uaddr)

	if (@probewrite(pid))
	  set_int_arg(2, pid)

	if (@probewrite(cpu))
	  set_int_arg(3, cpu)

	if (@probewrite(group_fd))
	  set_int_arg(4, group_fd)

	if (@probewrite(flags))
	  set_ulong_arg(5, flags)
%)

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

# dw_perf_event_open _____________________________________________________

probe dw_syscall.perf_event_open = kernel.function("sys_perf_event_open").call ?
{
	@_SYSCALL_PERF_EVENT_OPEN_NAME
	attr_uaddr = $attr_uptr
	pid = __int32($pid)
	cpu = __int32($cpu)
	group_fd = __int32($group_fd)
	flags = @__compat_ulong($flags)
	flags_str = _perf_event_open_flags_str(flags)
	@_SYSCALL_PERF_EVENT_OPEN_ARGSTR
}
probe dw_syscall.perf_event_open.return =
	kernel.function("sys_perf_event_open").return ?
{
	@_SYSCALL_PERF_EVENT_OPEN_NAME
	@SYSC_RETVALSTR($return)
}

# nd_perf_event_open _____________________________________________________

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

probe nd1_syscall.perf_event_open = kprobe.function("sys_perf_event_open") ?
{
	@_SYSCALL_PERF_EVENT_OPEN_NAME
	asmlinkage()
	@_SYSCALL_PERF_EVENT_OPEN_REGARGS
	@_SYSCALL_PERF_EVENT_OPEN_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.perf_event_open = kprobe.function(@arch_syscall_prefix "sys_perf_event_open") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_PERF_EVENT_OPEN_NAME
	@_SYSCALL_PERF_EVENT_OPEN_REGARGS
	@_SYSCALL_PERF_EVENT_OPEN_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PERF_EVENT_OPEN_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.perf_event_open = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_perf_event_open"), @const("__NR_compat_perf_event_open"))
	@_SYSCALL_PERF_EVENT_OPEN_NAME
	@_SYSCALL_PERF_EVENT_OPEN_REGARGS
	@_SYSCALL_PERF_EVENT_OPEN_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PERF_EVENT_OPEN_REGARGS_STORE %)
}

probe nd_syscall.perf_event_open.return = nd1_syscall.perf_event_open.return!, nd2_syscall.perf_event_open.return!, tp_syscall.perf_event_open.return
  { }
  
probe nd1_syscall.perf_event_open.return =
	kprobe.function("sys_perf_event_open").return ?
{
	@_SYSCALL_PERF_EVENT_OPEN_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.perf_event_open.return = kprobe.function(@arch_syscall_prefix "sys_perf_event_open").return ?
{
	@_SYSCALL_PERF_EVENT_OPEN_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.perf_event_open.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_perf_event_open"), @const("__NR_compat_perf_event_open"))
	@_SYSCALL_PERF_EVENT_OPEN_NAME
	@SYSC_RETVALSTR($ret)
}
