-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.pl
More file actions
executable file
·101 lines (82 loc) · 2.82 KB
/
Copy pathdebug.pl
File metadata and controls
executable file
·101 lines (82 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd 'abs_path';
use File::Basename;
my $root = dirname(abs_path($0));
my $build_dir = "$root/build/examples";
my $source_dir = "$root/examples";
my $missing_executable_exit_code = 3;
sub resolve_executable_name {
my ($cmake_file) = @_;
return undef if !-f $cmake_file;
open(my $fh, '<', $cmake_file) or return undef;
local $/;
my $cmake = <$fh>;
close($fh);
my ($target_name) = $cmake =~ /add_executable\s*\(\s*([^\s\)]+)/s;
return undef if !$target_name;
my $output_name;
if ($cmake =~ /set_target_properties\s*\(\s*\Q$target_name\E\s+PROPERTIES\s+([^\)]*)\)/s) {
my $props = $1;
($output_name) = $props =~ /OUTPUT_NAME\s+"([^"]+)"/s;
}
return $output_name // $target_name;
}
sub should_use_build_asset_path {
my ($cmake_file) = @_;
return 0 if !-f $cmake_file;
open(my $fh, '<', $cmake_file) or return 0;
local $/;
my $cmake = <$fh>;
close($fh);
return $cmake =~ /ASSET_DIR\s*=\s*"\$<TARGET_FILE_DIR:[^>]+>"/s
|| $cmake =~ /ASSET_DIR\s*=\s*"\$\{CMAKE_CURRENT_BINARY_DIR\}"/s;
}
my $program = shift @ARGV;
if (!$program) {
print "Usage: ./debug.pl <program_name> [extra args...]\n\n";
print "Available programs:\n";
my %progs;
if (-d $build_dir) {
opendir(my $dh, $build_dir);
while (my $entry = readdir($dh)) {
next if $entry =~ /^\./;
$progs{$entry} = 1 if -d "$build_dir/$entry";
}
closedir($dh);
}
for my $p (sort keys %progs) {
print " $p\n";
}
my $counter = scalar(keys %progs);
print "$counter total program(s)\n";
exit 1;
}
my $program_name = basename($program);
my $data_path = "$source_dir/$program_name";
my $cmake_file = "$data_path/CMakeLists.txt";
if (!-f $cmake_file) {
die "Error: Could not find source CMake file for '$program_name' at $cmake_file\n";
}
my $exe_name = resolve_executable_name($cmake_file);
if (!$exe_name) {
die "Error: Could not resolve executable target from $cmake_file\n";
}
my $exe_path = "$build_dir/$program_name/$exe_name";
my $use_build_asset_path = should_use_build_asset_path($cmake_file);
if (!-x $exe_path) {
warn "Skipping '$program_name': could not find executable at $exe_path\n";
exit $missing_executable_exit_code;
}
my $exe_dir = dirname($exe_path);
my $resolved_exe_name = basename($exe_path);
my $runtime_path = $use_build_asset_path ? $exe_dir : $data_path;
chdir($exe_dir) or die "Cannot cd to $exe_dir: $!\n";
if (!-d $runtime_path) {
warn "Warning: Data directory '$runtime_path' not found.\n";
}
my @cmd = ("./$resolved_exe_name", "-p", $runtime_path, @ARGV);
my @gdb_cmd = ("gdb", "-q", "-ex", "set confirm off", "-ex", "run", "--args", @cmd);
print ">> Executing: @gdb_cmd\n";
exec(@gdb_cmd) or die "Failed to exec gdb: $!\n";