In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the Linux gdb command". In daily operation, I believe many people have doubts about how to use the Linux gdb command. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the Linux gdb command". Next, please follow the editor to study!
Gdb powerful program debugger
It is added that the gdb command is included in GNU's gcc development kit and is a powerful program debugger. There are many commands in GDB, but we only need to master about ten of them to roughly complete the daily basic program debugging work.
Syntax gdb (option) (parameter) option-cd: set the working directory;-Q: quiet mode, do not print introduction and version information;-d: add file lookup path;-x: execute GDB instruction from the specified file;-s: set the symbol table file to be read. Command interpretation sample file loads the executable program file being debugged. Because GDB is usually executed in the directory where the program is being debugged, the text name does not need to have a path. (gdb) shorthand for file gdb-samplerRun to run the program being debugged. If there is no breakpoint before, the entire program is executed; if there is a breakpoint, the program is paused at the first available breakpoint. (gdb) shorthand for rcContinue to continue executing the debugged program until the next breakpoint or the end of the program. (gdb) cb b b * b * d [number] b: shorthand for Breakpoint to set a breakpoint. The breakpoint location can be specified by using "line number", "function name", "execution address" and so on. The "*" symbol before the function name indicates that the breakpoint is set "at the prolog code generated by the compiler". If you don't know the assembly, you can ignore this usage. D: shorthand for Delete breakpoint, delete a breakpoint with a specified number, or delete all breakpoints. The breakpoint number is incremented from 1. (gdb) b 8 (gdb) b main (gdb) b * main (gdb) b * 0x804835c (gdb) ds, ns: executes a line of source code and, if there is a function call in this line of code, enters the function; n: executes a line of source code, and the function calls in this line of code are also executed. S is equivalent to "Step Into" in other debuggers; n is equivalent to "Step Over" in other debuggers. These two commands can only be used if you have source code debugging information (GCC compiles with the "- g" argument). (gdb) s (gdb) nsi, the nisi command is similar to the s command, and the ni command is similar to the n command. The difference is that these two commands (si/ni) are directed at assembly instructions, while Spura is directed at source code. Shorthand for (gdb) si (gdb) nipPrint, showing the value of a specified variable (temporary or global). (gdb) p I (gdb) p nGlobalVardisplay... Undisplaydisplay, set the data and its format to be displayed after the program is interrupted. For example, if you want to see the next assembly instruction to be executed after each program break, you can use the command "display / I, where pc represents the current assembly instruction, and / I is displayed as sixteen. This command is useful when you need to care about assembly code. Undispaly, cancel the previous display setting, and the number is incremented from 1. (gdb) shorthand for display / I $pc (gdb) undisplay 1iinfo, used to display all kinds of information, please refer to "help I" for details. (gdb) I rqQuit shorthand to exit the GDB debugging environment. (gdb) qhelp [Command name] GDB help command, which provides an explanation of the GDB name command. If the Command name parameter is specified, a detailed description of the command is displayed; if no parameter is specified, all GDB commands are classified for further browsing and query. (gdb) help parameter file: binary executable program.
Example the following is an example of dgb debugging under linux. First, the Mini Program and C language code for an example is given:
# include int nGlobalVar = 0; int tempFunction (int a, int b) {printf ("tempFunction is called, a =% d, b =% d / n", a, b); return (a + b);} int main () {int n; n = 1; nGlobalVar + = 100; nGlobalVar-= 12; printf ("n =% d, nGlobalVar =% d / n", n, nGlobalVar) N = tempFunction (1,2); printf ("n =% d", n); return 0;} copy this code and save it to the file gdb-sample.c, then change to the directory where the file is located and compile it with GCC:
Gcc gdb-sample.c-o gdb-sample-g in the command line above, use the-o parameter to specify that the executable file generated by the compilation is named gdb-sample, and the parameter-g means to compile the source code information into the executable file. If you do not use the parameter-g, it will cause inconvenience to the later GDB debugging. Of course, if we don't have the source code of the program, we can't use the-g parameter, and we can only debug / trace at the assembly code level.
The following "gdb" command starts GDB, which first displays the GDB description, regardless of it:
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh) Copyright 2003 free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu". (gdb) the last line "(gdb)" above is the GDB internal command leader, waiting for the user to enter the GDB command.
Use the "file" command to load the debugged program gdb-sample (the gdb-sample here is the executable of the previous GCC compilation output):
(gdb) file gdb-sample Reading symbols from gdb-sample...done. The last line above indicates that it has been loaded successfully.
The following uses the "r" command to execute (Run) the debugged file, because no breakpoints have been set and will be executed directly until the end of the program:
(gdb) r Starting program: / home/liigo/temp/test_jmp/test_jmp/gdb-sample n = 1, nGlobalVar = 88 tempFunction is called, a = 1, b = 2 n = 3 Program exited normally. Use the "b" command to set a breakpoint (Breakpoint) at the beginning of the main function:
(gdb) b main Breakpoint 1 at 0x804835c: file gdb-sample.c, line 19. The last line above prompts that the breakpoint has been successfully set and gives the breakpoint information: set the breakpoint at line 19 of the source file gdb-sample.c; this is the first breakpoint of the program (serial number 1); the code address at the breakpoint is 0x804835c (this value may only be valid during this debugging). Looking back at the source code, the code in line 19 is "n = 1", which happens to be the first executable statement in the main function (the preceding "int n;" defines a statement for a variable, not an executable statement).
Use the "r" command to Run the debugged program again:
(gdb) rStarting program: / home/liigo/temp/gdb-sampleBreakpoint 1, main () at gdb-sample.c:1919 n = 1; the program interrupt is at line 19 of gdb-sample.c, where the main function is the first executable statement.
The last line above reads: the next source code to be executed is "n = 1;", which is line 19 in the source code file gdb-sample.c.
Use the "s" command (Step) to execute the next line of code (that is, line 19 "n = 1;"):
The above message indicates that "n = 1;" has been executed and shows that the next piece of code to be executed is "gdb;" on line 20.
Now that "n = 1;" has been executed, that is, the variable n is assigned a value of 1, let's use the "p" command (Print) to see if the value of variable n is 1:
(gdb) p nasty 1 = 1 is sure enough to be 1. (roughly means that this is the first time to use the command to execute again will display 2 = 1 "--this information should be of no use.)
Let's set a breakpoint on line 26 and at the beginning of the tempFunction function (using the command "b 26"b tempFunction", respectively):
(gdb) b 26Breakpoint 2 at 0x804837b: file gdb-sample.c, line 26. (gdb) b tempFunctionBreakpoint 3 at 0x804832e: file gdb-sample.c, line 12. Use the "c" command to continue (Continue) the debugged program, and the program will break at the second breakpoint (line 26), where the value of the global variable nGlobalVar should be 88; if you execute the "c" command again, the program will break at the third breakpoint (line 12, at the beginning of the tempFunction function), and the values of the two parameters an and b of the tempFunction function should be 1 and 2, respectively:
(gdb) cContinuing.Breakpoint 2, main () at gdb-sample.c:2626 printf ("n =% d, nGlobalVar =% d / n", n, nGlobalVar); (gdb) p nGlobalVar$2 = 88 (gdb) cContinuing.n = 1, nGlobalVar = 88Breakpoint 3, tempFunction (tempFunction is called, a =% d, b =% d, a, b) at gdb-sample.c:1212 printf ("tempFunction is called, a =% d, b =% d / n", a, b); (gdb) paqum3 = 1 (gdb) p Benz4 = 2 all the feedback is expected.
Execute the "c" command (Continue) again, because there are no other breakpoints later, and the program will execute until the end:
(gdb) cContinuing.tempFunction is called, a = 1, b = 2n = 3Program exited normally. Sometimes you need to see the assembly code generated by the compiler for assembly-level debugging or tracing. What do you do?
This is about to use the display command "display / I $pc" (which has been explained in detail earlier):
(gdb) display / I $pc (gdb) when the program is interrupted again, the assembly code can be displayed:
(gdb) rStarting program: / home/liigo/temp/test_jmp/test_jmp/gdb-sampleBreakpoint 1, main () at gdb-sample.c:1919 n = 1 * I $pc 0x804835c: movl $0xfffffffc (% ebp) saw the assembly code, "n = 1;" the corresponding assembly code is "movl $0x1author0xfffffc (% ebp)".
And each subsequent interruption of the program will display the next assembly assignment (the "si" command is used to execute a line of assembly code-- different from "s" executing a line of C code):
(gdb) si20 I $pc 0x8048363: lea 0xfffffffc (% ebp),% eax (gdb) si0x08048366 20 naughty match 1: xUnip I $pc 0x8048366: incl (% eax) (gdb) si21 nkashi 1: xUnip I $pc 0x8048368: lea 0xfffffffc (% ebp),% eax (gdb) si0x0804836b 21 n Mustang [1: xUnip I $pc 0x804836b: decl (% eax) (gdb) si23 nGlobalVar + = 100X 1: XUniI $pc 0x804836d: addl $0x6lt 0x80494fc next let's try the command "b *".
For simplicity, it is necessary to delete all current breakpoints first (using the "d" command-- Delete breakpoint):
(gdb) dDelete all breakpoints? (y or n) y (gdb) when asked whether to delete all breakpoints, enter "y" and press enter.
Use the command "b * main" to set breakpoints at the prolog code of the main function (prolog, epilog, which represents the code that the compiler inserts itself at the beginning and end of each function, respectively):
(gdb) b * mainBreakpoint 4 at 0x804834c: file gdb-sample.c, line 17. (gdb) rThe program being debugged has been started already.Start it from the beginning? (y or n) yStarting program: / home/liigo/temp/test_jmp/test_jmp/gdb-sampleBreakpoint 4, main () at gdb-sample.c:1717 {1: X si0x0804834d I $pc 0x804834c: push% ebp (gdb) si0x0804834d 17 {1: X si0x0804834d I $pc 0x804834d: mov% esp,%ebp (gdb) si0x0804834f in main () at gdb-sample.c:1717 {1: x esp,%ebp $pc 0x804834f: sub $0x8 school% ESP (gdb) si0x08048352 17 {1: x esp,%ebp $pc 0x8048352: and $0xfffffff0 % esp (gdb) si0x08048355 17 {1: X eax,%esp I $pc 0x8048355: mov $0x0 si19% Eax (gdb) si0x0804835a 17 {1: X eax,%esp I $pc 0x804835a: sub% eax,%esp (gdb) si19 n = 1 At this point, you can use the "I r" command to display the current value in the register-"I r" that is, "Infomation Register": 1: xswap I $pc 0x804835c: movl $0xfffffffc (% ebp):
(gdb) I reax 0xbffff6a4-1073744220ecx 0x42015554 1107383636edx 0x40016bc8 1073834952ebx 0x42130a14 1108544020esp 0xbffff6a0 0xbffff6a0ebp 0xbffff6a8 0xbffff6a8esi 0x40015360 1073828704edi 0x80483f0 134513648eip 0x8048366 0x8048366eflags 0x386 902cs 0x23 35ss 0x2b 43ds 0x2b 43es 0x2b 43fs 0x0 0gs 0x33 51 can also display any specified register value:
(gdb) i r eaxeax 0xbffff6a4-1073744220 the last command to be introduced is "Q" to Quit the GDB debug environment:
(gdb) qThe program is running. Exit anyway? (y or n) Supplementary content gdb course: Linux C language pointers and memory-Chapter 3
If the source code is deleted, auxiliary information such as line numbers cannot be displayed.
Gcc-g gdb.c-o gdb.out #-g supports gdb debugging -o output, default to a.outgdb gdb.out # enter gdb debug environment enter # to continue to execute the previous command l # list source code, default 10 lines, press l to continue start # to start step debugging, default main () the first line p a # view the value of a variable n # continue to the next line s # enter subfunction bt # view function stack f 1 # switch function stack Q exit debugging test code
# include void change (int a, int b) {int tmp=a; astatb; bountmp;} void change2 (int * a, int * b) {int tmp=*a; * astattmb; * bountmp;} int main () {int axiafid5 (change); printf ("change:\ na=%d\ nb=%d\ n", ahelomb); change2 (& aQuintemb) Printf ("change2:\ na=%d\ nb=%d\ n", a Linux gdb b);} at this point, the study of "how to use the Linux gdb command" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.