test_debuginfo.pl 2.4 KB

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