Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / memcheck / tests / linux / debuginfod-check.pl
blob4a2c1c1e1d9f11b6efbc8763c641670979336159
1 #! /usr/bin/perl
2 use warnings;
3 use strict;
4 use Cwd;
5 use IPC::Open3;
7 our $dir = Cwd::realpath("./.debuginfod");
8 our $pid = 0;
10 # Kill the server and remove temporary files.
11 sub cleanup_and_exit($) {
12 my $exit_code = $_[0];
13 mysystem("rm -rf $dir");
14 if ($pid != 0) {
15 kill "INT", $pid;
17 exit $exit_code;
20 # Propagate Ctrl-C and exit if command results in an error.
21 sub mysystem($)
23 my $exit_code = system($_[0]);
24 ($exit_code == 2) and cleanup_and_exit(1); # Exit if SIGINT
25 if ($exit_code != 0) {
26 #warn "Error while executing: $_[0]";
27 cleanup_and_exit(1);
29 return $exit_code;
32 # Check that debuginfod and debuginfod-find can be found
33 mysystem("debuginfod --help > /dev/null");
34 mysystem("debuginfod-find --help > /dev/null");
36 $SIG{'INT'} = sub { cleanup_and_exit(1) };
38 my $testname = "debuginfod-check";
39 my $tmp = "$dir/debuginfod_test.tmp";
40 my $dbg = "$dir/dbg";
41 mysystem("rm -rf $dir");
42 mysystem("mkdir -p $dbg");
44 # Compile the test executable, strip its debuginfo and store it so
45 # that valgrind cannot find it without debuginfod.
46 mysystem("gcc -O0 -g -o $testname $testname.c");
47 mysystem("objcopy --only-keep-debug $testname $testname.debug");
48 mysystem("objcopy --strip-unneeded $testname");
49 mysystem("objcopy --add-gnu-debuglink=$testname.debug $testname");
50 mysystem("mv $testname.debug $dbg");
51 mysystem("readelf -n $testname > $tmp 2>&1");
53 my $buildid = "";
54 open(TMP, '<', $tmp);
55 while (my $out = <TMP>) {
56 if ($out =~ /Build ID: ([0-9a-f]*)/) {
57 $buildid = $1;
61 if ($buildid eq "") {
62 warn "can't find $testname build-id";
63 cleanup_and_exit(1);
66 my $found_port = 0;
67 my $port = 7999;
69 # Find an unused port
70 while ($found_port == 0 and $port < 65536) {
71 $port++;
72 $pid = open3(undef, "TMP", undef,
73 "debuginfod", "-d", "$dir/db", '-F', "$dbg", "--port=$port");
74 for (my $i = 0; $i < 5 and $found_port == 0; $i++) {
75 while (my $got = <TMP>) {
76 if ($got =~ /Failed to bind/) {
77 last;
78 } elsif ($got =~ /started http server/) {
79 $found_port = 1;
80 last;
86 if ($port == 65536) {
87 warn "No available ports";
88 cleanup_and_exit(1);
91 my $server_ready = 0;
93 # Confirm that the server is ready to be queried
94 for (my $i = 0; $i < 10 and $server_ready == 0; $i++) {
95 sleep 1;
96 my $got = `curl -s http://localhost:$port/metrics`;
97 if ($got =~ /ready 1/
98 and $got =~ /thread_work_total\{role=\"traverse\"\} 1/
99 and $got =~ /thread_work_pending\{role=\"scan\"\} 0/
100 and $got =~ /thread_busy\{role=\"scan\"\} 0/) {
101 $server_ready = 1;
105 if ($server_ready == 0) {
106 warn "Can't start debuginfod server";
107 cleanup_and_exit(1);
110 # Query the server and store the debuginfo in the client cache for valgrind to find.
111 my $myres = mysystem("DEBUGINFOD_CACHE_PATH=$dir DEBUGINFOD_URLS=http://localhost:$port debuginfod-find debuginfo $buildid > /dev/null 2>&1");
112 if ($myres != 0) {
113 cleanup_and_exit(1);
115 kill "INT", $pid;
116 exit 0;