GDB 在进入 C 函数时显示不正确的函数参数

GDB showing incorrect function arguments on entry to C function

使用 GDB 调试 C 程序我看到以下内容(缩写):

(gdb)
1808            found = readOptionFromFile(options_file,...
(gdb) p options_file
 = (FILE *) 0xa812140
(gdb) s
readOptionFromFile (file=0x0, ...) at options.c:376
376 bool readOptionFromFile(FILE *file, ... {
(gdb) p file
 = (FILE *) 0x0
(gdb) n
378         int len, argc, i, c, isActiveSection, isActivePass, passn=0;
(gdb) p file
 = (FILE *) 0xa812140
(gdb)

请注意,在进入函数后,GDB 会显示参数,尤其是形式参数 file,如 0x0,这不是我所期望的,因为实际参数 options_file 在拨打电话之前被打印为 0xa812140。即使此时打印它也会显示这个不正确的值。但是,在 next 之后,它的后续 p 再次显示值 0xa812140。

我可以从技术角度(函数序言、堆栈框架等)理解这一点,但作为用户,我真的想要 GDB 的正确输出。这是 known/expected 行为还是错误?

我在

上使用 GNU gdb (Ubuntu 8.3-0ubuntu1) 8.3
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=19.10
DISTRIB_CODENAME=eoan
DISTRIB_DESCRIPTION="Ubuntu 19.10"

(实际上在 WSL 上)

尽管 GCC manual 明确表示:

-O0
    Reduce compilation time and make debugging produce the expected results.
    This is the default.

当然,您可以就 "expected results" 部分进行辩论...

但它还继续说明

-Og
    Optimize debugging experience. -Og should be the optimization level of choice
    for the standard edit-compile-debug cycle, offering a reasonable level of
    optimization while maintaining fast compilation and a good debugging experience. 
    It is a better choice than -O0 for producing debuggable code because some
    compiler passes that collect debug information are disabled at -O0.

使用 -Og 应该可以解决问题。

更新:我发现调试的最佳选择实际上通常是 -O0,因为 -Og 允许优化许多变量,因为 2016 年的 as-if rule. There is an issue about this