让gdb能更方便的调试nginx:查看ngx_array_t,ngx_list_t等结构

为了方便的调试nginx, 查看nginx的一些链表,数组等数据结构中的存储的元素的值,特地写了几个gdb的函数宏, 可以方便的查看.使用方法:
   把如下内容存入一个 名字 nginx_view.gdb 的文件里. 并把文件放在 ~/.gdb/ 目录下.启动gdb的时候, 运行如下命令即可: source ~/.gdb/nginx_view.gdb

define pnarr
     if $argc == 0
         help pnarr
     else
         set $size = $arg0.nelts
         set $capacity = $arg0.nalloc
         set $i = 0 
        while $i < $size 
            printf "elem[%d]: ", $i
             p *(($arg1*)($arg0.elts) + $i)
             set $i++
         end 
        printf "Priority array size = %u\n", $size
         printf "Priority array capacity = %u\n", $capacity
     end 
end
 
document pnarr
     Prints ngx_array_t information:all elements, size and capacity of the arr_var
     Syntax: pnarr arr_var elem_type                                      
     Example:
         pnarr cycle->pathes ngx_path_t​
end


define pnlist
     if $argc == 0
         help pnlist
     else
         set $size = $arg0.part.nelts
         set $capacity = $arg0.nalloc
         set $part = &($arg0.part)
         set $data = ($arg1*)($part->elts)
         set $i = 0 
         set $continue = 1
         while $continue == 1 
            if $i >= $part->nelts
                if $part->next == 0
                   set $continue = 0 
                end

                if $continue
                    set $part = $part->next
                    set $data = ($arg1*)$part->elts
                    set $i = 0
                end
            end

            if $continue
                printf "elem[%d]: ", $i
                p *$data
                set $i++
            end
         end 
         printf "frist list part's size = %u\n", $size
         printf "frist list part's capacity = %u\n", $capacity
     end 
end

document pnlist
     Prints ngx_list_t information : all elements, size and capacity of the list_var
     Syntax: pnlist list_var elem_type
end

define pnmodule
    if $argc == 0
        help pnmodule
    else
        set $i = $arg0
        echo \n
        printf "-----------------module[%d]---------------------\n", $i
        p ngx_modules_name[$i]
        
        echo \nmodule's type : 
        if ngx_modules[$i].type == 0x45524F43
            echo core_module
        end 
        if ngx_modules[$i].type == 0x464E4F43
            echo conf_module
        end 
        if ngx_modules[$i].type == 0x544E5645 
            echo event_module
        end 
        if ngx_modules[$i].type == 0x4C49414D 
            echo mail_module
        end 
        if ngx_modules[$i].type == 0x50545448 
            echo http_module 
        end 
        echo \n


        p *ngx_modules[$i]
        set $cmd = ngx_modules[$i]->commands


        if $cmd != 0
            set $j = 0 
            while $cmd[$j].name.len !=0 
                # command's type
                echo \n 
                echo command's type : 
                if $cmd[$j].type&0x01000000
                    echo main_conf,
                end 
                if $cmd[$j].type&0x0F000000
                    echo any_conf,
                end 
                if $cmd[$j].type&0x00000100
                    echo conf_block,
                end 
                if $cmd[$j].type&0x00000200
                    echo conf_flag,
                end 
                echo \n

                # command's detail
                p $cmd[$j]
                set $j++
            end 
        end 
    end
end

document pnmodule
    Print ngx_modules's infomation.
end


define pnallmodule
    set $i = 0
    set $module = ngx_modules[$i]
    while $module != 0
        pnmodule $i
        set $i++
    end
end

举例:

 
 (gdb) pnarr 
 
Prints ngx_array_t information.
Syntax: pnarr arr_var elem_type: Prints all elements, size and capacity of the arr_var
Example:
pnarr arr - prints all elements, size and capacity of arr


// 查看 ngx_cycle_s.pathes 内容, pathes是个array, 元素是ngx_path_t
(gdb) pnarr cycle->pathes ngx_path_t
elem[0]: $5 = {
  name = {
    len = 135379312, 
    data = 0x811c000 ")"
  }, 
  len = 135383844, 
  level = {135392488, 135400956, 0}, 
  manager = 0, 
  loader = 0, 
  data = 0x0, 
  conf_file = 0x0, 
  line = 9
}
elem[1]: $6 = {
  name = {
    len = 45, 
    data = 0x8109615 "/home/nemo/progs/nginx/sbin/../logs/error.log"
  }, 
  len = 0, 
  level = {0, 0, 10}, 
  manager = 0x2e, 
  loader = 0x811ba90, 
  data = 0x0, 
  conf_file = 0x0, 
  line = 0
}
elem[2]: $7 = {
  name = {
    len = 0, 
    data = 0x0
  }, 
  len = 0, 
  level = {0, 0, 0}, 
  manager = 0, 
  loader = 0, 
  data = 0x0, 
  conf_file = 0x0, 
  line = 0
}
elem[3]: $8 = {
  name = {
    len = 0, 
    data = 0x0
  }, 
  len = 0, 
  level = {0, 0, 0}, 
  manager = 0, 
---Type <return> to continue, or q <return> to quit---
  loader = 0, 
  data = 0x0, 
  conf_file = 0x0, 
  line = 0
}
elem[4]: $9 = {
  name = {
    len = 0, 
    data = 0x0
  }, 
  len = 0, 
  level = {0, 0, 0}, 
  manager = 0, 
  loader = 0, 
  data = 0x0, 
  conf_file = 0x0, 
  line = 0
}
Priority array size = 5
Priority array capacity = 10

你可能感兴趣的:(让gdb能更方便的调试nginx:查看ngx_array_t,ngx_list_t等结构)