public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: java memory allocation with gcj
       [not found] <704401.71578.qm@web56008.mail.re3.yahoo.com>
@ 2009-11-05 12:13 ` Andrew Haley
  0 siblings, 0 replies; 9+ messages in thread
From: Andrew Haley @ 2009-11-05 12:13 UTC (permalink / raw)
  To: java

isuru herath wrote:
> Hi Andrew,
> 
> Thanks for the reply. So I was thinking soon after the call to 
> _Jv_NewPrimArray the eax register should have the starting location of the 
> the memory allocated by the mmap. But the value at the eax register was 
> 166816.

So, if you're in gdb, try

   x $eax

166816 doesn't seem unreasonable.  It can be a valid address.

> When I tried this with C, the value at the eax register after the 
> malloc call was 155648008. I also put a printf(&name) in the C code and it 
> prints the same address.
> Then I tried store a value in the Java array and then tried to read the 
> memory address 166816 and it wasnot giving me the value that I stored in my 
> program. 

I think I need to see a test case.  Please, I need the code, and the steps
you took.

> Even the memory addresses given from C and java are different in sizes as well.
> 
> Am I missing something here. I just need to know how to find the address of 
> the block allocated by the "new" when running as native binary.
> 
> any help/advice on this regard is greatly appreciated.

Hard to say.  I know you must be doing something wrong, but I don't
know what.  If you can send me a test case that illustrates what
you're doing, I'll have a look.

Andrew.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: java memory allocation with gcj
  2009-11-06 16:43       ` Bryce McKinlay
@ 2009-11-06 20:06         ` isuru herath
  0 siblings, 0 replies; 9+ messages in thread
From: isuru herath @ 2009-11-06 20:06 UTC (permalink / raw)
  To: Bryce McKinlay; +Cc: java

Hi Bryce,

Thanks a lot for the mail. Ya I noticed that too. When I tried to store 
data in the first location of the array, in the assembly it was shown as 
the (base+8)th location which seems like 8 byte space for the header info. 
Thanks for the clarification.

regards,
Isuru

--- On Fri, 11/6/09, Bryce McKinlay <bmckinlay@gmail.com> wrote:

> From: Bryce McKinlay <bmckinlay@gmail.com>
> Subject: Re: java memory allocation with gcj
> To: "isuru herath" <isuru81@yahoo.com>
> Cc: "Andrew Haley" <aph@redhat.com>, java@gcc.gnu.org
> Date: Friday, November 6, 2009, 8:43 AM
> On Fri, Nov 6, 2009 at 4:02 PM, isuru
> herath <isuru81@yahoo.com>
> wrote:
> 
> > You were right. The memory address I am getting was
> correct eventhough it is
> > a small number compared to the one I got in C. The
> problem I had was, when I
> > read that location it was not giving me the correct
> data I stored there. The
> > problem was the memroy_read function of the simulator
> was expecting the
> > physical address where as I was giving the logical
> address. When obtained the
> > physical address corresponds to this logical address
> and give it to the
> > memory_read function it gives me the correct value.
> 
> Also be aware that Java arrays are different to C arrays.
> Unlike C, in
> Java the pointer returned from the allocator function will
> not
> correspond to the address of the first element stored in
> the array.
> This is due to the class identifier and array size being
> stored as
> well.
> 
> Bryce
> 


      

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: java memory allocation with gcj
  2009-11-06 16:02     ` isuru herath
@ 2009-11-06 16:43       ` Bryce McKinlay
  2009-11-06 20:06         ` isuru herath
  0 siblings, 1 reply; 9+ messages in thread
From: Bryce McKinlay @ 2009-11-06 16:43 UTC (permalink / raw)
  To: isuru herath; +Cc: Andrew Haley, java

On Fri, Nov 6, 2009 at 4:02 PM, isuru herath <isuru81@yahoo.com> wrote:

> You were right. The memory address I am getting was correct eventhough it is
> a small number compared to the one I got in C. The problem I had was, when I
> read that location it was not giving me the correct data I stored there. The
> problem was the memroy_read function of the simulator was expecting the
> physical address where as I was giving the logical address. When obtained the
> physical address corresponds to this logical address and give it to the
> memory_read function it gives me the correct value.

Also be aware that Java arrays are different to C arrays. Unlike C, in
Java the pointer returned from the allocator function will not
correspond to the address of the first element stored in the array.
This is due to the class identifier and array size being stored as
well.

Bryce

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: java memory allocation with gcj
  2009-11-06  9:54   ` Andrew Haley
  2009-11-06 12:44     ` isuru herath
@ 2009-11-06 16:02     ` isuru herath
  2009-11-06 16:43       ` Bryce McKinlay
  1 sibling, 1 reply; 9+ messages in thread
From: isuru herath @ 2009-11-06 16:02 UTC (permalink / raw)
  To: Andrew Haley; +Cc: java

Hi Andrew,

You were right. The memory address I am getting was correct eventhough it is 
a small number compared to the one I got in C. The problem I had was, when I 
read that location it was not giving me the correct data I stored there. The 
problem was the memroy_read function of the simulator was expecting the 
physical address where as I was giving the logical address. When obtained the 
physical address corresponds to this logical address and give it to the 
memory_read function it gives me the correct value. 

Thanks a lot for your help to clarify this.

regards,
Isuru


      

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: java memory allocation with gcj
  2009-11-06  9:54   ` Andrew Haley
@ 2009-11-06 12:44     ` isuru herath
  2009-11-06 16:02     ` isuru herath
  1 sibling, 0 replies; 9+ messages in thread
From: isuru herath @ 2009-11-06 12:44 UTC (permalink / raw)
  To: Andrew Haley; +Cc: java

Hi Andrew,

Thanks a lot for the reply. Here is the code.

before change

globl _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE
        .type   _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE, @function
_ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE:
.LFB2:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        subl    $40, %esp
.LCFI2:
        movl    $_ZN13test_java_new6class$E, (%esp)
        call    _Jv_InitClass
.LBB2:
        movl    $2, 4(%esp)
        movl    $_Jv_intClass, (%esp)
        call    _Jv_NewPrimArray
        movl    %eax, -4(%ebp)
        movl    -4(%ebp), %eax
        movl    %eax, -20(%ebp)
        movl    -20(%ebp), %edx
        movl    4(%edx), %eax
        testl   %eax, %eax
        jne     .L2
        movl    $0, (%esp)
        call    _Jv_ThrowBadArrayIndex
.L2:
        movl    $0, %eax
        movl    -20(%ebp), %edx
        movl    $12345, 8(%edx,%eax,4)
.LBE2:
        leave
        ret


after change

.globl _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE
        .type   _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE, @function
_ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE:
.LFB2:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        subl    $40, %esp
.LCFI2:
        movl    $_ZN13test_java_new6class$E, (%esp)
        call    _Jv_InitClass
.LBB2:
        movl    $2, 4(%esp)
        movl    $_Jv_intClass, (%esp)
        call    _Jv_NewPrimArray
#;added by isuru
        pushl   %ecx            #;for start location
        movl    %eax, %ecx      #;copy value in eax to ecx
        push    %eax            #;the memory tag
        movl    $5, %eax        #;set eax to HEAP tag
        xchg    %bx, %bx        #;magic instruction
        popl    %eax
        popl    %ecx
#;added by isuru
        movl    %eax, -4(%ebp)
        movl    -4(%ebp), %eax
        movl    %eax, -20(%ebp)
        movl    -20(%ebp), %edx
        movl    4(%edx), %eax
        testl   %eax, %eax
        jne     .L2
        movl    $0, (%esp)
        call    _Jv_ThrowBadArrayIndex
.L2:
        movl    $0, %eax
        movl    -20(%ebp), %edx
        movl    $12345, 8(%edx,%eax,4)
.LBE2:
        leave
        ret

the steps I follwed.
1. gcj -S test_java_new.java
2. edit the test_java_new.s file
3. as test_java_new.s -o test_java_new.o
4. gcj --main=test_java_new -o test_java_new test_java_new.o

Is there anything wrong or do I need to do anything extra.

your help on this regard is greatly appreciated.

sincerely,
isuru



--- On Fri, 11/6/09, Andrew Haley <aph@redhat.com> wrote:

> From: Andrew Haley <aph@redhat.com>
> Subject: Re: java memory allocation with gcj
> To: "isuru herath" <isuru81@yahoo.com>
> Cc: java@gcc.gnu.org
> Date: Friday, November 6, 2009, 1:54 AM
> isuru herath wrote:
> 
> > Thanks for the reply. I tried in x $eax gdb after the new being executed an it was giving me the following.
> > 0x804a0e0 <_ZN13test_java_new6class$E>: 0xb7a4afe8
> >
> > When converted to decimal it was 134521056 which seems close to that of C.
> > Could you please tell me what the other value represents here.
> >
> > Seems like the way I was doing is wrong. But I couldn't find another way.
> >
> > This is my Java class.
> >
> > class test_java_new
> > {
> >         public static
> void main(String[]args)
> >         {
> >             
>    int isuru[]=new int[2];
> >             
>    isuru[0] = 12345;
> >         }
> > }
> >
> > Then I compiled this with gcj -S. Then I open the
> test_java_new.s file.
> > There I add following lines after the
> > call    _Jv_NewPrimArray line.
> >
> > The lines I am adding are
> >     
>    pushl   %ecx   
>         #;for start location
> >         movl 
>   %eax, %ecx      #;copy value in eax to
> ecx
> >         push 
>   %eax            #;the
> memory tag
> >         movl 
>   $5, %eax        #;set eax to HEAP
> tag
> >         xchg 
>   %bx, %bx        #;magic
> instruction
> >         popl 
>   %eax
> >         popl 
>   %ecx
> 
> This looks right.
> 
> > Why I am doing this is, when the xchg %bx, %bx is
> executed my
> > simulator (Simics) triggers an event and my hardware
> module can get
> > the control of the system. So that I can pass
> information from the
> > user program to my hardware module. But when I check
> the eax
> > register at my hardware module after xchg %bx, %bx , I
> got that
> > small number which is 166816. But when I add the same
> assembly code
> > soon after the call malloc in a gcc generated assembly
> of a C
> > program, my hardware module gets the correct address.
> Therefore I
> > was thinking it should be the same here.
> 
> It certainly should be.  We know that when you print
> out $eax in gdb,
> you get the correct value.
> 
> > Some other thing I noticed is that when I invoke x
> $eax after the
> > isuru[0] = 12345; I got 0X28bc0 which is quite similar
> to 166848. I
> > don't know this helps you to help me to clarify my
> problem.
> 
> Let's see the assembly code of the whole function, before
> and after
> your changes.
> 
> Andrew.
> 
> 


      

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: java memory allocation with gcj
  2009-11-05 13:02 ` isuru herath
@ 2009-11-06  9:54   ` Andrew Haley
  2009-11-06 12:44     ` isuru herath
  2009-11-06 16:02     ` isuru herath
  0 siblings, 2 replies; 9+ messages in thread
From: Andrew Haley @ 2009-11-06  9:54 UTC (permalink / raw)
  To: isuru herath; +Cc: java

isuru herath wrote:

 > Thanks for the reply. I tried in x $eax gdb after the new being executed an
 > it was giving me the following.
 > 0x804a0e0 <_ZN13test_java_new6class$E>: 0xb7a4afe8
 >
 > When converted to decimal it was 134521056 which seems close to that of C.
 > Could you please tell me what the other value represents here.
 >
 > Seems like the way I was doing is wrong. But I couldn't find another way.
 >
 > This is my Java class.
 >
 > class test_java_new
 > {
 >         public static void main(String[]args)
 >         {
 >                 int isuru[]=new int[2];
 >                 isuru[0] = 12345;
 >         }
 > }
 >
 > Then I compiled this with gcj -S. Then I open the test_java_new.s file.
 > There I add following lines after the
 > call    _Jv_NewPrimArray line.
 >
 > The lines I am adding are
 >         pushl   %ecx            #;for start location
 >         movl    %eax, %ecx      #;copy value in eax to ecx
 >         push    %eax            #;the memory tag
 >         movl    $5, %eax        #;set eax to HEAP tag
 >         xchg    %bx, %bx        #;magic instruction
 >         popl    %eax
 >         popl    %ecx

This looks right.

 > Why I am doing this is, when the xchg %bx, %bx is executed my
 > simulator (Simics) triggers an event and my hardware module can get
 > the control of the system. So that I can pass information from the
 > user program to my hardware module. But when I check the eax
 > register at my hardware module after xchg %bx, %bx , I got that
 > small number which is 166816. But when I add the same assembly code
 > soon after the call malloc in a gcc generated assembly of a C
 > program, my hardware module gets the correct address. Therefore I
 > was thinking it should be the same here.

It certainly should be.  We know that when you print out $eax in gdb,
you get the correct value.

 > Some other thing I noticed is that when I invoke x $eax after the
 > isuru[0] = 12345; I got 0X28bc0 which is quite similar to 166848. I
 > don't know this helps you to help me to clarify my problem.

Let's see the assembly code of the whole function, before and after
your changes.

Andrew.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: java memory allocation with gcj
       [not found] <4AF2C075.5020409@redhat.com>
@ 2009-11-05 13:02 ` isuru herath
  2009-11-06  9:54   ` Andrew Haley
  0 siblings, 1 reply; 9+ messages in thread
From: isuru herath @ 2009-11-05 13:02 UTC (permalink / raw)
  To: Andrew Haley; +Cc: java

Hi Andrew,

I am really sorry for what happened. I pressed the wrong button. Please see 
the original mail below.

Thanks for the reply. I tried in x $eax gdb after the new being executed an 
it was giving me the following. 
0x804a0e0 <_ZN13test_java_new6class$E>: 0xb7a4afe8

When converted to decimal it was 134521056 which seems close to that of C. 
Could you please tell me what the other value represents here.

Seems like the way I was doing is wrong. But I couldn't find another way.

This is my Java class.

class test_java_new
{
        public static void main(String[]args)
        {
                int isuru[]=new int[2];
                isuru[0] = 12345;
        }
}

Then I compiled this with gcj -S. Then I open the test_java_new.s file.
There I add following lines after the 
call    _Jv_NewPrimArray line.

The lines I am adding are
        pushl   %ecx            #;for start location
        movl    %eax, %ecx      #;copy value in eax to ecx
        push    %eax            #;the memory tag
        movl    $5, %eax        #;set eax to HEAP tag
        xchg    %bx, %bx        #;magic instruction
        popl    %eax
        popl    %ecx

Why I am doing this is, when the xchg %bx, %bx is executed my simulator (Simics) triggers an event and my hardware module can get the control of the system. So that I can pass information from the user program to my hardware module. But when I check the eax register at my hardware module after xchg    %bx, %bx , I got that small number which is 166816. But when I add the same assembly code soon after the call malloc in a gcc generated assembly of a C program, my hardware module gets the correct address. Therefore I was thinking it should be the same here.

Some other thing I noticed is that when I invoke x $eax after the 
isuru[0] = 12345; I got 0X28bc0 which is quite similar to 166848. I don't know this helps you to help me to clarify my problem.

I really appreciate your help.

thanks and regards,
Isuru


--- On Thu, 11/5/09, Andrew Haley <aph@redhat.com> wrote:

> From: Andrew Haley <aph@redhat.com>
> Subject: Re: java memory allocation with gcj
> To: "isuru herath" <isuru81@yahoo.com>
> Date: Thursday, November 5, 2009, 4:09 AM
> isuru herath wrote:
> > Hi Andrew,
> > 
> > Thanks for the reply. So I was thinking soon after the
> call to _Jv_NewPrimArray the eax register should have the
> starting location of the the memory allocated by the mmap.
> But the value at the eax register was 166816.
> 
> So, if you're in gdb, try
> 
>   x $eax
> 
> 166816 doesn't seem unreasonable.  It can be a valid
> address.
> 
> > When I tried this with C, the value at the eax
> register after the malloc call was 155648008. I also put a
> printf(&name) in the C code and it prints the same
> address.
> > Then I tried store a value in the Java array and then
> tried to read the memory address 166816 and it wasnot giving
> me the value that I stored in my program. 
> 
> I think I need to see a test case.  Please, I need the
> code, and the steps
> you took.
> 
> > Even the memory addresses given from C and java are
> different in sizes as well.
> > 
> > Am I missing something here. I just need to know how
> to find the address of the block allocated by the "new" when
> running as native binary.
> > 
> > any help/advice on this regard is greatly
> appreciated.
> 
> Hard to say.  I know you must be doing something
> wrong, but I don't
> know what.  If you can send me a test case that
> illustrates what
> you're doing, I'll have a look.
> 
> Andrew.
> 


      

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: java memory allocation with gcj
       [not found] <106567.69563.qm@web56004.mail.re3.yahoo.com>
@ 2009-11-05 12:51 ` Andrew Haley
  0 siblings, 0 replies; 9+ messages in thread
From: Andrew Haley @ 2009-11-05 12:51 UTC (permalink / raw)
  To: java

isuru herath wrote:
> Hi Andrew,
> 
> Thanks for the reply. I tried in x $eax gdb after the new being executed an it was giving me the following. 
> 0x804a0e0 <_ZN13test_java_new6class$E>: 0xb7a4afe8
> 
> When converted to decimal it was 134521056 which seems close to that of C. Could you please tell me what the other value represents here.

Until you tell me how you obtained the other value, no, I can't.
What did you do before to get the value in $eax?

Andrew.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: java memory allocation with gcj
  2009-11-04 19:24 java memory allcation " isuru herath
@ 2009-11-05  9:23 ` Andrew Haley
  0 siblings, 0 replies; 9+ messages in thread
From: Andrew Haley @ 2009-11-05  9:23 UTC (permalink / raw)
  To: isuru herath; +Cc: java

isuru herath wrote:

 > I am interested in studying how "new" allocates memory when running
 > as native binary. For this I wrote a simple Java class which just
 > create an int array. Then I generate the assembly with gcj -S
 > option. There I spotted the call to _Jv_NewPrimArray. Since the
 > result of the call is stored in eax register I check the value of
 > the eax register after this call. But it is giving a small number. I
 > tried the same thing with C. There I spotted the call to malloc and
 > check the eax after the malloc call and it has the same value as the
 > &variable_name has.

_Jv_NewPrimArray calls the memory allocator (actually part of the
Boehm garbage collector) which calls
mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, ..) to map
the memory.

Andrew.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-11-06 20:06 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <704401.71578.qm@web56008.mail.re3.yahoo.com>
2009-11-05 12:13 ` java memory allocation with gcj Andrew Haley
     [not found] <4AF2C075.5020409@redhat.com>
2009-11-05 13:02 ` isuru herath
2009-11-06  9:54   ` Andrew Haley
2009-11-06 12:44     ` isuru herath
2009-11-06 16:02     ` isuru herath
2009-11-06 16:43       ` Bryce McKinlay
2009-11-06 20:06         ` isuru herath
     [not found] <106567.69563.qm@web56004.mail.re3.yahoo.com>
2009-11-05 12:51 ` Andrew Haley
2009-11-04 19:24 java memory allcation " isuru herath
2009-11-05  9:23 ` java memory allocation " Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).