remote-gdb: add build_dir/target-*/debug-* to solib-search-path
[openwrt/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 <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-([^_/]+)_([^_/]+)\b! )
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-([^_]+)_([^_]+)/ )
30 {
31 push @arches, [ $1, $2 ];
32 printf(" %2d) %s (%s)\n", @arches + 0, $1, $2);
33 }
34 }
35
36 # Query arch
37 do {
38 print("Target? > ");
39 chomp($tid = <STDIN>);
40 } while( !defined($tid) || $tid !~ /^\d+$/ || $tid < 1 || $tid > @arches );
41
42 ($arch, $libc) = @{$arches[$tid-1]};
43 }
44
45 closedir SD;
46
47 # Find gdb
48 my ($gdb) = glob("$Bin/../build_dir/toolchain-${arch}_*_${libc}/gdb-*/gdb/gdb");
49
50 if( -x $gdb )
51 {
52 my ( $fh, $fp ) = tempfile();
53
54 # Find library paths
55 my $libdirs = join ':', (
56 glob("$Bin/../build_dir/target-${arch}_${libc}/debug-*/{usr/,}lib/"),
57 glob("$Bin/../staging_dir/target-${arch}_${libc}/{usr/,}lib"),
58 glob("$Bin/../staging_dir/toolchain-${arch}_*_${libc}/lib")
59 );
60
61 print $fh "set solib-search-path $libdirs\n";
62 print $fh "target remote $ARGV[0]\n";
63
64 system($gdb, '-x', $fp, $ARGV[1]);
65
66 close($fh);
67 unlink($fp);
68 }
69 else
70 {
71 print("No gdb found! Make sure that CONFIG_GDB is set!\n");
72 exit(1);
73 }
74 }
75 else
76 {
77 print("No staging_dir found! You need to compile at least once!\n");
78 exit(1);
79 }