Linux下GDB調試簡單示例


這里介紹對文件first.c的基本GDB調試操作,只有部分命令,只是一個示例,運行環境為裝有gcc編譯器和gdb調試器的Linux環境,基本GDB調試命令如下表:

命令                描述
backtrace(或bt)  查看各級函數調用及參數
finish            連續運行到當前函數返回為止,然后停下來等待命令
frame(或f)       幀編號 選擇棧幀
info(或i)        locals 查看當前棧幀局部變量的值
list(或l)        列出源代碼,接着上次的位置往下列,每次列10行
list 行號        列出從第幾行開始的源代碼
list 函數名        列出某個函數的源代碼
next(或n)        執行下一行語句
print(或p)    打印表達式的值,通過表達式可以修改變量的值或者調用函數
set var            修改變量的值
start            開始執行程序,停在main函數第一行語句前面等待命令
step(或s)        執行下一行語句,單步調試用這個
x                查看變量內存
b                后面加行號表示在該行打斷點
c                繼續運行到下一個斷點
r                運行
quit            退出調試
vi                后面加文件名可以打開文件用vim進行編輯

disass (/m) main 查看main函數的匯編

 

以下是示例的全過程:

[LLL@localhost LLL]$ cd test //到目標目錄
[LLL@localhost test]$ ls
first.c server.c sock.c
[LLL@localhost test]$ cat first.c //查看first.c內容
#include <stdio.h>

int add(int m, int n)
{
int result;
m = 10*m;
n = 100*n;
result = m+n;
return result;
}

int main()
{
int a = 10;
int b = 100;
int i = 0;
printf("%d",i);
i = add(a,b);
b = add(b,a);
printf("%d %d %d",a,b,i);
return 0;
}
[LLL@localhost test]$ gcc -g first.c -o first //gcc 編譯
[LLL@localhost test]$ ls
first first.c server.c sock.c
[LLL@localhost test]$ ./first //運行目標文件
010 2000 10100[LLL@localhost test]$ ls
first first.c server.c sock.c
[LLL@localhost test]$ vi first.c //由於運行看出沒有換行,因此用vim編輯修改,打印中加入了換行
[LLL@localhost test]$ rm -f first
[LLL@localhost test]$ ls
first.c server.c sock.c
[LLL@localhost test]$ gcc -g first.c -o first
[LLL@localhost test]$ ./first //換行后的結果
0
10 2000 10100
[LLL@localhost test]$ gdb first //啟動GDB調試
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /data/ManyCore/software/LLL/test/first...done.
(gdb) l //列出內容
5	int result;
6	m = 10*m;
7	n = 100*n;
8	result = m+n;
9	return result;
10	}
11	
12	int main()
13	{
14	int a = 10;
(gdb) //Enter翻頁
15	int b = 100;
16	int i = 0;
17	printf("%d\n",i);
18	i = add(a,b);
19	b = add(b,a);
20	printf("%d %d %d\n",a,b,i);
21	return 0;
22	}
(gdb) b 17 //在17行打斷點
Breakpoint 1 at 0x40057c: file first.c, line 17.
(gdb) start //開始運行程序
Temporary breakpoint 2 at 0x400567: file first.c, line 14.
Starting program: /data/ManyCore/software/LLL/test/first 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Temporary breakpoint 2, main () at first.c:14
14	int a = 10; //進入main函數的首行
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64
(gdb) n //下一步,遇到子函數不進入,當做一行執行
15	int b = 100;
(gdb) s //下一步,s可以進入子函數
16	int i = 0;
(gdb) s

Breakpoint 1, main () at first.c:17
17	printf("%d\n",i);
(gdb) s
0
18	i = add(a,b);
(gdb) b 6 //在6行打斷點
Breakpoint 3 at 0x400537: file first.c, line 6.
(gdb) c //運行到下一個斷點
Continuing.

Breakpoint 3, add (m=10, n=100) at first.c:6
6	m = 10*m;
(gdb) c
Continuing.

Breakpoint 3, add (m=100, n=10) at first.c:6
6	m = 10*m;
(gdb) i locals //查看當棧幀中的局部變量(這里的add函數)
result = 10100 
(gdb) s
7	n = 100*n;
(gdb) s
8	result = m+n;
(gdb) s
9	return result;
(gdb) s
10	}
(gdb) s
main () at first.c:20
20	printf("%d %d %d\n",a,b,i);
(gdb) i locals //查看當棧幀中的局部變量(這里的main函數)
a = 10
b = 2000
i = 10100
(gdb) p a //查看變量
$1 = 10
(gdb) p b
$2 = 2000
(gdb) p &a //查看變量的地址
$3 = (int *) 0x7fffffffe3cc
(gdb) x 0x7ffffffe3cc
0x7ffffffe3cc:	Cannot access memory at address 0x7ffffffe3cc
(gdb) x 0x7fffffffe3cc //查看內存的值
0x7fffffffe3cc:	0x0000000a
(gdb) r //運行程序
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /data/ManyCore/software/LLL/test/first 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, main () at first.c:17
17	printf("%d\n",i);
(gdb) c
Continuing.
0

Breakpoint 3, add (m=10, n=100) at first.c:6
6	m = 10*m;
(gdb) c
Continuing.

Breakpoint 3, add (m=100, n=10) at first.c:6
6	m = 10*m;
(gdb) c
Continuing.
10 2000 10100
[Inferior 1 (process 118146) exited normally]
(gdb)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM