How do I generate a SIGINT when using XCode to debug?
Asked Answered
E

6

13

My console app traps SIGINT so it can exit gracefully.

Pressing CTRL+C inside XCode while the program is being debugged, though, has no effect.

I can find the process and use a terminal window to send a SIGINT to my process, however I'm hoping there's a simpler solution that I can do within XCode.

Edgeways answered 8/7, 2011 at 17:18 Comment(0)
E
7

The pause button of the debugger console actually sends a SIGINT to your app. If you want to make the debugger pass the signal to your app you can do the following:

  1. Press the pause button of the debugger and wait for the debug console to gain focus
  2. Type handle SIGINT pass and press ENTER
  3. Press the Continue button

Now pressing again the Pause button of the debugger console of Xcode will make the SIGINT hit your app.

If you don't want the debugger to stop as soon as the SIGINT is caught you might also want to add handle SIGINT nostop to the previous list.

Eichhorn answered 6/1, 2012 at 3:38 Comment(3)
Doesn't work anymore with XCode 6 which uses llvm debugger.Boleyn
@Boleyn I added an answer for Xcode 5 and above: https://mcmap.net/q/864689/-how-do-i-generate-a-sigint-when-using-xcode-to-debugSpitball
@wobbals added a solution for XCode 8 and higher (e.g. 10.0): https://mcmap.net/q/864689/-how-do-i-generate-a-sigint-when-using-xcode-to-debugUnmarked
N
6

Update for 2017 / Xcode 8: the proper command(s) for informing lldb of your intention for handling interrupts:

process handle SIGINT -s false
process handle SIGINT -p true

In my experience, even with the above commands, the pause feature of the debugger still will interrupt the app and yield control to the debugger's stack pointer, however a pkill -2 appname at the terminal will trigger your interrupt function without any interaction with the debugger, for example:

void on_signal(int sig) {
  is_interrupted = 1;
}

int main(int argc, const char * argv[]) {
  signal(SIGINT, on_signal);
  // ... do stuff
}
Nicolina answered 13/6, 2017 at 8:44 Comment(0)
S
4

In Xcode 5+ (llvm):

  1. Pause the process

  2. At the (llvm) prompt, enter process signal SIGINT

  3. Resume execution

Spitball answered 9/4, 2016 at 20:1 Comment(1)
XCode 8 and higher (e.g. 10.0): see @Nicolina solution https://mcmap.net/q/864689/-how-do-i-generate-a-sigint-when-using-xcode-to-debugUnmarked
C
2

One solution is to use the UNIX kill or killall commands.

If you know the Process ID you can open the Terminal application and type:

kill -SIGINT 415

(where 415 is the PID for this process)

Or, perhaps easier, you can type

killall -SIGINT my_console_app

(where my_console_app is the name of your app, i.e. the name of the binary created — not the bundle if it's in one)

This will send the signal to all instances of the application, as the name killall suggests, so it may not be appropriate. It might be worth checking if there are other processes with the same name running before you do a killall (;

Changchun answered 12/8, 2011 at 5:11 Comment(0)
B
1

One of the work-arounds to the hassel of working with interrupts is making a MainMenu with a CTRL-C short-cut that executes your requested routine, or sends itself a SIGINT, if you really need the sig.

Broca answered 8/7, 2011 at 20:20 Comment(0)
P
0

Update for XCode 12+

In the modern versions of XCode it is hard to intercept SIGINT. For debug purposes it is better to use SIGINFO in place of SIGINT. You can generate that in Terminal using ^T (CTRL+T).

So during debugging temporary replace SIGINT with SIGINFO in your code. To intercept SIGINFO try next steps all together:

  1. Go to Product > Scheme > Edit Scheme > Run > Options and change Console from Use Xcode to Use Terminal.
  2. Run your program
  3. Pause the program
  4. In the Xcode console, enter this:
(lldb) process handle -p true -s false -n false SIGINFO

You will get:

NAME         PASS   STOP   NOTIFY
===========  =====  =====  ======
SIGINFO      true   false  false
  1. Resume the program
  2. Switch to Terminal and press CTRL+T.

Your signal handler will fire.

Don't forget to return SIGINT back after finishing debugging!

Pratfall answered 22/9, 2022 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.