Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Dynamic linker

by zamanfou (Initiate)
on Jul 14, 2008 at 14:44 UTC ( #697487=perlquestion: print w/replies, xml ) Need Help??
zamanfou has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!

I want to ask you, if its possible to access a process at runtime, get/set status and other useful informations.

One more question. LD_PRELOAD instructs the loader to load additional libraries into a program, how could that be possible using perl?


Replies are listed 'Best First'.
Re: Dynamic linker
by pc88mxer (Vicar) on Jul 14, 2008 at 14:57 UTC
    What do you mean by "get/set status"? Do you mean the exit status of a child process? If so, the answer is $?.

    As for using LD_PRELOAD, using it works just fine with perl. For instance, see this example. Note, however, that depending on how it was built, perl may use it's own libraries for some things (like I/O) instead of the "standard" ones.

      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.


        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 ""):

        *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 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).

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

Re: Dynamic linker
by jethro (Monsignor) on Jul 14, 2008 at 16:24 UTC
    On linux you can look up the processes in /proc and extract the information you usually get with ps or top
      Before dealing with /proc manually, I'd take a look at Proc-ProcessTable, which works on GNU/Linux and several Unices too.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://697487]
Approved by pc88mxer
[james28909]: how can i write a while loop on one line (in a script) something like print "this\n" && print "that" && <DATA> && print the other\n" && next if /this matches/ while <DATA>?
[james28909]: i have tried with && and and and have tried to rearrange the functions as well to no avail
[shmem]: use parens to group expressions
[shmem]: you can't use next in a statement modifier
[shmem]: next requires a BLOCK

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-05-20 18:44 GMT
Find Nodes?
    Voting Booth?