How do you set the current directory of a debugged

2019-06-20 04:54发布

问题:

I have an application that is sensitive to the directory it gets invoked from — it loads some files using relative paths.

When I start the program through a debugger, how can I control what the initial current directory will be?

I'd prefer to avoid adding special code to the debugged program to make it call chdir; my instinct is that the debugger should provide some way to specify that externally, since it's inherently an external setting anyway.

The debugger I'm using is TotalView, but I'm open to answers for other Linux debuggers since it could help me find a comparable setting in TV. I can specify environment variables when I invoke the program, so on a lark, I tried setting PWD, but no luck.

回答1:

With GDB, the initial working directory is the directory you instantiate GDB from. So, just run GDB from whatever you want the working directory to be. Alternatively, while the program is running, you can change the current working directory just by doing:

(gdb) print chdir("new/working/directory")

GDB also had a built-in command for changing the process' working directory from the GDB prompt:

(gdb) cd new/working/directory

I've never used TotalView, but it should have a similar functionality for executing code (with side effects) from within the debugger.