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?

引数が必要なプログラムのデバックをlldbでしてみた

Last updated at Posted at 2024-05-30

lldbとは

ソフトウェアデバッガの一つです。
Macの場合は最初からlldbがあるため、これを使ってデバッグを行ってみます。
今回、引数を必要とするプログラムのデバッグ方法がわからなかったので、まとめてみました。

lldb使用方法

例えば、下記のようなプログラムを動かしたいとします。

main.c
int	main(int argc, char **argv)
{
	t_philo			philo;

	init_args(argc, argv, &philo);
	init_philo_data(&philo);
	start_dinner(&philo);
	return (0);
}

上記のプログラムの実行方法の例は以下です。

./philo 4 800 200 200 5

このプログラムのデバッグを行います。

lldbを起動する

まず初めに、プログラムをmakeし、実行ファイルphiloを生成します。
※コンパイル時は-gオプションを必ず付けてください。

そして、下記のコマンドでlldbの起動をします。

lldb ./philo

下記のコマンドで引数を設定します。

settings set -- target.run-args 4 800 200 200 5

ブレークポイントを設定する場合は、下記のどちらかのコマンドで設定できます。

breakpoint set --name main
br set -n main

上記は関数名を指定していますが、下記のどちらかで関数名と行数を指定することもできます。

br set --file main.c --line 6
br set -f main.c -l 6

最後に、rまたはrunでlldbが実行されます。

run

lldb実行後

lldb実行後、下記のように今現在デバッグを行なっている行数が指されます。
スクリーンショット 2024-05-30 22.17.10.png

今、矢印が指している関数を詳しくみていきたい場合はsまたはstepで関数の中に入れます。

step

次の行(関数)に移動したい場合はnまたはnextで次に進みます。

next

リークしている部分があれば処理が中断し、下記のように知らせてくれます。
スクリーンショット 2024-05-30 22.31.18.png

変数を出力

po(print object)またはprintコマンドを使用し、任意の出力したい変数名を入れると、その変数の値が表示されます。

print [変数名]

構造体の中身を出力したい場合は、[0]のように値を指定してください。

print philo[0]

スクリーンショット 2024-05-30 23.17.25.png
配列要素のメンバーや構造体メンバーの表示も可能です。

(lldb) print philo[0].philo_id
(unsigned int) 1
(lldb) print philo->args.number_of_philo
(unsigned int) 4

終了方法

control+dでlldbを終了させることができます。

まとめ

必要最低限のコマンドをまとめてみました。
役立つコマンドや効率の良い方法があればぜひ教えてください。

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?