Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Dynamic linker

by zamanfou (Initiate)
on Jul 14, 2008 at 19:15 UTC ( [id://697543]=note: print w/replies, xml ) Need Help??


in reply to Re: Dynamic linker
in thread Dynamic linker

Yes, this is what I mean :)
I would like to know how to do this:

#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

pid_t getpid(void)
{
printf("Hello, world!\n");
return syscall(SYS_getpid);
}

in Perl.

-zmf

Replies are listed 'Best First'.
Re^3: Dynamic linker
by almut (Canon) on Jul 14, 2008 at 22:39 UTC

    It's still not entirely clear to me what you want to achieve.

    In case the idea is to override a Perl builtin (kind of analogously to what you could use LD_PRELOAD for with C libs), you could use CORE::GLOBAL, e.g. like this (I'm using getppid() in the example, as Perl has no builtin named getpid() ):

    #!/usr/bin/perl BEGIN { *CORE::GLOBAL::getppid = sub () { return "Hello world!\n" . CORE::getppid(); } } print getppid();

    (but note that you can only override builtins whose effective prototype can be expressed as a Perl prototype — print() for example cannot be overridden due to its print FH $something, ... syntax (i.e. no comma after FH) )

    When you put the override code into a module (say "mygetppid.pm"):

    *CORE::GLOBAL::getppid = sub () { return "Hello world!\n" . CORE::getppid(); }; 1;

    you could then write

    $ perl -Mmygetppid -e "print getppid()" Hello world! 23726

    where the -Mmygetppid is sematically similar to saying something like LD_PRELOAD=mygetppid.so in order to override some function call made from a given C binary.

    OTOH, if the idea is to use the regular LD_PRELOAD mechanism to override some C library function which is being called under the hood by perl, you can do it just the same way you would if that library function was being called by some other binary. For example, Re: mocking or trapping system calls shows how to override the C lib execvp() call (invoked from perl).

Re^3: Dynamic linker
by chromatic (Archbishop) on Jul 14, 2008 at 19:46 UTC

    Are you looking for perldoc -f syscall (or syscall)?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://697543]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2024-04-23 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found