Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 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. |
Adrian Prantl | 4f1e5fb | 2013-09-06 18:12:01 +0000 | [diff] [blame] | 14 | # |
| 15 | # On Darwin the default is to use the llgdb.py wrapper script which |
| 16 | # translates gdb commands into their lldb equivalents. |
Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 17 | |
| 18 | use File::Basename; |
Adrian Prantl | 4f1e5fb | 2013-09-06 18:12:01 +0000 | [diff] [blame] | 19 | use Config; |
| 20 | use Cwd; |
Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 21 | |
| 22 | my $testcase_file = $ARGV[0]; |
| 23 | my $executable_file = $ARGV[1]; |
| 24 | |
| 25 | my $input_filename = basename $testcase_file; |
| 26 | my $output_dir = dirname $executable_file; |
| 27 | |
Devang Patel | 12b0179 | 2010-09-13 21:23:17 +0000 | [diff] [blame] | 28 | my $debugger_script_file = "$output_dir/$input_filename.debugger.script"; |
Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 29 | my $output_file = "$output_dir/$input_filename.gdb.output"; |
| 30 | |
Adrian Prantl | 4f1e5fb | 2013-09-06 18:12:01 +0000 | [diff] [blame] | 31 | my %cmd_map = (); |
| 32 | # Assume lldb to be the debugger on Darwin. |
| 33 | my $use_lldb = 0; |
| 34 | $use_lldb = 1 if ($Config{osname} eq "darwin"); |
| 35 | |
Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 36 | # Extract debugger commands from testcase. They are marked with DEBUGGER: |
Eric Christopher | db1714e | 2012-06-26 00:28:15 +0000 | [diff] [blame] | 37 | # at the beginning of a comment line. |
Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 38 | open(INPUT, $testcase_file); |
| 39 | open(OUTPUT, ">$debugger_script_file"); |
| 40 | while(<INPUT>) { |
| 41 | my($line) = $_; |
| 42 | $i = index($line, "DEBUGGER:"); |
| 43 | if ( $i >= 0) { |
| 44 | $l = length("DEBUGGER:"); |
| 45 | $s = substr($line, $i + $l); |
| 46 | print OUTPUT "$s"; |
| 47 | } |
| 48 | } |
| 49 | print OUTPUT "\n"; |
| 50 | print OUTPUT "quit\n"; |
| 51 | close(INPUT); |
| 52 | close(OUTPUT); |
| 53 | |
| 54 | # setup debugger and debugger options to run a script. |
| 55 | my $my_debugger = $ENV{'DEBUGGER'}; |
| 56 | if (!$my_debugger) { |
Adrian Prantl | 4f1e5fb | 2013-09-06 18:12:01 +0000 | [diff] [blame] | 57 | if ($use_lldb) { |
| 58 | my $path = dirname(Cwd::abs_path($0)); |
| 59 | $my_debugger = "/usr/bin/env python $path/../tools/clang/test/debuginfo-tests/llgdb.py"; |
| 60 | } else { |
| 61 | $my_debugger = "gdb"; |
| 62 | } |
Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 63 | } |
Adrian Prantl | 4f1e5fb | 2013-09-06 18:12:01 +0000 | [diff] [blame] | 64 | |
| 65 | # quiet / exit after cmdline / no init file / execute script |
Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 66 | my $debugger_options = "-q -batch -n -x"; |
| 67 | |
| 68 | # run debugger and capture output. |
Eric Christopher | 581a739 | 2012-07-23 20:54:17 +0000 | [diff] [blame] | 69 | system("$my_debugger $debugger_options $debugger_script_file $executable_file > $output_file 2>&1"); |
Devang Patel | c7183ba | 2010-09-13 20:42:15 +0000 | [diff] [blame] | 70 | |
| 71 | # validate output. |
| 72 | system("FileCheck", "-input-file", "$output_file", "$testcase_file"); |
| 73 | if ($?>>8 == 1) { |
| 74 | exit 1; |
| 75 | } |
| 76 | else { |
| 77 | exit 0; |
| 78 | } |