I was figuring out a problem where starting the application from GDB results in a symbol lookup error, but starting it from the shell works.
It turns out that whenever you start a program from within GDB it will start a new shell and thus override all environment variables I had set before starting GDB (like LD_LIBRARY_PATH
).
This is not really the behavior I want. Can someone explain the rationale behind this, or tell me how I can turn this off?
I am guessing that you unconditionally set
LD_LIBRARY_PATH
in your~/.cshrc
or the like. So if from a shell prompt you do this:the result is something other than
foo
. Don't do that.Usually this happens to CSH users, who neglected to protect their
~/.cshrc
against non-interactive shells. It could also happen to BASH users who set theirBASH_ENV
.When you start gdb from the shell, you start it as a new process, there's no way around that. In Unix new processes inherit some of the environment of the parent.
To make sure a variable is inherited, if you're using a bourne-like shell, try exporting it:
The debuggee (inferior in gdb parlance) is always started with a clean environment in order to get more reproducible results. In order to set a variable there, use the
command before running.
I meet the same problem. I find that in inferior.h (source code of gdb gdb/inferior.h) there is a macro
STARTUP_WITH_SHELL
, there is also a piece of comment asThen I set
STARTUP_WITH_SHELL
as 0 and decrementedSTART_INFERIOR_TRAPS_EXPECTED
and recompiled my gdb. After that gdb didn't start from shell any more.