#!/bin/bash - 
#===============================================================================
#
#          FILE: check-segv.sh
# 
#         USAGE: ./check-segv.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: YOUR NAME (), 
#  ORGANIZATION: 
#       CREATED: 10/12/2019 20:29
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error
COREFILE=./coredump
SRC=/root/libvirt
SLEEP_LIBVIRTD=5
LOG=/tmp/bitsect.log
COMMIT_HASH=$(git log -1 --format="%h")
function build(){
    CC=/usr/lib64/ccache/cc ./autogen.sh&&./configure --without-libssh --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --program-prefix= --disable-dependency-tracking --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-qemu --without-openvz --without-lxc --without-vbox --without-libxl --with-sasl --with-polkit --with-libvirtd --without-phyp --with-esx --without-hyperv --without-vmware --without-xenapi --without-vz --without-bhyve --with-interface --with-network --with-storage-fs --with-storage-lvm --with-storage-iscsi --with-storage-iscsi-direct --with-storage-scsi --with-storage-disk --with-storage-mpath --with-storage-rbd --without-storage-sheepdog --with-storage-gluster --without-storage-zfs --without-storage-vstorage --with-numactl --with-numad --with-capng --without-fuse --with-netcf --with-selinux --with-selinux-mount=/sys/fs/selinux --without-apparmor --without-hal --with-udev --with-yajl --with-sanlock --with-libpcap --with-macvtap --with-audit --with-dtrace --with-driver-modules --with-firewalld --with-firewalld-zone --without-wireshark-dissector --without-pm-utils --with-nss-plugin '--with-packager=Unknown, 2019-08-19-12:13:01, lab.rhel8.me' --with-packager-version=1.el8 --with-qemu-user=qemu --with-qemu-group=qemu --with-tls-priority=@LIBVIRT,SYSTEM --enable-werror --enable-expensive-tests --with-init-script=systemd --without-login-shell && make -j8
    return $?
}

function cleanup_exit(){
    killall libvirtd virtlogd
    exit $1
}

cd $SRC
build
if [ $? -ne 0 ];then
    echo "Build failure: $COMMIT_HASH" >> $LOG
    cleanup_exit 0
fi
LD_PRELOAD="$(find src -name '*.so.*'|tr '\n' ' ')" src/.libs/virtlogd &
pid_virtlogd=$!
LD_PRELOAD="$(find src -name '*.so.*'|tr '\n' ' ')" src/.libs/libvirtd &
pid_libvirtd=$!

sleep $SLEEP_LIBVIRTD

virsh list --all
sleep 2

coredumpctl -o $COREFILE dump $pid_libvirtd
if [ $? -eq 0 ];then
    gdb -q -batch|grep virDomainVirtioSerialAddrSetAddControllers -q
    if [ $? -eq 0 ];then
        echo "Get virDomainVirtioSerialAddrSetAddControllers crash: $COMMIT_HASH" >> $LOG
        cleanup_exit 1
    else
        echo "Get a unknown crash: $COMMIT_HASH" >> $LOG
    fi
else
    echo "Cannot get libvirt coredump: $COMMIT_HASH" >> $LOG
fi
cleanup_exit 0
