(gdb) r
Starting program: /home/bankde/Desktop/tmp/example.o
Put string to split: helloworld
Breakpoint 1, split (input=0x7fffffffdf40 "helloworld\n") at example.c:7
7 char** result = malloc(10*sizeof(char*));
(gdb) p input
$4 = 0x7fffffffdf40 "helloworld\n"
(gdb) set var input = "hello world"
(gdb) p input
$5 = 0x7ffff7fe1f20 "hello world"
(gdb) c
Continuing.
First str: Hello
Second str: World
จะเห็นว่า input ที่เราใส่เข้าไป helloworld แล้วปกติมันจะทำให้โปรแกรม crash ถูกเปลี่ยนเป็น hello world จนสามารถรันโปรแกรมได้ตามปกติได้ (ถ้า string อาจจะมีข้อจำกัดเรื่อง memory space นะครับ)
(gdb) b *0x00005555555549fb
Breakpoint 3 at 0x5555555549fb: file example.c, line 33.
(gdb) r
Starting program: /home/bankde/Desktop/tmp/example.o
Put string to split: hello world
First str: Hello
Second str: World
Breakpoint 3, getInputAndRun () at example.c:33
33 }
(gdb) set $eax = -1
(gdb) c
Continuing.
[Inferior 1 (process 20460) exited normally]
(gdb) break example.c:27
Breakpoint 1 at 0x976: file example.c, line 27.
(gdb) r
Starting program: /home/bankde/Desktop/tmp/example.o
Put string to split: hello world
Breakpoint 1, getInputAndRun () at example.c:27
27 *result[0] = toupper(result[0][0]);
(gdb) jump example.c:29
Continuing at 0x5555555549bc.
First str: hello
Second str: world
Run function
เราสามารถสั่ง function อะไรก็ได้ที่เราต้องการได้ระหว่าง GDB อยู่ครับ (แต่ function ต้องถูกอ่าน define มาก่อนแล้ว) อย่างตัวอย่างผมอยู่ที่ main ยังไม่ทันรับ input จริงๆ ผมก็สั่ง split เพื่อดูผลลัพธ์ได้เลย
Breakpoint 1, main () at example.c:37
37 if (getInputAndRun() == -1) break;
(gdb) call split("test bank")
$1 = (char **) 0x555555756260
(gdb) p ((char**)0x555555756260)[0]
$2 = 0x7ffff7fe1f20 "test"
(gdb) p ((char**)0x555555756260)[1]
$3 = 0x7ffff7fe1f25 "bank"
หรือใช้ p แทน call ก็ได้ครับ แล้วเปลี่ยน memory address เป็นเลขตัวแปรแทนก็ได้เช่นกัน
(gdb) p split("hello bonk")
$4 = (char **) 0x5555557562c0
(gdb) p ((char**)$4)[0]
$7 = 0x7ffff7fe1f30 "hello"
(gdb) p ((char**)$4)[1]
$8 = 0x7ffff7fe1f36 "bonk"