Coding C in VS Code on Linux.
I use;
signal(SIGINT, myhandler);
signal(SIGHUP, myhandler);
signal(SIGKILL, myhandler);
signal(SIGTERM, myhandler);
and I use VS code internal terminal, "run in terminal" option checked.
While my code running in a loop, when I send key-combo CTRL-C the execution is stopped but the signal was not caught by my code (myhandler).
I added "handle all nostop print pass" to .gdbinit file but nothing has been changed.
The question is how can I catch CTRL-C (SIGINT) sent from keyboard in my program currently being debugged in VS Code?
To elaborate I'll add my launch.json, tasks.json and main.c files, below:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gdb build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/bin/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${workspaceFolder}/*.c",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"isDefault": true,
"kind": "build"
},
"detail": "compiler: /usr/bin/gcc"
}
]
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
// function prototypes
void initialize(void);
void sigHandler(int signo);
/*************** main routine which contains the infinite loop ****************/
int main(int argc, char *argv[])
{
initialize();
printf("It is alive...");
while (1)
{
// do something
printf("looping...");
usleep(10000);
}
return 0;
} //------------------------------- main -------------------------------------//
/**
* Let's get ready and initialize everything.
*/
void initialize(void)
{
signal(SIGINT, sigHandler);
signal(SIGHUP, sigHandler);
signal(SIGKILL, sigHandler);
signal(SIGTERM, sigHandler);
}
/**
* Stop before exiting and close everything.
*/
void sigHandler(int signo)
{
printf("Caught signal: %d, exiting!", signo);
exit(0);
}