Fork me on GitHub

4/20/2012

[Linux] Use Debugfs on Android

Android 在 user space 可以在 run-time 的時候透過 adb shell getprop/setprop 來動態讀寫變數。所以,kernel 開發的過程中,我在想有沒有類似的東西讓我可以動態的存取變數,這樣 debug 會方便許多。

於是,發現了 Linux 其實已經有提供一個 light-weight 的 filesystem 叫做 Debugfs, 正好可以滿足我的需求,紀錄一下使用方式。

  1. Enable Debug FileSystem

    Kernel hacking
        [*] Debug filesystem
    
    mount -t debugfs none /sys/kernel/debug // Do this manually
    
  2. Include header

    #include <linux/debugfs.h>
    
  3. Call the API to create dir and file, then put them into the module init func

    struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
    struct dentry *debugfs_create_u8(const char *name, mode_t mode, struct dentry *parent, u8 *value)
    
    static struct dentry *dbg_entry_dir, *dbg_entry_test;                                           
    u8 dbg_print_test = 0;
    
    static int debugfs_init(void) {
        dbg_entry_dir = debugfs_create_dir("test_debugfs", NULL);
        if (!dbg_entry_dir)
            return -1;
    
        dbg_entry_test = debugfs_create_u8("print_test", 0644, dbg_entry_dir, &dbg_print_test);
        if (!dbg_entry_test)
            return -1;
    
        return 0;
    }
    
  4. Remember to remove after module remove

    void debugfs_remove(struct dentry *dentry)
    
    debugfs_remove(dbg_entry_dir); // Put this in module remove func
    
  5. Use the filenode you create to debug

    adb shell cat sys/kernel/debug/test_debugfs/print_test
    
    adb shell;
    echo 1 > sys/kernel/debug/test_debugfs/print_test
    

Reference

Linux Debugfs文件系統介紹及使用

Debugfs

Linux内核里的DebugFS

Debugfs

Kernel debugging

-- EOF --

4/07/2012

[C] variable/function scope in C

習慣 C++ 後換到 C 這種更精簡的語言,反而不是很清楚變數的生命週期,以及在不同程式單元共用的方式,所以 Google 了一篇 Scope and Storage Classes in C,在此筆記一下!

總而言之,4 scope, 4 specifier, 2 modifier

4 scope

Block
所謂 block 就是 { 和 } 包圍起來的區域,block scope 就是指自變數宣告處一直到 } 的生命週期。

Function
在 { 和 } 內都存活的生命週期。In C, only the goto label has function scope。

File
即 static + (program scope variable)

Program
當變數在函數以外宣告則為 program scope,也就是所謂的全域變數。全域變數欲在其他檔案存取時,需以 extern 關鍵字 declare 一次。

4 specifier

auto
只有在 block scope 的變數可以使用這個 specifier,但是 block scope 的變數本來就是 temp 的,所以這個很少用。

static
static 可以用在 block scope 或 program scope,當用在 block scope 時,指稱該變數具有 permanent duration,也就是出了該 block 該變數仍存在的意思;當用在 program scope 時,也就是用來修飾全域變數時,即把 program scope 縮小為 file scope,除了該檔案其他檔案無法使用。

register
建議 compiler 將變數儲存到 CPU 暫存器的修飾子。

extern
欲在另一檔案中使用某檔案之 program scope 變數,需要在該檔案 file scope 內搭配 extern 再宣告一次欲使用之變數。

2 modifier

const
const 變數宣告即必須初始化,並且之後不能再被賦值。

volatile
When the C compiler optimizes your program automatically, it intends to not update the value held by the variable unless the variable is on the left side of an assignment operator (=).

volatile 變數 tells the compiler not to optimize any expressions of the variable because the value saved by the variable may be changed without execution of any explicit assignment statement.

以上是變數的生命週期,另外函數的 scope 則相對簡單,沒有修飾子的函數即為全域函數,如果在前面加上 static 修飾子,就變成 file scope。如果另外一個檔案要使用 file scope 的函式,則需加上 extern 修飾子聲明。

接下來一定會有一個疑問,就是在 C 的世界裡面,要怎麼實現 private function 呢?在 C 裡頭並沒有強的 module 概念,可以參考 Ref2 當中的 translation unit,一個 translation unit 就是 source file 加上他包含的 header file (包括巢狀 include 當中的)。

所以,如果要實現 private function 的話,一樣也是用 static 關鍵字,將 function scope 縮小至 file scope,然後需要調用該函數的內部函數記得將 declaration 加到 header file 當中即可。

References

  1. How to implement a “private/restricted” function in C?
  2. Concept of modules in c

-– EOF --