Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b...

117
Assignment 0: Using the Debugger

Transcript of Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b...

Page 1: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Assignment 0: Using the Debugger

Julie Zelenski
A tutorial introduction to the QT Creator debugger written by Keith Schwarz
Page 2: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Hi everybody!

Page 3: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

As part of Assignment 0, we'd like you toget a little bit of practice using the

debugger in Qt Creator.

Page 4: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

The debugger is a tool you can use to helpsee what your program is doing as you

run it.

Page 5: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

It's really useful for helping find errors inyour programs, and the more practice youget with it, the easier it'll be to correct

mistakes in the programs you write.

Page 6: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Think of this guide as a little tutorialwalkthrough to help give you a sense of

how to use the debugger and how to makesense of what you're seeing.

Page 7: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

To start things off, open up the Name Hashprogram you ran in Part One of this assignment.Scroll down to the nameHash function so that youcan see the entire function in your window.

Page 8: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Move your mouse cursor so that it's in the spaceright before the line number for line 66.

Now, click the mouse!

Page 9: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

When you do, you should see a red circle with alittle hourglass pop up.

This is called a breakpoint. If we run the programin debug mode, whenever the program gets tothis line, it will pause and open up the debugger

so we can see what's going on.

Page 10: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Now, we're going to run this program in debugmode. To do so, click on the “run in debug mode”button in the bottom-right corner of the screen.It's the one just below the regular green “run”

button. When you do...

Page 11: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

... you should see something like this! Notice thata bunch of extra panels popped up in Qt Creator.We'll talk about what each of these windows mean

in a second.

Page 12: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

In the meantime, type in the first name Ada and hitenter, as shown here.

Page 13: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Now, type in Lovelace as a last name, butdon't hit enter yet!

Page 14: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

As soon as you hit enter, a bunch of things aregoing to pop up in Qt Creator. Don't panic! It's

normal.

Page 15: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

With that said, hit enter,and watch the magic happen!

Page 16: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Shazam! We're back in Qt Creator, and there'stons of values showing up everywhere.

Page 17: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

There's a lot going on right here. Let's see what'shappening.

Page 18: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

First, notice that our red breakpoint now has ayellow arrow in it.

Page 19: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

This yellow arrow indicates where in the programwe are right now. The program stopped running atthis line because we hit that breakpoint you set

earlier.

Page 20: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Whenever you pop up the debugger, it's good tofigure out exactly where you are in the programthat you're running, so you'll get into the habit

of checking for this yellow arrow.

Page 21: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Next, let's take a look at this panel.This is called the call stack.

Page 22: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Right now, we know we're in the nameHashfunction, because our helpful friend the Yellow

Arrow tells us exactly what line we're on!

Page 23: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

However, the yellow arrow can't tell us exactlyhow we got to this part of the program. Whatpart of the program actually called nameHash?

Page 24: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

The call stack can tell us exactly that!

Page 25: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Notice that the call stack lists a series of differentfunctions in order. Here, it has nameHash (where weare now) at the top, and right below that is qMain.

Page 26: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Go and double-click the call to Main on Level 1.When you do...

Page 27: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

... you'll end up over here!

Page 28: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Notice that the highlighted line here includes a callto the nameHash function. This the part of the codethat actually called nameHash, which is how we got

to the line with the breakpoint!

Page 29: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Generally speaking, you can use the call stack as away to see which function calls got us to the point

where the program paused at the breakpoint!

Page 30: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You might notice that there's some more stuffin the call stack beyond just main and nameHash.

What are those?

Page 31: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Let's find out! Double-click on the functionon Level 3. (Here’s what it looks like on mysystem; you might see something different.)

Page 32: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

When you do, you’ll see something like this.(This might be different depending on your OS.

Don’t panic if it doesn’t exactly match.)

Page 33: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Yikes! This looks Hairy and Scary! What happened?

Page 34: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Whenever you start up a program in CS106B, there'sa little bit of code that we automatically call foryou, which does things like setting up the console.

Page 35: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

This code will show up in the call stack below youractual program.

Page 36: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You shouldn't need to dig around this deep inthe call stack, and if you do, it should probablybe a message telling you to back up a bit back to

code that you actually wrote.

Page 37: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

So let's jump back to the code that we actuallywrote.

Page 38: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

To do that, double-click on Level 0, the call tonameHash. When you do...

Page 39: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You'll be teleported back to safety!

Page 40: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Let's quickly recap what we've seen so far.

Page 41: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

To set a breakpoint so that we can pause theprogram and look around, click in the margin justbefore the line number where you want to pause.

Page 42: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Once the breakpoint is reached, it will pull up allsorts of useful information.

Page 43: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

The yellow arrow points out where we are right now.

Page 44: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

The call stack shows us how we got into the currentfunction.

Page 45: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Now, let's see how we can read the values of thevariables in this function.

Page 46: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Look up at this panel over here.

Page 47: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

This window lets you take a look at all the valuesof the local variables that are in scope right now.

Page 48: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Depending on what OS you're using, these might bein a different order, and there might be someweird-looking ones in there in addition to nicer

ones like ch and hashVal.

Page 49: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

If we ignore the weird-looking ones, we cansee some nice, familiar names.

Page 50: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

For example, here you can see the values ofkLargePrime and kSmallPrime, which match the

values they were declared with.

Page 51: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

We can also see that, at this point, hashValis still zero.

Page 52: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

As we walk through the program one step at a time,we'll see these values change.

Page 53: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Now, let's take a look at this for loop.

Page 54: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

This loop is a range-based for loop. It says“for each character in the string first + last,

do something with that character.”

Page 55: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Remember (from a while back) that we enteredthe name Ada Lovelace.

Page 56: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

If we take a look at the current value of thevariable ch, we can see that it has the value A.

That's the first letter of the name Ada Lovelace.

Page 57: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

So now we know where we are (line 66), how we gotthere (main called nameHash), and the values in the

program at this point.

Page 58: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Now, let's do something really cool – we're going torun this program one line at a time, watching what

happens at each step!

Page 59: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Right above the stack trace, you'll see there aresome small button icons.

Page 60: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

These buttons let you resume the program, stop theprogram, walk through it one line at a time, etc.

Page 61: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Move your mouse so that you're hovering over thebutton that's third from the left. If you hover

over it, it should say “step over.”

Page 62: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Once you're confident that you're on the “Step Over”button – and not the “Step Into” or “Step Out”

buttons – go and click it! When you do...

Page 63: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

...your window should look something like this.

Page 64: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Okay! A few things have changed. Let's see what'sgoing on.

Page 65: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

First, notice that our helpful Yellow Arrow friendis now pointing at line 67.

Page 66: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

We're now at the line right after the one wherewe stopped. You just ran a single line of the

program! Pretty cool!

Page 67: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

So what did that line of code do?

Page 68: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

This line converts ch to lower case. The tolowerfunction takes in a character and returns a lower-case version of it, so this overwrites ch with a

lower-case version of itself.

Page 69: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You can actually see this by looking at the valuespanel over on the side!

Page 70: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Notice that the value associated with ch has changedfrom 'A' to 'a' – it's now in lower-case!

Page 71: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

If you'll notice, this value is in red while all theother values are in black.

Page 72: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

This indicates that the value here has changed sincethe previous step. This is a really useful way tokeep track of what's changing as you run the

program.

Page 73: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Now, let's take a look at line 67, where we areright now.

Page 74: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Not gonna lie, this is a pretty dense line ofcode. It performs some weird sort of

mathematical calculation on a bunch of differentvalues.

Page 75: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Fundamentally, though, it's just computing someweird function of some values and stashing it into

hashVal.

Page 76: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Let's go run that line of code and see whathappens!

Page 77: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Hover over the “Step Over” button, confirm thatthe button you're clicking really is “Step Over,”

and click it! When you do...

Page 78: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

... you'll end up with something like this!

Page 79: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Let's see what's changed.

Page 80: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

First, notice that the value stored in hashValchanged to 97. We know that it changed because the

value is in red, and we know that nothing elsechanged because nothing else is in red!

Page 81: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Second, notice that we're back up at the top ofthe for loop, since that's where the yellow arrowis pointing. We ended up back here because this

is the next line that gets executed.

Page 82: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

We just single-stepped through a single iterationof that loop! Pretty cool!

Page 83: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Let's go do it again!

Page 84: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Again, move your mouse over the Step Overbutton (and make sure it says “Step Over” and

not something else!), then click it.

Page 85: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Now we're here! Notice that ch now has the value'd', which is the second letter of the name Ada.

Page 86: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Go click “Step Over” again to run this line ofcode.

Page 87: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You should be here now. Notice that none of thevalues changed. That makes sense, since all we didwas convert a lower-case 'd' to a lower-case 'd'.

Page 88: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Now, click “Step Over” one more time.

Page 89: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You'll now be at this point in the program. We'vecovered up the value of hashVal in this image, because

at this point you should be able to see what hashVal is byreading the value in the side pane. This is the special valuewe want you to tell us when submitting the assignment!

???

Page 90: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

???

To finish up this section on the debugger, we'd like to showyou two last little techniques that you might find useful

when debugging programs.

Page 91: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

???

To start this off, click on the the breakpoint that we setearlier in the program. If you do...

Page 92: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

???

... it should clear the breakpoint. Now, if we were to runthis program again in debug mode, it would not stop at this

point, since nothing's telling it to!

Page 93: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

???

Now, take a look back at these buttons.

Page 94: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

???

Hover your mouse over the one that's on the far right. When you hover over

it, it should say “Step Out.”

Page 95: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

???

If you click this button, it will keep runningthis function up until it completes and

returns.

Page 96: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

???

Now, go click that button. If you dideverything right...

Page 97: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

... you should end up with something thatlooks like this!

Page 98: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Let's take a minute to get our bearings.Where exactly are we?

Page 99: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Well, the yellow arrow indicates that we'reback in main again. Cool!

Page 100: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

We can see that the nameHash functionreturned 1967457. Thanks, debugger!

Nicholas Bowman
Please note: This information in the return value panel may not appear on all systems. If you don’t see this value, then justmove on to the next step of the tutorial. �
Nicholas Bowman
Page 101: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

But if you look up over here in the valueswindow, you can see that hashValue has somereally weird-looking number stored in it.

(You'll almost certainly see something differenton your system.)

Nicholas Bowman
“Really weird-looking number” is a relative term here, asthe number will vary from computer to computer andmay be as innocent looking as “0” or “1” or as wild as “2349980890”.�
Nicholas Bowman
Page 102: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

But it looks like we're setting hashValueequal to the number that was returned bythe nameHash function. What's going on?

Page 103: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

This is pretty cool, actually!

Page 104: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

What's happened is that we've just returnedfrom nameHash with a value, but since we're

going through the program one step at a time,we haven't actually assigned that value to

hashValue yet!

Page 105: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Let's do a “Step Over” so that we can finishexecuting this line. Click “Step Over,” and

if you did everything right...

Page 106: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

... you should see the right value get stored(notice it's in red!) and we've moved to the

next line.

Page 107: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

At this point, we've seen just about everythingwe care about. Rather than single-steppingall the way to the end, let's just tell the

program to keep on running.

Page 108: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

To do this, click on this button. If you hoverover it, it says “Continue,” and that buttonmeans “unpause the program and let it keep

running from here.”

Page 109: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

If you do, you should see something like this.(The program window might not automaticallypop up. That's okay! Just open it manually.)

Our program is now done running!

Page 110: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

So there you have it! You've now gotten morefamiliar with the debugger!

Page 111: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You know how to set a breakpoint to pause theprogram at a particular point.

Page 112: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You know how to read the call stack and tosee the values of local variables.

Page 113: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You know how to single-step the program andsee what values change.

Page 114: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

You know how to run a function to completion,and how to let the program keep on running.

Page 115: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

As you write more and more complicatedprograms this quarter, you'll get a lot morefamiliar using the debugger and seeing how

your programs work.

Page 116: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

And, if you continue to build larger and largerpieces of software, you'll find that knowing how

to use a debugger is a surprisingly valuableskill!

Page 117: Assignment 0: Using the Debugger A tutorial introduction ...web.stanford.edu › class › cs106b › assignments › assign0 › DebuggerTutorial.pdfIt's the one just below the regular

Hope this helps, and welcome to CS106B!