0

I have a program that only generates the output when it returns normally or when it calls the exit() function (I'm trying to use gcov on a C program). On a special input my program hangs in an infinite loop so I have to terminate it by sending termination signal. In this situation it won't produce the output I need since it wouldn't call exit().

Is there any way that I can force a running program to call exit() and terminate without touching the source code and writing a signal handler?

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
afsafzal
  • 592
  • 5
  • 15
  • 3
    Attach to it with a debugger and tell it to call `exit()`. – Barmar Jun 30 '16 at 19:05
  • 1
    There's no signal whose default action is to call `exit()`. Either the signal is ignored, it terminates the process immediately, or it performs special action like suspending the process. – Barmar Jun 30 '16 at 19:07
  • You could write replacement for a function provided by a share library and used by the program. Load the replacement function using `LD_PRELOAD`, it could check for any condition and call `exit()`, else it would most likely call the original function. More on `LD_PRELOAD` here: http://stackoverflow.com/questions/426230/what-is-the-ld-preload-trick This implies that the function replaced is invoked in the logic blocking the program. – alk Jul 01 '16 at 10:18
  • 1
    As @Barmar suggested I executed my program within `gdb` and after receiving signal I call `exit()`. That worked pretty well. – afsafzal Jul 07 '16 at 15:22
  • Why not you or @Barmer add this as an answer? So at least this can get closed. – alk Jul 09 '16 at 10:12

1 Answers1

0

As suggested in the comments, I executed my program within gdb and after receiving signal I manually call exit() and it worked like charm.

afsafzal
  • 592
  • 5
  • 15