Linux 如何在gcc中恢复堆栈帧?
我想建立我自己的检查点库。我可以将堆栈帧保存到一个在此处调用checkpoint_(堆栈指针)的文件中,稍后可以通过调用recover(堆栈指针)函数来恢复该文件 这是我的问题:我可以从函数recover(sp)跳到main(),但是堆栈帧发生了变化(堆栈指针,帧指针)。所以我想在这里的检查点_(sp)被称为保留main()的堆栈帧之后,从recover(sp)跳转到main。我尝试了setjmp/longjmp,但无法使它们正常工作。提前感谢。Linux 如何在gcc中恢复堆栈帧?,linux,gcc,setjmp,Linux,Gcc,Setjmp,我想建立我自己的检查点库。我可以将堆栈帧保存到一个在此处调用checkpoint_(堆栈指针)的文件中,稍后可以通过调用recover(堆栈指针)函数来恢复该文件 这是我的问题:我可以从函数recover(sp)跳到main(),但是堆栈帧发生了变化(堆栈指针,帧指针)。所以我想在这里的检查点_(sp)被称为保留main()的堆栈帧之后,从recover(sp)跳转到main。我尝试了setjmp/longjmp,但无法使它们正常工作。提前感谢。 //jmp_buf env; void *ge
//jmp_buf env;
void *get_pc () { return __builtin_return_address(1); }
void checkpoint_here(register int *sp){
//printf("%p\n",get_pc());
void *pc;
pc=get_pc();//getting the program counter of caller
//printf("pc inside chk:%p\n",pc);
size_t i;
long size;
//if(!setjmp(env)){
void *l=__builtin_frame_address(1);//frame pointer of caller
int fd=open("ckpt1.bin", O_WRONLY|O_CREAT,S_IWUSR|S_IRUSR|S_IRGRP);
int mfd=open("map.bin", O_WRONLY|O_CREAT,S_IWUSR|S_IRUSR|S_IRGRP);
size=(long)l-(long)sp;
//printf("s->%ld\n",size);
write(mfd,&size,sizeof(long)); //writing the size of the data to be written to file.
write(mfd,&pc,sizeof(long)); //writing program counter of the caller.
write(fd,(char *)sp,(long)l-(long)sp); //writing local variables on the stack frame of caller.
close(fd);
close(mfd);
//}
}
void recover(register int *sp){
//int dummy;
long size;
void *pc;
//printf("old %p\n",sp);
/*void *newsp=(void *)&dummy;
printf("new %p old %p\n",newsp,sp);
if(newsp>=(void *)sp)
recover(sp);*/
int fd=open("ckpt1.bin", O_RDONLY,0644);
int mfd=open("map.bin", O_RDONLY,0644);
read(mfd,&size,sizeof(long)); //reading size of data written
read(mfd,&pc,sizeof(long)); //reading program counter
read(fd,(char *)sp,size); //reading local variables
close(mfd);
close(fd);
//printf("got->%ld\n",size);
//longjmp(env,1);
void (*foo)(void) =pc;
foo(); //trying to jump to main just after checkpoint_here() is called.
//asm volatile("jmp %0" : : "r" (pc));
}
int main(int argc,char **argv)
{
register int *sp asm ("rsp");
if(argc==2){
if(strcmp(argv[1],"recover")==0){
recover(sp); //restoring local variables
exit(0);
}
}
int a, b, c;
float s, area;
char x='a';
printf("Enter the sides of triangle\n");
//printf("\na->%p b->%p c->%p s->%p area->%p\n",&a,&b,&c,&s,&area);
scanf("%d %d %d",&a,&b,&c);
s = (a+b+c)/2.0;
//printf("%p\n",get_pc());
checkpoint_here(sp); //saving stack
//printf("here\n");
//printf("nsp->%p\n",sp);
area = (s*(s-a)*(s-b)*(s-c));
printf("%d %d %d %f %f %d\n",a,b,c,s,area,x);
printf("Area of triangle = %f\n", area);
printf("%f\n",s);
return 0;
}
一般来说,你不能这样做 您可以尝试不可移植(恢复上的
%rsp
和%rbp
)。您可以使用(请参阅和)-因为longjmp
正在恢复堆栈指针-假设您了解实现细节
多亏了“随机”的、不可复制的位置,堆栈才有。换句话说,如果启动两次相同的程序,main
的堆栈指针将不同。在C语言中,一些堆栈帧包含指向其他堆栈帧的指针。另见
阅读更多关于(请参阅)和研究源代码(或使用)
您可能会限制要使用的C代码(例如,如果您生成C代码),并且可能会根据您的需要扩展GCC使用。这是一项大量的工作
BTW,熔体(内部也)生成C++代码,具有受限的堆栈帧,可以很容易地检查点。你可以把它作为灵感来源
请阅读和(因为精确的GC必须扫描本地指针,这与您的需要类似)。为什么要实现这一点?