用python从c源代码文件中提取函数代码

2024-06-02 11:35:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种在Python中解析c源文件的方法。在

我知道有一个像pycparse这样的库可以解析c文件,但它似乎依赖于gcc编译器。在

我正在开发一个类似readelf的工具,它可以读取ELF文件,从函数中提取操作码。我需要读取c源文件以从文件中获取函数的相应c代码。在

因此,如果我们在拆分屏幕上思考,我希望在左边看到汇编程序/操作码,在右边看到相应的c代码。在

例如,当我打开一个用c编写的基本计算器时,我的二进制文件中有一个名为“add”的函数。我提取操作码/汇编程序并将其显示在窗口的左侧。现在我需要一个python函数,它可以打开目录中的所有c文件,以找到函数对应的c代码。在

有人知道如何解决这个难题吗?在

下面是我现在所拥有的示例输出:

|==================================================================|
| Adress             | Function                       | Size       |
|====================|================================|============|
| 0x000000000000065a | sub                            | 31         |
|==================================================================|
| 55 48 89 e5 89 7d ec 89 75 e8 c7 45 fc 00 00 00 00 8b 45 ec 2b   |
| 45 e8 89 45 fc 8b 45 fc 5d c3                                    |
|==================================================================|
| int sub(int a, int b)                                            |
| {                                                                |
|   int c = 0;                                                     |
|   c = a - b;                                                     |
|   return c;                                                      |
| }                                                                |
|==================================================================|

但是我的代码目前只能使用pycparse和basic-c文件,因为如果必须在不同的c文件中搜索函数,pycparse将失败。我认为它使用编译器来编译代码,就像gcc的包装器一样。在


Tags: 文件工具方法函数代码编译器intgcc
1条回答
网友
1楼 · 发布于 2024-06-02 11:35:38

我用ctags和python编写了一个小函数从c源代码中提取函数体。在

也许这对某人有帮助。。。在

import subprocess
import glob


def get_line_number(filename, funcname):
    found = False
    cmd = "ctags -x  c-kinds=fp " + filename + " | grep " + funcname

    output = subprocess.getoutput(cmd)
    lines = output.splitlines()

    for line in lines:
        if line.startswith(funcname + " "):    
            found = True

            if output.strip() is not "":
                output = output.split(" ")
                lines = list(filter(None, output))
                line_num = lines[2]

                print("Function found in file " + filename + " on line: " + line_num)
                return int(line_num)

    if found == False:
        #print("Function not found")
        return 0


def process_file(filename, line_num):
    print("opening " + filename + " on line " + str(line_num))

    code = ""
    cnt_braket = 0
    found_start = False
    found_end = False

    with open(filename, "r") as f:
        for i, line in enumerate(f):
            if(i >= (line_num - 1)):
                code += line

                if line.count("{") > 0:
                    found_start = True
                    cnt_braket += line.count("{")

                if line.count("}") > 0:
                    cnt_braket -= line.count("}")

                if cnt_braket == 0 and found_start == True:
                    found_end = True
                    return code


folder = "/usr/src/bash-4.4.18"
funcname = "add_alias"

for filename in glob.iglob(folder + "/*.c", recursive=True):
    line_num = get_line_number(filename, funcname)

    if line_num > 0:
        process_file(filename, line_num)

相关问题 更多 >