Handle xlat styles when printing rlimit

Dmitry V. Levin ldv at altlinux.org
Sun Feb 17 23:23:29 UTC 2019


On Sun, Feb 17, 2019 at 03:12:14PM -0800, shankarapailoor wrote:
> Subject: [PATCH v3] 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           | 22 ++++++++++++++
>  tests/xgetrlimit.c          | 42 ++++++++++++++++++++++++---
>  9 files changed, 120 insertions(+), 16 deletions(-)
>  create mode 100644 tests/setrlimit-Xabbrev.c
>  create mode 100644 tests/setrlimit-Xraw.c
>  create mode 100644 tests/setrlimit-Xverbose.c
> 
> diff --git a/resource.c b/resource.c
> index 724223ef..0ebf25a8 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) 

Trailing whitespace here.

> +		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
> @@ -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) 

Trailing whitespace here.

> +		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
> @@ -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
>  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..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));

In file included from setrlimit-Xraw.c:2:
setrlimit.c: In function 'main':
setrlimit.c:27:24: error: format '%lx' expects argument of type 'long unsigned int', but argument 2 has type 'uint64_t' {aka 'const long long unsigned int'} [-Werror=format=]
   printf("setrlimit(%#lx, NULL) = %s\n", xlat->val, sprintrc(rc));
                     ~~~^                 ~~~~~~~~~
                     %#llx

> +#elif XLAT_VERBOSE
> +		printf("setrlimit(%#lx /* %s */, NULL) = %s\n", xlat->val,
> +		       xlat->str, sprintrc(rc));

In file included from setrlimit-Xverbose.c:2:
setrlimit.c: In function 'main':
setrlimit.c:29:24: error: format '%lx' expects argument of type 'long unsigned int', but argument 2 has type '
uint64_t' {aka 'const long long unsigned int'} [-Werror=format=]
   printf("setrlimit(%#lx /* %s */, NULL) = %s\n", xlat->val,
                     ~~~^                          ~~~~~~~~~
                     %#llx

> +#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);

setrlimit.c:44:24: error: format '%lx' expects argument of type 'long unsigned int', but argument 2 has type 'uint64_t' {aka 'const long long unsigned int'} [-Werror=format=]
   printf("setrlimit(%#lx, {rlim_cur=%s, rlim_max=%s}) = %s\n",
                     ~~~^
                     %#llx
          xlat->val,
          ~~~~~~~~~      

> +#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);

setrlimit.c:49:24: error: format '%lx' expects argument of type 'long unsigned int', but argument 2 has type '
uint64_t' {aka 'const long long unsigned int'} [-Werror=format=]
   printf("setrlimit(%#lx /* %s */,"
                     ~~~^
                     %#llx
../../tests/setrlimit.c:51:10:
          xlat->val,
          ~~~~~~~~~      


-- 
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/5740d183/attachment.bin>


More information about the Strace-devel mailing list