Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

[B::Deparse] Trying to find sub's code

by Gangabass (Vicar)
on Jul 16, 2012 at 05:19 UTC ( #981958=perlquestion: print w/replies, xml ) Need Help??
Gangabass has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks.

This is first time I'm using B::Deparse so I need some help with it.

Here is code I have (I just want to check code of the $xs):

package WSD::Filter; use 5.006000; use Config; our $VERSION = '1.0'; use FindBin qw($Bin); use B::Deparse; require DynaLoader; @ISA = qw(DynaLoader); my $perlver = '508'; $perlver = '510' if($] >= 5.01); $perlver = '512' if($] >= 5.012); $perlver = '514' if($] >= 5.014); my %v0= (32=>[ '32','32_3', '32_4','32_5', '32_2'], 64=>['64_2', '64_ +3', '64_4', '64']); my $bits = 32; $bits = 64 if($Config{'archname'} =~ /x86_64/ || $Config{'archname'} = +~ /amd64/); my $libref = undef; my $file_version; my @v = @{$v0{$bits}}; while(@v && !$libref) { my $bit = shift @v; $file_version = $perlver.$bit; if (-e "$Bin/Filter$") { $libref = DynaLoader::dl_load_file("$Bin/Filter$file_version.s +o"); sleep 1; } } my $symref = DynaLoader::dl_find_symbol($libref, 'boot_WSD__Filter'); my $xs = DynaLoader::dl_install_xsub('WSD::Filter::bootstrap', $symref +); #this is my first time I'm using B::Deparse... #what I'm doing wrong here? my $deparse = B::Deparse->new(); my $body = $deparse->coderef2text($xs); print $body; ########## &$xs('WSD::Filter'); 1;

But I have only ";" as output :-(. What's wrong with my approach?c

Replies are listed 'Best First'.
Re: [B::Deparse] Trying to find sub's code
by chromatic (Archbishop) on Jul 16, 2012 at 06:21 UTC
Re: [B::Deparse] Trying to find sub's code
by cavac (Deacon) on Jul 16, 2012 at 06:42 UTC

    As others already said, XS is compiled C code.

    There are a few options, depending on what exactly you mean by "checking the code" (I'm assuming Linux here, although most/all of this should also work on Windows with the correct tools installed):

    • strings shows you all the assumed plain text in the file.
    • objdump let's you dump all kind of stuff from a binary or library.
    • You might need a decompiler, for example Boomerang.
    • If you got the money, go for Hex-Rays.
    "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"
Re: [B::Deparse] Trying to find sub's code
by bulk88 (Priest) on Jul 16, 2012 at 06:30 UTC
    Here is my favorite typical way of decompiling perl.
    use Data::Dumper; use B::Deparse; use Devel::Size qw(size total_size); use B::Concise; use Devel::Peek; my $deparse = B::Deparse->new(); print $deparse->coderef2text(*Win32::LoadLibrary{CODE}); print "\n\n".size(\&Win32::LoadLibrary )."\n\n"; my $walker = B::Concise::compile('-src','-exec',*Win32::LoadLibrary); $walker->(); Dump(*Win32::LoadLibrary{CODE});
    ; 247 *Win32::LoadLibrary: *Win32::LoadLibrary is XS code SV = IV(0xcbaf78) at 0xcbaf7c REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0xc45b3c SV = PVCV(0xc12a3c) at 0xc45b3c REFCNT = 2 FLAGS = () COMP_STASH = 0x0 XSUB = 0x3a2528 XSUBANY = 3 GVGV::GV = 0xc45b2c "Win32" :: "LoadLibrary" FILE = "Win32.xs" DEPTH = 0 FLAGS = 0x800 OUTSIDE_SEQ = 0 PADLIST = 0x0 OUTSIDE = 0x0 (null)
    Deparse can't decompile C for you. Only pure perl functions. Try Ollydbg (shareware) or pay for Ida Pro if you need to decompile machine code.

    update: WSD::Filter is a proprietary not-FOSS XS module with a EULA for an Apache webserver. To OP, either buy the license, or buy Ida Pro, or both (if you want to make sure your binary blob isn't a rootkit).

    update: since you use linux, I strongly suggest not investing in a copy of Ida Pro since Ida Pro only understands MS symbol files, not GCC symbol data.
Re: [B::Deparse] Trying to find sub's code
by Anonymous Monk on Jul 16, 2012 at 06:21 UTC

    But I have only ";" as output :-(. What's wrong with my approach?

    XS isn't bytecode, it isn't parsed or compiled by perl, so there is nothing to be deparsed

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981958]
Approved by Marshall
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2017-05-27 14:32 GMT
Find Nodes?
    Voting Booth?