《軟體調試的藝術》筆記--使用其他調試工具

來源:互聯網
上載者:User

標籤:style   使用   資料   os   art   問題   

1.splint

splint是一個開源的靜態代碼檢查工具,使用方法如下:

待檢測的程式程式如下:

#include <stdio.h>static void display(int i){printf("i = %d\n",i);}int main(void){int i,j;i = 1;i = i + 1;display(i);return 0;}
調試如下:

$ splint main.c 
Splint 3.1.2 --- 03 May 2009

main.c: (in function main)
main.c:10:8: Variable j declared but not used
  A variable is declared but never used. Use /*@[email protected]*/ in front of
  declaration to suppress message. (Use -varuse to inhibit warning)


Finished checking --- 1 code warning

提示定義了一個變數j但是從來沒有使用過。

將j定義去掉後,再進行檢測:

$ splint main.c 
Splint 3.1.2 --- 03 May 2009

Finished checking --- no warnings

splint有一大推flag可以使用,使用時標誌前加’+‘或’-’,‘+‘標誌開啟這個標誌,‘-‘表示關閉此標誌。

下列語句用於檢查程式中資料的越界問題。

splint +bounds main.c

2.stracestrace常用來跟蹤進程執行時的系統調用和所接收的訊號。對上面的程式進行追蹤測試:$ strace ./a.out 
execve("./a.out", ["./a.out"], [/* 50 vars */]) = 0
brk(0)                                  = 0x1033000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f64fa550000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=77020, ...}) = 0
mmap(NULL, 77020, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f64fa53d000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\30\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1815224, ...}) = 0
mmap(NULL, 3929304, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f64f9f70000
mprotect(0x7f64fa125000, 2097152, PROT_NONE) = 0
mmap(0x7f64fa325000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b5000) = 0x7f64fa325000
mmap(0x7f64fa32b000, 17624, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f64fa32b000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f64fa53c000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f64fa53b000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f64fa53a000
arch_prctl(ARCH_SET_FS, 0x7f64fa53b700) = 0
mprotect(0x7f64fa325000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ)     = 0
mprotect(0x7f64fa552000, 4096, PROT_READ) = 0
munmap(0x7f64fa53d000, 77020)           = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f64fa54f000
write(1, "i = 2\n", 6i = 2
)                  = 6
exit_group(0)                           = ?
3.ltraceltrace用來跟蹤進程調用庫函數的情況。
對上面的程式進行追蹤測試:
$ ltrace ./a.out 
__libc_start_main(0x400518, 1, 0x7ffff364a1a8, 0x400550, 0x4005e0 <unfinished ...>
printf("i = %d\n", 2i = 2
)                                                                   = 6
+++ exited (status 0) +++

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.