int main( ){ long * ret = getFP( ); printf( "A: %i\n", a( ) ); printf( "B: %i\n", b( ) ); printf( "C: %i\n", c( ) ); printf( "Main: %i\n", frameCount() ); return *ret; }
void print_stack_frame(){ static int rec=0; if(!rec){ rec=1; print_stack_frame(); } uint64_t fp=getFP().rsp; int64_t i; for(i=-6;i<=6;i++){ printf("%%rsp+%ld=%ld\n",i*8,fp+(i*8)); printf("%ld(%%rsp)==%ld\n",i*8,*(uint64_t*)(fp+(i*8))); } }
void Java_java_lang_Class_newInstance(void) { INSTANCE_CLASS currentClass = getFP()->thisMethod->ofClass; CLASS clazz = topStackAsType(CLASS); if (IS_ARRAY_CLASS(clazz) || ((clazz->accessFlags & (ACC_INTERFACE | ACC_ABSTRACT)) != 0)) { raiseException("java/lang/InstantiationException"); return; } if (classHasAccessToClass(currentClass, clazz)) { METHOD method = lookupMethod(clazz, initNameAndType, currentClass); if ( (method != NULL) && (method->ofClass == (INSTANCE_CLASS)clazz) /* I don't understand why, but we're not allowed access to * a protected <init> method of a superclass. */ && classHasAccessToMember(currentClass, (method->accessFlags & ~ACC_PROTECTED), (INSTANCE_CLASS)clazz, (INSTANCE_CLASS)clazz) ) { START_TEMPORARY_ROOTS DECLARE_TEMPORARY_ROOT(INSTANCE, object, instantiate((INSTANCE_CLASS)clazz)); if (object != NULL) { /* Put the result back on the stack */ topStackAsType(INSTANCE) = object; /* Will get the result */ /* We now need to call the initializer. We'd like to just * push a second copy of the object onto the stack, and then * do pushFrame(method). But we can't, because that would * not necessarily coincide with the stack map of the * current method. */ pushFrame(RunCustomCodeMethod); pushStackAsType(CustomCodeCallbackFunction, newInstanceReturnObject); pushStackAsType(INSTANCE, object); /* pushFrame may signal a stack overflow. */ pushFrame(method); } else { /* We will already have thrown an appropriate error */ } END_TEMPORARY_ROOTS return; } }
int recur(int x){ count++; get_rbp_2; get_rsp_2; printf("%rbp is %ld, using register local var it's %ld\n",get_rbp(),rbp); printf("%rsp is %ld, using register local var it's %ld\n",get_rsp(),rsp); printf("rbp difference is %ld\n",rbp_init-rbp); printf("rsp difference is %ld\n",rsp_init-rsp); printf("recursion depth is %ld\n",((rsp_init-rsp)/48)); printf("frame number (using rsp) %ld\n",frameCount()); printf("frame number (using rbp) %ld\n",(rbp_init-getFP().rbp)/48); if(x<0){ return -1; } else { return recur(recur(x+x)); } }
bool Mesh::saveStl(const char* filename) const { FILE* fp = fopen(filename,"w+"); if (fp==NULL) return false; std::cout<<"Writing Stl file "<<filename<<std::endl; //bool res = saveStl(fp); fprintf(fp,"solid mesh\n"); for (int i=0;i<nbf();i++) { fprintf(fp,"face normal %f %f %f\n",getFN(i)[0],getFN(i)[1],getFN(i)[2]); fprintf(fp," outer loop\n"); for (int j=0;j<3;j++) { int p = getFP(i)[j]; fprintf(fp," vertex %f %f %f\n",getPP(p)[0],getPP(p)[1],getPP(p)[2]); } fprintf(fp,"endloop\n"); fprintf(fp,"endfacet\n"); } fclose(fp); return true; }
/*========================================================================= * FUNCTION: tVM_Execute * TYPE: public interface * OVERVIEW: execute a basic function * INTERFACE: * parameters: * returns: * the result of the basic function *=======================================================================*/ int tVM_Execute() { int running = 1; u8 bytecode; u8 type; u8 ac_flag; s32 integer; s32 stackindex,index; tVMValue value1,value2,value3; tVMValue retValue;// = (tVMValue*)mem_alloc(sizeof(tVMValue)); /* initialize the running Stack FP */ setFP(FirstFP); /* seek to the entry function */ setFI(0); /* initialize the code reader */ tVM_InitializeCodeReader(); /* execute the byte codes in loop */ while(running) { bytecode = ReadCode(); switch(bytecode) { case C_NOP: break; case C_CONST: { ReadVMValue(&value1); PushVMValue(&value1); break; } case C_LOAD: { ac_flag =ReadAccessFlag(); if(ac_flag == ACCESS_FLAG_GLOBAL) stackindex = ReadIndex(); else stackindex = ReadIndex() + getFP(); LoadVMValue(stackindex,&value1); PushVMValue(&value1); break; } case C_STORE: { ac_flag =ReadAccessFlag(); if(ac_flag == ACCESS_FLAG_GLOBAL) stackindex = ReadIndex(); else stackindex = ReadIndex() + getFP(); PopVMValue(&value1); /* pop the source value */ StoreVMValue(stackindex,&value1); break; } case C_HEAP_LOAD: { type = ReadDataType(); PopVMValue(&value2); /* Pop Addr */ PopVMValue(&value1); /* Pop Base */ tVMValue_HeapLoad(value1.value.ptr_val+value2.value.int_val,type,&value3); /* load the heap memory */ PushVMValue(&value3); /* push the loaded value */ break; } case C_HEAP_STORE: { ptr32 addr; type = ReadDataType(); PopVMValue(&value3); /* Pop Addr */ PopVMValue(&value2); /* Pop Base */ PopVMValue(&value1); /* Pop Value */ addr = (ptr32)(value2.value.ptr_val + value3.value.int_val); if(value1.type != type) { tVMValue_ConvertType(&value1,type); } tVMValue_HeapStore(addr,&value1); break; } case C_FORCE_LOAD: { ac_flag =ReadAccessFlag(); if(ac_flag == ACCESS_FLAG_GLOBAL) stackindex = ReadIndex(); else stackindex = ReadIndex() + getFP(); type = ReadDataType(); ForceLoadVMValue(stackindex,type,&value1); PushVMValue(&value1); break; } case C_ALLOC: { PopVMValue(&value1); value2.type = PtrType; value2.value.ptr_val = (ptr32)mem_alloc(value1.value.int_val); memset(value2.value.ptr_val,0,value1.value.int_val); PushVMValue(&value2); break; } case C_ALLOC_ARRAY: { s32 i; s32 dimension; s32* index_ranges; dimension = ReadInteger(); if(dimension < 1) break; index_ranges = (s32*)mem_alloc(sizeof(s32)*dimension); for(i=0;i<dimension;i++) { PopVMValue(&value1); index_ranges[dimension-i-1] = value1.value.int_val; } value1.type = PtrType; value1.value.ptr_val = tVMValue_HeapAllocMultiArray(dimension,index_ranges,0); PushVMValue(&value1); mem_free(index_ranges); break; } case C_FREE: { PopVMValue(&value1); if(value1.value.ptr_val != NULL) mem_free(value1.value.ptr_val); break; } case C_FREE_ARRAY: { break; } case C_PUSH: { value1.type = ReadDataType(); value1.value.int_val = 0; PushVMValue(&value1); break; } case C_POP: { s32 i; integer = ReadInteger(); for(i=0;i<integer;i++) { PopVMValue(&value1); tVMValue_FreeSelf(&value1); } break; } case C_POP_RESTOP: { s32 i; integer = ReadInteger(); PopVMValue(&value2); /* reserve top value */ for(i=0;i<integer;i++) { PopVMValue(&value1); tVMValue_FreeSelf(&value1); } PushVMValue(&value2); /* push back top value */ break; } case C_CONVERT: { u8 type = (u8)ReadDataType(); PopVMValue(&value1); tVMValue_ConvertType(&value1,type); PushVMValue(&value1); break; } case C_ADD: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Add(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_SUB: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Sub(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_MUL: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Mul(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_DIV: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Div(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_MOD: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Mod(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_OPP: { PopVMValue(&value1); tVMValue_Opp(&value1,&value2); PushVMValue(&value2); tVMValue_FreeSelf(&value1); break; } case C_AND: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_AND(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_OR: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_OR(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_EQ: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_EQ(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_NOT_EQ: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_NOTEQ(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_LT: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_LT(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_LG: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_LG(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_LT_EQ: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_LTEQ(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_LG_EQ: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_LGEQ(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_FJP: { s32 size = ReadIndex(); PopVMValue(&value1); if(value1.value.int_val == 0) /* if it is false */ addIP(size); break; } case C_TJP: { s32 size = ReadIndex(); PopVMValue(&value1); if(value1.value.int_val != 0) /* if it is true */ addIP(size); break; } case C_JMP: { s32 size = ReadIndex(); addIP(size); break; } case C_CALL: { /* read function name */ integer = ReadIndex(); /* push the stack frame */ PushInteger(getIP()); PushInteger(getFI()); PushInteger(getFP()); /* goto the call function code */ tVM_ReleaseCodeReader(); setFI(integer); tVM_InitializeCodeReader(); /* set new FP,RP */ setFP(getSP()); break; } case C_INVOKE: { /* read function name */ index = ReadIndex(); /* execute the native function */ tNativeFunction_Invoke(index); break; } case C_RET: { u32 param_bytes = ReadIndex(); /* get the result of the function */ retValue.type = NullType; PopVMValue(&retValue); /* if this is the start function,then exit the loop */ if(getFP() == FirstFP) { running = 0; /* set flag to stop while */ break; } /* restore last stack frame and return to last function code */ tVM_ReleaseCodeReader(); PopInteger(integer); setFP(integer); PopInteger(integer); setFI(integer); tVM_InitializeCodeReader(); PopInteger(integer); setIP(integer); /* pop the old parameters */ PopBytes(param_bytes); /* push back result of last function */ PushVMValue(&retValue); break; } } } /* close the code reader */ tVM_ReleaseCodeReader(); return 1; }
uint64_t frameCount(){ uint64_t i,fp=getFP().rbp; for(i=0;*(uint64_t*)fp;i++){fp=*(uint64_t*)fp;} return i; }
uint64_t get_main_frame(){ uint64_t *fp; fp=(uint64_t*)getFP().rbp; while(*(uint64_t*)fp){fp=*(uint64_t*)fp;} return fp; }
Frame Frame::getCallerFrame() { int status = 0; assert( lwp_->status() != running ); /* Initialize the unwinder. */ if( getProc()->unwindAddressSpace == NULL ) { // /* DEBUG */ fprintf( stderr, "Creating unwind address space for process pid %d\n", proc->getPid() ); getProc()->unwindAddressSpace = (unw_addr_space *)getDBI()->createUnwindAddressSpace( & _UPT_accessors, 0 ); assert( getProc()->unwindAddressSpace != NULL ); } /* Initialize the thread-specific accessors. */ unsigned lid = lwp_->get_lwp_id(); if( ! getProc()->unwindProcessArgs.defines( lid ) ) { getProc()->unwindProcessArgs[ lid ] = getDBI()->UPTcreate( lid ); assert( getProc()->unwindProcessArgs[ lid ] != NULL ); } /* Generating the synthetic frame above the instrumentation is in cross-platform code. */ Frame currentFrame; if( ! this->hasValidCursor ) { /* DEBUG */ fprintf( stderr, "%s[%d]: no valid cursor in frame, regenerating.\n", __FILE__, __LINE__ ); /* Allocate an unwindCursor for this stackwalk. */ unw_cursor_t * unwindCursor = (unw_cursor_t *)malloc( sizeof( unw_cursor_t ) ); assert( unwindCursor != NULL ); /* Initialize it to the active frame. */ status = getDBI()->initFrame( unwindCursor, getProc()->unwindAddressSpace, getProc()->unwindProcessArgs[ lid ] ); assert( status == 0 ); /* Unwind to the current frame. */ currentFrame = createFrameFromUnwindCursor( unwindCursor, lwp_, lid ); while( ! currentFrame.isUppermost() ) { if( getFP() == currentFrame.getFP() && getSP() == currentFrame.getSP() && getPC() == currentFrame.getPC() ) { currentFrame = createFrameFromUnwindCursor( unwindCursor, lwp_, lid ); break; } /* end if we've found this frame */ currentFrame = createFrameFromUnwindCursor( unwindCursor, lwp_, lid ); } /* createFrameFromUnwindCursor() copies the unwind cursor into the Frame it returns. */ free( unwindCursor ); } /* end if this frame was copied before being unwound. */ else { /* Don't try to walk off the end of the stack. */ assert( ! this->uppermost_ ); /* Allocate an unwindCursor for this stackwalk. */ unw_cursor_t * unwindCursor = (unw_cursor_t *)malloc( sizeof( unw_cursor_t ) ); assert( unwindCursor != NULL ); /* Initialize it to this frame. */ * unwindCursor = this->unwindCursor; /* Unwind the cursor to the caller's frame. */ int status = getDBI()->stepFrameUp( unwindCursor ); /* We unwound from this frame once before to get its FP. */ assert( status > 0 ); /* Create a Frame from the unwound cursor. */ currentFrame = createFrameFromUnwindCursor( unwindCursor, lwp_, lid ); /* createFrameFromUnwindCursor() copies the unwind cursor into the Frame it returns. */ free( unwindCursor ); } /* end if this frame was _not_ copied before being unwound. */ /* Make sure we made progress. */ if( getFP() == currentFrame.getFP() && getSP() == currentFrame.getSP() && getPC() == currentFrame.getPC() ) { /* This will forcibly terminate the stack walk. */ currentFrame.fp_ = (Address)NULL; currentFrame.pc_ = (Address)NULL; currentFrame.sp_ = (Address)NULL; currentFrame.uppermost_ = false; fprintf( stderr, "%s[%d]: detected duplicate stack frame, aborting stack with zeroed frame.\n", __FILE__, __LINE__ ); } if( thread_ != NULL ) { currentFrame.thread_ = thread_; } /* Return the result. */ return currentFrame; } /* end getCallerFrame() */