GDB检查数据显示格式从连续8个字节到4个字节

问题描述 投票:0回答:3

这是我的

gdb

的展示
(gdb) x/20bx 0xbffff2c0
0xbffff2c0: 0xd4    0xf2    0xff    0xbf    0x16    0x8f    0x04    0x08
0xbffff2c8: 0x05    0x00    0x00    0x00    0x00    0x00    0x0c    0x42
0xbffff2d0: 0x6b    0x00    0x00    0x00

可以改成连续4个字节吗?

gdb
3个回答
7
投票

gdb
(至少在我查看的 7.1 和 7.6 源代码中)根据格式硬连接
x
将打印的每行最大元素数。

maxelts = 8;
if (size == 'w')
  maxelts = 4;
if (size == 'g')
  maxelts = 2;
if (format == 's' || format == 'i')
  maxelts = 1;

获得所需内容的解决方法是键入

x/4bx 0xbffff2c0
打印 4 个元素,然后仅键入 enter 打印每组连续的 4 个元素。


4
投票

使用

x/20wx

(gdb) x/20bx &result
0x7fff5fbff5f4: 0xff    0x7f    0x00    0x00    0x5e    0x10    0xc0    0x5f
0x7fff5fbff5fc: 0xff    0x7f    0x00    0x00    0x10    0xf6    0xbf    0x5f
0x7fff5fbff604: 0xff    0x7f    0x00    0x00

(gdb) x/20wx &result
0x7fff5fbff5f4: 0x00007fff  0x5fc0105e  0x00007fff  0x5fbff610
0x7fff5fbff604: 0x00007fff  0x8994d5fd  0x00007fff  0x00000000
0x7fff5fbff614: 0x00000000  0x00000001  0x00000000  0x5fbff7e8
0x7fff5fbff624: 0x00007fff  0x00000000  0x00000000  0x5fbff804
0x7fff5fbff634: 0x00007fff  0x5fbff830  0x00007fff  0x5fbff847

0
投票

我想要相同的输出,但退出答案不会这样做。

我要求 GPT 给我一个基于 python 脚本的配置。有用。希望有帮助。

~/.gdbinit
中添加以下内容:

#----------------------------------------------------------------------
# => define xbytes command, to view memory bytes, one byte per line
# usage example:
# (gdb) xbytes 4 &a
# 0x7fffffffd848: 78
# 0x7fffffffd849: 56
# 0x7fffffffd84a: 34
# 0x7fffffffd84b: 12
#----------------------------------------------------------------------
python
import gdb

class XBytes(gdb.Command):
    """xbytes NUM_BYTES ADDRESS
    Display NUM_BYTES bytes starting at ADDRESS in blue color."""

    def __init__(self):
        super(XBytes, self).__init__("xbytes", gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL)

    def invoke(self, arg, from_tty):
        argv = gdb.string_to_argv(arg)
        if len(argv) != 2:
            raise gdb.GdbError("xbytes requires 2 arguments: NUM_BYTES and ADDRESS")

        num_bytes = int(argv[0])
        address = gdb.parse_and_eval(argv[1])
        address = int(address.cast(gdb.lookup_type('void').pointer()))

        # ANSI escape code for blue color and reset
        blue = '\033[34m'
        reset = '\033[0m'

        # Read and display the specified number of bytes
        for addr in range(address, address + num_bytes):
            byte = gdb.selected_inferior().read_memory(addr, 1)
            byte_value = int.from_bytes(byte, byteorder='little')
            # Print address in blue
            print(f"{blue}0x{addr:x}:{reset} 0x{byte_value:02x}")

# Register the command
XBytes()
end

然后调用它。让我们使用这个

test.cpp

#include <stdio.h>
int main()
{
    int a = 0x12345678;
    int b = 0x0001;
    int c = 0xabcdef;
    printf("a = %d\n", a);
    printf("b = %d\n", b);
    return 0;
}

运行:

g++ test.cpp -O0 -g
gdb ./a.out
b main
r
n
n
n
(gdb) xbytes 4 &a
0x7fffffffd844: 0x78
0x7fffffffd845: 0x56
0x7fffffffd846: 0x34
0x7fffffffd847: 0x12
(gdb) xbytes 4 &b
0x7fffffffd848: 0x01
0x7fffffffd849: 0x00
0x7fffffffd84a: 0x00
0x7fffffffd84b: 0x00
(gdb) xbytes 4 &c
0x7fffffffd84c: 0xef
0x7fffffffd84d: 0xcd
0x7fffffffd84e: 0xab
0x7fffffffd84f: 0x00
© www.soinside.com 2019 - 2024. All rights reserved.