gdbkerneldebug的程式斷點

stormbm發表於2018-05-11

gdb除錯kernel的時候, 如果設定通用函式斷點, 比如vfs_read, 就會遇到一堆撞到斷點的地方, 比如tty輸入一個字元, 就是vfs_read, 沒辦法除錯具體的某一個程式

一種辦法就是條件斷點, 其實不是很好用, 比如用pid, 但是有時候這個程式還沒啟動, 比如task的comm來判定, 但是kernel中是不支援strcmp來判斷字串是否相等, 因為需要跑函式

gdb 7.5對此問題做了增強, gdb自己去比較字串, 而不需要機器去跑程式碼

https://sourceware.org/gdb/current/onlinedocs/gdb/Convenience-Funs.html#Convenience-Funs

b do_fault if $_streq($lx_current()->comm, "a.out")

還可以對呼叫者來做條件斷點, 比如a->c, b->c, 斷點只停在b呼叫c的地方

$_caller_is(name[, number_of_frames])

    Returns one if the calling function’s name is equal to name. Otherwise it returns zero.

    If the optional argument number_of_frames is provided, it is the number of frames up in the stack to look. The default is 1.

    Example:

    (gdb) backtrace
    #0  bottom_func ()
        at testsuite/gdb.python/py-caller-is.c:21
    #1  0x00000000004005a0 in middle_func ()
        at testsuite/gdb.python/py-caller-is.c:27
    #2  0x00000000004005ab in top_func ()
        at testsuite/gdb.python/py-caller-is.c:33
    #3  0x00000000004005b6 in main ()
        at testsuite/gdb.python/py-caller-is.c:39
    (gdb) print $_caller_is ("middle_func")
    $1 = 1
    (gdb) print $_caller_is ("top_func", 2)
    $1 = 1


相關文章