1#!/usr/bin/perl
2#===-- test_bcc_debuginfo.pl - Debugger integration test driver script ---===#
3#
4#                     The LLVM Compiler Infrastructure
5#
6# This file is distributed under the University of Illinois Open Source
7# License. See LICENSE.TXT for details.
8#
9#===----------------------------------------------------------------------===#
10#
11# This script tests debugging information generated by a compiler.
12# Input arguments
13#   - Path to FileCheck tool.
14#   - Input source program. Usually this source file is decorated using
15#     special comments (//DEBUGGER:, //CHECK:)to communicate debugger commands.
16#   - Executable file. This file is generated by the compiler.
17#
18# This perl script extracts debugger commands from input source program
19# comments in a script. A debugger is used to load the executable file
20# and run the script generated from source program comments. Finally,
21# the debugger output is checked, using FileCheck, to validate
22# debugging information.
23#
24#===----------------------------------------------------------------------===#
25
26use File::Basename;
27
28my $filecheck_tool = $ARGV[0];
29my $testcase_file = $ARGV[1];
30my $testcase_output = $ARGV[2];
31
32my $input_filename = basename $testcase_file;
33my $output_dir = dirname $testcase_output;
34
35my $debugger_script_file = "$output_dir/$input_filename.debugger.script";
36my $output_file = "$output_dir/$input_filename.gdb.output";
37
38open(OUTPUT, ">$debugger_script_file");
39
40# Enable extra verbosity in GDB
41print OUTPUT "set verbose on\n";
42
43# Extract debugger commands from testcase. They are marked with DEBUGGER:
44# at the beginning of a comment line.
45open(INPUT, $testcase_file);
46while(<INPUT>) {
47    my($line) = $_;
48    $i = index($line, "DEBUGGER:");
49    if ( $i >= 0) {
50        $l = length("DEBUGGER:");
51        $s = substr($line, $i + $l);
52        $s =~ s/\%s/$input_filename/g;
53        $s =~ s/\%t/$testcase_output/g;
54        print OUTPUT  "$s";
55    }
56}
57print OUTPUT "\n";
58print OUTPUT "quit\n";
59close(INPUT);
60close(OUTPUT);
61
62# setup debugger and debugger options to run a script.
63my $debugger = $ENV{'DEBUGGER'};
64my $debugger_options = $ENV{'DEBUGGER_ARGS'};
65if (!$debugger) {
66    print "Please set DEBUGGER prior to using this script";
67    exit 1;
68}
69$debugger_options = "-x $debugger_script_file $debugger_options $testcase_output";
70
71# run debugger and capture output.
72system("$debugger $debugger_options > $output_file 2>&1") ;
73if ($?>>8 != 0) {
74  print "Error during debugger invocation. Command used was: \n";
75  print("$debugger $debugger_options > $output_file 2>&1\n") ;
76  exit 1;
77}
78
79# validate output.
80system("$filecheck_tool", "-input-file", "$output_file", "$testcase_file");
81if ($?>>8 != 0) {
82    print "Error during verification. Debugger command used was: \n";
83    print("$debugger $debugger_options > $output_file 2>&1\n") ;
84    print "Verification command used was: \n";
85    print "$filecheck_tool -input-file $output_file $testcase_file\n";
86    exit 1;
87}
88else {
89    exit 0;
90}
91