1

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.
Community
  • 1
  • 1
Invictus
  • 4,028
  • 10
  • 50
  • 80

1 Answers1

2

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().

pwes
  • 2,040
  • 21
  • 30
  • So should i run it like (mdb)run and when should i perform it ? – Invictus Apr 19 '12 at 18:54
  • yes, 1) `break` 7; 2) `run` (debugger stops in main); 3) `watch m`; 4) `cont` (debugger will stop after `m=11`) – pwes Apr 19 '12 at 18:56
  • Done i run the program after break statement and the kept the watch for the variable and it was successful thanks a lot – Invictus Apr 19 '12 at 18:57