尝试linux下的debug工具: gdb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试linux下的debug工具: gdb相关的知识,希望对你有一定的参考价值。

[email protected]:~/projects/ConsoleApplication1$ cat main.cpp
#include <cstdio>

int main()
{
// int a = 1;
printf("hello from ConsoleApplication1!\n");
return 0;
}
[email protected]:~/projects/ConsoleApplication1$ cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8)

project(coroutine)
SET(CMAKE_CXX_COMPILER "g++")

set(CXX_FLAGS "-std=c++11 -Wall -Werror -fPIC")

set(CMAKE_CXX_FLAGS "-g ${CXX_FLAGS}")

set(CMAKE_CXX_FLAGS_RELEASE "-O3 ${CXX_FLAGS}")

add_executable(testProgram main.cpp)

[email protected]:~/projects/ConsoleApplication1$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hjs/projects/ConsoleApplication1
[email protected]:~/projects/ConsoleApplication1$ make
[100%] Built target testProgram

[email protected]:~/projects/ConsoleApplication1$ ls
bin CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt CMakeLists.txtY main.cpp Makefile obj testProgram
[email protected]:~/projects/ConsoleApplication1$ ./testProgram
hello from ConsoleApplication1!

[email protected]:~/projects/ConsoleApplication1$ gdb ./testProgram
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./testProgram...done.
(gdb)

(gdb) l
1 #include <cstdio>
2
3 int main()
4 {
5 // int a = 1;
6 printf("hello from ConsoleApplication1!\n");
7 return 0;
8 }
(gdb) b 6
Breakpoint 1 at 0x40078a: file /home/hjs/projects/ConsoleApplication1/main.cpp, line 6.
(gdb) r
Starting program: /home/hjs/projects/ConsoleApplication1/testProgram

Breakpoint 1, main () at /home/hjs/projects/ConsoleApplication1/main.cpp:6
6 printf("hello from ConsoleApplication1!\n");
(gdb)

(gdb) c

Continuing.
hello from ConsoleApplication1!
[Inferior 1 (process 3005) exited normally]

gdb命令参考

 

http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/gdb.html

 

Note

gdb也有窗口

  • layout:用于分割窗口,可以一边查看代码,一边测试:
  • layout src:显示源代码窗口
  • layout asm:显示反汇编窗口
  • layout regs:显示源代码/反汇编和CPU寄存器窗口
  • layout split:显示源代码和反汇编窗口
  • Ctrl + L:刷新窗口

┌──Register group: general──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│rax 0x400786 4196230 rbx 0x0 0 rcx 0x0 0 │
│rdx 0x7fffffffe5d8 140737488348632 rsi 0x7fffffffe5c8 140737488348616 rdi 0x1 1 │
│rbp 0x7fffffffe4e0 0x7fffffffe4e0 rsp 0x7fffffffe4e0 0x7fffffffe4e0 r8 0x400810 4196368 │
│r9 0x7ffff7de78e0 140737351940320 r10 0x846 2118 r11 0x7ffff7a2e740 140737348036416 │
│r12 0x400690 4195984 r13 0x7fffffffe5c0 140737488348608 r14 0x0 0 │
│r15 0x0 0 rip 0x40078a 0x40078a <main()+4> eflags 0x246 [ PF ZF IF ] │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
│4 { │
│5 // int a = 1; │
B+>│6 printf("hello from ConsoleApplication1!\n"); │
│7 return 0; │
│8 } │
│9 │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
native process 3007 In: main L6 PC: 0x40078a
[Inferior 1 (process 3006) exited normally]
(gdb) layout regs
(gdb) r
Starting program: /home/hjs/projects/ConsoleApplication1/testProgram

Breakpoint 1, main () at /home/hjs/projects/ConsoleApplication1/main.cpp:6
(gdb) Quit
(gdb)

以上是关于尝试linux下的debug工具: gdb的主要内容,如果未能解决你的问题,请参考以下文章

Linux Debugging 一次生产环境下的“内存泄露”

Trace32 ubuntu(linux) 下的安装

如何使用eclipse中的debug工具,调试部署在weblogic中的应用?

Linux环境下weblogic12开启远程debug端口

四:(之八_Linux压力测试工具stress)Dockerfile语法梳理和实践

Linux内核二常用的网络丢包错包debug工具介绍