android メモ

# Android メモ

## よく忘れるコマンド

```msshell
C:\Users\th-yano\AppData\Local\Android\Sdk\emulator\emulator -avd Pixel_5_API_30  -netdelay none -netspeed full -no-snapshot -show-kernel
```
```
adb exec-out screencap -p > display.png
```

```shell
ps -eo user,pid,ppid,cpu,pcpu,virt,rss,args --sort -pcpu | head -5
```
```shell
ps -aT `ps -ef |grep kot | awk '{print $2}'`
```

```shell
adb shell dumpsys window > window.txt
```

## スタックトレース
<details><summary>gdbで見た場合</summary><div>

```
(gdb) thread apply all bt

Thread 19 (Thread 4398.4422):
#0  0xec9f1b99 in ?? ()
Backtrace stopped: Cannot access memory at address 0xc

Thread 18 (Thread 4398.4421):
#0  0xec9f1b99 in ?? ()

Thread 17 (Thread 4398.4419):
#0  0xec9f1b99 in ?? ()
#1  0xebd1a13c in ?? ()
#2  0xebd1a3db in ?? ()
#3  0xebd1af19 in ?? ()
#4  0xebd4766a in ?? ()
#5  0xeae59117 in ?? ()
#6  0xe8da4fef in ?? ()
#7  0xeae587da in ?? ()
#8  0xe7912975 in ?? ()
#9  0xe78a4568 in ?? ()
#10 0xe7886b87 in ?? ()
#11 0xe64dbd2b in ?? ()
#12 0xe64c652a in ?? ()
Backtrace stopped: Cannot access memory at address 0xf1774ac6

Thread 16 (Thread 4398.4418):
#0  0xec9f1b99 in ?? ()
#1  0xebd1a13c in ?? ()
#2  0xebd1a3db in ?? ()
#3  0xebd1af19 in ?? ()
#4  0xebd4766a in ?? ()
#5  0xeae59117 in ?? ()
#6  0xe8da4fef in ?? ()
#7  0xeae587da in ?? ()
#8  0xe7912975 in ?? ()
#9  0xe78a4568 in ?? ()
#10 0xe7886b87 in ?? ()
#11 0xe64dbd2b in ?? ()
#12 0xe64c652a in ?? ()
Backtrace stopped: Cannot access memory at address 0xf1774ac6

Thread 15 (Thread 4398.4417):
#0  0xec9f1b99 in ?? ()
#1  0xebd1a13c in ?? ()
#2  0xebd1a3db in ?? ()
#3  0xebd1af19 in ?? ()
#4  0xebd4766a in ?? ()
#5  0xeae59117 in ?? ()
#6  0xe8da4fef in ?? ()
#7  0xeae587da in ?? ()
#8  0xe7912975 in ?? ()
#9  0xe78a4568 in ?? ()
#10 0xe7886b87 in ?? ()
#11 0xe78a3e91 in ?? ()
#12 0xe7912854 in ?? ()
#13 0xe791bde0 in ?? ()
#14 0x00000000 in ?? ()

Thread 14 (Thread 4398.4416):
#0  0xec9f1b99 in ?? ()
#1  0xebd1a13c in ?? ()
#2  0xebd1a3db in ?? ()
#3  0xebd1af19 in ?? ()
#4  0xebd4766a in ?? ()
#5  0xeae59117 in ?? ()
#6  0xe8da4fef in ?? ()
#7  0xeae587da in ?? ()
#8  0xe7912975 in ?? ()
#9  0xe78a4568 in ?? ()
#10 0xe7886b87 in ?? ()
#11 0xe6499bf7 in ?? ()

Thread 13 (Thread 4398.4415):
#0  0xec9f1b99 in ?? ()

Thread 12 (Thread 4398.4414):
#0  0xec9f1b99 in ?? ()

Thread 11 (Thread 4398.4413):
#0  0xec9f1b99 in ?? ()

Thread 10 (Thread 4398.4412):
#0  0xec9f1b99 in ?? ()

Thread 9 (Thread 4398.4411):
#0  0xec9f1b99 in ?? ()

Thread 8 (Thread 4398.4410):
#0  0xec9f1b99 in ?? ()
#1  0xde0f5494 in ?? ()
#2  0xde0f36d3 in ?? ()
#3  0xe7912975 in ?? ()
#4  0xe78a4568 in ?? ()
#5  0xe7886b87 in ?? ()
#6  0xff991ad4 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

Thread 7 (Thread 4398.4409):
#0  0xec9f1b99 in ?? ()
#1  0xe7912975 in ?? ()
#2  0xe78a4568 in ?? ()
#3  0xe7886b87 in ?? ()
#4  0xff9918ac in ?? ()
#5  0x43e00000 in ?? ()

Thread 6 (Thread 4398.4408):
#0  0xec9f1b99 in ?? ()
#1  0xe674ad7e in ?? ()
#2  0xe6749760 in ?? ()
#3  0xe7912975 in ?? ()
#4  0xe78a4568 in ?? ()
#5  0xe7886b87 in ?? ()
#6  0xe6897881 in ?? ()
#7  0x00000002 in ?? ()

Thread 5 (Thread 4398.4407):
#0  0xec9f1b99 in ?? ()

Thread 4 (Thread 4398.4406):
#0  0xec9f1b99 in ?? ()

Thread 3 (Thread 4398.4405):
#0  0xec9f1b99 in ?? ()

Thread 2 (Thread 4398.4404):
#0  0xec9f1b99 in ?? ()

Thread 1 (Thread 4398.4398):
#0  0xec9f1b99 in ?? ()
#1  0xbccd71de in ?? ()
#2  0xe62252b3 in ?? ()
#3  0x00000000 in ?? ()
(gdb) c
Continuing.
```
</div></details>

<details><summary>C++から適当にコード書いて呼んでみた場合</summary><div>

```
2021-12-13 09:30:11.815 4398-4398/jp.co.try_hard.kotlinsample I/MainActivity:   # 0: 0xbccd68f7  _Z16captureBacktracePPvj
      # 1: 0xbccd7194  Java_jp_co_try_1hard_kotlinsample_MainActivity_stackTrace
      # 2: 0xe62252b3  
      # 3: 0xe621eaa3  
      # 4: 0xe62b3502  _ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc
      # 5: 0xe6469882  _ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_11ShadowFrameEtPNS_6JValueE
      # 6: 0xe645dbbf  _ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE
      # 7: 0xe6886018  MterpInvokeVirtual
      # 8: 0xe6218922  
      # 9: 0xe688635f  MterpInvokeVirtual
      #10: 0xe6218922  
      #11: 0xe688635f  MterpInvokeVirtual
      #12: 0xe6218922  
      #13: 0xe688635f  MterpInvokeVirtual
      #14: 0xe6218922  
      #15: 0xe6452c83  
      #16: 0xe645cda0  _ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadERKNS_20CodeItemDataAccessorEPNS_11ShadowFrameEPNS_6JValueE
      #17: 0xe645e636  _ZN3art11interpreter6DoCallILb0ELb1EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE
      #18: 0xe626be92  _ZN3art11interpreter20ExecuteSwitchImplCppILb1ELb0EEEvPNS0_17SwitchImplContextE
      #19: 0xe6225f63  
      #20: 0xe6452c0f  
      #21: 0xe645cda0  _ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadERKNS_20CodeItemDataAccessorEPNS_11ShadowFrameEPNS_6JValueE
      #22: 0xe645e636  _ZN3art11interpreter6DoCallILb0ELb1EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE
      #23: 0xe6271794  _ZN3art11interpreter20ExecuteSwitchImplCppILb1ELb0EEEvPNS0_17SwitchImplContextE
      #24: 0xe6225f63  
      #25: 0xe6452c0f  
      #26: 0xe645cda0  _ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadERKNS_20CodeItemDataAccessorEPNS_11ShadowFrameEPNS_6JValueE
      #27: 0xe645dba1  _ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE
      #28: 0xe6886018  MterpInvokeVirtual
      #29: 0xe6218922  
```
</div></details>


<details><summary>debuggerdを利用</summary><div>

```
----- pid 4662 at 2021-12-13 00:52:35 -----
Cmd line: jp.co.try_hard.kotlinsample
ABI: 'x86'

"rd.kotlinsample" sysTid=4662
    #00 pc 00000b99  [vdso] (__kernel_vsyscall+9)
    #01 pc 000ce3d7  /apex/com.android.runtime/lib/bionic/libc.so (nanosleep+23) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)
    #02 pc 00087070  /apex/com.android.runtime/lib/bionic/libc.so (sleep+64) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)
    #03 pc 000311dd  /data/app/~~EJ2WGdPVV4r-QK68iTfKBg==/jp.co.try_hard.kotlinsample-X_ZOcH-ndQRtuVi02XsLxw==/base.apk!libkotlinsample.so (offset 0xfd000) (Java_jp_co_try_1hard_kotlinsample_MainActivity_stackTrace+205) (BuildId: af17a6c09f01b276e0b2db5c6112935c54cbf7af)
    #04 pc 001422b2  /apex/com.android.art/lib/libart.so (art_quick_generic_jni_trampoline+82) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #05 pc 0013baa2  /apex/com.android.art/lib/libart.so (art_quick_invoke_stub+338) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #06 pc 001d0501  /apex/com.android.art/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+241) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #07 pc 00386881  /apex/com.android.art/lib/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+385) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #08 pc 0037abbe  /apex/com.android.art/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+1070) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #09 pc 007a3017  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+967) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #10 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #11 pc 0000048e  [anon:dalvik-classes3.dex extracted in memory from /data/app/~~EJ2WGdPVV4r-QK68iTfKBg==/jp.co.try_hard.kotlinsample-X_ZOcH-ndQRtuVi02XsLxw==/base.apk!classes3.dex] (jp.co.try_hard.kotlinsample.MainActivity.onCreate+10)
    #12 pc 007a335e  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+1806) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #13 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #14 pc 001a99a0  /system/framework/framework.jar (android.app.Activity.performCreate+88)
    #15 pc 007a335e  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+1806) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #16 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #17 pc 001a992e  /system/framework/framework.jar (android.app.Activity.performCreate+2)
    #18 pc 007a335e  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+1806) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #19 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #20 pc 0021200e  /system/framework/framework.jar (android.app.Instrumentation.callActivityOnCreate+6)
    #21 pc 0036fc82  /apex/com.android.art/lib/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.llvm.10914192770458939989)+370) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #22 pc 00379d9f  /apex/com.android.art/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+207) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #23 pc 0037b635  /apex/com.android.art/lib/libart.so (bool art::interpreter::DoCall<false, true>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+2117) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #24 pc 00188e91  /apex/com.android.art/lib/libart.so (void art::interpreter::ExecuteSwitchImplCpp<true, false>(art::interpreter::SwitchImplContext*)+58609) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #25 pc 00142f62  /apex/com.android.art/lib/libart.so (ExecuteSwitchImplAsm+18) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #26 pc 00198584  /system/framework/framework.jar (android.app.ActivityThread.performLaunchActivity)
    #27 pc 0036fc0e  /apex/com.android.art/lib/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.llvm.10914192770458939989)+254) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #28 pc 00379d9f  /apex/com.android.art/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+207) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #29 pc 0037b635  /apex/com.android.art/lib/libart.so (bool art::interpreter::DoCall<false, true>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+2117) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #30 pc 0018e793  /apex/com.android.art/lib/libart.so (void art::interpreter::ExecuteSwitchImplCpp<true, false>(art::interpreter::SwitchImplContext*)+81395) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #31 pc 00142f62  /apex/com.android.art/lib/libart.so (ExecuteSwitchImplAsm+18) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #32 pc 00198494  /system/framework/framework.jar (android.app.ActivityThread.handleLaunchActivity)
    #33 pc 0036fc0e  /apex/com.android.art/lib/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.llvm.10914192770458939989)+254) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #34 pc 00379d9f  /apex/com.android.art/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+207) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #35 pc 0037aba0  /apex/com.android.art/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+1040) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #36 pc 007a3017  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+967) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #37 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #38 pc 0028e18a  /system/framework/framework.jar (android.app.servertransaction.LaunchActivityItem.execute+134)
    #39 pc 007a335e  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+1806) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #40 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #41 pc 0029038e  /system/framework/framework.jar (android.app.servertransaction.TransactionExecutor.executeCallbacks+154)
    #42 pc 007a335e  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+1806) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #43 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #44 pc 002902ca  /system/framework/framework.jar (android.app.servertransaction.TransactionExecutor.execute+146)
    #45 pc 007a335e  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+1806) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #46 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #47 pc 0019785c  /system/framework/framework.jar (android.app.ActivityThread$H.handleMessage+96)
    #48 pc 007a335e  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+1806) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #49 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #50 pc 0036f462  /system/framework/framework.jar (offset 0x92b000) (android.os.Handler.dispatchMessage+38)
    #51 pc 007a335e  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+1806) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #52 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #53 pc 00396c4a  /system/framework/framework.jar (offset 0x92b000) (android.os.Looper.loop+482)
    #54 pc 0036fc82  /apex/com.android.art/lib/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.llvm.10914192770458939989)+370) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #55 pc 00379d9f  /apex/com.android.art/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+207) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #56 pc 0037b635  /apex/com.android.art/lib/libart.so (bool art::interpreter::DoCall<false, true>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+2117) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #57 pc 0018e793  /apex/com.android.art/lib/libart.so (void art::interpreter::ExecuteSwitchImplCpp<true, false>(art::interpreter::SwitchImplContext*)+81395) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #58 pc 00142f62  /apex/com.android.art/lib/libart.so (ExecuteSwitchImplAsm+18) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #59 pc 001a17c0  /system/framework/framework.jar (android.app.ActivityThread.main)
    #60 pc 0036fc0e  /apex/com.android.art/lib/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.llvm.10914192770458939989)+254) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #61 pc 00379c80  /apex/com.android.art/lib/libart.so (art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*)+176) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #62 pc 0078d185  /apex/com.android.art/lib/libart.so (artQuickToInterpreterBridge+1061) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #63 pc 0014238d  /apex/com.android.art/lib/libart.so (art_quick_to_interpreter_bridge+77) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #64 pc 0013bc72  /apex/com.android.art/lib/libart.so (art_quick_invoke_static_stub+418) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #65 pc 001d0512  /apex/com.android.art/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+258) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #66 pc 00631e68  /apex/com.android.art/lib/libart.so (art::InvokeMethod(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jobject*, _jobject*, unsigned int)+1464) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #67 pc 0058a500  /apex/com.android.art/lib/libart.so (art::Method_invoke(_JNIEnv*, _jobject*, _jobject*, _jobjectArray*)+80) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #68 pc 0008a778  /apex/com.android.art/javalib/x86/boot.oat (art_jni_trampoline+168) (BuildId: b386118502376442aa402e8d28c29e95a6eb8614)
    #69 pc 0013baa2  /apex/com.android.art/lib/libart.so (art_quick_invoke_stub+338) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #70 pc 001d0501  /apex/com.android.art/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+241) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #71 pc 00386881  /apex/com.android.art/lib/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+385) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #72 pc 0037abbe  /apex/com.android.art/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+1070) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #73 pc 007a3017  /apex/com.android.art/lib/libart.so (MterpInvokeVirtual+967) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #74 pc 00135921  /apex/com.android.art/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #75 pc 00449446  /system/framework/framework.jar (offset 0x125d000) (com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run+22)
    #76 pc 0036fc82  /apex/com.android.art/lib/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.llvm.10914192770458939989)+370) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #77 pc 00379c80  /apex/com.android.art/lib/libart.so (art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*)+176) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #78 pc 0078d185  /apex/com.android.art/lib/libart.so (artQuickToInterpreterBridge+1061) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #79 pc 0014238d  /apex/com.android.art/lib/libart.so (art_quick_to_interpreter_bridge+77) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #80 pc 00893656  /system/framework/x86/boot-framework.oat (com.android.internal.os.ZygoteInit.main+2102) (BuildId: 6267c422a69286b583c337676c46b2cbf1d3b60c)
    #81 pc 0013bc72  /apex/com.android.art/lib/libart.so (art_quick_invoke_static_stub+418) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #82 pc 001d0512  /apex/com.android.art/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+258) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #83 pc 006304b3  /apex/com.android.art/lib/libart.so (art::JValue art::InvokeWithVarArgs<art::ArtMethod*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, art::ArtMethod*, char*)+579) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #84 pc 00630985  /apex/com.android.art/lib/libart.so (art::JValue art::InvokeWithVarArgs<_jmethodID*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, char*)+85) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #85 pc 004cf63f  /apex/com.android.art/lib/libart.so (art::JNI<true>::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, char*)+735) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #86 pc 0008f90e  /system/lib/libandroid_runtime.so (_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+62) (BuildId: 93b8a76261a5d972cc69b8b7c6e74be9)
    #87 pc 00098c8e  /system/lib/libandroid_runtime.so (android::AndroidRuntime::start(char const*, android::Vector<android::String8> const&, bool)+910) (BuildId: 93b8a76261a5d972cc69b8b7c6e74be9)
    #88 pc 00003804  /system/bin/app_process32 (main+1588) (BuildId: c5eedbfb6130af84c3db8e121fb1202e)
    #89 pc 000522e3  /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+115) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)

"Signal Catcher" sysTid=4672
    #00 pc 00000b99  [vdso] (__kernel_vsyscall+9)
    #01 pc 000ce821  /apex/com.android.runtime/lib/bionic/libc.so (__rt_sigtimedwait+33) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)
    #02 pc 00086c55  /apex/com.android.runtime/lib/bionic/libc.so (sigwait+69) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)
    #03 pc 00667d7d  /apex/com.android.art/lib/libart.so (art::SignalCatcher::WaitForSignal(art::Thread*, art::SignalSet&)+461) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #04 pc 0066675f  /apex/com.android.art/lib/libart.so (art::SignalCatcher::Run(void*)+479) (BuildId: bf39832c4acabbc939d5c516b6f1d211)
    #05 pc 000e6974  /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+100) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)
    #06 pc 00078567  /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)
```
</div></details>

debuggerdは[この辺](https://cs.android.com/android/platform/superproject/+/master:system/core/debuggerd/libdebuggerd/backtrace.cpp)かなぁ

<details><summary>gdb ビルド</summary><div>

 1. MSYS2のインストール
 1. ターミナルからツールチェインを入れていく

```
    5  pacman -S base-devel
    7  pacman -S msys2-devel
    8  pacman -S mingw-w64-i686-toolchain
    9  pacman -S mingw-w64-x86_64-toolchain
   10  pacman -S make openssh git
   11  pacman -S vim
   42  pacman -S expr
   43  pacman -S isl
   44  pacman -S isl-devel
   89  pacman -S libgmp
   90  pacman -S gmp-devel
   91  pacman -S gmp
   94  pacman -S mpc mpfr mpc-devel mpfr-devel
  123  pacman -S mpc-devel
  124  pacman -S mpc
```

 3. mingw64のincludeとlibを指定する必要がある
```
./configure  --host=x86_64-w64-mingw32 --enable-targets=all --disable-binutils --disable-ld --disable-gold --disable-gas --disable-sim --disable-gprof CXXFLAGS='-g3 -O0' CFLAGS='-g3 -O0 -I/mingw64/include' LDFLAGS=' -L/mingw64/lib';make
```

</div></details>

<details><summary>gdb で接続</summary><div>

```
adb root
adb forward tcp:5039 tcp:5039
gdbserver --attach :5039 `ps -ef | grep kotlin | grep -v grep | awk '{print $2}'`
```

```
$ cat .gdbinit
set pagination off
target remote 127.0.0.1:5039
thread apply all bt
c
```

</div></details>


<details><summary>コード</summary><div>

```
#include <jni.h>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <iostream>
#include <iomanip>

#include <unwind.h>
#include <dlfcn.h>
#include <sstream>
#include <android/log.h>

struct BacktraceState
{
    void** current;
    void** end;
};
static _Unwind_Reason_Code unwindCallback(struct _Unwind_Context* context, void* arg)
{
    BacktraceState* state = static_cast<BacktraceState*>(arg);
    uintptr_t pc = _Unwind_GetIP(context);
    if (pc) {
        if (state->current == state->end) {
            return _URC_END_OF_STACK;
        } else {
            *state->current++ = reinterpret_cast<void*>(pc);
        }
    }
    return _URC_NO_REASON;
}
size_t captureBacktrace(void** buffer, size_t max)
{
    BacktraceState state = {buffer, buffer + max};
    _Unwind_Backtrace(unwindCallback, &state);

    return state.current - buffer;
}

void dumpBacktrace(std::ostream& os, void** buffer, size_t count)
{
    for (size_t idx = 0; idx < count; ++idx) {
        const void* addr = buffer[idx];
        const char* symbol = "";
        const char* blank = "";

        Dl_info info;
        if (dladdr(addr, &info) && info.dli_sname) {
            symbol = info.dli_sname;
        } else {
            symbol = blank;
        }
        os << "  #" << std::setw(2) << idx << ": " << addr << "  " << symbol << "\n";
    }
}
extern "C"
JNIEXPORT jstring JNICALL
Java_jp_co_try_1hard_kotlinsample_MainActivity_stackTrace(JNIEnv *env, jobject thiz) {
    const size_t max = 30;
    void* buffer[max];
    std::ostringstream oss;

    dumpBacktrace(oss, buffer, captureBacktrace(buffer, max));

    sleep(30);
    return env->NewStringUTF(oss.str().c_str());
}

```
</div></details>

javaはlibartで実行されている。

gdbで見れない理由はわからない


## C++だけでWindow表示
https://github.com/Um6ra1/VsAndroid

<details><summary>動かすとこんな感じ</summary><div>

![sample.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2383810/267d545d-2050-9506-4fe2-61e1ac74fb47.png)
</div></details>

# アプリ起動の流れ

```
export CLASSPATH=/system/framework/am.jar ;exec /system/bin/app_process32 /system/bin com.android.commands.am.Am start -n jp.co.try_hard.empty_kotlin/.MainActivity
```

[app_process](https://cs.android.com/android/platform/superproject/+/master:frameworks/base/cmds/app_process/app_main.cpp)