Week 7 Manual
Week 7 Manual
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
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
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
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
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
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.
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.
UE23CS151B 2024 14
Department of CSE, PES University
UE23CS151B – Problem Solving with C
Laboratory-Week 7
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