Unix Programming - Find out the process address space

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > Find out the process address space





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Find out the process address space
For-NG

2004-01-23, 5:01 pm

Hi there,

Is there a way to find out the process address space boundaries
(interms of addresses) programatically (in the user space only). Also
the start and end boundaries of different segments, if they can be
found out.

More precisely, I want to find out an authentic way by which I can
say a given address is invalid for this process (or for the current
process), and I get EFAULT kind of error if I use this address for
system calls like write,sigaction etc. Is there any existing library
routine exists which can do so. More wanted for Linux.

I hope I am clear in specifying my doubt.

TIA!
mjt

2004-01-23, 5:01 pm

On 29 Nov 2003 04:16:07 -0800, singhal_maneesh@yahoo.com (For-NG) wrote:

quote:

> Is there a way to find out



....hmmm. smells like homework ...
http://www.oreilly.com/catalog/linuxkernel2/
..
--
/// Michael J. Tobler: motorcyclist, surfer, skydiver, \\\
\\\ and author: "Inside Linux", "C++ HowTo", "C++ Unleashed" ///
"Life is like a bowl of soup with hairs floating on it.
You have to eat it nevertheless." - Flaubert
mjt

2004-01-23, 5:01 pm

On 29 Nov 2003 04:16:07 -0800, singhal_maneesh@yahoo.com (For-NG) wrote:

quote:

> Is there a way to find out



....hmmm. smells like homework ...
http://www.oreilly.com/catalog/linuxkernel2/
..
--
/// Michael J. Tobler: motorcyclist, surfer, skydiver, \\\
\\\ and author: "Inside Linux", "C++ HowTo", "C++ Unleashed" ///
"Life is like a bowl of soup with hairs floating on it.
You have to eat it nevertheless." - Flaubert
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5:01 pm

singhal_maneesh@yahoo.com (For-NG) writes:
quote:

> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.



No.
quote:

> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.



Read the address. If you get a SIGSEGV, it wasn't valid. Relying on
being able to determine the validity of an address is bad design.

--
Måns Rullgård
mru@kth.se
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5:01 pm

singhal_maneesh@yahoo.com (For-NG) writes:
quote:

> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.



No.
quote:

> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.



Read the address. If you get a SIGSEGV, it wasn't valid. Relying on
being able to determine the validity of an address is bad design.

--
Måns Rullgård
mru@kth.se
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5:01 pm

mjt <mjtobler@removethis_consultant.com> writes:
quote:

>
> ...hmmm. smells like homework ...



... or possibly an mswindows programmer. If it is homework, someone
should have a word with the teacher.

--
Måns Rullgård
mru@kth.se
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5:01 pm

mjt <mjtobler@removethis_consultant.com> writes:
quote:

>
> ...hmmm. smells like homework ...



... or possibly an mswindows programmer. If it is homework, someone
should have a word with the teacher.

--
Måns Rullgård
mru@kth.se
Andrew Gabriel

2004-01-23, 5:01 pm

In article <b0e1dd47.0311290416.10c8b8be@posting.google.com>,
singhal_maneesh@yahoo.com (For-NG) writes:
quote:

> Hi there,
>
> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.



On Solaris, you can use procfs (the /proc filesystem) to do this.
Not tried on Linux, but I imagine you could do the same, although
the code required is likely to be different.
quote:

> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.



Well, you found a way already -- try using the address with a system
call. A good system call for this purpose is mincore(2). Again, you
are not interested in knowing if a particular address is in or out
of 'core' (main store if you're a youngster;-), but you look to see
if you get EFAULT meaning the address you enquired about is invalid
in the process's address space. (Again, I'm not sure if Linux has
mincore(2) or not -- I'll leave you to check.)

--
Andrew Gabriel
Consultant Software Engineer
Andrew Gabriel

2004-01-23, 5:01 pm

In article <b0e1dd47.0311290416.10c8b8be@posting.google.com>,
singhal_maneesh@yahoo.com (For-NG) writes:
quote:

> Hi there,
>
> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.



On Solaris, you can use procfs (the /proc filesystem) to do this.
Not tried on Linux, but I imagine you could do the same, although
the code required is likely to be different.
quote:

> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.



Well, you found a way already -- try using the address with a system
call. A good system call for this purpose is mincore(2). Again, you
are not interested in knowing if a particular address is in or out
of 'core' (main store if you're a youngster;-), but you look to see
if you get EFAULT meaning the address you enquired about is invalid
in the process's address space. (Again, I'm not sure if Linux has
mincore(2) or not -- I'll leave you to check.)

--
Andrew Gabriel
Consultant Software Engineer
Paul Pluzhnikov

2004-01-23, 5:01 pm

mru@kth.se (Måns Rullgård) writes:
quote:

> Relying on
> being able to determine the validity of an address is bad design.



Not necessarily.

There are a few legitimate reasons why one would want to know. Such
as malloc debugger doing a memory sweep while searching for leaks.

In general, al kinds of "in-process debuggers" do care about address
space layout, and it's a pity Linux doesn't provide one.

In this thread another reason for "knowing" is given:
http://groups.google.com/groups?sel...%40BitWagon.com

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Casper H.S. Dik

2004-01-23, 5:01 pm

mru@kth.se (=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=) writes:
quote:

>singhal_maneesh@yahoo.com (For-NG) writes:


quote:

[QUOTE][color=darkred]
>No.



In some OSes they can be found out; mincore() can be used to
probe the whole address space (only doable in 32 bit mode).

On Solaris, /proc/self/map has the info you need.

Making the code that uses this portable is hard and it
seems to indicate that you want to do other unportable
things like "MAP_FIXED" of self-specified addresses.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Paul Pluzhnikov

2004-01-23, 5:01 pm

mru@kth.se (Måns Rullgård) writes:
quote:

> Relying on
> being able to determine the validity of an address is bad design.



Not necessarily.

There are a few legitimate reasons why one would want to know. Such
as malloc debugger doing a memory sweep while searching for leaks.

In general, al kinds of "in-process debuggers" do care about address
space layout, and it's a pity Linux doesn't provide one.

In this thread another reason for "knowing" is given:
http://groups.google.com/groups?sel...%40BitWagon.com

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Casper H.S. Dik

2004-01-23, 5:01 pm

mru@kth.se (=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=) writes:
quote:

>singhal_maneesh@yahoo.com (For-NG) writes:


quote:

[QUOTE][color=darkred]
>No.



In some OSes they can be found out; mincore() can be used to
probe the whole address space (only doable in 32 bit mode).

On Solaris, /proc/self/map has the info you need.

Making the code that uses this portable is hard and it
seems to indicate that you want to do other unportable
things like "MAP_FIXED" of self-specified addresses.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5:01 pm

Casper H.S. Dik <Casper.Dik@Sun.COM> writes:
quote:

>
>
> In some OSes they can be found out; mincore() can be used to
> probe the whole address space (only doable in 32 bit mode).
>
> On Solaris, /proc/self/map has the info you need.
>
> Making the code that uses this portable is hard and it
> seems to indicate that you want to do other unportable
> things like "MAP_FIXED" of self-specified addresses.



I should have said no simple, reliable way. It still remains true
that actually needing this kind of information is very rare.

--
Måns Rullgård
mru@kth.se
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5:01 pm

Casper H.S. Dik <Casper.Dik@Sun.COM> writes:
quote:

>
>
> In some OSes they can be found out; mincore() can be used to
> probe the whole address space (only doable in 32 bit mode).
>
> On Solaris, /proc/self/map has the info you need.
>
> Making the code that uses this portable is hard and it
> seems to indicate that you want to do other unportable
> things like "MAP_FIXED" of self-specified addresses.



I should have said no simple, reliable way. It still remains true
that actually needing this kind of information is very rare.

--
Måns Rullgård
mru@kth.se
David Schwartz

2004-01-23, 5:01 pm


"For-NG" <singhal_maneesh@yahoo.com> wrote in message
news:b0e1dd47.0311290416.10c8b8be@posting.google.com...
quote:

> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.


quote:

> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.


quote:

> I hope I am clear in specifying my doubt.



There are, in general, three types of ways you can find this information
out:

1) Read /proc or otherwise interrogate platform-specific information
about the memory map of the process.

2) Catch SIGSEGV and attempt to access the memory directly.
Unfortunately, how you clean up after the signal and return to code is
platform-specific.

3) Write the contents of the memory to /dev/null and see if you get back
EFAULT.

DS


David Schwartz

2004-01-23, 5:01 pm


"For-NG" <singhal_maneesh@yahoo.com> wrote in message
news:b0e1dd47.0311290416.10c8b8be@posting.google.com...
quote:

> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.


quote:

> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.


quote:

> I hope I am clear in specifying my doubt.



There are, in general, three types of ways you can find this information
out:

1) Read /proc or otherwise interrogate platform-specific information
about the memory map of the process.

2) Catch SIGSEGV and attempt to access the memory directly.
Unfortunately, how you clean up after the signal and return to code is
platform-specific.

3) Write the contents of the memory to /dev/null and see if you get back
EFAULT.

DS


inf

2004-01-23, 5:01 pm

For-NG wrote:
quote:

> Hi there,
>
> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.
>
> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.
>
> I hope I am clear in specifying my doubt.
>
> TIA!



hi just a newb here, but if you're on linux its just a matter of
reading the ELF format specification. all of the segment data is
contained in the object file in the program header table. all of the
virtual addresses for different segments will be contained in that
table. there's some code here on my site about 1/2 way down called
"Elf binary fuxxoring" that parses all the structures in an elf binary.
hope that helps.
-sean

inf

2004-01-23, 5:01 pm

For-NG wrote:
quote:

> Hi there,
>
> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.
>
> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.
>
> I hope I am clear in specifying my doubt.
>
> TIA!



hi just a newb here, but if you're on linux its just a matter of
reading the ELF format specification. all of the segment data is
contained in the object file in the program header table. all of the
virtual addresses for different segments will be contained in that
table. there's some code here on my site about 1/2 way down called
"Elf binary fuxxoring" that parses all the structures in an elf binary.
hope that helps.
-sean

inf

2004-01-23, 5:01 pm

For-NG wrote:
quote:

> Hi there,
>
> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.
>
> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.
>
> I hope I am clear in specifying my doubt.
>
> TIA!



hi just a newb here, but if you're on linux its just a matter of
reading the ELF format specification. all of the segment data is
contained in the object file in the program header table. all of the
virtual addresses for different segments will be contained in that
table. there's some code here on my site about 1/2 way down called
"Elf binary fuxxoring" that parses all the structures in an elf binary.
hope that helps. www.1nfamus.netfirms.com
-sean

inf

2004-01-23, 5:01 pm

For-NG wrote:
quote:

> Hi there,
>
> Is there a way to find out the process address space boundaries
> (interms of addresses) programatically (in the user space only). Also
> the start and end boundaries of different segments, if they can be
> found out.
>
> More precisely, I want to find out an authentic way by which I can
> say a given address is invalid for this process (or for the current
> process), and I get EFAULT kind of error if I use this address for
> system calls like write,sigaction etc. Is there any existing library
> routine exists which can do so. More wanted for Linux.
>
> I hope I am clear in specifying my doubt.
>
> TIA!



hi just a newb here, but if you're on linux its just a matter of
reading the ELF format specification. all of the segment data is
contained in the object file in the program header table. all of the
virtual addresses for different segments will be contained in that
table. there's some code here on my site about 1/2 way down called
"Elf binary fuxxoring" that parses all the structures in an elf binary.
hope that helps. www.1nfamus.netfirms.com
-sean

Casper H.S. Dik

2004-01-23, 5:01 pm

"David Schwartz" <davids@webmaster.com> writes:

quote:

> There are, in general, three types of ways you can find this information
>out:


quote:

> 1) Read /proc or otherwise interrogate platform-specific information
>about the memory map of the process.


quote:

> 2) Catch SIGSEGV and attempt to access the memory directly.
>Unfortunately, how you clean up after the signal and return to code is
>platform-specific.


quote:

> 3) Write the contents of the memory to /dev/null and see if you get back
>EFAULT.



You forget mincore(); it will return ENOMEM when you try to determine
the "incoreness" of a page which isn't mapped.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Casper H.S. Dik

2004-01-23, 5:01 pm

"David Schwartz" <davids@webmaster.com> writes:

quote:

> There are, in general, three types of ways you can find this information
>out:


quote:

> 1) Read /proc or otherwise interrogate platform-specific information
>about the memory map of the process.


quote:

> 2) Catch SIGSEGV and attempt to access the memory directly.
>Unfortunately, how you clean up after the signal and return to code is
>platform-specific.


quote:

> 3) Write the contents of the memory to /dev/null and see if you get back
>EFAULT.



You forget mincore(); it will return ENOMEM when you try to determine
the "incoreness" of a page which isn't mapped.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
David Schwartz

2004-01-23, 5:01 pm


"Casper H.S. Dik" <Casper.Dik@Sun.COM> wrote in message
news:3fc9ca03$0$1496$e4fe514c@news.xs4all.nl...
quote:

> You forget mincore(); it will return ENOMEM when you try to determine
> the "incoreness" of a page which isn't mapped.



I completely forgot about that!

DS


David Schwartz

2004-01-23, 5:01 pm


"Casper H.S. Dik" <Casper.Dik@Sun.COM> wrote in message
news:3fc9ca03$0$1496$e4fe514c@news.xs4all.nl...
quote:

> You forget mincore(); it will return ENOMEM when you try to determine
> the "incoreness" of a page which isn't mapped.



I completely forgot about that!

DS


Shaun Clowes

2004-01-23, 5:01 pm


"inf" <britney_spears@hotpop.com> wrote in message
news:3FC98F66.8050304@hotpop.com...
quote:

> For-NG wrote:
>
> hi just a newb here, but if you're on linux its just a matter of
> reading the ELF format specification. all of the segment data is
> contained in the object file in the program header table. all of the
> virtual addresses for different segments will be contained in that
> table. there's some code here on my site about 1/2 way down called
> "Elf binary fuxxoring" that parses all the structures in an elf binary.



Yeah, but that won't help with the stack, heap, anon pages etc. All the
other input in this thread should have helped, but it might just be easiest
to parse /proc/pid/maps

Cheers,
Shaun


Shaun Clowes

2004-01-23, 5:01 pm


"inf" <britney_spears@hotpop.com> wrote in message
news:3FC98F66.8050304@hotpop.com...
quote:

> For-NG wrote:
>
> hi just a newb here, but if you're on linux its just a matter of
> reading the ELF format specification. all of the segment data is
> contained in the object file in the program header table. all of the
> virtual addresses for different segments will be contained in that
> table. there's some code here on my site about 1/2 way down called
> "Elf binary fuxxoring" that parses all the structures in an elf binary.



Yeah, but that won't help with the stack, heap, anon pages etc. All the
other input in this thread should have helped, but it might just be easiest
to parse /proc/pid/maps

Cheers,
Shaun


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com