remote-gdb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if( @arches > 1 )
  31. {
  32. # Query arch
  33. do {
  34. print("Target? > ");
  35. chomp($tid = <STDIN>);
  36. } while( !defined($tid) || $tid !~ /^\d+$/ || $tid < 1 || $tid > @arches );
  37. ($arch, $libc) = @{$arches[$tid-1]};
  38. }
  39. else
  40. {
  41. ($arch, $libc) = @{$arches[0]};
  42. }
  43. }
  44. closedir SD;
  45. # Find gdb
  46. my ($gdb) = glob("$Bin/../build_dir/toolchain-${arch}_*_${libc}/gdb-*/gdb/gdb");
  47. if( defined($gdb) && -x $gdb )
  48. {
  49. my ( $fh, $fp ) = tempfile();
  50. # Find sysroot
  51. my ($sysroot) = glob("$Bin/../staging_dir/target-${arch}_${libc}/root-*/");
  52. print $fh "set sysroot $sysroot\n" if $sysroot;
  53. print $fh "target remote $ARGV[0]\n";
  54. system($gdb, '-x', $fp, $ARGV[1]);
  55. close($fh);
  56. unlink($fp);
  57. }
  58. else
  59. {
  60. print("No gdb found! Make sure that CONFIG_GDB is set!\n");
  61. exit(1);
  62. }
  63. }
  64. else
  65. {
  66. print("No staging_dir found! You need to compile at least once!\n");
  67. exit(1);
  68. }