GNU Debugger (GDB)

GDB macros

Here are some GDB macros for debugging Linux kernel:

define ps
    dont-repeat
    # 4 for 32bit kernels. 8 for 64bit kernels.
    set $sz = sizeof(long)
    set $tasks = (struct list_head *)init_task->tasks
    set $offset = (char *)&init_task->tasks - (char *)&init_task
    set $task = $tasks
    set $task_entry = (struct task_struct *)((char *)$task - $offset)
    if ($sz == 4)
        printf "Pointer       PID      Command\n"
    else
        printf "Pointer               PID      Command\n"
    end
    printf "0x%-12lx%-9d%s\n", $task_entry, $task_entry->pid, $task_entry->comm
    set $task = $task->next
    while $task != $tasks
        set $task_addr = (char *)$task - $offset
        set $task_entry = (struct task_struct *)((char *)$task - $offset)
        if ($task_entry->pid) != 0
            printf "0x%-12lx%-9d%s\n", $task_entry, $task_entry->pid, $task_entry->comm
            if ($sz == 4)
                printf "0x%-12lx%-9d%s\n", $task_entry, $task_entry->pid, $task_entry->comm
            else
                printf "0x%-20lx%-9d%s\n", $task_entry, $task_entry->pid, $task_entry->comm
            end
        end
        set $task = $task->next
    end
end

document ps
Report a snapshot of the current processes.
end

define lsmod
    dont-repeat
    # 4 for 32bit kernels. 8 for 64bit kernels.
    set $sz = sizeof(long)
    set $mod = (struct list_head *)modules
    if ($sz == 4)
        printf "Pointer       Address       Name\n"
    else
        printf "Pointer               Address               Name\n"
    end
    while 1
        set $mod_entry = (struct module *)((char *)$mod - $sz)
        if ($sz == 4)
            printf "0x%-12lx0x%-12lx%s\n", $mod_entry, $mod_entry->module_core, $mod_entry->name
        else
            printf "0x%-20lx0x%-20lx%s\n", $mod_entry, $mod_entry->module_core, $mod_entry->name
        end
        set $mod = $mod->next
        if ($mod == &modules)
            loop_break
        end
    end
end

document lsmod
Show the status of modules in the Linux kernel.
end

define log
    dont-repeat
    printf "%s", log_buf
end

document log
Dump system message buffer.
end

None: GDB (last edited 2009-10-15 02:48:28 by ZhigangWang)