remote-gdb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use FindBin '$Bin';
  5. use File::Temp 'tempfile';
  6. @ARGV == 2 || do {
  7. die "Usage: $0 <host:port> <executable>\n";
  8. exit 1;
  9. };
  10. if( opendir SD, "$Bin/../staging_dir" )
  11. {
  12. my ( $tid, $arch, $libc, @arches );
  13. if( $ARGV[1] =~ m!\btarget-([^_/]+)_([^_/]+)\b! )
  14. {
  15. print("Using target $1 ($2)\n");
  16. ($arch, $libc) = ($1, $2);
  17. }
  18. else
  19. {
  20. # Find arches
  21. print("Choose target:\n");
  22. while( defined( my $e = readdir SD ) )
  23. {
  24. if( -d "$Bin/../staging_dir/$e" && $e =~ /^target-([^_]+)_([^_]+)/ )
  25. {
  26. push @arches, [ $1, $2 ];
  27. printf(" %2d) %s (%s)\n", @arches + 0, $1, $2);
  28. }
  29. }
  30. # Query arch
  31. do {
  32. print("Target? > ");
  33. chomp($tid = <STDIN>);
  34. } while( !defined($tid) || $tid !~ /^\d+$/ || $tid < 1 || $tid > @arches );
  35. ($arch, $libc) = @{$arches[$tid-1]};
  36. }
  37. closedir SD;
  38. # Find gdb
  39. my ($gdb) = glob("$Bin/../build_dir/toolchain-${arch}_*_${libc}/gdb-*/gdb/gdb");
  40. if( -x $gdb )
  41. {
  42. my ( $fh, $fp ) = tempfile();
  43. # Find library paths
  44. my $libdirs = join ':', (
  45. glob("$Bin/../staging_dir/target-${arch}_${libc}/root-*/{,usr/}lib/"),
  46. glob("$Bin/../staging_dir/target-${arch}_${libc}/{,usr/}lib/"),
  47. glob("$Bin/../staging_dir/toolchain-${arch}_*_${libc}/lib/")
  48. );
  49. print $fh "set solib-search-path $libdirs\n";
  50. print $fh "target remote $ARGV[0]\n";
  51. system($gdb, '-x', $fp, $ARGV[1]);
  52. close($fh);
  53. unlink($fp);
  54. }
  55. else
  56. {
  57. print("No gdb found! Make sure that CONFIG_GDB is set!\n");
  58. exit(1);
  59. }
  60. }
  61. else
  62. {
  63. print("No staging_dir found! You need to compile at least once!\n");
  64. exit(1);
  65. }