Howto send signals to a program started in Debug mode in KDevelop - c++

I want to analyse a program that i've written in KDevelop.
I compile the Program and start it with
Right Click on the CMake Project -> Debug as... -> Native Application
Now the program runs in KDevelop and I can see the output on the console embedded into KDevelop.
My program stops running when I press Ctrl+C" (SIGTERM). I can press it when I'm running the program in a console outside KDevelop.
How can I send the signal "SIGTERM" to the embedded console inside KDevelop?
As a workaround I can start htop, select the program and send a SIGTERM from there, which works fine although it would be nicer to have all the functionality in KDevelop itself.

One possible Solution is:
Right Click on the CMake Project -> Debug as... -> Native Application.
Change to the "gdb"-Tab inside KDevelop.
Hit the "Pause"-Icon on the right corner to enable the input field of the "gdb"-Tab
Type signal <Signal>, e.g. signal SIGTERM
The program continues and it catches the signal sent.

Use the kill command to send a signal to a process. kill -l should provide you with a list of signals and their IDs.
For example, on FreeBSD, the SIGTERM signal is #15 as shown by this output:
$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGEMT 8) SIGFPE 9) SIGKILL 10) SIGBUS
11) SIGSEGV 12) SIGSYS 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGURG 17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD
21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGINFO 30) SIGUSR1
31) SIGUSR2
So to send a SIGTERM to my process, I look up the process ID and then send it a kill command like so:
kill -15 <process ID>

You can send SIGINT from inside KDevelop:
Run -> Interrupt
However you can't send any other signal.
If you think that's an useful feature create a wish request on bugs.kde.org - eventually including an attached patch :D

Related

Signal Handling for C++ for single Program

Is it possible that signal handling can be done for a particular process only.
For Ex:-
2 Independent different process A & B are running in background & signal handling for SIGTERM has been done only in the code for process A. So, if I will send the SIGTERM to process B to terminate it then the signal handler function in process A will execute & I want that the signal handler will execute only when the SIGTERM is sent to process A.
Note:- I am using Linux OS.
So, is there any way for achieving the above.

Which signal numbers does GDB use?

I've been trying to put together a kind of 'remote gdb agent', but I don't seem to find the right signal numbers for stop packets. Where/how can I find the signal numbers gdb actually uses? At least the gdb-multiarch from Debian Jessie repo acts weird.
Signal 31 is shown as SIG37 - real-time event 37 (I expected SIGUSR2)
and if I send signal 10, dgb shows "Can't send signals to this remote system. SIGURG not sent." and sends a 'c'-packet (I expected SIGBUS).
With remote and serial debugs on, I can see that the signals 31 and 10 are actually received by gdb.
[edit]
By trying I found out the first 30 signals. Here are the first ones:
(the asterisk meand that can't continue with debugging)
2 SIGINT
4 SIGILL
5 SIGTRAP
6 SIGABRT *
7 SIGEMT
8 SIGFPE
9 SIGKILL
10 SIGURG *
11 SIGSTOP
12 SIGTSTP
13 SIGCONT *
14 SIGCHLD *
15 SIGTTIN *
16 SIGTTOU
17 SIGIO *
18 SIGXCPU *
[edit2]
[r $][T][1][0][#][b][5]Packet received: T10
...
Can't send signals to this remote system. SIGURG not sent.
Sending packet: $c#63...[\x00][\x00][\x00][\x00][\x00][
r +]Ack
The gdb remote protocol uses its own numbers for signals. These have to be translated to the correct system values by your remote agent. See the documentation (first paragraph) for details; I think the signal numbers are only available in a gdb header file.

Use GDB to Debug SIGTERM

I have searched several questions on stackoverflow about debugging SIGTERM, but have not get the information I needed. Perhaps, I am still new to this issue.
My program terminated with the SIGTERM signal without core dump and I donot know how to track this down. My Question is, what is the general way of debugging this issue in GDB?
Thanks.
Although SIGTERM can be sent by the kernel in a few cases, it's almost always sent by another user process. If you run your program under gdb, then when it receives a SIGTERM it will become paused. You can then get some info about the signal by looking at the $_siginfo structure:
(gdb) print $_siginfo._sifields._kill
$2 = {si_pid = 3926, si_uid = 1001}
This is on Linux. It means that pid 3926 sent the signal, and the userid who sent it is 1001.
My program terminated with the SIGTERM signal without core dump
It is expected that if someone sends your program a SIGTERM, then no core dump is produced.
and I donot know how to track this down.
You need to figure out where that SIGTERM is coming from. Someone sends it your program, and the key question is who.
Usually SIGTERM is sent when either you type Control-C in the terminal in which you started the program (correction, that would send SIGINT, not SIGTERM), or you type kill <pid> in some other terminal.

Handling signals with gdb

I'm debugging a C++ app for Ubuntu 10.04 that sometimes receives a SIGKILL signal.
I want to catch the signal and stop it from killing the execution, just to see if I can get some useful info of the app's state at that precise moment.
Reading the gdb documentation I found the handle command, so I tried to apply it to the SIGKILL signal:
(gdb) handle SIGKILL stop nopass
Signal Stop Print Pass to program Description
SIGKILL Yes Yes No Killed
So, as I understand this correctly:
stop
GDB should stop your program when this signal happens. This implies the print keyword as well.
print
GDB should print a message when this signal happens.
nopass
GDB should not allow your program to see this signal.
once the SIGKILL signal is emitted, gdb should somehow catch it, print the message, stop the execution and don't let the app kill itself, right?
The problem is that this doesn't happen and the app terminates.
Do you know how could I catch the signal?
Useful Info:
The piece of code that is running when the signal is emitted is executed in another thread.
gdb version: 4.4.3
g++ version: 7.1
From unix signal(7) man page:
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
So the debugger can set the handler but that doesn't make any sense. The OS directly performs the needed action. If SIGKILL could be handled from application the OS has no chance to terminate a broken application. For that reason SIGKILL is a bit special :-)

Signals when debugging

I'm developing an application (a service/daemon, really) on Linux in C++ that needs to interface with a piece of hardware. If my program doesn't release the resources for this peice of hardware cleanly when terminating, then I have to reload the device driver, a process that takes about 10 minutes and for obvious reasons having to wait 10 minutes between each test of the program would be frustrating.
So I have used the sigaction() function to catch a SIGINT (a ctrl-c) so that my program can cleanly shutdown when I'm finished with it. When running the program from the console, this works fine. However, when debugging in Netbeans or Eclipse (I've tried both) things don't work.
In Eclipse, if I hit ctrl-c in the console it provides, it doesn't seem to register that a SIGINT ever occurred
In Eclipse, if I run the program in debug mode and then use kill -SIGINT <pid>, the program just breaks as if it hit a breakpoint
Netbeans actually seems to realise a signal has been sent when I hit ctrl-c in the console, and pops up a dialog asking if I want to forward it to the application. Clicking "Forward and continue" just seems to break the program and the signal is not received by the application. It also says I can configure this stuff in Debug -> Dbx configure, a menu item that doesn't exist
In Netbeans, if I run the program in debug mode and then use kill -SIGINT <pid>, the behaviour is the same as above
I then added a SIGQUIT handler and tried sending that via kill when debugging in Netbeans. This time, no dialog appears and the signal handler is never tripped.
I need some way to cleanly shutdown my app while I'm debugging. Any ideas?
It turns out the problem had nothing to do with Netbeans or Eclipse, but rather gdb.
gdb can be configured to handle signals in a variety of ways. If you run:
gdb
then type:
info signals
You'll get a list of signals and gdb actions on what to do if it receives that signal:
Signal Stop Print Pass to program Description
SIGHUP Yes Yes Yes Hangup
SIGINT Yes Yes No Interrupt
SIGQUIT Yes Yes Yes Quit
SIGILL Yes Yes Yes Illegal instruction
SIGTRAP Yes Yes No Trace/breakpoint trap
etc...
My temporary work around has been to use SIGALRM which gdb defaults to not breaking and sending to the process. However, you can also customise the default gdb settings by creating a .gdbinit file where you can set these
Even this post is old, hope it can help others.
To prevent Eclipse from catching the Ctrl+C, you can configure your gdb using .gbdinit file.
You create a .gdinit with this content
#we want Ctrl+C to be no break, pass to application and printed by the debugger
handle SIGINT nostop
handle SIGINT pass
handle SIGINT print
In your eclipse configuration, you can define where is your .gdbinit file to use in your Debug configuration
Simple solution.. Try using DEBUG macros to handle your situation.
// Register the signal handler to stop service.
#ifdef _DEBUG
signal(SIGKILL, <your signal handler>);
#endif
Also, you may try to clean up your app before exiting.