linux programming tutoring

44
Linux Programming Tutoring - Introduction to fundamental GDB- - More GDB function -

Upload: mariah

Post on 19-Mar-2016

38 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Linux Programming Tutoring

Linux Programming Tutoring- Introduction to fundamental GDB-

- More GDB function -

Page 2: Linux Programming Tutoring

Get files

Slides :http://140.112.28.132/gdb_leb

Packages :$wget http://140.112.28.132/gdb.zip$unzip gdb.zip

Page 3: Linux Programming Tutoring

Outline

• Why debugger• Introduction• Running program• Stop and continue• Examining source and data

Page 4: Linux Programming Tutoring

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(...);...

}

Page 5: Linux Programming Tutoring

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...

}

Page 6: Linux Programming Tutoring

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

Page 7: Linux Programming Tutoring

Outline

• Why debugger• Introduction• Running program• Stop and continue• Examining source and data

Page 8: Linux Programming Tutoring

GDB : The GNU Project Debugger

• Official website:– http://www.gnu.org/software/gdb/

• We focus on how to use today– Documentation/User Manual

Page 9: Linux Programming Tutoring

Invoking and quitting• gdb program– Debug program

• gdb program pid– Debug running program with pid

• quit[q] or <Ctrl> + d– Quit gdb

Page 10: Linux Programming Tutoring

Something useful

• shell command-strings – invoke shell to execute command-

strings– $SHELL

• <tab>– auto completion

• blank line input (<ret>)– repeat last command

Page 11: Linux Programming Tutoring

Outline

• Why debugger• Introduction• Running program• Stop and continue• Examining source and data

Page 12: Linux Programming Tutoring

Running program

• gcc –g foo.c –o foo– compiling for debugging

• gdb foo– Debug program

• run[r]– run the program under gdb

Page 13: Linux Programming Tutoring

Arugments

• set args arg1 arg2– to set arguments

• set args– clear arguments

• show args– display current arguments

Bash$ foo arg1 arg2

Page 14: Linux Programming Tutoring

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

Page 15: Linux Programming Tutoring

Working directory

• Program inherits working directory from the gdb

• cd directory– change working directory

• pwd– display current workign directory

Page 16: Linux Programming Tutoring

Outline

• Why debugger• Introduction• Running program• Stopping and Continuing• Examining source and data

Page 17: Linux Programming Tutoring

Stopping program

• We use breakpoints to stop the program

• info program– display status , why we stop

Page 18: Linux Programming Tutoring

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

Page 19: Linux Programming Tutoring

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

Page 20: Linux Programming Tutoring

Continuing and Stepping

• continue[c]– Resume program execution

• step[s] [count]– Continue, step in function

• next[n] [count]– Continue, execute function without stop

Page 21: Linux Programming Tutoring

Schematic diagram of continue

Main{

ins1foo();ins2ins3}

foo{

ins4ins5

}

Page 22: Linux Programming Tutoring

Schematic diagram of step

Main{

ins1foo();ins2ins3}

foo{

ins4ins5

}

Page 23: Linux Programming Tutoring

Schematic diagram of next

Main{

ins1foo();ins2ins3}

foo{

ins4ins5

}

Page 24: Linux Programming Tutoring

Outline

• Why debugger• Introduction• Running program• Stopping and Continuing• Examining source and data

Page 25: Linux Programming Tutoring

Examining source

• list[l] location– print source line centered around specific

location• list first, last– print lines between first and last

list 10,30

Page 26: Linux Programming Tutoring

Examining source

• list– print next lines

• list -– print lines before the line last printed list

list -

Last print

Page 27: Linux Programming Tutoring

Examining data

• print[p] expression– print the value of expression

• print function::variable– print static variable in function

Page 28: Linux Programming Tutoring

Automic display

• info display– print a list of all set up expression

• display expr– print expr automatically

• undisplay dnum

Page 29: Linux Programming Tutoring

Automic display

• delete display dnum• disable display dnum• enable display dnum

Page 30: Linux Programming Tutoring

Outline

• Breakpoint• Formating output• Examing memory• Print dynamic allocate array• Altering execution

Page 31: Linux Programming Tutoring

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

Page 32: Linux Programming Tutoring

Condition break(2/2)

• condition bnum expression– add condition to breakpoint bnum

• condition bnum– clear condition

break foocondition 1 argc == 1

Page 33: Linux Programming Tutoring

One stop breakpoint

• tbreak location– Set up as the same as break– But automatically remove after stop

tbreak foo

Page 34: Linux Programming Tutoring

Outline

• Breakpoint• Formating output• Examing memory• Print dynamic allocate array• Altering execution

Page 35: Linux Programming Tutoring

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

Page 36: Linux Programming Tutoring

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

Page 37: Linux Programming Tutoring

Outline

• Breakpoint• Formating output• Examing memory• Print dynamic allocate array• Altering execution

Page 38: Linux Programming Tutoring

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

Page 39: Linux Programming Tutoring

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) );

Page 40: Linux Programming Tutoring

Outline

• Breakpoint• Formating output• Examing memory• Print dynamic allocate array• Altering execution

Page 41: Linux Programming Tutoring

Altering execution

• We can alter execution by– change value of variable– jump to some location– return makeup value

Page 42: Linux Programming Tutoring

Altering execution

• set var assignment_exp• print assignment_exp– gdb will evaluate the expression

p foo=4

Page 43: Linux Programming Tutoring

jump and return

• jump location– jump to assigned location, and

continue execution

Page 44: Linux Programming Tutoring

jump and return

• return expression– return from function call with

expression value