From: Shankara Pailoor Date: Sat, 12 Jan 2019 03:07:33 +0000 (-0800) Subject: Honor xlat styles when decoding resource limits X-Git-Tag: v5.0~54 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ab9944f03cbee7fa8326517bb9afe371f6241094;p=strace Honor xlat styles when decoding resource limits * resource.c (sprint_rlim64, sprint_rlim32): Remove. (print_rlim64_t, print_rlim32_t): New functions. (print_rlimit64, print_rlimit32): Use them. * tests/setrlimit-Xabbrev.c: New file. * tests/setrlimit-Xraw.c: Likewise. * tests/setrlimit-Xverbose.c: Likewise. * tests/setrlimit.c (main): Handle XLAT_ABBREV, XLAT_RAW, and XLAT_VERBOSE macros. * tests/xgetrlimit.c (sprint_rlim): Likewise. * tests/gen_tests.in (setrlimit-Xabbrev, setrlimit-Xraw, setrlimit-Xverbose): New tests. * tests/pure_executables.list: Add setrlimit-Xabbrev, setrlimit-Xraw, and setrlimit-Xverbose. * tests/.gitignore: Likewise. Co-Authored-by: Dmitry V. Levin --- diff --git a/resource.c b/resource.c index 724223ef..259840a6 100644 --- a/resource.c +++ b/resource.c @@ -16,19 +16,27 @@ #include "xlat/resources.h" -static const char * -sprint_rlim64(uint64_t lim) -{ - static char buf[sizeof(uint64_t)*3 + sizeof("*1024")]; +static void +print_rlim64_t(uint64_t lim) { + const char *str = NULL; if (lim == UINT64_MAX) - return "RLIM64_INFINITY"; + str = "RLIM64_INFINITY"; + else if (lim > 1024 && lim % 1024 == 0) { + static char buf[sizeof(lim) * 3 + sizeof("*1024")]; - if (lim > 1024 && lim % 1024 == 0) xsprintf(buf, "%" PRIu64 "*1024", lim / 1024); - else - xsprintf(buf, "%" PRIu64, lim); - return buf; + str = buf; + } + + if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) + tprintf("%" PRIu64, lim); + + if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW) + return; + + (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE + ? tprints_comment : tprints)(str); } static void @@ -40,26 +48,37 @@ 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_rlim64_t(rlim.rlim_cur); + tprints(", rlim_max="); + print_rlim64_t(rlim.rlim_max); + tprints("}"); } } #if !defined(current_wordsize) || current_wordsize == 4 -static const char * -sprint_rlim32(uint32_t lim) -{ - static char buf[sizeof(uint32_t)*3 + sizeof("*1024")]; +static void +print_rlim32_t(uint32_t lim) { + const char *str = NULL; if (lim == UINT32_MAX) - return "RLIM_INFINITY"; + str = "RLIM_INFINITY"; + else if (lim > 1024 && lim % 1024 == 0) { + static char buf[sizeof(lim) * 3 + sizeof("*1024")]; - if (lim > 1024 && lim % 1024 == 0) xsprintf(buf, "%" PRIu32 "*1024", lim / 1024); - else - xsprintf(buf, "%" PRIu32, lim); - return buf; + str = buf; + } + + if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) + tprintf("%" PRIu32, lim); + + 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 +90,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_rlim32_t(rlim.rlim_cur); + tprints(", rlim_max="); + print_rlim32_t(rlim.rlim_max); + tprints("}"); } } diff --git a/tests/.gitignore b/tests/.gitignore index eb08d8f9..5fd5c0a9 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -502,6 +502,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 9cb21491..b9886565 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 -a27 -e trace=setrlimit -Xabbrev +setrlimit-Xraw -a19 -e trace=setrlimit -Xraw +setrlimit-Xverbose -a36 -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..a128d5a8 100644 --- a/tests/setrlimit.c +++ b/tests/setrlimit.c @@ -23,7 +23,16 @@ 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 struct rlimit libc_rlim = {}; if (getrlimit((int) res, &libc_rlim)) @@ -33,10 +42,23 @@ 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 } puts("+++ exited with 0 +++"); diff --git a/tests/xgetrlimit.c b/tests/xgetrlimit.c index b807f290..deaad254 100644 --- a/tests/xgetrlimit.c +++ b/tests/xgetrlimit.c @@ -19,23 +19,55 @@ 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 unsigned int i; + + i &= 1; + +#if XLAT_RAW + 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 + sprintf(buf[i], "%llu /* RLIM64_INFINITY */", + (unsigned long long) lim); + return buf[i++]; +# else /* XLAT_ABBREV */ return "RLIM64_INFINITY"; +# endif + } } else { - if (lim == (kernel_ulong_t) -1U) + if (lim == (kernel_ulong_t) -1U) { +# if XLAT_VERBOSE + sprintf(buf[i], "%llu /* RLIM_INFINITY */", + (unsigned long long) lim); + return buf[i++]; +# else /* XLAT_ABBREV */ return "RLIM_INFINITY"; +# endif + } } - 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 /* XLAT_ABBREV */ sprintf(buf[i], "%llu*1024", (unsigned long long) lim / 1024); +# endif else sprintf(buf[i], "%llu", (unsigned long long) lim); return buf[i++]; +#endif /* !XLAT_RAW */ } #ifdef NR_GETRLIMIT