1da0c48c4Sopenharmony_ci#!/usr/bin/env bash
2da0c48c4Sopenharmony_ci#
3da0c48c4Sopenharmony_ci# Copyright (C) 2019-2021 Red Hat, Inc.
4da0c48c4Sopenharmony_ci# This file is part of elfutils.
5da0c48c4Sopenharmony_ci#
6da0c48c4Sopenharmony_ci# This file is free software; you can redistribute it and/or modify
7da0c48c4Sopenharmony_ci# it under the terms of the GNU General Public License as published by
8da0c48c4Sopenharmony_ci# the Free Software Foundation; either version 3 of the License, or
9da0c48c4Sopenharmony_ci# (at your option) any later version.
10da0c48c4Sopenharmony_ci#
11da0c48c4Sopenharmony_ci# elfutils is distributed in the hope that it will be useful, but
12da0c48c4Sopenharmony_ci# WITHOUT ANY WARRANTY; without even the implied warranty of
13da0c48c4Sopenharmony_ci# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14da0c48c4Sopenharmony_ci# GNU General Public License for more details.
15da0c48c4Sopenharmony_ci#
16da0c48c4Sopenharmony_ci# You should have received a copy of the GNU General Public License
17da0c48c4Sopenharmony_ci# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18da0c48c4Sopenharmony_ci
19da0c48c4Sopenharmony_ci. $srcdir/debuginfod-subr.sh
20da0c48c4Sopenharmony_ci
21da0c48c4Sopenharmony_ci# for test case debugging, uncomment:
22da0c48c4Sopenharmony_ciset -x
23da0c48c4Sopenharmony_ciunset VALGRIND_CMD
24da0c48c4Sopenharmony_ci# This variable is essential and ensures no time-race for claiming ports occurs
25da0c48c4Sopenharmony_ci# set base to a unique multiple of 100 not used in any other 'run-debuginfod-*' test
26da0c48c4Sopenharmony_cibase=8400
27da0c48c4Sopenharmony_ciget_ports
28da0c48c4Sopenharmony_cimkdir F
29da0c48c4Sopenharmony_ci
30da0c48c4Sopenharmony_ciDB=${PWD}/.debuginfod_tmp.sqlite
31da0c48c4Sopenharmony_citempfiles $DB
32da0c48c4Sopenharmony_ciexport DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache
33da0c48c4Sopenharmony_ci
34da0c48c4Sopenharmony_ciecho 'int main(int argc, char * argv){ return 0; }' > ${PWD}/prog.c
35da0c48c4Sopenharmony_cigcc -Wl,--build-id -g -o prog ${PWD}/prog.c
36da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/src/strip -g -f prog.debug ${PWD}/prog
37da0c48c4Sopenharmony_citempfiles prog prog.debug prog.c
38da0c48c4Sopenharmony_ciBUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
39da0c48c4Sopenharmony_ci          -a prog | grep 'Build ID' | cut -d ' ' -f 7`
40da0c48c4Sopenharmony_ciexport DEBUGINFOD_URLS=http://127.0.0.1:$PORT1
41da0c48c4Sopenharmony_cienv LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -d $DB -F -p $PORT1 -t0 -g0 -v F > vlog$PORT1 2>&1 &
42da0c48c4Sopenharmony_ciPID1=$!
43da0c48c4Sopenharmony_citempfiles vlog$PORT1
44da0c48c4Sopenharmony_cierrfiles vlog$PORT1
45da0c48c4Sopenharmony_ci
46da0c48c4Sopenharmony_ci# Server must become ready
47da0c48c4Sopenharmony_ciwait_ready $PORT1 'ready' 1
48da0c48c4Sopenharmony_ci# And the initial scan should have been done before moving
49da0c48c4Sopenharmony_ci# files under the scan dirs.
50da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="traverse"}' 1
51da0c48c4Sopenharmony_ci
52da0c48c4Sopenharmony_cimv prog F
53da0c48c4Sopenharmony_cimv prog.debug F
54da0c48c4Sopenharmony_citempfiles prog/F
55da0c48c4Sopenharmony_ci
56da0c48c4Sopenharmony_cikill -USR1 $PID1
57da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="traverse"}' 2
58da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_pending{role="scan"}' 0
59da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_busy{role="scan"}' 0
60da0c48c4Sopenharmony_ci
61da0c48c4Sopenharmony_ci# Check thread comm names
62da0c48c4Sopenharmony_cips -q $PID1 -e -L -o '%p %c %a' | grep groom
63da0c48c4Sopenharmony_cips -q $PID1 -e -L -o '%p %c %a' | grep scan
64da0c48c4Sopenharmony_cips -q $PID1 -e -L -o '%p %c %a' | grep traverse
65da0c48c4Sopenharmony_ci
66da0c48c4Sopenharmony_ci# Add artifacts to the search paths and test whether debuginfod finds them while already running.
67da0c48c4Sopenharmony_ci# Build another, non-stripped binary
68da0c48c4Sopenharmony_ciecho "int main() { return 0; }" > ${PWD}/prog2.c
69da0c48c4Sopenharmony_citempfiles prog2.c
70da0c48c4Sopenharmony_cigcc -Wl,--build-id -g -o prog2 ${PWD}/prog2.c
71da0c48c4Sopenharmony_ci#testrun ${abs_top_builddir}/src/strip -g -f prog.debug ${PWD}/prog
72da0c48c4Sopenharmony_ciBUILDID2=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
73da0c48c4Sopenharmony_ci          -a prog2 | grep 'Build ID' | cut -d ' ' -f 7`
74da0c48c4Sopenharmony_cimv prog2 F
75da0c48c4Sopenharmony_ci#mv prog2.debug F
76da0c48c4Sopenharmony_citempfiles F/prog2 F/prog2.debug
77da0c48c4Sopenharmony_ci
78da0c48c4Sopenharmony_cikill -USR1 $PID1
79da0c48c4Sopenharmony_ci# Now there should be 3 files in the index
80da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="traverse"}' 3
81da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_pending{role="scan"}' 0
82da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_busy{role="scan"}' 0
83da0c48c4Sopenharmony_ci
84da0c48c4Sopenharmony_ci########################################################################
85da0c48c4Sopenharmony_ci
86da0c48c4Sopenharmony_ci# Test whether debuginfod-find is able to fetch those files.
87da0c48c4Sopenharmony_cirm -rf $DEBUGINFOD_CACHE_PATH # clean it from previous tests
88da0c48c4Sopenharmony_cifilename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID`
89da0c48c4Sopenharmony_cicmp $filename F/prog.debug
90da0c48c4Sopenharmony_ci
91da0c48c4Sopenharmony_cifilename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find executable F/prog`
92da0c48c4Sopenharmony_cicmp $filename F/prog
93da0c48c4Sopenharmony_ci
94da0c48c4Sopenharmony_ci# raw source filename
95da0c48c4Sopenharmony_cifilename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID ${PWD}/foobar///./../prog.c`
96da0c48c4Sopenharmony_cicmp $filename  ${PWD}/prog.c
97da0c48c4Sopenharmony_ci
98da0c48c4Sopenharmony_ci# and also the canonicalized one
99da0c48c4Sopenharmony_cifilename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID ${PWD}/prog.c`
100da0c48c4Sopenharmony_cicmp $filename  ${PWD}/prog.c
101da0c48c4Sopenharmony_ci
102da0c48c4Sopenharmony_ci# Rerun same tests for the prog2 binary
103da0c48c4Sopenharmony_cifilename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find -v debuginfo $BUILDID2 2>vlog`
104da0c48c4Sopenharmony_cicmp $filename F/prog2
105da0c48c4Sopenharmony_cigrep -q Progress vlog
106da0c48c4Sopenharmony_cigrep -q Downloaded.from vlog
107da0c48c4Sopenharmony_citempfiles vlog
108da0c48c4Sopenharmony_cifilename=`testrun env DEBUGINFOD_PROGRESS=1 ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID2 2>vlog2`
109da0c48c4Sopenharmony_cicmp $filename F/prog2
110da0c48c4Sopenharmony_cigrep -q 'Downloading.*http' vlog2
111da0c48c4Sopenharmony_citempfiles vlog2
112da0c48c4Sopenharmony_cifilename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID2 ${PWD}/prog2.c`
113da0c48c4Sopenharmony_cicmp $filename ${PWD}/prog2.c
114da0c48c4Sopenharmony_ci
115da0c48c4Sopenharmony_ci
116da0c48c4Sopenharmony_cikill $PID1
117da0c48c4Sopenharmony_ciwait $PID1
118da0c48c4Sopenharmony_ciPID1=0
119da0c48c4Sopenharmony_ciexit 0
120