franklin@uvicctr.UVic.CA.UUCP (Katherine Franklin) (03/03/90)
I am writing an MPW Pascal tool, and I have written an invariant check function. What I would like to do is call is this function many times throughout the program and each time the invariant *doesn't* hold is to write a message to the console. I would like an easily maintainable way of telling the user which call to the invariant function failed. I don't want to hard code a label for them, because that makes them very hard to maintain if I insert another function call in my program. Unix C has a macro variable that allows you to pass the current line number in the source code on to the object code to report during execution. I would like to do this, or some other dynamic way. Is this possible ? if so, how ? Thanks, Katherine ------ Katherine Franklin franklin@uvicctr.uvic.ca University of Victoria franklin@uvunix.bitnet Victoria, BC, Canada
keith@Apple.COM (Keith Rollin) (03/05/90)
In article <939@uvicctr.UVic.CA.UUCP> franklin@uvicctr.UUCP (Katherine Franklin) writes: >I am writing an MPW Pascal tool, and I have written an invariant check >function. What I would like to do is call is this function many times >throughout the program and each time the invariant *doesn't* hold is to >write a message to the console. > >I would like an easily maintainable way of telling the user which call >to the invariant function failed. I don't want to hard code a label for them, >because that makes them very hard to maintain if I insert another function >call in my program. > >Unix C has a macro variable that allows you to pass the current line number >in the source code on to the object code to report during execution. I would >like to do this, or some other dynamic way. > >Is this possible ? if so, how ? > > Thanks, > Katherine > >------ >Katherine Franklin franklin@uvicctr.uvic.ca >University of Victoria franklin@uvunix.bitnet >Victoria, BC, Canada Katherine, I don't know if there is a way to get the actual line number an instruction came from in MPW Pascal. However, there is a kludge that will give you the procedure name as long as you have compiled your program with Debugger names turned on. MacApp uses the following routine in its own debugger to determine where errors occured, or for monitoring tracing: PROCEDURE GetProcName(ppc: Longint; VAR className, procName: MAName); { GetProcName returns the name of the procedure or function in which ppc points. If it is in a method, then it return's the name of the method's class in className. } VAR pc, nextPC, limit: Ptr; index: INTEGER; BEGIN pc := Handle(ppc)^; IF (ord(pc) <> 0) & NOT Odd(ord(pc)) THEN BEGIN limit := Ptr(ord(pc) + 32767); WHILE (endOfModule(pc, limit, @procName, nextPC) = NIL) DO BEGIN IF ord(pc) >= ord(limit) THEN BEGIN className := ''; procName := ''; LEAVE; END ELSE pc := Ptr(ord(pc) + 2); END; index := pos('.', procName); IF index <> 0 THEN BEGIN className := copy(procName, 1, index - 1); END ELSE className := ''; END ELSE BEGIN className := ''; procName := ''; END; END; ("endofmodule" is in the DisAsm library that comes with MPW.) From your assertion routine, call this routine with a pointer to the return address of the routine that called your assertion. You can get this return address with something like the following expression: Ord4(GetCurStackFramePtr) + 4 Where "GetCurStackFramPtr" is an inline procedure that looks like: FUNCTION GetCurStackFramePtr: Ptr; INLINE $2E8E; { MOVE.L A6,(A7) } Hope this helps, -- ------------------------------------------------------------------------------ Keith Rollin --- Apple Computer, Inc. --- Developer Technical Support INTERNET: keith@apple.com UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith "Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions