How Linux uses GDB to view core stack information __linux

Source: Internet
Author: User
Tags exception handling
Core dump is generally a segmentation fault (segment error) in the case of the resulting file, need to be set up by Ulimit to get. Debug words Input: GDB filename core   filename is the production of core files executable file, core is generated dump file to view stack information ————— when the program is stopped, the first thing you need to do is to see where the program is stopped. When your program calls a function, the address of the function, the function arguments, the local variables within the function are pressed into the stack. You can use the GDB command to view the information in the current stack. Here are some GDB commands for viewing the function call stack information: BackTrace BT prints all the information for the current function call stack. such as: (GDB) bt #0 func (n=250) at Tst.c:6 #1 0x08048524 on Main (argc=1, argv=0xbffff674) at tst.c:30 #2 0x400409ed in __LIBC _start_main () from/lib/libc.so.6 can see the call stack information from the function: __libc_start_main--> main ()--> func () BackTrace bt n is a positive integer, Represents a stack information that prints only n layers on top of the stack. BackTrace <-n> bt <-n>-n Table A negative integer that prints only the stack information at the bottom n level of the stack. If you want to see a layer of information, you need to switch the current stack, in general, when the program stops, the topmost stack is the current stack, if you want to look at the bottom layer of the stack details, the first thing to do is to switch the current stack. Frame f N is a 0-based integer that is the layer number in the stack. For example: Frame 0, which represents the top of the stack, frame 1, represents the second layer of the stack. Up means to move n layers above the stack, without hitting N, to move up one level. Down means to move n layers underneath the stack, without hitting N, to move down one level. The above command will print out the information of the stack layer that is moved to. If you don't want it to be typed. You can use these three commands: Select-frame corresponds to the frame command. up-silently corresponds to the UP command. down-silently corresponds to the down command. To view the current stack layer information, you can use the following GDB command: Frame or F will print out this information: Stack layer number, the current function name, the function parameter value, the file and the line number of the function, and the statement that the function executes to. Info Frame Info f This command prints more detailed information about the current stack layer, except that most are internal addresses at run time. For example: function address, call function address, the address of the called function, the current function is written by what program language, function parameter address and value, local variable address and so on. such as: (GDB) info F Stack Level 0, frame at 0xbffff5d4:eip = 0x804845d in func (Tst.c:6); Saved EIP 0x8048524 called by frame at 0xbffff60c Source language C. Arglist in 0xbffff5d4, args:n=250 Locals at 0xbffff5 D4, Previous frame ' s SP is 0x0 Saved registers:ebp in 0xbffff5d4, EIP at 0xbffff5d8 info args prints the parameter name and its value for the current function. Info locals prints out all local variables and their values in the current function. The info catch prints out the exception handling information in the current function. View Source ————— One, display source code GDB can print out the source code of the debugger, of course, in the program compile must add-g parameters, the source program information compiled into the execution file. Otherwise, you will not see the source program. When the program stops, GDB reports that the program is parked on the first line of that file. You can use the List command to print the source code of the program. Let's take a look at the GDB command to view the source code. List shows the source program around the LineNum line. The list displays the source program for the function named function. The list displays the source programs that follow the current line. List-Displays the source program before the current line. Generally print the current line of the top 5 lines and the next 5 lines, if the display function is 2 lines under 8 lines, the default is 10 lines, of course, you can also customize the scope of the display, use the following command to set the number of lines to display the source program. Set Listsize sets the number of lines to display the source code at once. Show Listsize view the settings for the current listsize. The list command also has the following usage: list, which shows the source code from the beginning to the last row. List, showing the source code between the current line and the last row. List + back to show source generationCode. Generally after the list can be followed by the following parameters: line number. <+offset> the positive offset of the current line number. <-offset> negative offset of current line number. Which row of which file. The name of the function. which function in which file. The address of the statement in memory at the time the <*address> program runs. Second, search source code not only this, GDB also provides the source code Search command: Forward-search search to the front. Reverse-search all searches. In which, is the regular expression, but also the main string matching pattern, about regular expressions, I do not speak here, but also ask you to check the relevant information. Third, specify the path of the source file at some point, the execution program that was compiled with-G only included the name of the source file, no path name. GDB provides a command that allows you to specify the path to the source file so that gdb can search. Directory dir plus a source file path to the front of the current path. If you want to specify multiple paths, Unix you can use ":" Under Windows you can use ";" Directory clears all custom source file search path information. Show directories displays the defined source file search path. Iv. Source code memory you can use the Info Line command to view the address of the source code in memory. Info line can be followed by "row number", "Function name", "FileName: line number", "FileName: function name", this command will print out the specified source code at runtime memory address, such as: (GDB) Info line Tst.c:func Line 5 of "tst.c" star TS at address 0x8048456 and ends at 0x804845d. There is also a command (disassemble) where you can view the machine code for the current execution of the source program, which will dump the instructions in the current memory. The following example represents the assembly code that views the function func. (GDB) disassemble func Dump of assembler code for function Func:0x8048450:push%ebp 0x8048451:mov%esp,%ebp 0x8048453 : Sub $0x18,%esp 0x8048456:movl $0X0,0XFFFFFFFC (%EBP) 0x804845d:movl $0x1,0xfffffff8 (%ebP) 0x8048464:mov 0xfffffff8 (%EBP),%eax 0x8048467:cmp 0x8 (%EBP),%eax 0x804846a:jle 0x8048470 0x804846c:jmp-0x804848 0 0x804846e:mov%esi,%esi 0x8048470:mov 0xfffffff8 (%EBP),%eax 0x8048473:add%EAX,0XFFFFFFFC (%EBP) 0X8048476:INCL 0 XFFFFFFF8 (%EBP) 0x8048479:jmp 0x8048464 0x804847b:nop 0x804847c:lea 0x0 (%esi,1),%esi 0x8048480:mov 0XFFFFFFFC (%ebp ),%edx 0x8048483:mov%edx,%eax 0x8048485:jmp 0x8048487 0x8048487:mov%ebp,%esp 0x8048489:pop%ebp 0x804848a:ret End of a

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.