0

Look at this code:

void F1() {
    for (int i = 0; i < 100; i ++)
        F2();
}

void F2() {
    for (int i = 0; i < 100; i ++)
        F3();
}

void F3() {
    int a = 0; // break point here
    for (int i = 0; i < 100; i ++)
        a ++;
}

I want to put a break point at the line int a = 0; in F3(), but I only want to pause when i==70 in F1() and i==80 in F2().

That means when I am in F3()'s scope, I have to peek F2()'s and F1()'s local variables. But in actual code, those two variables are not meant to be passed down.

What is the right way to do this?

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
landings
  • 686
  • 1
  • 8
  • 25
  • 1
    related, not necessarily a duplicate: https://stackoverflow.com/a/4251470/1132334 (object id approach, object ids are valid in the condition box of a breakpoint) – Cee McSharpface Apr 26 '21 at 08:21
  • What I usually do in such cases is to create a global variable for debugging. – Devolus Apr 26 '21 at 08:22
  • You cant see out-of-scope variables as they do not exist in another scope. – 0___________ Apr 26 '21 at 08:24
  • @0___________ inside the program no, but I wouldn't be so sure about MSVC debugger, since it supports the so-called [ObjectID references](https://abhijitjana.net/2010/10/20/how-to-track-an-object-which-is-out-of-scope-while-debugging/). – rustyx Apr 26 '21 at 08:39

4 Answers4

1

Maybe you can add a counter that track the number of times the fonction F3() is called.

//track F3() call of times
private int times = 0;
void F3() {
    int a = 0; // break point if times == 70 || times == 80
    for (int i = 0; i < 100; i ++)
        a ++;    
    //add 1 call of times
    times++;
}
Allen Hu
  • 141
  • 6
  • 1
    code only answer is cool. But perhaps you can add details and explanation. like : "_Maybe you_ want to add a counter that track the number of `times` the fonction `Foo()` is called. That way you may use that to track the number of execution of ... etc" – Self Apr 26 '21 at 09:05
  • This is indeed a solution. But sometimes what we need is not about counting. For example, maybe the condition I need in `F1()` is `some_array[i] == "Tom"`. – landings Apr 27 '21 at 14:59
1

If it is possible to change the code, the less interfering code that I can imagine is the following - without using the trick of the hits count of @Maaz.

It is based on using the current thread to "storage" the values.

void F1() {
    for (int i = 0; i < 100; i ++)
    {
        CallContext<Int32>.SetData("i-first", i);
        F2();
    }
}

void F2() {
    for (int i = 0; i < 100; i ++)
    {
        CallContext<Int32>.SetData("i-second", i);
        F3();
    }
}

void F3() {
    int a = 0; // break point here
    for (int i = 0; i < 100; i ++)
        a ++;
}

//Helper class for .Net Core.
//For .Net Framework you can use CallContext.LogicalSetData, CallContext.LogicalGetData
public static class CallContext<T>
{
    static ConcurrentDictionary<string, AsyncLocal<T>> state = 
         new ConcurrentDictionary<string, AsyncLocal<T>>();

    public static void SetData(string name, T data) =>
            state.GetOrAdd(name, _ => new AsyncLocal<T>()).Value = data;

    public static T GetData(string name) =>
            state.TryGetValue(name, out AsyncLocal<T> data) ? data.Value : default(T);
}

The conditional expression for the breakpoint is:

CallContext<Int32>.GetData("i-first") == 70 && CallContext<Int32>.GetData("i-second") == 80
0

You could set a conditional breakpoint that does a hit test. Set it to hit the value 7081 and it should work.

F2() is called 70 times, and for each of those 70 times, F3() is called 100 times. So, for the 71st loop, i.e. F1()'s i == 70, and 81th sub loop, i.e F2()'s i == 80, a hit test for 70*100 + 81 should work.

Standard_101
  • 325
  • 4
  • 14
-1

Yes, Follow below steps

  1. Set the breakpoint on the line you want to debug
  2. Right-click on the breakpoint
  3. Select "Conditions", this will prompt you to the pop-up
  4. Write the condition
  5. Close and Debug.
Syed
  • 417
  • 1
  • 6
  • 13