0

while debugging I need to print the value of a variable that is declared in the else block. something like this :

if(condition){ 

}
else {
string str = "abcd";
strcpy(globalvariable,str,sizeOf(str));
}

I want to see the value of str.

2 Answers 2

3
  1. Run the program inside the debugger.
  2. Set break point to stop the execution of program sequence using break command. In your case, (gdb) break strcpy to break every time it is being called strcpy in else.
  3. To print you can use any of the following, x str, x/s str, print str, print "%s", str.
Sign up to request clarification or add additional context in comments.

1 Comment

I just wanted to add that it may be necessary to turn off compiler optimization if you expect this variable to actually exist in the executable code.
0

You can't see the value of str if condition is true during program flow because it does not exist in memory in this case.
You have to enter else block somehow, either during normal program flow or using gdb jump command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.