33

I would like to know if there is any way in which I can monitor a value of a variable until for example a counter reaches a value and then log the output of variable value during each counter value?

2 Answers 2

44

Set a watch point on the counter:

(gdb) watch var

And make that watchpoint conditional:

(gdb) cond <watchpoint_number> var>=value

If you want to log to a file:

(gdb) set logging file <filename>
(gdb) set logging on

By default gdb logs to gdb.txt

Sign up to request clarification or add additional context in comments.

1 Comment

How do I log it to an output file? what is th epath of default log file if I say "set logging on"
4

You can use watchpoints to make gdb monitor the value of a variable, and break execution of the program when the value changes. Once execution is stopped, you can use gdb's command set to inspect and print the value. I'm not sure if you can script gdb to do this automatically each time it breaks.

4 Comments

@Jerinaw What answer are you talking about?
You wrote "Yes, you can do that too - check out the "commands" command..." it would have been more helpful to provide a link to the command that answers the question.
Here is a link to the commands command sourceware.org/gdb/current/onlinedocs/gdb/…

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.