remote-gdb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <corefile|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-(.+?)_(([^_]+libc|musl)[^/]+)\b!i )
  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-(.+?)_(([^_]+.libc|musl)+)/i )
  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/../staging_dir/toolchain-${arch}_*_${libc}*/bin/*-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. my $cmd = "target remote";
  54. -f $ARGV[0] and $cmd = "core-file";
  55. print $fh "$cmd $ARGV[0]\n";
  56. my $file = -f "$sysroot/$ARGV[1]" ? "$sysroot/$ARGV[1]" : $ARGV[1];
  57. system($gdb, '-x', $fp, $file);
  58. close($fh);
  59. unlink($fp);
  60. }
  61. else
  62. {
  63. print("No gdb found! Make sure that CONFIG_GDB is set!\n");
  64. exit(1);
  65. }
  66. }
  67. else
  68. {
  69. print("No staging_dir found! You need to compile at least once!\n");
  70. exit(1);
  71. }