Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line application: How to attach a child process to xcode debugger?

I'm currently making a command line app in C in which many child process is created. So I need to debug this children code. I have created a child process on Xcode as follow. (see the break point and running cursor.)

enter image description here

After executing some statements, I get xcode to attach GBN(885) to the xcode debugger as shown in below figure.

enter image description hereenter image description here

It doesn't work. How can I attach the child process to xcode debugger? Thank you in advance.

like image 879
inherithandle Avatar asked Nov 23 '13 10:11

inherithandle


People also ask

How do you attach a debugger to a process in Xcode?

When you run an application in Xcode, the debugger is automatically started and attached to the process of the application. Click the Run button in the top left or press Command + R. From the moment the application is up and running, we can start inspecting the process and, if necessary, debug it.


1 Answers

Google and Apple developer page are really silent on this issue and finally I've found a good workaround. This error message appears when we get Xcode debugger to attach to the process that is a child process of process being debugged or that is a process already being debugged by Xcode debugger, gdb, or lldb.

error message

In order to avoid this irritating message, First put kill(0, SIGSTOP) statement where you want to stop the child process. In the following figure, I have put the kill statement right after the child is created.

enter image description here

Second, use gcc to compile your source codes with -g option.

$ ls
GBN.1  gbn.c  gbn.h  type.h util.c util.h
$ gcc *.c -g
$ ./a.out

[1]+  Stopped                 ./a.out
$ ps -ef | grep a.out
  501   628   600   0  9:33AM ttys000    0:00.00 ./a.out
  501   629   628   0  9:33AM ttys000    0:00.00 ./a.out

Now, all we have to do is to tell Xcode debugger to attach to the process by pid. Click the Debug menu and then find the Attach to process option to click By Process Identifier (PID) or name option. Then type the pid for child process (or parent process) and click attach button.

enter image description here

Enjoy Debugging!

enter image description here

like image 199
inherithandle Avatar answered Sep 30 '22 09:09

inherithandle