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

Sahil Siddiq icegambit91 at gmail.com
Tue Feb 14 21:24:00 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>
---
 src/strace.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/strace.c b/src/strace.c
index 4e710db80..bdfa705cf 100644
--- a/src/strace.c
+++ b/src/strace.c
@@ -84,6 +84,7 @@ int Tflag_scale = 1000;
 int Tflag_width = 6;
 bool iflag;
 bool nflag;
+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