Skip to content

Commit

Permalink
Write envp files
Browse files Browse the repository at this point in the history
  • Loading branch information
andresdelfino committed Mar 29, 2023
1 parent e190acb commit a8d3516
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions strace_tree_printer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class StraceTreePrinter:
CHILD_PID_RE = r'^(clone|__clone2|clone3)\(.+?(\d+)$'
CHILD_PID_RE = r'^(clone|clone3|fork|vfork)\(.+?(\d+)$'
COMMAND_RE = r'^execve\((.+?)\) = 0'
EXIT_STATUS_RE = r'^exit_group\((\d+)\)'

Expand All @@ -22,6 +22,7 @@ def __init__(self, *, root_path: str, prefix: str) -> None:
self.childs = collections.defaultdict(list)
self.pathnames = {}
self.argvs = {}
self.envps = {}
self.exit_statuses = {}
self.data = []
self.stdout = set()
Expand Down Expand Up @@ -51,9 +52,9 @@ def run(self) -> None:

command_match = re.search(self.COMMAND_RE, line)
if command_match:
pathname, argv_env = command_match[1].split(', ', maxsplit=1)
pathname, argv_envp = command_match[1].split(', ', maxsplit=1)
self.pathnames[pid] = pathname[1:-1]
self.argvs[pid] = ' '.join(self.parse_argv_env(argv_env)[0])
self.argvs[pid], self.envps[pid] = self.parse_argv_envp(argv_envp)
continue

exit_status_match = re.search(self.EXIT_STATUS_RE, line)
Expand All @@ -70,6 +71,7 @@ def run(self) -> None:

root_pid = (pids - child_pids).pop()

self.write_envp_files()
self.fill_table(root_pid)

tabulated_data = tabulate.tabulate(
Expand Down Expand Up @@ -105,23 +107,28 @@ def fill_table(self, node: int, level: str = 0) -> None:
log = f'{self.prefix}.{node}'
pathname = self.pathnames.get(node, '?')
output = f'{stdout} {stderr}'
argv = padding + self.argvs.get(node, '?')

if node in self.argvs:
argv = ' '.join(self.argvs[node])
else:
argv = '?'
formatted_argv = padding + argv

self.data.append(
(
log,
pathname,
output,
formatted_exit_status,
argv,
formatted_argv,
)
)

for child in self.childs[node]:
self.fill_table(child, level=level + 1)

@staticmethod
def parse_argv_env(line: str) -> tuple[list[str], list[str]]:
def parse_argv_envp(line: str) -> tuple[list[str], list[str]]:
data = []

words = []
Expand Down Expand Up @@ -150,6 +157,12 @@ def parse_argv_env(line: str) -> tuple[list[str], list[str]]:

return data[0], data[1]

def write_envp_files(self) -> None:
for pid, envp in self.envps.items():
with open(f'{pid}.envp', 'w') as f:
for var in envp:
f.write(f'{var}\n')


def main() -> int:
parser = argparse.ArgumentParser()
Expand Down

0 comments on commit a8d3516

Please sign in to comment.