-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack_analyzer.py
More file actions
387 lines (322 loc) · 12.8 KB
/
Copy pathstack_analyzer.py
File metadata and controls
387 lines (322 loc) · 12.8 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import os
import re
import json
import argparse
from dataclasses import dataclass, replace
from typing import List, Dict, Optional, Set
@dataclass
class Function:
name: str
file: str
def serialize(self):
return {
"file": self.file,
"function": self.name,
}
@dataclass
class StackStats:
usage: int = 0
static: bool = True
bounded: bool = True
def serialize(self):
return {
"usage": self.usage,
"static": self.static,
"bounded": self.bounded,
}
@dataclass
class StackData:
stats: Optional[StackStats] = None
untracked: Optional[Set[str]] = None
def serialize(self):
output = {}
if self.stats:
output = self.stats.serialize()
if self.untracked and len(self.untracked) > 0:
output["untracked"] = list(self.untracked)
return output
@dataclass
class CallGraph:
function: Function
calls: list[Function]
@dataclass
class CflowLine:
function: Function
level: int
@dataclass
class StackUsage:
function: Function
self_stack: Optional[StackData] = None
total_stack: Optional[StackData] = None
def serialize(self):
output = self.function.serialize()
if self.self_stack:
output["self"] = self.self_stack.serialize()
if self.total_stack:
output["total"] = self.total_stack.serialize()
return output
@dataclass
class FunctionReport:
result: StackUsage
called: List[StackUsage]
def serialize(self):
output = self.result.serialize()
output["calls"] = []
if self.called:
for called in sorted(self.called, key=lambda x: (x.function.file, x.function.name)):
output["calls"].append(called.serialize())
return output
def parse_su_file(file_path: str) -> List[StackUsage]:
"""
Analyzes a .su file to obtain stack usage information.
Args:
file_path: Path to the .su file to be analyzed
Returns:
List of StackUsage objects containing stack usage information
"""
stack_usages = []
with open(file_path, 'r') as file:
for line in file:
# Expected format: file:line:col:function_name bytes type
match = re.match(r'([^:]+):(\d+):(\d+):(\S+)\s+(\d+)\s+(\S+)', line.strip())
if match:
source_file, _, _, function_name, usage, usage_type = match.groups()
function = Function(file=source_file, name=function_name)
is_static = False
explicitly_bounded = False
if "static" in usage_type:
is_static = True
if "bounded" in usage_type:
explicitly_bounded = True
stack_stats = StackStats(
usage=int(usage),
static=is_static,
bounded=explicitly_bounded or is_static
)
stack_data = StackData(stats=stack_stats)
stack_usage = StackUsage(
function=function,
self_stack=stack_data,
)
stack_usages.append(stack_usage)
return stack_usages
def find_su_files(directory: str) -> List[StackUsage]:
"""
Recursively navigates through a directory looking for .su files and extracts stack usage information.
Args:
directory: Root directory to start the search
Returns:
List of StackUsage objects containing stack usage information from all files found
"""
all_stack_usages = []
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.su'):
file_path = os.path.join(root, file)
try:
stack_usages = parse_su_file(file_path)
all_stack_usages.extend(stack_usages)
except Exception as e:
print(f"Error processing {file_path}: {e}")
return all_stack_usages
def parse_cflow_line(line: str) -> Optional[CflowLine]:
"""
Analyzes a line from the cflow file to extract function call information.
Args:
line: Line from the cflow file
Returns:
CflowLine object containing line information, or None if the line doesn't contain valid information
"""
output = None
# Complete match {indent_level} function_name() at source_file:line_number
match = re.match(r'{\s*(\d+)\}\s+(\S+)\(\).*at\s+([^:]+):(\d+)', line)
if match:
indent_level = int(match.group(1))
function_name = match.group(2)
source_file = match.group(3)
output = CflowLine(
level=indent_level,
function=Function(name=function_name, file=source_file)
)
# Incomplete match {indent_level} function_name()
else:
match = re.match(r'{\s*(\d+)\}\s+(\S+)\(\)', line)
if match:
indent_level = int(match.group(1))
function_name = match.group(2)
output = CflowLine(
level=indent_level,
function=Function(name=function_name, file="")
)
return output
def parse_cflow_file(file_path: str) -> List[CallGraph]:
"""
Analyzes a file generated by the cflow tool to extract the function call graph.
Args:
file_path: Path to the cflow file to be analyzed
Returns:
List of CallGraph objects representing the call graph
"""
call_graphs = []
call_stack = []
with open(file_path, 'r') as file:
for line in file:
parsed = parse_cflow_line(line)
if not parsed:
continue
current_function = parsed.function
# Adjust call stack based on indentation level
while len(call_stack) > parsed.level:
call_stack.pop()
# Create CallGraph entry for the parent function if it exists
if call_stack and parsed.level > 0:
parent = call_stack[-1]
# Find if we already have a CallGraph for this parent
parent_graph = next((cg for cg in call_graphs if cg.function == parent), None)
if parent_graph:
# Check if this function call is already in the parent's calls
if not any(f == current_function for f in parent_graph.calls):
parent_graph.calls.append(current_function)
else:
# Create new CallGraph for this parent
call_graphs.append(CallGraph(function=parent, calls=[current_function]))
# Add current function to call stack
call_stack.append(current_function)
# If this is a new root function, create a CallGraph for it
if parsed.level == 0 and not any(cg.function == current_function for cg in call_graphs):
call_graphs.append(CallGraph(function=current_function, calls=[]))
return call_graphs
def get_total_stack(function: Function, call_graph_map: Dict[str, CallGraph],
stack_usage_map: Dict[str, StackData], visited: Set[str] = None) -> StackData:
"""
Calculates the total stack usage for a function, considering recursive calls.
Args:
function: Function to calculate stack usage for
call_graph_map: Mapping of functions to their call graphs
stack_usage_map: Mapping of functions to their stack usage data
visited: Set of already visited functions to avoid infinite loops
Returns:
Total stack usage in bytes
"""
if visited is None:
visited = set()
# Create a unique key for the function
function_key = get_function_key(function)
# If we've already visited this function, return neutral stack data
if function_key in visited:
return StackData(stats=StackStats())
# Mark this function as visited
visited.add(function_key)
# Get the stack stats for this function
accumulated = None
stack_data = stack_usage_map.get(function_key)
if stack_data is not None:
accumulated = replace(stack_data.stats)
# Get the call graph for this function
call_graph = call_graph_map.get(function_key)
max_call_path_usage = 0
untracked = set()
if call_graph and accumulated:
for called_function in call_graph.calls:
called_key = get_function_key(called_function)
# Skip self-recursive calls as they're already accounted for in the base usage
if called_key != function_key:
call_data = get_total_stack(
called_function,
call_graph_map,
stack_usage_map,
visited.copy()
)
if call_data.stats:
max_call_path_usage = max(
max_call_path_usage, call_data.stats.usage
)
accumulated.static = accumulated.static and call_data.stats.static
accumulated.bounded = accumulated.bounded and call_data.stats.bounded
else:
untracked.add(called_function.name)
if call_data.untracked:
untracked.update(call_data.untracked)
accumulated.usage += max_call_path_usage
return StackData(
stats=accumulated,
untracked=untracked,
)
def get_function_key(function: Function) -> str:
"""
Generates a unique key for a function based on its name and file.
Args:
function: Function object to generate the key for
Returns:
Unique string key for the function
"""
return f"{function.name}:{os.path.basename(function.file)}"
def generate_report_data(stack_usages: List[StackUsage], call_graphs: List[CallGraph]) -> List[FunctionReport]:
"""
Generates a JSON report with stack analysis data.
Args:
stack_usages: List of stack usage information
call_graphs: List of call graph information
Returns:
List of FunctionReport objects with stack usage information for each function
"""
# Create mapping for easier lookup
stack_usage_map = {get_function_key(su.function): su.self_stack for su in stack_usages}
call_graph_map = {get_function_key(cg.function): cg for cg in call_graphs}
# Create the report data
report_data = []
for su in stack_usages:
# Calculate total stack usage for this function
total_stack = get_total_stack(su.function, call_graph_map, stack_usage_map)
# Get the list of called functions
call_graph = call_graph_map.get(get_function_key(su.function))
called_functions = []
if call_graph:
for called_func in call_graph.calls:
called_functions.append(StackUsage(
function=called_func,
total_stack=get_total_stack(
called_func, call_graph_map, stack_usage_map
)
))
# Create the entry for this function
entry = FunctionReport(
StackUsage(
function=su.function,
self_stack=su.self_stack,
total_stack=total_stack
),
called=called_functions
)
report_data.append(entry)
return report_data
def save_json_report(report_data: List[FunctionReport], output_path: str):
"""
Saves the JSON report to a file.
Args:
report_data: List of FunctionReport objects
output_path: Path where the file will be saved
"""
report_data.sort(key=lambda x: (x.result.function.file, x.result.function.name))
json_data = [report.serialize() for report in report_data]
with open(output_path, 'w') as file:
json.dump(json_data, file, indent=2)
def main():
"""
Main function that processes command-line arguments and executes stack usage analysis.
"""
parser = argparse.ArgumentParser(description='Analyze stack usage from compiler output and cflow files')
parser.add_argument('--su-dir', required=True, help='Directory to recursively search for .su files')
parser.add_argument('--cflow-file', required=True,
help='Path to the cflow output file (options --print-level and --format=gnu are required for good parsing)'
)
parser.add_argument('--output', default='stack_analysis.json', help='Path to output JSON report file (default: %(default)s)')
args = parser.parse_args()
stack_usages = find_su_files(args.su_dir)
print(f"Found {len(stack_usages)} stack usage records")
call_graphs = parse_cflow_file(args.cflow_file)
print(f"Found {len(call_graphs)} call graph entries")
save_json_report(generate_report_data(stack_usages, call_graphs), args.output)
print(f"JSON report saved to {args.output}")
if __name__ == "__main__":
main()