dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.31k stars 1.59k forks source link

Provide an easy way to view AOT disassembly #45894

Open vsmenon opened 3 years ago

vsmenon commented 3 years ago

Today:

dart -disassemble foo.dart

provides a convenient way to view disassembled code, but only for JIT mode.

AFAIK, you need a checked SDK to view AOT disassembled code (which is more useful) - e.g.,:

.../dart/sdk/pkg/vm/tool/precompiler2 --disassemble foo.dart foo.aot

Is there a way to integrate that into the Dart cli?

@jefflim-google

vsmenon commented 3 years ago

FYI - @mraleph - @jefflim-google was asking.

vsmenon commented 3 years ago

Also, is there an easy way to show ARM code from an x86 machine?

bkonyi commented 3 years ago

cc @alexmarkov

mraleph commented 3 years ago

Currently, you can't get disassembly with --disassemble from dart compile exe or flutter build apk --release because they use PRODUCT build of gen_snapshot which exclude disassembler. There is actually no good reason for why it excludes it (it historical). I have recently (see d7a57a5df4629045ebf406951eb73679a601c7b8) fixed it to include IL printer into PRODUCT gen_snapshot and I think we can similarly include disassembler as well.

That said you can do the following today:

$ aarch64-linux-gnu-nm ./build/app/intermediates/merged_native_libs/release/out/lib/arm64-v8a/libapp.so | grep main
0000000000034334 t Precompiled__IntegerImplementation_0150898_remainder_584
00000000000e7204 t Precompiled____main_3795
00000000000e7234 t Precompiled____main_main_3796
0000000000083df0 t Precompiled_ReadBuffer_get_hasRemaining_1949
$ aarch64-linux-gnu-objdump --source --disassemble=Precompiled____main_3795 ./build/app/intermediates/merged_native_libs/release/out/lib/arm64-v8a/libapp.so

./build/app/intermediates/merged_native_libs/release/out/lib/arm64-v8a/libapp.so:     file format elf64-littleaarch64

Disassembly of section .text:

Disassembly of section .text:

00000000000e7204 <Precompiled____main_3795>:
EnterCallRuntimeFrame
EnterCallRuntimeFrame
EnterCallRuntimeFrame
B0
B1
Enter frame
   e7204:   a9bf79fd    stp x29, x30, [x15, #-16]!
   e7208:   aa0f03fd    mov x29, x15
CheckStackOverflow:8(stack=0, loop=0)
   e720c:   f9402350    ldr x16, [x26, #64]
   e7210:   eb1001ff    cmp x15, x16
   e7214:   540000c9    b.ls    e722c <Precompiled____main_3795+0x28>  // b.plast
StaticCall:10( runApp<0> , result_type = T{Null?})
   e7218:   97fef350    bl  a3f58 <Precompiled____runApp_2550>
ParallelMove r0 <- C
   e721c:   aa1603e0    mov x0, x22
Return:12(v0)
   e7220:   aa1d03ef    mov x15, x29
   e7224:   a8c179fd    ldp x29, x30, [x15], #16
   e7228:   d65f03c0    ret
CheckStackOverflowSlowPath
   e722c:   97fffd11    bl  e6670 <Precompiled_Stub__iso_stub_StackOverflowSharedWithoutFPURegsStub>
   e7230:   54ffff4e    b.al    e7218 <Precompiled____main_3795+0x14>

This is on Linux using aarch64 binutils (available via sudo apt-get install binutils-aarch64-linux-gnu, similarly you can install binutils for ARM).

Alternatively if you have Android NDK installed (which you often do), you can look for llvm-objdump there. It has slightly different flags and format of the output, but the idea is the same (you can replace linux below with darwin on Mac).

$ ${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-objdump --source --disassemble-symbols=Precompiled____main_3795 ./build/app/intermediates/merged_native_libs/release/out/lib/arm64-v8a/libapp.so

./build/app/intermediates/merged_native_libs/release/out/lib/arm64-v8a/libapp.so:   file format elf64-littleaarch64

Disassembly of section .text:

00000000000e7204 <Precompiled____main_3795>:
; Enter frame
   e7204: fd 79 bf a9   stp x29, x30, [x15, #-16]!
   e7208: fd 03 0f aa   mov x29, x15
; CheckStackOverflow:8(stack=0, loop=0)
   e720c: 50 23 40 f9   ldr x16, [x26, #64]
   e7210: ff 01 10 eb   cmp x15, x16
   e7214: c9 00 00 54   b.ls    0xe722c <Precompiled____main_3795+0x28>
; StaticCall:10( runApp<0> , result_type = T{Null?})
   e7218: 50 f3 fe 97   bl  0xa3f58 <Precompiled____runApp_2550>
; ParallelMove r0 <- C
   e721c: e0 03 16 aa   mov x0, x22
; Return:12(v0)
   e7220: ef 03 1d aa   mov x15, x29
   e7224: fd 79 c1 a8   ldp x29, x30, [x15], #16
   e7228: c0 03 5f d6   ret
; CheckStackOverflowSlowPath
   e722c: 11 fd ff 97   bl  0xe6670 <Precompiled_Stub__iso_stub_StackOverflowSharedWithoutFPURegsStub>
   e7230: 4e ff ff 54   b.al    0xe7218 <Precompiled____main_3795+0x14>