Skip to content

Commit

Permalink
execsnoop: argument to change the number of arguments parsed
Browse files Browse the repository at this point in the history
New argument to change the maximum number of arguments parsed and
displayed.
  • Loading branch information
pchaigno committed Oct 12, 2017
1 parent b334702 commit a0c9b48
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
3 changes: 3 additions & 0 deletions man/man8/execsnoop.8
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Only print command lines matching this name (regex)
.TP
\-l LINE
Only print commands where arg contains this line (regex)
.TP
\--max-args MAXARGS
Maximum number of arguments parsed and displayed, defaults to 20
.SH EXAMPLES
.TP
Trace all exec() syscalls:
Expand Down
33 changes: 9 additions & 24 deletions tools/execsnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
help="only print commands matching this name (regex), any arg")
parser.add_argument("-l", "--line",
help="only print commands where arg contains this line (regex)")
parser.add_argument("--max-args", default="20",
help="maximum number of arguments parsed and displayed, defaults to 20")
args = parser.parse_args()

# define BPF program
Expand All @@ -52,7 +54,6 @@
#include <linux/sched.h>
#include <linux/fs.h>
#define MAXARG 20
#define ARGSIZE 128
enum event_type {
Expand Down Expand Up @@ -99,28 +100,12 @@
__submit_arg(ctx, (void *)filename, &data);
int i = 1; // skip first arg, as we submitted filename
// unrolled loop to walk argv[] (MAXARG)
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++; // X
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++;
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0) goto out; i++; // XX
// skip first arg, as we submitted filename
#pragma unroll
for (int i = 1; i < MAXARG; i++) {
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0)
goto out;
}
// handle truncated argument list
char ellipsis[] = "...";
Expand All @@ -143,7 +128,7 @@
"""

# initialize BPF
b = BPF(text=bpf_text)
b = BPF(text=bpf_text.replace("MAXARG", args.max_args))

# header
if args.timestamp:
Expand Down
6 changes: 4 additions & 2 deletions tools/execsnoop_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ rpm 3345452 4146419 0 /bin/rpm -qa testpkg
USAGE message:

# ./execsnoop -h
usage: execsnoop [-h] [-t] [-x] [-n NAME]
usage: execsnoop [-h] [-t] [-x] [-n NAME] [-l LINE] [--max-args MAX_ARGS]

Trace exec() syscalls

Expand All @@ -91,10 +91,12 @@ optional arguments:
arg
-l LINE, --line LINE only print commands where arg contains this line
(regex)
--max-args MAX_ARGS maximum number of arguments parsed and displayed,
defaults to 20

examples:
./execsnoop # trace all exec() syscalls
./execsnoop -x # include failed exec()s
./execsnoop -x # include failed exec()s
./execsnoop -t # include timestamps
./execsnoop -n main # only print command lines containing "main"
./execsnoop -l tpkg # only print command where arguments contains "tpkg"

0 comments on commit a0c9b48

Please sign in to comment.