參考:https://blog.csdn.net/justlinux2010/article/details/9453151
循環/條件
在gdb的腳本中循環遍歷整個哈希表,並且加上判斷條件來進行統計,然后輸出結果。導入gdb腳本的方式很簡單,gdb中輸入“source 腳本名”。
腳本如下:
set $i=32707
set $j=0
while ($i)
if (fcluster->hash_table[$i].addr == 0x380aa8c0)
set $j++
end
set $i--
end
p $j
前兩行的set是定義兩個變量,注意一定要加上'$'符號,否則會認為是被調試的程序中的變量。fcluster->hash_table是我的哈希表。gdb腳本中while、if語句塊要以end結尾。腳本中的判斷語句和C語言中的語法一樣。最后輸出統計結果。
上面的腳本也可以直接在gdb命令窗口輸入。
printf
print只能打印一個變量或表達式,如果需要指定格式打印,可以使用printf,使用方法和C語言相同。
(gdb) help printf
printf "printf format string", arg1, arg2, arg3, ..., argn
This is useful for formatted output in user-defined commands.
