linux programming tutoring
DESCRIPTION
Linux Programming Tutoring. Introduction to fundamental GDB- More GDB function -. Get files. Slides : http://140.112.28.132/gdb_leb Packages : $ wget http:// 140.112.28.132/gdb.zip $unzip gdb.zip. Outline. Why debugger Introduction Running program Stop and continue - PowerPoint PPT PresentationTRANSCRIPT
Linux Programming Tutoring- Introduction to fundamental GDB-
- More GDB function -
Get files
Slides :http://140.112.28.132/gdb_leb
Packages :$wget http://140.112.28.132/gdb.zip$unzip gdb.zip
Outline
• Why debugger• Introduction• Running program• Stop and continue• Examining source and data
Why Debugger(1/3)
• With print (printf in c)– Add lines to print values– Must comment debug lines when
release
Int main(int argc, char** argv){
int var1,var2, ... ;...print(var1,var2, ...);...//print(...);...
}
Why Debugger(2/3)
• Use preprocessor – define, ifdef– Still not flexible enough
#define debugmodeInt main(int argc, char** argv){
int var1,var2, ... ;...#ifdef debugmode
print(...)#endif...
}
Why Debugger(3/3)
• With debugger– We can concentrate on writing
program easily– We can print values and stop anywhere without recompilation
– We can make up some value or alter program flow
Outline
• Why debugger• Introduction• Running program• Stop and continue• Examining source and data
GDB : The GNU Project Debugger
• Official website:– http://www.gnu.org/software/gdb/
• We focus on how to use today– Documentation/User Manual
Invoking and quitting• gdb program– Debug program
• gdb program pid– Debug running program with pid
• quit[q] or <Ctrl> + d– Quit gdb
Something useful
• shell command-strings – invoke shell to execute command-
strings– $SHELL
• <tab>– auto completion
• blank line input (<ret>)– repeat last command
Outline
• Why debugger• Introduction• Running program• Stop and continue• Examining source and data
Running program
• gcc –g foo.c –o foo– compiling for debugging
• gdb foo– Debug program
• run[r]– run the program under gdb
Arugments
• set args arg1 arg2– to set arguments
• set args– clear arguments
• show args– display current arguments
Bash$ foo arg1 arg2
Enviroment
• show environment– Show the current environment
• path directory– add directory to $PATH
• set environment varname [=value]– assign value to varname
set environment SHELL=/bin/bash
Working directory
• Program inherits working directory from the gdb
• cd directory– change working directory
• pwd– display current workign directory
Outline
• Why debugger• Introduction• Running program• Stopping and Continuing• Examining source and data
Stopping program
• We use breakpoints to stop the program
• info program– display status , why we stop
Stopping program
• info breakpoints– print a table of all breakpoints
Num Enb What
1 y foo.c::5
2 n foo.c::12
3 y foo.c::6
Breakpoints
• break[b] location– set breakpoint at location
• clear location– clear breakpoint at location
• enable/disable [breakpoints] [range...]• delete [breakpoints] [range...]
filename:linenumfilename:function-/+offset
Continuing and Stepping
• continue[c]– Resume program execution
• step[s] [count]– Continue, step in function
• next[n] [count]– Continue, execute function without stop
Schematic diagram of continue
Main{
ins1foo();ins2ins3}
foo{
ins4ins5
}
Schematic diagram of step
Main{
ins1foo();ins2ins3}
foo{
ins4ins5
}
Schematic diagram of next
Main{
ins1foo();ins2ins3}
foo{
ins4ins5
}
Outline
• Why debugger• Introduction• Running program• Stopping and Continuing• Examining source and data
Examining source
• list[l] location– print source line centered around specific
location• list first, last– print lines between first and last
list 10,30
Examining source
• list– print next lines
• list -– print lines before the line last printed list
list -
Last print
Examining data
• print[p] expression– print the value of expression
• print function::variable– print static variable in function
Automic display
• info display– print a list of all set up expression
• display expr– print expr automatically
• undisplay dnum
Automic display
• delete display dnum• disable display dnum• enable display dnum
Outline
• Breakpoint• Formating output• Examing memory• Print dynamic allocate array• Altering execution
Condition break(1/2)
• info breakpoint• break[b] location– stop everytime
• break location if expression
break foo
break foo if argc == 1break foo if argc >= 3
Condition break(2/2)
• condition bnum expression– add condition to breakpoint bnum
• condition bnum– clear condition
break foocondition 1 argc == 1
One stop breakpoint
• tbreak location– Set up as the same as break– But automatically remove after stop
tbreak foo
Outline
• Breakpoint• Formating output• Examing memory• Print dynamic allocate array• Altering execution
Formating output(1/2)• We can format output like printf– command/format expression
format descriptionx as hexadecimal.
d, u as signed(unsigned) integer decimalc as a characterf as floating points as a string
Formating output(2/2)
• p/format expression– print expression value in format
• display/format expression– print expression valuein fromat
automatically p/x counterdisplay/x counter
Outline
• Breakpoint• Formating output• Examing memory• Print dynamic allocate array• Altering execution
Examing memory• x address– examine address value
• x/nfu address– n : number of elements– f : format as previously describe– u : unit of element
b Byte, 1-byte w Words, 4-bytes (default)h Half-word,s 2-bytes g Giant words,8-bytes long
x/4xb &variable
Print dynamic allocate array
• p marray– $1 = (int *) 0x601010
• p *marray– $2 = 0
• p *marray@len– $3 = {0, 0, 0, 0, 0}
int *marray = (int*)malloc( len * sizeof(int) );
Outline
• Breakpoint• Formating output• Examing memory• Print dynamic allocate array• Altering execution
Altering execution
• We can alter execution by– change value of variable– jump to some location– return makeup value
Altering execution
• set var assignment_exp• print assignment_exp– gdb will evaluate the expression
p foo=4
jump and return
• jump location– jump to assigned location, and
continue execution
jump and return
• return expression– return from function call with
expression value