Linux gdb debugging and Linuxgdb debugging

Source: Internet
Author: User

Linux gdb debugging and Linuxgdb debugging

I don't have much to talk about gdb. I will go to the topic directly.


I. gdb basic command list:

Command Explanation Shorthand
File Load the executable file to be debugged None
List List part of the source code that generates the execution File L
Next Execute a line of source code but do not enter the function. N
Step Execute a line of source code and enter the function. S
Run Execute the program currently being debugged R
Continue Continue Program Execution C
Quit Terminate gdb Q
Print Output the value of the specified variable P
Break Set breakpoints in code B
Info break View the breakpoint setting information Ib
Delete Delete a breakpoint D
Watch Monitor the value of a variable. Once the value changes, the program stops. Wa
Help Help commands in gdb H


Ii. Examples of using gdb commands:

1. Create a source file vi yrp. cc. The source code is as follows:

# Include <iostream>
Using namespace std;
Void swap (int & a, int & B)
{
Int tmp;
Tmp =;
A = B;
B = tmp;
}
Int main ()
{
Int I, j;
Cout <endl <"Input two int number:" <endl;
Cin> I> j;
Cout <"Before swap (), I =" <I <"j =" <j <endl;
Swap (I, j );
Cout <"After swap (), I =" <I <"j =" <j <endl;
Return 0;
}


2. generate an executable fileG ++-g-o yrp. ccNote that the-g parameter must be used, and debugging information will be added to the compilation; otherwise, the execution file cannot be debugged.



3. Start debuggingGdb yrp



Load the file yrp of the program to be debugged



4. View source filesList 1, (Starting from the first line) Press enter to repeat the previous command



5. Set debugging breakpointsBreak 16, Set a breakpoint in row 16th,Info breakView breakpoint information (ib)



6. debugging run InputRun



7. Single-step debuggingStepTo enter the function.



8. View VariablesPrintExample print B



9. View function stacksBt, Exit the FunctionFinish



10. Continue running until the next breakpoint or main function ends.Continue



11. Exit debuggingQuit





End!







In linux, how does one use gdb to debug multiple c files?

We recommend that you use makefile to compile several files into an executable file. Remember to add the-g parameter during the compilation process, in this way, the compiled executable file can be tested through gdb. Note that the makefile syntax is very demanding, and the space cannot be wrong. If you want to learn advanced programming in the linux environment and do not use makefile, it will be very difficult. I wish you a smooth learning {100% original, just give the score as appropriate}

If makefile is not used
Gcc-g-o a. out 1.c 2.c 3.c 4.c 5.c
A. out is the generated file name. Others, you know!

How to debug Linux C ++ with gdb?

Since it will be debugged in windows, it will be the same in linux.
First, set the PATH of the linux environment variable. If you want to use it temporarily, you can directly use it in the shell terminal:
Export PATH = $ PATH: [PATH you want to set]
$ PATH is equivalent to % PATH % in win, separated by a colon (:), which is equivalent to a semicolon (;) in win;
To take effect permanently, you can:
Cd ~ // Go to the user directory
Vi. bash_profile // open the property File
Export PATH = $ PATH: [PATH you want to set] // The same settings
: Wq // save
This setting is complete. By default, this property file is generated only when the user logs on. to take effect immediately, you can:
Source ~ /. Bash_profile

Is the compiler embedded in linux? Is gcc g ++ gdb installed in linux? Refer to this article:
Blog.csdn.net/..948806

For the commands used for debugging, Simply put:
1. Use the-g parameter during compilation, for example, gcc-g foo. c-o foo.
2. Perform debugging, for example:
Gdb./foo // debug foo
(Gdb) l // list source code
(Gdb) B 5 // Add a breakpoint to row 5th
(Gdb) r // run debugging
(Gdb) p size // assume that there is a variable of size, so let's look at its value.
(Gdb) c // continue execution until the next breakpoint
(Gdb) n // execute the next statement
(Gdb) s // enter the execution function.
....
No, read this article.
Blog.csdn.net/..935052
 

Related Article

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.