[PATCH v2 1/2] [GSoC] Stop condition based on number of syscalls

Sahil Siddiq icegambit91 at gmail.com
Wed Feb 15 18:07:05 UTC 2023


Add option to detach strace after capturing a
specified number of syscalls.

* src/strace.c: Implementation of -l/--syscall-limit option

Signed-off-by: Sahil Siddiq <icegambit91 at gmail.com>
---
Changes since v1:
 - Declare "nflag" and "syscall_limit" with the static modifier.

 src/strace.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/strace.c b/src/strace.c
index 4e710db80..162c802ab 100644
--- a/src/strace.c
+++ b/src/strace.c
@@ -83,7 +83,8 @@ bool Tflag;
 int Tflag_scale = 1000;
 int Tflag_width = 6;
 bool iflag;
-bool nflag;
+static bool nflag;
+static int syscall_limit = -1;
 bool count_wallclock;
 static int tflag_scale = 1000000000;
 static unsigned tflag_width = 0;
@@ -461,6 +462,10 @@ Statistics:\n\
   -w, --summary-wall-clock\n\
                  summarise syscall latency (default is system time)\n\
 \n\
+Stop condition:\n\
+  -l LIMIT, --syscall-limit=LIMIT\n\
+                 Detach tracer after tracing LIMIT syscalls\n\
+\n\
 Tampering:\n\
   -e inject=SET[:error=ERRNO|:retval=VALUE][:signal=SIG][:syscall=SYSCALL]\n\
             [:delay_enter=DELAY][:delay_exit=DELAY]\n\
@@ -2245,7 +2250,7 @@ init(int argc, char *argv[])
 #endif
 
 	static const char optstring[] =
-		"+a:Ab:cCdDe:E:fFhiI:kno:O:p:P:qrs:S:tTu:U:vVwxX:yYzZ";
+		"+a:Ab:cCdDe:E:fFhiI:kl:no:O:p:P:qrs:S:tTu:U:vVwxX:yYzZ";
 
 	enum {
 		GETOPT_SECCOMP = 0x100,
@@ -2291,6 +2296,7 @@ init(int argc, char *argv[])
 		{ "instruction-pointer", no_argument,      0, 'i' },
 		{ "interruptible",	required_argument, 0, 'I' },
 		{ "stack-traces",	no_argument,	   0, 'k' },
+		{ "syscall-limit",	required_argument, 0, 'l' },
 		{ "syscall-number",	no_argument,	   0, 'n' },
 		{ "output",		required_argument, 0, 'o' },
 		{ "summary-syscall-overhead", required_argument, 0, 'O' },
@@ -2432,6 +2438,11 @@ init(int argc, char *argv[])
 					  "build of strace");
 #endif
 			break;
+		case 'l':
+			syscall_limit = string_to_uint(optarg);
+			if (syscall_limit <= 0)
+				error_opt_arg(c, lopt, optarg);
+			break;
 		case 'n':
 			nflag = 1;
 			break;
@@ -3646,6 +3657,8 @@ trace_syscall(struct tcb *tcp, unsigned int *sig)
 		int res = syscall_exiting_decode(tcp, &ts);
 		if (res != 0) {
 			res = syscall_exiting_trace(tcp, &ts, res);
+			if (syscall_limit != -1)
+				syscall_limit--;
 		}
 		syscall_exiting_finish(tcp);
 		return res;
@@ -3716,6 +3729,8 @@ dispatch_event(const struct tcb_wait_data *wd)
 			 */
 			return true;
 		}
+		if (syscall_limit == 0)
+			return false;
 		if (has_seccomp_filter(current_tcp)) {
 			/*
 			 * Syscall and seccomp stops can happen in different
-- 
2.39.1



More information about the Strace-devel mailing list