Try the Debug tool under Linux: GDB

Source: Internet
Author: User
Tags documentation

[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 has 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) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or Later This is the free software:you was 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 is configured as "X86_64-linux-gnu".
Type "Show Configuration" for configuration details.
for bugs reporting instructions, please see:
Find the GDB manual and other documentation resources online at:
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 Command Reference

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

Note

GDB also has Windows

    • Layout: For splitting windows, you can view the code while testing:
    • Layout src: Display source code window
    • Layout asm: Display Disassembly window
    • Layout regs: Display source code/disassembly and CPU Register window
    • Layout split: Displaying source code and Disassembly window
    • Ctrl + L: Refresh window

┌──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)

Try the Debug tool under Linux: GDB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.