基本語法
調(diào)試C程序
-
一段C代碼
#include <stdio.h>
// 對參數(shù) a 和 b 求和
int sum(int a, int b) {
int s = a + b;
return s;
}
// main函數(shù):程序入口
int main(int argc, char *argv[]) {
int n = sum(1, 2); // 調(diào)用sum函數(shù)對求和
printf("n: %d\n", n); //在屏幕輸出 n 的值
return 0;
}
-
在gdb中我們通過disass main反匯編main函數(shù)
[xxx@xxx xxx]$ gdb ./gdb_usecase
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-119.el7
(gdb) disass main
Dump of assembler code for function main:
0x0000000000400547 <+0>: push %rbp
0x0000000000400548 <+1>: mov %rsp,%rbp
0x000000000040054b <+4>: sub $0x20,%rsp
0x000000000040054f <+8>: mov %edi,-0x14(%rbp)
0x0000000000400552 <+11>: mov %rsi,-0x20(%rbp)
0x0000000000400556 <+15>: mov $0x2,%esi
0x000000000040055b <+20>: mov $0x1,%edi
0x0000000000400560 <+25>: callq 0x40052d <sum>
0x0000000000400565 <+30>: mov %eax,-0x4(%rbp)
0x0000000000400568 <+33>: mov -0x4(%rbp),%eax
0x000000000040056b <+36>: mov %eax,%esi
0x000000000040056d <+38>: mov $0x400620,%edi
0x0000000000400572 <+43>: mov $0x0,%eax
0x0000000000400577 <+48>: callq 0x400410 <printf@plt>
0x000000000040057c <+53>: mov $0x0,%eax
0x0000000000400581 <+58>: leaveq
0x0000000000400582 <+59>: retq
End of assembler dump.
調(diào)試Go程序
-
一段Go代碼
package main
//計(jì)算a, b的平方和
func sum(a, b int) int {
a2 := a * a
b2 := b * b
c := a2 + b2
return c
}
func main() {
sum(1, 2)
}
-
Gdb調(diào)試Go可執(zhí)行程序
# 關(guān)閉內(nèi)聯(lián)優(yōu)化
go build -gcflags '-N -l' main.go
# 刪除調(diào)試符號(hào)
# -s: 去掉符號(hào)信息依痊。
# -w: 去掉DWARF調(diào)試信息。
go build -ldflags '-s -w'
[xxx@xxx xxx]$ go build -gcflags '-N -l' main.go
[xxx@xxx xxx]$ gdb main
(gdb) disass main.main
No symbol "main" in current context.
(gdb) l main.main
7 c := a2 + b2
8
9 return c
10 }
11
12 func main() {
13 sum(1, 2)
14 }
(gdb) disass main.main
Dump of assembler code for function main.main:
0x000000000044f990 <+0>: mov %fs:0xfffffffffffffff8,%rcx
0x000000000044f999 <+9>: cmp 0x10(%rcx),%rsp
0x000000000044f99d <+13>: jbe 0x44f9cd <main.main+61>
0x000000000044f99f <+15>: sub $0x20,%rsp
0x000000000044f9a3 <+19>: mov %rbp,0x18(%rsp)
0x000000000044f9a8 <+24>: lea 0x18(%rsp),%rbp
0x000000000044f9ad <+29>: movq $0x1,(%rsp)
0x000000000044f9b5 <+37>: movq $0x2,0x8(%rsp)
0x000000000044f9be <+46>: callq 0x44f930 <main.sum>
0x000000000044f9c3 <+51>: mov 0x18(%rsp),%rbp
0x000000000044f9c8 <+56>: add $0x20,%rsp
0x000000000044f9cc <+60>: retq
0x000000000044f9cd <+61>: callq 0x447840 <runtime.morestack_noctxt>
0x000000000044f9d2 <+66>: jmp 0x44f990 <main.main>
End of assembler dump.
調(diào)試C 和 Go的差異
C使用 disass main
Go使用 disass main.main