0

I am trying to make a watchdog for a single-threaded program. The problem is, that we run some foreign so/dlls (the code is available) which means that we pass control there.

The idea is to recompile these with some callback to a sort of a cancellation routine.

Is it possible to let GCC call some callback functions in between of C-transactions or asm-transactions in this compiled foreign code?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Stasik
  • 2,568
  • 1
  • 25
  • 44
  • 2
    The answer to this SO post explains how to get gcc to inject debug calls per function enter and exit. Might well be what you need: http://stackoverflow.com/questions/2281739/automatically-adding-enter-exit-function-logs-to-a-project – Alex Wilson Jul 03 '12 at 19:07
  • that goes into the right direction, however i need some more calls e.g. in the loops – Stasik Jul 03 '12 at 19:16
  • 1
    What exactly is a "C-transaction"? – Keith Thompson Jul 03 '12 at 19:16
  • @Stasik: I'm pretty certain that what you want to do is not possible with GCC. If you're happy to use [Clang/LLVM](http://clang.llvm.org/) and write a plugin to add the calls, then you can probably do what you need. But that sounds like a lot of work... – Alex Wilson Jul 03 '12 at 19:18
  • @Keith: I do not know a proper explanation for a set of ASM commands that are generated from one C statement like "a=a+1;" are actually several ASM commands. – Stasik Jul 03 '12 at 19:19
  • @AlexWilson it looks like that, this is sad. – Stasik Jul 03 '12 at 19:25

1 Answers1

0

What I'm about to suggest does not involve the compiler, but this sounds like a problem you can solve at runtime with POSIX signals or ptrace ...

With a signal you can interrupt the current context, similar to what would happen in kernel mode with an IRQ. You will have to worry about being "signal-safe" (example: your handler can't use malloc because it might interrupt malloc itself while its data structures are in an indeterminate state.)

With ptrace you can step through instructions in another process as if in a debugger.

Tread carefully, as these are difficult mechanisms to use correctly and it's very easy to shoot yourself in the foot.

asveikau
  • 39,039
  • 2
  • 53
  • 68
  • POSIX is a not real alternative since we use mixed windows/linux environments :( – Stasik Jul 03 '12 at 19:17
  • @Stasik In that case you'll probably need radically different approaches for each.. Have you looked at `QueueUserAPC`? `WaitForDebugEvent`? – asveikau Jul 03 '12 at 19:20
  • yep, ptrace seems to be a way of realizing it, however, I actually was looking for a single-threaded solution. – Stasik Jul 03 '12 at 19:22
  • @Stasik - The code in question will still run on a single thread, even if your control code is running in another process or thread. The way I see it you're rejecting workable solutions if favor of something that doesn't make sense at all. (Who knows if your original problem maps well onto this question? Maybe you should take a step back and re-evaluate what it is you want to do.) – asveikau Jul 03 '12 at 20:45
  • this is true, however we often do run on uC's, meaning there is no way of having a watchdog thread. Still, you might be right with the ugly way I would do it. Okay us assume, I take POSIX signals, is there a way to force a program to smash the stack and return from the current function it is stack in an interrupt handler e.g. for the ALARM signal? (not to speak about memory leaks ;)). – Stasik Jul 03 '12 at 20:55
  • the answer is here http://stackoverflow.com/questions/9968148/is-it-possible-to-give-a-processor-execution-time-for-certain-functions-in-c – Stasik Jul 03 '12 at 21:02