Bug 339231

Summary: JJ: kdevelop fails to set the working directory when debugging if the path contains spaces
Product: [Applications] kdevelop Reporter: Nicolas Werner <nicolas.werner>
Component: CPP DebuggerAssignee: kdevelop-bugs-null
Status: RESOLVED FIXED    
Severity: normal CC: nicolas.werner, niko.sams
Priority: NOR    
Version: 4.7.0   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
URL: https://forum.kde.org/viewtopic.php?f=218&t=122931
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: probably related debug output of kdevelop
Proposed patch for 4.7.x

Description Nicolas Werner 2014-09-20 12:54:51 UTC
If you use a build path that contains spaces, setting the working directory of the executable will fail if you try to debug the program via the "Debug" button.
This happens in both cases if the working directory is implicitly assumed to be the build directory or if you set it manually in Run -> Configure launches.

Quote:
the bug is that KDevelop uses single quotes instead of double quotes to escape the path. gdb needs double quotes

Reproducible: Always

Steps to Reproduce:
1. Create new project
2. Set build directory to a path with spaces e.g. "~/project x/build"
3. Configure Launch to start the executable, optionally set working directory to the build path
4. Build and run via "Debug"
5. Check cwd in /proc/$PID/cwd/

Actual Results:  
Working directory of the process is set to ~/

Expected Results:  
Working directory equals the build directory
Comment 1 Nicolas Werner 2014-09-20 13:08:56 UTC
Created attachment 88763 [details]
probably related debug output of kdevelop

"/home/nicolas/Dokumente/devel/C++/Lone Writer/build/" as build directory
gdb expects double quotes, but path is given in single quotes, which only works when there are no spaces in the build path
Comment 2 Nicolas Werner 2014-11-07 14:34:50 UTC
I have now looked at it, and found where the problem occurs, but I'm not sure, which way it the right one to fix the problem.

The quoting occurs in line 1040 debuggers/gdb/debugsession.cpp@master
 queueCmd(new GDBCommand(GDBMI::EnvironmentCd, KShell::quoteArg(dir.toLocalFile())));

(or line 1039 in the 4.7er branch, that uses QString instead of QUrl)

KShell only does a single quote and doesn't escape spaces.
That seems to be the right solution in some cases, so I should probably only do an in place quoting of dir.toLocalFile() with double quotes, right?
Or should I check, if the path already contains double quotes and escape them?
Or is the semantic of KShell::quoteArg() counter intuitive and should be changed? (Probably not, as it is used elsewhere)

But basically the fix would be a one liner, what do you think about it?
Comment 3 Milian Wolff 2014-11-07 15:36:48 UTC
I somehow doubt that the quoting is required here at all. This is a GDB console, and that is EOF delimited. So something like

cd "foo"

is not equivalent to

cd foo

(gdb) cd "/"
"/": No such file or directory.
(gdb) cd /
Working directory /.

Also one cannot inject commands by doing something like

cd foo; someothercommand

If at all that could happen when the file contains a newline, which can easily be checked. But maybe the above is different for the MI interface which we use in KDevelop. Needs to be checked, but I agree that KShell seems superfluous here.
Comment 4 Nicolas Werner 2014-11-07 18:03:29 UTC
Ah, I didn't notice, that kdevelop uses a different interface of gdb.
Running some tests:

(gdb) 
-environment-cd '/home/nicolas/Dokumente/devel/C++/Lone Writer/build/'
^error,msg="-environment-cd: Usage DIRECTORY"

(gdb) 
-environment-cd /home/nicolas/Dokumente/devel/C++/Lone Writer/build/
^error,msg="-environment-cd: Usage DIRECTORY"

(gdb) 
-environment-cd "/home/nicolas/Dokumente/devel/C++/Lone Writer/build/"
^done

(gdb) 
-environment-cd /home/nicolas/Dokumente/devel/C++/Lone\ Writer/build/
^error,msg="-environment-cd: Usage DIRECTORY"

So it only works, when putting the string in double quotes. I'll think of a patch, and I will send it in for review.

Thank you for the fast response!
Comment 5 Nicolas Werner 2014-11-07 23:09:52 UTC
Sent in a review request (via review board).

But I also have a patch for the 4.7 branch and I don't know, where to put it, so I'll post it here.
Comment 6 Nicolas Werner 2014-11-07 23:10:40 UTC
(In reply to Nicolas Werner from comment #5)
> Sent in a review request (via review board).
> 
> But I also have a patch for the 4.7 branch and I don't know, where to put
> it, so I'll post it here.

https://git.reviewboard.kde.org/r/121061/
(forgot the link...)
Comment 7 Nicolas Werner 2014-11-07 23:11:57 UTC
Created attachment 89497 [details]
Proposed patch for 4.7.x
Comment 8 Milian Wolff 2014-11-15 20:13:52 UTC
Git commit f455ae9f4305a3db66bd091318ee370d4a97a48f by Milian Wolff, on behalf of Nicolas Werner.
Committed on 15/11/2014 at 20:10.
Pushed by mwolff into branch '4.7'.

Fix environment-cd for paths containing spaces.

Problem: GDBMI only accepts paths containing spaces if they are in
double quotes otherwise cd will fail.

Solution: Add prepend and append double quotes to the dir string
instead of using KShell::quoteArg.

REVIEW: 121061

M  +1    -1    debuggers/gdb/debugsession.cpp
M  +2    -0    debuggers/gdb/unittests/CMakeLists.txt
M  +21   -1    debuggers/gdb/unittests/gdbtest.cpp
M  +1    -0    debuggers/gdb/unittests/gdbtest.h
A  +2    -0    debuggers/gdb/unittests/path with space/CMakeLists.txt
A  +31   -0    debuggers/gdb/unittests/path with space/spacedebugee.cpp     [License: LGPL (v2)]

http://commits.kde.org/kdevelop/f455ae9f4305a3db66bd091318ee370d4a97a48f