Regarding stack analysis

I would like to know how I could do the following :

void func(){
  int a = 100; b=0;
  int c = a/b;
}

void sig_handler (int sig,siginfo_t *info,void *context){
//signal handling function
//here I want to access the variables of func() 
}
  
int main(){
  struct sigaction *act = (struct sigaction *)malloc(sizeof(struct sigaction));
  act->sa_flags = SA_SIGINFO;
  act->sa_sigaction = sig_handler; 
  sigaction(SIGFPE,act,NULL); 
  func();
}

As expected, this program should give an exception. But I use sigaction to catch the signal (arithmetric exception) and send control to another function(sig_handler), where I want to handle this signal.

I would like to access the variables inside func() and modify them. How can I do this ?

thank you,

Normally, coders test for division by zero in the function and take corrective action to the values before SIGFPE hits the process. Not after.

With your method, you will need global pointers to the problem value(s), but how do you expect to correct the problem? You have already gone past the division step. Call the function again from the signal handler? Then what happens? The code sets b=0 again and you get another SIGFPE, and you are back where you started.

I understand what you are saying, but my aim is not exception handling. I want to modify certain memory locations before dumping the core , and this is why I want access to the local variables in func().

For instance, we've the ucontext_t (in sigaction) which gives the context of the exception(similar to setjmp env). But I don't know how I can get a mapping between the variables in func() and the context ?

thanks.

If you want to modify function-local variables via some form of stack inspection/modification in an attempt to add global exception handling in C I'd respectfully suggest you are doing it wrong.

C based interpreted languages have these kinds of features without breaking C. Maybe you don't really want C at all.

Also gdb has all these introspection capabilities. Learn the debugger.

Either make those global variables or use the setjmp() and longjmp() calls. Caveat with setjmp() and longjmp() is that only one of the local variables of main() either a, b, or c can be changed by func() not all of them. To change all of them declare them as globals.

I think he wants to the core to be created, and I'm guessing there is a security problem with having the core file sitting out there. ulimit can turn off core creation.

The OP needs to clarify what exactly is needed.

I apologize for the delayed reply. I'm basically trying to access local variables of a program.

I was trying to use this reverse engineering api (eresi-project.org), but that also doesn't give me direct access to local variables.

In other words, I want to get access to local variables like gdb does, but unfortunately gdb doesn't have an api.

thanks,
praveen