GDB Common debugging commands and multi-process multithreaded debugging

Source: Internet
Author: User
Tags gdb debugger

Reprinted from: http://blog.csdn.net/freeelinux/article/details/53700266

One: General Command 1.list command
    • List LineNum shows the program around line LineNum
    • List function shows the source of functions called function
    • List shows the source program after the current line
    • List-Displays the source program in front of the current line
2.run (r) run the command.
    • The run args Run command can be directly connected to a command-line parameter value, or it can be implemented by set args + parameter values before executing run.
3.break (b) Break point, how to use:
    • b linenum break point in a line
    • b +offset/-offset the current line number before or after offset stop
    • b filename:linenum break point in a certain line of a file
    • b filename:function stop at a function entry in a file
    • b *address stop at the operating address of the program
    • b No parameters stop in the next sentence
    • b where if condition when a condition is met, stop at a line (this is useful, such as b if ret = = 5)
For the break command, we want to use it flexibly. For example, hit multiple breakpoints.    In multithreaded programs we can break points immediately after the main function is created, execute thread function entry break points, and so on. Close breakpoint: Delete (d) Breakpoint-id 4. Single-step command normal usage don't say it.
    • Step count executes count step at once, and if a function enters the function
    • Next count executes count at a time without entering the function
    • Finish runs the program until the current function finishes returning and prints the stack address and return value as well as the parameter information when the function returns
    • Until exiting the loop body (especially for the For loop, which is annoying)
5.continue command when the program is stopped, you can use the Continue (c) command to resume running the program until the program ends, or the next breakpoint is reached. It is important to note that if there is no breakpoint the program will end directly. 6.print (P) command This command is more commonly used to see what we want to see. For example, you can see all of the arrays, or from left to right: The Print command looks at the output format for the variable:
    • 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 t displays variables in binary format
    • A variable is displayed in hexadecimal format
    • c Display variables in character format
    • F Display variables by floating-point number format
7.watch Command This command is more useful. Watch is used to see if the value of an expression (a variable is an expression) changes, and if there is a change, stop the program immediately. We have several ways to set the Observer point:
    • watch   expr                 set an observation point for expression expr, and once the expression value changes, stop the program
    • Rwatch  expr                   When expression expr is read, stop the program
    • Awatch expr                   When the value of an expression is read or written, stop the program.
    • info      watchpoints       List all observers (Info directive can usually go to set)
As an example, the observation of the value of *i, once the change stopped: In the loop we can also use watch, with ignore, it is in addition to the until command is another way to let us jump out of the loop, but Watch+ignore more powerful, can jump to the first cycle. They mean to observe a variable, which can be understood as a breakpoint, how many times the breakpoint is ignore, and then can be skipped directly with continue. The 8.examine command uses this command to view the values in the memory address. The syntax is: x/u addr addr Represents a memory address. The "x/" after the N, F, u are optional parameters, n is a positive integer, indicating the length of the display memory, that is, from the current address to display the contents of several addresses, F indicates the format shown, if the address refers to a string, then the format can be S, if the address is an instruction address, then the format can be i;u Represents the number of bytes requested from the current address, and if not specified, gdb defaults to 4 bytes. The u parameter can be substituted by some characters: b for single byte, h for Double Byte, W for four bytes, and G for eight bytes. When we specify a byte length, GDB starts with the specified memory address, reads and writes the specified byte, and takes it as a value. The 3 parameters of N, F, and u can be used together, for example, the command "X/3uh 0x54320" indicates that 3 units (3) of memory are displayed in double-byte 1 units (h), 16 binary mode (U) starting from memory address 0x54320. The 9.jump command jump command does not change the contents of the program stack, generally only jumps within the same function.
    • Jump Linespec Specifies the run point of the next statement, Linespec can be linenum,filename+linenum,+offset in several forms
    • Jump address jumps to the line of code
The 10.signal command uses the signal signal name (such as SIGINT) to send the signal to the program, and if the program registers the Signal_handler function, it can also be processed to help debug the program. 11.set command
    • Set args sets command-line arguments
    • Set ENV Environmentvarname=value sets environment variables. such as: Set env User=benben
12.call command
    • Call function forces the invocation of certain functions
Forces a function to be called, which displays the function return value (if the function returns a value other than void). The Print command can also complete this function. 13.disassemble Command disassembly command to view the machine code of the source code at execution time. Two: Multi-process debugging 1. Debug Subprocess Individually we can run the program first and then use PS-EF on the other end | grep "Main" (main here is the executable file name) searched for the child process PID and then started GDB, attaching it (attach) to the GDB debugger.
    • Attach child-pid Use this command, run directly, and debug the normal program is no different.
    • Dettach Disengagement Process
2. Using the debugger option Follow-fork-mode We know that if you do not set any options, GDB defaults to debugging the parent process. Debugger options Use the following:
    • Set Follow-fork-mode mode where the selectable values for mode are parent and child, respectively, to debug the parent process and subprocess.
    • Info inferiors Querying the process being debugged
    • Inferior processnum Switching process
By default, GDB only debugs the main process when debugging a multi-process program. But GDB (>v7.0) supports multi-process and simultaneous debugging, in other words, GDB can debug multiple programs at the same time. You only need to set Follow-fork-mode (default: Parent) and Detach-on-fork (default: On). We can also use the Catch fork instruction, which stops the program if the fork is abnormal.
Follow-fork-mode detach-on-forkDescription
Parent on debug main process only (gdb default)
Child on only debugging sub-processes
Parent off simultaneously debug two processes, GDB with main process, sub-process block at fork position
Child off simultaneously debug two processes, GDB and subprocess, main process block at fork position
Setting method: Set Follow-fork-mode [Parent|child] Set detach-on-fork [On|off] Three: multithreaded debugging GDB debugging generally has two modes: all-stop mode and no-stop mode (gdb7.0    No-stop mode is not supported previously). 1.all-stop mode in this mode, when your program stops at GDB for any reason, all threads will stop, not just the current thread. In general, GDB cannot step through all of the threads. Because thread scheduling is beyond the control of GDB.    Whenever GDB stops your program, it will automatically switch to the thread that triggered the breakpoint. 2.no-stop mode (commonly used in network programming) as the name implies, the boot is not off mode. When the program is stopped in GDB, only the current thread will be stopped, and the other threads will continue to run.        At this time step,next these commands only work on the current thread. If you need to turn on No-stop mode, you can add a configuration file to ~/.gdbinit: [Python]View PlainCopy
    1. #Enable the Async interface
    2. Set Target-async 1
    3. #If using the CLI, pagination breaks non-stop
    4. Set Pagination off
    5. #Finall, turn it on
    6. Set Non-stop on
GDB supports two types of life: foreground (synchronous) and background (asynchronous). The difference is simple, synchronous in the output prompt will wait until the program report some thread has terminated the information, asynchronous is directly returned. So we need set target-async 1. Set pagination off do not appear with the Type <return> to continue prompt information.        The final step is to open. The following are common commands:
    • Info Threads Show All threads
    • Thread ID switches to the specified threads
    • Break Filename:linenum thread all sets a breakpoint on the corresponding line of all threads, note that if the main thread does not execute to that row and starts All-stop mode, the main thread executes n or s to switch past
    • Set scheduler-locking off|on\step default off, execution S or c other threads are also executed synchronously. On, only the current proportionate execution. Step, only when the front-line execution
    • Show Scheduler-locking shows the current mode
    • Thread apply all command to execute the consent command for each thread, such as BT. or thread apply 1 3 bt, that is, thread 1, 3 executes BT.
Mainly we want to use on the use of, such as No-stop mode, general multi-threaded debugging is very useful. Four: Core file
    • Ulimit-c Unlimited generates a core file, or it can be a specified size, and then use GDB./main core Launch, BT view the call stack.
Reference: 1190000003733061 http://www.cnblogs.com/frankbadpot/archive/2010/06/23/1762916.html

GDB Common debugging commands and multi-process multithreaded debugging

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.