【发布时间】:2021-12-22 10:49:01
【问题描述】:
function_graph 跟踪器以类似于 C 代码和/或 JSON 的格式输出看似时间相关的树。是否有内置的python3可以将其转换为dict?
txt示例(每行末尾有一个\n):
__fdget() {
__fget_light() {
__fget();
}
}
【问题讨论】:
function_graph 跟踪器以类似于 C 代码和/或 JSON 的格式输出看似时间相关的树。是否有内置的python3可以将其转换为dict?
txt示例(每行末尾有一个\n):
__fdget() {
__fget_light() {
__fget();
}
}
【问题讨论】:
因此,在没有找到任何内置 Python3 之后,我编写了一个将其转换为 Python3 字典对象的代码(忽略 /* cmets */)。
代码如下
import os, sys
def parse(total, i, nest, result, j, unique_elems):
if len(total) == i: return
# Path 1) it's a comment and some closing
if '/*' in total[i] and '*/' in total[i]:
if '}' in total[i]:
nest -= 1
if len(j)>1:
j = j[:-1]
parse(total, i+1, nest, result, j, unique_elems)
# Path 2) it's only a closing thing
elif '}' in total[i]:
nest -= 1
if nest == 0:
result += [{}]
j = [j[0]+1]
else:
if len(j)>1:
j = j[:-1]
parse(total, i+1, nest, result, j, unique_elems)
# Path 3) it's an opening
elif '{' in total[i]:
nest += 1
localdata = total[i].split('()')[0].split(' ')[-1]
unique_elems.add(localdata)
update_item_and_expand(j[1:], result[j[0]], localdata)
j.append(localdata)
parse(total, i+1, nest, result, j, unique_elems)
# Path 4) it's a call that ends with ; without affecting nesting
elif ';' in total[i]:
localdata = total[i].split('()')[0].split(' ')[-1]
unique_elems.add(localdata)
update_item_and_expand(j[1:], result[j[0]], localdata)
if nest == 0 and len(j)==1:
j = [j[0]+1]
result += [{}]
parse(total, i+1, nest, result, j, unique_elems)
并使用以下效用函数
def update_item_and_expand(j, result, localdata):
if j==[]:
result[localdata]={}
elif len(j)==1:
result[j[0]][localdata] = {}
elif len(j)>1:
update_item_and_expand(j[1:], result[j[0]], localdata)
else:
print("[ERROR] [update_item_and_expand] Fatal unknown (bad design?)")
sys.exit(1)
运行它并显示它,例如,
unique_elems = set()
result = [{}]
j=[0]
nest = 0
i=0
with open('kerneltrace.txt','r') as f:
total = f.readlines()
parse(total, i, nest, result, j, unique_elems)
delim="\n-------------\n"
print(f"{delim}RAW{delim}")
for t in total:
print(t)
print(f "{delim}PROCESSED{delim}")
for x in result:
for k in x.keys():
print(k)
for k2 in x[k].keys():
print(f'\t{k2}')
print(f'\t\t{x[k][k2]}')
产量:
-------------
RAW
-------------
/* __fdget */
eventfd_poll() {
__pollwait() {
add_wait_queue() {
_raw_spin_lock_irqsave();
_raw_spin_unlock_irqrestore();
}
}
}
fput() {
fput_many();
}
__fdget() {
__fget_light() {
__fget();
}
}
eventfd_poll() {
__pollwait() {
add_wait_queue() {
_raw_spin_lock_irqsave();
_raw_spin_unlock_irqrestore();
}
}
}
-------------
PROCESSED
-------------
eventfd_poll
__pollwait
{'add_wait_queue': {'_raw_spin_lock_irqsave': {}, '_raw_spin_unlock_irqrestore': {}}}
fput
fput_many
{}
__fdget
__fget_light
{'__fget': {}}
eventfd_poll
__pollwait
{'add_wait_queue': {'_raw_spin_lock_irqsave': {}, '_raw_spin_unlock_irqrestore': {}}}
这对于通过将空字典解释为嵌套结束来构建时间相关图/树很有用。
【讨论】: