The GDB debugger usage of the tool article

Source: Internet
Author: User
Tags terminates gdb debugger

The role GDB accomplishes:

    1. Start the program, you can run the program as required by the engineer's custom
    2. Let the debugged program stop at the breakpoint specified by the engineer, and the breakpoint can be a conditional expression
    3. When the program is stopped, you can check what is happening in this program and
    4. Dynamically changing the operating environment of the program

Problems:

    1. How do I print the value of a variable? (Print Var)
    2. How do I print the address of a variable? (Print &var)
    3. How do I print the data value of an address? (Print *address)
    4. How do I view the currently running files and rows? (BackTrace)
    5. How do I view the code for a specified file? (List file:n)
    6. How do I execute the current function immediately, but not complete the entire application? (finish)
    7. How the program is multi-file, how to navigate to the specified file of the specified line or function? (List file:n)
    8. How many loops, how do I finish the current loop? (until)
    9. How is multithreading debugged?
GDB Common Commands
    • BackTrace (or BT): View all levels of function calls and parameters
    • Finish: Runs continuously until the current function returns, and then stops waiting for the command to run out of the current function
    • FRAME (or F) Framing number: Select Stack frame
    • info (or i) Locals: View the value of the current stack frame local variable
    • List (or L): Lists the source code, followed by the last position, column 10 row at a time
    • List line number: Lists the source code starting from the first line
    • List function Name: Lists the source code for a function
    • Next (or N): executes the next statement
    • Print (or P): Prints the value of the expression, which can be modified by the expression to modify the value of the variable or call the function
    • Quit (or Q): Exit GDB Debug Environment
    • Set VAR: Modifying the value of a variable
    • Set args parameter 1 ... : Set parameters passed in
    • Start: Begin executing the program, stop waiting for the command in front of the first line of the main function
    • Step (s): Executes the next statement and, if there is a function call, enters the function
Example 1. Start GDB
gdb

This allows you to interact with GDB.

2. Start GdB and split-screen display source code
gdb -tui

In this way, using the '-tui ' option, the start can be directly divided into two parts of the screen, the above display source code, more convenient than the list. At this time using the upper and lower arrow keys can produce the source code,
You want the command line to use the up and down keys with [Ctrl + N] and [Ctrl + P].

3. Start GDB debugging the specified program app
gdb app

This allows the app to be loaded directly after GDB is launched, and it is important to note that the loaded app must have GDB debugging options at compile time, such as ' Gcc-g app App.c ',
Note that if you modify the source code of the program, but do not compile, then the display in GDB will be the modified source code, but the run is the pre-change program, which will lead to tracking confusion.

4. After starting the program, then debug with GDB
gdb <program> <PID>

Over here

5. After starting the program, start GDB debugging again
gdb <PID>

Here, the program is a service program, then you can specify the process ID of this service program runtime,

6. Interactive command after starting GDB

Interactive Commands Support Tab completion

7. Display Help information
(gdb) help
8. Procedures for loading into execution
(gdb) file app

This loads the executable program app that you want to debug in GDB. If you just started gdb instead of launching it with the GDB app, you can load the app and, of course, compile the app to add the-G
Debugging options.

9. Re-run the Debug program
(gdb) run

To run a program ready for debugging, you can use the Run command, followed by any parameters that are sent to the program, including standard input and standard output specifiers (< and >) and Shell wildcard characters
(* 、?、 [,]).

10. Modify the parameters sent to the program
(gdb) set args no

Here, assuming that the "R Yes" setting program startup parameter is yes, then set args here sets the parameter argv[1] to No.

11. Display the default parameter list
(gdb) show args
12. List the code for the specified range (N1 to N2)
(gdb) list n1 n2

This way, the list can be abbreviated to L, and the code between the N1 and N2 lines will be displayed, and if you start gdb with-tui, it will be displayed in the appropriate location. Without the N1 and N2 parameters, you will
By default, the current line and the following 10 rows are displayed, and then the 10 rows are executed and rolled down. In addition, the list can be followed by the function name.

In general, following these parameters can be followed by the list:

    • <+offset>: Positive offset of the current line number
    • <-OFFSET>: Negative offset of the current line number
    • <*ADDRESS>: The address of the statement in memory when the program runs
13. Perform the next step
(gdb) next

This way, a line of code is executed, and the function is skipped if it is a function. This command is simplified to n.

14. Perform the next N times
(gdb) next N
15. Execute the last command executed
(gdb) [Enter]

Here, the direct input carriage will execute the last command.

16. Single Step Into
(gdb) step

In this way, a line of code is executed, but if a function is encountered, it goes inside the function and executes on one line.

17. Perform the function returned by the current function to call it
(gdb) finish

Here, run the program until the current function has finished running back and then stop. For example, to enter a single step execution if you have entered a function and want to exit the function to return to his calling function, you can use finish.

18. Specify the program until you exit the current loop body
(gdb) until

Abbreviated to U. You need to stop the cursor at the head of the loop and then enter u to automatically perform all the loops.

19. Jump to the 5th line of execution Program
(gdb) jump 5

Abbreviated to "J 5". It is important to note that the jump to the 5th line after execution, if there is no power outage then continue to execute, and not to stop there. Also, jumps do not change the current stack contents,
So jumping to other functions will have strange phenomenon, so the jump is best in a function inside, the parameters of the jump can also be the address of the program code line, function name and so on like list.

20. Force the return of the current function
(gdb) return

This will ignore the statement that the current function has not finished executing, forcing the return. Return can be followed by an expression, the return value of the expression is the return value of the function.

21. Force Call function
(gdb) call <expr>

Over here

22. Force Call Function 2
(gdb) print <expr>

Here, the print and call functions are similar, except that if the return value of the function is void then call does not print the return value, but print prints the return value of the function and stores it in the
The history record.

23. Set breakpoints in a line in the current file (assuming 6)
(gdb) break 6
24. Set Conditional breakpoints
(gdb) break 46 if testsize == 100

Here, if testsize = = 100 Sets the breakpoint at line 46.

24. Detect the change in expression stop
(gdb) watch i != 10

Here, I! = 10 Once the expression has changed, it stops. Watch

25. Set breakpoints in the current file for a function (assumed as func)
(gdb) break func
26. Set a breakpoint at a line (N) of the specified file (filename)
(gdb) break filename:N

Here, it is similar to setting breakpoints on a function in a file.

27. Display the current GDB breakpoint information
(gdb) info breakpoints

Here, you can shorthand for info break. All current breakpoints, breakpoint numbers, breakpoint positions, and so on are displayed.

28. Remove the N breakpoint
(gdb) delete N
29. Delete all Breakpoints
(gdb) delete
30. Clear all breakpoints above row N
(gdb) clear N
31. Continue running the program directly to the next breakpoint
(gdb) continue

Here, you run without a breakpoint. # # # # # 38. Terminate a program that is being debugged

(gdb) kill

Here, entering kill terminates the program you are debugging.

Print display variable (VAR) value
(gdb) print var

Here, the print shorthand for P,print is a very powerful command of GDB, which can be used to display any valid expression in the language being debugged. Expression in addition to the variables in your program,
It can also include function calls, complex data structures, history, and so on.

32. Displaying functions in the current call function stack
(gdb) backtrace

Abbreviated BT. The command produces a list of all the valid processes starting with the most recent process and the parameters that call those procedures. Of course, it will also show where it is currently running (file, line).

33. View the locale of the current debugger
(gdb) show language

Here, if GDB does not recognize the program you are debugging, it is the C language by default.

34. View the program language of the current function
(gdb) info frame
35. Display the current debug source file
(gdb) info source

This displays the current source code file information, such as file name, program language, and so on.

36. Manually set the current program language to C + +
(gdb) set language c++

Here, if GDB does not detect your programming language, you can set this up.

37. View the program languages you can set
(gdb) set language

Here, you can view the programming languages that can be set in GDB using set language without parameters.

38. Terminating a program that is being debugged
(gdb) kill

Here, entering kill terminates the program you are debugging.

A. Print display variable (VAR) value
(gdb) print var

Here, the print shorthand for P,print is a very powerful command of GDB, which can be used to display any valid expression in the language being debugged. Expression in addition to the variables in your program,
It can also include function calls, complex data structures, history, and so on.

40. Using the 16 binary display (VAR) value
(gdb) print /x var

Print can perform the display format, where '/X ' is used to represent the 16 binary format.

The variable display formats that can be supported are:

    • X: Display variables in hexadecimal format
    • D: Display variables in decimal format
    • U: Display unsigned integer in hexadecimal format
    • O: Display variables in octal format
    • T: Display variables in binary format
    • A: Display variables in hexadecimal format
    • C: Display variables in character format
    • F: Display variables in floating-point number format
41. If a is an array, 10 elements, if you want to display the
(gdb) print *[email protected]

In this way, 10 elements are displayed, regardless of whether a is double or int, 10 elements are displayed correctly.

42. Changing the value of variables at runtime
(gdb) print x=4

Here, x=4 is the C/s + + syntax, meaning to change the variable x value to 4, if you are currently debugging the language is Pascal, then you can use Pascal's syntax: X:=4.

43. Display a variable var type
(gdb) whatis var
44. Display the variable var type in a more detailed way
(gdb) ptype var

Here, the structure definition of VAR is printed out.

The GDB debugger usage of the tool 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.