2014年10月14日火曜日

OS X Mavericks - Appleには lldb が標準(Xcode - Appleのインストールが必要かも)で搭載されてるから、GDBの代わりにlldbを使って本書を読み進めようと思ったら、lldbとgdbには微妙な違いが結構あるみたいだった。ということで、とりあえずスムーズに読み進めるためにGDBをインストールすることにしたのでそのメモ。

  1. とりあえずMacPortsで探してみることに。(MacPortsはインストール済み。)
    $ port search gdb
    # …
    gdb @7.7.1 (devel)
        GDB: The GNU Project Debugger
    
    gdb-apple @1824 (devel)
        GDB: The GNU Project Debugger with Apple Modifications (based on 6.3.50)
    # …
    $
    
    2つあるみたい。
  2. Appleはlldbに力を入れている(?)っぽいから、通常のgdbをインストールすることに。
    $ sudo port install gdb
    
  3. インストール完了。
  4. インストール時に、
    You will need to make sure
    /System/Library/LaunchDaemons/com.apple.taskgated.plist has the '-p' option,
    e.g.
                <key>ProgramArguments</key>
                <array>
                        <string>/usr/libexec/taskgated</string>
                        <string>-sp</string>
                </array>
    
    Due to kernel caching, you usually need to restart Mac OS X for this option to
    effect.
    
    というメッセージが表示されたので、それに従ってファイルを修正。
  5. 終了って思ったら、
    /opt/local/bin
    
    にgdbが無いからいろいろ探してみたら、
    /opt/local/libexec/gnubin/
    
    に発見。
  6. 環境変数PATHが増えてごちゃごちゃしそうだから、PATHに
    /opt/local/libexec/gnubin/
    
    を追加するのではなく、
    /usr/local/bin
    
    にシンボリックリンクを作成することに。(今後、他のGNUのツールも使うことになった時、MacPorts経由でインストールしたのはgnubinフォルダに置かれるのかなぁ。それなら
    /opt/local/libexec/gnubin
    
    もPATHに追加しておいた方がいいかも。)
  7. symbolic linkを作成。
    $ sudo ln -s /opt/local/libexec/gnubin/gdb /usr/local/bin/gdb 
    
  8. コンピューターを再起動。

使えるようになったか確認。

コード(BBEdit, Emacs)

temp.c

#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Hello, World!\n");

  return (0);
}

入出力結果(Terminal)

$ cc -g -Wall -O3 temp.c
$ gdb a.out
GNU gdb (GDB) 7.7.1
Copyright (C) 2014 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-apple-darwin13.2.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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 a.out...Reading symbols from /Users/kamimura/a.out.dSYM/Contents/Resources/DWARF/a.out...done.
done.
(gdb) run
Starting program: /Users/kamimura/a.out 
Hello, World!
[Inferior 1 (process 2629) exited normally]
(gdb) quit
$

無事インストール終了。

0 コメント:

コメントを投稿