From the course: Visual Studio: Advanced Debugging Tools

View function calls in historical debugging - Visual Studio Tutorial

From the course: Visual Studio: Advanced Debugging Tools

Start my 1-month free trial

View function calls in historical debugging

- [Instructor] Our first stop is to look in tools, options, go to IntelliTrace, and we're going to switch from the default to the second item in this list. This will give us the extra call information. In the documentation, Microsoft calls this function call. Of course in C# we typically call those method calls. The point of saving this value is that we can dig ourselves deep into the call stack and then use historical debugging to move around in our historical call stacks. Next we'll look at the application. This is a loan calculation application. So when you click on this button, it calculates the monthly payments for three loans shown here with these amounts, 1100, 2200 and 3300. Notice how they start with one, two, three? This will be useful when we get into the historical debugging and start looking at multiple iterations of the calls, we'll know within the first, second or third run of the loan calculation. Now let's look at this interest rate here. That's generated by making a call to a simulated online bank service, so every time I click this button, I get a different value, this is a randomly generated interest rate. Other than that, you can see we got the loan, the interest rate for 10 months, and this is the calculated payment. So now we can look at the source code. We'll start by looking at MainWindow.xaml. We start out declaring some variables to hold values like monthly payment, the current rate, the number of months, the loan amount, our formatted results, then we set the real values for the number of months, the loan amount. And then I go out to another class, and call this method, call it GetCurrentLoanRate and that method, if I press F12, that is using the simulated online services to get the bank rate from the system. And then we get our loan fee and then we build up a value and return our calculated loan rate. Now of course we're going to go deeper into the call stack here. There's a method called GetBankRateFromSystem. Press F12 to look at that, and you see it's just a randomly generated value. Now let's go back. So here let's take a look at this again. We're going to get the current loan rate, store that here, then we make a call to calculate monthly payment, F12 to take a look at that. This is not an accurate financial formula in here, it's very simple. We get the number of months, the loan amount and the loan rate and then it calculates your payment and returns that. That's our basics of the application. Now, let's do some historical debugging. Let me close all these windows. And I'll put a break point in here, right here on this first line where we're setting a value. Press F9 to set the break point and close these windows. And then I'll hide solution explorer because that gives us more room on the screen, and then we're ready to start our debug session. All right, first step is to start a debugging session. Break point by clicking on this first button. Then I want to have my call stack and my locals windows pinned to the bottom. You can find those in the debug window section. Here you can do watch autos and locals, and here's where you find call stack. Now remember that we're recording every step in the debugger so as I step like this, by pressing F10 it recorded that step and it's also saving this value. This is a local value, but also it will do data tips. If I hover over this it's going to save that amount. Or let's say do it here. Next, I will step into this GetCurrentLoanRate method. You can see in the call stack, I'm one level deep in the call stack. And then down this line of code I'm going to drill down into another level, making a call to the online services class. Like that, I'm pressing F11 to go through my code. And now I am back in the button click handler. Now I'll step into the calculate monthly payment, and at this point I've added the item to the list box and I've got this formatted results string here. Now I'm ready to try the next iteration where I have the 2200 value. And rather than stepping through all that code with F11, I'll press F10, that will just skip over those ones. So that'll get us quickly to the end line here. So at this point I've run all three of the loans. And now I'm ready to do the historical debugging. In the earlier demo in this chapter, I went up to the debug toolbar, and there was a button up here to step backwards, but since I have turned on function calls, that's not there anymore. Instead, you'll notice over here on the side bar, is the yellow arrow that indicates the current break point where the code is going to execute. But then there is these other buttons that have to do with the historical debugging. So as soon as I click on any of these, we'll enter historical debugging. So right now it says go to previous call, or until a trace event, so what was I doing? I was stepping through my codes. So if I click on this button, we'll switch over to historical debugging, and it shows that I was running this line of code. And I think I just clicked on the run button by accident. So let's just go do that again. Click here and now I've got a choice of going back one more step, going forward one more step, or I can step into this method. Doesn't look like that did much, so let's go back two steps and get to this part here where I was going to step into this CalculateMonthlyPayment. Actually I don't want to do that. Let's keep going backwards. I'll step back a couple more. Let's get back to the first item. You have to be careful, when you're clicking these arrows move so if you just hold your mouse in one position and click you might click the wrong button. Pay attention to what you're doing. Let's stop here. The values are at 2200 when I move into this line. So right now you see it's 1100, that's the historical data that was there from the previous run, and the number of months was 10. Now I'll step forward, that gets set to four, step forward again, you see that's 2200. That's my historical data. And now I want to step into this CalculateMonthlyPayment. I can click on this crooked arrow here. See this is step into, it's the same as pressing F11 in a live session. And that'll step into that method. You see that I'm two levels deep in my call stack here. And now I'm looking at the local variables, the historical information saved when I ran this method with the values of 2200, months and so on. And you notice how Fishel Studio is giving us extra clues that we are doing historical debugging here? There's the bars across the top. Also if you look at my call stack, it says historical debugging here, same with my locals and my autos. That's so we know that we're looking at historical data. And also when I do things like my data tips, let's try that again, I should say up here in the parameters, you see that it says value at method entry. So that's the value that was passed in when the method was called. And then here's the value. It says data is not readily available. See, the local windows were available in IntelliTrace data. Okay so let's step through the code. Again using these buttons here, or can I press F11? I can, look at that. Historical debugging with F11. And then remember that's one of the points of Microsoft's idea, that you use the same tools that you use for live debugging for historical debugging. So F11 to step through this historical data, now exit this method call back to the line 33 in the button handler. That was interesting. Let's see if I can drill down even further into the call stack. Let's go up here to where I called GetCurrentLoanRate. So I'll back up, I'll back up to here, then step into by pressing F11. Step in, and now I am in. I'm three level deep. So I'm at this online services. Let's return back to live debugging. There's several ways you can do that. You can click on this hyperlink at the top or you can click on this arrow here. Go to live mode, or you can press F5. I'll try this one in the other example. I'll try this one here. And now we are back at live debugging. So that's a quick look at how to look at your function calls from the historical debugging tools.

Contents