.NETプロジェクトをgdbデバッグする方法

自分用メモ

SDL2を使った.NETプロジェクトでsegfaultで落ちたりして何が原因なのかよくわからないときにgdbで追いかける方法

syslogにはこんな感じに出る。

```text:/var/log/message
10月 06 10:34:41 desktop kernel: myapp[5134]: segfault at 7fff00000010 ip 00007ffff789e631 sp 00007fffffffc630 error 4 in libc.so.6[7ffff7822000+178000] likely on CPU 10 (core 20, socket 0)
```

~/.gdbinit に以下のようにセットしておく([参考情報 - https://www.mono-project.com/docs/debug+profile/debug/](https://www.mono-project.com/docs/debug+profile/debug/))

```text:~/.gdbinit
set debuginfod enabled on
handle SIGXCPU SIG33 SIG35 SIG36 SIG37 SIG38 SIGPWR nostop noprint
```

プログラムの実行はこんな感じにする

```shell
$ dotnet build && gdb --args bin/Debug/net7.0/myapp
```

```shell
MSBuild version 17.4.8+6918b863a for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  myapp -> /home/aaa/myapp/bin/Debug/net7.0/myapp.dll

ビルドに成功しました。
    0 個の警告
    0 エラー

経過時間 00:00:00.78
GNU gdb (Ubuntu 13.1-2ubuntu2) 13.1
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from bin/Debug/net7.0/myapp...
(No debugging symbols found in bin/Debug/net7.0/myapp)                                                                                                                                 
(gdb) r <= r+[Enter] で実行を続ける
```

オンデマンドでデバッグ用の情報をダウンロードしたりするので少し待つ場合がある。segvで止まると以下のような感じになる。

```shell
Thread 1 "myapp" received signal SIGSEGV, Segmentation fault.
0x00007ffff789c0bd in unlink_chunk (p=p@entry=0x5555af28cd90, av=0x7ffff79f6c80 <main_arena>) at ./malloc/malloc.c:1610
Download failed: 無効な引数です.  Continuing without source file ./malloc/./malloc/malloc.c.                                                                                          
1610	./malloc/malloc.c: そのようなファイルやディレクトリはありません.
(gdb) bt <= bt+[Enter] でバックトレースする
```

バックトレースするとsegvに至った経緯が表示される

```shell
(gdb) bt
#0  0x00007ffff789c0bd in unlink_chunk (p=p@entry=0x5555af28cd90, av=0x7ffff79f6c80 <main_arena>) at ./malloc/malloc.c:1610
#1  0x00007ffff789ee86 in _int_malloc (av=av@entry=0x7ffff79f6c80 <main_arena>, bytes=bytes@entry=112) at ./malloc/malloc.c:4293
#2  0x00007ffff78a0ea8 in __libc_calloc (n=n@entry=1, elem_size=elem_size@entry=112) at ./malloc/malloc.c:3664
#3  0x00007fbebe6fe4cb in lp_fence_create (rank=20) at ../src/gallium/drivers/llvmpipe/lp_fence.c:51
#4  0x00007fbebe70eead in begin_binning (setup=setup@entry=0x555555b33fe0) at ../src/gallium/drivers/llvmpipe/lp_setup.c:234
#5  0x00007fbebe70f1c9 in execute_clears (setup=0x555555b33fe0) at ../src/gallium/drivers/llvmpipe/lp_setup.c:312
#6  set_scene_state (setup=setup@entry=0x555555b33fe0, new_state=new_state@entry=SETUP_FLUSHED, reason=<optimized out>) at ../src/gallium/drivers/llvmpipe/lp_setup.c:359
#7  0x00007fbebe70fc97 in lp_setup_bind_framebuffer (setup=0x555555b33fe0, fb=fb@entry=0x555555afa698) at ../src/gallium/drivers/llvmpipe/lp_setup.c:400
#8  0x00007fbebe72f4b1 in llvmpipe_set_framebuffer_state (pipe=0x555555af9210, fb=0x7fffffffc9f0) at ../src/gallium/drivers/llvmpipe/lp_state_surface.c:99
#9  0x00007fbebe3c6442 in st_update_framebuffer_state (st=0x555555aabce0) at ../src/mesa/state_tracker/st_atom_framebuffer.c:219
#10 0x00007fbebe3ca9ed in st_validate_state (pipeline_state_mask=1107296512, st=0x555555aabce0) at ../src/util/bitscan.h:115
#11 st_Clear (ctx=0x555555b70bd0, mask=128) at ../src/mesa/state_tracker/st_cb_clear.c:432
#12 0x00007fbed4e91584 in GL_RunCommandQueue (renderer=0x555555b6f3b0, cmd=0x5555557b8d70, vertices=0x5555573f16c0, vertsize=480)
    at /home/aaa/SDL2-2.28.3/src/render/opengl/SDL_render_gl.c:1266
#13 0x00007fbed4e7ccea in FlushRenderCommands (renderer=0x555555b6f3b0) at /home/aaa/SDL2-2.28.3/src/render/SDL_render.c:249
#14 0x00007fbed4e82b25 in SDL_SetRenderTarget_REAL (renderer=0x555555b6f3b0, texture=0x5555573785e0) at /home/aaa/SDL2-2.28.3/src/render/SDL_render.c:2207
#15 0x00007fbed4e4ff70 in SDL_SetRenderTarget (a=0x555555b6f3b0, b=0x555557378540) at /home/aaa/SDL2-2.28.3/src/dynapi/SDL_dynapi_procs.h:354
#16 0x00007fff78cc4100 in ?? ()
#17 0x0000555555b6f3b0 in ?? ()
#18 0x0000000000009c29 in ?? ()
#19 0x00007ffff7696230 in ?? () from /usr/lib/dotnet/shared/Microsoft.NETCore.App/7.0.11/libcoreclr.so
#20 0xffffffffffffffff in ?? ()
#21 0x00007fff79395b00 in ?? ()
#22 0x00007fff79395b00 in ?? ()
#23 0x00007fffffffcd10 in ?? ()
#24 0x00007fff78cc4100 in ?? ()
#25 0x00007fffffffcd90 in ?? ()
#26 0x0000000000000000 in ?? ()
(gdb) [Ctrl]+[D] で抜けることができる
```

.NET側も追いかけられるといいんですけどね。