-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create script for creating call graphs
- Loading branch information
1 parent
3ed5b17
commit d4c2f21
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/env python3 | ||
import argparse | ||
|
||
from pathlib import Path | ||
|
||
from kftlib.elf import Elf | ||
from kftlib.event import KFTEventType | ||
from kftlib import inspect | ||
|
||
|
||
def get_fn_graphs(events, elf, function, max_depth): | ||
graphs = [] | ||
function_pc = elf.fun2pc[function] | ||
for td, td_events in events.items(): | ||
graph = "" | ||
depth = 0 | ||
start_time = 0 | ||
drawing_graph = False | ||
for event in td_events: | ||
if not drawing_graph and event.pc != function_pc: | ||
continue | ||
|
||
if event.pc == function_pc and event.typ == KFTEventType.KFT_IN: | ||
drawing_graph = True | ||
start_time = event.timestamp | ||
graph = "" | ||
|
||
append = not max_depth or depth <= max_depth | ||
|
||
time = event.timestamp - start_time | ||
if event.typ == KFTEventType.KFT_IN: | ||
fn_name = elf.pc2fun[event.pc] | ||
line = f"{time:>4} " + depth * "| " + fn_name + "\n" # in | ||
depth += 1 | ||
else: | ||
depth -= 1 | ||
line = f"{time:>4} " + depth * "| " + "*" + "\n" # out | ||
|
||
if append: | ||
graph += line | ||
|
||
if event.pc == function_pc and event.typ == KFTEventType.KFT_OUT: | ||
graphs.append((time, graph)) | ||
drawing_graph = False | ||
|
||
# Sort by time and return only graphs | ||
graphs.sort() | ||
_, graphs = zip(*graphs) | ||
return graphs | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Launch kernel in a board simulator." | ||
) | ||
parser.add_argument("function", | ||
type=str, | ||
help="Function to show call graph for.") | ||
parser.add_argument("kft_dump", | ||
type=Path, | ||
default=Path("dump.kft"), | ||
help="KFTrace dump to process") | ||
parser.add_argument("-e", "--elf-file", | ||
type=Path, | ||
default=Path("sys/mimiker.elf"), | ||
help="Path to mimiker.elf") | ||
parser.add_argument("-d", "--max-depth", | ||
type=int) | ||
parser.add_argument("-o", "--out", | ||
type=Path, | ||
default=Path("graph.txt")) | ||
|
||
args = parser.parse_args() | ||
elf = Elf.inspect_elf_file(args.elf_file) | ||
events = inspect.inspect_kft_file(args.kft_dump, elf) | ||
graphs = get_fn_graphs(events, elf, args.function, args.max_depth) | ||
with open(args.out, "w") as f: | ||
f.write("\n\n".join(graphs)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |