# getrusage __________________________________________________
# long sys_getrusage(int who, struct rusage __user *ru)
# COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *,
#			 ru)
#

@define _SYSCALL_GETRUSAGE_NAME
%(
	name = "getrusage"
%)

@define _SYSCALL_GETRUSAGE_ARGSTR
%(
	argstr = sprintf("%s, %p", who_str, usage_uaddr)
%)

@define _SYSCALL_GETRUSAGE_REGARGS
%(
	who = int_arg(1)
	who_str = _rusage_who_str(who)
	usage_uaddr = pointer_arg(2)
%)

@define _SYSCALL_GETRUSAGE_REGARGS_STORE
%(
        if (@probewrite(who))
          set_int_arg(1, who)

	if (@probewrite(usage_uaddr))
	  set_pointer_arg(2, usage_uaddr)
%)

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

# dw_getrusage _____________________________________________________

probe dw_syscall.getrusage = __syscall.getrusage,
	kernel.function("compat_sys_getrusage").call ?
{
	@_SYSCALL_GETRUSAGE_NAME
	who = __int32($who)
	who_str = _rusage_who_str(who)
	usage_uaddr = @__pointer($ru)
	@_SYSCALL_GETRUSAGE_ARGSTR
}
probe __syscall.getrusage = kernel.function("sys_getrusage").call
{
	@__syscall_gate(@const("__NR_getrusage"))
}
probe dw_syscall.getrusage.return = __syscall.getrusage.return,
	kernel.function("compat_sys_getrusage").return ?
{
	@_SYSCALL_GETRUSAGE_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.getrusage.return = kernel.function("sys_getrusage").return
{
	@__syscall_gate(@const("__NR_getrusage"))
}

# nd_getrusage _____________________________________________________

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

probe nd1_syscall.getrusage = __nd1_syscall.getrusage,
	kprobe.function("compat_sys_getrusage").call ?
{
	@_SYSCALL_GETRUSAGE_NAME
	asmlinkage()
	@_SYSCALL_GETRUSAGE_REGARGS
	@_SYSCALL_GETRUSAGE_ARGSTR
}
probe __nd1_syscall.getrusage = kprobe.function("sys_getrusage").call
{
	@__syscall_gate(@const("__NR_getrusage"))
}

/* kernel 4.17+ */
probe nd2_syscall.getrusage = kprobe.function(@arch_syscall_prefix "sys_getrusage") ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_getrusage") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_GETRUSAGE_NAME
	@_SYSCALL_GETRUSAGE_REGARGS
	@_SYSCALL_GETRUSAGE_ARGSTR
},
{
        @_SYSCALL_GETRUSAGE_REGARGS_STORE
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.getrusage = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_getrusage"), @const("__NR_compat_getrusage"))
	@_SYSCALL_GETRUSAGE_NAME
	@_SYSCALL_GETRUSAGE_REGARGS
	@_SYSCALL_GETRUSAGE_ARGSTR
},
{
        @_SYSCALL_GETRUSAGE_REGARGS_STORE
}

probe nd_syscall.getrusage.return = nd1_syscall.getrusage.return!, nd2_syscall.getrusage.return!, tp_syscall.getrusage.return
  { }
  
probe nd1_syscall.getrusage.return = __nd1_syscall.getrusage.return,
	kprobe.function("compat_sys_getrusage").return ?
{
	@_SYSCALL_GETRUSAGE_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.getrusage.return = kprobe.function("sys_getrusage").return
{
	@__syscall_gate(@const("__NR_getrusage"))
}

/* kernel 4.17+ */
probe nd2_syscall.getrusage.return = kprobe.function(@arch_syscall_prefix "sys_getrusage").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_getrusage").return ?
{
	@_SYSCALL_GETRUSAGE_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.getrusage.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_getrusage"), @const("__NR_compat_getrusage"))
	@_SYSCALL_GETRUSAGE_NAME
	@SYSC_RETVALSTR($ret)
}
