How to Set The Current Directory of a Debugged Process

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

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.

VSCode -- how to set working directory for debugging a Python program

@SpeedCoder5's comment deserves to be an answer.

In launch.json, specify a dynamic working directory (i.e. the directory where the currently-open Python file is located) using:

"cwd": "${fileDirname}"

This takes advantage of the "variables reference" feature in VS Code, and the predefined variable fileDirname.

If you're using the Python: Current File (Integrated Terminal) option when you run Python, your launch.json file might look like mine, below (more info on launch.json files here).

{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
},

//... other settings, but I modified the "Current File" setting above ...
}

Remember the launch.json file controls the run/debug settings of your Visual Studio code project; my launch.json file was auto-generated by VS Code, in the directory of my current "Open Project". I just edited the file manually to add "cwd": "${fileDirname}" as shown above.

Remember the launch.json file may be specific to your project, or specific to your directory, so confirm you're editing the correct launch.json (see comment)

If you don't have a launch.json file, try this:

To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.

Per @kbro's comment, you might be prompted to create a launch.json file by clicking the Debug button itself:

When I clicked on the Debug button on my navigation panel it said "To customise Run and Debug create a launch.json file." Clicking on "create..." opened a dialog asking what language I was debugging. In my case I selected Python

Changing current directory and command line arguments to CDB debugging process

Well, it isn't as easy as in GDB but it is possible in CDB.

To change the working directory and maintain the breakpoints you have to .kill the process, tell it where to startup using .creatdir, and restart the process with .create.

For example:

 0:000> bp CreateFileW 
0:000> bl 0 e 750316af
0001 (0001) 0:**** kernel32!CreateFileW
0:000> .kill
Terminated. Exit thread and process events will occur.
0:000> .createdir d:\dev
Process creation dir: d:\dev
Process will inherit handles
0:000> .create c:\windows\system32\notepad.exe
CommandLine: c:\windows\system32\notepad.exe
Starting directory: d:\dev
Create will proceed with next execution
0:000> g
Symbol search path is: SRV*d:\symbols*http://msdl.microsoft.com/download/symbols
Executable search path is: ModLoad: 00880000 008b0000 notepad.exe eax=00000000
ebx=00000000 ecx=25c50000 edx=0009dc08 esi=fffffffe edi=00000000 eip=774d0fac
esp=000df398 ebp=000df3c4 iopl=0 nv up ei pl zr na pe nc cs=0023 ss=002b
ds=002b es=002b fs=0053 gs=002b efl=00000244
ntdll!LdrpDoDebuggerBreak+0x2d: 774d0fac 8975fc mov dword ptr [ebp-4],esi ss:002b:000df3c0=00000000
0:000> bl 0 e 750316af
0001 (0001) 0:**** kernel32!CreateFileW
0:000> g

To restart with different arguments you do the same .kill and then pass new arguments to the .create command.

If you are going to be doing this a lot then I would invest some time in writing a script that made this multi-step process easier.

How do I set the working directory to the solution directory?

If your current directory is changing, you should probably save your working directory at startup in some variable you can access later to set cwd back there. At least this is how I understand your question.

For getting the cwd, this might help.

Seperate Working Directory for GDB and Debugged Process

Stay in bin/ but pass the "-c" option to make so that it switches to ../build first

$ cd ./bin
$ gdb ProjectBinary
(gdb) make -C ../build

You can also create bin/Makefile that calls the build/Makefile:

bin/Makefile

.PHONY: all
all:
make -C ../build

(Change spaces to tab at the start of the line that calls make)

Then run:

$ gdb ProjectBinary
(gdb) make

How to monitor the change of a process's working directory?

You may as well use strace and watch it for making chdir() system calls - as those really are the only way to change the current working directory.

This is really a debugger-style requirement, and you're going to need to use debug interfaces to achieve it.



Related Topics



Leave a reply



Submit