scripts/config: make wildcard include with no results non-fatal
[openwrt.git] / scripts / remote-gdb
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use FindBin '$Bin';
6 use File::Temp 'tempfile';
7
8 @ARGV == 2 || do {
9         die "Usage: $0 <corefile|host:port> <executable>\n";
10         exit 1;
11 };
12
13 if( opendir SD, "$Bin/../staging_dir" )
14 {
15         my ( $tid, $arch, $libc, @arches );
16
17         if( $ARGV[1] =~ m!\btarget-(.+?)_(([^_]+libc|musl)[^/]+)\b!i )
18         {
19                 print("Using target $1 ($2)\n");
20                 ($arch, $libc) = ($1, $2);
21         }
22         else
23         {
24                 # Find arches
25                 print("Choose target:\n");
26
27                 while( defined( my $e = readdir SD ) )
28                 {
29                         if( -d "$Bin/../staging_dir/$e" && $e =~ /^target-(.+?)_(([^_]+.libc|musl)+)/i )
30                         {
31                                 push @arches, [ $1, $2 ];
32                                 printf(" %2d) %s (%s)\n", @arches + 0, $1, $2);
33                         }
34                 }
35
36                 if( @arches > 1 )
37                 {
38                         # Query arch
39                         do {
40                                 print("Target? > ");
41                                 chomp($tid = <STDIN>);
42                         } while( !defined($tid) || $tid !~ /^\d+$/ || $tid < 1 || $tid > @arches );
43
44                         ($arch, $libc) = @{$arches[$tid-1]};
45                 }
46                 else
47                 {
48                         ($arch, $libc) = @{$arches[0]};
49                 }
50         }
51
52         closedir SD;
53
54         # Find gdb
55         my ($gdb) = glob("$Bin/../staging_dir/toolchain-${arch}_*_${libc}*/bin/*-gdb");
56         if( defined($gdb) && -x $gdb )
57         {
58                 my ( $fh, $fp ) = tempfile();
59
60                 # Find sysroot
61                 my ($sysroot) = glob("$Bin/../staging_dir/target-${arch}_${libc}/root-*/");
62
63                 print $fh "set sysroot $sysroot\n" if $sysroot;
64                 my $cmd = "target remote";
65                 -f $ARGV[0] and $cmd = "core-file";
66                 print $fh "$cmd $ARGV[0]\n";
67
68                 my $file = -f "$sysroot/$ARGV[1]" ? "$sysroot/$ARGV[1]" : $ARGV[1];
69                 system($gdb, '-x', $fp, $file);
70
71                 close($fh);
72                 unlink($fp);
73         }
74         else
75         {
76                 print("No gdb found! Make sure that CONFIG_GDB is set!\n");
77                 exit(1);
78         }
79 }
80 else
81 {
82         print("No staging_dir found! You need to compile at least once!\n");
83         exit(1);
84 }