Cppfilt.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <http://www.gnu.org/licenses/>.
  15. package Dpkg::Shlibs::Cppfilt;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. use base 'Exporter';
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::IPC;
  22. use IO::Handle;
  23. our @EXPORT = qw(cppfilt_demangle_cpp);
  24. our @EXPORT_OK = qw(cppfilt_demangle);
  25. # A hash of 'objects' referring to preforked c++filt processes for the distinct
  26. # demangling types.
  27. my %cppfilts;
  28. sub get_cppfilt {
  29. my $type = shift || 'auto';
  30. # Fork c++filt process for demangling $type unless it is forked already.
  31. # Keeping c++filt running improves performance a lot.
  32. my $filt;
  33. if (exists $cppfilts{$type}) {
  34. $filt = $cppfilts{$type};
  35. } else {
  36. $filt = { from => undef, to => undef,
  37. last_symbol => '', last_result => '' };
  38. $filt->{pid} = spawn(exec => [ 'c++filt', "--format=$type" ],
  39. from_pipe => \$filt->{from},
  40. to_pipe => \$filt->{to});
  41. internerr(_g('unable to execute %s'), 'c++filt')
  42. unless defined $filt->{from};
  43. $filt->{from}->autoflush(1);
  44. $cppfilts{$type} = $filt;
  45. }
  46. return $filt;
  47. }
  48. # Demangle the given $symbol using demangler for the specified $type (defaults
  49. # to 'auto') . Extraneous characters trailing after a mangled name are kept
  50. # intact. If neither whole $symbol nor portion of it could be demangled, undef
  51. # is returned.
  52. sub cppfilt_demangle {
  53. my ($symbol, $type) = @_;
  54. # Start or get c++filt 'object' for the requested type.
  55. my $filt = get_cppfilt($type);
  56. # Remember the last result. Such a local optimization is cheap and useful
  57. # when sequential pattern matching is performed.
  58. if ($filt->{last_symbol} ne $symbol) {
  59. # This write/read operation should not deadlock because c++filt flushes
  60. # output buffer on LF or each invalid character.
  61. print { $filt->{from} } $symbol, "\n";
  62. my $demangled = readline($filt->{to});
  63. chop $demangled;
  64. # If the symbol was not demangled, return undef
  65. $demangled = undef if $symbol eq $demangled;
  66. # Remember the last result
  67. $filt->{last_symbol} = $symbol;
  68. $filt->{last_result} = $demangled;
  69. }
  70. return $filt->{last_result};
  71. }
  72. sub cppfilt_demangle_cpp {
  73. my $symbol = shift;
  74. return cppfilt_demangle($symbol, 'auto');
  75. }
  76. sub terminate_cppfilts {
  77. foreach (keys %cppfilts) {
  78. next if not defined $cppfilts{$_}{pid};
  79. close $cppfilts{$_}{from};
  80. close $cppfilts{$_}{to};
  81. wait_child($cppfilts{$_}{pid}, cmdline => 'c++filt',
  82. nocheck => 1,
  83. timeout => 5);
  84. delete $cppfilts{$_};
  85. }
  86. }
  87. # Close/terminate running c++filt process(es)
  88. END {
  89. # Make sure exitcode is not changed (by wait_child)
  90. my $exitcode = $?;
  91. terminate_cppfilts();
  92. $? = $exitcode;
  93. }
  94. 1;