Re: Reading memory of a different process
by Corion (Patriarch) on Jan 31, 2005 at 13:54 UTC
|
No, in general, it's not possible, because the memory address belongs to memory that does not belong to the current process (perl) and thus is not readable for Perl. You can try to get at a pointer to any arbitrary memory adress via Devel::Peek or Pointer, but the operating system will not let you see the memory of the other program, especially when the other program is not running anymore.
| [reply] |
|
|
is it possible if im running a c code witin perl file
| [reply] |
|
|
No, most likely not. You don't seem to understand how memory is allocated by modern operating systems - normally, no process (=program, for our purposes) is allowed to read the memory section of any other program, unless the other program explicitly opens up some memory to be read by other programs. So even if you start another program as a subprogram, you are unlikely to be allowed by your OS to read it. Also, at least with reasonably modern operating systems, memory addresses should be reused and should be local to every process, but I think that at least on the x86 platform, all 32-bit operating systems hand out a shared address space to all processes (but still with read/write protection).
If the program has stopped running already (like your C program after it prints the memory address), the memory of the program will be overwritten by other programs as the operating system gives the memory to other programs.
In any case, Perl is most likely not the tool you want to be using, whatever you want to do.
| [reply] |
Re: Reading memory of a different process
by Anonymous Monk on Jan 31, 2005 at 14:22 UTC
|
No, usually not. There are two things to prevent that. First, the address you get is an address that local to your process. You might run the process several times, and get the same address, while in physical memory, it's mapped to a different place each time (in fact, it's very likely it will be mapped to a different place, and during the lifetime of the process, the physical address might change (think swapping)). Secondly, your OS will not allow you to access the memory of another process directly.
That's not to say there it's entirely impossible. Most modern (Unix) OSses do have some API to access the memory. Either in its entire, like /dev/mem, or on a process by process bases using /proc/PID/mem.
Note that the typical way of sharing memory between processes involves setting up a shared memory segment. IPC::SysV might help you set it up. | [reply] |
|
|
| [reply] |
Re: Reading memory of a different process
by zentara (Cardinal) on Jan 31, 2005 at 14:10 UTC
|
You may be interested in Mmap question, where I seek knowledge about doing this thru IPC.
I'm not really a human, but I play one on earth.
flash japh
| [reply] |
Re: Reading memory of a different process
by rg0now (Chaplain) on Jan 31, 2005 at 21:52 UTC
|
This is mostly unrelated to Perl, but might I advise you to always avoid constructs like yours:
int *pointer;
*pointer = 12;
This is a pretty straight call for a segfault. You actually allocate a pointer and then write to the random address (junk) contained in that pointer.
That is the kind of problem I encounter more and more frequently when reviewing the C programs coded by my students, so we should not duplicate this wrong coding style here.
rgonow | [reply] [d/l] |
Re: Reading memory of a different process
by gellyfish (Monsignor) on Jan 31, 2005 at 13:56 UTC
|
Not directly from perl code no. You could however do so in an XS extension, though I'm not sure whether this would be a good idea or not. Of course if you have the pointer as output from another program - it is likely to be meaningless (and probably dabgerous to use) to a different process.
/J\
| [reply] |
Re: Reading memory of a different process
by ambrus (Abbot) on Jan 31, 2005 at 14:00 UTC
|
Yes, it is possible, but you do not want to do that.
$pointer_to_int = 0x8ffcfff;
$int = unpack("i!", unpack("p4", pack("l", $p))); # DO NOT DO THIS
| [reply] [d/l] |
Re: Reading memory of a different process
by ambrus (Abbot) on Jan 31, 2005 at 20:11 UTC
|
Let me confirm this.
Do you really want to access the memory of a different
process, as the new title suggests?
If so,
that's much more complicated and is even less
likely to be useful. Only debuggers do that.
I think it's possible with three ptrace and two wait* calls,
but I'm not sure in the details.
If you want to access the meory of the same process
(the process of the perl interpreter), that's simpler,
but it's probably still not what you want.
Do read the references tutorial
someone suggested, or this Q&A entry,
or perldoc perlref, or perldoc perlreftut or other things about
references.
| [reply] |