Handle xlat styles when printing rlimit

Dmitry V. Levin ldv at altlinux.org
Mon Feb 18 01:30:14 UTC 2019


On Sun, Feb 17, 2019 at 03:59:51PM -0800, shankarapailoor wrote:
> Subject: [PATCH v4] 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                  | 58 +++++++++++++++++++++++++++++--------
>  tests/.gitignore            |  3 ++
>  tests/gen_tests.in          |  3 ++
>  tests/pure_executables.list |  3 ++
>  tests/setrlimit-Xabbrev.c   |  1 +
>  tests/setrlimit-Xraw.c      |  2 ++
>  tests/setrlimit-Xverbose.c  |  2 ++
>  tests/setrlimit.c           | 23 +++++++++++++++
>  tests/xgetrlimit.c          | 42 ++++++++++++++++++++++++---
>  9 files changed, 121 insertions(+), 16 deletions(-)
>  create mode 100644 tests/setrlimit-Xabbrev.c
>  create mode 100644 tests/setrlimit-Xraw.c
>  create mode 100644 tests/setrlimit-Xverbose.c

Merged with some adjustments, thanks.

> diff --git a/resource.c b/resource.c
> index 724223ef..b9853530 100644
> --- a/resource.c
> +++ b/resource.c
> @@ -24,11 +24,25 @@ sprint_rlim64(uint64_t lim)
>  	if (lim == UINT64_MAX)
>  		return "RLIM64_INFINITY";
>  
> -	if (lim > 1024 && lim % 1024 == 0)
> +	if (lim > 1024 && lim % 1024 == 0) {
>  		xsprintf(buf, "%" PRIu64 "*1024", lim / 1024);
> -	else
> -		xsprintf(buf, "%" PRIu64, lim);
> -	return buf;
> +		return buf;
> +	}
> +	return NULL;
> +}
> +
> +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);
>  }

I cannot say that the name of this new function is self-explanatory.
Given that sprint_rlim64 is now used just once inside this function,
I merged both into a new function called print_rlim64_t.

>  static void
> @@ -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("}");
>  	}
>  }
>  
> @@ -55,11 +72,25 @@ sprint_rlim32(uint32_t lim)
>  	if (lim == UINT32_MAX)
>  		return "RLIM_INFINITY";
>  
> -	if (lim > 1024 && lim % 1024 == 0)
> +	if (lim > 1024 && lim % 1024 == 0) {
>  		xsprintf(buf, "%" PRIu32 "*1024", lim / 1024);
> -	else
> -		xsprintf(buf, "%" PRIu32, lim);
> -	return buf;
> +		return buf;
> +	}
> +	return NULL;
> +}
> +
> +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);
>  }

Likewise, I merged both into a new function called print_rlim32_t.

>  static void
> @@ -71,8 +102,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

I changed -a1 to something else.

>  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-Xabbrev.c b/tests/setrlimit-Xabbrev.c
> new file mode 100644
> index 00000000..d5cf2ad3
> --- /dev/null
> +++ b/tests/setrlimit-Xabbrev.c
> @@ -0,0 +1 @@
> +#include "setrlimit.c"
> diff --git a/tests/setrlimit-Xraw.c b/tests/setrlimit-Xraw.c
> new file mode 100644
> index 00000000..1a5ead0b
> --- /dev/null
> +++ b/tests/setrlimit-Xraw.c
> @@ -0,0 +1,2 @@
> +#define XLAT_RAW 1
> +#include "setrlimit.c"
> diff --git a/tests/setrlimit-Xverbose.c b/tests/setrlimit-Xverbose.c
> new file mode 100644
> index 00000000..20cd135f
> --- /dev/null
> +++ b/tests/setrlimit-Xverbose.c
> @@ -0,0 +1,2 @@
> +#define XLAT_VERBOSE 1
> +#include "setrlimit.c"
> diff --git a/tests/setrlimit.c b/tests/setrlimit.c
> index f199abe1..f1bb12d1 100644
> --- a/tests/setrlimit.c
> +++ b/tests/setrlimit.c
> @@ -23,7 +23,15 @@ 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(%#x, NULL) = %s\n",
> +			(unsigned int) xlat->val, sprintrc(rc));
> +#elif XLAT_VERBOSE
> +		printf("setrlimit(%#x /* %s */, NULL) = %s\n",
> +			(unsigned int) xlat->val, xlat->str, sprintrc(rc));
> +#else
>  		printf("setrlimit(%s, NULL) = %s\n", xlat->str, sprintrc(rc));
> +#endif

I changed indentation of preprocessor directives here

>  
>  		struct rlimit libc_rlim = {};
>  		if (getrlimit((int) res, &libc_rlim))
> @@ -33,10 +41,25 @@ main(void)
>  
>  		rc = syscall(__NR_setrlimit, res, rlimit);
>  		const char *errstr = sprintrc(rc);
> +#if XLAT_RAW
> +		printf("setrlimit(%#x, {rlim_cur=%s, rlim_max=%s}) = %s\n",
> +		       (unsigned int) xlat->val,
> +		       sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1]),
> +		       errstr);
> +#elif XLAT_VERBOSE
> +		printf("setrlimit(%#x /* %s */,"
> +		       " {rlim_cur=%s, rlim_max=%s}) = %s\n",
> +		       (unsigned int) 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
> +

... and here.

>  	}
>  
>  	puts("+++ exited with 0 +++");
> diff --git a/tests/xgetrlimit.c b/tests/xgetrlimit.c
> index b807f290..448ca9c3 100644
> --- a/tests/xgetrlimit.c
> +++ b/tests/xgetrlimit.c
> @@ -19,23 +19,57 @@
>  const char *
>  sprint_rlim(kernel_ulong_t lim)
>  {
> +	static char buf[2][ /* space for 2 llu strings */
> +				2*sizeof(lim)*3 +
> +			    /* space for XLAT_STYLE_ABBREV decoding */
> +				sizeof("*1024") + sizeof("RLIM64_INFINITY") +
> +			    /* space for C style comments */
> +				6];
> +	static int i;
> +
> +#if XLAT_RAW
> +	i &= 1;

I moved this statement to the beginning of this function.

> +	sprintf(buf[i], "%llu", (unsigned long long) lim);
> +	return buf[i++];
> +#else
>  	if (sizeof(lim) == sizeof(uint64_t)) {
> -		if (lim == (kernel_ulong_t) -1ULL)
> +		if (lim == (kernel_ulong_t) -1ULL) {
> +#if XLAT_VERBOSE
> +			i &= 1;
> +			sprintf(buf[i], "%llu /* RLIM64_INFINITY */",
> +				(unsigned long long) lim);
> +			return buf[i++];
> +#else /* XLAT_ABBREV */
>  			return "RLIM64_INFINITY";
> +#endif /* #if XLAT_VERBOSE */
> +		}
>  	} else {
> -		if (lim == (kernel_ulong_t) -1U)
> +		if (lim == (kernel_ulong_t) -1U) {
> +#if XLAT_VERBOSE
> +			i &= 1;
> +			sprintf(buf[i], "%llu /* RLIM_INFINITY */",
> +				(unsigned long long) lim);
> +			return buf[i++];
> +#else /* XLAT_ABBREV */
>  			return "RLIM_INFINITY";
> +#endif /* #if XLAT_VERBOSE */
> +		}
>  	}
>  
> -	static char buf[2][sizeof(lim)*3 + sizeof("*1024")];
> -	static int i;
>  	i &= 1;
>  	if (lim > 1024 && lim % 1024 == 0)
> +#if XLAT_VERBOSE
> +		sprintf(buf[i], "%llu /* %llu*1024 */",
> +			(unsigned long long) lim,
> +			(unsigned long long) lim / 1024);
> +#else
>  		sprintf(buf[i], "%llu*1024", (unsigned long long) lim / 1024);
> +#endif
>  	else
>  		sprintf(buf[i], "%llu", (unsigned long long) lim);
>  
>  	return buf[i++];
> +#endif /* #if XLAT_RAW */

I fixed indentation of preprocessor directives in this file.


-- 
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/20190218/705e74e2/attachment.bin>


More information about the Strace-devel mailing list