OLD | NEW |
| (Empty) |
1 # Copyright © 2009-2010 Modestas Vainius <modax@debian.org> | |
2 # | |
3 # This program is free software; you can redistribute it and/or modify | |
4 # it under the terms of the GNU General Public License as published by | |
5 # the Free Software Foundation; either version 2 of the License, or | |
6 # (at your option) any later version. | |
7 # | |
8 # This program is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
13 # You should have received a copy of the GNU General Public License | |
14 # along with this program. If not, see <https://www.gnu.org/licenses/>. | |
15 | |
16 package Dpkg::Shlibs::Cppfilt; | |
17 | |
18 use strict; | |
19 use warnings; | |
20 | |
21 our $VERSION = '0.01'; | |
22 | |
23 use Exporter qw(import); | |
24 | |
25 use Dpkg::ErrorHandling; | |
26 use Dpkg::IPC; | |
27 use IO::Handle; | |
28 | |
29 our @EXPORT = qw(cppfilt_demangle_cpp); | |
30 our @EXPORT_OK = qw(cppfilt_demangle); | |
31 | |
32 # A hash of 'objects' referring to preforked c++filt processes for the distinct | |
33 # demangling types. | |
34 my %cppfilts; | |
35 | |
36 sub get_cppfilt { | |
37 my $type = shift || 'auto'; | |
38 | |
39 # Fork c++filt process for demangling $type unless it is forked already. | |
40 # Keeping c++filt running improves performance a lot. | |
41 my $filt; | |
42 if (exists $cppfilts{$type}) { | |
43 $filt = $cppfilts{$type}; | |
44 } else { | |
45 $filt = { from => undef, to => undef, | |
46 last_symbol => '', last_result => '' }; | |
47 $filt->{pid} = spawn(exec => [ 'c++filt', "--format=$type" ], | |
48 from_pipe => \$filt->{from}, | |
49 to_pipe => \$filt->{to}); | |
50 syserr(_g('unable to execute %s'), 'c++filt') | |
51 unless defined $filt->{from}; | |
52 $filt->{from}->autoflush(1); | |
53 | |
54 $cppfilts{$type} = $filt; | |
55 } | |
56 return $filt; | |
57 } | |
58 | |
59 # Demangle the given $symbol using demangler for the specified $type (defaults | |
60 # to 'auto') . Extraneous characters trailing after a mangled name are kept | |
61 # intact. If neither whole $symbol nor portion of it could be demangled, undef | |
62 # is returned. | |
63 sub cppfilt_demangle { | |
64 my ($symbol, $type) = @_; | |
65 | |
66 # Start or get c++filt 'object' for the requested type. | |
67 my $filt = get_cppfilt($type); | |
68 | |
69 # Remember the last result. Such a local optimization is cheap and useful | |
70 # when sequential pattern matching is performed. | |
71 if ($filt->{last_symbol} ne $symbol) { | |
72 # This write/read operation should not deadlock because c++filt flushes | |
73 # output buffer on LF or each invalid character. | |
74 print { $filt->{from} } $symbol, "\n"; | |
75 my $demangled = readline($filt->{to}); | |
76 chop $demangled; | |
77 | |
78 # If the symbol was not demangled, return undef | |
79 $demangled = undef if $symbol eq $demangled; | |
80 | |
81 # Remember the last result | |
82 $filt->{last_symbol} = $symbol; | |
83 $filt->{last_result} = $demangled; | |
84 } | |
85 return $filt->{last_result}; | |
86 } | |
87 | |
88 sub cppfilt_demangle_cpp { | |
89 my $symbol = shift; | |
90 return cppfilt_demangle($symbol, 'auto'); | |
91 } | |
92 | |
93 sub terminate_cppfilts { | |
94 foreach (keys %cppfilts) { | |
95 next if not defined $cppfilts{$_}{pid}; | |
96 close $cppfilts{$_}{from}; | |
97 close $cppfilts{$_}{to}; | |
98 wait_child($cppfilts{$_}{pid}, cmdline => 'c++filt', | |
99 nocheck => 1, | |
100 timeout => 5); | |
101 delete $cppfilts{$_}; | |
102 } | |
103 } | |
104 | |
105 # Close/terminate running c++filt process(es) | |
106 END { | |
107 # Make sure exitcode is not changed (by wait_child) | |
108 my $exitcode = $?; | |
109 terminate_cppfilts(); | |
110 $? = $exitcode; | |
111 } | |
112 | |
113 1; | |
OLD | NEW |