Thread: backward debugging in Visual Studio??

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    backward debugging in Visual Studio??

    Hello everyone,


    Suppose a program looks like,

    --------------------
    code block 1;

    if (condition == true)
    {
    code block 2;
    }
    --------------------

    I want to see the state of block 1 (variable values) if condition is true. So I am wondering whether Visual Studio supports backward debugging, if it supports, I could set a breakpoint at the beginning of code block 2, and if it is executed, I could run *back* to the beginning of code block 1 to execute code block 1 again see the detailed running process of code block 1.


    thanks in advance,
    George

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This should be moved someplace else, as it isn't C specific. It's compiler, or rather, IDE specific.

    1 - Install the MSDN.
    2 - Press F1.
    3 - RTFMSDN!


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The way I handle this is to set a stop on the variable. In the first code block, whenever that variable was changed to true (in your case), the program would instantly stop. Long before you got into the second code block.

    Very handy, and certainly supported by nearly all IDE's.

    Adak

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can also use a conditional breakpoint.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Quote Originally Posted by quzah
    This should be moved someplace else, as it isn't C specific. It's compiler, or rather, IDE specific.

    1 - Install the MSDN.
    2 - Press F1.
    3 - RTFMSDN!


    Quzah.
    What is RTFMSDN??


    regards,
    George

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you anonytmouse!


    Quote Originally Posted by anonytmouse
    You can also use a conditional breakpoint.
    I have tried ... But in my situation, it is hard to know the condition before an error occurs ... means the condisiton is not predictable and there is not a fixed a rule for defining conditions. The only point where I can find errors is, the execution of code block 2.

    In more details, I want to monitor the original values, which will incur the error. For example, code block 1 will be executed a lot of times and code block 2 will only be executed when there are some errors. I want to set a breakpoint to the beginning of code block 2 and find the states of variables in code block 1 when there are errors. I can not set a break point to the beginning of code block 1 since it will be executed a lot of times even if there are no errors ...


    regards,
    George

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you Adak!


    Quote Originally Posted by Adak
    The way I handle this is to set a stop on the variable. In the first code block, whenever that variable was changed to true (in your case), the program would instantly stop. Long before you got into the second code block.

    Very handy, and certainly supported by nearly all IDE's.

    Adak
    I have tried ... But in my situation, the condition bool variable is a function in my situation (not a simple variable), and it is hard to know the condition which will incur an error, before an error occurs ... means the condisiton is not predictable and there is not a fixed a rule for defining conditions. The only point where I can find errors is, the execution of code block 2.

    In more details, I want to monitor the original values, which will incur the error. For example, code block 1 will be executed a lot of times and code block 2 will only be executed when there are some errors. I want to set a breakpoint to the beginning of code block 2 and find the states of variables in code block 1 when there are errors. I can not set a break point to the beginning of code block 1 since it will be executed a lot of times even if there are no errors ...


    regards,
    George

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm a little confused as to why you are having problems with this.
    The code snippet you provide seems to indicate that code block 1 is within scope of code block 2. Is this the case?

    Because if it is you can simply place your breakpoint at the beginning of code block 2 and use the debugger to get information on the names in code block 1. All you have to do once the program breaks is to set watches for each name you want to check the value of.

    Another thing I have prolem understanding is why you say the condition predicate is unpredictable. I guess it's best if you provide us with real life code. Regardless, you can set conditional breakpoints where the condition part of the breakpoint is a function.

    Another option, which I'm almost sure MSVC++ also supports is offset breakpoints. You can set a breakpoint at the beginning of code block 2 and have it offset X lines from where it stopped. In this case you would have it offset upwards in the code, probably to the beginning of code block 1. However, there's a catch. You can only offset within the same stack frame.

    As you can see there's at least 3 options. But if code block 1 is not within scope of code block 2, then you need to provide us the code so that we can study the solution better. However, almost always the solution to that is to examine the call stack and then backtrace accordingly.
    Last edited by Mario F.; 09-27-2006 at 05:55 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you Mario,


    Quote Originally Posted by Mario F.
    I'm a little confused as to why you are having problems with this.
    The code snippet you provide seems to indicate that code block 1 is within scope of code block 2. Is this the case?

    Because if it is you can simply place your breakpoint at the beginning of code block 2 and use the debugger to get information on the names in code block 1. All you have to do once the program breaks is to set watches for each name you want to check the value of.

    Another thing I have prolem understanding is why you say the condition predicate is unpredictable. I guess it's best if you provide us with real life code. Regardless, you can set conditional breakpoints where the condition part of the breakpoint is a function.

    Another option, which I'm almost sure MSVC++ also supports is offset breakpoints. You can set a breakpoint at the beginning of code block 2 and have it offset X lines from where it stopped. In this case you would have it offset upwards in the code, probably to the beginning of code block 1. However, there's a catch. You can only offset within the same stack frame.

    As you can see there's at least 3 options. But if code block 1 is not within scope of code block 2, then you need to provide us the code so that we can study the solution better. However, almost always the solution to that is to examine the call stack and then backtrace accordingly.
    The code is too long and application dependent. I have simplified it. Here it is,

    // code block 1 begins
    set variable A to some value;
    some operations on variable A;
    set variable A to some other value;
    some operations on variable A;
    set variable A to some other value;
    some operations on variable A;

    // code block 2 begins
    if (true == condition)
    {
    error checking ...
    }

    In this scenario, I am interested in variable A and since it is changed several times, so even if I catch (by setting a break point at the beginning of code block 2) the error condition in code block 2, it is hard to know the detailed information of the original value and the values of variable A changed each time.


    regards,
    George

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You still don't provide enough information, george.

    what exactly is code block 1? just a set of operations in scope of code blocks 2? In other words, things like "set variable A to some other value;" and "some operations on variable A;" are happening inside functions? Or instead they are just a string of expressions within the same function where the error checking is happening?

    If they are happening inside functions, backtrace in the call stack. Check your compiler manual to learn how to. If not, you can use breakpoint offsets. Also check your compiler manual on how to.

    For all cases, you can set the breakpoint at the beginning of code block 2.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you Mario!


    Quote Originally Posted by Mario F.
    You still don't provide enough information, george.

    what exactly is code block 1? just a set of operations in scope of code blocks 2? In other words, things like "set variable A to some other value;" and "some operations on variable A;" are happening inside functions? Or instead they are just a string of expressions within the same function where the error checking is happening?

    If they are happening inside functions, backtrace in the call stack. Check your compiler manual to learn how to. If not, you can use breakpoint offsets. Also check your compiler manual on how to.

    For all cases, you can set the breakpoint at the beginning of code block 2.
    1. Backtrace means call stack?

    2. Breakpoint offsets is a good idea, does Visual Studio 2003 supports it?


    regards,
    George

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1. Backtrace means call stack?
    Yes.

    Also see http://www.cprogramming.com/debuggers.html and maybe try googling for it: http://www.google.ca/search?hl=en&q=...G=Search&meta=
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    set variable A to some value;
    some operations on variable A;
    set variable A to some other value;
    some operations on variable A;
    set variable A to some other value;
    some operations on variable A;
    Maybe avoid over-enthusiastic variable re-use and have
    Code:
    set variable A to some value;
    some operations on variable A;
    set variable B to some other value;
    some operations on variable B;
    set variable C to some other value;
    some operations on variable C;
    or maybe check for errors between operations.

    Or maybe save the 3 separate interesting values of A in other variables so they're available when you hit the breakpoint.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM