# truncate ___________________________________________________
#
# sys_truncate(const char __user * path, unsigned long length)
# sys_truncate64(const char __user * path, loff_t length)
# COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path,
#			 compat_off_t, length)
#

@define _SYSCALL_TRUNCATE_NAME
%(
	name = "truncate"
%)

@define _SYSCALL_TRUNCATE_ARGSTR
%(
	argstr = sprintf("%s, %d", path, length)
%)

@define _SYSCALL_GATE
%(
    %( arch == "x86_64" || arch == "s390" %?
	// On x86_64 and s390x RHL6 kernels, the 32-bit compat
	// truncate syscall is hooked up directly to sys_truncate(),
	// instead of going through compat_sys_truncate().
	@__syscall_compat_gate(@const("__NR_truncate"),
			       @const("__NR_compat_truncate"))
    %:
	@__syscall_gate_compat_simple
    %)
%)

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

# dw_truncate _____________________________________________________

probe dw_syscall.truncate = __syscall.truncate ?, __syscall.truncate64 ?,
	__syscall.compat_truncate ?
{
	@_SYSCALL_TRUNCATE_NAME
	path_uaddr = @__pointer($path)
	path = user_string_quoted(path_uaddr)
	@_SYSCALL_TRUNCATE_ARGSTR
}
probe __syscall.truncate = kernel.function("sys_truncate").call ?
{
	@_SYSCALL_GATE
	length = @__compat_long($length)
}
probe __syscall.truncate64 = kernel.function("sys_truncate64").call ?
{
	length = $length
}
probe __syscall.compat_truncate = kernel.function("compat_sys_truncate").call ?
{
	length = __int32($length)
}
probe dw_syscall.truncate.return = __syscall.truncate.return ?,
                                kernel.function("sys_truncate64").return ?,
				kernel.function("compat_sys_truncate").return ?
{
	@_SYSCALL_TRUNCATE_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.truncate.return = kernel.function("sys_truncate").return ?
{
	@_SYSCALL_GATE
}

# nd_truncate _____________________________________________________

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

probe nd1_syscall.truncate = __nd1_syscall.truncate ?,
			     __nd1_syscall.truncate64 ?,
			     __nd1_syscall.compat_truncate ?
{
	@_SYSCALL_TRUNCATE_NAME
	asmlinkage()
	path_uaddr = pointer_arg(1)
	path = user_string_quoted(path_uaddr)
	@_SYSCALL_TRUNCATE_ARGSTR
}
probe __nd1_syscall.truncate = kprobe.function("sys_truncate") ?
{
	@_SYSCALL_GATE
	asmlinkage()
	length = long_arg(2)
}
probe __nd1_syscall.truncate64 = kprobe.function("sys_truncate64") ?
{
	asmlinkage()
	length = longlong_arg(2)
}
probe __nd1_syscall.compat_truncate = kprobe.function("compat_sys_truncate") ?
{
	asmlinkage()
	length = int_arg(2)
}

/* kernel 4.17+ */
probe nd2_syscall.truncate = __nd2_syscall.truncate ?,
                             __nd2_syscall.compat_truncate ?
{
	@_SYSCALL_TRUNCATE_NAME
	path_uaddr = pointer_arg(1)
	path = user_string_quoted(path_uaddr)
	@_SYSCALL_TRUNCATE_ARGSTR
}
probe __nd2_syscall.truncate = kprobe.function(@arch_syscall_prefix "sys_truncate") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	length = long_arg(2)
}
probe __nd2_syscall.compat_truncate = kprobe.function(@arch_syscall_prefix "compat_sys_truncate") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	length = int_arg(2)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.tuncate = __tp_syscall.truncate ?,
			   __tp_syscall.truncate64 ?,
			   __tp_syscall.compat_truncate ?
{
	@_SYSCALL_TRUNCATE_NAME
	path_uaddr = pointer_arg(1)
	path = user_string_quoted(path_uaddr)
	@_SYSCALL_TRUNCATE_ARGSTR
}
probe __tp_syscall.truncate = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_truncate"))
	length = long_arg(2)
}
probe __tp_syscall.truncate64 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_truncate64"))
	length = longlong_arg(2)
}
probe __tp_syscall.compat_truncate = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_truncate"))
	length = int_arg(2)
}

probe nd_syscall.truncate.return = nd1_syscall.truncate.return!, nd2_syscall.truncate.return!, tp_syscall.truncate.return
  { }

probe nd1_syscall.truncate.return = __nd1_syscall.truncate.return ?,
	kprobe.function("sys_truncate64").return ?,
	kprobe.function("compat_sys_truncate").return ?
{
	@_SYSCALL_TRUNCATE_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.truncate.return = kprobe.function("sys_truncate").return ?
{
	@_SYSCALL_GATE
}

/* kernel 4.17+ */
probe nd2_syscall.truncate.return =
	kprobe.function(@arch_syscall_prefix "sys_truncate").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_truncate").return ?
{
	@_SYSCALL_TRUNCATE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.truncate.return = __tp_syscall.truncate.return,
                                   __tp_syscall.truncate64.return
{
	__set_syscall_pt_regs($regs)
	@_SYSCALL_TRUNCATE_NAME
	@SYSC_RETVALSTR($ret)
}
probe __tp_syscall.truncate.return = kernel.trace("sys_exit")
{
	@__syscall_compat_gate(@const("__NR_truncate"), @const("__NR_compat_truncate"))
}
probe __tp_syscall.truncate64.return = kernel.trace("sys_exit")
{
	@__syscall_gate(@const("__NR_truncate64"))
}
