| 1 | # Copyright © 2009-2010 Modestas Vainius <modax@debian.org> | ||||||||||||||
| 2 | # Copyright © 2010, 2012-2015 Guillem Jover <guillem@debian.org> | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||||||||||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||
| 7 | # (at your option) any later version. | ||||||||||||||
| 8 | # | ||||||||||||||
| 9 | # This program is distributed in the hope that it will be useful, | ||||||||||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||
| 12 | # GNU General Public License for more details. | ||||||||||||||
| 13 | # | ||||||||||||||
| 14 | # You should have received a copy of the GNU General Public License | ||||||||||||||
| 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =encoding utf8 | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 NAME | ||||||||||||||
| 20 | |||||||||||||||
| 21 | Dpkg::Shlibs::Cppfilt - C++ symbol mangling support via c++filt | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||
| 24 | |||||||||||||||
| 25 | This module provides functions that wrap over c++filt for its easy and | ||||||||||||||
| 26 | fast usage. | ||||||||||||||
| 27 | |||||||||||||||
| 28 | B<Note>: This is a private module, its API can change at any time. | ||||||||||||||
| 29 | |||||||||||||||
| 30 | =cut | ||||||||||||||
| 31 | |||||||||||||||
| 32 | package Dpkg::Shlibs::Cppfilt 0.01; | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 4 | ▶ | use v5.36; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | 4 | our @EXPORT = qw( | |||||||||||||
| 37 | cppfilt_demangle_cpp | ||||||||||||||
| 38 | ); | ||||||||||||||
| 39 | 4 | our @EXPORT_OK = qw( | |||||||||||||
| 40 | cppfilt_demangle | ||||||||||||||
| 41 | ); | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 4 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 4 | ▶ | use Dpkg::ErrorHandling; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | 4 | ▶ | use Dpkg::IPC; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | |||||||||||||||
| 48 | # A hash of 'objects' referring to preforked c++filt processes for the distinct | ||||||||||||||
| 49 | # demangling types. | ||||||||||||||
| 50 | 4 | my %cppfilts; | |||||||||||||
| 51 | |||||||||||||||
| 52 | 605 | ▶ | sub get_cppfilt { | ||||||||||||
|
Subroutine
get_cppfilt: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 53 | 605 | ▶ | my $type = shift || 'auto'; | ||||||||||||
|
Condition50%1/2
shift() || 'auto'
MC/DC0%0/1
shift() || 'auto'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||
| 54 | |||||||||||||||
| 55 | # Fork c++filt process for demangling $type unless it is forked already. | ||||||||||||||
| 56 | # Keeping c++filt running improves performance a lot. | ||||||||||||||
| 57 | 605 | my $filt; | |||||||||||||
| 58 | 605 | ▶ | if (exists $cppfilts{$type}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 601 | $filt = $cppfilts{$type}; | |||||||||||||
| 60 | } else { | ||||||||||||||
| 61 | 4 | $filt = { | |||||||||||||
| 62 | from => undef, | ||||||||||||||
| 63 | to => undef, | ||||||||||||||
| 64 | last_symbol => '', | ||||||||||||||
| 65 | last_result => '', | ||||||||||||||
| 66 | }; | ||||||||||||||
| 67 | $filt->{pid} = spawn( | ||||||||||||||
| 68 | exec => [ 'c++filt', "--format=$type" ], | ||||||||||||||
| 69 | from_pipe => \$filt->{from}, | ||||||||||||||
| 70 | to_pipe => \$filt->{to}, | ||||||||||||||
| 71 | 4 | ); | |||||||||||||
| 72 | syserr(g_('cannot execute %s'), 'c++filt') | ||||||||||||||
| 73 | 2 | ▶ | unless defined $filt->{from}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 2 | $filt->{from}->autoflush(1); | |||||||||||||
| 75 | |||||||||||||||
| 76 | 2 | $cppfilts{$type} = $filt; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | 603 | return $filt; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | # Demangle the given $symbol using demangler for the specified $type (defaults | ||||||||||||||
| 82 | # to 'auto') . Extraneous characters trailing after a mangled name are kept | ||||||||||||||
| 83 | # intact. If neither whole $symbol nor portion of it could be demangled, undef | ||||||||||||||
| 84 | # is returned. | ||||||||||||||
| 85 | 605 | ▶ | sub cppfilt_demangle { | ||||||||||||
|
Subroutine
cppfilt_demangle: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 86 | 605 | my ($symbol, $type) = @_; | |||||||||||||
| 87 | |||||||||||||||
| 88 | # Start or get c++filt 'object' for the requested type. | ||||||||||||||
| 89 | 605 | my $filt = get_cppfilt($type); | |||||||||||||
| 90 | |||||||||||||||
| 91 | # Remember the last result. Such a local optimization is cheap and useful | ||||||||||||||
| 92 | # when sequential pattern matching is performed. | ||||||||||||||
| 93 | 603 | ▶ | if ($filt->{last_symbol} ne $symbol) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | # This write/read operation should not deadlock because c++filt flushes | ||||||||||||||
| 95 | # output buffer on LF or each invalid character. | ||||||||||||||
| 96 | 336 | print { $filt->{from} } $symbol, "\n"; | |||||||||||||
| 97 | 336 | my $demangled = readline($filt->{to}); | |||||||||||||
| 98 | 336 | chop $demangled; | |||||||||||||
| 99 | |||||||||||||||
| 100 | # If the symbol was not demangled, return undef. Otherwise normalize | ||||||||||||||
| 101 | # it as llvm packs ending angle brackets with no intermediate spaces | ||||||||||||||
| 102 | # as allowed by C++11, contrary to GNU binutils. | ||||||||||||||
| 103 | 336 | ▶ | if ($symbol eq $demangled) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 104 | 50 | $demangled = undef; | |||||||||||||
| 105 | } elsif ($demangled =~ m{operator>>}) { | ||||||||||||||
| 106 | # Special case operator>> and operator>>=. | ||||||||||||||
| 107 | } else { | ||||||||||||||
| 108 | 256 | $demangled =~ s{(?<=>)(?=>)}{ }g; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | # Remember the last result. | ||||||||||||||
| 112 | 336 | $filt->{last_symbol} = $symbol; | |||||||||||||
| 113 | 336 | $filt->{last_result} = $demangled; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | 603 | return $filt->{last_result}; | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | 605 | ▶ | sub cppfilt_demangle_cpp { | ||||||||||||
|
Subroutine
cppfilt_demangle_cpp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 119 | 605 | my $symbol = shift; | |||||||||||||
| 120 | 605 | return cppfilt_demangle($symbol, 'auto'); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 2 | ▶ | sub terminate_cppfilts { | ||||||||||||
|
Subroutine
terminate_cppfilts: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 124 | 2 | foreach my $type (keys %cppfilts) { | |||||||||||||
| 125 | 2 | ▶ | next if not defined $cppfilts{$type}{pid}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 2 | close $cppfilts{$type}{from}; | |||||||||||||
| 127 | 2 | close $cppfilts{$type}{to}; | |||||||||||||
| 128 | wait_child($cppfilts{$type}{pid}, | ||||||||||||||
| 129 | 2 | cmdline => 'c++filt', | |||||||||||||
| 130 | no_check => 1, | ||||||||||||||
| 131 | timeout => 5, | ||||||||||||||
| 132 | ); | ||||||||||||||
| 133 | 2 | delete $cppfilts{$type}; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | # Close/terminate running c++filt process(es). | ||||||||||||||
| 138 | 1 | ▶ | END { | ||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||
| 139 | # Make sure exitcode is not changed (by wait_child). | ||||||||||||||
| 140 | 1 | my $exitcode = $?; | |||||||||||||
| 141 | 1 | terminate_cppfilts(); | |||||||||||||
| 142 | 1 | $? = $exitcode; | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | =head1 CHANGES | ||||||||||||||
| 146 | |||||||||||||||
| 147 | =head2 Version 0.xx | ||||||||||||||
| 148 | |||||||||||||||
| 149 | This is a private module. | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =cut | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 4 | 1; | |||||||||||||