GDB:显示某些数据的类型信息
gdb: show typeinfo of some data
基本上,我想得到typeid(*this).name()
,即真正的this
类型。
我想在 GDB 中获取它(不修改源代码(。我尝试了print typeid(*this)
但它说typeid
未知(因为我没有将其包含在源文件中(。
使用ptype
命令,如下所示:
(gdb) ptype 42
type = int
使用 ptype
、 whatis
和 explore
(我最喜欢的!
其中有一个名为 value
的变量,其定义为:
uint32_t value = 1234;
。以下所有工作:
-
ptype value
显示unsigned int
-
whatis value
显示uint32_t
-
explore value
(我最喜欢的!(显示:The value of 'value' is of type 'uint32_t' which is a typedef of type 'unsigned int' 'value' is a scalar value of type 'unsigned int'. value = 1234
例:
(gdb) ptype value
type = unsigned int
(gdb) ptype &value
type = unsigned int *
(gdb) whatis value
type = uint32_t
(gdb) explore value
The value of 'value' is of type 'uint32_t' which is a typedef of type 'unsigned int'
'value' is a scalar value of type 'unsigned int'.
value = 1234
感谢@o11c下面的评论指出whatis
命令的存在。
我通过在 gdb 中运行help all
发现了explore
命令。请参阅我在下面的"参考"部分中的评论。
自己试试:
# download the file "type_punning.c"
wget https://raw.githubusercontent.com/Generalsimus/eRCaGuy_hello_world/master/c/type_punning.c
# build it with optimization OFF (`-O0`) and debugging symbols ON (`-ggdb`),
# and output all intermediary files (`-save-temps=obj`), and run it in the
# gdb debugger (`gdb bin/type_punning`)
mkdir -p bin && gcc -Wall -Wextra -Werror -O0 -ggdb -std=c11 -save-temps=obj
type_punning.c -o bin/type_punning && gdb bin/type_punning
现在,在运行gdb
的情况下,执行以下操作:
# set breakpoint to a line just after `u.value = 1234;`
b type_punning.c:52
# run to that point
r
# Now run these various commands to see what type `u.value` is:
ptype u.value
whatis u.value
explore u.value
完整的示例命令和输出:
eRCaGuy_hello_world/c$ mkdir -p bin && gcc -Wall -Wextra -Werror -O0 -ggdb -std=c11 -save-temps=obj type_punning.c -o bin/type_punning && gdb bin/type_punning GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1 Copyright (C) 2018 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 "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from bin/type_punning...done. (gdb) b type_punning.c:52 Breakpoint 1 at 0x70b: file type_punning.c, line 52. (gdb) r Starting program: /home/gabriel/GS/dev/eRCaGuy_hello_world/c/bin/type_punning Type punning and ptr-based serialization demo TECHNIQUE 1: union-based type punning: Breakpoint 1, main () at type_punning.c:53 53 printf("1st byte = 0x%02Xn", (u.bytes)[0]); (gdb) ptype u.value type = unsigned int (gdb) whatis u.value type = uint32_t (gdb) explore u.value The value of 'u.value' is of type 'uint32_t' which is a typedef of type 'unsigned int' 'u.value' is a scalar value of type 'unsigned int'. u.value = 1234 (gdb)
我的旧/原始答案
正如@Star Brilliant在这里所说,这个:
ptype my_var
返回类似 type = unsigned short
的东西,但我希望它返回 type = uint16_t
而不是,这样我在检查内存时可以真正知道它有多少字节。为了获得这种效果,我能想到的最好的事情就是:
print &my_var
它打印(uint16_t *) 0x7ffffffefc2c
,从而显示它的指针类型是uint16_t*
,这意味着它的类型是uint16_t
。
我发现这比ptype my_var
更有用,但是如果您有任何建议,则需要一种更直接的方式来获得这种效果。
示例 gdb 命令和输出:
(gdb) ptype my_var
type = unsigned short
(gdb) print &my_var
$27 = (uint16_t *) 0x7ffffffefc2c
再次,注意ptype my_var
揭示它是一个unsigned short
,而print &my_var
揭示了更详细和期望的答案,即它是一个uint16_t
。
引用:
- @o11c下面的评论
- @Star辉煌的答案
-
help all
- 我在运行gdb
时使用此命令,将输出全部复制粘贴到文本编辑器中,并搜索"type"以发现explore
命令。
另请参阅:
- 我的答案是如何在 GDB 中像数组一样查看指针?
- 我关于如何在 GDB 中使用
printf
以便围绕变量输出编写自定义描述的答案 - [我的回答。 "GDB"调试器奇怪地跳过断点
- [我的问答]编译器的"-O0"选项和"-Og"选项有什么区别?
- [我的问答] https://askubuntu.com/questions/1349047/where-do-i-find-core-dump-files-and-how-do-i-view-and-analyze-the-backtrace-st
中查看变量类型和值;如何构建和编译 gdb 调试符号
ptype [ARG]
命令将打印类型。
这个问题可能与此相关: 使用 gdb 的多态类C++中的 vtable:
(gdb) help set print object
Set printing of object's derived type based on vtable info.
它不完全是typeid((,但它应该在检查多态指针时显示真实的对象类型(例如 this
在基类中(。当然仅适用于具有 vtable(即至少一个虚拟方法(的类,但typeid
也是如此。
- Keil armcc 生成类型信息名称,即使使用 --no_rtti_data
- 错误:未定义对"静脉类型信息::电池访问"的引用
- 如何在编译过程中输出C 类型信息
- 如何在C 中检查确切的类型信息(具有CV-REF-POIRT特征)
- 共享库中非模板基的模板子类导致未定义的符号类型信息'class'链接错误
- 为什么C++不使用集中存储类型信息以实现高效的 RTTI
- 给定DDS主题名称,可以在运行时确定主题类型信息
- 使用友元函数从多态类中检索类型信息
- 如何在函数模板中隐式推导数组中元素的类型信息
- 使用标准类型的动态类型信息实例化标识符
- 超载功能没有上下文类型信息
- 对"vtable for "命名空间继承的未定义引用 对"类型信息"的未定义引用
- 多映射插入键类型信息 std::make_pair vs std::p air 构造函数
- Visual Studio-使用PDB进行调试,但缺少类型信息
- 如何在不同的上下文(例如线程)中保留原始异常类型信息
- C++正在传输运行时条件类型信息
- C++虚拟表上的虚拟定义器和类的类型信息
- C++测试宏定义的类型信息值
- 编译器不可知的运行时类型信息
- C++类型信息