Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the current directory of a debugged process?

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.

like image 830
Rob Kennedy Avatar asked Nov 19 '08 21:11

Rob Kennedy


Video Answer


1 Answers

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.

like image 55
Adam Rosenfield Avatar answered Nov 02 '22 22:11

Adam Rosenfield