6 use File::Temp 'tempfile';
9 die "Usage: $0 <corefile|host:port> <executable>\n";
13 if( opendir SD, "$Bin/../staging_dir" )
15 my ( $tid, $arch, $libc, @arches );
17 if( $ARGV[1] =~ m!\btarget-(.+?)_(([^/_]+libc|musl)[^/]+)\b!i )
19 print("Using target $1 ($2)\n");
20 ($arch, $libc) = ($1, $2);
25 print("Choose target:\n");
27 while( defined( my $e = readdir SD ) )
29 if( -d "$Bin/../staging_dir/$e" && $e =~ /^target-(.+?)_(([^_]+libc|musl).+)/i )
31 push @arches, [ $1, $2 ];
32 printf(" %2d) %s (%s)\n", @arches + 0, $1, $2);
41 chomp($tid = <STDIN>);
42 } while( !defined($tid) || $tid !~ /^\d+$/ || $tid < 1 || $tid > @arches );
44 ($arch, $libc) = @{$arches[$tid-1]};
48 ($arch, $libc) = @{$arches[0]};
55 my ($gdb) = glob("$Bin/../staging_dir/toolchain-${arch}_*_${libc}*/bin/*-gdb");
56 if( defined($gdb) && -x $gdb )
58 my ( $fh, $fp ) = tempfile();
61 my ($sysroot) = glob("$Bin/../staging_dir/target-${arch}_${libc}/root-*/");
63 print $fh "set sysroot $sysroot\n" if $sysroot;
64 my $cmd = "target extended-remote";
65 -f $ARGV[0] and $cmd = "core-file";
66 print $fh "$cmd $ARGV[0]\n";
69 print $fh "set history filename $Bin/../tmp/.gdb_history\n";
70 print $fh "set history size 100000000\n";
71 print $fh "set history save on\n";
73 my $file = -f "$sysroot/$ARGV[1]" ? "$sysroot/$ARGV[1]" : $ARGV[1];
74 system($gdb, '-x', $fp, $file);
81 print("No gdb found! Make sure that CONFIG_GDB is set!\n");
87 print("No staging_dir found! You need to compile at least once!\n");