Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
154 lines (125 loc) · 5.66 KB
/
Copy pathutils.py
File metadata and controls
154 lines (125 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from config import *
import subprocess
from subprocess import PIPE
def run_addr2line(addr):
# print("Running addr2line for address: ", addr)
out = subprocess.Popen(['addr2line', '-f', '-e', str(linux_image), addr],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = out.communicate()
if "??:0" in str(stdout):
#retun null
return None, None, None
stdout = str(stdout).replace("b'", "")
stdout = stdout.split("\\n")
func_name = stdout[0]
file = stdout[1].replace("/root/linux/kbuild/../", "")
line = file.split(":")[1]
file = file.split(":")[0]
return func_name, file, line
def run_eu_addr2line(addr):
# Run eu-addr2line to get function name and location
out = subprocess.Popen(['eu-addr2line', '-f', '-e', str(linux_image), addr],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = out.communicate()
if "??:0" in str(stdout):
# Return null if no information is found
return None, None, None
stdout = stdout.decode("utf-8").strip().split("\n")
# print("eu-addr2line output: ", stdout)
# Extract the function name and location
func_name = stdout[0]
file_info = stdout[1]
# print(f"Function name: {func_name}")
# print(f"File info: {file_info}")
# Handle inlined functions
if "inlined at" in func_name:
# Inlined function is in the first line, so we need to adjust
# Extract the actual function name from the inlined function string
func_name = func_name.split(" ")[0].strip() # Take the first part before "inlined at"
# print(f"Adjusted function name: {func_name}")
# Clean up the file path (remove the leading part) and ensure it starts with "/"
file_info = file_info.replace("/root/linux/kbuild/../", "")
# Ensure the file path starts with "/"
if file_info.startswith('../'):
file_info = file_info[3:]
# Extract the file path and line number
file, line = file_info.rsplit(":", 1)
file = file_info.split(":")[0] # Get the file path without line number
line = file_info.split(":")[1] # Get the line number
# Print the desired output
# print(f"{func_name} {file} {line}")
return func_name, file, line
def read_line(file_path, line_number):
try:
with open(file_path, 'r') as file:
for line_num, line in enumerate(file, 1):
if line_num == line_number:
# print(line_num)
# print(file_path)
return line.strip() # Remove trailing newline character
except FileNotFoundError:
print("File not found.")
print(file_path)
except Exception as e:
print("An error occurred:", e)
# search all the instances of lockdep_primitives and create a map of lockdep keys to lock names.
# Placement of lock dep keys in the kernel source code differs based on the primitive used
# Details: docs/lockdep_details.csv
def create_lockdep_keys_map():
#open a file and create a map of lockdep keys to lock names
lockdep_primitives = ["lockdep_set_class", "lockdep_set_class_and_subclass", "lockdep_set_class_and_name"]
# lockdep_primitives = ["lockdep_set_class_and_subclass"]
working_dir = os.getcwd()
for p in lockdep_primitives:
lockdep_keys = {}
db = pd.DataFrame(columns=["lockdep_key", "lock_name"])
# use cscope to find all instances of lockdep_set_class
os.chdir(kernel_path)
proc = subprocess.run(["cscope", "-dL0", p],
stdout=subprocess.PIPE, stderr=subprocess.PIPE , universal_newlines=True)
sub_out = proc.stdout
sub_err = proc.stderr
buf_ls = sub_out.split(sep="\n")
for i in buf_ls:
out = i
if i == "":
break
# e = i.split()
# print(i)
# remove everthing before the primitive
if p in i:
# remove everything before the primitive
i = i.split(p)[1]
#check for paranthesis
if "(" in i and ")" in i:
i = i.split("(")[1]
i = i.split(")")[0]
# 3rd arg is the lock name
if "," in i and "lockdep_set_class_and_name" in p:
lock_name = i.split(",")[0].strip()
lockdep_key = i.split(",")[2].strip()
lockdep_keys[lockdep_key] = lock_name
# 2nd arg is the lock name
elif "," in i:
lock_name = i.split(",")[0].strip()
lockdep_key = i.split(",")[1].strip()
lockdep_keys[lockdep_key] = lock_name
else:
print("Comma not found in: ", i)
continue
# lock_name = i.split(",")[0]
# lockdep_key = i.split(",")[1]
# lockdep_keys[lockdep_key] = lock_name
else:
# write missed cases to a file
with open(working_dir + "/missed_lockdep_keys.txt", "a") as f:
f.write(str(out))
f.write("\n")
continue
os.chdir(working_dir)
for key, value in lockdep_keys.items():
db = db.append({"lockdep_key": key, "lock_name": value}, ignore_index=True)
db.to_csv(lockdep_keys_base_path + p + ".csv", index=False)
# print(lockdep_keys)
# lockdep_keys.append(e[1].strip())
os.chdir(working_dir)