ソースコードリーディング1(準備)

version date memo
1.0 2013/07/14 first
1.1 2013/07/15 gtags-parse-file関連追記

Information

Ubuntu 12.04 (LXDE)

はじめに

Linux Kernelに興味があったので、ソースコードリーディングでもしようかと決意。
しかし、最近のLinux Kernelって1500万行ぐらいあるらしい。(2012年の頭ぐらいの話)
・・・全部読むの無理じゃね?
と思ったが、こんな書籍を発見。

はじめてのOSコードリーディング ~UNIX V6で学ぶカーネルのしくみ (Software Design plus)

はじめてのOSコードリーディング ~UNIX V6で学ぶカーネルのしくみ (Software Design plus)

Linux以前の古いUnixカーネルだが、現在のカーネルにも応用されている基本的な作法が学べるそうだ。
行数は1万行ほどで、読もうと思えば読めるレベル。
作者ブログ : 初めてのOS source code reading(UNIX 6th source code readingのススメ) - やる気のないはてだ(A boring diary)

また、ソースコードを読む方法として「ひらメソッド」を用いて行なっていこうと思う。

環境設定

emacsの設定

基本的に以下のリンク先のまとめ
http://qiita.com/yewton@github/items/d9e686d2f2a092321e34
ソースコードを読む際に、emacsを利用する。
出てきた関数を一々grepを掛けて検索するのは面倒。
なので、GNU GLOBALを入れる。

sudo apt-get install global



emacsからgtagsを利用するので、以下のパッケージを入れる

(auto-installを入れていない場合は、下記リンクを参照のこと。
http://blog.kondoyoshiyuki.com/2012/12/17/install-auto-install-el-on-emacs/

以下を".emacs"にでも書いておく

  (setq gtags-prefix-key "\C-c")
  (require 'gtags)
  (require 'anything-gtags)
  ;; キーバインド
  (setq gtags-mode-hook
        '(lambda ()
           (define-key gtags-mode-map "\C-cs" 'gtags-find-symbol)
           (define-key gtags-mode-map "\C-cr" 'gtags-find-rtag)
           (define-key gtags-mode-map "\C-ct" 'gtags-find-tag)
           (define-key gtags-mode-map "\C-cf" 'gtags-parse-file)))
  ;; gtags-mode を使いたい mode の hook に追加する
  (add-hook 'c-mode-common-hook
            '(lambda()
               (gtags-mode 1)))

  ;; update GTAGS
  (defun update-gtags (&optional prefix)
    (interactive "P")
    (let ((rootdir (gtags-get-rootpath))
          (args (if prefix "-v" "-iv")))
      (when rootdir
        (let* ((default-directory rootdir)
               (buffer (get-buffer-create "*update GTAGS*")))
          (save-excursion
            (set-buffer buffer)
            (erase-buffer)
            (let ((result (process-file "gtags" nil buffer nil args)))
              (if (= 0 result)
                  (message "GTAGS successfully updated.")
                (message "update GTAGS error with exit status %d" result))))))))
  (add-hook 'after-save-hook 'update-gtags)

  (defun gtags-parse-file2 ()
    (interactive)
    (if (gtags-get-rootpath)
        (let*
            ((root (gtags-get-rootpath))
             (path (buffer-file-name))
             (gtags-path-style 'root)
             (gtags-rootdir root))
          (if (string-match (regexp-quote root) path)
              (gtags-goto-tag
               (replace-match "" t nil path)
               "f" nil)
            ;; delegate to gtags-parse-file
            (gtags-parse-file)))
      ;; delegate to gtags-parse-file
      (gtags-parse-file)))



コマンド 内容
\C-cs シンボル検索
\C-cr gtags-find-rtag?
\C-ct タグ検索
\C-cf メソッド定義一覧

ファイルを開いている最中に利用する場合は、M-x gtags-modeを予め実行しておくこと。
ちなみに、私の環境では、gtags-parse-file()もgtags-parse-file2()もうまく行っていない。
なんでだろ。

仕方が無いので、anything-gtags-selectを代わりに入れた。

; 以下のように変更
;(define-key gtags-mode-map "\C-cf" 'gtags-parse-file)))
(define-key gtags-mode-map "\C-cf" 'anything-gtags-select)))