2012-04-19 51 views
1

我面臨問題在gdb中使用watch。我正試圖在我的代碼中保留一個 監視變量m。但由於某種原因,我收到以下消息 no symbol m in current context。我在第7行保留了一個斷點,以便m的範圍是已知的。設置觀看GDB不工作對我

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

下面是我的程序: -

# 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; 
    } 

我也看到How can I use "watch" GDB?但它並沒有幫助我很多。有人可以解釋這個問題我facing.Below是當你加載程序到它沒有被尚未運行調試器與我的GNU

[email protected]:~$ 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. 

回答

2

信息。但是,您嘗試觀看一個符號,該符號將開始「生活」在一個函數中 - 並且在您從該函數返回時將「消失」。

例如,在這個代碼

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

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

,直到執行進入func()不能設置的a開始之前執行的程序價值的手錶,並就b價值的手錶。

+0

所以我應該運行它像(mdb)運行,我應該什麼時候執行它? – Invictus 2012-04-19 18:54:47

+0

是的,1)「break」7; 2)'run'(調試器停在main中); 3)'看m'; 4)'cont'(調試器將在'm = 11'後停止) – pwes 2012-04-19 18:56:16

+0

完成我在break語句之後運行程序並保存變量的值班,並且非常感謝 – Invictus 2012-04-19 18:57:02