2
0

remote-gdb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env 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, $3)\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 %s)\n", @arches + 0, $1, $2, $3);
  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 extended-remote";
  54. -f $ARGV[0] and $cmd = "core-file";
  55. print $fh "$cmd $ARGV[0]\n";
  56. # History settings
  57. print $fh "set history filename $Bin/../tmp/.gdb_history\n";
  58. print $fh "set history size 100000000\n";
  59. print $fh "set history save on\n";
  60. my $file = -f "$sysroot/$ARGV[1]" ? "$sysroot/$ARGV[1]" : $ARGV[1];
  61. system($gdb, '-x', $fp, $file);
  62. close($fh);
  63. unlink($fp);
  64. }
  65. else
  66. {
  67. print("No gdb found! Make sure that CONFIG_GDB is set!\n");
  68. exit(1);
  69. }
  70. }
  71. else
  72. {
  73. print("No staging_dir found! You need to compile at least once!\n");
  74. exit(1);
  75. }