Rationale: We at NetApp added a gdb feature allowing users to specify environment variable names when invoking gdb commands 'set logging file', 'source' & 'shell'. This has been a big help especially in gdb macros since it allows macro writers to customize macro behavior with environment var settings. We've implemented this feature for the above three gdb commands but it can easily be extended to other commands such as 'set substitute-path' Usage example below. setenv LOG_DIR /tmp setenv SCRIPT_DIR "." I've created a script 'macroScript' in the current directory ("."); it is referenced in the gdb session. Now, run the gdb with the new env var feature: gdbWithEnvVarFeature .... GNU gdb (GDB) 9.1 Copyright (C) 2020 Free Software Foundation, Inc. ... (gdb) shell head ${SCRIPT_DIR}/macroScript define macro1 echo macro1\n end define macro2 echo macro2\n end macro1 macro2 gdb) source ${SCRIPT_DIR}/macroScript macro1 macro2 (gdb) set logging file ${LOG_DIR}/logFile (gdb) show logging file The current logfile is "${LOG_DIR}/logFile". (gdb) set logging on Copying output to /tmp/logFile. Copying debug output to /tmp/logFile. (gdb) help set Evaluate expression EXP and assign result to variable VAR. Usage: set VAR = EXP This uses assignment syntax appropriate for the current language (VAR = EXP or VAR := EXP for example) ... set architecture -- Set architecture of target. ... (gdb) set var $i = 1 (gdb) printf "i:%d\n", $i i:1 (gdb) shell tail ${LOG_DIR}/logFile set watchdog -- Set watchdog timer. set width -- Set number of characters where GDB should wrap lines of its output. set write -- Set writing into executable and core files. .... Command name abbreviations are allowed if unambiguous. ... i:1 The patch to gdb9.1 is here; the changed lines are bracket by #if defined ALLOW_ENV_VARS #endif patch: *** fsf/cli-cmds.c 2022-09-22 15:59:17.238371000 -0700 --- modifiedFsf/cli-cmds.c 2022-09-22 16:29:59.506242000 -0700 *************** *** 660,665 **** --- 660,744 ---- script_from_file (stream, file); } + #define ALLOW_ENV_VARS 1 + #if defined ALLOW_ENV_VARS + /* return true if s uses one or more env vars, specified as ${VARNAME} */ + extern int + contains_env_var(const char *s0) + { + return s0 && (strlen (s0) > 1) && (strstr(s0, "${") != NULL) && + (strchr(s0, '}') != NULL); + } + + /* substitute_env_var - given a string, return a copy of the string with + env vars interpolated (if any). env vars are recognized by the + syntax ...${VARNAME}... + */ + + + extern char * + substitute_env_var(const char *s0, const char* msg, const int from_tty) + { + int slen, i; + + char *s = xstrdup(s0); + slen = strlen(s); + + for (i=0; i opened = find_and_open_script (file, search_path); if (!opened) { *************** *** 844,849 **** --- 931,945 ---- if (!arg) execl (user_shell, p, (char *) 0); else + #if defined ALLOW_ENV_VARS + /* support interpolation of env vars in shell arg */ + if (contains_env_var(arg)) + { + char* newArg = substitute_env_var(arg, "exec'ing cmd", from_tty); + execl (user_shell, p, "-c", newArg, (char *) 0); //do as usual + xfree(newArg); + } else + #endif //ALLOW_ENV_VARS execl (user_shell, p, "-c", arg, (char *) 0); fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell, *** fsf/cli-logging.c 2022-09-22 15:59:17.250302000 -0700 --- modifiedFsf/cli-logging.c 2022-09-22 16:26:12.482260000 -0700 *************** *** 139,144 **** --- 139,151 ---- current_uiout->redirect (gdb_stdout); } + #define ALLOW_ENV_VARS 1 + #if defined (ALLOW_ENV_VARS) + extern int contains_env_var(const char *s0); + extern char * substitute_env_var + (const char *s0, const char* msg, const int from_tty); + #endif + static void set_logging_on (const char *args, int from_tty) { *************** *** 149,154 **** --- 156,173 ---- xfree (logging_filename); logging_filename = xstrdup (rest); } + + #if defined (ALLOW_ENV_VARS) + // recognize ${} in log file name + else + { + if (contains_env_var(logging_filename)) + { + logging_filename = substitute_env_var + (logging_filename, "Logging to", from_tty); + } + } + #endif // ALLOW_ENV_VARS handle_redirections (from_tty); }