[haiku-3rdparty-dev] Re: Getting stack Top/Bottom of running application

  • From: Ingo Weinhold <ingo_weinhold@xxxxxx>
  • To: haiku-3rdparty-dev@xxxxxxxxxxxxx
  • Date: Wed, 25 Nov 2009 10:42:11 +0100

On 2009-11-24 at 22:36:58 [+0100], Tomas Wilhelmsson 
<tomas.wilhelmsson@xxxxxxxxx> wrote:
> > Again, define what you need exactly. Otherwise no-one can help you.
> >
> Ok The grabage collector in D needs the stack top pointer and the stack
> bottom pointer of the currently running application to work.
> 
> for example in Win32 the stack bottom is taken as so:
> 
> void *rt_stackBottm() {
>         void* bottom;
>         asm
>         {
>             mov EAX, FS:4;
>             mov bottom, EAX;
>         }
>         return bottom;
> }

Mmh, no idea what windows stores in FS.

> in linux it is:
> 
> void *rt_stackBottm() {
>             static void** libc_stack_end;
> 
>             if( libc_stack_end == libc_stack_end.init )
>             {
>                 void* handle = dlopen( null, RTLD_NOW );
>                 libc_stack_end = cast(void**) dlsym( handle,
> "__libc_stack_end" );
>                 dlclose( handle );
>             }
>            return *libc_stack_end;
> }

Google tells me that __libc_stack_end is set to the highest process stack 
address at program start. Not sure whether that is the last usable address or 
the address after the last usable one. I'm also not sure what this means in 
the context of a multithreaded program.

> what would be the equalent of these two in Haiku ?

thread_info::stack_end is the first address after the end of a thread's 
stack. So you probably want to use it somehow. Without an exact specification 
what rt_stackBottm() is supposed to return, it's not possible to say how 
exactly.

> And for the data segment start/end also needed by the GC for cleanups
> 
> The Data_Start should point to the data segment in the memory where the file
> was loaded i guess and
> Data_End should point to where it ends.
> 
> the alias keyword is like the #define in C, just defines Data_Start/Data_End
> to _data_start__ and _bss_end__
> 
> 
> also here for example in win32 they do:
> 
>         extern (C)
>         {
>             extern int _data_start__;
>             extern int _bss_end__;
>         }
> 
>         alias _data_start__ Data_Start;
>         alias _bss_end__    Data_End;
> 
> and in linux:
> 
>         extern (C)
>         {
>             extern int _data;
>             extern int __data_start;
>             extern int _end;
>             extern int _data_start__;
>             extern int _data_end__;
>             extern int _bss_start__;
>             extern int _bss_end__;
>             extern int __fini_array_end;
>         }
> 
>         alias __data_start  Data_Start;
>         alias _end          Data_End;
> 
> and also here, what would be the equalent to do in Haiku to get where the
> data segment starts/ends of the currently running application =)

Assuming that is supposed to mean the data segment of the loaded program's 
executable (as written before, each shared object has a data segment). You 
can get the image_info for the executable by iterating through the team's 
image via get_next_image_next() and finding the one that has type 
B_APP_IMAGE. The image_info::data and image_info::size describe the data 
segment.

CU, Ingo

Other related posts: