歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> Linux業界 >> 進程在做什麼 Linux下pstack的實現

進程在做什麼 Linux下pstack的實現

日期:2017/3/2 13:45:38   编辑:Linux業界

  Linux下有時候我們需要知道一個進程在做什麼,比如說程序不正常的時候,他到底在干嗎?最直接的方法就是打印出他所有線程的調用棧,這樣我們從棧再配合程序代碼就知道程序在干嗎了。

  Linux下這個工具叫做pstack. 使用方法是

  ?

  # pstack

  Usage: pstack

  當然這個被調查的程序需要有符號信息。 比較雷人的是 這個程序竟然是個shell腳本,核心實現是gdb的 thread apply all bt, 我們可以觀摩下他的實現,這個我們做類似的程序提供了一個很好的思路:

  ?

  [root@=i ~]# cat `which pstack`

  #!/bin/sh

  if test $# -ne 1; then

  echo "Usage: `basename $0 .sh`" 1>&2

  exit 1

  fi

  if test ! -r /proc/$1; then

  echo "Process $1 not found." 1>&2

  exit 1

  fi

  # GDB doesn't allow "thread apply all bt" when the process isn't

  # threaded; need to peek at the process to determine if that or the

  # simpler "bt" should be used.

  backtrace="bt"

  if test -d /proc/$1/task ; then

  # Newer kernel; has a task/ directory.

  if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then

  backtrace="thread apply all bt"

  fi

  elif test -f /proc/$1/maps ; then

  # Older kernel; go by it loading libpthread.

  if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then

  backtrace="thread apply all bt"

  fi

  fi

  GDB=${GDB:-/usr/bin/gdb}

  if $GDB -nx --quiet --batch --readnever > /dev/null 2>&1; then

  readnever=--readnever

  else

  readnever=

  fi

  # Run GDB, strip out unwanted noise.

  $GDB --quiet $readnever -nx /proc/$1/exe $1 <&1 |

  $backtrace

  EOF

  /bin/sed -n

  -e 's/^(gdb) //'

  -e '/^#/p'

  -e '/^Thread/p'

Copyright © Linux教程網 All Rights Reserved