0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

別プロセスから起動されるコマンドをgdbでデバッグしてみる

Posted at

gdbを使う場合、デバッグしたいプログラムを直接gdbで起動することは可能だが、あるプログラムから実行されたプログラムをデバッグしたい場合、どうすれば可能かを考えてみた。あるプログラムを wrapper.sh とした場合に、そrを gdbserver コマンドで実行するラッパープログラム(シェル)を作ってみてはどうだろうか?

wrapper.sh を実行するプログラム名に置き換えることで、呼び元を騙す。
gdbserverはgdbコマンドから接続されるまではじっと待っているので、すぐに終了してしまうようなプログラムでもデバッグできるのではないか、という考え。

未検証。

shell
# chmod +x wrapper.sh
# mv wrapper.sh <実行したいプログラム名>
wrapper.sh
#!/bin/sh

CMD="ls aa"
HOST="localhost"
PORT="12345"

EXCEPT_CODE="99"

# usage
echo "execute command:[${CMD}]" 1>&2
echo "Please execute gdb" 1>&2
echo "# gdb"
CMD_LIST=($CMD)
echo "(gdb) file ${CMD_LIST[0]}"
echo "(gdb) target remote ${HOST}:${PORT}"
echo "(gdb) b main"
echo "(gdb) run"
echo -e

# デバッグしたいコマンドを実行
STATUS=$(gdbserver ${HOST}:${PORT} ${CMD} |& awk '/Child exited with status/ {print $5}')

# if gdbserverが status code なしで終了した場合
if [ -z ${RET} ]; then
        echo "Child exited with status is not set!!" 1>&2
        ret=${EXCEPT_CODE}
else
        echo "Child exited with status ${STATUS}" 1>&2
fi

exit ${STATUS}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?