转换GDB调用栈到流程图

如果你想在GDB调试时把调用堆栈保存下来归档,那下面这个脚本就方便你了。原理是将调用堆栈的函数抽取出来,再完成调用关系就可以了。稍加改动就可以同时读取多个调用栈,生成一张流程图。


首先你要安装dot (Mac OS下安装Graphviz), 如果你想转为文本格式,就可安装Perl的Graph::Easy包(命令行:sudo perl -MCPAN -e ‘install Graph::Easy‘, Ubuntu下直接安装libgraph-easy-perl)。


然后按需要执行脚本就可以了, 假定如下的调用栈:

   #0  WebCore::FrameLoader::FunctionA (this=0x2a7d91f8) at /FrameLoader.cpp

   #1  0x4efd2514 in WebCore::FrameLoader::FunctionB (this=0x2a7d91f8) at /FrameLoader.cpp:553

   #2  0x4efd1918 in FunctionC ()at /mainFile.cpp:100


1. 转为图片:

python convertStackToDot.pystack.txt|dot -Tpng>output.png

转换GDB调用栈到流程图

默认由上到下排列,如果你想改变,可以通过在脚本的参数中增加dot设定调整,比如:

python convertStackToDot.py stack.txt ‘rankdir=LR;|dot -Tpng>output.png

就会变成横排:

转换GDB调用栈到流程图


2. 转为文本

有时你希望转为文本,还好有Graph Easy包,不然你就要使用asciio自己画了。

python convertStackToDot.py stack.txt ‘rankdir=LR;’|graph-easy -as_ascii>output.txt


效果如下:

+-----------+     +------------------------+     +------------------------+

| FunctionC | --> | FrameLoader::FunctionB | --> | FrameLoader::FunctionA |

+-----------+     +------------------------+     +------------------------+


转载请注明出处: http://blog.csdn.net/horkychen


脚本如下:

#!/usr/bin/python
#coding=utf-8
#python convertStackToDot.py stack.txt|dot -Tpng>output.png
#To generate ascii flow chart, graph_easy should be installed:
#  sudo apt-get install libgraph-easy-perl
#Then use below command line:
#python convertStackToDot.py stack.txt|graph-easy -as_ascii>output.txt

import sys
import re

function_name_str = r"[a-zA-Z][a-zA-Z0-9]*::[a-zA-Z0-9\_\-<>]* \(|[a-zA-Z0-9_\-<>]* \("

def convertToDot(filepath,extraOptions):
    f = open(filepath, ‘r‘)

    outputText = "digraph backtrace{ \n" + extraOptions +"\n"
    callingStack = ""
    lineNo = 0
    for line in f:
        m = re.search(function_name_str, line)
        lineNo = lineNo+1
        if m:
            text=m.group(0).strip()
            text = text[:-1]

            if(len(text)==0):
            	break
            
            node_label = ‘Node‘+str(lineNo)+‘[label="‘+text+‘" shape=box];‘;
            outputText = outputText+node_label+‘\n‘

            if(1==lineNo):
            	callingStack = callingStack +‘Node‘+str(lineNo)
            else:
            	callingStack = ‘Node‘+str(lineNo)+‘->‘+callingStack

    outputText = outputText+callingStack+"\n}"

    print(outputText)

    f.close()

if __name__=="__main__":
    if len(sys.argv)<=1:
        print "**Please specify the target file!"
        print "  Usage: python convertStackToDot.py <folder> [extraDotOptions]"
        print "    extraDotOptions:  rankdir=LR; --> To show functions in horizontal."
        print "  Any comment, please feel free to contact horky.chen@gmail.com."
        quit()

    extraOptions = ‘‘

    if len(sys.argv)>2:
        extraOptions = sys.argv[2]

    convertToDot(sys.argv[1],extraOptions)


转换GDB调用栈到流程图,布布扣,bubuko.com

转换GDB调用栈到流程图

上一篇:10G RAC节点2宕机通过修改listener.ora实现客户端通过节点2VIP连接到数据库


下一篇:验证service_names、db_name、db_unique_name在注册动态监听服务名时的作用