[Mono-dev] How to debug native code?

2016-01-18 Thread Pin Cheng
Hi all,
I am trying to port Mono into Aix host, I have am very fundamental question:
How are you guys debug native code?
Because JIT compiler compiles assembly into native code then JIT invoke it,
I am very curious if there errors in native code How do you find out the bug 
and fix it.
And if you updates the cpu-.md or -codegen.h how do you verify your 
changes is ok?
There must be somewhat effect method to debug the JIT and it specific 
architecture.

Since I am porting Mono into Aix, I got  assembler code which disassembled from 
native code:
   0: 7c 08 02 a6   mflrr0
   4: 90 01 00 04  stw r0,4(r1)
   8: 94 21 ff b0   stwur1,-80(r1)
   c:  93 61 00 48  stw r27,72(r1)
  10:93 e1 00 4c  stw r31,76(r1)
  14:7c 3f 0b 78   mr  r31,r1
  18:90 7f 00 28   stw r3,40(r31)
  1c:90 9f 00 2c   stw r4,44(r31)
  20:90 bf 00 30   stw r5,48(r31)
  24:90 df 00 34   stw r6,52(r31)
  28:38 60 00 00  li  r3,0
  2c:90 7f 00 20   stw r3,32(r31)
  30:38 60 00 00  li  r3,0
  34:90 7f 00 24   stw r3,36(r31)
  38:80 7f 00 30   lwz r3,48(r31)
  3c:2c 03 00 00   cmpwi   r3,0
  40:41 82 00 a4  beq 0xe4
  44:3c 60 20 03   lis r3,8195
  48:60 63 e5 04  ori r3,r3,58628
  4c:80 63 00 00  lwz r3,0(r3)
  50:2c 03 00 00   cmpwi   r3,0
  54:41 82 00 38  beq 0x8c
  58:3d 80 30 3c  lis r12,12348
  5c:61 8c cd d8  ori r12,r12,52696
  60:7d 88 03 a6  mtlrr12
  64:4e 80 00 21  blrl
  68:7c 64 1b 78  mr  r4,r3
  6c:7c 9b 23 78  mr  r27,r4
  70:2c 03 00 00   cmpwi   r3,0
  74:41 82 00 18  beq 0x8c
  78:7f 63 db 78  mr  r3,r27
  7c:3d 80 30 00  lis r12,12288
  80:61 8c 82 90   ori r12,r12,33424
  84:7d 88 03 a6  mtlrr12
  88:4e 80 00 21  blrl
  8c:80 7f 00 2c   lwz r3,44(r31)
  90:80 83 00 00  lwz r4,0(r3)
  94:80 7f 00 28   lwz r3,40(r31)
  98:80 bf 00 34   lwz r5,52(r31)
  9c:7c a8 03 a6   mtlrr5
  a0:4e 80 00 21  blrl
  a4:48 00 00 38  b   0xdc
  a8:80 7f 00 1c   lwz r3,28(r31)
  ac:80 7f 00 1c   lwz r3,28(r31)
  b0:90 7f 00 24   stw r3,36(r31)
  b4:80 7f 00 30   lwz r3,48(r31)
  b8:80 9f 00 24   lwz r4,36(r31)
  bc:90 9f 00 40   stw r4,64(r31)
  c0:90 83 00 00  stw r4,0(r3)
  c4:3d 80 30 3c  lis r12,12348
  c8:61 8c cf 50ori r12,r12,53072
  cc: 7d 88 03 a6  mtlrr12
  d0:4e 80 00 21  blrl
  d4:80 7f 00 40   lwz r3,64(r31)
  d8:48 00 00 04  b   0xdc
  dc:80 7f 00 20   lwz r3,32(r31)
  e0:48 00 00 38  b   0x118
  e4:3c 60 20 03   lis r3,8195
  e8:60 63 e5 04  ori r3,r3,58628
  ec:80 63 00 00  lwz r3,0(r3)
  f0: 28 03 00 00  cmplwi  r3,0
  f4: 40 82 00 40  bne 0x134
  f8: 48 00 00 04  b   0xfc
  fc: 80 7f 00 2c   lwz r3,44(r31)
100:   80 83 00 00  lwz r4,0(r3)
104:   80 7f 00 28   lwz r3,40(r31)
108:   80 bf 00 34   lwz r5,52(r31)
10c:   7c a8 03 a6   mtlrr5
110:   4e 80 00 21  blrl
114:   80 7f 00 20   lwz r3,32(r31)
118:   80 1f 00 54   lwz r0,84(r31)
11c:   7c 08 03 a6   mtlrr0
120:   7f ec fb 78   mr  r12,r31
124:   83 7f 00 48   lwz r27,72(r31)
128:   83 ff 00 4clwz r31,76(r31)
12c:   38 2c 00 50   addir1,r12,80
130:   4e 80 00 20  blr
134:   3d 80 30 3c  lis r12,12348
138:   61 8c cd d8  ori r12,r12,52696
13c:   7d 88 03 a6  mtlrr12
140:   4e 80 00 21  blrl
144:   7c 64 1b 78  mr  r4,r3
148:   7c 9b 23 78  mr  r27,r4
14c:   2c 03 00 00   cmpwi   r3,0
150:   41 82 ff acbeq 0xfc
154:   7f 63 db 78  mr  r3,r27
158:   3d 80 30 00  lis r12,12288
15c:   61 8c 82 90   ori r12,r12,33424
160:   7d 88 03 a6  mtlrr12
164:   4e 80 00 21 

Re: [Mono-dev] How to debug native code?

2016-01-20 Thread Bill Seurer
When I am debugging problems with native code I use gdb to debug it.  I 
have used other debuggers, too.


If you watch the commands that are running you should be able to see 
when the mono program is called.  Note the parameters.  Then run the 
debugger on mono and then run the program within the debugger using the 
parameters you noted.  When the segmentation fault occurs you can see 
where it happened.


If you add -v options on mono (try using more than one) you can get a 
trace of what native functions are called and the assembler for those 
functions.


Within the debugger you can use this command:

print mono_pmip($pc)

to see the name of the native function you are inside of.


On 01/18/16 20:14, Pin Cheng wrote:

Hi all,

I am trying to port Mono into Aix host, I have am very fundamental question:

How are you guys debug native code?

Because JIT compiler compiles assembly into native code then JIT invoke it,

I am very curious if there errors in native code How do you find out the
bug and fix it.

And if you updates the cpu-.md or -codegen.h how do you
verify your changes is ok?

There must be somewhat effect method to debug the JIT and it specific
architecture.

Since I am porting Mono into Aix, I got  assembler code which
disassembled from native code:

0: 7c 08 02 a6   mflrr0

4: 90 01 00 04  stw r0,4(r1)

8: 94 21 ff b0   stwur1,-80(r1)

c:  93 61 00 48  stw r27,72(r1)

   10:93 e1 00 4c  stw r31,76(r1)

   14:7c 3f 0b 78   mr  r31,r1

   18:90 7f 00 28   stw r3,40(r31)

   1c:90 9f 00 2c   stw r4,44(r31)

   20:90 bf 00 30   stw r5,48(r31)

   24:90 df 00 34   stw r6,52(r31)

   28:38 60 00 00  li  r3,0

   2c:90 7f 00 20   stw r3,32(r31)

   30:38 60 00 00  li  r3,0

   34:90 7f 00 24   stw r3,36(r31)

   38:80 7f 00 30   lwz r3,48(r31)

   3c:2c 03 00 00   cmpwi   r3,0

   40:41 82 00 a4  beq 0xe4

   44:3c 60 20 03   lis r3,8195

   48:60 63 e5 04  ori r3,r3,58628

   4c:80 63 00 00  lwz r3,0(r3)

   50:2c 03 00 00   cmpwi   r3,0

   54:41 82 00 38  beq 0x8c

   58:3d 80 30 3c  lis r12,12348

   5c:61 8c cd d8  ori r12,r12,52696

   60:7d 88 03 a6  mtlrr12

   64:4e 80 00 21  blrl

   68:7c 64 1b 78  mr  r4,r3

   6c:7c 9b 23 78  mr  r27,r4

   70:2c 03 00 00   cmpwi   r3,0

   74:41 82 00 18  beq 0x8c

   78:7f 63 db 78  mr  r3,r27

   7c:3d 80 30 00  lis r12,12288

   80:61 8c 82 90   ori r12,r12,33424

   84:7d 88 03 a6  mtlrr12

   88:4e 80 00 21  blrl

   8c:80 7f 00 2c   lwz r3,44(r31)

   90:80 83 00 00  lwz r4,0(r3)

   94:80 7f 00 28   lwz r3,40(r31)

   98:80 bf 00 34   lwz r5,52(r31)

   9c:7c a8 03 a6   mtlrr5

   a0:4e 80 00 21  blrl

   a4:48 00 00 38  b   0xdc

   a8:80 7f 00 1c   lwz r3,28(r31)

   ac:80 7f 00 1c   lwz r3,28(r31)

   b0:90 7f 00 24   stw r3,36(r31)

   b4:80 7f 00 30   lwz r3,48(r31)

   b8:80 9f 00 24   lwz r4,36(r31)

   bc:90 9f 00 40   stw r4,64(r31)

   c0:90 83 00 00  stw r4,0(r3)

   c4:3d 80 30 3c  lis r12,12348

   c8:61 8c cf 50ori r12,r12,53072

   cc: 7d 88 03 a6  mtlrr12

   d0:4e 80 00 21  blrl

   d4:80 7f 00 40   lwz r3,64(r31)

   d8:48 00 00 04  b   0xdc

   dc:80 7f 00 20   lwz r3,32(r31)

   e0:48 00 00 38  b   0x118

   e4:3c 60 20 03   lis r3,8195

   e8:60 63 e5 04  ori r3,r3,58628

   ec:80 63 00 00  lwz r3,0(r3)

   f0: 28 03 00 00  cmplwi  r3,0

   f4: 40 82 00 40  bne 0x134

   f8: 48 00 00 04  b   0xfc

   fc: 80 7f 00 2c   lwz r3,44(r31)

100:   80 83 00 00  lwz r4,0(r3)

104:   80 7f 00 28   lwz r3,40(r31)

108:   80 bf 00 34   lwz r5,52(r31)

10c:   7c a8 03 a6   mtlrr5

110:   4e 80 00 21  blrl

114:   80 7f 00 20   lwz r3,32(r31)

118:   80 1f 00 54   lwz r0,84(r31)

11c: