0% found this document useful (0 votes)
12 views

Week 7 Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Week 7 Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Problem Solving with C

LABORATORY MANUAL
Week 7

Semester: 2
Course Code: UE23CS151B
Course Anchor: Prof. C N Rajeswari
Lab Anchors: Prof. Sowmya Shree P, Prof. Chaitra S
Session: Feb 2024 – June 2024
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

Commands to execute Programs using gcc

1. Editing
$gedit filename.c

2. Compile
$ gcc -c filename.c

3. Linking
$ gcc filename.o

4. Loading - Execute
$ ./a.out (Linux Operating System)
$ a.exe (Windows Operating System)

UE23CS151B 2024 2
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

Introduction to debugging with GDB


gdb stands for “GNU Debugger”. GDB allows you to inspect the C programs while they are
executing and also allows you to see what exactly happens when your program crashes. It is supported
by various languages such as Ada, Assembly, C, C++, D, Fortran, Go, Objective-C, OpenCL, Modula-2, Pascal,
Rust. GDB can run on UNIX and Microsoft Windows variants and Mac OS X. Richard Stallman was the
original author of gdb, and of many other gnu programs. Many others have also contributed to its
development. The Latest version of GDB is 10.2 which was released on Apr 25th, 2021.

Tasks Supported by GDB


gdb can do four main kinds of things (plus other things in support of these) to help you catch bugs
 Start your program, specifying anything that might affect its behavior.
 Make your program stop on specified conditions.
 Examine what has happened, when your program has stopped.
 Change things in your program, so you can experiment with correcting the
effects of one bug and go on to learn about another.

Getting In and Out of GDB


1) Starting the GDB: type ‘gdb’ and press enter key

Gdb open prompt lets you know that it is ready for commands.

2) Exiting the GDB: type ‘quit’ or q and press enter key ( press Ctrl-d)

UE23CS151B 2024 3
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

Invoking gdb
 The gdb can be run with a variety of arguments and options, to specify the
debugging environment
 The most usual way to start gdb is with one argument, specifying an executable program:
gdb program
Ex: gdb a.exe
 It can also be started byspecifying both an executable program and core file :
gdb program core
Ex: gdb a.exe core
 To debug a running process,specify a process ID as a second argument or option –p
gdb program 1234
Ex:gdb a.exe 1234
gdb -p 1234
This would attach gdb to process 1234. With option -p you can omit the program filename.
 gdb can be run without printing the front material by specifying --silent (or -q/--quiet):
gdb --silent OR gdb –q OR gdb --quiet

UE23CS151B 2024 4
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

What gdb does During Startup?


1. Performs minimal setup required to initialize basic internal state.
2. Reads commands from the early initialization file in your home directory.Only a restricted
set of commands can be placed into an early initialization file.
3. Executes commands and command files specified by the ‘-eiex’ and ‘-eix’ command line
options in their specified order. Only a restricted set of commands can be used with ‘-eiex’
and ‘eix’.
4. Sets up the command interpreter as specified by the command line .
5. Reads the system wide initialization file and the files from the system wide initialization
directory.
6. Reads the initialization file (if any) in your home directory and executes all the commands
in that file.

7. Executes commands and command files specified by the ‘-iex’ and ‘-ix’ options in their
specified order. The options ‘-ex’ and ‘-x’ can also be used for the same purpose .This way the
settings can be applied before gdb init files get executed and before inferior gets loaded.
8. Processes command line options and operands.
9. Reads and executes the commands from the initialization file (if any) in the current
working directory as long as ‘set auto-load local-gdbinit’ is set to ‘on’ .This is only done if the
current directory is different from your home directory. Thus,we can have more than one init
file, one generic in your home directory, and another, specific to the program you are
debugging, in the directory where you invoke gdb.
10. Executes commands and command files specified by the ‘-ex’ and ‘-x’ options in their
specified order.
11. Reads the command history recorded in the history file.

UE23CS151B 2024 5
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

Usage of GDB on C code

Problem to be solved:
Program to add two numbers that shows undefined behavior
gdb1.c
#include<stdio.h>
int main()
{
int c;
int a;
int b;
c=a+b;
printf("%d\n",c);
return 0;
}

Function to calculate the sum of all the factors of a given number and client must display the
result.
1_find_factors_sum.c
#include<stdio.h>
int sum_factors(int n);
int main()
{
int number;int sum; scanf("%d",&number);
sum=sum_factors(number);
printf("Sum of factors of a number %d is %d\n",number,sum);
return 0;
}

UE23CS151B 2024 6
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

int sum_factors(int n)
{
int sum=0;
for(int i=1;i<=n;++i)
{
if(n%i==0)
{
sum = sum+i;
}
}

return sum;
}
Compile the code with –g option:
-g: Produces debugging Information in the Operating system’s native format.

Invoke gdb : gdb and press enter // There are different ways

UE23CS151B 2024 7
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

GDB Commands
help or h: Provides the list of classes of commands.
help class: Using one of the general help classes as an argument, a list of the individual commands in that
class can be obtained
Example: help breakpoint
help command:With a command name as help argument, gdb displays a short paragraph on
how to use that command.
.Example: help run
Using help all details of all commands in all the classes can be obtained

run OR r: Starts the program under gdb. Program begins to execute immediately.
 Specify the executable with an argument to gdb
 Use target exec command OR use file command

list OR l: Used to print lines from a source file. By default, ten lines are printed
 list line_num : Print lines centered around that specified line number
 list func: Print lines centered around the beginning of function func.
 list: Print more lines.
 list -: Print lines just before the lines last printed.
 list first, last: print lines from first to last
 list ,last: Print lines ending with last

UE23CS151B 2024 8
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

 listsize : Display the number of lines that list prints.


 set listsize unlimited:Make the list command display count source lines (unless
the list argument explicitly specifies some other number). Setting count to
unlimited or 0 means there’s no limit.

UE23CS151B 2024 9
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

print OR p: It evaluates and prints the value of an expression of the language your program is
written in.
 p expression: prints the value of a given expression.
 p variable: prints the value of a given variable
 
p function_name :: variable: To specify a static variable in a particular function or file colon-
colon (::) notation is used.


GDB Commands - Stopping and Continuing in GDB

UE23CS151B 2024 10
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7
breakpoint: Makes the program stop whenever a certain point in the program is reached
 break line_num: breaks at start of code for that line.
 break function_name : break at start of code for that function.
 break address_of_instruction: break at that exact address.
 break if cond: Set a breakpoint with condition cond.It evaluate the expression cond
each time the breakpoint is reached, and stop only if the value is nonzero. 
 break with no arguments: Sets the breakpoint at the next instruction to be executed
in the selected stack frame.

UE23CS151B 2024 11
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

watchpoint: A special breakpoint that stops the program when the value of an expression changes,
without having to predict a particular place where this may happen. When expression is modified, the
program will interrupt and print out the old and new values. The expression may be as simple as the value
of a single variable,or as complex as many variables combined by operators
watch expression

UE23CS151B 2024 12
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7
catchpoint: A special breakpoint that stops the program when a certain kind of event occurs such as
exceptions.
delete: Can individually delete the above using their numbers.

clear: Delete any breakpoints set at the specified location .


 clear line_num: all breakpoints in that line are cleared.
 clear file_name: breakpoints at beginning of function are cleared
 clear: Delete any breakpoints at the next instruction to be executed in the selected
stack frame.

UE23CS151B 2024 13
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

disable: Makes the breakpoint inoperative, but remembers the information on the breakpoint so that it
can be enabled later using enable.

To re-enable the recent disabled breakpoint. Type enable b.

UE23CS151B 2024 14
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

Info: To print a list of all breakpoints, watchpoints, and catchpoints

GDB Commands
file: To change to a different file during a gdb session file command is used . It reads the symbols for
getting the contents of pure memory, and it is the program executed when you use the `run' command.

UE23CS151B 2024 15
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7

next OR n: Continue to the next source line in the current stack frame. Function calls that appear within
the line of code are executed without stopping

UE23CS151B 2024 16
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7
step OR s: Same as next but steps inside any functions called within the line.
continue OR c: Resume program execution at the address where your program last stopped. Any
breakpoints set at that address are bypassed.
whatis: Prints the data type of arg, which can be either an expression or a name of a data type. With no
argument, print the data type of $, the last value in the value history.
set var: Modify the run-time behavior of readline by altering the values of variables.
set var a =10

backtrace - produces a stack trace of the function calls that lead to a seg fault (should remind you of Java
exceptions) .
where - same as backtrace. It works even when you’re still in the middle of the program.
finish - runs until the current function is finished.

UE23CS151B 2024 17

You might also like