簡體   English   中英

gdb 'x' 命令有什么作用?

[英]What does gdb 'x' command do?

我正在讀一關於黑客的,其中有一章關於匯編。

以下是我用 C 編寫的小程序。

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int i;

    for (i = 0; i < 10; i++) {
        puts("Hello World!");
    }

    return 0;
}

以下是gdb測試:

(gdb) break main
Breakpoint 1 at 0x40050f: file main.c, line 7.
(gdb) run
Breakpoint 1, main (argc=1, argv=0x7fffffffe708) at main.c:7
7       for (i = 0; i < 10; i++) {
(gdb) disassemble main
Dump of assembler code for function main:
   0x0000000000400500 <+0>: push   rbp
   0x0000000000400501 <+1>: mov    rbp,rsp
   0x0000000000400504 <+4>: sub    rsp,0x20
   0x0000000000400508 <+8>: mov    DWORD PTR [rbp-0x14],edi
   0x000000000040050b <+11>:    mov    QWORD PTR [rbp-0x20],rsi
=> 0x000000000040050f <+15>:    mov    DWORD PTR [rbp-0x4],0x0
   0x0000000000400516 <+22>:    jmp    0x400526 <main+38>
   0x0000000000400518 <+24>:    mov    edi,0x4005c4
   0x000000000040051d <+29>:    call   0x4003e0 <puts@plt>
   0x0000000000400522 <+34>:    add    DWORD PTR [rbp-0x4],0x1
   0x0000000000400526 <+38>:    cmp    DWORD PTR [rbp-0x4],0x9
   0x000000000040052a <+42>:    jle    0x400518 <main+24>
   0x000000000040052c <+44>:    mov    eax,0x0
---Type <return> to continue, or q <return> to quit---
   0x0000000000400531 <+49>:    leave  
   0x0000000000400532 <+50>:    ret    
End of assembler dump.

下面是我不明白的部分。 請注意 $rip 是“指令指針”,指向0x000000000040050f <+15>

(gdb) x/x $rip
0x40050f <main+15>: 0x00fc45c7
(gdb) x/12x $rip
0x40050f <main+15>: 0x00fc45c7  0xeb000000  0x05c4bf0e  0xbee80040
0x40051f <main+31>: 0x83fffffe  0x8301fc45  0x7e09fc7d  0x0000b8ec
0x40052f <main+47>: 0xc3c90000  0x1f0f2e66  0x00000084  0x1f0f0000
(gdb) x/8xb $rip
0x40050f <main+15>: 0xc7    0x45    0xfc    0x00    0x00    0x00    0x00    0xeb
(gdb) x/8xh $rip
0x40050f <main+15>: 0x45c7  0x00fc  0x0000  0xeb00  0xbf0e  0x05c4  0x0040  0xbee8
(gdb) x/8xw $rip
0x40050f <main+15>: 0x00fc45c7  0xeb000000  0x05c4bf0e  0xbee80040
0x40051f <main+31>: 0x83fffffe  0x8301fc45  0x7e09fc7d  0x0000b8ec

第一個命令x/x $rip輸出0x40050f <main+15>: 0x00fc45c7

是 0x40050f 處的指令嗎? 0x00fc45c7是否與mov DWORD PTR [rbp-0x4],0x0 (0x40050f 處的匯編指令)相同?

其次,如果是指令,那么命令x/12x $rip , x/8xw $rip , x/8xh $rip的輸出中的那些十六進制數是多少?

至於(1),你猜對了。

對於 (2), x 命令最多有 3 個說明符:要打印的對象數量; 以哪種格式; 和什么對象大小。 在所有示例中,您選擇打印為十六進制 (x)。 對於第一個說明符,您要求打印 12、8、8 個對象。

至於您的案例中的最后一個說明符:
x/12x 沒有,所以 gdb 默認假設你想要 4 字節的塊(GDB 稱之為“字”,x86 稱之為“雙字”)。 通常,我總是指定您想要什么,而不是退回到默認設置。

x/8xw 對 8 個對象執行相同的操作,正如您現在明確請求的 dwords。

x命令默認為您使用的最后一個大小,但啟動時的初始默認值是w字)


x/8xh 請求 2 個字節的半字大小的塊,因此以 2 個字節的塊打印對象。 (半字相對於 GDB 的標准 32 位字長;x86 稱之為“字”)。

如果您想知道為什么兩個相鄰值的串聯與您在 dwords 中打印時報告的不相等,這是因為 x86 是小端架構。 這意味着什么在埃里克森的書中再次詳細地描述了——如果你向前看幾頁,他做了一些你可能會發現有用的計算。 簡而言之,如果你重新組合它們 (2,1) (4,3), ...,你會看到它們匹配。

(gdb) help x
Examine memory: x/FMT ADDRESS.
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),
  t(binary), f(float), a(address), i(instruction), c(char) and s(string),
  T(OSType), A(floating point values in hex).
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format.

Defaults for format and size letters are those previously used.
Default count is 1.  Default address is following last thing printed
with this command or "print".

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM