0

Is it possible to write a signal handler that forces the program to return from a function?

For example, in the following code:

void myfun() { __asm__("repeat: jmp repeat"); } 

myfun() has an indefinite loop. I would like to stop this indefinite loop and return to main function by raising some signal. Is this possible?

Thanks.

1
  • man setjmp Commented Jun 22, 2013 at 8:54

1 Answer 1

1

Inside the signal handler perform a longjmp to a setjmp prepared in main.

Sign up to request clarification or add additional context in comments.

2 Comments

In the given assembly code, no volatile variable is being checked. I am using it to profile processor performance for branch instructions, and so interference from other variables should be minimal. Ideally, I would like to raise an exception that allows the function to return.
@Arani I checked the standard and I don't see any provision that forbids you to call the longjmp directly from the handler and then the loop doesn't have to be changed. I'll change my answer accordingly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.