【问题标题】:Crazy idea? Program itself tells GDB to watch variable for changes疯狂的想法?程序本身告诉 GDB 观察变量的变化
【发布时间】:2011-05-18 23:08:56
【问题描述】:

我为什么需要这个? 数据的位置不断变化,因为输入数据变化太大,所以除了打印它,休眠 30 秒,以便我可以手动将其输入到 GDB,然后继续程序,让程序告诉 GDB 在哪里可能很有用观看。 但这样的事情可能吗?

【问题讨论】:

    标签: debugging gdb watch


    【解决方案1】:

    你可以靠近;为简单起见假设 C/C++ 语言

    定义一个函数,返回对要跟踪的数据的引用:

     // debug.h
     extern "C" mydatastruct* GetDatumForDebug();
    
     // debug.cpp
     mydatastruct* GetDatumForDebug()
     {
         if (s_initialized)
              return &some_complicated_address_lookup_perhaps_in_Cpp_or_java_orwhatever();
         return (mydatastruct*) 0;
     }
    

    你可以随后只是

     (gdb) display GetDatumForDebug()
    

    甚至

     (gdb) display GetDatumForDebug()->s
    

    我认为可以在您的调试手表中使用 GetDatumForDebug() 的结果,我不确定您是做什么/如何做到的 :)


    这是一个工作示例,为了提高速度,塞进了单个源 (test.cpp):使用 g++ -g test.cpp -o test 编译:

    static bool s_initialized = false;
    struct mydatastruct { const char* s; };
    static mydatastruct& some_complicated_address_lookup_perhaps_in_Cpp_or_java_orwhatever() 
    { 
        static mydatastruct s_instance = { "hello world" };
        s_initialized = true;
        return s_instance;
    }
    
    extern "C" mydatastruct* GetDatumForDebug();
    
    // debug.cpp
    mydatastruct* GetDatumForDebug()
    {
        if (s_initialized)
            return &some_complicated_address_lookup_perhaps_in_Cpp_or_java_orwhatever();
        return (mydatastruct*) 0;
    }
    
    int main()
    {
        // force initialize for demo purpose:
        some_complicated_address_lookup_perhaps_in_Cpp_or_java_orwhatever();
        return 42;
    }
    

    自动化 gdb 命令

    将以下内容附加到您工作目录中的.gdbinit

    break main
    run
    call some_complicated_address_lookup_perhaps_in_Cpp_or_java_orwhatever()
    display GetDatumForDebug()? GetDatumForDebug()->s : ""
    

    这将在该目录中启动 gdb 时自动执行这些命令

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多