目次: Linux
同じことをしている人があまり居なさそうだったので、メモしておきます。
きっかけはGCCのコードをGDBのCUIモードで追っていて辛くなったことです。GCCのコードは超ぐちゃぐちゃの悲惨なコードで非常に追いづらく、GDBをもってしても何が起きているのか把握するのは困難です。せめてデバッガの画面くらいはGUIにして、見やすくできないか、と考えました。
WindowsからLinuxアプリのデバッグ、それぞれの役割
想定する構成は上記のとおりで、Linux側にはGUIがなく(ディスプレイを繋いでいない、など)、Windows側はデバッグのみで、Linux側でその他の全て(ビルドなど)を行う想定です。
この記事を読んでいるということは、既に何かデバッグしたいアプリケーションがあると思いますから、基本的なツールであるgccやmakeなどのインストールは省略させてもらいます。gdbserverをインストールするだけで良いはずです。
# apt-get install gdbserver Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: gdbserver 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 0 B/458 kB of archives. After this operation, 1165 kB of additional disk space will be used. Selecting previously unselected package gdbserver. (Reading database ... 99167 files and directories currently installed.) Preparing to unpack .../gdbserver_8.2.1-2+b3_amd64.deb ... Unpacking gdbserver (8.2.1-2+b3) ... Setting up gdbserver (8.2.1-2+b3) ...
デバッグするプログラムは何でも良いですが、下記を使用します。渡した引数を表示するだけのプログラムです。
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("argc: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("argv[%d]: %s\n", i, argv[i]);
}
return 0;
}
$ gcc -Wall -g -O0 a.c $ ./a.out a b c argc: 4 argv[1]: a argv[2]: b argv[3]: c
まずは普通にCUIからGDBで追ってみます。図示するとこのような構成です。
あえてやる必要もなさそうですけど、このあとの一貫性のために試します。
$ gdb a.out ... For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from a.out... (gdb) b main Breakpoint 1 at 0x1144: file a.c, line 5. (gdb) r a b c d Starting program: /home/katsuhiro/share/falcon/projects/c/test_argv/a.out a b c d Breakpoint 1, main (argc=5, argv=0x7fffffffdca8) at a.c:5 5 printf("argc: %d\n", argc); (gdb) c Continuing. argc: 5 argv[1]: a argv[2]: b argv[3]: c argv[4]: d [Inferior 1 (process 4008866) exited normally]
普通です。次はgdbserverと連携させリモートでGDBで追ってみます。図示するとこのような構成です。
実際にやってみましょう。ターミナルを2つ用意してください。
★★ターミナル1つ目 $ gdbserver --multi localhost:1234 ./a.out a b c d Process ./a.out created; pid = 4008908 Listening on port 1234 Remote debugging from host ::1, port 32918 ★mainでbreakしたあとのcontinueを実行すると下記が出力される argc: 5 argv[1]: a argv[2]: b argv[3]: c argv[4]: d Child exited with status 0 ★★ターミナル2つ目 $ gdb ... For help, type "help". Type "apropos word" to search for commands related to "word". (gdb) target remote :1234 Remote debugging using :1234 Reading test_argv/a.out from remote target... warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead. Reading test_argv/a.out from remote target... Reading symbols from target:test_argv/a.out... Reading /lib64/ld-linux-x86-64.so.2 from remote target... Reading /lib64/ld-linux-x86-64.so.2 from remote target... Reading symbols from target:/lib64/ld-linux-x86-64.so.2... Reading symbols from /usr/lib/debug/.build-id/5b/e47e85c990f390b0dccb6ca9dc3e70f410dc6a.debug... 0x00007ffff7fd3090 in _start () from target:/lib64/ld-linux-x86-64.so.2 (gdb) b main Breakpoint 1 at 0x555555555144: file a.c, line 5. (gdb) c Continuing. Reading /lib/x86_64-linux-gnu/libc.so.6 from remote target... Breakpoint 1, main (argc=5, argv=0x7fffffffdd18) at a.c:5 5 printf("argc: %d\n", argc); (gdb) l 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) 4 { 5 printf("argc: %d\n", argc); 6 7 for (int i = 1; i < argc; i++) { 8 printf("argv[%d]: %s\n", i, argv[i]); 9 } 10 (gdb) c Continuing. [Inferior 1 (process 4008908) exited normally]
ブレークもできますし、ソースコードも表示されます。良い感じですね。
使い勝手はGDB単体でデバッグするときとほぼ同じですが、1点だけ注意があります。デバッガとして動作するのはgdbserverであり、プログラムのprintfなどの標準出力は、gdbserverを動かしているターミナル側に出ることに注意してください。この使い方におけるGDBはgdbserverと通信するだけの役です。
Sambaのセットアップについては、ここで解説せずとも、詳しいサイトがたくさんあると思いますので、そちらをご参照ください。
思ったより長くなってきたので、続きは次回に。
< | 2021 | > | ||||
<< | < | 03 | > | >> | ||
日 | 月 | 火 | 水 | 木 | 金 | 土 |
- | 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 | - | - | - |
合計:
本日:
管理者: Katsuhiro Suzuki(katsuhiro( a t )katsuster.net)
This is Simple Diary 1.0
Copyright(C) Katsuhiro Suzuki 2006-2023.
Powered by PHP 8.2.15.
using GD bundled (2.1.0 compatible)(png support.)