Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming – Introduction Today Assembly programmer’s exec model Accessing information Arithmetic operations.
Advertisements

Introduction to the Omega Server CSE Overview Intro to Omega Basic Unix Command Files Directories Printing C and C++ compilers GNU Debugger.
The art of exploitation
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
Cse322, Programming Languages and Compilers 1 6/18/2015 Lecture #16, May 24, 2007 Runtime.c Running the code debugging assembler division strings for println.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Recitation: Bomb Lab June 5, 2015 Dipayan Bhattacharya.
Homework Reading Programming Assignments
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Goals: To gain an understanding of assembly To get your hands dirty in GDB.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Carnegie Mellon Recitation: Bomb Lab 21 Sep 2015 Monil Shah, Shelton D’Souza.
Introduction to InfoSec – Recitation 2 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
CSI605 Introduction to gdb. Compiling for gdb Execution I often compile as follows gcc -g -Wall -omyprog myprog.c g++ -g -Wall -omyprog myprog.c It suffices.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 4, 2010 CSCE 212Honors Computer Organization.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
DEBUG.
X86 Assembly - Data.
Reading Condition Codes (Cont.)
Instruction Set Architecture
Computer Architecture and Assembly Language
CSCE 212Honors Computer Organization
Debugging with gdb gdb is the GNU debugger on our CS machines.
Homework Reading Machine Projects Labs PAL, pp ,
Homework In-line Assembly Code Machine Language
Assembly Language Programming V: In-line Assembly Code
Machine-Level Programming II: Arithmetic & Control
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming 1 Introduction
Machine-Level Programming 4 Procedures
Instructor: David Ferry
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Discussions on HW2 Objectives
Machine-Level Programming I: Introduction Feb. 1, 2000
Machine-Level Programming I: Introduction
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
X86 Assembly - Data.
Machine-Level Programming: Introduction
Discussions on HW2 Objectives
Homework Reading Programming Assignments Finish K&R Chapter 1
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming I:
CSCE 212Honors Computer Organization
Machine-Level Programming I: Introduction Sept. 10, 2002
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
By Hugues Leger / Intro to GDB debugger By Hugues Leger / 11/16/2019.
Presentation transcript:

Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization

– 2 – CSCE 212H Spring 2012 Overview Last Time IA32 Assembler Lab02 intro Objectives for the Day Lecture 6 – slides 7-19; Lea for arithmetic examples, jumps Lecture 7 – slides 9- ; function calls rfact.c Pop Quiz address modes Gnu compiler “gcc”: gcc –O2 -S GDB Next Time/Future: Function calls, 16-Feb Tues Test 1 22-Feb Last day to drop a course or withdraw without grade of "WF"

– 3 – CSCE 212H Spring 2012 Class Exercise

– 4 – CSCE 212H Spring 2012 Gnu compiler “gcc”: gcc –O2 -S cp $c/arith.c. // $c=/class/csce /Code/ cp $c/rfact.c. gcc –S arith.c//produces arith.s gcc –O2 –S arith.c //produces arith.s optimized Gcc –O2 arith.c

– 5 – CSCE 212H Spring 2012 Using leal for Arithmetic Expressions int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } arith: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 16(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax movl %ebp,%esp popl %ebp ret Body Set Up Finish

– 6 – CSCE 212H Spring 2012 Understanding arith int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } movl 8(%ebp),%eax# eax = x movl 12(%ebp),%edx# edx = y leal (%edx,%eax),%ecx# ecx = x+y (t1) leal (%edx,%edx,2),%edx# edx = 3*y sall $4,%edx# edx = 48*y (t4) addl 16(%ebp),%ecx# ecx = z+t1 (t2) leal 4(%edx,%eax),%eax# eax = 4+t4+x (t5) imull %ecx,%eax# eax = t5*t2 (rval) y x Rtn adr Old % ebp %ebp Offset Stack z 16

– 7 – CSCE 212H Spring 2012 Understanding arith int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } # eax = x movl 8(%ebp),%eax # edx = y movl 12(%ebp),%edx # ecx = x+y (t1) leal (%edx,%eax),%ecx # edx = 3*y leal (%edx,%edx,2),%edx # edx = 48*y (t4) sall $4,%edx # ecx = z+t1 (t2) addl 16(%ebp),%ecx # eax = 4+t4+x (t5) leal 4(%edx,%eax),%eax # eax = t5*t2 (rval) imull %ecx,%eax

– 8 – CSCE 212H Spring 2012 Another Example int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } logical: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax xorl 12(%ebp),%eax sarl $17,%eax andl $8185,%eax movl %ebp,%esp popl %ebp ret Body Set Up Finish movl 8(%ebp),%eaxeax = x xorl 12(%ebp),%eaxeax = x^y(t1) sarl $17,%eaxeax = t1>>17(t2) andl $8185,%eaxeax = t2 & = 8192, 2 13 – 7 = 8185

– 9 – CSCE 212H Spring 2012 Gnu DeBugger (GDB) Overview Gnu (Gnu is not Unix) Free Software Foundation Richard Stallman – emacs fame Debuggers Allows one to execute a program step by step and observe the changes made during execution Allows one to execute a program step by step and observe the changes made during execution Dbx – Berkeley debugger Dbx – Berkeley debugger GDB – gnu debugger GDB – gnu debugger Compile with the “-g” option gcc –g source.c

– 10 – CSCE 212H Spring 2012 GDB QUICK REFERENCE Essential Commands Starting GDB Stop GDB Getting Help Executing your Program run arglist Shell Commands Cd, pwd, make, etc Breakpoints and Watchpoints b line-number Program Stack bt (backtrace, i.e. showstack) Execution Control Display Automatic Display Expressions Symbol Table GDB Scripts Signals Debugging Targets Controlling GDB Working Files Source Files GDB under Emacs

– 11 – CSCE 212H Spring 2012 GDB - Essential Commands gdb program [core] debug program [using coredump core] b func [file:]function set breakpoint at function [in file] run [arglist] start your program [with arglist] backtrace: display program stack p expr display the value of an expression c continue running your program n next line, stepping over function calls s next line, stepping into function calls sinext assembly instruction info reg/args/locals

– 12 – CSCE 212H Spring 2012 GDB - Display print [/f ] [expr] p [/f ] [expr] show value of expr [or last value $] according to format f: call [/f ] expr like print but does not display void

– 13 – CSCE 212H Spring 2012 GDB – Display Formats x hexadecimal d signed decimal u unsigned decimal o octal t binary a address, absolute and relative c character f floating point (char *) null terminated string

– 14 – CSCE 212H Spring 2012 GDB - Automatic Display Automatic Display display /f exprshow the value of expr each time program stops according to format f Formats – x d u t (binary) c f s (string) i (machine instr) Formats – x d u t (binary) c f s (string) i (machine instr)Examples display /i $eip - show the machine instruction everytime gdb stops display /i $eip - show the machine instruction everytime gdb stops display /x numshow the value of the variable “num” … display /x numshow the value of the variable “num” …

– 15 – CSCE 212H Spring 2012 GDB – eXamining Memory x [/Nuf ] expr examine memory at address expr; Optional format spec follows slashFormat N count of how many units to display N count of how many units to display u unit size; one of u unit size; one of b individual bytes h halfwords (two bytes) w words (four bytes) g giant words (eight bytes)

– 16 – CSCE 212H Spring 2012 GDB Example - fibonnaci #include #include int fib(int n){ int nm1, nm2; int nm1, nm2; int temp, temp2; int temp, temp2; printf("Calling fib(%d)\n", n); printf("Calling fib(%d)\n", n); nm1 = n-1; nm1 = n-1; nm2 = n-2; nm2 = n-2; if (n <= 1) return 1; if (n <= 1) return 1; else{ else{ temp = fib(nm1); temp = fib(nm1); temp2 = fib(nm2); temp2 = fib(nm2); return temp + temp2; return temp + temp2; }} /* fibonnaci function written with lots of extra variables */ main(){ int i,j; int i,j; i = 4; i = 4; j = i+1; j = i+1; i = fib(j); i = fib(j);}

– 17 – CSCE 212H Spring 2012 Compiling and Running GDB ares> gcc -g fib.c -o fib ares> gdb fib GNU gdb 6.3 Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i486-slackware-linux"...Using host libthread_db library "/lib/libthread_db.so.1". (gdb)

– 18 – CSCE 212H Spring 2012 GDB: Run, List gdb > run Starting program: /class/csce /Examples/Lab04/fib Calling fib(5) Calling fib(4) Calling fib(3) Calling fib(2) Calling fib(1) Calling fib(0) Calling fib(1) Calling fib(2) Calling fib(1) Calling fib(0) Calling fib(3) Calling fib(2) Calling fib(1) Calling fib(0) Calling fib(1) Program exited with code 010. (gdb) list 10, /* fibonnaci function written with lots of extra variables */ 12 int fib(int n){ 13 int nm1, nm2; 14 int temp, temp2; 15 printf("Calling fib(%d)\n", n); 16 nm1 = n-1; 17 nm2 = n-2; 18 if (n <= 1) return 1; 19 else{ 20 temp = fib(nm1);

– 19 – CSCE 212H Spring 2012 Setting Breakpoints and N, S, C commands Setting breakpoint – b command Setting breakpoint – b command (gdb) b 17 Breakpoint 1 at 0x80483d5: file fib.c, line 17. Running the program Running the program (gdb) run Starting program: /class/csce /Examples/Lab04/fib Calling fib(5) Breakpoint 1, fib (n=5) at fib.c:17 Next command or just n Next command or just n (gdb) n 18 if (n <= 1) return 1; Continue command Continue command (gdb) c Continuing. Calling fib(4) Breakpoint 1, fib (n=4) at fib.c:17 17 nm2 = n-2; (gdb) c Continuing. Calling fib(3) Breakpoint 1, fib (n=3) at fib.c:17 17 nm2 = n-2; (gdb) c

– 20 – CSCE 212H Spring 2012 Backtrace – Show Activation Records Backtrace (bt) show the stack of activation records Backtrace (bt) show the stack of activation records (gdb) bt #0 fib (n=2) at fib.c:10 #1 0x080483e7 in fib (n=3) at fib.c:10 #2 0x080483e7 in fib (n=4) at fib.c:10 #3 0x080483e7 in fib (n=5) at fib.c:10 #4 0x in main () at fib.c:24 Up // move up one Activation Record in the stack Up // move up one Activation Record in the stack (gdb) up #1 0x in fib (n=2) at … (gdb) up #2 0x080483f8 in fib (n=3) at … p expr // Print the value of expr p expr // Print the value of expr (gdb) p nm2 $3 = 1 (gdb) p nm1 $4 = 2 (gdb)

– 21 – CSCE 212H Spring 2012 What’s in the registers? (gdb) info reg eax 0x0 0 ecx 0x0 0 edx 0xf 15 ebx 0xb7fcdff esp 0xbffff510 0xbffff510 ebp 0xbffff528 0xbffff528 esi 0xbffff63c edi 0x1 1 eip 0x80483dc 0x80483dc eip 0x80483dc 0x80483dc eflags 0x cs 0x ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x0 0 Note ebp – the frame pointer Note ebp – the frame pointer Esp – the stack pointer Esp – the stack pointer Eip – the program counter Eip – the program counter

– 22 – CSCE 212H Spring 2012 A look at the stack using X (examine) (gdb) x/30wx 0xbffff528 // the frame pointer (%ebp) 0xbffff528: 0xbffff558 0x080483e7 0x x xbffff538: 0xbffff63c 0x xbffff558 0xb7ee88f0 0xbffff548: 0xb7fce7a0 0x x x xbffff558: 0xbffff588 0x080483e7 0x x xbffff568: 0xbffff63c 0x xbffff588 0xb7ee88f0 0xbffff578: 0xb7fce7a0 0x x x xbffff588: 0xbffff5b8 0x080483e7 0x x xbffff598: 0xbffff5a8 0xb7eb90d5 ebp’s: 0xbffff528, 0xbffff558, 0xbffff588, 0xbffff5b8 Return addresses ebp+4 Argument1’s ebp+8