调试¶
本文更新于 2018.10.18
Linux + gdb¶
首先汇编的时候要添加调试信息:
$ nasm -f elf -g -F stabs asm_io.asm
$ nasm -f elf -g -F stabs loop.asm
$ gcc -m32 -o loop loop.o asm_io.o driver.c
gdb调试:
$gdb ./loop
(gdb) set disassembly-flavor intel
(gdb) b asm_main
Breakpoint 1 at 0x80484b4
(gdb) r
Starting program: /home/zzq/dev/storage/code/asm/./loop
Breakpoint 1, 0x080484b4 in asm_main ()
Missing separate debuginfos, use: debuginfo-install glibc-2.17-222.el7.i686
(gdb) l
1 ;
2 ; Using Linux and gcc:
3 ; nasm -f elf asm_io.asm
4 ; nasm -f elf loop.asm
5 ; gcc -m32 -o loop loop.o driver.c asm_io.o
6
7 %include "asm_io.inc"
8
9 ;
10 ; initialized data is put in the .data segment
(gdb) si
0x080484b5 in asm_main ()
(gdb) si
0x080484ba in asm_main ()
(gdb) l
11 ;
12 segment .data
13 ;
14 ; These labels refer to strings used for output
15 ;
16 outmsg db "Sum is ", 0
17
18 ;
19 ; uninitialized data is put in the .bss segment
20 ;
(gdb) display /i $pc
1: x/i $pc
=> 0x80484ba <asm_main+10>: mov ebx,0x0
(gdb) si
0x080484bf in loop_start ()
1: x/i $pc
=> 0x80484bf <loop_start>: add ebx,ecx
(gdb) si
0x080484c1 in loop_start ()
1: x/i $pc
=> 0x80484c1 <loop_start+2>: loop 0x80484bf <loop_start>
(gdb) info registers
eax 0x1 1
ecx 0xa 10
edx 0xffffd3d4 -11308
ebx 0xa 10
esp 0xffffd368 0xffffd368
ebp 0xffffd388 0xffffd388
esi 0x0 0
edi 0x0 0
eip 0x80484c1 0x80484c1 <loop_start+2>
eflags 0x206 [ PF IF ]
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x0 0
gs 0x63 99
(gdb) si
0x080484bf in loop_start ()
1: x/i $pc
=> 0x80484bf <loop_start>: add ebx,ecx
(gdb) info registers
eax 0x1 1
ecx 0x9 9
edx 0xffffd3d4 -11308
ebx 0xa 10
esp 0xffffd368 0xffffd368
ebp 0xffffd388 0xffffd388
esi 0x0 0
edi 0x0 0
eip 0x80484bf 0x80484bf <loop_start>
eflags 0x206 [ PF IF ]
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x0 0
gs 0x63 99
(gdb) p $eax
$1 = 1
(gdb) p $ebx
$2 = 10