IT Workshop-1
(AIML)
Lecture-1
About the Course:
Lab Type Course (2 Credits)
Evaluation Will be of 100Marks
Attendance+Lab Assignments
Contents:
Linux Based Commands
Linux System/VM
Debugger
Debugger is a program that runs/simulates another program allows to :
-pause and continue its execution
-set “breakpoints” or conditions where the execution pauses so you can look
at its state.
-view and watch variable values
-step through the program line by line.
➢ Gdb(GNU debugger)
Debugger In Linux
GDB, the GNU Debugger
Text-based, invoked with:
gdb [<programfile> [<corefile>]
Argument descriptions:
<programfile> executable program file
<corefile> core dump of program (crash records)
Compile <programfile> with –g for debug info (gcc -g
<programfile>)
corefiles very useful in post mortem analysis (figuring out what 30
gdb Example
$ gcc -g reversefirst.c -o reverse
$ gdb reverse
<…>
(gdb) help
<…>
Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
(gdb)
Basic GDB Commands
General Commands:
file [<file>] selects <file> as the program to debug
run [<args>] runs selected program with arguments
<args>
quit quits the gdb program
help [<topic>] accesses the internal help
documentation
Stepping and Continuing:
c[ontinue] continue execution (after a stop)
s[tep]step one line, entering called functions
n[ext] step one line, without entering functions
finish finish the function and print the return value
GDB Breakpoints
Useful breakpoint commands:
b[reak] [<where>] sets breakpoints. <where> can be a number of things,
including hex address, a function name, a line number,
or a relative line offset
[r]watch <expr> sets a watchpoint, which will break when <expr> is
written to [or read]
info break[points] prints out a listing of all breakpoints
clear [<where>] clears a breakpoint at <where>
d[elete] [<nums>] deletes breakpoints by number
Playing with Data in GDB
Commands for looking around:
list [<where>] prints out source code at <where>
search <regexp> searches source code for <regexp>
backtrace [<n>] prints a backtrace <n> levels deep
info [<what>] prints out info on <what> (like
local variables or function args)
p[rint] [<expr>] prints out the evaluation of <expr>
gdb - Example
(gdb) list 1 ---> can use “l” for “list”
1 /* REVERSE.C */
2
3 #include <stdio.h>
4
5 /* Function Prototype */
6 void reverse ();
7
8 /****************************************************************/
9
10 main ()
(gdb) list ---> same as “list”; continues from the previous
11
12 {
13 char str [100]; /* Buffer to hold reversed string */
14
15 reverse ("cat", str); /* Reverse the string "cat" */
16 printf ("reverse (\"cat\") = %s\n", str); /* Display */
17 reverse ("noon", str); /* Reverse the string "noon" */
18 printf ("reverse (\"noon\") = %s\n", str); /* Display */
19 }
gdb - Example
(gdb) break main ---> set a breakpoint in function main
Breakpoint 1 at 0x29f4: file reversefirst.c, line 15.
(gdb) break 16 ---> set a breakpoint in line 16 (current source)
Breakpoint 2 at 0x2a0c: file reversefirst.c, line 16.
(gdb) break reverse ---> set a breakpoint in function reverse
Breakpoint 3 at 0x2a80: file reversefirst.c, line 33.
(gdb) info break ---> display information on breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x000029f4 in main at reversefirst.c:15
2 breakpoint keep y 0x00002a0c in main at reversefirst.c:16
3 breakpoint keep y 0x00002a80 in reverse at reversefirst.c:33
(gdb) run
Starting program: /home/usersNN/userID/reverse/reverse
Breakpoint 1, main () at reversefirst.c:15
15 reverse ("cat", str); /* Reverse the string "cat" */
(gdb) continue ---> you can use “c” as well
gdb - Example
(gdb) backtrace ---> show the execution stack
#0 reverse (before=0x40001028 "cat", after=0x7f7f0958 "") at reversefirst.c:33
#1 0x2a0c in main () at reversefirst.c:15
(gdb) l
28 {
29 int i;
30 int j;
31 int len;
32
33 len = strlen (before);
34
35 for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */
36 after[i] = before[j];
37
(gdb) next ---> execute next line
35 for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */
(gdb) n ---> same as “next”
36 after[i] = before[j];
(gdb) _
gdb - Example
(gdb) print after ---> display data (expression)
$1 = 0 '\000'
(gdb) p before ---> same as “print”
$2 = 116 't'
(gdb) _
(gdb) n
35 for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */
(gdb) p after ---> print
$4 = 0x7f7f0958 "t"
(gdb) p before
$5 = 0x40001028 "cat"
(gdb) c
Continuing.
Breakpoint 2, main () at reversefirst.c:16
printf ("reverse (\"cat\") = %s\n", str); /* Display */
(gdb) _
gdb - Example
(gdb) n
reverse ("cat") = tac
17 reverse ("noon", str); /* Reverse the string "noon" */
(gdb) s
Breakpoint 3, reverse (before=0x40001030 "noon", after=0x7f7f0958 "tac")
at reversefirst.c:33
len = strlen (before);
(gdb) p str
$4 = "tac", '\000' <repeats 96 times>
(gdb) n
reverse ("noon") = tac ---> this is the output from the
program
19 }
(gdb) quit
$_
DDD interface to GDB debugger
GDB – GNU Source-Level Debugger.
GDB can do four main kinds of things:
-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
DDD – Data Display Debugger.
DDD is a graphic interface for DGB.
DDD installation
You can install DDD (and GDB) in your computer (you must run Linux or
Unix of course) – this software is free ☺.
Go to DDD homepage to download it: http://www.gnu.org/software/ddd
.
You should download GDB as well.
run DDD
We will use sample executable file.
We can get it by compiling the following two files: main123.c.
To run DDD simply type
> ddd
you should get the following window →
argument field
source window
command tool
Note: if you don’t see any
window , use
View → <window name>
menu option.
machine code
debugger console
status line
open program (execution file)
To open your executable file :
- File → Open Program
- choose sample file
you should get the following window →
You file would appear in the
machine code window (since
there is no source file, source
file window displays nothing.
Note: you can resize windows
with resize buttons
To get help on any DDD option:
Move mouse cursor on any field,
button or menu option. After a few
seconds you will get a short
explanation about an object you
selected.
If you want to get more help with
the object, press F1.
Define breakpoints
The first thing to do now is to place a Breakpoint , making sample stop at
a location you are interested in:
- click on the blank space left to the first code line
- the Argument field (): now contains the location (0x080484d0 in the
example)
- now, click on Break to create a breakpoint at the location in (). You see
a little red stop sign appear in line 0x080484d0.
This is reported in the
debugger console:
(gdb) break * 0x080484d0
Breakpoint 1 at 0x080484d0
(gdb)
program execution
- Program → Run
(Run Program dialog box appears)
In Run with Arguments, you can enter arguments for the program.
- press “Run” button
(Now GDB starts executing sample)
Execution stops after the
breakpoint is reached. This is
reported in the debugger
console:
(gdb) run
Starting program:
/user/…../sample
Breakpoint 1, 0x080484d0
(gdb)
Examine registers values
You can examine register value by simply move the mouse pointer on its
name and leave it there.
To see values of all registers:
- Status → Registers
(Registers window pops up)
Print register value:
- choose register (on registers window)
- press “Print” button
This is reported in the
debugger console:
(gdb) print /x $ebx
$13 = 0x42130a14
(gdb)
Examine registers values (2)
Display register value:
- choose register (on registers window)
- press “Display” button
Data Window will pop up.
On this window, a value of ebx
register will be displayed.
Task: display value of esp
register; then make two step
(press twice on “Stepi” button)
– its value should be changed,
because we executed
“mov esp, ebp” instruction !
Note: you can remove displayed register by pressing “Undisplay” button
execute program step by step
To execute next step you should press:
- “Stepi” (executes one machine code instruction)
- “Step” (executes one source code instruction)
- “Nexti” (as Stepi, but proceed through subroutine calls)
- “Next” (as Nexti, but for source code)
You can undo the last step by using “Undo” button.
execute program step by step (2)
Task: run (step by step) the program till reach a call to printf. Step
into it.
You can see back trace of calls
(to subroutines):
- Status → Backtrace
(Backtrace window should pop up)
You can see that there was one
call (from main) to printf.
Task:
- click on “… main () “
(you should return to the main function
on Machine code window)
- click on “…printf ()”
- close Backtrace window
- press “Finish” button (of
Command tool)
=> What happened?
DDD - summary
1. Each action that you do graphically, you can do using (gdb) command
line.
2. You can continue an execution of the program by pressing “Cont”
button.
3. DDD is unstable, so if you don’t succeed doing something, you
should try to reload DDD.
5. Read “DDD Manual” (it’s very good) for more explanations;
DDD has lots of options and abilities that weren’t covered in this slides.
http://www.gnu.org/manual/ddd/html_mono/ddd.html