Cppfilt.pm 3.4 KB

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