c - 在此 C 程序中,静态变量在内存中的位置如何变化?

标签 c debugging lldb

我目前正在调试一个程序(使用 lldb),该程序使用静态变量来打开或关闭调试日志记录。奇怪的是,一些日志消息没有显示(而其他的则显示)。

我正在 address of the variable 上设置硬件断点初始设置时:idevicedebug.c#L232 , debug.c#L46

但是,如果我进入其中一个不工作的日志记录功能,不仅硬件断点没有命中,如果我打印静态变量的地址,情况就完全不同了。

这里调用例如:idevicedebug.c#L322debug_level 的地址与最初在 internal_set_debug_level 中的地址不同(现在值是 0 而不是 1):debug.c#L87

我是不是误解了静态变量的工作原理?二进制文件能否以某种方式链接到同一代码的两个版本?我不确定如何进一步调试。

编辑:以下是相关代码的摘要(为简洁起见进行了编辑),这是一个 CLI 实用程序,用于通过 USB 连接向 iOS 设备发送 GDB 数据包命令:

idevicedebug.c :

int main(int argc, char *argv[])
{
    /* ... */
    int i;
    int debug_level = 0;
    /* ... */


    /* parse command line arguments */
    for (i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
            debug_level++;
            idevice_set_debug_level(debug_level); // <- local debug_level is 1
            continue;
        }
    }

    /* ... */

    /* set maximum packet size */
    debug_info("Setting maximum packet size..."); // <- this does not print to stdout

    /* ... */

debug.h :

/* ... */

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && !defined(STRIP_DEBUG_CODE)
#define debug_info(...) debug_info_real (__func__, __FILE__, __LINE__, __VA_ARGS__)
#elif defined(__GNUC__) && __GNUC__ >= 3 && !defined(STRIP_DEBUG_CODE)
#define debug_info(...) debug_info_real (__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
#else
#define debug_info(...)
#endif

void debug_info_real(const char *func,
                                            const char *file,
                                            int line,
                                            const char *format, ...);

/* ... */

void internal_set_debug_level(int level);

#endif

debug.c :

/* ... */

static int debug_level;

void internal_set_debug_level(int level)
{
    debug_level = level; // <- static debug_level set to 1
}

/* ... */

void debug_info_real(const char *func, const char *file, int line, const char *format, ...)
{
#ifndef STRIP_DEBUG_CODE
    va_list args;
    char *buffer = NULL;

    if (!debug_level) // <- this is 0 despite being set previously
        return;       // breakpoint in internal_set_debug_level shows
                      // debug_level at 0x00000001000b75fc (== 1), yet
                      // here at 0x00000001000041b4 (== 0)

    /* run the real fprintf */
    va_start(args, format);
    (void)vasprintf(&buffer, format, args);
    va_end(args);

    debug_print_line(func, file, line, buffer);

    free(buffer);
#endif
}

lldb 输出:

Process 29615 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.2
    frame #0: 0x00000001000ae0c7 libimobiledevice.6.dylib`internal_set_debug_level(level=1) at debug.c:46
   43
   44   void internal_set_debug_level(int level)
   45   {
-> 46           debug_level = level;
   47   }
   48
Target 0: (idevicedebug) stopped.
(lldb) p debug_level
(int) $0 = 0
(lldb) p &debug_level
(int *) $1 = 0x00000001000b75fc
(lldb) watch set expression (int *)0x00000001000b75fc
Watchpoint created: Watchpoint 1: addr = 0x1000b75fc size = 8 state = enabled type = w
    new value: 0x0000000000000000
(lldb) n

Watchpoint 1 hit:
old value: 0x0000000000000000
new value: 0x0000000000000001
Process 29615 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 1
    frame #0: 0x00000001000ae0d0 libimobiledevice.6.dylib`internal_set_debug_level(level=1) at debug.c:47
   44   void internal_set_debug_level(int level)
   45   {
   46           debug_level = level;
-> 47   }
Target 0: (idevicedebug) stopped.
(lldb) c
Process 29615 resuming
Process 29615 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x0000000100001e58 idevicedebug`main(argc=4, argv=0x00007fff5fbfef50) at idevicedebug.c:359
   357
   358                          /* set maximum packet size */
-> 359                          debug_info("Setting maximum packet size...");
   360
Target 0: (idevicedebug) stopped.
(lldb) s
Process 29615 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step in
    frame #0: 0x0000000100002fff idevicedebug`debug_info_real(func="main", file="idevicedebug.c", line=359, format="Setting maximum packet size...") at debug.c:85
   82   {
   83   #ifndef STRIP_DEBUG_CODE
   84           va_list args;
-> 85           char *buffer = NULL;
   86
   87           if (!debug_level)
   88                   return;
Target 0: (idevicedebug) stopped.
(lldb) p debug_level
(int) $3 = 0
(lldb) p &debug_level
(int *) $4 = 0x00000001000041b4

最佳答案

您可以毫无疑问地理解静态变量的工作原理。

不确定,但显然问题出在链接阶段。可能是静态/动态链接的混合。根据您的调试器输出,您两次包含相同的代码:

  • libimobiledevice.la 包括 common/debug.c(通过 common/libinternalcommon.la)
  • idevicedebug 似乎静态包含它(也通过 libinternalcommon),并通过 libimobiledevice 动态包含它。

我会尝试从 tools/Makefile.am 中的 idevicedebug_LDFLAGS 中删除 common/libinternalcommon.la

autogen.sh 在我的机器上失败,所以我帮不上忙,但你明白了。

关于c - 在此 C 程序中,静态变量在内存中的位置如何变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52635772/

相关文章:

c - 使用 fscanf 读取 C 中的坐标时出现问题

c - C 模式下的 Emacs 注释区域

c - 使用 lldb 分析 .DSYM 文件

swift - lldb 错误 : Couldn't lookup symbols. Swift iOS

将字符串数组组件与字符进行比较

java - 找不到插入finally block 的位置以消除错误: Insert Finally to complete TryStatement

android - 如果我想调试应用程序,为什么是 "monitor will be closed to enable ADB integration"?

c# - 我在哪里可以找到 .net 4.0 SOS.dll?

xcode - 如何记录自定义 LLDB 命令(别名)?

c - 发送字符串数组 MPI C