gdb:print 无法识别具有非标准名称或字符的变量?

标签 gdb

我正在使用 LLVM 编写一个 BASIC 编译器,一切正常。我正在尝试调试工作,一切顺利,但我在变量名和 GDB 的打印语句方面遇到了一个奇怪的问题。

为不熟悉的人介绍一些 BASIC 背景知识:

In BASIC, variable names use a symbol on the end to determine their type. So a variable x% would be integer, and variable x$ would be a string, and both can co-exist.

The statement DEFINT A-Z means that any variable starting with any letter from A to Z will be type INT, even without the trailing %. But, internally, my compiler will store the name mangled with the trailing % to avoid overlap with another variable with a trailing $.

这是我的小测试程序:

defint a-z

x = 5
y = 100
z = x + y
if x = 5 then
  print "z=";z
else
  print "z!=";z
end if

所以'x'实际上在内部存储并作为x%放入符号表中。

好的,所以我将编译后的 EXE 加载到 GDB 中......

D:\dev\BasicParser>gdb t.exe
GNU gdb (GDB) 7.4
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from D:\dev\BasicParser\t.exe...done.
(gdb) b _jbc_main
Breakpoint 1 at 0x401000: file t.bas, line 1.
(gdb) r
Starting program: D:\dev\BasicParser\t.exe
[New Thread 1532.0x25b8]

Breakpoint 1, _jbc_main () at t.bas:1
1       defint a-z
(gdb) n
4       x = 5
(gdb) info address x%
Symbol "x%" is static storage at address 0x4263c4.

此时,您可以看到 GDB 识别了符号 x%,因为它识别并显示了它的地址。但是下面的“打印”失败了:

(gdb) print x%
No symbol "x" in current context.

嗯……这很有趣……它去掉了 % 符号。

但我可以查看存储 x% 的内存位置,这看起来是正确的:

(gdb) x 0x4263c4
0x4263c4 <x%>:  0x00000000

如果我显示所有符号,我会按预期得到:

(gdb) info variables
All defined variables:

File t.bas:
static i32 x%;
static i32 y%;
static i32 z%;

继续,以证明 x% 确实被代码修改了:

(gdb) n
5       y = 100
(gdb) print x%
No symbol "x" in current context.
(gdb) x 0x4263c4
0x4263c4 <x%>:  0x00000005
(gdb)

如您所见,GDB 认为 x% 所在的内存肯定已更新。

出于某种原因,“打印”命令不喜欢 % 符号。

这是此模块的“信息源”结果,您可以看到我没有将 C 指定为语言:

(gdb) info source
Current source file is t.bas
Compilation directory is D:\dev\BasicParser
Located in D:\dev\BasicParser\t.bas
Contains 18 lines.
Source language is minimal.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.

有什么解决方法吗?我一直在搜索和搜索,但找不到解决方案。

谢谢!

最佳答案

gdb 只是将您的命令解释为模运算。这不是 gdb 中的错误。

请看,info source 为您的文件显示了 minimal。这是来自 gdb 的文档:http://sourceware.org/gdb/current/onlinedocs/gdb/Unsupported-Languages.html#Unsupported-Languages

In addition to the other fully-supported programming languages, GDB also provides a pseudo-language, called minimal. It does not represent a real programming language, but provides a set of capabilities close to what the C or assembly languages provide.

那么,调试 session 中的这个警告是什么意思?

(gdb) print x%
No symbol "x" in current context.

由于 minimal 是一组接近 C 的功能,因此 gdb 必须将其解释为试图获得除法的余数 (http://en.wikipedia.org/wiki/Modulo_operation) - 根据 C 编程语言规则.所以 x 是左边的 arg,% 是操作,右边的 arg 是缺失的。

让我举个例子。这是一个测试 C++ 程序,显示了最小 调试 session :

D:\src-c++\tests\test.vars>cat minimal.cpp

int main()
{
  int k;
  k = 1;
  k = 2;
  return 0;
}

这是一个调试 session :

D:\src-c++\tests\test.vars>gdb -q minimal.exe
Reading symbols from D:\src-c++\tests\test.vars\minimal.exe...done.
(gdb) start
Temporary breakpoint 1 at 0x4016be: file minimal.cpp, line 5.
Starting program: D:\src-c++\tests\test.vars/minimal.exe
[New Thread 2872.0x8c0]

Temporary breakpoint 1, main () at minimal.cpp:5
5         k = 1;
(gdb) show language
The current source language is "auto; currently c++".
(gdb) set language minimal
Warning: the current language does not match this frame.
(gdb) show language
The current source language is "minimal".
Warning: the current language does not match this frame.
(gdb) n
6         k = 2;
(gdb) print k
$1 = 1
(gdb) print k%
A syntax error in expression, near `'.
(gdb) print kk%
No symbol "kk" in current context.

看——当前上下文中没有符号“kk”,与您遇到的错误相同。

关于gdb:print 无法识别具有非标准名称或字符的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23840553/

相关文章:

gdb - 在 GDB 中打印 `errno` 名称而不是值

string - assembly GDB 打印字符串

android - 从eclipse远程调试Android NDK程序

使用 CLion 的 GDB 远程调试不起作用

c - 数组中的文件指针

debugging - 使用 GDB 在 Fortran 中调试 MPI 程序(在 MAC 上)

python - C+Python 和核心转储

c - GDB monitor inside TMUX乱码

linux - GDB 使用 clone() 调试应用程序

c - 即使加载了调试符号,GDB 进入共享库也会显示 "no such file"