diff options
author | Frediano Ziglio <fziglio@redhat.com> | 2016-09-16 20:57:00 +0100 |
---|---|---|
committer | Frediano Ziglio <fziglio@redhat.com> | 2016-09-16 20:57:00 +0100 |
commit | 6db4646fbe4a1118bb3cbe434f5aa3ac9399f6c0 (patch) | |
tree | 454318d8a7b4b5df8c8b6eadb99bf9aef75c04b7 | |
parent | 321e8461534a70f39af6cd84260b493e9ba8c611 (diff) |
Add script to detect unused functions
Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
-rwxr-xr-x | spice-server/unused.pl | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/spice-server/unused.pl b/spice-server/unused.pl new file mode 100755 index 0000000..c98bd1f --- /dev/null +++ b/spice-server/unused.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +# detect unused functions and +# functions which could be static + +use strict; + +my $verbose = 0; + +my %requested; +my %requested_externally; +my %exported; + +open(IN, '<server/spice-server.syms') or die; +my $in_global = 0; +while(<IN>) { + if (/global:/) { + $in_global = 1; + } elsif ($in_global) { + if (/}/) { + $in_global = 0; + } else { + s/;.*//s; + s/^\s+//s; + if (/^spice_/) { + $requested{$_} = 1; + $requested_externally{$_} = 1; + } + } + } +} +close(IN); + +sub process_obj($) { + my $fn = shift; + open(IN, "objdump -t $fn |") or die; + while (<IN>) { + # 0000000000006e87 g F .text 0000000000000114 .hidden spice_canvas_set_usr_data + # 0000000000000000 *UND* 0000000000000000 rop3_init + my @row = split(/\s+/); + if ($row[1] eq '*UND*') { + my $func = $row[-1]; + print "requested $func\n" if $verbose; + $requested{$func} = 1; + $requested_externally{$func} = 1; + } elsif ($row[1] eq 'g' && $row[3] eq '.text') { + my $func = $row[-1]; + print "exported $func\n" if $verbose; + $exported{$func} = 1; + } + } + close(IN); + + open(IN, "objdump -r $fn |") or die; + while (<IN>) { + # 0000000000000043 R_X86_64_PLT32 __asan_report_load4-0x0000000000000004 + if (/^[0-9a-f]+\s+R\S+\s+(.*?)[-+]0x/i) { + $requested{$1} = 1; + } + } + close(IN); + +} + +for my $fn (glob("server/.libs/*.o")) { + print "Parsing $fn\n" if $verbose; + process_obj($fn); +} + +print "Unused functions:\n"; +for my $func (sort keys %exported) { + if (!exists($requested{$func})) { + print "\t$func\n"; + } +} + +print "Can be static functions:\n"; +for my $func (sort keys %exported) { + if (exists($requested{$func}) && !exists($requested_externally{$func})) { + print "\t$func\n"; + } +} |