Skip to main content

Debug C Program using gdb

Debugging  C Program using gdb

Step 1. Compile the C program with debugging option -g
Compile your C program with -g option. This allows the compiler to collect the debugging information.
Eg:
        $cc -g test.c  //test.c is the sample program to debug
Step 2. Launch gdb
Launch the C debugger (gdb) as shown below.
    $gdb a.out
Step 3. Set up a break point inside C program
Syntax:
    break [file_name]:line_number
Eg:
        break test.c:3 // this will set a break point at line number3
Places break point in the C program, where you suspect errors. While executing the program, the debugger will stop at the break point, and gives you the prompt to debug.
Step 4. Execute the C program in gdb debugger
You can start running the program using the run command in the gdb debugger.
  Eg: run [args]  // args are optional
Step 5. Printing the variable values inside gdb debugger
Syntax: print {variable}
Example:
    print i
Step 6. Continue, stepping over and in – gdb commands
There are three kind of gdb operations you can choose when the program stops at a break point. They are continuing until the next break point, stepping in, or stepping over the next program lines.
    c or continue: Debugger will continue executing until the next break point.
    n or next: Debugger will execute the next line as single instruction.
    s or step: Same as next, but does not treats function as a single
      instruction, instead goes into the function and executes it line by line.
gdb command shortcuts
Use following shortcuts for most of the frequent gdb operations.
    l – list
    p – print
    c – continue
    s – step
    ENTER: pressing enter key would execute the previously executed command again.
Miscellaneous gdb commands
    l command: Use gdb command l or list to print the source code in the debug mode. Use l line-number to view a specific line number (or) l function to view a specific function.
    bt: backtrack – Print backtrace of all stack frames, or innermost COUNT frames.
    help – View help for a particular gdb topic — help TOPICNAME.
    quit – Exit from the gdb debugger.

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar