My first gdb

Wanted to use gdb since I’m always using Windows debuggers at work. Good thing is that now MacOS runs on Intel CPU and I don’t need to look into X68000 or PowerPC.

Here is today’s test target.

 #include <stdio.h>
 bool hogecheck();
 void page();
 bool hogecheck(){
     return false;
 void page(){
 int main(int args, char* argv[])
     int i;
     if(false == hogecheck()){
         return 1;
     return 0;

And here is how I changed the result of ‘hogecheck’ and run ‘page’ function.

(gdb) b main
Breakpoint 1 at 0x100000e7f: file first.c, line 17.
(gdb) g
Undefined command: "g".  Try "help".
(gdb) r
(gdb) set disassembly-flavor intel
(gdb) disas
Dump of assembler code for function main:
0x0000000100000e70 :	push   rbp
0x0000000100000e71 :	mov    rbp,rsp
0x0000000100000e74 :	sub    rsp,0x20
0x0000000100000e78 :	mov    DWORD PTR [rbp-0x4],edi
0x0000000100000e7b :	mov    QWORD PTR [rbp-0x10],rsi
0x0000000100000e7f :	call   0x100000e30 <_Z9hogecheckv>
0x0000000100000e84 :	mov    cl,al
0x0000000100000e86 :	cmp    cl,0x0
0x0000000100000e89 :	setne  cl
0x0000000100000e8c :	xor    cl,0x1
0x0000000100000e8f :	and    cl,0x1
0x0000000100000e92 :	mov    BYTE PTR [rbp-0x19],cl
0x0000000100000e95 :	mov    cl,BYTE PTR [rbp-0x19]
0x0000000100000e98 :	cmp    cl,0x0
0x0000000100000e9b :	je     0x100000ea6 
0x0000000100000e9d :	mov    DWORD PTR [rbp-0x18],0x1
0x0000000100000ea4 :	jmp    0x100000eb2 
0x0000000100000ea6 :	call   0x100000e50 <_Z4pagev>
0x0000000100000eab :	mov    DWORD PTR [rbp-0x18],0x0
0x0000000100000eb2 :	mov    eax,DWORD PTR [rbp-0x18]
0x0000000100000eb5 :	mov    DWORD PTR [rbp-0x14],eax
0x0000000100000eb8 :	mov    eax,DWORD PTR [rbp-0x14]
0x0000000100000ebb :	add    rsp,0x20
0x0000000100000ebf :	pop    rbp
0x0000000100000ec0 :	ret    

(gdb) info r
rax            0x100000e70	4294970992
rbx            0x0	0
rip            0x100000e7f	0x100000e7f  <-- Stopped one step before

(gdb) n

Breakpoint 2, 0x0000000100000e84 in main (args=1, argv=0x7fff5fbff9e0) at first.c:17
17		if(false == hogecheck()){

(gdb) info r
rax            0x0	0
rbx            0x0	0
rip            0x100000e84	0x100000e84 

(gdb) set $rax=1
(gdb) i r 
rax            0x1	1 <-- set
rbx            0x0	0

(gdb) c
page <-- displayed
Program exited normally.

Windbg version is here.

0:000> X first!*main*
00000000`00d03014 first!__native_dllmain_reason = 0xffffffff
00000000`00d03018 first!mainret = 0n0
00000000`00d01000 first!wmain (int, wchar_t **)
00000000`00d01119 first!__tmainCRTStartup (void)
00000000`00d0127e first!wmainCRTStartup (void)
00000000`00d02084 first!_imp____wgetmainargs = <no type information>
0:000> bp first!wmain
0:000> bl
 0 e x86 00000000`01131470     0001 (0001)  0:**** first!wmain
0:000:x86> g
Breakpoint 0 hit
01131470 55              push    ebp
0:000:x86> u eip eip+100
first!wmain [c:\users\sokoide\projects\spike\first\first.cpp @ 19]:
01131470 55              push    ebp
01131471 8bec            mov     ebp,esp
01131473 81eccc000000    sub     esp,0CCh
01131479 53              push    ebx
0113147a 56              push    esi
0113147b 57              push    edi
0113147c 8dbd34ffffff    lea     edi,[ebp-0CCh]
01131482 b933000000      mov     ecx,33h
01131487 b8cccccccc      mov     eax,0CCCCCCCCh
0113148c f3ab            rep stos dword ptr es:[edi]
0113148e e881fbffff      call    first!ILT+15(?hogecheckYA_NXZ) (01131014)
01131493 0fb6c0          movzx   eax,al
01131496 85c0            test    eax,eax
01131498 7507            jne     first!wmain+0x31 (011314a1)
0113149a b801000000      mov     eax,1
0113149f eb07            jmp     first!wmain+0x38 (011314a8)
011314a1 e8e1fbffff      call    first!ILT+130(?pageYAXXZ) (01131087)
011314a6 33c0            xor     eax,eax
011314a8 5f              pop     edi
011314a9 5e              pop     esi
011314aa 5b              pop     ebx
011314ab 81c4cc000000    add     esp,0CCh
011314b1 3bec            cmp     ebp,esp
011314b3 e897fcffff      call    first!ILT+330(__RTC_CheckEsp) (0113114f)
011314b8 8be5            mov     esp,ebp
011314ba 5d              pop     ebp
011314bb c3              ret
011314bc cc              int     3
0:000:x86> bp 01131496
0:000:x86> g
Breakpoint 1 hit
01131496 85c0            test    eax,eax
0:000:x86> r @eax=1
0:000:x86> r
eax=00000001 ebx=7f5fe000 ecx=00000000 edx=00000001 esi=00000000 edi=00b1fa4c
eip=01131496 esp=00b1f974 ebp=00b1fa4c iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
01131496 85c0            test    eax,eax
0:000:x86> g

-> 'page' displayed in the console.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.