C Language Development in Linux (GDB debugging)

Source: Internet
Author: User
[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Debugging is indispensable in coding. In Windows, we have visual Studio Tools. In Linux, you have no choice except for the gdb tool. So how can I use GDB for debugging? We can try it step by step.

#include <stdio.h>int iterate(int value){    if(1 == value)        return 1;    return iterate(value - 1) + value;}int main(){    printf("%d\n", iterate(10));    return 1;}

Now that debugging is required, the generated executable file must contain debugging information. What should I do here? Enter GCC test. C-g-o test. After the command is entered, if there is no compilation or link error, you can see the executable file test.

The debugging procedure is as follows,

(01) First, enter GDB Test
(02) after entering the debugging interface of GDB, enter list to view the source file test. C.
(03) set the breakpoint and enter B Main
(04) start the test program and enter run
(05) The Program sets a breakpoint at the beginning of main, so the program is disconnected at printf.
(06) at this time, you can track in one step. S can enter the function in one step, while N in one step is beyond the function.
(07) If you want to continue running the program from the breakpoint, enter C
(08) If you want to run the program until the function ends, enter finish
(09) view the breakpoint information and enter info break
(10) If you want to view the stack information, enter BT
(11) to view the memory, enter the X/64 XH + Memory Address
(12) to delete a breakpoint, enter the delete break + breakpoint sequence number.
(13) to view the value of the local variable of the function, enter print + variable name.

(14) to modify the memory value, directly enter print + * address = Value
(15) to print the value of a variable in real time, enter the display + variable name.
(16) view the assembly code of the function and enter the disassemble + function name.
(17) Exit debugging and enter quit.

Related 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.