Cppfilt.pm 3.3 KB

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