0

I am debugging a C++ code using commandline gdb in Ubuntu. I have a class A with different functions func1, func2 and func3 each of which ends on lines 28, 42 and 64 in the file A.cpp. The func1 uses some local variables like p1, q1 and class member variables such a_r, a_t. Similarly func2 and func3 have local variables such as p2, q2 and p3, q3. The func2 also uses another class member variable a_s. They also use some variables other than the ones mentioned here. Now these functions are called inside a loop with loop varaiable l_num.

I would like to log the above variables to a .txt file when l_num has a value between 10 and 20. I tried:

set logging on
if (l_num >= 10 && l_num <= 20)
 >break A.cpp: 28
 >commands
  >print a_r
  >print a_t
  >print p1
  >print q1
  >end
 >break A.cpp: 42
 >commands
  >print p2
  >print a_s
  >end
 >break A.cpp: 64
 >commands
  >print p3
  >print q3
  >end
 >end

But this threw an error: No symbol "l_num" in current context.

I have gdb version = 7.11.1, gcc version/ g++ version 5.4.0. I am compiling using cmake in Debug mode. I have the following setting to turn off optimization:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")

Then in the terminal, I compile using:

cmake -DCMAKE_BUILD_TYPE=Debug .

I am relatively new to GDB. Kindly guide on this problem.

Edit: It is not actually right to call l_num as loop variable. It's just a variable that is updated with every run of the loop. The loop actually is reading lines of a file and is exited when EOF reached.

skr
  • 914
  • 3
  • 18
  • 35

1 Answers1

0

This:

if (l_num >= 10 && l_num <= 20)
>break A.cpp: 28
...

will only work if you have l_num in current context (i.e. if you are in the frame where variable l_num exists).

It will also not do what you want: if l_num is between 10 and 20, it will set (multiple) breakpoints at A.cpp:28.

What you appear to want is to set one breakpoint on A.cpp:28, but only enable that breakpoint when l_num is between 10 and 20.

Here is an example of how to do somewhat similar thing, which can be adapted to your use case.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362