【问题标题】:setting Watch in GDB not working for me在 GDB 中设置 Watch 对我不起作用
【发布时间】:2012-04-20 02:50:54
【问题描述】:

在 gdb 中使用 watch 时遇到问题。我试图保持一个 在我的代码中观察变量m。但由于某种原因,我收到以下消息 no symbol m in current context。我在第 7 行保留了一个断点,以便知道 m 的范围。

    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?,但对我帮助不大。谁能解释我面临的这个问题。以下是与我的 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.

【问题讨论】:

    标签: gdb watch


    【解决方案1】:

    当您将程序加载到调试器中时,它还没有运行。但是,您尝试观察一个符号,该符号将开始“存在”在函数中——main() 函数——并在您从函数返回时“消失”。

    例如,在这段代码中

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

    您不能在开始执行程序之前设置a 的值的监视,并且在执行进入func() 之前监视b 的值。

    【讨论】:

    • 那么我应该像 (mdb)run 一样运行它吗?我应该什么时候执行它?
    • 是的,1) break 7; 2)run(调试器在 main 中停止); 3)watch m; 4)cont(调试器将在m=11之后停止)
    • 完成后我在 break 语句后运行程序并保持监视变量,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 2015-04-14
    • 1970-01-01
    • 2016-04-09
    • 2018-08-23
    相关资源
    最近更新 更多