-
Notifications
You must be signed in to change notification settings - Fork 4
/
stack.pl
executable file
·145 lines (130 loc) · 3.85 KB
/
stack.pl
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /usr/bin/env perl
# Roughly computes stack usage.
# Works only on some x86 .s files.
# Produces a .dot file with results.
use strict;
use warnings;
use Data::Dumper;
use Carp;
sub file_contents($)
{
my ($fn) = @_;
open(my $f, '<', $fn) or die "ERROR: $fn: open: $!\n";
return <$f>;
}
sub add_stack($$);
sub add_stack($$)
{
my ($funs, $fun) = @_;
my $f = $funs->{$fun} or die;
$f->{rec} //= 0;
return if $f->{rec} == 2;
die "Error: Recursion detected in '$fun'\n" if $f->{rec} == 1;
croak if $f->{rec};
$f->{rec} = 1;
# first add up called functions:
for my $other ((keys %{ $f->{call} }), (keys %{ $f->{jmp} })) {
add_stack($funs, $other);
}
# get max of all tail called functions:
$f->{stack}{sum} = $f->{stack}{single};
$f->{stack}{sum_src} = { $fun => 1 };
for my $other (keys %{ $f->{jmp} }) {
my $g = $funs->{$other};
my $g_sum = $g->{stack}{sum} or die "$fun: ERROR: No stack usage for '$other'\n";
if ($g_sum > $f->{stack}{sum}) {
$f->{stack}{sum} = $g_sum;
$f->{stack}{sum_src} = { $other => 1 };
}
}
for my $other (keys %{ $f->{call} }) {
my $g = $funs->{$other};
my $g_sum = $g->{stack}{sum} or die "$fun: ERROR: No stack usage for '$other'\n";
$g_sum += $f->{stack}{single};
if ($g_sum > $f->{stack}{sum}) {
$f->{stack}{sum} = $g_sum;
$f->{stack}{sum_src} = { $fun => 1, $other => 1 };
}
elsif ($g_sum == $f->{stack}{sum}) {
$f->{stack}{sum_src}{$other} = 1;
}
}
$f->{rec} = 2;
}
my ($in_s, $in_su) = @ARGV;
die "Usage: stack.pl FILE.s [FILE.su]\n" unless defined $in_s;
if (!defined($in_su)) {
$in_su = $in_s;
$in_su =~ s/[.]s$//g;
$in_su .= ".su";
}
my $out_dot = $in_s;
$out_dot =~ s/[.]s$//g;
$out_dot .= ".dot";
my %fun = ();
for my $l (file_contents $in_su) {
if ($l =~ /^.*:(\S+)\s+(\d+)\s+\S+\n/) {
my ($fun, $usage) = ($1,$2);
$fun{$fun}{stack}{single} = $usage;
}
}
my %possible_call = ();
my $cur = undef;
for my $l (file_contents $in_s) {
if ($l =~ /^\s*[.]type\s+([.a-zA-Z0-9_]+),\s*\@function\s*$/) {
my ($fun) = ($1);
print "fun: $fun\n";
$fun{$fun} //= {};
}
if ($l =~ /^\s*[.]cfi_endproc/) {
$cur = undef;
}
if ($l =~ /^([.a-zA-Z0-9_]+):/) {
my ($fun) = ($1);
if (my $f = $fun{$fun}) {
$cur = $f;
}
}
if ($cur) {
if ($l =~ /^\s*(call|jmp)\s*([.a-zA-Z0-9_]+)\s*$/) {
my ($what, $other) = ($1,$2);
if ($fun{$other}) {
$cur->{$what}{$other} = 1;
}
}
elsif ($l =~ /^\s*(call|jmp)\s+[*].*\%/) {
my ($what) = ($1);
for my $other (keys %possible_call) {
if ($fun{$other}) {
$cur->{$what}{$other} = 1;
}
}
%possible_call = ();
}
elsif ($l =~ /possible_call:\s*\"?([a-z_0-9]+)/) {
$possible_call{$1} = 1;
}
}
}
for my $fun (keys %fun) {
add_stack(\%fun, $fun);
}
open(my $h, '>', $out_dot) or die "ERROR: $out_dot: open: $!\n";
print {$h} "digraph x {\n";
for my $fun (sort keys %fun) {
my $f = $fun{$fun};
print {$h} "\"$fun\" [label=\"$f->{stack}{single}\\n$fun\\n$f->{stack}{sum}\"];\n";
for my $other (sort keys %{ $f->{call} }) {
my $bold = $f->{stack}{sum_src}{ $other };
my $bold_str = $bold ? ",style=bold" : ",style=dashed";
print {$h} "\"$fun\" -> \"$other\" [color=red$bold_str];\n";
}
for my $other (sort keys %{ $f->{jmp} }) {
my $bold = $f->{stack}{sum_src}{ $other };
my $bold_str = $bold ? ",style=bold" : ",style=dashed";
print {$h} "\"$fun\" -> \"$other\" [color=black$bold_str];\n";
}
}
print {$h} "}\n";
close $h;
0;