В настоящее время я отлаживаю программу (использующую lldb), которая использует статическую переменную для включения или выключения регистрации отладки.Как ни странно, некоторые сообщения журнала не отображаются (в то время как другие).
Я устанавливаю аппаратную точку останова на адресе переменной , когда она изначально установлена: idevicedebug.c # L232 , debug.c # L46
Однако, если я войду в одну из неработающих функций ведения журнала, аппаратная точка останова не только не будетХит, если я печатаю адрес статической переменной, это совершенно другое.
Вызывается здесь, например: idevicedebug.c # L322 , адрес debug_level
отличается от того, который был в internal_set_debug_level
изначально (и теперь значение равно 0 вместо 1): debug.c # L87
Не понимаю ли я, как работают статические переменные?Может ли двоичный файл быть как-то связан с двумя версиями одного и того же кода?Я не уверен, как отлаживать это дальше.
РЕДАКТИРОВАТЬ: Вот краткое изложение рассматриваемого кода (отредактировано для краткости) утилита CLI для отправки пакетных команд GDB на устройство iOS через USB-соединение:
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 output:
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