zoukankan      html  css  js  c++  java
  • 令人惊奇的gdb和pstack

    pstack竟然是一个shell脚本,核心是调用gdb的thread apply all bt查看进程的所有线程的堆栈,之后用sed正则展示线程堆栈信息。

    /proc/pid/exe是一个指向可执行文件的软连接。 

    #!/bin/sh
    if test $# -ne 1; then
        echo "Usage: `basename $0 .sh` <process-id>" 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 <<EOF 2>&1 | 
    set width 0
    set height 0
    set pagination no
    $backtrace
    EOF
    /bin/sed -n
        -e 's/^(gdb) //'
        -e '/^#/p'
        -e '/^Thread/p'
  • 相关阅读:
    React 构建方法总结
    集思广益 (一)
    C#方法调用
    C# Hello World 实例
    C# 环境
    C# 简介
    对象类型的本地写入---plist文件创建以及读取
    正则判断 手机邮箱的正确格式
    数组去重
    Base64编码
  • 原文地址:https://www.cnblogs.com/learn-my-life/p/4208007.html
Copyright © 2011-2022 走看看