- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要在相当大的 (5-15,000) 组 C(不是 C++)文件中检测直接和间接递归。
文件已经过预处理。
出于安全原因,该代码非常“老派”,因此其中没有函数指针等花哨的东西,只有传递变量的函数和一些做同样事情的函数宏。
检测递归最自然的方法是制作一个有向调用图,将每个函数视为一个节点,其边连接到它调用的所有其他函数。如果图有任何循环,则我们有递归。
查找函数调用的正则表达式很简单,但我还需要知道调用的是哪个函数。
PyCParser 很好,但它会提示很多事情,例如 undefined variable 或未定义源类型或在不同文件中定义的 typedef,这与我的用例完全无关。该项目使用自定义依赖管理系统,因此一些包含和此类自动添加,所以我需要 PyCParser 不关心 任何东西 除了 FuncCall
和 FuncDef
节点,我认为没有办法将解析过程本身限制到此。
我宁愿不实现解析器,因为我没有时间去学习如何在 python 中执行此操作,然后再实现解决方案。
回到问题,我将如何解析C文件中的函数?基本上得到一个以字符串(文件中定义的函数名称)作为键,以字符串列表(每个函数调用的函数)作为值的字典?正则表达式似乎是最自然的解决方案。
遗憾的是,使用 python 不是可选的。
最佳答案
为什么不对编译后的代码使用 objdump
,然后解析生成的程序集来构建图表?
test1.c 文件:
extern void test2();
void test1()
{
test2();
}
test2.c 文件:
extern void test1();
void test2()
{
test1();
}
int main()
{
test2();
}
现在构建它:
gcc -g test1.c test2.c -o myprog
现在拆解
objdump -d myprog > myprog.asm
使用几个简单的正则表达式查找所有函数调用,同时记住您所在的上下文。反汇编示例向您展示了它应该是多么容易:
00401630 <_test1>:
401630: 55 push %ebp
401631: 89 e5 mov %esp,%ebp
401633: 83 ec 08 sub $0x8,%esp
401636: e8 05 00 00 00 call 401640 <_test2>
40163b: c9 leave
40163c: c3 ret
40163d: 90 nop
40163e: 90 nop
40163f: 90 nop
00401640 <_test2>:
401640: 55 push %ebp
401641: 89 e5 mov %esp,%ebp
401643: 83 ec 08 sub $0x8,%esp
401646: e8 e5 ff ff ff call 401630 <_test1>
40164b: c9 leave
40164c: c3 ret
然后使用 python 对反汇编进行后处理并构建一个函数=>调用字典:
import re
import collections
calldict = collections.defaultdict(set)
callre = re.compile(".*\scall\s+.*<(.*)>")
funcre = re.compile("[0-9a-f]+\s<(.*)>:")
current_function = ""
with open("myprog.asm") as f:
for l in f:
m = funcre.match(l)
if m:
current_function = m.group(1)
else:
m = callre.search(l)
if m:
called = m.group(1)
calldict[current_function].add(called)
我没有编写完整的图搜索,但您可以使用如下简单代码检测“乒乓”递归:
for function,called_set in calldict.items():
for called in called_set:
callset = calldict.get(called)
if callset and function in callset:
print(function,called)
这给了我:
_test2 _test1
_test1 _test2
此符号/asm 分析技术也用于 callcatcher检测未使用的 C 函数(这也可以通过检查不在任何集合中的键以及对编译器符号的一些过滤来非常容易地完成)
关于python - 使用 Python 检测 C 文件中的递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49175624/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!