1 / 10

CS 115 Lecture

Learn how to effectively debug your program by using breakpoints, single stepping, and monitoring variables with the help of a debugger tool. Discover the techniques to identify and solve programming errors efficiently.

evangelinej
Download Presentation

CS 115 Lecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 115 Lecture Debugging Taken from notes by Dr. Neil Moore

  2. Debugging We’ve seen how to write test cases to help you find bugs in your program. What should you do when you find something is wrong? • Find the line that’s wrong and fix it • Which line is wrong? • We know the symptoms, not the disease • So our job is like that of a doctor • Find out what’s making the patient sick!

  3. Debugging • Sometimes just reading and tracing the program is enough • And once upon a time that was the only option • But doctors can run tests, ask questions… • If we could interact with the program while it’s running… we might be able to see the bug as it happens, we need “X-Ray vision”

  4. The debugger • The debugger is a tool for controlling and inspecting a program as it runs • Part of most IDEs (WingIDE and IDLE for sure) • It does not find the bugs for you • Instead, it • “Slows down” the program by letting you run it one line at a time (step through code) • Shows you variables and their values as they change • Shows what functions are currently running (“call stack”)

  5. Breakpoints When using the debugger, you usually start by setting a breakpoint. • This tells the debugger to pause the program just before it executes that line. • In WingIDE, click next to the line number • A red dot appears to show there is a breakpoint there. • Click it again to turn it off • Note: breakpoints are not saved with the program! • In IDLE, right click the line of code and choose “Set breakpoint”. • The line will be highlighted in yellow

  6. Breakpoints • To run with the debugger in WingIDE, click the “Debug” icon, the program will start running • In IDLE select Debug → Debugger then run the program normally (using Run Module) • The program will run full-speed until control reaches the breakpoint • Then the program pauses, the IDE gives you control • If you run the program without using “Debug”, breakpoints are ignored, the program runs normally. • You may have to put in more than one breakpoint if you have branches or loops, to make sure execution actually reaches one of them

  7. Single-stepping Single stepping means running the next line of the program. In most IDEs there are 3 ways to single-step. • Step Over (“Over” in IDLE) • Execute the current line, pausing again when it finishes • If the line calls a function, the whole function’s code will be executed without being seen

  8. Single-stepping • Step Into (“Step” in IDLE) • Execute the current line, pausing at the start of the next line to be executed • If the line calls a function, execution will pause at the start of that function’s definition • Step Out (“Out” in IDLE) • Executing the rest of this function, pausing after it returns • Stepped into a function you didn’t want to see? Use this to get back to the calling code

  9. Single-stepping • More about the difference in the different modes when we cover functions • These stepping functions only work when your program is paused by the debugger • So only in debug mode • And you must have a breakpoint set somewhere

  10. Debugging variables The watch window shows you the variables that exist before executing the current line • In WingIDE, called “Stack Data” • In IDLE, called “Locals” • Shows variables that are in scope • Already defined in this function • Not destroyed yet (at end of the function) • Also shows their values and types • Watch this window when single-stepping • That will show you which variables are changing and how • Make sure they change as you expect them to!

More Related