问题:现有一函数库,这里是lapack3.5。lapack提供的每一个函数API都单独是一个.c。请给出这些API的相互调用关系。间接调用也要统计,循环调用(如果可能的话)不计。进一步的,如果给定一个搜索范围,即只要求其中一些函数API的相互调用关系,又该怎么做?
思路:linux下使用nm命令即可查看二进制文件符号表。根据不同函数的符号表确定调用关系,得到一个图数据结构。针对要调研的API,以这个API为根节点在图中DFS搜索即可。更进一步的要求只需要DFS输出时过滤。
import os
relations = dict()
for obj in os.listdir('.') :
if os.path.isfile(obj) and os.path.splitext(obj)[1]=='.o':
tempSymbol = os.popen('nm '+ obj)
relations[obj[:-2]] = set()
for context in tempSymbol.readlines():
message = context.split()
if message[0] == 'U':
if message[1][-1] == "_":
relations[obj[:-2]].add(message[1][:-1])
else:
relations[obj[:-2]].add(message[1])
# for key,value in relations.items():
# print(key + " : ",end = '')
# for val in value:
# print(val + " ",end = '')
# print()
symbols = []
for key,value in relations.items():
symbols.append(key)
for val in value:
if not val in symbols:
symbols.append(val)
# for s in symbols:
# print(str(symbols.index(s)) + " : " + s)
graph = []
for symbol in symbols:
tempAdj = []
if symbol in relations.keys():
for n in relations[symbol]:
tempAdj.append(symbols.index(n))
graph.append(tempAdj)
# idx = 0
# for adjs in graph:
# print(str(idx) + " ->",end = '')
# idx += 1
# for n in adjs:
# print(n,end = '')
# print()
# print()
search_list =["ssyevd","sgeqrf","sorgqr","sormqr","spotrf","spotri","sgesvd","spotrs","spptrf","sgeqp3","strtrs"\
,"sormrq","sgerqf","sgels","sgeev","sgetrf","sgetri","sgelqf","sgetrs","slasrt"]
optmize_list=["slacpy","slaset","slascl"]
#DFS the graph -> a tree
def getRelation(root_idx):
visited = [False] * len(symbols)
depth = 0
def dfs(obj_idx):
nonlocal depth
visited[obj_idx] = True
if symbols[obj_idx] in search_list or symbols[obj_idx] in optmize_list:
print("\t"*depth + symbols[obj_idx])
if obj_idx < len(graph):
for child in graph[obj_idx]:
if visited[child] == False:
depth += 1
dfs(child)
depth -= 1
dfs(root_idx)
for search_symbol in search_list:
idx = symbols.index(search_symbol)
getRelation(idx)
exit()
while True:
symbol_name = input("Enter the func : ")
if symbol_name == "#":
break
if symbol_name in symbols:
idx = symbols.index(symbol_name)
getRelation(idx)
else:
print("Sorry, there is no this function...")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)