#!/bin/sh
#
# Check -i option.
#
# Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
# Copyright (c) 2015-2018 The strace developers.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

. "./init.sh"

check_prog sed

run_prog > /dev/null

args="-if $args"
$STRACE $args 2> "$LOG" ||
	dump_log_and_fail_with "$STRACE $args failed"

len="$(sed -r -n 's/^\[[[:xdigit:]]+\] write\(-1, NULL, ([[:digit:]]{1,2})\)[[:space:]]+= -1 .*/\1/p' "$LOG")" &&
[ -n "$len" ] &&
pid="$(sed -r -n 's/^\[[[:xdigit:]]{'"$len"'}\] --- SIGCHLD \{si_signo=SIGCHLD, si_code=CLD_(KILLED|DUMPED), si_pid=([[:digit:]]+), .*/\2/p' "$LOG")" &&
[ -n "$pid" ] &&
ip="$(sed -r -n 's/^\[pid +'"$pid"'\] \[([[:xdigit:]]{'"$len"'})] --- SIGSEGV \{.*\} ---$/\1/p' "$LOG")" &&
[ -n "$ip" ] &&
addr="$(echo "$ip" |sed -r 's/^0+//')" &&
[ -n "$addr" ] ||
	dump_log_and_fail_with

cat > "$EXP" << __EOF__
\\[[[:xdigit:]]{$len}\\] munmap\\(0x[[:xdigit:]]+, 0\\) += -1 .*
\\[pid +$pid\\] \\[$ip\\] --- SIGSEGV \\{si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x$addr\\} ---
\\[pid +$pid\\] \\[\\?{$len}\\] \\+\\+\\+ killed by SIGSEGV( \\(core dumped\\))? \\+\\+\\+
\\[\\?{$len}\\] \\+\\+\\+ exited with 0 \\+\\+\\+
__EOF__

match_grep "$LOG" "$EXP"
