Cppfilt.pm 3.3 KB

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