COMP 2710 Software Construction Introduction to GDB

Slides:



Advertisements
Similar presentations
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
Advertisements

The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Memory & Storage Architecture Seoul National University Computer Architecture “ Bomb Lab Hints” 2nd semester, 2014 Modified version : The original.
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
Program Input and the Software Design Process ROBERT REAVES.
Debugger Presented by 李明璋 2012/05/08. The Definition of Bug –Part of the code which would result in an error, fault or malfunctioning of the program.
Gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Chocolate Bar! luqili. Milestone 3 Speed 11% of final mark 7%: path quality and speed –Some cleverness required for full marks –Implement some A* techniques.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
ENEE150 – 0102 ANDREW GOFFIN Testing and Debugging.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Debugging and Profiling With some help from Software Carpentry resources.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
1 SEEM3460 Tutorial Compiling and Debugging C programs.
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
C++ crash course Class 9 flight times program, using gdb.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
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.
Mastering LMC Coding Part #1 Introduction to Low Level Languages Introduction to Little Man computer Simple examples (demos) with video tutorials included.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors Locate seg faults and bus errors Prepared by Dr. Spiegel.
Lab 4: Loops and Iteration Graham Northup
DEBUG.
Computer System Laboratory
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors
EGR 2261 Unit 11 Pointers and Dynamic Variables
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Debugging with Clion and GDB
Repetition Structures
BIT116: Scripting Lecture 06
ECE Application Programming
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
CSE 374 Programming Concepts & Tools
Testing and Debugging.
Chapter 2 Assignment and Interactive Input
Debugging CMSC 202.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
Chapter 5: Loops and Files.
COMP 2710 Software Construction Pointers
gdb gdb is the GNU debugger on our CS machines.
Introduction to Computer Systems
Lab: ssh, scp, gdb, valgrind
Barb Ericson Georgia Institute of Technology Dec 2009
Computer Architecture “Bomb Lab Hints”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lab: ssh, scp, gdb, valgrind
Debuggers.
Common C Programming Errors, GDB Debugging
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Debugging CSCE 121 J. Michael Moore.
Tonga Institute of Higher Education
Using a Debugger 1-Jan-19.
Repetition Structures
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Debugging Taken from notes by Dr. Neil Moore
When your program crashes
Our Environment We will exercise on Microsoft Visual C++ v.6
Debugging Taken from notes by Dr. Neil Moore
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Repetition Statements (Loops) - 2
CSE 303 Concepts and Tools for Software Development
Chapter 15 Debugging.
Debugging.
Introduction to C CS 3410.
Presentation transcript:

COMP 2710 Software Construction Introduction to GDB Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu See /comp2710/samples/gdb_factorial.cpp Error: init j = 1; i<“=“num Enter the number: 5 The factorial of 5 = 120

Demo 1: gdb_segfault.cpp Please observe messages after we run: $ ./gdb_segfault.cpp $ gdb gdb_segfault.cpp /comp2710/samples/gdb_sample.cpp /comp2710/samples/gdb_factorial.cpp /comp2710/gdb_segfault.cpp /comp2710/gdb_linked_list.cpp

Using GDB Why use a debugger? Usually you can figure out what’s going on just by using cout (print statements) Debuggers allow for more fine-tuned control, and don’t force you to repeatedly re-compile your code every time you want to test something new

Using GDB C++ programs with the g++ compiler can use GDB - the Gnu debugger Function of a debugger: set breakpoints examine the contents of variables dereference pointers to see what they’re pointing to step into functions to see what’s happening

Using GDB First step: we need to compile the program especially for debugging purposes Add the flag –g when compiling Your foo.cpp programs: $ g++ -g -o foo-dbg foo.cpp This will create a foo-dbg executable

Using GDB Without the debugger, that executable will run perfectly normally can do: ./foo-dbg ...and get perfectly respectable output. You can also get into the details, using gdb: $ gdb foo-dbg

Demo 2: run a program with GDB samples/gdb_sample.cpp Please observe messages after we run: $ gdb <executable_file> What will happy if we forget to compile the source code? What will happy if we forget to use the “-g” flag? /comp2710/samples/gdb_sample.cpp /comp2710/samples/gdb_factorial.cpp /comp2710/gdb_segfault.cpp /comp2710/gdb_linked_list.cpp

Using GDB gdb will open a prompt (gdb) <enter commands here> Keywords: break start, run step, next print continue

Using GDB Debuggers have the concept of a breakpoint No matter what the code is doing, adding a breakpoint to your code will force the program to halt when it reaches that line (gdb) break foo.cpp:9 ...will add a breakpoint at line 9 of foo.cpp

Using GDB Set up several breakpoints where you think that your code is probably having problems, or where you’d like to see the contents of the variables You can add as many breakpoints as you like on every line is overkill, but it won’t hurt you Once the code hits the breakpoint, you have several options

Using GDB Set up breakpoints before you start the program When you’ve got your breakpoints, type start or run at the command line, and the code will start running until it hits a breakpoint (gdb) start You can also use start / run to restart a currently running program if you want to go back to the beginning

Using GDB When the execution hits one of your predefined breakpoints, it’ll stop and you’ll have the option to give more commands to the debugger Breakpoint 1, main () at foo.cpp:9 9 cout << “what is the number?" << endl; (gdb) The line that gets displayed is the line you chose to stop at – note that at this point, execution is right before this line, so the line hasn’t actually run yet

Demo 3: set up a breakpoint Where do you want to set up a breakpoint? $ gdb gdb_sample (gdb) break <which line?> What will happy if we set a breakpoint at a line without any code? What will happy if we are at line 9, and set up a breakpoint at line 6? /comp2710/samples/gdb_sample.cpp

Set up a watchpoint watch x == 3 Set a watchpoint, which pauses the program when a condition changes (when x == 3 changes). Watchpoints are great for certain inputs (myPtr != NULL) without having to break on every function call. /comp2710/samples/gdb_sample.cpp

Demo 4: set up a watchpoint Command “continue” $gdb gdb_factorial b 12 s //Step into factorial() watch i == n-1 continue /comp2710/samples/gdb_sample.cpp

Using GDB In order to make the line run, we can use the step and next commands These are effectively equivalent, they move execution forward BUT one crucial difference: next moves forward in the same function (in this case the main function). after one line, it’ll stop again and wait for a command step will stop after one line, but if there are any function calls on the line, it’ll “step” into them and stop there

Using GDB Sometimes you’ll get a blank line as a prompt – that’s because you’re still running your program, and so anytime you have a cin >>, you’ll have to stop and enter input 15 cin >> value; (gdb) next 1030 1415

Demo 5: use “next” How to use “next” and trace to the line that has “cin”? /comp2710/samples/gdb_factorial.cpp /comp2710/samples/gdb_sample.cpp

Using GDB The step command moves into a function 17 object = testFunction(var1, var2); (gdb) step testFunction:: testFunction (this=0x804d008, var1=30, var3=18) at foo.cpp:12 12 testFunction(); There are a couple of things to look at here: testFunction::testFunction – name of function this=0x804d008 – that’s the memory address of the object we’ve just created var1, var2 are the arguments to the function foo.cpp:12 is where we’re stopped – line 12 of foo.cpp

Demo 6: use “step” Function: factorial() in main() /comp2710/samples/gdb_factorial.cpp Function: factorial() in main() Step into the “factorial” function. /comp2710/samples/gdb_sample.cpp

Using GDB Be a little careful when you use step If you have a function that belongs to a standard library like string or vector, it’ll go ahead and step into it, but the output will be completely meaningless to you (Why?) (you can sort of decipher it but it’s really not worth it, you can go ahead and trust that the problem is on your end)

Using GDB If you don’t like what you’re seeing, and you know the problem isn’t here, you can type continue to get to the next breakpoint (gdb) continue Continuing. Breakpoint 3, main () at foo.cpp:22 22 clientManager.add_client(client_info); (gdb)

Using GDB After running this line (with which keyword?), I’d like to see what ends up inside the client.name We can use the print statement for that

Using GDB Print tells you what’s currently in the variable Sometimes this will look meaningless: memory addresses are values for pointers (gdb) print clientManager.client_DB $17 = std::vector of length 1, capacity 1 = {0x804d008}

Demo 7: use “print”, “display”, “undisplay” Use “print” to see values of the two variables - i and j How? /comp2710/samples/gdb_sample.cpp

Using GDB Often a simple print statement will be totally sufficient (gdb) print flightCtr $18 = 0 We can use the dereference operator if we happen to have a pointer we’d like to examine (gdb) print client_pointer $19 = (Client *) 0x804d030 (gdb) print *client_pointer $20 = {SSN = 123456, name = “sean”, … }

Using GDB Arrays make it pretty easy to be examined, since they’re just a stretch of memory Vectors are a little trickier – we have no direct access to the array underlying it. But the debugger has a way around it! (gdb) print *(ta.flights._M_impl._M_start)@ta.flights.size() $24 = {0x804d008} (gdb) print *(*(ta.flights._M_impl._M_start)@ta.flights.size()) $25 = (FlightTime *) 0x804d008 (gdb) print *(*(ta.flights._M_impl._M_start)@ta.flights.size())[0] $26 = {beginTime = 630, endTime = 855, valid = true, invalid_num = 0} It’s a little ugly, but it gets you where you need to be.

Using GDB You’ll even be able to call some functions, but not all Remember that if you have a pointer to 0, it’s a null pointer and shouldn’t be dereferenced Fortunately gdb is smarter than our plain compiler and you won’t get a segfault (gdb) print *client_pointer Cannot access memory at address 0x0

Demo 8: use “set” set x = 3 set x = y Set x to a set value (3) or to another variable (y) /comp2710/samples/gdb_sample.cpp

Exercise: debug gdb_linked_list.cpp Where to set break points? Watch which variables? /comp2710/samples/gdb_sample.cpp