使用 clion 远程调试

步骤

server 端

  • gdbserver :1234 vcmu
  • gdbserver :1234 --attach pid
  • scp 192.168.0.0:/home/test .

client 端

  1. run -> edit configurations -> + -> RemoteDebug -> tcp:192.168.0.102:1234
  2. 设置 Path mapping -> remote + local, 有可以设置 symbol file 的地方
  3. 打断点即可,点击 虫子按钮即可调试

debug

  1. shift + F8 执行完当前函数
  2. F7 进入函数
  3. F8 不进入函数
  4. 移动 curosr -> 点击运行到 cursor,可以跳出循环

技巧

  1. set print pretty on => 可以很漂亮地打印结构体;还可以设置 C++ 相关的
  2. print g_struct[0] => 可以打印全局变量的变量
  3. bt => 函数调用栈,程序挂死或者崩掉,都可以用 GDB 查看调用链
  4. 内核函数调用栈 => 驱动中使用 NULL 指针,kernel panic 即可打印调用链

多线程/进程调试

  1. set scheduler-locking on => 只运行当前线程
  2. set follow-fork-mode [parent|child] => 必须detach-on-fork 为 0 才有效
    • parent: fork之后继续调试父进程,子进程不受影响。
    • child: fork之后调试子进程,父进程不受影响。
  3. set detach-on-fork [on|off]
    • on: 断开调试follow-fork-mode指定的进程。
    • off: gdb将控制父进程和子进程。follow-fork-mode指定的进程将被调试,另一个进程置于暂停(suspended)状态。
  4. info thread
  5. thread i
  6. prctl(PR_SET_NAME, "xx"); 设置线程名,方便在 clion 中查看

查看 gdb 命令

help 命令使用

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Type "apropos -v word" for full documentation of commands related to "word".

help 命令选项

aliases -- User-defined aliases of other commands.
breakpoints -- Making program stop at certain points.
data -- Examining data.
files -- Specifying and examining files.
internals -- Maintenance commands.
obscure -- Obscure features.
running -- Running the program.
stack -- Examining the stack.
status -- Status inquiries.
support -- Support facilities.
text-user-interface -- TUI is the GDB text based interface.
tracepoints -- Tracing of program execution without stopping the program.
user-defined -- User-defined commands.
  • help data => set print, ...
  • help status => info
  • help files => file
  • help stack => backtrace, frame(栈帧)
  • help running => 运行时使用
posted @ 2023-08-16 01:22  chumoath  阅读(579)  评论(0)    收藏  举报