Handle xlat styles when printing rlimit

Dmitry V. Levin ldv at altlinux.org
Sat Feb 9 01:29:22 UTC 2019


Hi,

On Fri, Jan 11, 2019 at 07:12:11PM -0800, shankarapailoor wrote:
> Hi,
> 
> strace doesn't honor xlat_styles when printing rlimit fields. Attached
> is a patch which corrects this.
> 
> Regards,
> Shankara Pailoor

> From 89ef3e2a7634c16d78ca539cda8242e9f72f01c4 Mon Sep 17 00:00:00 2001
> From: Shankara Pailoor <shankarapailoor at gmail.com>
> Date: Fri, 11 Jan 2019 19:07:33 -0800
> Subject: [PATCH v1] handle xlat_styles when printing rlim_cur, rlim_max
> 
> * resource.c (sprint_rlimit{32, 64}, print_rlimfield{32,64},
> print_rlimit{32, 64}): handle xlat styles.
> * tests/setrlimit.c: Add XLAT_{ABBREV, RAW, VERBOSE} macros.
> * tests/xgetrlimit.c (sprint_rlim): Likewise.
> * tests/setrlimit-Xabbrev.c: Add new test for rlimit xlat_style.
> * tests/setrlimit-Xraw.c: Likewise.
> * tests/setrlimit-Xverbose.c: Likewise.
> * tests/pure_executables.list: Add setrlimit-X{abbrev, raw,
> verbose} tests.
> * tests/gen_tests.in: Likewise.
> ---
>  resource.c                  | 49 ++++++++++++++++++++++++++++++++-----
>  tests/.gitignore            |  3 +++
>  tests/gen_tests.in          |  3 +++
>  tests/pure_executables.list |  3 +++
>  tests/setrlimit.c           | 22 +++++++++++++++++
>  tests/xgetrlimit.c          | 39 +++++++++++++++++++++++++----
>  6 files changed, 108 insertions(+), 11 deletions(-)
> 
> diff --git a/resource.c b/resource.c
> index 724223ef..3572555b 100644
> --- a/resource.c
> +++ b/resource.c
> @@ -20,17 +20,31 @@ static const char *
>  sprint_rlim64(uint64_t lim)
>  {
>  	static char buf[sizeof(uint64_t)*3 + sizeof("*1024")];
> +	if ((lim != UINT64_MAX && lim % 1024 > 0) || (lim <= 1024))
> +		return NULL;

Instead of replicating the check, ...
>  
>  	if (lim == UINT64_MAX)
>  		return "RLIM64_INFINITY";
>  
>  	if (lim > 1024 && lim % 1024 == 0)
>  		xsprintf(buf, "%" PRIu64 "*1024", lim / 1024);
> -	else
> -		xsprintf(buf, "%" PRIu64, lim);

... you could just return NULL here.

Alternatively, you could inverse the check, e.g.

	if (lim <= 1024 || lim % 1024)
		return NULL;
	xsprintf(buf, "%" PRIu64 "*1024", lim / 1024);

>  	return buf;
>  }
>  
> +static void
> +print_rlimfield64(uint64_t rlim_field) {
> +	const char *str = sprint_rlim64(rlim_field);
> +
> +	if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) 
> +		tprintf("%" PRIu64, rlim_field);
> +
> +	if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
> +		return;
> +
> +	(xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
> +                ? tprints_comment : tprints)(str);
> +}
> +
>  static void
>  print_rlimit64(struct tcb *const tcp, const kernel_ulong_t addr)
>  {
> @@ -40,8 +54,11 @@ print_rlimit64(struct tcb *const tcp, const kernel_ulong_t addr)
>  	} rlim;
>  
>  	if (!umove_or_printaddr(tcp, addr, &rlim)) {
> -		tprintf("{rlim_cur=%s,", sprint_rlim64(rlim.rlim_cur));
> -		tprintf(" rlim_max=%s}", sprint_rlim64(rlim.rlim_max));
> +		tprints("{rlim_cur=");
> +		print_rlimfield64(rlim.rlim_cur);
> +		tprints(", rlim_max=");
> +		print_rlimfield64(rlim.rlim_max);
> +		tprints("}");
>  	}
>  }
>  
> @@ -52,6 +69,9 @@ sprint_rlim32(uint32_t lim)
>  {
>  	static char buf[sizeof(uint32_t)*3 + sizeof("*1024")];
>  
> +	if ((lim != UINT32_MAX && lim % 1024 > 0) || (lim <= 1024))
> +		return NULL;

Likewise, there is no need to replicate the check.

>  	if (lim == UINT32_MAX)
>  		return "RLIM_INFINITY";
>  
> @@ -62,6 +82,20 @@ sprint_rlim32(uint32_t lim)
>  	return buf;
>  }
>  
> +static void
> +print_rlimfield32(uint32_t rlim_field) {
> +	const char *str = sprint_rlim32(rlim_field);
> +
> +	if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) 
> +		tprintf("%" PRIu32, rlim_field);
> +
> +	if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
> +		return;
> +
> +	(xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
> +                ? tprints_comment : tprints)(str);
> +}
> +
>  static void
>  print_rlimit32(struct tcb *const tcp, const kernel_ulong_t addr)
>  {
> @@ -71,8 +105,11 @@ print_rlimit32(struct tcb *const tcp, const kernel_ulong_t addr)
>  	} rlim;
>  
>  	if (!umove_or_printaddr(tcp, addr, &rlim)) {
> -		tprintf("{rlim_cur=%s,", sprint_rlim32(rlim.rlim_cur));
> -		tprintf(" rlim_max=%s}", sprint_rlim32(rlim.rlim_max));
> +		tprints("{rlim_cur=");
> +		print_rlimfield32(rlim.rlim_cur);
> +		tprints(", rlim_max=");
> +		print_rlimfield32(rlim.rlim_max);
> +		tprints("}");
>  	}
>  }
>  
> diff --git a/tests/.gitignore b/tests/.gitignore
> index 24b17017..327a75cf 100644
> --- a/tests/.gitignore
> +++ b/tests/.gitignore
> @@ -501,6 +501,9 @@ setresuid32
>  setreuid
>  setreuid32
>  setrlimit
> +setrlimit-Xabbrev
> +setrlimit-Xraw
> +setrlimit-Xverbose
>  setuid
>  setuid32
>  shmxt
> diff --git a/tests/gen_tests.in b/tests/gen_tests.in
> index e2e70a12..1c11d2da 100644
> --- a/tests/gen_tests.in
> +++ b/tests/gen_tests.in
> @@ -414,6 +414,9 @@ setresuid32	-a21
>  setreuid	-a15
>  setreuid32	-a17
>  setrlimit	-a27
> +setrlimit-Xabbrev	-a1 -e trace=setrlimit -Xabbrev
> +setrlimit-Xraw	-a1 -e trace=setrlimit -Xraw
> +setrlimit-Xverbose	-a1 -e trace=setrlimit -Xverbose
>  setuid	-a10
>  setuid32	-a12
>  shmxt	-a11 -e trace='/(osf_)?shmat,shmdt'
> diff --git a/tests/pure_executables.list b/tests/pure_executables.list
> index 23aabb9e..616ee9ed 100755
> --- a/tests/pure_executables.list
> +++ b/tests/pure_executables.list
> @@ -420,6 +420,9 @@ setresuid32
>  setreuid
>  setreuid32
>  setrlimit
> +setrlimit-Xabbrev
> +setrlimit-Xraw
> +setrlimit-Xverbose
>  setuid
>  setuid32
>  shmxt
> diff --git a/tests/setrlimit.c b/tests/setrlimit.c
> index f199abe1..084e7beb 100644
> --- a/tests/setrlimit.c
> +++ b/tests/setrlimit.c
> @@ -23,7 +23,14 @@ main(void)
>  	for (xlat = resources; xlat->str; ++xlat) {
>  		unsigned long res = 0xfacefeed00000000ULL | xlat->val;
>  		long rc = syscall(__NR_setrlimit, res, 0);
> +#if XLAT_RAW
> +		printf("setrlimit(%#lx, NULL) = %s\n", xlat->val, sprintrc(rc));
> +#elif XLAT_VERBOSE
> +		printf("setrlimit(%#lx /* %s */, NULL) = %s\n", xlat->val,
> +		       xlat->str, sprintrc(rc));
> +#else
>  		printf("setrlimit(%s, NULL) = %s\n", xlat->str, sprintrc(rc));
> +#endif
>  
>  		struct rlimit libc_rlim = {};
>  		if (getrlimit((int) res, &libc_rlim))
> @@ -33,10 +40,25 @@ main(void)
>  
>  		rc = syscall(__NR_setrlimit, res, rlimit);
>  		const char *errstr = sprintrc(rc);
> +#if XLAT_RAW
> +		printf("setrlimit(%#lx, {rlim_cur=%s, rlim_max=%s}) = %s\n",
> +		       xlat->val,
> +		       sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1]),
> +		       errstr);
> +#elif XLAT_VERBOSE
> +		printf("setrlimit(%#lx /* %s */,"
> +		       " {rlim_cur=%s, rlim_max=%s}) = %s\n",
> +		       xlat->val,
> +		       xlat->str,
> +		       sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1]),
> +		       errstr);
> +#else
>  		printf("setrlimit(%s, {rlim_cur=%s, rlim_max=%s}) = %s\n",
>  		       xlat->str,
>  		       sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1]),
>  		       errstr);
> +#endif
> +
>  	}
>  
>  	puts("+++ exited with 0 +++");
> diff --git a/tests/xgetrlimit.c b/tests/xgetrlimit.c
> index b807f290..3efd4baf 100644
> --- a/tests/xgetrlimit.c
> +++ b/tests/xgetrlimit.c
> @@ -18,24 +18,53 @@
>  
>  const char *
>  sprint_rlim(kernel_ulong_t lim)
> -{
> +{	
> +	static char buf[2][1024+sizeof(lim)*3 + sizeof("*1024")];

Why 1024?


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20190209/7243b6b0/attachment.bin>


More information about the Strace-devel mailing list