Filter out debug levels in debugging information displayed via debugfs.
[ksplice.git] / ksplice.pl.in
blob3b1d411b11e8612e22180e937def9dfb2c25a2fc
1 #!/usr/bin/perl
3 # Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
4 # Jeffrey Brian Arnold <jbarnold@mit.edu>,
5 # Tim Abbott <tabbott@mit.edu>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License, version 2.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
18 # 02110-1301, USA.
20 use strict;
21 use warnings;
22 use lib 'KSPLICE_DATA_DIR';
23 use ksplice;
24 use File::Copy;
25 use Digest::MD5;
27 sub empty_diff {
28 my ($out) = @_;
29 my ($obj) = $out =~ /^(.*)\.KSPLICE$/ or die;
30 unlink "$obj.KSPLICE_primary" if (-e "$obj.KSPLICE_primary");
31 unlink "$obj.KSPLICE_helper" if (-e "$obj.KSPLICE_helper");
32 open OUT, '>', "$out.tmp";
33 close OUT;
34 rename "$out.tmp", $out;
37 sub do_snap {
38 my ($out) = @_;
39 my ($obj) = $out =~ /^(.*)\.KSPLICE$/ or die;
40 die if (!-e $obj);
41 unlink "$obj.KSPLICE_pre" if (-e "$obj.KSPLICE_pre");
42 empty_diff($out);
45 sub do_diff {
46 my ($out) = @_;
47 my ($obj) = $out =~ /^(.*)\.KSPLICE$/ or die;
48 my $obj_old = "$obj.KSPLICE_pre";
49 return do_snap($out) if (!-e $obj_old);
50 die if (!-e $obj);
51 if (system('cmp', '-s', '--', $obj_old, $obj) == 0) {
52 unlink $obj_old;
53 return empty_diff($out);
56 my ($bits, $sections, $entrysyms) = split("\n", runsuc("objdiff", $obj_old, $obj));
57 die if ($bits ne '32' && $bits ne '64');
58 return empty_diff($out) if ($sections eq '' && $entrysyms eq '');
60 copy($obj, "$obj.KSPLICE_primary");
61 copy($obj_old, "$obj.KSPLICE_helper");
63 open OBJ, '<', $obj or die;
64 open OBJ_OLD, '<', $obj_old or die;
65 my $tag = Digest::MD5->new->addfile(*OBJ)->addfile(*OBJ_OLD)->hexdigest;
66 close OBJ;
67 close OBJ_OLD;
69 runsuc("objmanip", "$obj.KSPLICE_primary", "keep-primary", "____${tag}", "_post", split(/\s/, $sections));
70 runsuc("objmanip", "$obj.KSPLICE_helper", "keep-helper", "____${tag}", "_pre");
72 runsuc("objmanip", "$obj.KSPLICE_primary", "sizelist", "____${tag}", "_post");
73 runsuc("objmanip", "$obj.KSPLICE_helper", "sizelist", "____${tag}", "_pre");
75 runsuc("objmanip", "$obj.KSPLICE_primary", "patchlist", "____${tag}", "_pre", "_post", split(/\s/, $entrysyms));
77 open OUT, '>', "$out.tmp";
78 print OUT "$bits\n";
79 close OUT;
80 rename "$out.tmp", $out;
83 sub do_combine {
84 my ($out, @ins) = @_;
85 my @objs;
86 my $outbits = undef;
87 foreach my $in (@ins) {
88 next if (!-s $in);
89 my ($obj) = $in =~ /^(.*)\.KSPLICE$/ or die;
90 push @objs, $obj;
92 open IN, '<', $in;
94 chomp(my $bits = <IN>);
95 die if (defined $outbits && $outbits ne $bits);
96 $outbits = $bits;
98 close IN;
101 return empty_diff($out) unless (defined $outbits);
103 my ($obj) = $out =~ /^(.*)\.KSPLICE$/ or die;
104 if (@objs == 1) {
105 copy "$objs[0].KSPLICE_primary", "$obj.KSPLICE_primary";
106 copy "$objs[0].KSPLICE_helper", "$obj.KSPLICE_helper";
107 } else {
108 system("ld", "-r", "-o",
109 map { "$_.KSPLICE_primary" } ($obj, @objs));
110 system("ld", "-r", "-o",
111 map { "$_.KSPLICE_helper" } ($obj, @objs));
114 open OUT, '>', "$out.tmp";
115 print OUT "$outbits\n";
116 close OUT;
117 rename "$out.tmp", $out;
120 sub do_rmsyms {
121 my ($obj, @rmsyms) = @_;
122 my $relocs = runsuc("objmanip", $obj, "rmsyms", @rmsyms);
125 sub do_system_map_lookup {
126 my ($symarg) = @_;
127 open(SYMS, "<", "$ENV{KSPLICE_CONFIG_DIR}/System.map") or die;
128 my $line;
129 while(defined($line = <SYMS>)) {
130 my ($addr, $type, $sym, $mod) = split(/\s+/, $line);
131 if ($sym eq $symarg) { print $addr; last; }
133 close(SYMS);
136 my %handlers = (
137 'snap' => \&do_snap,
138 'diff' => \&do_diff,
139 'combine' => \&do_combine,
140 'rmsyms' => \&do_rmsyms,
141 'system_map_lookup' => \&do_system_map_lookup,
144 my ($cmd, @args) = @ARGV;
145 if (exists $handlers{$cmd}) {
146 my $handler = $handlers{$cmd};
147 &$handler(@args);
148 } else {
149 print "Usage: ksplice.pl ", join('|', keys %handlers), " ...\n";