首页 > 编程语言 >Python程序调用图(Call Graph)

Python程序调用图(Call Graph)

时间:2023-10-08 17:24:37浏览次数:68  
标签:分析 Python Graph 代码 静态 Call call 工具

 

 

vitsalis/PyCG: Static Python call graph generator (github.com)

2103.00587.pdf (arxiv.org)

 

PyCG - Practical Python Call Graphs

Linters Tests

PyCG generates call graphs for Python code using static analysis. It efficiently supports

  • Higher order functions
  • Twisted class inheritance schemes
  • Automatic discovery of imported modules for further analysis
  • Nested definitions

You can read the full methodology as well as a complete evaluation on the ICSE 2021 paper.

You can cite PyCG as follows. Vitalis Salis, Thodoris Sotiropoulos, Panos Louridas, Diomidis Spinellis and Dimitris Mitropoulos. PyCG: Practical Call Graph Generation in Python. In 43rd International Conference on Software Engineering, ICSE '21, 25–28 May 2021.

 

What is a Call Graph? And How to Generate them Automatically (freecodecamp.org)

Once everything is ready to run, the first step is to get the call graph data by using pycg library. With the following command we store in a .json file all the neccessary information about our static call graph, which later will be converted to its visual representation:

!pycg file.py -o cg.json

The second step is to visualize the resulting graph from the .json file. So, with pyvis and json Python modules, we can transform our current JSON format data into an HTML file that displays an interactive version of the resulting graph.

 

 

 

 

scottrogowski/code2flow: Pretty good call graphs for dynamic languages (github.com)

 

Code2flow generates call graphs for dynamic programming language. Code2flow supports Python, JavaScript, Ruby, and PHP.

The basic algorithm is simple:

  1. Translate your source files into ASTs.
  2. Find all function definitions.
  3. Determine where those functions are called.
  4. Connect the dots.

Code2flow is useful for:

  • Untangling spaghetti code.
  • Identifying orphaned functions.
  • Getting new developers up to speed.

Code2flow provides a pretty good estimate of your project's structure. No algorithm can generate a perfect call graph for a dynamic language – even less so if that language is duck-typed. See the known limitations in the section below.

(Below: Code2flow running against a subset of itself code2flow code2flow/engine.py code2flow/python.py --target-function=code2flow --downstream-depth=3)

code2flow running against a subset of itself

 

 

 

 

 

Python 程序分析工具调研 | Jckling's Blog

Python 程序分析工具调研

Github 上的开源工具,* 号表示仓库已经不再维护

工具描述开发语言
PySonar2 语义索引器,用于批量处理大型代码库。牺牲实时索引能力,使用过程间分析来推断变量、参数和函数的类型。目前作为大型代码索引服务的基础引擎,Sourcegraph 中也有使用该工具。 Java
Sourcegraph 代码搜索和导航引擎,帮助阅读和理解大型项目的代码。目前支持 Go、Java、Python 等多种编程语言,能够以浏览器插件的形式提供在线代码阅读的功能。 Go、TypeScript
coala 使用配置文件检测和修复代码,可自定义规则和标准检查代码质量,扩展性强。支持 Python、Java、C/C++、JavaScript 等多种编程语言,支持作为编辑器插件使用。 Python
vprof 性能分析工具,为 Python 程序的运行时间、内存使用情况等提供交互式可视化,不适用于大型代码的分析。 Python、JavaScript
Code2Flow 为动态语言生成生成调用图(Call Graph),目前支持 Python、JavaScript、Ruby、PHP。基础流程:AST -> 函数定义 -> 函数调用点 -> 连接点 ,主要是提供一个粗略的概览。缺点是没有定义的函数、不同命名空间相同名字的函数将被跳过等,因此并不适用于大型代码的分析。 Python、JavaScript
pydeps Python 模块依赖可视化,导出 .svg 或 .png 文件。只考虑导入的文件(模块必须安装),在 Python 字节码中查找导入操作码,同时支持外部模块的分析。 Python
pycallgraph* 为 Python 应用生成调用图(Call Graph),包括函数的调用次数、执行时间等信息,提供一定程度的性能分析功能。同时支持过滤函数,避免生成的图太大无法分析,默认导出 .png 文件。 Python
undebt* 执行大规模自动化代码重构,根据自定义模式对代码进行查找和替换,适用于任何语言。 Python
pyt* 基于理论基础的 Python Web 应用静态分析(控制流图、定点、数据流分析),能够检测 SSRF、SQL 注入、XSS、目录遍历等攻击,支持自定义源点和汇点、传播污点的函数。目前已停更,作者建议使用 Pyre 。 Python
Pyre (Facebook) 性能类型检查器,集成静态分析工具 Pysa(污点分析)Pysa 通过跟踪数据流实现安全检查,依赖类型注释,能够高速处理大型代码。 OCaml、Python
Bandit 构建 AST 并使用插件检查安全问题,最初在 OpenStack 安全项目中开发,后来被重新定位到 PyCQA。主要用于扫描危险函数,支持自定义漏洞测试和扩展插件。 Python
Sourcetrail 跨平台的源代码浏览器,支持 C/C++、Java、Python,提供搜索、代码、图形三个交互式视图。亮点是给出了源代码的结构,但只有符号名称。 C++、Java
radon 计算源代码的度量(metrics),包括 McCabe、Halstead、可维护性索引三种度量指标,支持在 coala 中使用。主要用于评估代码的复杂度。 Python
xenon 基于 radon 的监测工具,检查代码复杂性。可应用于 CI/CD,在每次提交代码时运行,根据自定义代码复杂性阈值返回成功或失败。 Python
jedi Python 静态分析工具,侧重于自动补全和跳转,通常作为编辑器的插件使用。 Python
mypy 静态类型检查器(PEP 484),能够对 Python 程序中的类型注释执行静态检查。 Python
pyright (Microsoft) 可用于大型 Python 源代码库的快速类型检查器,使用内置的类型存根进行类型推断,可以在监视模式下运行,支持增量更新。 TypeScript、Python
pytype (Google) 静态分析器,无需类型注释就能检查和推断 Python 代码的类型,也利用了类型存根(pyi 文件)。 Python
Vulture 查找 Python 程序中未使用的代码(死代码),存在漏报,并且隐式调用可能会被误报。支持设置最小置信度、白名单等功能,可以对单个文件或目录的 py 文件执行分析。 Python
PyCG 使用静态分析为 Python 代码生成调用图,支持高阶函数、类继承、导入的模块、嵌套定义。详见论文 ICSE 2021 paper Python
Wily 复杂性检查 Python
McCabe 复杂性检查 Python
it 代码检查工具/框架 Python

以及一些闭源工具

工具 
CodeQL 代码分析引擎
Fortify SCA 静态代码审计工具
Pylance (Microsoft) 基于 pyright 的 Python 代码静态分析工具
DeepSource 支持多种语言的静态分析,个人免费
Codacy 能够通过静态代码分析获得关于安全问题、代码覆盖率、代码重复率、代码复杂性的信息

前段时间的 log4j 貌似就是用 CodeQL 找到的

标签:分析,Python,Graph,代码,静态,Call,call,工具
From: https://www.cnblogs.com/sinferwu/p/17749666.html

相关文章

  • python装饰器顺序
    Python的装饰器是应用的函数或方法的特殊类型改变,它们会在被装饰的函数或方法被调用时执行。你可以使用多个装饰器来装饰一个函数,装饰器的执行顺序与它们应用的顺序有关#使用两个装饰器装饰一个函数@decorator1@decorator2deffunc():pass在上述代码中,首先应用的装饰......
  • Kubernetes 部署 Python Flask 项目
    1、编写源代码及Dockerfiletest.pyfromflaskimportFlaskimportsocketapp=Flask(__name__)@app.route('/')defhello():returnsocket.gethostbyname(socket.gethostname())if__name__=='__main__':app.run(host='0.0.0.0......
  • ControlNet-trt优化总结3:使用multi-stream和cuda-graph构建并行流水线
    ControlNet-trt优化总结3:使用multi-stream和cuda-graph构建并行流水线上节谈到使用TRT-API来构建网络,在这一节中总结一些trick来提升模型的运行效率,这些trick在所有的trt优化中均可使用,主要有以下几点:使用cuda_graph减少kernel间的启动间隙使用Mutil-stream增加异步cuda_gra......
  • python dict和ttl支持自动过期缓存
    pythondict和ttl支持自动过期缓存 github: https://github.com/mailgun/expiringdict 安装pipinstallexpiringdictpipinstallexpiring-dict使用:fromexpiringdictimportExpiringDictfromdatetimeimporttimedelta#创建一个带有过期时间的字典,过期时间......
  • Python-串口通信2
    安装pipinsatllpyserial 初始化简单初始化示例importserialser=serial.Serial('com1',9600,timeout=1) 所有参数 ser=serial.Serial(port=None,#numberofdevice,numberingstartsat#zero.ifeverythingfails,theuser#can......
  • python命令行传参
    #run.pyimportargparseparser=argparse.ArgumentParser()parser.add_argument('--device',type=str,default='cpu:0',help='usewhichdevice')parser.add_argument('--worker',type=int,default=1,help='numofwor......
  • Python学习 —— 初步认知
    写在前面Python是一种流行的高级编程语言,具有简单易学、代码可读性高、应用广泛等优势。它是一种解释型语言,可以直接在终端或集成开发环境(IDE)中运行,而无需事先编译。Python的安装Python的安装过程非常简单。首先,你可以从Python的官方网站(https://www.python.org)下载安装包。根......
  • python查看占用系统内存
    importosimportpsutilinfo=psutil.virtual_memory()print(u'cpu个数:',psutil.cpu_count())print(u'电脑总内存:%.4fGB'%(info.total/1024/1024/1024))print(u'当前进程的内存使用:%.4fGB'%(psutil.Process(os.getpid()).memory_info().rs......
  • Python入门示例系列20 函数
     函数function函数是组织好的,可重复使用的,用来实现单一或相关联功能的代码段。函数能提高应用的模块性,和代码的重复利用率。Python提供了许多内建函数built-in,比如print()。也可以自己创建函数,叫做用户自定义函数。 函数的定义(define)和调用(call)掌握函数的定义和调用。定义一个函......
  • Python入门示例系列18 条件控制 Conditional Statements
    Python提供了bool类型来表示真(对)或假(错),比如常见的5>3比较算式,这个是正确的,在程序世界里称之为真(对),Python使用True来代表;再比如4>20比较算式,这个是错误的,在程序世界里称之为假(错),Python使用False来代表。True和False是Python中的关键字,当作为Python代码输入时,一......