From: Dmitry V. Levin Date: Sun, 2 Jul 2017 00:31:50 +0000 (+0000) Subject: Use program_invocation_name instead of a local progname variable X-Git-Tag: v4.18~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=340b00aaa7330c74be7c5c2bc9533c613579bd50;p=strace Use program_invocation_name instead of a local progname variable Emulate program_invocation_name only if it is not provided by libc. * configure.ac: Check for program_invocation_name variable. * strace.c (progname): Remove. [!HAVE_PROGRAM_INVOCATION_NAME] (program_invocation_name): New variable. (verror_msg, error_msg_and_help): Use it instead of progname. (init): Initialize program_invocation_name instead of progname. --- diff --git a/configure.ac b/configure.ac index bc7b637d..eb7b0e78 100644 --- a/configure.ac +++ b/configure.ac @@ -709,6 +709,16 @@ if test "x$st_cv_have___builtin_popcount" = xyes; then [Define to 1 if the system provides __builtin_popcount function]) fi +AC_CACHE_CHECK([for program_invocation_name], [st_cv_have_program_invocation_name], + [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[return !*program_invocation_name]])], + [st_cv_have_program_invocation_name=yes], + [st_cv_have_program_invocation_name=no])]) +if test "x$st_cv_have_program_invocation_name" = xyes; then + AC_DEFINE([HAVE_PROGRAM_INVOCATION_NAME], [1], + [Define to 1 if the system provides program_invocation_name variable]) +fi + AC_CHECK_LIB([dl], [dladdr], [dl_LIBS='-ldl'], [dl_LIBS=]) if test "x$ac_cv_lib_dl_dladdr" = xyes; then AC_DEFINE([HAVE_DLADDR], [1], [Define to 1 if the system provides dladdr]) diff --git a/strace.c b/strace.c index 2bb4ec15..5ece0247 100644 --- a/strace.c +++ b/strace.c @@ -151,7 +151,10 @@ static struct tcb *current_tcp; static struct tcb **tcbtab; static unsigned int nprocs, tcbtabsize; -static const char *progname; + +#ifndef HAVE_PROGRAM_INVOCATION_NAME +char *program_invocation_name; +#endif unsigned os_release; /* generated from uname()'s u.release */ @@ -298,13 +301,15 @@ static void verror_msg(int err_no, const char *fmt, va_list p) msg = NULL; if (vasprintf(&msg, fmt, p) >= 0) { if (err_no) - fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no)); + fprintf(stderr, "%s: %s: %s\n", + program_invocation_name, msg, strerror(err_no)); else - fprintf(stderr, "%s: %s\n", progname, msg); + fprintf(stderr, "%s: %s\n", + program_invocation_name, msg); free(msg); } else { /* malloc in vasprintf failed, try it without malloc */ - fprintf(stderr, "%s: ", progname); + fprintf(stderr, "%s: ", program_invocation_name); vfprintf(stderr, fmt, p); if (err_no) fprintf(stderr, ": %s\n", strerror(err_no)); @@ -339,7 +344,8 @@ void error_msg_and_help(const char *fmt, ...) va_start(p, fmt); verror_msg(0, fmt, p); } - fprintf(stderr, "Try '%s -h' for more information.\n", progname); + fprintf(stderr, "Try '%s -h' for more information.\n", + program_invocation_name); die(); } @@ -1617,7 +1623,11 @@ init(int argc, char *argv[]) int c, i; int optF = 0; - progname = argv[0] ? argv[0] : "strace"; + if (!program_invocation_name || !*program_invocation_name) { + static char name[] = "strace"; + program_invocation_name = + (argv[0] && *argv[0]) ? argv[0] : name; + } strace_tracer_pid = getpid();