1

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

4

1 回答 1

0

你可以靠近;为简单起见假设 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 时自动执行这些命令

于 2011-05-18T14:53:56.893 回答