1#!/usr/bin/perl
2#
3# This script tests debugging information generated by a compiler.
4# Input arguments
5#   - Input source program. Usually this source file is decorated using
6#     special comments to communicate debugger commands.
7#   - Executable file. This file is generated by the compiler.
8#
9# This perl script extracts debugger commands from input source program
10# comments in a script. A debugger is used to load the executable file
11# and run the script generated from source program comments. Finally,
12# the debugger output is checked, using FileCheck, to validate
13# debugging information.
14
15use File::Basename;
16
17my $testcase_file = $ARGV[0];
18my $executable_file = $ARGV[1];
19
20my $input_filename = basename $testcase_file;
21my $output_dir = dirname $executable_file;
22
23my $debugger_script_file = "$output_dir/$input_filename.debugger.script";
24my $output_file = "$output_dir/$input_filename.gdb.output";
25
26# Extract debugger commands from testcase. They are marked with DEBUGGER:
27# at the beginning of a comment line.
28open(INPUT, $testcase_file);
29open(OUTPUT, ">$debugger_script_file");
30while(<INPUT>) {
31    my($line) = $_;
32    $i = index($line, "DEBUGGER:");
33    if ( $i >= 0) {
34        $l = length("DEBUGGER:");
35        $s = substr($line, $i + $l);
36        print OUTPUT  "$s";
37    }
38}
39print OUTPUT "\n";
40print OUTPUT "quit\n";
41close(INPUT);
42close(OUTPUT);
43
44# setup debugger and debugger options to run a script.
45my $my_debugger = $ENV{'DEBUGGER'};
46if (!$my_debugger) {
47    $my_debugger = "gdb";
48}
49my $debugger_options = "-q -batch -n -x";
50
51# run debugger and capture output.
52system("$my_debugger $debugger_options $debugger_script_file $executable_file > $output_file 2>&1");
53
54# validate output.
55system("FileCheck", "-input-file", "$output_file", "$testcase_file");
56if ($?>>8 == 1) {
57    exit 1;
58}
59else {
60    exit 0;
61}
62