A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
GDB Basic Commands This article describes common commands for using the GDB debugger. Main content: [introduction] [Example] [other] [introduction]=============GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. If you are doing software under the UNIX platform, you will find that GDB has a more powerful debugging tool than VC, BCB's graphical debugger. GDB also has a graphical debug side such as DDD. In general, GDB mainly accomplishes the following four functions: (1To start your program, you can run the program at your own pace according to your customized requirements. (2) allows the program being debugged to stop at the breakpoint you have specified for the reset. (A breakpoint can be a conditional expression) (3When the program is stopped, you can check what happens in your program at this time. (4dynamically change the execution environment of your program. Interest is the best teacher, here first to summarize in the process of debugging frequently encountered problems. Learning and practicing with these questions can help deepen the impression. Then is my practice in the process of summarizing the common commands, if there are any problems or suggestions, you can contact me, thank you! ^_^ (1How do I print the value of a variable? (Printvar) (2How do I print the address of a variable? (Print &var) (3How do I print the data value of an address? (Print *address) (4how do I see which files and rows are currently running? (BackTrace) (5How do I see the code for the specified file? (List file:n) (6How do you finish the current function immediately, but not the entire application? (finish) (7If the program is multi-file, how do you navigate to the specified line or function of the specified file? (List file:n) (8If there are many loops, how do you finish the current loop? (until) (9How is multithreading debugged? (???) Quietheart Email: [Email protected]126. com [Example]============= *start gdb $gdb so you can interact with GDB. *start GDB, and display the source code on the split screen: $gdb-Tui So, using the'-tui'option, launch can be directly divided into two parts of the screen, the above display source code, more convenient than the list. At this time use the UP and DOWN ARROW keys to view the source code, you want to use the command line with [Ctrl]n and [ctrl]p].*start GDB Debug specified program app: $gdb app to load the app executable directly after launching GDB, 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. *After starting the program, debug with GDB: $gdb<program> <PID>here,<program> is the executable file name of the program,<pid>is the PID of the program to be debugged. If your program is a service program, you can specify the process ID at which the service program runs. GDB will automatically attach up and debug him. The program should be searched in the PATH environment variable. *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,<PID>is to debug the PID of the program. So GDB is attached to the program, but now it is not possible to view the source code, and the file command indicates that the executable can display the source code. **Interactive command After starting GDB: the interactive command supports [TAB] completion. *Display Help information: (GDB)*load the specified program: (GDB) file app to load the executable program app you want to debug in GDB. If you're just starting gdb instead of using the GDB app, you can load the app and, of course, add it when compiling the app.-g debug Options. *rerun the Debug Program: (GDB) run for the program you want to debug, 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 (*,? , [,]). *Modify the parameters sent to the program: (GDB)Setargs No here, suppose I use"R Yes"Set the program startup parameter to Yes, then set args here sets the parameter argv is no. *Displays the default list of parameters: (GDB) show args*lists the code for the specified range (N1 to N2): (gdb) List N1 n2 so that the list can be abbreviated to L, and the code between the N1 row and the N2 row will be displayed if you use-Tui launches GDB and will be displayed in the appropriate location. Without the N1 and N2 parameters, the current line and the following 10 rows are displayed by default, followed by 10 rows. In addition, the list can be followed by the function name. In general, after the list can be followed by the following parameters:<linenum>line number. <+offset>the positive offset of the current line number. <-offset>the negative offset of the current line number. <filename:linenum>Which file is in which line. <function>The function name. <filename:function>Which function in the file. <*address>The address of the statement in memory when the program runs. *perform the next step: (GDB) Next, execute a line of code, and if the function skips the function. This command can be simplified to n. *Execute n Next: (GDB) Next n*execute the last command executed: (GDB) [Enter] here, the direct enter will execute the last command. *Step Into: (GDB) Step This will also execute a line of code, but if a function is encountered, it will enter the inside of the function, and then one line of execution. *after executing the current function, return to the function that called 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 returned to its calling function, you can use the command finish. *Specify the program until the current loop body is exited: (GDB) until or (GDB) u here, it is found that you need to stop the cursor at the head of the loop and then enter u so that it automatically executes all loops. *Jump execution To line 5th: (gdb) jumps5here, you can shorthand for"J 5"It is important to note that after the jump to line 5th is complete, if no breakpoint is followed, the execution continues, not where it was parked. In addition, the jump does not change the current stack content, so jumping to other functions will have strange phenomenon, so it is better to jump 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. *Force return Current function: (GDB)returnThis 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. *Force invoke function: (GDB) Call<expr>here,<expr>Can be a function, which returns the return value of the function, if the return type of the function is void then the return value of the function is not printed, but the practice finds that the print statements in the function run are still not printed. *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 history. *sets a breakpoint on a line in the current file (assuming 6): (GDB) Break 6*Set Conditional breakpoints: (GDB) Break $ iftestsize== -here, if Testsize==100 breakpoint at line 46. *detect expression Change is stopped: (gdb) Watch I!=Tenhere, I! = 10 Once the expression has changed, it stops. Watch <expr>set an observer point for the expression (variable) expr. Stop the program (also a breakpoint) as soon as the value of the expression changes. *sets a breakpoint in the current file for a function (assumed to be func): (GDB) Breakfunc*sets a breakpoint at a line (N) of the specified file (fileName): (GDB) BreakFilename:n Here, it is similar to setting breakpoints on a function in a file. *Displays the current GDB breakpoint information: (GDB) Info breakpoints Here, can be abbreviated as info Break. Displays all current breakpoints, breakpoint numbers, breakpoint locations, and so on. *Delete N breakpoint: (gdb) Delete n*Delete all breakpoints: (GDB) Delete*clears all breakpoints above row N: (gdb) Clear n*continue running the program directly to the next breakpoint: (GDB)Continuehere, it runs without a breakpoint. *displays the functions in the current call function stack: (GDB) The BackTrace command produces a list of all valid procedures starting from the most recent procedure and the parameters that call them. Of course, it will also show where it is currently running (file, line). *To 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. *to view the program language of the current function: (GDB) Info frame*Displays the current debug source file: (GDB) Info source Displays the current source code file information, such as file name, program language, and so on. * Manually set the current program language to C + +: (GDB)SetLanguage C + +here, if GDB does not detect your programming language, you can set this up. *View the programming languages you can set: (GDB)Setlanguage here, use set language without parameters to see the programming languages that can be set in GDB. *terminates a program that is being debugged: (GDB) kill here, the input kill terminates the program being debugged. *print Display Variables (var) Value: (gdb) Printvarhere, print can be abbreviated as P,print is a very powerful command of GDB, which can be used to display any valid expression in the language being debugged. Expressions can include function calls, complex data structures, history, and so on, in addition to variables in your program. * with 16 binary display (var) Value: (gdb) Print/xvaras you can see, print can specify the format of the display, where'/ x'represents the 16 binary format. The variable display format that can be supported is: x displays the variable in hexadecimal format. D Displays the variable in decimal format. u displays unsigned integers in hexadecimal format. o Displays the variable in octal format. T displays the variable in binary format. A displays the variable in hexadecimal format. C Displays the variable in character format. F Displays the variable in floating-point number format. *If A is an array, 10 elements, if it is to be displayed: (gdb) Print*[email protected]TenIn this way, 10 elements are displayed, regardless of whether a is double or int, 10 elements are displayed correctly. *modifying variable values at runtime: (gdb) Print x=4here, x=4 is a c/s + + syntax, meaning to change the variable x value to 4, if your current debugging language is Pascal, then you can use Pascal's syntax: x:=4. *displays the type of VAR for a variable: (GDB) Whatisvar*shows the type of variable var in more detail: (GDB) PTypevarhere, the structure definition of VAR is printed out. [Other]============= *Print the MSG variable for qstring msg in the qt4.x environment: the steps are as follows:1) define a macro printqstring define printqstring printf"(QString) 0x%x (length=%i): \ "",& $arg 0, $arg 0.d->sizeSet$i =0 while$i < $arg 0.d->sizeSet$c = $arg 0.d->data[$i + +] if$c < +|| $c >127printf"\\u0x%04x", $cElseprintf"%c", (Char) $c end end printf"\ "\ n"End2) (GDB) printqstring msg Here, this macro can be directly defined in GDB and is said to be written to $home/. Gdbinit, which automatically loads each time it starts. *Debug also indicates the build core file: $gdb<program>The core uses GDB to debug a running program and core file at the same time, and the core is the file generated after the core dump is executed illegally. When the program crashes, it generates a core file and then uses this command to navigate directly to where the program crashed. Note: Sometimes you need to set the system command "Ulimit-C Unlimited "to produce a core file. **no practice.*Print displays a storage block, such as 10 integers after h: print [email protected]Ten**
GDB basic commands (very detailed)
Start building with 50+ products and up to 12 months usage for Elastic Compute Service