Invictus
Invictus

Reputation: 4328

setting Watch in GDB not working for me

I am facing problem While using watch in gdb . I am trying to keep a watch on variable m in my code . But for some reason i am getting the following message no symbol m in current context. I have kept a break point at line 7 so that scope of m is known .

    steps performed by me :-
    1>g++ -g a.cpp
    2>gdb a.out
    3>(gdb)break 7
    4>(gdb)watch m

Below is my program :-

    # include<iostream>
    # include<stdio.h>
    using namespace std;

    int main()
    {

      int m=10;
      char *abc = (char *)"ritesh";
      cout << abc << endl ;
      m=11; 
      m=13;
      abc=NULL;
      cout << *abc <<endl;

     return 0;
    }

I have also seen How can I use "watch" GDB? But it did not help me much . Can someone explain this problem i am facing.Below are information related to my GNU

    ritesh@ubuntu:~$ gdb a.out 
    GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i686-linux-gnu".
    For bug reporting instructions, please see:
    <http://bugs.launchpad.net/gdb-linaro/>...
    Reading symbols from /home/ritesh/a.out...done.

Upvotes: 1

Views: 1551

Answers (1)

pwes
pwes

Reputation: 2040

When you load your program into a debugger it is not being run yet. However, you try to watch a symbol, which will start to "live" in a function -- main() function -- and will "disappear" when you return from the function.

For example, in this code

void func() {
  int b = 1;
  ++b;
  cout << b << endl;
}

int main() {
  int a = 1;
  func();
  cout << a << endl;
}

you can't set a watch of the value of a before start executing a program, and a watch on the value of b until the execution enters func().

Upvotes: 2

Related Questions