179894
commit 86f65dffc2396d408beb628f1cad2b8f63e197bd
179894
Author: H.J. Lu <hjl.tools@gmail.com>
179894
Date:   Sun Jul 12 06:04:53 2020 -0700
179894
179894
    ld.so: Add --list-tunables to print tunable values
179894
    
179894
    Pass --list-tunables to ld.so to print tunables with min and max values.
179894
    
179894
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
179894
179894
Conflicts:
179894
	elf/Makefile
179894
	  (different backporting order)
179894
179894
diff --git a/elf/Makefile b/elf/Makefile
179894
index 3e71939d3234c4c3..aa65ec59f143bccf 100644
179894
--- a/elf/Makefile
179894
+++ b/elf/Makefile
179894
@@ -44,6 +44,10 @@ dl-routines += dl-tunables
179894
 tunables-type = $(addprefix TUNABLES_FRONTEND_,$(have-tunables))
179894
 CPPFLAGS-dl-tunables.c += -DTUNABLES_FRONTEND=$(tunables-type)
179894
 
179894
+ifeq (yesyes,$(build-shared)$(run-built-tests))
179894
+tests-special += $(objpfx)list-tunables.out
179894
+endif
179894
+
179894
 # Make sure that the compiler does not insert any library calls in tunables
179894
 # code paths.
179894
 ifeq (yes,$(have-loop-to-function))
179894
@@ -1825,6 +1829,13 @@ $(objpfx)tst-glibc-hwcaps-mask.out: \
179894
 # tst-glibc-hwcaps-cache.
179894
 $(objpfx)tst-glibc-hwcaps-cache.out: $(objpfx)tst-glibc-hwcaps
179894
 
179894
+$(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so
179894
+	$(SHELL) $< $(objpfx)ld.so '$(test-wrapper-env)' \
179894
+	    '$(run_program_env)' > $(objpfx)/tst-rtld-list-tunables.out
179894
+	cmp tst-rtld-list-tunables.exp \
179894
+	    $(objpfx)/tst-rtld-list-tunables.out > $@; \
179894
+	$(evaluate-test)
179894
+
179894
 tst-dst-static-ENV = LD_LIBRARY_PATH='$$ORIGIN'
179894
 
179894
 $(objpfx)tst-rtld-help.out: $(objpfx)ld.so
179894
diff --git a/elf/dl-main.h b/elf/dl-main.h
179894
index 566713a0d10cfdb7..9e7b51d8f010e904 100644
179894
--- a/elf/dl-main.h
179894
+++ b/elf/dl-main.h
179894
@@ -63,7 +63,7 @@ struct audit_list
179894
 enum rtld_mode
179894
   {
179894
     rtld_mode_normal, rtld_mode_list, rtld_mode_verify, rtld_mode_trace,
179894
-    rtld_mode_help,
179894
+    rtld_mode_list_tunables, rtld_mode_help,
179894
   };
179894
 
179894
 /* Aggregated state information extracted from environment variables
179894
diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
179894
index bbc3679e3564a766..3c84809d44381241 100644
179894
--- a/elf/dl-tunables.c
179894
+++ b/elf/dl-tunables.c
179894
@@ -26,6 +26,7 @@
179894
 #include <sysdep.h>
179894
 #include <fcntl.h>
179894
 #include <ldsodefs.h>
179894
+#include <array_length.h>
179894
 
179894
 #define TUNABLES_INTERNAL 1
179894
 #include "dl-tunables.h"
179894
@@ -359,6 +360,48 @@ __tunables_init (char **envp)
179894
     }
179894
 }
179894
 
179894
+void
179894
+__tunables_print (void)
179894
+{
179894
+  for (int i = 0; i < array_length (tunable_list); i++)
179894
+    {
179894
+      const tunable_t *cur = &tunable_list[i];
179894
+      if (cur->type.type_code == TUNABLE_TYPE_STRING
179894
+	  && cur->val.strval == NULL)
179894
+	_dl_printf ("%s:\n", cur->name);
179894
+      else
179894
+	{
179894
+	  _dl_printf ("%s: ", cur->name);
179894
+	  switch (cur->type.type_code)
179894
+	    {
179894
+	    case TUNABLE_TYPE_INT_32:
179894
+	      _dl_printf ("%d (min: %d, max: %d)\n",
179894
+			  (int) cur->val.numval,
179894
+			  (int) cur->type.min,
179894
+			  (int) cur->type.max);
179894
+	      break;
179894
+	    case TUNABLE_TYPE_UINT_64:
179894
+	      _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
179894
+			  (long int) cur->val.numval,
179894
+			  (long int) cur->type.min,
179894
+			  (long int) cur->type.max);
179894
+	      break;
179894
+	    case TUNABLE_TYPE_SIZE_T:
179894
+	      _dl_printf ("0x%Zx (min: 0x%Zx, max: 0x%Zx)\n",
179894
+			  (size_t) cur->val.numval,
179894
+			  (size_t) cur->type.min,
179894
+			  (size_t) cur->type.max);
179894
+	      break;
179894
+	    case TUNABLE_TYPE_STRING:
179894
+	      _dl_printf ("%s\n", cur->val.strval);
179894
+	      break;
179894
+	    default:
179894
+	      __builtin_unreachable ();
179894
+	    }
179894
+	}
179894
+    }
179894
+}
179894
+
179894
 /* Set the tunable value.  This is called by the module that the tunable exists
179894
    in. */
179894
 void
179894
diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h
179894
index 7f181f3316cd9fc1..f4f2cfaeb9828599 100644
179894
--- a/elf/dl-tunables.h
179894
+++ b/elf/dl-tunables.h
179894
@@ -69,9 +69,11 @@ typedef struct _tunable tunable_t;
179894
 # include "dl-tunable-list.h"
179894
 
179894
 extern void __tunables_init (char **);
179894
+extern void __tunables_print (void);
179894
 extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
179894
 extern void __tunable_set_val (tunable_id_t, void *);
179894
 rtld_hidden_proto (__tunables_init)
179894
+rtld_hidden_proto (__tunables_print)
179894
 rtld_hidden_proto (__tunable_get_val)
179894
 
179894
 /* Define TUNABLE_GET and TUNABLE_SET in short form if TOP_NAMESPACE and
179894
diff --git a/elf/dl-usage.c b/elf/dl-usage.c
179894
index e22a9c39427187d1..908b4894b3014b2d 100644
179894
--- a/elf/dl-usage.c
179894
+++ b/elf/dl-usage.c
179894
@@ -255,7 +255,12 @@ setting environment variables (which would be inherited by subprocesses).\n\
179894
                         in LIST\n\
179894
   --audit LIST          use objects named in LIST as auditors\n\
179894
   --preload LIST        preload objects named in LIST\n\
179894
-  --argv0 STRING        set argv[0] to STRING before running\n\
179894
+  --argv0 STRING        set argv[0] to STRING before running\n"
179894
+#if HAVE_TUNABLES
179894
+"\
179894
+  --list-tunables       list all tunables with minimum and maximum values\n"
179894
+#endif
179894
+"\
179894
   --help                display this help and exit\n\
179894
   --version             output version information and exit\n\
179894
 \n\
179894
diff --git a/elf/rtld.c b/elf/rtld.c
179894
index 9e09896da078274d..54b621ec5ca014fa 100644
179894
--- a/elf/rtld.c
179894
+++ b/elf/rtld.c
179894
@@ -47,6 +47,7 @@
179894
 #include <libc-early-init.h>
179894
 #include <dl-main.h>
179894
 #include <gnu/lib-names.h>
179894
+#include <dl-tunables.h>
179894
 
179894
 #include <assert.h>
179894
 
179894
@@ -1262,6 +1263,16 @@ dl_main (const ElfW(Phdr) *phdr,
179894
 	    _dl_argc -= 2;
179894
 	    _dl_argv += 2;
179894
 	  }
179894
+#if HAVE_TUNABLES
179894
+	else if (! strcmp (_dl_argv[1], "--list-tunables"))
179894
+	  {
179894
+	    state.mode = rtld_mode_list_tunables;
179894
+
179894
+	    ++_dl_skip_args;
179894
+	    --_dl_argc;
179894
+	    ++_dl_argv;
179894
+	  }
179894
+#endif
179894
 	else if (strcmp (_dl_argv[1], "--help") == 0)
179894
 	  {
179894
 	    state.mode = rtld_mode_help;
179894
@@ -1282,6 +1293,14 @@ dl_main (const ElfW(Phdr) *phdr,
179894
 	else
179894
 	  break;
179894
 
179894
+#if HAVE_TUNABLES
179894
+      if (__glibc_unlikely (state.mode == rtld_mode_list_tunables))
179894
+	{
179894
+	  __tunables_print ();
179894
+	  _exit (0);
179894
+	}
179894
+#endif
179894
+
179894
       /* If we have no further argument the program was called incorrectly.
179894
 	 Grant the user some education.  */
179894
       if (_dl_argc < 2)
179894
diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
179894
new file mode 100644
179894
index 0000000000000000..4f3f7ee4e30a2b42
179894
--- /dev/null
179894
+++ b/elf/tst-rtld-list-tunables.exp
179894
@@ -0,0 +1,14 @@
179894
+glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0x[f]+)
179894
+glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0x[f]+)
179894
+glibc.malloc.check: 0 (min: 0, max: 3)
179894
+glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
179894
+glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0x[f]+)
179894
+glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0x[f]+)
179894
+glibc.malloc.perturb: 0 (min: 0, max: 255)
179894
+glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0x[f]+)
179894
+glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0x[f]+)
179894
+glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0x[f]+)
179894
+glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0x[f]+)
179894
+glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+)
179894
+glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
179894
+glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0x[f]+)
179894
diff --git a/elf/tst-rtld-list-tunables.sh b/elf/tst-rtld-list-tunables.sh
179894
new file mode 100755
179894
index 0000000000000000..e7bbdde94952b872
179894
--- /dev/null
179894
+++ b/elf/tst-rtld-list-tunables.sh
179894
@@ -0,0 +1,34 @@
179894
+#!/bin/sh
179894
+# Test for --list-tunables option ld.so.
179894
+# Copyright (C) 2021 Free Software Foundation, Inc.
179894
+# This file is part of the GNU C Library.
179894
+#
179894
+# The GNU C Library is free software; you can redistribute it and/or
179894
+# modify it under the terms of the GNU Lesser General Public
179894
+# License as published by the Free Software Foundation; either
179894
+# version 2.1 of the License, or (at your option) any later version.
179894
+#
179894
+# The GNU C Library is distributed in the hope that it will be useful,
179894
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
179894
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
179894
+# Lesser General Public License for more details.
179894
+#
179894
+# You should have received a copy of the GNU Lesser General Public
179894
+# License along with the GNU C Library; if not, see
179894
+# <https://www.gnu.org/licenses/>.
179894
+
179894
+set -e
179894
+
179894
+rtld=$1
179894
+test_wrapper_env=$2
179894
+run_program_env=$3
179894
+
179894
+LC_ALL=C
179894
+export LC_ALL
179894
+
179894
+${test_wrapper_env} \
179894
+${run_program_env} \
179894
+$rtld --list-tunables \
179894
+| sort -u \
179894
+| egrep "(rtld|malloc)" \
179894
+| sed -e "s/0xf\+/0x[f]+/"
179894
diff --git a/manual/tunables.texi b/manual/tunables.texi
179894
index 07887981748bc44b..43272cf885d1e3e6 100644
179894
--- a/manual/tunables.texi
179894
+++ b/manual/tunables.texi
179894
@@ -28,6 +28,44 @@ Finally, the set of tunables available may vary between distributions as
179894
 the tunables feature allows distributions to add their own tunables under
179894
 their own namespace.
179894
 
179894
+Passing @option{--list-tunables} to the dynamic loader to print all
179894
+tunables with minimum and maximum values:
179894
+
179894
+@example
179894
+$ /lib64/ld-linux-x86-64.so.2 --list-tunables
179894
+glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
179894
+glibc.elision.skip_lock_after_retries: 3 (min: -2147483648, max: 2147483647)
179894
+glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.malloc.perturb: 0 (min: 0, max: 255)
179894
+glibc.cpu.x86_shared_cache_size: 0x100000 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.mem.tagging: 0 (min: 0, max: 255)
179894
+glibc.elision.tries: 3 (min: -2147483648, max: 2147483647)
179894
+glibc.elision.enable: 0 (min: 0, max: 1)
179894
+glibc.cpu.x86_rep_movsb_threshold: 0x1000 (min: 0x100, max: 0xffffffffffffffff)
179894
+glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.elision.skip_lock_busy: 3 (min: -2147483648, max: 2147483647)
179894
+glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.cpu.x86_rep_stosb_threshold: 0x800 (min: 0x1, max: 0xffffffffffffffff)
179894
+glibc.cpu.x86_non_temporal_threshold: 0xc0000 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.cpu.x86_shstk:
179894
+glibc.cpu.hwcap_mask: 0x6 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
179894
+glibc.elision.skip_trylock_internal_abort: 3 (min: -2147483648, max: 2147483647)
179894
+glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.cpu.x86_ibt:
179894
+glibc.cpu.hwcaps:
179894
+glibc.elision.skip_lock_internal_abort: 3 (min: -2147483648, max: 2147483647)
179894
+glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0xffffffffffffffff)
179894
+glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.cpu.x86_data_cache_size: 0x8000 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0xffffffffffffffff)
179894
+glibc.pthread.mutex_spin_count: 100 (min: 0, max: 32767)
179894
+glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0xffffffffffffffff)
179894
+glibc.malloc.check: 0 (min: 0, max: 3)
179894
+@end example
179894
+
179894
 @menu
179894
 * Tunable names::  The structure of a tunable name
179894
 * Memory Allocation Tunables::  Tunables in the memory allocation subsystem