| 1 |
#! /usr/bin/env stap |
| 2 |
# Copyright (C) 2014-2018 Red Hat, Inc. |
| 3 |
# by Josh Stone <[email protected]> |
| 4 |
# |
| 5 |
# pstree.stp generates a process diagram in DOT form. For instance, it may be |
| 6 |
# useful on a 'make' command to see all the processes that are started. |
| 7 |
# |
| 8 |
# Run the script with: |
| 9 |
# stap pstree.stp -c 'command_to_watch' -o output.dot |
| 10 |
# |
| 11 |
# Render the diagram with: |
| 12 |
# dot -Tsvg output.dot >output.svg |
| 13 |
|
| 14 |
probe begin |
| 15 |
{ |
| 16 |
printf("digraph pstree {\n") |
| 17 |
printf("rankdir=\"LR\"\n") |
| 18 |
} |
| 19 |
|
| 20 |
function dot_escape(str) |
| 21 |
{ |
| 22 |
# In DOT double-quoted strings, the only escape is " to \" |
| 23 |
return str_replace(str, "\"", "\\\"") |
| 24 |
} |
| 25 |
|
| 26 |
global depth |
| 27 |
global exe_name |
| 28 |
global exe_name_ctr |
| 29 |
|
| 30 |
probe process.begin |
| 31 |
{ |
| 32 |
if (!(pid() in depth)) { |
| 33 |
depth[pid()] = 1 |
| 34 |
if (pid() != target()) |
| 35 |
printf("PID%d_%d -> PID%d_1\n", ppid(), depth[ppid()], pid()) |
| 36 |
printf("PID%d_1 [ label=\"(%d) %s\" tooltip=\"forked from %d\" ];\n", |
| 37 |
pid(), pid(), dot_escape(execname()), ppid()) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
probe syscall.execve |
| 42 |
{ |
| 43 |
# Save value of filename. Note that filename is quoted, but we'll |
| 44 |
# strip these quotes in syscall.execve.return. |
| 45 |
exe_name[tid(), ++exe_name_ctr[tid()]] = filename |
| 46 |
} |
| 47 |
|
| 48 |
probe syscall.execve.return |
| 49 |
{ |
| 50 |
# We'd like to use '@entry(user_string($filename))' here, but we |
| 51 |
# can't. On kernels < 3.7, sys_execve was in arch-specific code and |
| 52 |
# had wildly varying variable names for the filename argument. So, |
| 53 |
# we'll mimic @entry() here. |
| 54 |
saved_exe_name = exe_name[tid(), exe_name_ctr[tid()]] |
| 55 |
delete exe_name[tid(), (exe_name_ctr[tid()])--] |
| 56 |
if (!(exe_name_ctr[tid()])) |
| 57 |
delete exe_name_ctr[tid()] |
| 58 |
|
| 59 |
if (retval == 0 && pid() in depth) { |
| 60 |
d = ++depth[pid()] |
| 61 |
printf("PID%d_%d -> PID%d_%d [ style=\"dashed\" ];\n", |
| 62 |
pid(), d-1, pid(), d) |
| 63 |
printf("PID%d_%d [ label=\"(%d) %s\" tooltip=\"%s\" ];\n", |
| 64 |
pid(), d, pid(), |
| 65 |
str_replace(saved_exe_name, "\"", ""), |
| 66 |
dot_escape(cmdline_str())) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
probe end |
| 71 |
{ |
| 72 |
printf("}\n") |
| 73 |
} |