gdb小技巧总结

gdb日志使用记录

1、显示结构体定义:

(gdb) ptype struct kmem_cache_cpu
type = struct kmem_cache_cpu {
  void **freelist;
  unsigned long tid;
  struct page *page;
  struct page *partial;
}

(gdb) 

2、显示结构字段偏移

(gdb) ptype /o struct kmem_cache_cpu
/* offset | size */ type = struct kmem_cache_cpu {
/* 0 | 8 */ void **freelist;
/* 8 | 8 */ unsigned long tid;
/* 16 | 8 */ struct page *page;
/* 24 | 8 */ struct page *partial;

                /* total size (bytes): 32 */
                         }
(gdb)

3、backtrace

bt -5 显示栈底5层栈
bt 5 显示栈顶5层栈
set height 0 不分页输出
set pagination off 关闭分页

 

4、command命令,实现自动化

set pagination off
set logging file gdb.log
set logging overwrite
set logging on
break sum if 0 == (a%5)
commands
silent
printf "\n"
p \$rdi
bt
continue
end

 

5、gdb断点禁止线程切换

set scheduler-locking on/off
b func thread x

 

6、gdb trap相关

handle SIGTRAP nostop noprint //不中断,不打印提示
设置watchpoint后,
x86上gdb不停收到SIGTRAP,可以查看eflags寄存器值,TF位置被置1了
修改为0后不再收到sigtrap:
set  \$ps &= ~(1<<8)
【on x86-based machines \$ps is an alias for the eflags register, TF is the 9th bit of eflags】

 

7、自定义变量

(gdb) info locals
object = 0xffff800005803400
(gdb) p object
\$11 = (void *) 0xffff800005803400
(gdb) set \$foo=object
(gdb) p \$foo
\$12 = (void *) 0xffff800005803400

 

8、循环打印链表

(gdb)set \$foo = next
(gdb)while 1 //while \$foo
>print \$foo
>set \$foo = next->next
>end

 

posted @ 2021-11-19 20:03  kitiz  阅读(447)  评论(0)    收藏  举报