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
25da0c48c4Sopenharmony_ciDB=${PWD}/.debuginfod_tmp.sqlite
26da0c48c4Sopenharmony_citempfiles $DB
27da0c48c4Sopenharmony_ciexport DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache
28da0c48c4Sopenharmony_ci
29da0c48c4Sopenharmony_cimkdir F
30da0c48c4Sopenharmony_cimkdir -p $DEBUGINFOD_CACHE_PATH
31da0c48c4Sopenharmony_ci# This variable is essential and ensures no time-race for claiming ports occurs
32da0c48c4Sopenharmony_ci# set base to a unique multiple of 100 not used in any other 'run-debuginfod-*' test
33da0c48c4Sopenharmony_cibase=9700
34da0c48c4Sopenharmony_ciget_ports
35da0c48c4Sopenharmony_ci
36da0c48c4Sopenharmony_cienv LD_LIBRARY_PATH=$ldpath DEBUGINFOD_URLS= ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -F -p $PORT1 -d $DB \
37da0c48c4Sopenharmony_ci    -t0 -g0 -v F > vlog$PORT1 2>&1 &
38da0c48c4Sopenharmony_ciPID1=$!
39da0c48c4Sopenharmony_citempfiles vlog$PORT1
40da0c48c4Sopenharmony_cierrfiles vlog$PORT1
41da0c48c4Sopenharmony_ci# Server must become ready
42da0c48c4Sopenharmony_ciwait_ready $PORT1 'ready' 1
43da0c48c4Sopenharmony_ci# And initial scan should be done
44da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="traverse"}' 1
45da0c48c4Sopenharmony_ci
46da0c48c4Sopenharmony_ciexport DEBUGINFOD_URLS=http://127.0.0.1:$PORT1/   # or without trailing /
47da0c48c4Sopenharmony_ci
48da0c48c4Sopenharmony_ci# Check thread comm names
49da0c48c4Sopenharmony_cips -q $PID1 -e -L -o '%p %c %a' | grep groom
50da0c48c4Sopenharmony_cips -q $PID1 -e -L -o '%p %c %a' | grep scan
51da0c48c4Sopenharmony_cips -q $PID1 -e -L -o '%p %c %a' | grep traverse
52da0c48c4Sopenharmony_ci
53da0c48c4Sopenharmony_ci# We use -t0 and -g0 here to turn off time-based scanning & grooming.
54da0c48c4Sopenharmony_ci# For testing purposes, we just sic SIGUSR1 / SIGUSR2 at the process.
55da0c48c4Sopenharmony_ci
56da0c48c4Sopenharmony_ci########################################################################
57da0c48c4Sopenharmony_ci
58da0c48c4Sopenharmony_ci# Compile a simple program, strip its debuginfo and save the build-id.
59da0c48c4Sopenharmony_ci# Also move the debuginfo into another directory so that elfutils
60da0c48c4Sopenharmony_ci# cannot find it without debuginfod.
61da0c48c4Sopenharmony_ciecho "int main() { return 0; }" > ${PWD}/p+r%o\$g.c
62da0c48c4Sopenharmony_citempfiles p+r%o\$g.c
63da0c48c4Sopenharmony_ci# Create a subdirectory to confound source path names
64da0c48c4Sopenharmony_cimkdir foobar
65da0c48c4Sopenharmony_cigcc -Wl,--build-id -g -o p+r%o\$g ${PWD}/foobar///./../p+r%o\$g.c
66da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/src/strip -g -f p+r%o\$g.debug ${PWD}/p+r%o\$g
67da0c48c4Sopenharmony_ciBUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
68da0c48c4Sopenharmony_ci          -a p+r%o\\$g | grep 'Build ID' | cut -d ' ' -f 7`
69da0c48c4Sopenharmony_ci
70da0c48c4Sopenharmony_cimv p+r%o\$g F
71da0c48c4Sopenharmony_cimv p+r%o\$g.debug F
72da0c48c4Sopenharmony_cikill -USR1 $PID1
73da0c48c4Sopenharmony_ci# Wait till both files are in the index.
74da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="traverse"}' 2
75da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_pending{role="scan"}' 0
76da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_busy{role="scan"}' 0
77da0c48c4Sopenharmony_ci
78da0c48c4Sopenharmony_ci########################################################################
79da0c48c4Sopenharmony_ci
80da0c48c4Sopenharmony_ci# Test whether the cache default locations are correct
81da0c48c4Sopenharmony_cimkdir tmphome
82da0c48c4Sopenharmony_ci
83da0c48c4Sopenharmony_ci# $HOME/.cache should be created.
84da0c48c4Sopenharmony_citestrun env HOME=$PWD/tmphome XDG_CACHE_HOME= DEBUGINFOD_CACHE_PATH= ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
85da0c48c4Sopenharmony_ciif [ ! -f $PWD/tmphome/.cache/debuginfod_client/$BUILDID/debuginfo ]; then
86da0c48c4Sopenharmony_ci  echo "could not find cache in $PWD/tmphome/.cache"
87da0c48c4Sopenharmony_ci  err
88da0c48c4Sopenharmony_cifi
89da0c48c4Sopenharmony_ci
90da0c48c4Sopenharmony_ci# $HOME/.cache should be found.
91da0c48c4Sopenharmony_citestrun env HOME=$PWD/tmphome XDG_CACHE_HOME= DEBUGINFOD_CACHE_PATH= ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID
92da0c48c4Sopenharmony_ciif [ ! -f $PWD/tmphome/.cache/debuginfod_client/$BUILDID/executable ]; then
93da0c48c4Sopenharmony_ci  echo "could not find cache in $PWD/tmphome/.cache"
94da0c48c4Sopenharmony_ci  err
95da0c48c4Sopenharmony_cifi
96da0c48c4Sopenharmony_ci# $XDG_CACHE_HOME should take priority over $HOME.cache.
97da0c48c4Sopenharmony_citestrun env HOME=$PWD/tmphome XDG_CACHE_HOME=$PWD/tmpxdg DEBUGINFOD_CACHE_PATH= ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
98da0c48c4Sopenharmony_ciif [ ! -f $PWD/tmpxdg/debuginfod_client/$BUILDID/debuginfo ]; then
99da0c48c4Sopenharmony_ci  echo "could not find cache in $PWD/tmpxdg/"
100da0c48c4Sopenharmony_ci  err
101da0c48c4Sopenharmony_cifi
102da0c48c4Sopenharmony_ci
103da0c48c4Sopenharmony_ci# A cache at the old default location ($HOME/.debuginfod_client_cache) should take
104da0c48c4Sopenharmony_ci# priority over $HOME/.cache, $XDG_CACHE_HOME.
105da0c48c4Sopenharmony_cicp -vr $DEBUGINFOD_CACHE_PATH tmphome/.debuginfod_client_cache || true
106da0c48c4Sopenharmony_ci# ||true is for tolerating errors, such a valgrind or something else
107da0c48c4Sopenharmony_ci#        leaving negative-hit files in there
108da0c48c4Sopenharmony_ci
109da0c48c4Sopenharmony_ci# Add a file that doesn't exist in $HOME/.cache, $XDG_CACHE_HOME.
110da0c48c4Sopenharmony_cimkdir tmphome/.debuginfod_client_cache/deadbeef
111da0c48c4Sopenharmony_ciecho ELF... > tmphome/.debuginfod_client_cache/deadbeef/debuginfo
112da0c48c4Sopenharmony_cifilename=`testrun env HOME=$PWD/tmphome XDG_CACHE_HOME=$PWD/tmpxdg DEBUGINFOD_CACHE_PATH= ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo deadbeef`
113da0c48c4Sopenharmony_cicmp $filename tmphome/.debuginfod_client_cache/deadbeef/debuginfo
114da0c48c4Sopenharmony_ci
115da0c48c4Sopenharmony_ci# $DEBUGINFO_CACHE_PATH should take priority over all else.
116da0c48c4Sopenharmony_citestrun env HOME=$PWD/tmphome XDG_CACHE_HOME=$PWD/tmpxdg DEBUGINFOD_CACHE_PATH=$PWD/tmpcache ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
117da0c48c4Sopenharmony_ciif [ ! -f $PWD/tmpcache/$BUILDID/debuginfo ]; then
118da0c48c4Sopenharmony_ci  echo "could not find cache in $PWD/tmpcache/"
119da0c48c4Sopenharmony_ci  err
120da0c48c4Sopenharmony_cifi
121da0c48c4Sopenharmony_cirm -rf ${PWD}/tmphome/ ${PWD}/tmpxdg ${PWD}/tmpcache
122da0c48c4Sopenharmony_ci
123da0c48c4Sopenharmony_cikill $PID1
124da0c48c4Sopenharmony_ciwait $PID1
125da0c48c4Sopenharmony_ciPID1=0
126da0c48c4Sopenharmony_ciexit 0
127