XCode 15: simple execv program exits with code 5 in the console.

Hello,

found the following curious behaviour, If I try to run from within xcode (pressing Run) the following code:

#include <unistd.h>

int main(int argc, const char * argv[])
{
    char *args[] = {"/bin/ls", "-r", "-t", "-l", (char *) 0 };
    execv(args[0], args);

    return 0;
}

the program does not print the expected list of files and folders but instead exits with:

Message from debugger: Terminated due to signal 5
Program ended with exit code: 5

But if I try to run the exact same compiled program from the terminal, it works as expected. I lost so many hours wondering what I was doing wrong, but apparently it was the xcode console that does not play nice with execing? Could it be that changing the process image throws a wrench into xcode? Anybody has any idea why this could be? Thanks.

Answered by DTS Engineer in 767027022

Oh, you’re doing an exec* without a fork. That, and its reverse, presents unique challenges on macOS.

If you run your test and then look in Console you’ll see that ls generated a crash report:

Exception Type:  EXC_CRASH (SIGTRAP)

However, this doesn’t really tell you what’s gone wrong. My best guess si that this is related to the debugger. ls is a platform binary, and thus can’t be debugged:

% lldb /bin/ls
(lldb) target create "/bin/ls"
Current executable set to '/bin/ls' (x86_64).
(lldb) r
error: process exited with status -1 …

You’re trying to get to the same destination via a different route, and the system is blocking that as well.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Oh, you’re doing an exec* without a fork. That, and its reverse, presents unique challenges on macOS.

If you run your test and then look in Console you’ll see that ls generated a crash report:

Exception Type:  EXC_CRASH (SIGTRAP)

However, this doesn’t really tell you what’s gone wrong. My best guess si that this is related to the debugger. ls is a platform binary, and thus can’t be debugged:

% lldb /bin/ls
(lldb) target create "/bin/ls"
Current executable set to '/bin/ls' (x86_64).
(lldb) r
error: process exited with status -1 …

You’re trying to get to the same destination via a different route, and the system is blocking that as well.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

XCode 15: simple execv program exits with code 5 in the console.
 
 
Q