Author Topic: Signals (debugging) - can anyone explain this weird behaviour?  (Read 5601 times)

Offline johne53

  • Regular
  • ***
  • Posts: 253
Signals (debugging) - can anyone explain this weird behaviour?
« on: November 20, 2008, 06:30:45 pm »
Consider the following simple program:-

Code
#include <signal.h>
#include <iostream>

void sighandler(int signum) {
  std::cout << "Received signal " << signum << std::endl;
  if (signum == 9) exit(42);
}

int main()
{
  for (int i = 1; i <= 15; ++i )
    signal(i, sighandler);
  for(;;) sleep(1);
  exit(0);
}

All it does is catch signals and print a message in a console window. If I compile it under Linux (gcc4.2) or under Cygwin (gcc3.4) and run it normally, it works fine. So if I launch the program, click on the console window and press CTRL+C, I see the message Received signal 2. Both Linux and Cygwin use the gdb debugger and it usually works fine for me. However, if I try to debug this program using C::B, here's what happens:-

LINUX:
The program runs and I click inside its console Window. If I then type CTRL+C, the console window disappears but gdb appears to keep running.

CYGWIN:
The program runs and I click inside its console Window. If I then type CTRL+C, a message appears in the debugger output window saying Program received signal SIGINT, Interrupt but the signal doesn't get passed to the running program (i.e. if I set a break point in function sighandler() it never gets reached).

Do I need to enable something within C::B to make this work in such a way that the program works the same way, whether it's running normally or in the debugger?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #1 on: November 21, 2008, 07:05:13 am »
Do I need to enable something within C::B to make this work in such a way that the program works the same way, whether it's running normally or in the debugger?
Normally not. Since you are using two completely different dev environments (and I'd bet the version of e.g. GCC/GDB differ quite much) you are comparing kind of apples with oranges here. I'd say it's best if you ask in a GCC/GDB forum.
You could try before if it's the same if you run GDB on the command line. Then you know for sure it's not a C::B issue. Still - there *could* be a switch - but then again: Better ask the GCC/GDB devs.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #2 on: November 21, 2008, 07:40:42 am »
If you start your program with gdb from commandline, you can stop it pressing Ctrl-C and type info signals, to see what gdb does if an external signal occured.

In the most cases it stops the program and prints, what signal occured, in some cases it does not pass the signal to the program after pressing "c".

You can change that behaviour.
Type "help handle" to see what is possible.

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #3 on: November 21, 2008, 10:07:42 am »
If you start your program with gdb from commandline, you can stop it pressing Ctrl-C and type info signals, to see what gdb does if an external signal occured.

"Debug->Information->Signal handling" will do the same thing in C::B. I just didn't have the time to make it editable so far.
Be patient!
This bug will be fixed soon...

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #4 on: November 21, 2008, 11:08:41 am »
"Debug->Information->Signal handling" will do the same thing in C::B. I just didn't have the time to make it editable so far.

Hmmm.... Debug->Information->Signal handling is grayed out in my version (Windows platform). Is that what you meant? Or is it time for me to update to the latest version?

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #5 on: November 21, 2008, 11:16:00 am »
"Debug->Information->Signal handling" will do the same thing in C::B. I just didn't have the time to make it editable so far.

Hmmm.... Debug->Information->Signal handling is grayed out in my version (Windows platform). Is that what you meant? Or is it time for me to update to the latest version?

It is only available, while debugging and if the debugging process is interrupted.
Start debugging and click the stop button once. This sends SIGINT to the debugger.

Now you should be able to use the "Information" submenu.

To change the debuggers behaviour, you can send commands to the debugger via "Debug -> Send user command to debugger" (only if debugging is interrupted).
The output is shown in the debugger log.

To see how signal handling can be changed, send "help handle" to the debugger.

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #6 on: November 21, 2008, 11:38:50 am »
Thanks Jens, I see it now. I can see that the default setting for SIGINT is not to pass it to the program. To change this, what command would I need to send to the debugger? Or is there maybe a config file somewhere that I could edit?

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #7 on: November 21, 2008, 01:09:03 pm »
Or alternatively, is there any other signal that I can generate from a command line? It just occured to me that for testing purposes it doesn't need to be CTRL-C. Any signal could be suitable as long as I can generate it from the command line.

BTW, I did try help handle but it didn't seem to offer any solutions.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #8 on: November 21, 2008, 02:34:45 pm »
Or alternatively, is there any other signal that I can generate from a command line? It just occured to me that for testing purposes it doesn't need to be CTRL-C. Any signal could be suitable as long as I can generate it from the command line.
http://sunsite.ualberta.ca/Documentation/Gnu/gdb-5.0/html_node/gdb_99.html#SEC99

BTW, I did try help handle but it didn't seem to offer any solutions.
http://sunsite.ualberta.ca/Documentation/Gnu/gdb-5.0/html_node/gdb_38.html

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: Signals (debugging) - can anyone explain this weird behaviour?
« Reply #9 on: November 21, 2008, 06:58:40 pm »
Those links were very helpful - thanks  :D