Working directory when debugging as root

I need to run/debug my command line app as root, fortunately there is a nice checkbox in the Product->Scheme->Edit Scheme->Info for this option. But as soon as I choose to debug as root the working directory becomes / even if I set custom working directory in the product scheme options.

Any idea how to have both a sudo user and custom working directory?

Replies

What’s the intended execution environment for this privileged code?

In my experience most privileged code on macOS eventually ends up running via a launchd daemon, and if that’s the case then it’s better, in my experience, to let

launchd
run your daemon and then attach to it with the debugger. The execution environment for a launchd daemon is very different than the execution environment for, say, code run via
sudo
.

There’s specific affordance for this in both Xcode (“Wait for executable to be launched”) and

launchd
(the
WaitForDebugger
key in the launchd property list).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Production environment is ubuntu server and the app is run with sudo. It is a web app, which has to listen to the 443 port. I wonder what would be the best way to reproduce the environment on my mbp? At the moment I am just using a higher port number as a workaround.

You wrote:

At the moment I am just using a higher port number as a workaround.

I think that’s the right approach to use. While it’s very different from your final target environment, it has the advantage that it’s super convenient. Getting closer to your target runtime environment by running as root doesn’t necessarily help — there are lots of other differences between BSD-based systems like macOS and Linux — and it’s a lot more hassle.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I see your point and accept it.

Thinking about xcode functionality I would say that it makes sense to disable "custom working directory" option when debug as root is chosen because it has no effect. Maybe I should create a ticket for that, it looks like a bug to me.

Maybe I should create a ticket for that …

That makes sense to me too.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"