for i in `ps -AL |grep Worker | awk '{print $2}'`; do \ echo === $i ===; \ gdb --q --n --ex bt --batch --pid $i; \ done 2>&1 |tee /var/stacks.txt
CPU使用率較低但負載較高
ps -axjf
命令查看是否存在 D+ 狀態進程,該狀態是指不可中斷的睡眠狀態。處於該狀態的進程無法終止,也無法自行退出。只能通過恢復其依賴的資源或者重啟系統來解決。
ps -axjf
-
top -c 然后按P,獲取CPU使用率最高的進程
- top -H 按照 線程名稱顯示
- top -H -p pid 顯示某pid的所有線程
GDB 加載運行的進程
# 用法 xgdb.sh a prog_bin=$1 running_name=$(basename $prog_bin) pid=$(/sbin/pidof $running_name) gdb attach $pid
使用gdb調試程序時,可以使用“i frame
”命令(i
是info
命令縮寫)顯示函數堆棧幀信
可以用“disas /m fun”(disas是disassemble命令縮寫)命令將函數代碼和匯編指令映射起來
如果只想查看某一行所對應的地址范圍,可以:
(gdb) i line 13 Line 13 of "foo.c" starts at address 0x4004e9 <main+37> and ends at 0x40050c <main+72>.
如果只想查看這一條語句對應的匯編代碼,可以使用“disassemble [Start],[End]
”命令
使用gdb調試匯編程序時,可以用“display /i $pc
”命令顯示當程序停止時,將要執行的匯編指令
在gdb中,可以使用如下命令查看變量的類型: whatis xx
ptype xx
i variables xx
用gdb調試程序時,可以用下面的自定義命令,打印程序動態分配內存的信息: 主要是使用gdb的define 作用
https://wizardforcel.gitbooks.io/100-gdb-tips/content/print-malloc-memory.html
define mallocinfo set $__f = fopen("/dev/tty", "w") call malloc_info(0, $__f) call fclose($__f) end 以上面程序為例: Temporary breakpoint 5, main () at a.c:7 7 int i = 0; (gdb) mallocinfo
設置匯編指令格式:

在Intel x86處理器上,gdb默認顯示匯編指令格式是AT&T格式。例如: (gdb) disassemble main Dump of assembler code for function main: 0x08050c0f <+0>: push %ebp 0x08050c10 <+1>: mov %esp,%ebp 0x08050c12 <+3>: call 0x8050c00 <change_var> 0x08050c17 <+8>: mov $0x0,%eax 0x08050c1c <+13>: pop %ebp 0x08050c1d <+14>: ret End of assembler dump. 可以用“set disassembly-flavor”命令將格式改為intel格式: (gdb) set disassembly-flavor intel (gdb) disassemble main Dump of assembler code for function main: 0x08050c0f <+0>: push ebp 0x08050c10 <+1>: mov ebp,esp 0x08050c12 <+3>: call 0x8050c00 <change_var> 0x08050c17 <+8>: mov eax,0x0 0x08050c1c <+13>: pop ebp 0x08050c1d <+14>: ret End of assembler dump. 目前“set disassembly-flavor”命令只能用在Intel x86處理器上,並且取值只有“intel”和“att”。
設置觀察點只針對特定線程生效
gdb可以使用“watch expr thread threadnum
”命令設置觀察點只針對特定線程生效,也就是只有編號為threadnum
的線程改變了變量的值,程序才會停下來,其它編號線程改變變量的值不會讓程序停住
PS:
gdb可以使用“rwatch
”命令設置讀觀察點,也就是當發生讀取變量行為時,程序就會暫停住
gdb可以使用“awatch
”命令設置讀寫觀察點,也就是當發生讀取變量或改變變量值的行為時,程序就會暫停
如果想在調試一個線程時,讓其它線程暫停執行,可以使用“set scheduler-locking on
”命令: